LLVM API Documentation

MachineSink.cpp

Go to the documentation of this file.
00001 //===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass 
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "machine-sink"
00015 #include "llvm/CodeGen/Passes.h"
00016 #include "llvm/CodeGen/MachineRegisterInfo.h"
00017 #include "llvm/CodeGen/MachineDominators.h"
00018 #include "llvm/Target/TargetRegisterInfo.h"
00019 #include "llvm/Target/TargetInstrInfo.h"
00020 #include "llvm/Target/TargetMachine.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Support/Compiler.h"
00023 #include "llvm/Support/Debug.h"
00024 using namespace llvm;
00025 
00026 STATISTIC(NumSunk, "Number of machine instructions sunk");
00027 
00028 namespace {
00029   class VISIBILITY_HIDDEN MachineSinking : public MachineFunctionPass {
00030     const TargetMachine   *TM;
00031     const TargetInstrInfo *TII;
00032     MachineFunction       *CurMF; // Current MachineFunction
00033     MachineRegisterInfo  *RegInfo; // Machine register information
00034     MachineDominatorTree *DT;   // Machine dominator tree for the current Loop
00035 
00036   public:
00037     static char ID; // Pass identification
00038     MachineSinking() : MachineFunctionPass(&ID) {}
00039     
00040     virtual bool runOnMachineFunction(MachineFunction &MF);
00041     
00042     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00043       MachineFunctionPass::getAnalysisUsage(AU);
00044       AU.addRequired<MachineDominatorTree>();
00045       AU.addPreserved<MachineDominatorTree>();
00046     }
00047   private:
00048     bool ProcessBlock(MachineBasicBlock &MBB);
00049     bool SinkInstruction(MachineInstr *MI, bool &SawStore);
00050     bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
00051   };
00052 } // end anonymous namespace
00053   
00054 char MachineSinking::ID = 0;
00055 static RegisterPass<MachineSinking>
00056 X("machine-sink", "Machine code sinking");
00057 
00058 FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
00059 
00060 /// AllUsesDominatedByBlock - Return true if all uses of the specified register
00061 /// occur in blocks dominated by the specified block.
00062 bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg, 
00063                                              MachineBasicBlock *MBB) const {
00064   assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
00065          "Only makes sense for vregs");
00066   for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
00067        E = RegInfo->reg_end(); I != E; ++I) {
00068     if (I.getOperand().isDef()) continue;  // ignore def.
00069     
00070     // Determine the block of the use.
00071     MachineInstr *UseInst = &*I;
00072     MachineBasicBlock *UseBlock = UseInst->getParent();
00073     if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
00074       // PHI nodes use the operand in the predecessor block, not the block with
00075       // the PHI.
00076       UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
00077     }
00078     // Check that it dominates.
00079     if (!DT->dominates(MBB, UseBlock))
00080       return false;
00081   }
00082   return true;
00083 }
00084 
00085 
00086 
00087 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
00088   DOUT << "******** Machine Sinking ********\n";
00089   
00090   CurMF = &MF;
00091   TM = &CurMF->getTarget();
00092   TII = TM->getInstrInfo();
00093   RegInfo = &CurMF->getRegInfo();
00094   DT = &getAnalysis<MachineDominatorTree>();
00095 
00096   bool EverMadeChange = false;
00097   
00098   while (1) {
00099     bool MadeChange = false;
00100 
00101     // Process all basic blocks.
00102     for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end(); 
00103          I != E; ++I)
00104       MadeChange |= ProcessBlock(*I);
00105     
00106     // If this iteration over the code changed anything, keep iterating.
00107     if (!MadeChange) break;
00108     EverMadeChange = true;
00109   } 
00110   return EverMadeChange;
00111 }
00112 
00113 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
00114   bool MadeChange = false;
00115   
00116   // Can't sink anything out of a block that has less than two successors.
00117   if (MBB.succ_size() <= 1) return false;
00118   
00119   // Walk the basic block bottom-up.  Remember if we saw a store.
00120   bool SawStore = false;
00121   for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
00122     MachineBasicBlock::iterator LastIt = I;
00123     if (SinkInstruction(--I, SawStore)) {
00124       I = LastIt;
00125       ++NumSunk;
00126     }
00127   }
00128   
00129   return MadeChange;
00130 }
00131 
00132 /// SinkInstruction - Determine whether it is safe to sink the specified machine
00133 /// instruction out of its current block into a successor.
00134 bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
00135   // Check if it's safe to move the instruction.
00136   if (!MI->isSafeToMove(TII, SawStore))
00137     return false;
00138   
00139   // FIXME: This should include support for sinking instructions within the
00140   // block they are currently in to shorten the live ranges.  We often get
00141   // instructions sunk into the top of a large block, but it would be better to
00142   // also sink them down before their first use in the block.  This xform has to
00143   // be careful not to *increase* register pressure though, e.g. sinking
00144   // "x = y + z" down if it kills y and z would increase the live ranges of y
00145   // and z only the shrink the live range of x.
00146   
00147   // Loop over all the operands of the specified instruction.  If there is
00148   // anything we can't handle, bail out.
00149   MachineBasicBlock *ParentBlock = MI->getParent();
00150   
00151   // SuccToSinkTo - This is the successor to sink this instruction to, once we
00152   // decide.
00153   MachineBasicBlock *SuccToSinkTo = 0;
00154   
00155   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00156     const MachineOperand &MO = MI->getOperand(i);
00157     if (!MO.isReg()) continue;  // Ignore non-register operands.
00158     
00159     unsigned Reg = MO.getReg();
00160     if (Reg == 0) continue;
00161     
00162     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
00163       // If this is a physical register use, we can't move it.  If it is a def,
00164       // we can move it, but only if the def is dead.
00165       if (MO.isUse() || !MO.isDead())
00166         return false;
00167     } else {
00168       // Virtual register uses are always safe to sink.
00169       if (MO.isUse()) continue;
00170       
00171       // FIXME: This picks a successor to sink into based on having one
00172       // successor that dominates all the uses.  However, there are cases where
00173       // sinking can happen but where the sink point isn't a successor.  For
00174       // example:
00175       //   x = computation
00176       //   if () {} else {}
00177       //   use x
00178       // the instruction could be sunk over the whole diamond for the 
00179       // if/then/else (or loop, etc), allowing it to be sunk into other blocks
00180       // after that.
00181       
00182       // Virtual register defs can only be sunk if all their uses are in blocks
00183       // dominated by one of the successors.
00184       if (SuccToSinkTo) {
00185         // If a previous operand picked a block to sink to, then this operand
00186         // must be sinkable to the same block.
00187         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo)) 
00188           return false;
00189         continue;
00190       }
00191       
00192       // Otherwise, we should look at all the successors and decide which one
00193       // we should sink to.
00194       for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
00195            E = ParentBlock->succ_end(); SI != E; ++SI) {
00196         if (AllUsesDominatedByBlock(Reg, *SI)) {
00197           SuccToSinkTo = *SI;
00198           break;
00199         }
00200       }
00201       
00202       // If we couldn't find a block to sink to, ignore this instruction.
00203       if (SuccToSinkTo == 0)
00204         return false;
00205     }
00206   }
00207   
00208   // If there are no outputs, it must have side-effects.
00209   if (SuccToSinkTo == 0)
00210     return false;
00211 
00212   // It's not safe to sink instructions to EH landing pad. Control flow into
00213   // landing pad is implicitly defined.
00214   if (SuccToSinkTo->isLandingPad())
00215     return false;
00216   
00217   DEBUG(cerr << "Sink instr " << *MI);
00218   DEBUG(cerr << "to block " << *SuccToSinkTo);
00219   
00220   // If the block has multiple predecessors, this would introduce computation on
00221   // a path that it doesn't already exist.  We could split the critical edge,
00222   // but for now we just punt.
00223   // FIXME: Split critical edges if not backedges.
00224   if (SuccToSinkTo->pred_size() > 1) {
00225     DEBUG(cerr << " *** PUNTING: Critical edge found\n");
00226     return false;
00227   }
00228   
00229   // Determine where to insert into.  Skip phi nodes.
00230   MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
00231   while (InsertPos != SuccToSinkTo->end() && 
00232          InsertPos->getOpcode() == TargetInstrInfo::PHI)
00233     ++InsertPos;
00234   
00235   // Move the instruction.
00236   SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
00237                        ++MachineBasicBlock::iterator(MI));
00238   return true;
00239 }