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