LLVM  3.7.0
Sink.cpp
Go to the documentation of this file.
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"
18 #include "llvm/Analysis/LoopInfo.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"
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
42  Sinking() : FunctionPass(ID) {
44  }
45 
46  bool runOnFunction(Function &F) override;
47 
48  void getAnalysisUsage(AnalysisUsage &AU) const override {
49  AU.setPreservesCFG();
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)
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.
76 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 
99 bool Sinking::runOnFunction(Function &F) {
100  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
101  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
102  AA = &getAnalysis<AliasAnalysis>();
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 
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 || BB.empty()) 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;
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 
157 static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA,
159 
160  if (Inst->mayWriteToMemory()) {
161  Stores.insert(Inst);
162  return false;
163  }
164 
165  if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
167  for (Instruction *S : Stores)
168  if (AA->getModRefInfo(S, Loc) & AliasAnalysis::Mod)
169  return false;
170  }
171 
172  if (isa<TerminatorInst>(Inst) || isa<PHINode>(Inst))
173  return false;
174 
175  // Convergent operations can only be moved to control equivalent blocks.
176  if (auto CS = CallSite(Inst)) {
177  if (CS.hasFnAttr(Attribute::Convergent))
178  return false;
179  }
180 
181  return true;
182 }
183 
184 /// IsAcceptableTarget - Return true if it is possible to sink the instruction
185 /// in the specified basic block.
186 bool Sinking::IsAcceptableTarget(Instruction *Inst,
187  BasicBlock *SuccToSinkTo) const {
188  assert(Inst && "Instruction to be sunk is null");
189  assert(SuccToSinkTo && "Candidate sink target is null");
190 
191  // It is not possible to sink an instruction into its own block. This can
192  // happen with loops.
193  if (Inst->getParent() == SuccToSinkTo)
194  return false;
195 
196  // If the block has multiple predecessors, this would introduce computation
197  // on different code paths. We could split the critical edge, but for now we
198  // just punt.
199  // FIXME: Split critical edges if not backedges.
200  if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) {
201  // We cannot sink a load across a critical edge - there may be stores in
202  // other code paths.
203  if (!isSafeToSpeculativelyExecute(Inst))
204  return false;
205 
206  // We don't want to sink across a critical edge if we don't dominate the
207  // successor. We could be introducing calculations to new code paths.
208  if (!DT->dominates(Inst->getParent(), SuccToSinkTo))
209  return false;
210 
211  // Don't sink instructions into a loop.
212  Loop *succ = LI->getLoopFor(SuccToSinkTo);
213  Loop *cur = LI->getLoopFor(Inst->getParent());
214  if (succ != nullptr && succ != cur)
215  return false;
216  }
217 
218  // Finally, check that all the uses of the instruction are actually
219  // dominated by the candidate
220  return AllUsesDominatedByBlock(Inst, SuccToSinkTo);
221 }
222 
223 /// SinkInstruction - Determine whether it is safe to sink the specified machine
224 /// instruction out of its current block into a successor.
225 bool Sinking::SinkInstruction(Instruction *Inst,
227 
228  // Don't sink static alloca instructions. CodeGen assumes allocas outside the
229  // entry block are dynamically sized stack objects.
230  if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
231  if (AI->isStaticAlloca())
232  return false;
233 
234  // Check if it's safe to move the instruction.
235  if (!isSafeToMove(Inst, AA, Stores))
236  return false;
237 
238  // FIXME: This should include support for sinking instructions within the
239  // block they are currently in to shorten the live ranges. We often get
240  // instructions sunk into the top of a large block, but it would be better to
241  // also sink them down before their first use in the block. This xform has to
242  // be careful not to *increase* register pressure though, e.g. sinking
243  // "x = y + z" down if it kills y and z would increase the live ranges of y
244  // and z and only shrink the live range of x.
245 
246  // SuccToSinkTo - This is the successor to sink this instruction to, once we
247  // decide.
248  BasicBlock *SuccToSinkTo = nullptr;
249 
250  // Instructions can only be sunk if all their uses are in blocks
251  // dominated by one of the successors.
252  // Look at all the postdominators and see if we can sink it in one.
253  DomTreeNode *DTN = DT->getNode(Inst->getParent());
254  for (DomTreeNode::iterator I = DTN->begin(), E = DTN->end();
255  I != E && SuccToSinkTo == nullptr; ++I) {
256  BasicBlock *Candidate = (*I)->getBlock();
257  if ((*I)->getIDom()->getBlock() == Inst->getParent() &&
258  IsAcceptableTarget(Inst, Candidate))
259  SuccToSinkTo = Candidate;
260  }
261 
262  // If no suitable postdominator was found, look at all the successors and
263  // decide which one we should sink to, if any.
264  for (succ_iterator I = succ_begin(Inst->getParent()),
265  E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) {
266  if (IsAcceptableTarget(Inst, *I))
267  SuccToSinkTo = *I;
268  }
269 
270  // If we couldn't find a block to sink to, ignore this instruction.
271  if (!SuccToSinkTo)
272  return false;
273 
274  DEBUG(dbgs() << "Sink" << *Inst << " (";
275  Inst->getParent()->printAsOperand(dbgs(), false);
276  dbgs() << " -> ";
277  SuccToSinkTo->printAsOperand(dbgs(), false);
278  dbgs() << ")\n");
279 
280  // Move the instruction.
281  Inst->moveBefore(SuccToSinkTo->getFirstInsertionPt());
282  return true;
283 }
static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA, SmallPtrSetImpl< Instruction * > &Stores)
Definition: Sink.cpp:157
iterator_range< use_iterator > uses()
Definition: Value.h:283
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:224
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
iterator end()
Definition: Function.h:459
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:78
machine Machine code sinking
F(f)
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
FunctionPass * createSinkingPass()
Definition: Sink.cpp:72
#define false
Definition: ConvertUTF.c:65
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:3287
bool empty() const
Definition: BasicBlock.h:242
Base class for the actual dominator tree node.
iterator begin()
Definition: Function.h:457
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Definition: InstrTypes.h:57
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
#define INITIALIZE_AG_DEPENDENCY(depName)
Definition: PassSupport.h:72
static unsigned getIncomingValueNumForOperand(unsigned i)
bool mayWriteToMemory() const
mayWriteToMemory - Return true if this instruction may modify memory.
Representation for a specific memory location.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
iterator end()
Definition: BasicBlock.h:233
Module.h This file contains the declarations for the Module class.
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:67
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
void initializeSinkingPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
Definition: Instruction.cpp:89
#define DEBUG(X)
Definition: Debug.h:92
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
Can only be moved to control-equivalent blocks.
Definition: Attributes.h:76
machine sink
When an instruction is found to only be used outside of the loop, this function moves it to the exit ...
const BasicBlock * getParent() const
Definition: Instruction.h:72
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76