1 //===-- Sink.cpp - Code Sinking -------------------------------------------===// 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 moves instructions into successor blocks, when possible, so that 11 // they aren't executed on paths where their results aren't needed. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Scalar.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/Analysis/AliasAnalysis.h" 18 #include "llvm/Analysis/LoopInfo.h" 19 #include "llvm/Analysis/ValueTracking.h" 20 #include "llvm/IR/CFG.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/Dominators.h" 23 #include "llvm/IR/IntrinsicInst.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/raw_ostream.h" 27 using namespace llvm; 28 29 #define DEBUG_TYPE "sink" 30 31 STATISTIC(NumSunk, "Number of instructions sunk"); 32 STATISTIC(NumSinkIter, "Number of sinking iterations"); 33 34 namespace { 35 class Sinking : public FunctionPass { 36 DominatorTree *DT; 37 LoopInfo *LI; 38 AliasAnalysis *AA; 39 const DataLayout *DL; 40 41 public: 42 static char ID; // Pass identification 43 Sinking() : FunctionPass(ID) { 44 initializeSinkingPass(*PassRegistry::getPassRegistry()); 45 } 46 47 bool runOnFunction(Function &F) override; 48 49 void getAnalysisUsage(AnalysisUsage &AU) const override { 50 AU.setPreservesCFG(); 51 FunctionPass::getAnalysisUsage(AU); 52 AU.addRequired<AliasAnalysis>(); 53 AU.addRequired<DominatorTreeWrapperPass>(); 54 AU.addRequired<LoopInfoWrapperPass>(); 55 AU.addPreserved<DominatorTreeWrapperPass>(); 56 AU.addPreserved<LoopInfoWrapperPass>(); 57 } 58 private: 59 bool ProcessBlock(BasicBlock &BB); 60 bool SinkInstruction(Instruction *I, SmallPtrSetImpl<Instruction*> &Stores); 61 bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB) const; 62 bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo) const; 63 }; 64 } // end anonymous namespace 65 66 char Sinking::ID = 0; 67 INITIALIZE_PASS_BEGIN(Sinking, "sink", "Code sinking", false, false) 68 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 69 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 70 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 71 INITIALIZE_PASS_END(Sinking, "sink", "Code sinking", false, false) 72 73 FunctionPass *llvm::createSinkingPass() { return new Sinking(); } 74 75 /// AllUsesDominatedByBlock - Return true if all uses of the specified value 76 /// occur in blocks dominated by the specified block. 77 bool Sinking::AllUsesDominatedByBlock(Instruction *Inst, 78 BasicBlock *BB) const { 79 // Ignoring debug uses is necessary so debug info doesn't affect the code. 80 // This may leave a referencing dbg_value in the original block, before 81 // the definition of the vreg. Dwarf generator handles this although the 82 // user might not get the right info at runtime. 83 for (Use &U : Inst->uses()) { 84 // Determine the block of the use. 85 Instruction *UseInst = cast<Instruction>(U.getUser()); 86 BasicBlock *UseBlock = UseInst->getParent(); 87 if (PHINode *PN = dyn_cast<PHINode>(UseInst)) { 88 // PHI nodes use the operand in the predecessor block, not the block with 89 // the PHI. 90 unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo()); 91 UseBlock = PN->getIncomingBlock(Num); 92 } 93 // Check that it dominates. 94 if (!DT->dominates(BB, UseBlock)) 95 return false; 96 } 97 return true; 98 } 99 100 bool Sinking::runOnFunction(Function &F) { 101 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 102 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 103 AA = &getAnalysis<AliasAnalysis>(); 104 DL = &F.getParent()->getDataLayout(); 105 106 bool MadeChange, EverMadeChange = false; 107 108 do { 109 MadeChange = false; 110 DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n"); 111 // Process all basic blocks. 112 for (Function::iterator I = F.begin(), E = F.end(); 113 I != E; ++I) 114 MadeChange |= ProcessBlock(*I); 115 EverMadeChange |= MadeChange; 116 NumSinkIter++; 117 } while (MadeChange); 118 119 return EverMadeChange; 120 } 121 122 bool Sinking::ProcessBlock(BasicBlock &BB) { 123 // Can't sink anything out of a block that has less than two successors. 124 if (BB.getTerminator()->getNumSuccessors() <= 1 || BB.empty()) return false; 125 126 // Don't bother sinking code out of unreachable blocks. In addition to being 127 // unprofitable, it can also lead to infinite looping, because in an 128 // unreachable loop there may be nowhere to stop. 129 if (!DT->isReachableFromEntry(&BB)) return false; 130 131 bool MadeChange = false; 132 133 // Walk the basic block bottom-up. Remember if we saw a store. 134 BasicBlock::iterator I = BB.end(); 135 --I; 136 bool ProcessedBegin = false; 137 SmallPtrSet<Instruction *, 8> Stores; 138 do { 139 Instruction *Inst = I; // The instruction to sink. 140 141 // Predecrement I (if it's not begin) so that it isn't invalidated by 142 // sinking. 143 ProcessedBegin = I == BB.begin(); 144 if (!ProcessedBegin) 145 --I; 146 147 if (isa<DbgInfoIntrinsic>(Inst)) 148 continue; 149 150 if (SinkInstruction(Inst, Stores)) 151 ++NumSunk, MadeChange = true; 152 153 // If we just processed the first instruction in the block, we're done. 154 } while (!ProcessedBegin); 155 156 return MadeChange; 157 } 158 159 static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA, 160 SmallPtrSetImpl<Instruction *> &Stores) { 161 162 if (Inst->mayWriteToMemory()) { 163 Stores.insert(Inst); 164 return false; 165 } 166 167 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) { 168 AliasAnalysis::Location Loc = AA->getLocation(L); 169 for (Instruction *S : Stores) 170 if (AA->getModRefInfo(S, Loc) & AliasAnalysis::Mod) 171 return false; 172 } 173 174 if (isa<TerminatorInst>(Inst) || isa<PHINode>(Inst)) 175 return false; 176 177 return true; 178 } 179 180 /// IsAcceptableTarget - Return true if it is possible to sink the instruction 181 /// in the specified basic block. 182 bool Sinking::IsAcceptableTarget(Instruction *Inst, 183 BasicBlock *SuccToSinkTo) const { 184 assert(Inst && "Instruction to be sunk is null"); 185 assert(SuccToSinkTo && "Candidate sink target is null"); 186 187 // It is not possible to sink an instruction into its own block. This can 188 // happen with loops. 189 if (Inst->getParent() == SuccToSinkTo) 190 return false; 191 192 // If the block has multiple predecessors, this would introduce computation 193 // on different code paths. We could split the critical edge, but for now we 194 // just punt. 195 // FIXME: Split critical edges if not backedges. 196 if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) { 197 // We cannot sink a load across a critical edge - there may be stores in 198 // other code paths. 199 if (!isSafeToSpeculativelyExecute(Inst, DL)) 200 return false; 201 202 // We don't want to sink across a critical edge if we don't dominate the 203 // successor. We could be introducing calculations to new code paths. 204 if (!DT->dominates(Inst->getParent(), SuccToSinkTo)) 205 return false; 206 207 // Don't sink instructions into a loop. 208 Loop *succ = LI->getLoopFor(SuccToSinkTo); 209 Loop *cur = LI->getLoopFor(Inst->getParent()); 210 if (succ != nullptr && succ != cur) 211 return false; 212 } 213 214 // Finally, check that all the uses of the instruction are actually 215 // dominated by the candidate 216 return AllUsesDominatedByBlock(Inst, SuccToSinkTo); 217 } 218 219 /// SinkInstruction - Determine whether it is safe to sink the specified machine 220 /// instruction out of its current block into a successor. 221 bool Sinking::SinkInstruction(Instruction *Inst, 222 SmallPtrSetImpl<Instruction *> &Stores) { 223 224 // Don't sink static alloca instructions. CodeGen assumes allocas outside the 225 // entry block are dynamically sized stack objects. 226 if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) 227 if (AI->isStaticAlloca()) 228 return false; 229 230 // Check if it's safe to move the instruction. 231 if (!isSafeToMove(Inst, AA, Stores)) 232 return false; 233 234 // FIXME: This should include support for sinking instructions within the 235 // block they are currently in to shorten the live ranges. We often get 236 // instructions sunk into the top of a large block, but it would be better to 237 // also sink them down before their first use in the block. This xform has to 238 // be careful not to *increase* register pressure though, e.g. sinking 239 // "x = y + z" down if it kills y and z would increase the live ranges of y 240 // and z and only shrink the live range of x. 241 242 // SuccToSinkTo - This is the successor to sink this instruction to, once we 243 // decide. 244 BasicBlock *SuccToSinkTo = nullptr; 245 246 // Instructions can only be sunk if all their uses are in blocks 247 // dominated by one of the successors. 248 // Look at all the postdominators and see if we can sink it in one. 249 DomTreeNode *DTN = DT->getNode(Inst->getParent()); 250 for (DomTreeNode::iterator I = DTN->begin(), E = DTN->end(); 251 I != E && SuccToSinkTo == nullptr; ++I) { 252 BasicBlock *Candidate = (*I)->getBlock(); 253 if ((*I)->getIDom()->getBlock() == Inst->getParent() && 254 IsAcceptableTarget(Inst, Candidate)) 255 SuccToSinkTo = Candidate; 256 } 257 258 // If no suitable postdominator was found, look at all the successors and 259 // decide which one we should sink to, if any. 260 for (succ_iterator I = succ_begin(Inst->getParent()), 261 E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) { 262 if (IsAcceptableTarget(Inst, *I)) 263 SuccToSinkTo = *I; 264 } 265 266 // If we couldn't find a block to sink to, ignore this instruction. 267 if (!SuccToSinkTo) 268 return false; 269 270 DEBUG(dbgs() << "Sink" << *Inst << " ("; 271 Inst->getParent()->printAsOperand(dbgs(), false); 272 dbgs() << " -> "; 273 SuccToSinkTo->printAsOperand(dbgs(), false); 274 dbgs() << ")\n"); 275 276 // Move the instruction. 277 Inst->moveBefore(SuccToSinkTo->getFirstInsertionPt()); 278 return true; 279 } 280