LLVM  5.0.0
LoopPredication.cpp
Go to the documentation of this file.
1 //===-- LoopPredication.cpp - Guard based loop predication pass -----------===//
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 // The LoopPredication pass tries to convert loop variant range checks to loop
11 // invariant by widening checks across loop iterations. For example, it will
12 // convert
13 //
14 // for (i = 0; i < n; i++) {
15 // guard(i < len);
16 // ...
17 // }
18 //
19 // to
20 //
21 // for (i = 0; i < n; i++) {
22 // guard(n - 1 < len);
23 // ...
24 // }
25 //
26 // After this transformation the condition of the guard is loop invariant, so
27 // loop-unswitch can later unswitch the loop by this condition which basically
28 // predicates the loop by the widened condition:
29 //
30 // if (n - 1 < len)
31 // for (i = 0; i < n; i++) {
32 // ...
33 // }
34 // else
35 // deoptimize
36 //
37 //===----------------------------------------------------------------------===//
38 
40 #include "llvm/Analysis/LoopInfo.h"
41 #include "llvm/Analysis/LoopPass.h"
45 #include "llvm/IR/Function.h"
46 #include "llvm/IR/GlobalValue.h"
47 #include "llvm/IR/IntrinsicInst.h"
48 #include "llvm/IR/Module.h"
49 #include "llvm/IR/PatternMatch.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Transforms/Scalar.h"
54 
55 #define DEBUG_TYPE "loop-predication"
56 
57 using namespace llvm;
58 
59 namespace {
60 class LoopPredication {
61  /// Represents an induction variable check:
62  /// icmp Pred, <induction variable>, <loop invariant limit>
63  struct LoopICmp {
65  const SCEVAddRecExpr *IV;
66  const SCEV *Limit;
67  LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV,
68  const SCEV *Limit)
69  : Pred(Pred), IV(IV), Limit(Limit) {}
70  LoopICmp() {}
71  };
72 
73  ScalarEvolution *SE;
74 
75  Loop *L;
76  const DataLayout *DL;
77  BasicBlock *Preheader;
78 
79  Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI);
80 
81  Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder,
82  ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
83  Instruction *InsertAt);
84 
85  Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
86  IRBuilder<> &Builder);
87  bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander);
88 
89 public:
90  LoopPredication(ScalarEvolution *SE) : SE(SE){};
91  bool runOnLoop(Loop *L);
92 };
93 
94 class LoopPredicationLegacyPass : public LoopPass {
95 public:
96  static char ID;
97  LoopPredicationLegacyPass() : LoopPass(ID) {
99  }
100 
101  void getAnalysisUsage(AnalysisUsage &AU) const override {
103  }
104 
105  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
106  if (skipLoop(L))
107  return false;
108  auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
109  LoopPredication LP(SE);
110  return LP.runOnLoop(L);
111  }
112 };
113 
115 } // end namespace llvm
116 
117 INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication",
118  "Loop predication", false, false)
120 INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication",
121  "Loop predication", false, false)
122 
124  return new LoopPredicationLegacyPass();
125 }
126 
129  LPMUpdater &U) {
130  LoopPredication LP(&AR.SE);
131  if (!LP.runOnLoop(&L))
132  return PreservedAnalyses::all();
133 
135 }
136 
138 LoopPredication::parseLoopICmp(ICmpInst *ICI) {
139  ICmpInst::Predicate Pred = ICI->getPredicate();
140 
141  Value *LHS = ICI->getOperand(0);
142  Value *RHS = ICI->getOperand(1);
143  const SCEV *LHSS = SE->getSCEV(LHS);
144  if (isa<SCEVCouldNotCompute>(LHSS))
145  return None;
146  const SCEV *RHSS = SE->getSCEV(RHS);
147  if (isa<SCEVCouldNotCompute>(RHSS))
148  return None;
149 
150  // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV
151  if (SE->isLoopInvariant(LHSS, L)) {
152  std::swap(LHS, RHS);
153  std::swap(LHSS, RHSS);
154  Pred = ICmpInst::getSwappedPredicate(Pred);
155  }
156 
157  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS);
158  if (!AR || AR->getLoop() != L)
159  return None;
160 
161  return LoopICmp(Pred, AR, RHSS);
162 }
163 
164 Value *LoopPredication::expandCheck(SCEVExpander &Expander,
165  IRBuilder<> &Builder,
166  ICmpInst::Predicate Pred, const SCEV *LHS,
167  const SCEV *RHS, Instruction *InsertAt) {
168  Type *Ty = LHS->getType();
169  assert(Ty == RHS->getType() && "expandCheck operands have different types?");
170  Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt);
171  Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt);
172  return Builder.CreateICmp(Pred, LHSV, RHSV);
173 }
174 
175 /// If ICI can be widened to a loop invariant condition emits the loop
176 /// invariant condition in the loop preheader and return it, otherwise
177 /// returns None.
178 Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI,
179  SCEVExpander &Expander,
180  IRBuilder<> &Builder) {
181  DEBUG(dbgs() << "Analyzing ICmpInst condition:\n");
182  DEBUG(ICI->dump());
183 
184  auto RangeCheck = parseLoopICmp(ICI);
185  if (!RangeCheck) {
186  DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
187  return None;
188  }
189 
190  ICmpInst::Predicate Pred = RangeCheck->Pred;
191  const SCEVAddRecExpr *IndexAR = RangeCheck->IV;
192  const SCEV *RHSS = RangeCheck->Limit;
193 
194  auto CanExpand = [this](const SCEV *S) {
195  return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE);
196  };
197  if (!CanExpand(RHSS))
198  return None;
199 
200  DEBUG(dbgs() << "IndexAR: ");
201  DEBUG(IndexAR->dump());
202 
203  bool IsIncreasing = false;
204  if (!SE->isMonotonicPredicate(IndexAR, Pred, IsIncreasing))
205  return None;
206 
207  // If the predicate is increasing the condition can change from false to true
208  // as the loop progresses, in this case take the value on the first iteration
209  // for the widened check. Otherwise the condition can change from true to
210  // false as the loop progresses, so take the value on the last iteration.
211  const SCEV *NewLHSS = IsIncreasing
212  ? IndexAR->getStart()
213  : SE->getSCEVAtScope(IndexAR, L->getParentLoop());
214  if (NewLHSS == IndexAR) {
215  DEBUG(dbgs() << "Can't compute NewLHSS!\n");
216  return None;
217  }
218 
219  DEBUG(dbgs() << "NewLHSS: ");
220  DEBUG(NewLHSS->dump());
221 
222  if (!CanExpand(NewLHSS))
223  return None;
224 
225  DEBUG(dbgs() << "NewLHSS is loop invariant and safe to expand. Expand!\n");
226 
227  Instruction *InsertAt = Preheader->getTerminator();
228  return expandCheck(Expander, Builder, Pred, NewLHSS, RHSS, InsertAt);
229 }
230 
231 bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard,
232  SCEVExpander &Expander) {
233  DEBUG(dbgs() << "Processing guard:\n");
234  DEBUG(Guard->dump());
235 
236  IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
237 
238  // The guard condition is expected to be in form of:
239  // cond1 && cond2 && cond3 ...
240  // Iterate over subconditions looking for for icmp conditions which can be
241  // widened across loop iterations. Widening these conditions remember the
242  // resulting list of subconditions in Checks vector.
243  SmallVector<Value *, 4> Worklist(1, Guard->getOperand(0));
244  SmallPtrSet<Value *, 4> Visited;
245 
247 
248  unsigned NumWidened = 0;
249  do {
250  Value *Condition = Worklist.pop_back_val();
251  if (!Visited.insert(Condition).second)
252  continue;
253 
254  Value *LHS, *RHS;
255  using namespace llvm::PatternMatch;
256  if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) {
257  Worklist.push_back(LHS);
258  Worklist.push_back(RHS);
259  continue;
260  }
261 
262  if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
263  if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) {
264  Checks.push_back(NewRangeCheck.getValue());
265  NumWidened++;
266  continue;
267  }
268  }
269 
270  // Save the condition as is if we can't widen it
271  Checks.push_back(Condition);
272  } while (Worklist.size() != 0);
273 
274  if (NumWidened == 0)
275  return false;
276 
277  // Emit the new guard condition
278  Builder.SetInsertPoint(Guard);
279  Value *LastCheck = nullptr;
280  for (auto *Check : Checks)
281  if (!LastCheck)
282  LastCheck = Check;
283  else
284  LastCheck = Builder.CreateAnd(LastCheck, Check);
285  Guard->setOperand(0, LastCheck);
286 
287  DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
288  return true;
289 }
290 
291 bool LoopPredication::runOnLoop(Loop *Loop) {
292  L = Loop;
293 
294  DEBUG(dbgs() << "Analyzing ");
295  DEBUG(L->dump());
296 
297  Module *M = L->getHeader()->getModule();
298 
299  // There is nothing to do if the module doesn't use guards
300  auto *GuardDecl =
301  M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
302  if (!GuardDecl || GuardDecl->use_empty())
303  return false;
304 
305  DL = &M->getDataLayout();
306 
307  Preheader = L->getLoopPreheader();
308  if (!Preheader)
309  return false;
310 
311  // Collect all the guards into a vector and process later, so as not
312  // to invalidate the instruction iterator.
314  for (const auto BB : L->blocks())
315  for (auto &I : *BB)
316  if (auto *II = dyn_cast<IntrinsicInst>(&I))
317  if (II->getIntrinsicID() == Intrinsic::experimental_guard)
318  Guards.push_back(II);
319 
320  if (Guards.empty())
321  return false;
322 
323  SCEVExpander Expander(*SE, *DL, "loop-predication");
324 
325  bool Changed = false;
326  for (auto *Guard : Guards)
327  Changed |= widenGuardConditions(Guard, Expander);
328 
329  return Changed;
330 }
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
static bool Check(DecodeStatus &Out, DecodeStatus In)
const NoneType None
Definition: None.h:24
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:550
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:72
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1634
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
loop predication
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
The main scalar evolution driver.
DataLayout DL
loop Loop false
INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass,"loop-predication","Loop predication", false, false) INITIALIZE_PASS_END(LoopPredicationLegacyPass
LoopT * getParentLoop() const
Definition: LoopInfo.h:104
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
BlockT * getHeader() const
Definition: LoopInfo.h:103
const SCEV * getStart() const
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:591
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:664
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
This node represents a polynomial recurrence on the trip count of the specified loop.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:128
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:144
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:101
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Type * getType() const
Return the LLVM type of this SCEV expression.
Represent the analysis usage information of a pass.
Pass * createLoopPredicationPass()
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:860
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
Value * getOperand(unsigned i) const
Definition: User.h:154
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:934
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:3606
void dump() const
This method is used for debugging.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:423
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:974
void setOperand(unsigned i, Value *Val)
Definition: User.h:159
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
This class uses information about analyze scalars to rewrite expressions in canonical form...
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:479
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:360
#define I(x, y, z)
Definition: MD5.cpp:58
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:1022
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:323
const Loop * getLoop() const
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1063
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void initializeLoopPredicationLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
LLVM Value Representation.
Definition: Value.h:73
#define DEBUG(X)
Definition: Debug.h:118
A container for analyses that lazily runs them and caches their results.
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44