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