MachineSink.cpp revision 6f0d024a534af18d9e60b3ea757376cd8a3a980e
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/TargetRegisterInfo.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" 25using namespace llvm; 26 27STATISTIC(NumSunk, "Number of machine instructions sunk"); 28 29namespace { 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, bool &SawStore); 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 58FunctionPass *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. 62bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg, 63 MachineBasicBlock *MBB) const { 64 assert(TargetRegisterInfo::isVirtualRegister(Reg) && 65 "Only makes sense for vregs"); 66 for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg), 67 E = RegInfo->reg_end(); I != E; ++I) { 68 if (I.getOperand().isDef()) continue; // ignore def. 69 70 // Determine the block of the use. 71 MachineInstr *UseInst = &*I; 72 MachineBasicBlock *UseBlock = UseInst->getParent(); 73 if (UseInst->getOpcode() == TargetInstrInfo::PHI) { 74 // PHI nodes use the operand in the predecessor block, not the block with 75 // the PHI. 76 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB(); 77 } 78 // Check that it dominates. 79 if (!DT->dominates(MBB, UseBlock)) 80 return false; 81 } 82 return true; 83} 84 85 86 87bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { 88 DOUT << "******** Machine Sinking ********\n"; 89 90 CurMF = &MF; 91 TM = &CurMF->getTarget(); 92 TII = TM->getInstrInfo(); 93 RegInfo = &CurMF->getRegInfo(); 94 DT = &getAnalysis<MachineDominatorTree>(); 95 96 bool EverMadeChange = false; 97 98 while (1) { 99 bool MadeChange = false; 100 101 // Process all basic blocks. 102 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end(); 103 I != E; ++I) 104 MadeChange |= ProcessBlock(*I); 105 106 // If this iteration over the code changed anything, keep iterating. 107 if (!MadeChange) break; 108 EverMadeChange = true; 109 } 110 return EverMadeChange; 111} 112 113bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { 114 bool MadeChange = false; 115 116 // Can't sink anything out of a block that has less than two successors. 117 if (MBB.succ_size() <= 1) return false; 118 119 // Walk the basic block bottom-up. Remember if we saw a store. 120 bool SawStore = false; 121 for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){ 122 MachineBasicBlock::iterator LastIt = I; 123 if (SinkInstruction(--I, SawStore)) { 124 I = LastIt; 125 ++NumSunk; 126 } 127 } 128 129 return MadeChange; 130} 131 132/// SinkInstruction - Determine whether it is safe to sink the specified machine 133/// instruction out of its current block into a successor. 134bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) { 135 const TargetInstrDesc &TID = MI->getDesc(); 136 137 // Ignore stuff that we obviously can't sink. 138 if (TID.mayStore() || TID.isCall()) { 139 SawStore = true; 140 return false; 141 } 142 if (TID.isReturn() || TID.isBranch() || TID.hasUnmodeledSideEffects()) 143 return false; 144 145 // See if this instruction does a load. If so, we have to guarantee that the 146 // loaded value doesn't change between the load and the end of block. The 147 // check for isInvariantLoad gives the targe the chance to classify the load 148 // as always returning a constant, e.g. a constant pool load. 149 if (TID.mayLoad() && !TII->isInvariantLoad(MI)) { 150 // Otherwise, this is a real load. If there is a store between the load and 151 // end of block, we can't sink the load. 152 // 153 // FIXME: we can't do this transformation until we know that the load is 154 // not volatile, and machineinstrs don't keep this info. :( 155 // 156 //if (SawStore) 157 return false; 158 } 159 160 // FIXME: This should include support for sinking instructions within the 161 // block they are currently in to shorten the live ranges. We often get 162 // instructions sunk into the top of a large block, but it would be better to 163 // also sink them down before their first use in the block. This xform has to 164 // be careful not to *increase* register pressure though, e.g. sinking 165 // "x = y + z" down if it kills y and z would increase the live ranges of y 166 // and z only the shrink the live range of x. 167 168 // Loop over all the operands of the specified instruction. If there is 169 // anything we can't handle, bail out. 170 MachineBasicBlock *ParentBlock = MI->getParent(); 171 172 // SuccToSinkTo - This is the successor to sink this instruction to, once we 173 // decide. 174 MachineBasicBlock *SuccToSinkTo = 0; 175 176 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 177 const MachineOperand &MO = MI->getOperand(i); 178 if (!MO.isReg()) continue; // Ignore non-register operands. 179 180 unsigned Reg = MO.getReg(); 181 if (Reg == 0) continue; 182 183 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 184 // If this is a physical register use, we can't move it. If it is a def, 185 // we can move it, but only if the def is dead. 186 if (MO.isUse() || !MO.isDead()) 187 return false; 188 } else { 189 // Virtual register uses are always safe to sink. 190 if (MO.isUse()) continue; 191 192 // FIXME: This picks a successor to sink into based on having one 193 // successor that dominates all the uses. However, there are cases where 194 // sinking can happen but where the sink point isn't a successor. For 195 // example: 196 // x = computation 197 // if () {} else {} 198 // use x 199 // the instruction could be sunk over the whole diamond for the 200 // if/then/else (or loop, etc), allowing it to be sunk into other blocks 201 // after that. 202 203 // Virtual register defs can only be sunk if all their uses are in blocks 204 // dominated by one of the successors. 205 if (SuccToSinkTo) { 206 // If a previous operand picked a block to sink to, then this operand 207 // must be sinkable to the same block. 208 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo)) 209 return false; 210 continue; 211 } 212 213 // Otherwise, we should look at all the successors and decide which one 214 // we should sink to. 215 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(), 216 E = ParentBlock->succ_end(); SI != E; ++SI) { 217 if (AllUsesDominatedByBlock(Reg, *SI)) { 218 SuccToSinkTo = *SI; 219 break; 220 } 221 } 222 223 // If we couldn't find a block to sink to, ignore this instruction. 224 if (SuccToSinkTo == 0) 225 return false; 226 } 227 } 228 229 // If there are no outputs, it must have side-effects. 230 if (SuccToSinkTo == 0) 231 return false; 232 233 DEBUG(cerr << "Sink instr " << *MI); 234 DEBUG(cerr << "to block " << *SuccToSinkTo); 235 236 // If the block has multiple predecessors, this would introduce computation on 237 // a path that it doesn't already exist. We could split the critical edge, 238 // but for now we just punt. 239 // FIXME: Split critical edges if not backedges. 240 if (SuccToSinkTo->pred_size() > 1) { 241 DEBUG(cerr << " *** PUNTING: Critical edge found\n"); 242 return false; 243 } 244 245 // Determine where to insert into. Skip phi nodes. 246 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); 247 while (InsertPos != SuccToSinkTo->end() && 248 InsertPos->getOpcode() == TargetInstrInfo::PHI) 249 ++InsertPos; 250 251 // Move the instruction. 252 SuccToSinkTo->splice(InsertPos, ParentBlock, MI, 253 ++MachineBasicBlock::iterator(MI)); 254 return true; 255} 256