1 //===-- MachineSink.cpp - Sinking for machine instructions ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass 11 // 12 //===----------------------------------------------------------------------===// 13 14 #define DEBUG_TYPE "machine-sink" 15 #include "llvm/CodeGen/Passes.h" 16 #include "llvm/CodeGen/MachineRegisterInfo.h" 17 #include "llvm/CodeGen/MachineDominators.h" 18 #include "llvm/Target/MRegisterInfo.h" 19 #include "llvm/Target/TargetInstrInfo.h" 20 #include "llvm/Target/TargetMachine.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Support/Compiler.h" 24 #include "llvm/Support/Debug.h" 25 using namespace llvm; 26 27 STATISTIC(NumSunk, "Number of machine instructions sunk"); 28 29 namespace { 30 class VISIBILITY_HIDDEN MachineSinking : public MachineFunctionPass { 31 const TargetMachine *TM; 32 const TargetInstrInfo *TII; 33 MachineFunction *CurMF; // Current MachineFunction 34 MachineRegisterInfo *RegInfo; // Machine register information 35 MachineDominatorTree *DT; // Machine dominator tree for the current Loop 36 37 public: 38 static char ID; // Pass identification 39 MachineSinking() : MachineFunctionPass((intptr_t)&ID) {} 40 41 virtual bool runOnMachineFunction(MachineFunction &MF); 42 43 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 44 MachineFunctionPass::getAnalysisUsage(AU); 45 AU.addRequired<MachineDominatorTree>(); 46 AU.addPreserved<MachineDominatorTree>(); 47 } 48 private: 49 bool ProcessBlock(MachineBasicBlock &MBB); 50 bool SinkInstruction(MachineInstr *MI); 51 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const; 52 }; 53 54 char MachineSinking::ID = 0; 55 RegisterPass<MachineSinking> X("machine-sink", "Machine code sinking"); 56 } // end anonymous namespace 57 58 FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); } 59 60 /// AllUsesDominatedByBlock - Return true if all uses of the specified register 61 /// occur in blocks dominated by the specified block. 62 bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg, 63 MachineBasicBlock *MBB) const { 64 assert(MRegisterInfo::isVirtualRegister(Reg) && "Only makes sense for vregs"); 65 for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg), 66 E = RegInfo->reg_end(); I != E; ++I) { 67 if (I.getOperand().isDef()) continue; // ignore def. 68 69 // Determine the block of the use. 70 MachineInstr *UseInst = &*I; 71 MachineBasicBlock *UseBlock = UseInst->getParent(); 72 if (UseInst->getOpcode() == TargetInstrInfo::PHI) { 73 // PHI nodes use the operand in the predecessor block, not the block with 74 // the PHI. 75 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB(); 76 } 77 // Check that it dominates. 78 if (!DT->dominates(MBB, UseBlock)) 79 return false; 80 } 81 return true; 82 } 83 84 85 86 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { 87 DOUT << "******** Machine Sinking ********\n"; 88 89 CurMF = &MF; 90 TM = &CurMF->getTarget(); 91 TII = TM->getInstrInfo(); 92 RegInfo = &CurMF->getRegInfo(); 93 DT = &getAnalysis<MachineDominatorTree>(); 94 95 bool EverMadeChange = false; 96 97 while (1) { 98 bool MadeChange = false; 99 100 // Process all basic blocks. 101 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end(); 102 I != E; ++I) 103 MadeChange |= ProcessBlock(*I); 104 105 // If this iteration over the code changed anything, keep iterating. 106 if (!MadeChange) break; 107 EverMadeChange = true; 108 } 109 return EverMadeChange; 110 } 111 112 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { 113 bool MadeChange = false; 114 115 // Can't sink anything out of a block that has less than two successors. 116 if (MBB.succ_size() <= 1) return false; 117 118 // Walk the basic block bottom-up 119 for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){ 120 MachineBasicBlock::iterator LastIt = I; 121 if (SinkInstruction(--I)) { 122 I = LastIt; 123 ++NumSunk; 124 } 125 } 126 127 return MadeChange; 128 } 129 130 /// SinkInstruction - Determine whether it is safe to sink the specified machine 131 /// instruction out of its current block into a successor. 132 bool MachineSinking::SinkInstruction(MachineInstr *MI) { 133 // Don't sink things with side-effects we don't understand. 134 if (TII->hasUnmodelledSideEffects(MI)) 135 return false; 136 137 // Loop over all the operands of the specified instruction. If there is 138 // anything we can't handle, bail out. 139 MachineBasicBlock *ParentBlock = MI->getParent(); 140 141 // SuccToSinkTo - This is the successor to sink this instruction to, once we 142 // decide. 143 MachineBasicBlock *SuccToSinkTo = 0; 144 145 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 146 const MachineOperand &MO = MI->getOperand(i); 147 if (!MO.isReg()) continue; // Ignore non-register operands. 148 149 unsigned Reg = MO.getReg(); 150 if (Reg == 0) continue; 151 152 if (MRegisterInfo::isPhysicalRegister(Reg)) { 153 // If this is a physical register use, we can't move it. If it is a def, 154 // we can move it, but only if the def is dead. 155 if (MO.isUse() || !MO.isDead()) 156 return false; 157 } else { 158 // Virtual register uses are always safe to sink. 159 if (MO.isUse()) continue; 160 161 // Virtual register defs can only be sunk if all their uses are in blocks 162 // dominated by one of the successors. 163 if (SuccToSinkTo) { 164 // If a previous operand picked a block to sink to, then this operand 165 // must be sinkable to the same block. 166 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo)) 167 return false; 168 continue; 169 } 170 171 // Otherwise, we should look at all the successors and decide which one 172 // we should sink to. 173 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(), 174 E = ParentBlock->succ_end(); SI != E; ++SI) { 175 if (AllUsesDominatedByBlock(Reg, *SI)) { 176 SuccToSinkTo = *SI; 177 break; 178 } 179 } 180 181 // If we couldn't find a block to sink to, ignore this instruction. 182 if (SuccToSinkTo == 0) 183 return false; 184 } 185 } 186 187 // If there are no outputs, it must have side-effects. 188 if (SuccToSinkTo == 0) 189 return false; 190 191 // FIXME: Check that the instr doesn't have side effects etc. 192 193 DEBUG(cerr << "Sink instr " << *MI); 194 DEBUG(cerr << "to block " << *SuccToSinkTo); 195 196 // If the block has multiple predecessors, this would introduce computation on 197 // a path that it doesn't already exist. We could split the critical edge, 198 // but for now we just punt. 199 if (SuccToSinkTo->pred_size() > 1) { 200 DEBUG(cerr << " *** PUNTING: Critical edge found\n"); 201 return false; 202 } 203 204 // Determine where to insert into. Skip phi nodes. 205 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); 206 while (InsertPos != SuccToSinkTo->end() && 207 InsertPos->getOpcode() == TargetInstrInfo::PHI) 208 ++InsertPos; 209 210 // Move the instruction. 211 SuccToSinkTo->splice(InsertPos, ParentBlock, MI, 212 ++MachineBasicBlock::iterator(MI)); 213 return true; 214 } 215