LLVM  3.7.0
SimplifyIndVar.cpp
Go to the documentation of this file.
1 //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
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 file implements induction variable simplification. It does
11 // not define any actual pass or policy, but provides a single function to
12 // simplify a loop's induction variables based on ScalarEvolution.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/LoopPass.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/Support/Debug.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "indvars"
35 
36 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
37 STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
38 STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
39 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
40 
41 namespace {
42  /// This is a utility for simplifying induction variables
43  /// based on ScalarEvolution. It is the primary instrument of the
44  /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
45  /// other loop passes that preserve SCEV.
46  class SimplifyIndvar {
47  Loop *L;
48  LoopInfo *LI;
49  ScalarEvolution *SE;
50 
51  SmallVectorImpl<WeakVH> &DeadInsts;
52 
53  bool Changed;
54 
55  public:
56  SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, LoopInfo *LI,
58  : L(Loop), LI(LI), SE(SE), DeadInsts(Dead), Changed(false) {
59  assert(LI && "IV simplification requires LoopInfo");
60  }
61 
62  bool hasChanged() const { return Changed; }
63 
64  /// Iteratively perform simplification on a worklist of users of the
65  /// specified induction variable. This is the top-level driver that applies
66  /// all simplicitions to users of an IV.
67  void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
68 
69  Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
70 
71  bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
72  void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
73  void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand,
74  bool IsSigned);
75  bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
76 
77  Instruction *splitOverflowIntrinsic(Instruction *IVUser,
78  const DominatorTree *DT);
79  };
80 }
81 
82 /// Fold an IV operand into its use. This removes increments of an
83 /// aligned IV when used by a instruction that ignores the low bits.
84 ///
85 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
86 ///
87 /// Return the operand of IVOperand for this induction variable if IVOperand can
88 /// be folded (in case more folding opportunities have been exposed).
89 /// Otherwise return null.
90 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
91  Value *IVSrc = nullptr;
92  unsigned OperIdx = 0;
93  const SCEV *FoldedExpr = nullptr;
94  switch (UseInst->getOpcode()) {
95  default:
96  return nullptr;
97  case Instruction::UDiv:
98  case Instruction::LShr:
99  // We're only interested in the case where we know something about
100  // the numerator and have a constant denominator.
101  if (IVOperand != UseInst->getOperand(OperIdx) ||
102  !isa<ConstantInt>(UseInst->getOperand(1)))
103  return nullptr;
104 
105  // Attempt to fold a binary operator with constant operand.
106  // e.g. ((I + 1) >> 2) => I >> 2
107  if (!isa<BinaryOperator>(IVOperand)
108  || !isa<ConstantInt>(IVOperand->getOperand(1)))
109  return nullptr;
110 
111  IVSrc = IVOperand->getOperand(0);
112  // IVSrc must be the (SCEVable) IV, since the other operand is const.
113  assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
114 
115  ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
116  if (UseInst->getOpcode() == Instruction::LShr) {
117  // Get a constant for the divisor. See createSCEV.
118  uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
119  if (D->getValue().uge(BitWidth))
120  return nullptr;
121 
122  D = ConstantInt::get(UseInst->getContext(),
123  APInt::getOneBitSet(BitWidth, D->getZExtValue()));
124  }
125  FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
126  }
127  // We have something that might fold it's operand. Compare SCEVs.
128  if (!SE->isSCEVable(UseInst->getType()))
129  return nullptr;
130 
131  // Bypass the operand if SCEV can prove it has no effect.
132  if (SE->getSCEV(UseInst) != FoldedExpr)
133  return nullptr;
134 
135  DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
136  << " -> " << *UseInst << '\n');
137 
138  UseInst->setOperand(OperIdx, IVSrc);
139  assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
140 
141  ++NumElimOperand;
142  Changed = true;
143  if (IVOperand->use_empty())
144  DeadInsts.emplace_back(IVOperand);
145  return IVSrc;
146 }
147 
148 /// SimplifyIVUsers helper for eliminating useless
149 /// comparisons against an induction variable.
150 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
151  unsigned IVOperIdx = 0;
152  ICmpInst::Predicate Pred = ICmp->getPredicate();
153  if (IVOperand != ICmp->getOperand(0)) {
154  // Swapped
155  assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
156  IVOperIdx = 1;
157  Pred = ICmpInst::getSwappedPredicate(Pred);
158  }
159 
160  // Get the SCEVs for the ICmp operands.
161  const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
162  const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
163 
164  // Simplify unnecessary loops away.
165  const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
166  S = SE->getSCEVAtScope(S, ICmpLoop);
167  X = SE->getSCEVAtScope(X, ICmpLoop);
168 
169  // If the condition is always true or always false, replace it with
170  // a constant value.
171  if (SE->isKnownPredicate(Pred, S, X))
173  else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X))
175  else
176  return;
177 
178  DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
179  ++NumElimCmp;
180  Changed = true;
181  DeadInsts.emplace_back(ICmp);
182 }
183 
184 /// SimplifyIVUsers helper for eliminating useless
185 /// remainder operations operating on an induction variable.
186 void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
187  Value *IVOperand,
188  bool IsSigned) {
189  // We're only interested in the case where we know something about
190  // the numerator.
191  if (IVOperand != Rem->getOperand(0))
192  return;
193 
194  // Get the SCEVs for the ICmp operands.
195  const SCEV *S = SE->getSCEV(Rem->getOperand(0));
196  const SCEV *X = SE->getSCEV(Rem->getOperand(1));
197 
198  // Simplify unnecessary loops away.
199  const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
200  S = SE->getSCEVAtScope(S, ICmpLoop);
201  X = SE->getSCEVAtScope(X, ICmpLoop);
202 
203  // i % n --> i if i is in [0,n).
204  if ((!IsSigned || SE->isKnownNonNegative(S)) &&
205  SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
206  S, X))
207  Rem->replaceAllUsesWith(Rem->getOperand(0));
208  else {
209  // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
210  const SCEV *LessOne =
211  SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1));
212  if (IsSigned && !SE->isKnownNonNegative(LessOne))
213  return;
214 
215  if (!SE->isKnownPredicate(IsSigned ?
216  ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
217  LessOne, X))
218  return;
219 
220  ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
221  Rem->getOperand(0), Rem->getOperand(1));
222  SelectInst *Sel =
223  SelectInst::Create(ICmp,
224  ConstantInt::get(Rem->getType(), 0),
225  Rem->getOperand(0), "tmp", Rem);
226  Rem->replaceAllUsesWith(Sel);
227  }
228 
229  DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
230  ++NumElimRem;
231  Changed = true;
232  DeadInsts.emplace_back(Rem);
233 }
234 
235 /// Eliminate an operation that consumes a simple IV and has
236 /// no observable side-effect given the range of IV values.
237 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
238 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
239  Instruction *IVOperand) {
240  if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
241  eliminateIVComparison(ICmp, IVOperand);
242  return true;
243  }
244  if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) {
245  bool IsSigned = Rem->getOpcode() == Instruction::SRem;
246  if (IsSigned || Rem->getOpcode() == Instruction::URem) {
247  eliminateIVRemainder(Rem, IVOperand, IsSigned);
248  return true;
249  }
250  }
251 
252  // Eliminate any operation that SCEV can prove is an identity function.
253  if (!SE->isSCEVable(UseInst->getType()) ||
254  (UseInst->getType() != IVOperand->getType()) ||
255  (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
256  return false;
257 
258  DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
259 
260  UseInst->replaceAllUsesWith(IVOperand);
261  ++NumElimIdentity;
262  Changed = true;
263  DeadInsts.emplace_back(UseInst);
264  return true;
265 }
266 
267 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
268 /// unsigned-overflow. Returns true if anything changed, false otherwise.
269 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
270  Value *IVOperand) {
271 
272  // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
273  if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
274  return false;
275 
276  const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
278 
279  switch (BO->getOpcode()) {
280  default:
281  return false;
282 
283  case Instruction::Add:
284  GetExprForBO = &ScalarEvolution::getAddExpr;
285  break;
286 
287  case Instruction::Sub:
288  GetExprForBO = &ScalarEvolution::getMinusSCEV;
289  break;
290 
291  case Instruction::Mul:
292  GetExprForBO = &ScalarEvolution::getMulExpr;
293  break;
294  }
295 
296  unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
297  Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
298  const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
299  const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
300 
301  bool Changed = false;
302 
303  if (!BO->hasNoUnsignedWrap()) {
304  const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
305  const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
306  SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
308  if (ExtendAfterOp == OpAfterExtend) {
309  BO->setHasNoUnsignedWrap();
310  SE->forgetValue(BO);
311  Changed = true;
312  }
313  }
314 
315  if (!BO->hasNoSignedWrap()) {
316  const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
317  const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
318  SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
320  if (ExtendAfterOp == OpAfterExtend) {
321  BO->setHasNoSignedWrap();
322  SE->forgetValue(BO);
323  Changed = true;
324  }
325  }
326 
327  return Changed;
328 }
329 
330 /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow
331 /// analysis and optimization.
332 ///
333 /// \return A new value representing the non-overflowing add if possible,
334 /// otherwise return the original value.
335 Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser,
336  const DominatorTree *DT) {
337  IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser);
338  if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow)
339  return IVUser;
340 
341  // Find a branch guarded by the overflow check.
342  BranchInst *Branch = nullptr;
343  Instruction *AddVal = nullptr;
344  for (User *U : II->users()) {
345  if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) {
346  if (ExtractInst->getNumIndices() != 1)
347  continue;
348  if (ExtractInst->getIndices()[0] == 0)
349  AddVal = ExtractInst;
350  else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse())
351  Branch = dyn_cast<BranchInst>(ExtractInst->user_back());
352  }
353  }
354  if (!AddVal || !Branch)
355  return IVUser;
356 
357  BasicBlock *ContinueBB = Branch->getSuccessor(1);
358  if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB))
359  return IVUser;
360 
361  // Check if all users of the add are provably NSW.
362  bool AllNSW = true;
363  for (Use &U : AddVal->uses()) {
364  if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) {
365  BasicBlock *UseBB = UseInst->getParent();
366  if (PHINode *PHI = dyn_cast<PHINode>(UseInst))
367  UseBB = PHI->getIncomingBlock(U);
368  if (!DT->dominates(ContinueBB, UseBB)) {
369  AllNSW = false;
370  break;
371  }
372  }
373  }
374  if (!AllNSW)
375  return IVUser;
376 
377  // Go for it...
378  IRBuilder<> Builder(IVUser);
379  Instruction *AddInst = dyn_cast<Instruction>(
380  Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1)));
381 
382  // The caller expects the new add to have the same form as the intrinsic. The
383  // IV operand position must be the same.
384  assert((AddInst->getOpcode() == Instruction::Add &&
385  AddInst->getOperand(0) == II->getOperand(0)) &&
386  "Bad add instruction created from overflow intrinsic.");
387 
388  AddVal->replaceAllUsesWith(AddInst);
389  DeadInsts.emplace_back(AddVal);
390  return AddInst;
391 }
392 
393 /// Add all uses of Def to the current IV's worklist.
394 static void pushIVUsers(
395  Instruction *Def,
397  SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
398 
399  for (User *U : Def->users()) {
400  Instruction *UI = cast<Instruction>(U);
401 
402  // Avoid infinite or exponential worklist processing.
403  // Also ensure unique worklist users.
404  // If Def is a LoopPhi, it may not be in the Simplified set, so check for
405  // self edges first.
406  if (UI != Def && Simplified.insert(UI).second)
407  SimpleIVUsers.push_back(std::make_pair(UI, Def));
408  }
409 }
410 
411 /// Return true if this instruction generates a simple SCEV
412 /// expression in terms of that IV.
413 ///
414 /// This is similar to IVUsers' isInteresting() but processes each instruction
415 /// non-recursively when the operand is already known to be a simpleIVUser.
416 ///
417 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
418  if (!SE->isSCEVable(I->getType()))
419  return false;
420 
421  // Get the symbolic expression for this instruction.
422  const SCEV *S = SE->getSCEV(I);
423 
424  // Only consider affine recurrences.
425  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
426  if (AR && AR->getLoop() == L)
427  return true;
428 
429  return false;
430 }
431 
432 /// Iteratively perform simplification on a worklist of users
433 /// of the specified induction variable. Each successive simplification may push
434 /// more users which may themselves be candidates for simplification.
435 ///
436 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
437 /// instructions in-place during analysis. Rather than rewriting induction
438 /// variables bottom-up from their users, it transforms a chain of IVUsers
439 /// top-down, updating the IR only when it encouters a clear optimization
440 /// opportunitiy.
441 ///
442 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
443 ///
444 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
445  if (!SE->isSCEVable(CurrIV->getType()))
446  return;
447 
448  // Instructions processed by SimplifyIndvar for CurrIV.
450 
451  // Use-def pairs if IV users waiting to be processed for CurrIV.
453 
454  // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
455  // called multiple times for the same LoopPhi. This is the proper thing to
456  // do for loop header phis that use each other.
457  pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
458 
459  while (!SimpleIVUsers.empty()) {
460  std::pair<Instruction*, Instruction*> UseOper =
461  SimpleIVUsers.pop_back_val();
462  Instruction *UseInst = UseOper.first;
463 
464  // Bypass back edges to avoid extra work.
465  if (UseInst == CurrIV) continue;
466 
467  if (V && V->shouldSplitOverflowInstrinsics()) {
468  UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree());
469  if (!UseInst)
470  continue;
471  }
472 
473  Instruction *IVOperand = UseOper.second;
474  for (unsigned N = 0; IVOperand; ++N) {
475  assert(N <= Simplified.size() && "runaway iteration");
476 
477  Value *NewOper = foldIVUser(UseOper.first, IVOperand);
478  if (!NewOper)
479  break; // done folding
480  IVOperand = dyn_cast<Instruction>(NewOper);
481  }
482  if (!IVOperand)
483  continue;
484 
485  if (eliminateIVUser(UseOper.first, IVOperand)) {
486  pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
487  continue;
488  }
489 
490  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
491  if (isa<OverflowingBinaryOperator>(BO) &&
492  strengthenOverflowingOperation(BO, IVOperand)) {
493  // re-queue uses of the now modified binary operator and fall
494  // through to the checks that remain.
495  pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
496  }
497  }
498 
499  CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
500  if (V && Cast) {
501  V->visitCast(Cast);
502  continue;
503  }
504  if (isSimpleIVUser(UseOper.first, L, SE)) {
505  pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
506  }
507  }
508 }
509 
510 namespace llvm {
511 
513 
514 /// Simplify instructions that use this induction variable
515 /// by using ScalarEvolution to analyze the IV's recurrence.
518 {
519  LoopInfo *LI = &LPM->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
520  SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, LI, Dead);
521  SIV.simplifyUsers(CurrIV, V);
522  return SIV.hasChanged();
523 }
524 
525 /// Simplify users of induction variables within this
526 /// loop. This does not actually change or add IVs.
529  bool Changed = false;
530  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
531  Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, LPM, Dead);
532  }
533  return Changed;
534 }
535 
536 } // namespace llvm
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:537
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
iterator_range< use_iterator > uses()
Definition: Value.h:283
ExtractValueInst - This instruction extracts a struct member or array element value from an aggregate...
STATISTIC(NumFunctions,"Total number of functions")
Intrinsic::ID getIntrinsicID() const
getIntrinsicID - Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:44
ScalarEvolution - This class is the main scalar evolution driver.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
Definition: InstrTypes.h:783
unsigned less than
Definition: InstrTypes.h:722
virtual void anchor()
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
Definition: LoopInfo.h:540
const DominatorTree * getDomTree() const
BlockT * getHeader() const
Definition: LoopInfo.h:96
Interface for visiting interesting IV users that are recognized but not simplified by this utility...
SelectInst - This class represents the LLVM 'select' instruction.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:106
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:406
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
bool shouldSplitOverflowInstrinsics() const
#define false
Definition: ConvertUTF.c:65
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:117
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
BasicBlock * getSuccessor(unsigned i) const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
bool isSCEVable(Type *Ty) const
isSCEVable - Test if values of the given type are analyzable within the SCEV framework.
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
size_type size() const
Definition: SmallPtrSet.h:79
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
BranchInst - Conditional or Unconditional Branch instruction.
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead, IVVisitor *V=nullptr)
simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution t...
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
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead)
SimplifyLoopIVs - Simplify users of induction variables within this loop.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:114
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1137
Value * getOperand(unsigned i) const
Definition: User.h:118
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:760
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:214
BinaryOps getOpcode() const
Definition: InstrTypes.h:323
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
signed less than
Definition: InstrTypes.h:726
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:799
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:530
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE)
Return true if this instruction generates a simple SCEV expression in terms of that IV...
iterator_range< user_iterator > users()
Definition: Value.h:300
LLVM_ATTRIBUTE_UNUSED_RESULT 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:285
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
SCEV - This class represents an analyzed expression in the program.
virtual void visitCast(CastInst *Cast)=0
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
const Loop * getLoop() const
bool use_empty() const
Definition: Value.h:275
static void pushIVUsers(Instruction *Def, SmallPtrSet< Instruction *, 16 > &Simplified, SmallVectorImpl< std::pair< Instruction *, Instruction * > > &SimpleIVUsers)
Add all uses of Def to the current IV's worklist.
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
const SCEV * getSCEV(Value *V)
getSCEV - Return a SCEV expression for the full generality of the specified expression.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:112
#define DEBUG(X)
Definition: Debug.h:92
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMulExpr - Get a canonical multiply expression, or something simpler if possible.
const BasicBlock * getParent() const
Definition: Instruction.h:72
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37