LLVM  10.0.0
CodeMoverUtils.cpp
Go to the documentation of this file.
1 //===- CodeMoverUtils.cpp - CodeMover Utilities ----------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This family of functions perform movements on basic blocks, and instructions
10 // contained within a function.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Dominators.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "codemover-utils"
24 
25 STATISTIC(HasDependences,
26  "Cannot move across instructions that has memory dependences");
27 STATISTIC(MayThrowException, "Cannot move across instructions that may throw");
28 STATISTIC(NotControlFlowEquivalent,
29  "Instructions are not control flow equivalent");
30 STATISTIC(NotMovedPHINode, "Movement of PHINodes are not supported");
31 STATISTIC(NotMovedTerminator, "Movement of Terminator are not supported");
32 
34  const DominatorTree &DT,
35  const PostDominatorTree &PDT) {
36  return isControlFlowEquivalent(*I0.getParent(), *I1.getParent(), DT, PDT);
37 }
38 
40  const DominatorTree &DT,
41  const PostDominatorTree &PDT) {
42  if (&BB0 == &BB1)
43  return true;
44 
45  return ((DT.dominates(&BB0, &BB1) && PDT.dominates(&BB1, &BB0)) ||
46  (PDT.dominates(&BB0, &BB1) && DT.dominates(&BB1, &BB0)));
47 }
48 
50  llvm::Statistic &Stat) {
51  ++Stat;
52  LLVM_DEBUG(dbgs() << "Unable to move instruction: " << I << ". "
53  << Stat.getDesc());
54  return false;
55 }
56 
57 /// Collect all instructions in between \p StartInst and \p EndInst, and store
58 /// them in \p InBetweenInsts.
59 static void
61  SmallPtrSetImpl<Instruction *> &InBetweenInsts) {
62  assert(InBetweenInsts.empty() && "Expecting InBetweenInsts to be empty");
63 
64  /// Get the next instructions of \p I, and push them to \p WorkList.
65  auto getNextInsts = [](Instruction &I,
67  if (Instruction *NextInst = I.getNextNode())
68  WorkList.insert(NextInst);
69  else {
70  assert(I.isTerminator() && "Expecting a terminator instruction");
71  for (BasicBlock *Succ : successors(&I))
72  WorkList.insert(&Succ->front());
73  }
74  };
75 
77  getNextInsts(StartInst, WorkList);
78  while (!WorkList.empty()) {
79  Instruction *CurInst = *WorkList.begin();
80  WorkList.erase(CurInst);
81 
82  if (CurInst == &EndInst)
83  continue;
84 
85  if (!InBetweenInsts.insert(CurInst).second)
86  continue;
87 
88  getNextInsts(*CurInst, WorkList);
89  }
90 }
91 
93  const DominatorTree &DT,
94  const PostDominatorTree &PDT,
95  DependenceInfo &DI) {
96  // Cannot move itself before itself.
97  if (&I == &InsertPoint)
98  return false;
99 
100  // Not moved.
101  if (I.getNextNode() == &InsertPoint)
102  return true;
103 
104  if (isa<PHINode>(I) || isa<PHINode>(InsertPoint))
105  return reportInvalidCandidate(I, NotMovedPHINode);
106 
107  if (I.isTerminator())
108  return reportInvalidCandidate(I, NotMovedTerminator);
109 
110  // TODO remove this limitation.
111  if (!isControlFlowEquivalent(I, InsertPoint, DT, PDT))
112  return reportInvalidCandidate(I, NotControlFlowEquivalent);
113 
114  // As I and InsertPoint are control flow equivalent, if I dominates
115  // InsertPoint, then I comes before InsertPoint.
116  const bool MoveForward = DT.dominates(&I, &InsertPoint);
117  if (MoveForward) {
118  // When I is being moved forward, we need to make sure the InsertPoint
119  // dominates every users. Or else, a user may be using an undefined I.
120  for (const Use &U : I.uses())
121  if (auto *UserInst = dyn_cast<Instruction>(U.getUser()))
122  if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U))
123  return false;
124  } else {
125  // When I is being moved backward, we need to make sure all its opernads
126  // dominates the InsertPoint. Or else, an operand may be undefined for I.
127  for (const Value *Op : I.operands())
128  if (auto *OpInst = dyn_cast<Instruction>(Op))
129  if (&InsertPoint == OpInst || !DT.dominates(OpInst, &InsertPoint))
130  return false;
131  }
132 
133  Instruction &StartInst = (MoveForward ? I : InsertPoint);
134  Instruction &EndInst = (MoveForward ? InsertPoint : I);
135  SmallPtrSet<Instruction *, 10> InstsToCheck;
136  collectInstructionsInBetween(StartInst, EndInst, InstsToCheck);
137  if (!MoveForward)
138  InstsToCheck.insert(&InsertPoint);
139 
140  // Check if there exists instructions which may throw, may synchonize, or may
141  // never return, from I to InsertPoint.
143  if (std::any_of(InstsToCheck.begin(), InstsToCheck.end(),
144  [](Instruction *I) {
145  if (I->mayThrow())
146  return true;
147 
148  const CallBase *CB = dyn_cast<CallBase>(I);
149  if (!CB)
150  return false;
151  if (!CB->hasFnAttr(Attribute::WillReturn))
152  return true;
153  if (!CB->hasFnAttr(Attribute::NoSync))
154  return true;
155 
156  return false;
157  })) {
158  return reportInvalidCandidate(I, MayThrowException);
159  }
160 
161  // Check if I has any output/flow/anti dependences with instructions from \p
162  // StartInst to \p EndInst.
163  if (std::any_of(InstsToCheck.begin(), InstsToCheck.end(),
164  [&DI, &I](Instruction *CurInst) {
165  auto DepResult = DI.depends(&I, CurInst, true);
166  if (DepResult &&
167  (DepResult->isOutput() || DepResult->isFlow() ||
168  DepResult->isAnti()))
169  return true;
170  return false;
171  }))
172  return reportInvalidCandidate(I, HasDependences);
173 
174  return true;
175 }
176 
178  const DominatorTree &DT,
179  const PostDominatorTree &PDT, DependenceInfo &DI) {
180  for (auto It = ++FromBB.rbegin(); It != FromBB.rend();) {
181  Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();
182  Instruction &I = *It;
183  // Increment the iterator before modifying FromBB.
184  ++It;
185 
186  if (isSafeToMoveBefore(I, *MovePos, DT, PDT, DI))
187  I.moveBefore(MovePos);
188  }
189 }
bool isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint, const DominatorTree &DT, const PostDominatorTree &PDT, DependenceInfo &DI)
Return true if I can be safely moved before InsertPoint.
iterator_range< use_iterator > uses()
Definition: Value.h:375
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isTerminator() const
Definition: Instruction.h:128
void moveInstsBottomUp(BasicBlock &FromBB, BasicBlock &ToBB, const DominatorTree &DT, const PostDominatorTree &PDT, DependenceInfo &DI)
Move instructions from FromBB bottom up to the beginning of ToBB when proven safe.
STATISTIC(NumFunctions, "Total number of functions")
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1105
reverse_iterator rend()
Definition: BasicBlock.h:280
reverse_iterator rbegin()
Definition: BasicBlock.h:278
DependenceInfo - This class is the main dependence-analysis driver.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
const char * getDesc() const
Definition: Statistic.h:58
bool isControlFlowEquivalent(const Instruction &I0, const Instruction &I1, const DominatorTree &DT, const PostDominatorTree &PDT)
Return true if I0 and I1 are control flow equivalent.
static bool reportInvalidCandidate(const Instruction &I, llvm::Statistic &Stat)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool PossiblyLoopIndependent)
depends - Tests for a dependence between the Src and Dst instructions.
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
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:370
bool mayThrow() const
Return true if this instruction may throw an exception.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1189
op_range operands()
Definition: User.h:237
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
Definition: InstrTypes.h:1391
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:377
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:249
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
iterator begin() const
Definition: SmallPtrSet.h:396
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
iterator end() const
Definition: SmallPtrSet.h:401
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
Definition: Value.h:74
succ_range successors(Instruction *I)
Definition: CFG.h:259
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:86
const Instruction * getFirstNonPHIOrDbg() const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Definition: BasicBlock.cpp:203
static void collectInstructionsInBetween(Instruction &StartInst, const Instruction &EndInst, SmallPtrSetImpl< Instruction *> &InBetweenInsts)
Collect all instructions in between StartInst and EndInst, and store them in InBetweenInsts.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const BasicBlock * getParent() const
Definition: Instruction.h:66