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