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