LLVM  4.0.0
LoopUtils.h
Go to the documentation of this file.
1 //===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -*- C++ -*-=========//
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 defines some loop transformation utilities.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
16 
17 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/IRBuilder.h"
22 
23 namespace llvm {
24 class AliasSet;
25 class AliasSetTracker;
26 class AssumptionCache;
27 class BasicBlock;
28 class DataLayout;
29 class DominatorTree;
30 class Loop;
31 class LoopInfo;
32 class OptimizationRemarkEmitter;
33 class Pass;
34 class PredicatedScalarEvolution;
35 class PredIteratorCache;
36 class ScalarEvolution;
37 class SCEV;
38 class TargetLibraryInfo;
39 
40 /// \brief Captures loop safety information.
41 /// It keep information for loop & its header may throw exception.
43  bool MayThrow; // The current loop contains an instruction which
44  // may throw.
45  bool HeaderMayThrow; // Same as previous, but specific to loop header
46  // Used to update funclet bundle operands.
49 };
50 
51 /// The RecurrenceDescriptor is used to identify recurrences variables in a
52 /// loop. Reduction is a special case of recurrence that has uses of the
53 /// recurrence variable outside the loop. The method isReductionPHI identifies
54 /// reductions that are basic recurrences.
55 ///
56 /// Basic recurrences are defined as the summation, product, OR, AND, XOR, min,
57 /// or max of a set of terms. For example: for(i=0; i<n; i++) { total +=
58 /// array[i]; } is a summation of array elements. Basic recurrences are a
59 /// special case of chains of recurrences (CR). See ScalarEvolution for CR
60 /// references.
61 
62 /// This struct holds information about recurrence variables.
64 
65 public:
66  /// This enum represents the kinds of recurrences that we support.
68  RK_NoRecurrence, ///< Not a recurrence.
69  RK_IntegerAdd, ///< Sum of integers.
70  RK_IntegerMult, ///< Product of integers.
71  RK_IntegerOr, ///< Bitwise or logical OR of numbers.
72  RK_IntegerAnd, ///< Bitwise or logical AND of numbers.
73  RK_IntegerXor, ///< Bitwise or logical XOR of numbers.
74  RK_IntegerMinMax, ///< Min/max implemented in terms of select(cmp()).
75  RK_FloatAdd, ///< Sum of floats.
76  RK_FloatMult, ///< Product of floats.
77  RK_FloatMinMax ///< Min/max implemented in terms of select(cmp()).
78  };
79 
80  // This enum represents the kind of minmax recurrence.
89  };
90 
92  : StartValue(nullptr), LoopExitInstr(nullptr), Kind(RK_NoRecurrence),
93  MinMaxKind(MRK_Invalid), UnsafeAlgebraInst(nullptr),
94  RecurrenceType(nullptr), IsSigned(false) {}
95 
99  : StartValue(Start), LoopExitInstr(Exit), Kind(K), MinMaxKind(MK),
100  UnsafeAlgebraInst(UAI), RecurrenceType(RT), IsSigned(Signed) {
101  CastInsts.insert(CI.begin(), CI.end());
102  }
103 
104  /// This POD struct holds information about a potential recurrence operation.
105  class InstDesc {
106 
107  public:
108  InstDesc(bool IsRecur, Instruction *I, Instruction *UAI = nullptr)
109  : IsRecurrence(IsRecur), PatternLastInst(I), MinMaxKind(MRK_Invalid),
110  UnsafeAlgebraInst(UAI) {}
111 
113  : IsRecurrence(true), PatternLastInst(I), MinMaxKind(K),
114  UnsafeAlgebraInst(UAI) {}
115 
116  bool isRecurrence() { return IsRecurrence; }
117 
118  bool hasUnsafeAlgebra() { return UnsafeAlgebraInst != nullptr; }
119 
120  Instruction *getUnsafeAlgebraInst() { return UnsafeAlgebraInst; }
121 
122  MinMaxRecurrenceKind getMinMaxKind() { return MinMaxKind; }
123 
124  Instruction *getPatternInst() { return PatternLastInst; }
125 
126  private:
127  // Is this instruction a recurrence candidate.
128  bool IsRecurrence;
129  // The last instruction in a min/max pattern (select of the select(icmp())
130  // pattern), or the current recurrence instruction otherwise.
131  Instruction *PatternLastInst;
132  // If this is a min/max pattern the comparison predicate.
133  MinMaxRecurrenceKind MinMaxKind;
134  // Recurrence has unsafe algebra.
135  Instruction *UnsafeAlgebraInst;
136  };
137 
138  /// Returns a struct describing if the instruction 'I' can be a recurrence
139  /// variable of type 'Kind'. If the recurrence is a min/max pattern of
140  /// select(icmp()) this function advances the instruction pointer 'I' from the
141  /// compare instruction to the select instruction and stores this pointer in
142  /// 'PatternLastInst' member of the returned struct.
143  static InstDesc isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
144  InstDesc &Prev, bool HasFunNoNaNAttr);
145 
146  /// Returns true if instruction I has multiple uses in Insts
147  static bool hasMultipleUsesOf(Instruction *I,
149 
150  /// Returns true if all uses of the instruction I is within the Set.
152 
153  /// Returns a struct describing if the instruction if the instruction is a
154  /// Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y)
155  /// or max(X, Y).
156  static InstDesc isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev);
157 
158  /// Returns identity corresponding to the RecurrenceKind.
160 
161  /// Returns the opcode of binary operation corresponding to the
162  /// RecurrenceKind.
163  static unsigned getRecurrenceBinOp(RecurrenceKind Kind);
164 
165  /// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
167  Value *Left, Value *Right);
168 
169  /// Returns true if Phi is a reduction of type Kind and adds it to the
170  /// RecurrenceDescriptor.
171  static bool AddReductionVar(PHINode *Phi, RecurrenceKind Kind, Loop *TheLoop,
172  bool HasFunNoNaNAttr,
173  RecurrenceDescriptor &RedDes);
174 
175  /// Returns true if Phi is a reduction in TheLoop. The RecurrenceDescriptor is
176  /// returned in RedDes.
177  static bool isReductionPHI(PHINode *Phi, Loop *TheLoop,
178  RecurrenceDescriptor &RedDes);
179 
180  /// Returns true if Phi is a first-order recurrence. A first-order recurrence
181  /// is a non-reduction recurrence relation in which the value of the
182  /// recurrence in the current loop iteration equals a value defined in the
183  /// previous iteration.
184  static bool isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop,
185  DominatorTree *DT);
186 
188 
190 
192 
193  Instruction *getLoopExitInstr() { return LoopExitInstr; }
194 
195  /// Returns true if the recurrence has unsafe algebra which requires a relaxed
196  /// floating-point model.
197  bool hasUnsafeAlgebra() { return UnsafeAlgebraInst != nullptr; }
198 
199  /// Returns first unsafe algebra instruction in the PHI node's use-chain.
200  Instruction *getUnsafeAlgebraInst() { return UnsafeAlgebraInst; }
201 
202  /// Returns true if the recurrence kind is an integer kind.
203  static bool isIntegerRecurrenceKind(RecurrenceKind Kind);
204 
205  /// Returns true if the recurrence kind is a floating point kind.
207 
208  /// Returns true if the recurrence kind is an arithmetic kind.
209  static bool isArithmeticRecurrenceKind(RecurrenceKind Kind);
210 
211  /// Determines if Phi may have been type-promoted. If Phi has a single user
212  /// that ANDs the Phi with a type mask, return the user. RT is updated to
213  /// account for the narrower bit width represented by the mask, and the AND
214  /// instruction is added to CI.
215  static Instruction *lookThroughAnd(PHINode *Phi, Type *&RT,
218 
219  /// Returns true if all the source operands of a recurrence are either
220  /// SExtInsts or ZExtInsts. This function is intended to be used with
221  /// lookThroughAnd to determine if the recurrence has been type-promoted. The
222  /// source operands are added to CI, and IsSigned is updated to indicate if
223  /// all source operands are SExtInsts.
224  static bool getSourceExtensionKind(Instruction *Start, Instruction *Exit,
225  Type *RT, bool &IsSigned,
228 
229  /// Returns the type of the recurrence. This type can be narrower than the
230  /// actual type of the Phi if the recurrence has been type-promoted.
231  Type *getRecurrenceType() { return RecurrenceType; }
232 
233  /// Returns a reference to the instructions used for type-promoting the
234  /// recurrence.
236 
237  /// Returns true if all source operands of the recurrence are SExtInsts.
238  bool isSigned() { return IsSigned; }
239 
240 private:
241  // The starting value of the recurrence.
242  // It does not have to be zero!
243  TrackingVH<Value> StartValue;
244  // The instruction who's value is used outside the loop.
245  Instruction *LoopExitInstr;
246  // The kind of the recurrence.
247  RecurrenceKind Kind;
248  // If this a min/max recurrence the kind of recurrence.
249  MinMaxRecurrenceKind MinMaxKind;
250  // First occurrence of unasfe algebra in the PHI's use-chain.
251  Instruction *UnsafeAlgebraInst;
252  // The type of the recurrence.
253  Type *RecurrenceType;
254  // True if all source operands of the recurrence are SExtInsts.
255  bool IsSigned;
256  // Instructions used for type-promoting the recurrence.
258 };
259 
260 /// A struct for saving information about induction variables.
262 public:
263  /// This enum represents the kinds of inductions that we support.
265  IK_NoInduction, ///< Not an induction variable.
266  IK_IntInduction, ///< Integer induction variable. Step = C.
267  IK_PtrInduction, ///< Pointer induction var. Step = C / sizeof(elem).
268  IK_FpInduction ///< Floating point induction variable.
269  };
270 
271 public:
272  /// Default constructor - creates an invalid induction.
274  : StartValue(nullptr), IK(IK_NoInduction), Step(nullptr),
275  InductionBinOp(nullptr) {}
276 
277  /// Get the consecutive direction. Returns:
278  /// 0 - unknown or non-consecutive.
279  /// 1 - consecutive and increasing.
280  /// -1 - consecutive and decreasing.
281  int getConsecutiveDirection() const;
282 
283  /// Compute the transformed value of Index at offset StartValue using step
284  /// StepValue.
285  /// For integer induction, returns StartValue + Index * StepValue.
286  /// For pointer induction, returns StartValue[Index * StepValue].
287  /// FIXME: The newly created binary instructions should contain nsw/nuw
288  /// flags, which can be found from the original scalar operations.
290  const DataLayout& DL) const;
291 
292  Value *getStartValue() const { return StartValue; }
293  InductionKind getKind() const { return IK; }
294  const SCEV *getStep() const { return Step; }
296 
297  /// Returns true if \p Phi is an induction in the loop \p L. If \p Phi is an
298  /// induction, the induction descriptor \p D will contain the data describing
299  /// this induction. If by some other means the caller has a better SCEV
300  /// expression for \p Phi than the one returned by the ScalarEvolution
301  /// analysis, it can be passed through \p Expr.
302  static bool isInductionPHI(PHINode *Phi, const Loop* L, ScalarEvolution *SE,
304  const SCEV *Expr = nullptr);
305 
306  /// Returns true if \p Phi is a floating point induction in the loop \p L.
307  /// If \p Phi is an induction, the induction descriptor \p D will contain
308  /// the data describing this induction.
309  static bool isFPInductionPHI(PHINode *Phi, const Loop* L,
311 
312  /// Returns true if \p Phi is a loop \p L induction, in the context associated
313  /// with the run-time predicate of PSE. If \p Assume is true, this can add
314  /// further SCEV predicates to \p PSE in order to prove that \p Phi is an
315  /// induction.
316  /// If \p Phi is an induction, \p D will contain the data describing this
317  /// induction.
318  static bool isInductionPHI(PHINode *Phi, const Loop* L,
320  InductionDescriptor &D, bool Assume = false);
321 
322  /// Returns true if the induction type is FP and the binary operator does
323  /// not have the "fast-math" property. Such operation requires a relaxed FP
324  /// mode.
326  return InductionBinOp &&
327  !cast<FPMathOperator>(InductionBinOp)->hasUnsafeAlgebra();
328  }
329 
330  /// Returns induction operator that does not have "fast-math" property
331  /// and requires FP unsafe mode.
333  if (!InductionBinOp ||
334  cast<FPMathOperator>(InductionBinOp)->hasUnsafeAlgebra())
335  return nullptr;
336  return InductionBinOp;
337  }
338 
339  /// Returns binary opcode of the induction operator.
341  return InductionBinOp ? InductionBinOp->getOpcode() :
342  Instruction::BinaryOpsEnd;
343  }
344 
345 private:
346  /// Private constructor - used by \c isInductionPHI.
347  InductionDescriptor(Value *Start, InductionKind K, const SCEV *Step,
348  BinaryOperator *InductionBinOp = nullptr);
349 
350  /// Start value.
351  TrackingVH<Value> StartValue;
352  /// Induction kind.
353  InductionKind IK;
354  /// Step value.
355  const SCEV *Step;
356  // Instruction that advances induction variable.
357  BinaryOperator *InductionBinOp;
358 };
359 
360 BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
361  bool PreserveLCSSA);
362 
363 /// Ensures LCSSA form for every instruction from the Worklist in the scope of
364 /// innermost containing loop.
365 ///
366 /// For the given instruction which have uses outside of the loop, an LCSSA PHI
367 /// node is inserted and the uses outside the loop are rewritten to use this
368 /// node.
369 ///
370 /// LoopInfo and DominatorTree are required and, since the routine makes no
371 /// changes to CFG, preserved.
372 ///
373 /// Returns true if any modifications are made.
374 bool formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
375  DominatorTree &DT, LoopInfo &LI);
376 
377 /// \brief Put loop into LCSSA form.
378 ///
379 /// Looks at all instructions in the loop which have uses outside of the
380 /// current loop. For each, an LCSSA PHI node is inserted and the uses outside
381 /// the loop are rewritten to use this node.
382 ///
383 /// LoopInfo and DominatorTree are required and preserved.
384 ///
385 /// If ScalarEvolution is passed in, it will be preserved.
386 ///
387 /// Returns true if any modifications are made to the loop.
388 bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE);
389 
390 /// \brief Put a loop nest into LCSSA form.
391 ///
392 /// This recursively forms LCSSA for a loop nest.
393 ///
394 /// LoopInfo and DominatorTree are required and preserved.
395 ///
396 /// If ScalarEvolution is passed in, it will be preserved.
397 ///
398 /// Returns true if any modifications are made to the loop.
399 bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
400  ScalarEvolution *SE);
401 
402 /// \brief Walk the specified region of the CFG (defined by all blocks
403 /// dominated by the specified block, and that are in the current loop) in
404 /// reverse depth first order w.r.t the DominatorTree. This allows us to visit
405 /// uses before definitions, allowing us to sink a loop body in one pass without
406 /// iteration. Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree,
407 /// DataLayout, TargetLibraryInfo, Loop, AliasSet information for all
408 /// instructions of the loop and loop safety information as
409 /// arguments. Diagnostics is emitted via \p ORE. It returns changed status.
410 bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
411  TargetLibraryInfo *, Loop *, AliasSetTracker *,
412  LoopSafetyInfo *, OptimizationRemarkEmitter *ORE);
413 
414 /// \brief Walk the specified region of the CFG (defined by all blocks
415 /// dominated by the specified block, and that are in the current loop) in depth
416 /// first order w.r.t the DominatorTree. This allows us to visit definitions
417 /// before uses, allowing us to hoist a loop body in one pass without iteration.
418 /// Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree, DataLayout,
419 /// TargetLibraryInfo, Loop, AliasSet information for all instructions of the
420 /// loop and loop safety information as arguments. Diagnostics is emitted via \p
421 /// ORE. It returns changed status.
422 bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
423  TargetLibraryInfo *, Loop *, AliasSetTracker *,
424  LoopSafetyInfo *, OptimizationRemarkEmitter *ORE);
425 
426 /// \brief Try to promote memory values to scalars by sinking stores out of
427 /// the loop and moving loads to before the loop. We do this by looping over
428 /// the stores in the loop, looking for stores to Must pointers which are
429 /// loop invariant. It takes AliasSet, Loop exit blocks vector, loop exit blocks
430 /// insertion point vector, PredIteratorCache, LoopInfo, DominatorTree, Loop,
431 /// AliasSet information for all instructions of the loop and loop safety
432 /// information as arguments. Diagnostics is emitted via \p ORE. It returns
433 /// changed status.
434 bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl<BasicBlock *> &,
435  SmallVectorImpl<Instruction *> &,
436  PredIteratorCache &, LoopInfo *,
437  DominatorTree *, const TargetLibraryInfo *,
438  Loop *, AliasSetTracker *, LoopSafetyInfo *,
439  OptimizationRemarkEmitter *);
440 
441 /// \brief Computes safety information for a loop
442 /// checks loop body & header for the possibility of may throw
443 /// exception, it takes LoopSafetyInfo and loop as argument.
444 /// Updates safety information in LoopSafetyInfo argument.
445 void computeLoopSafetyInfo(LoopSafetyInfo *, Loop *);
446 
447 /// Returns true if the instruction in a loop is guaranteed to execute at least
448 /// once.
449 bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT,
450  const Loop *CurLoop,
451  const LoopSafetyInfo *SafetyInfo);
452 
453 /// \brief Returns the instructions that use values defined in the loop.
454 SmallVector<Instruction *, 8> findDefsUsedOutsideOfLoop(Loop *L);
455 
456 /// \brief Find string metadata for loop
457 ///
458 /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
459 /// operand or null otherwise. If the string metadata is not found return
460 /// Optional's not-a-value.
461 Optional<const MDOperand *> findStringMetadataForLoop(Loop *TheLoop,
462  StringRef Name);
463 
464 /// \brief Set input string into loop metadata by keeping other values intact.
465 void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
466  unsigned V = 0);
467 
468 /// \brief Get a loop's estimated trip count based on branch weight metadata.
469 /// Returns 0 when the count is estimated to be 0, or None when a meaningful
470 /// estimate can not be made.
471 Optional<unsigned> getLoopEstimatedTripCount(Loop *L);
472 
473 /// Helper to consistently add the set of standard passes to a loop pass's \c
474 /// AnalysisUsage.
475 ///
476 /// All loop passes should call this as part of implementing their \c
477 /// getAnalysisUsage.
478 void getLoopAnalysisUsage(AnalysisUsage &AU);
479 
480 /// Returns true if the hoister and sinker can handle this instruction.
481 /// If SafetyInfo is null, we are checking for sinking instructions from
482 /// preheader to loop body (no speculation).
483 /// If SafetyInfo is not null, we are checking for hoisting/sinking
484 /// instructions from loop body to preheader/exit. Check if the instruction
485 /// can execute speculatively.
486 /// If \p ORE is set use it to emit optimization remarks.
487 bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
488  Loop *CurLoop, AliasSetTracker *CurAST,
489  LoopSafetyInfo *SafetyInfo,
490  OptimizationRemarkEmitter *ORE = nullptr);
491 }
492 
493 #endif
MachineLoop * L
Bitwise or logical XOR of numbers.
Definition: LoopUtils.h:73
static bool isArithmeticRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is an arithmetic kind.
Definition: LoopUtils.cpp:65
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static bool isFPInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D)
Returns true if Phi is a floating point induction in the loop L.
Definition: LoopUtils.cpp:769
Instruction * getUnsafeAlgebraInst()
Returns first unsafe algebra instruction in the PHI node's use-chain.
Definition: LoopUtils.h:200
Various leaf nodes.
Definition: ISDOpcodes.h:60
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
aarch64 AArch64 CCMP Pass
Min/max implemented in terms of select(cmp()).
Definition: LoopUtils.h:74
InstDesc(bool IsRecur, Instruction *I, Instruction *UAI=nullptr)
Definition: LoopUtils.h:108
The main scalar evolution driver.
int getConsecutiveDirection() const
Get the consecutive direction.
Definition: LoopUtils.cpp:688
static bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes)
Returns true if Phi is a reduction in TheLoop.
Definition: LoopUtils.cpp:475
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
MinMaxRecurrenceKind getMinMaxRecurrenceKind()
Definition: LoopUtils.h:189
static InstDesc isRecurrenceInstr(Instruction *I, RecurrenceKind Kind, InstDesc &Prev, bool HasFunNoNaNAttr)
Returns a struct describing if the instruction 'I' can be a recurrence variable of type 'Kind'...
Definition: LoopUtils.cpp:424
static Instruction * lookThroughAnd(PHINode *Phi, Type *&RT, SmallPtrSetImpl< Instruction * > &Visited, SmallPtrSetImpl< Instruction * > &CI)
Determines if Phi may have been type-promoted.
Definition: LoopUtils.cpp:79
DenseMap< BasicBlock *, ColorVector > BlockColors
Definition: LoopUtils.h:47
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:299
bool isSigned()
Returns true if all source operands of the recurrence are SExtInsts.
Definition: LoopUtils.h:238
bool hasUnsafeAlgebra()
Returns true if the recurrence has unsafe algebra which requires a relaxed floating-point model...
Definition: LoopUtils.h:197
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:588
static InstDesc isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev)
Returns a struct describing if the instruction if the instruction is a Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y) or max(X, Y).
Definition: LoopUtils.cpp:375
ConstantInt * getConstIntStepValue() const
Definition: LoopUtils.cpp:695
SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
Definition: LoopUtils.cpp:920
void computeLoopSafetyInfo(LoopSafetyInfo *, Loop *)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Definition: LICM.cpp:450
Value * transform(IRBuilder<> &B, Value *Index, ScalarEvolution *SE, const DataLayout &DL) const
Compute the transformed value of Index at offset StartValue using step StepValue. ...
Definition: LoopUtils.cpp:701
Function Alias Analysis false
static bool areAllUsesIn(Instruction *I, SmallPtrSetImpl< Instruction * > &Set)
Returns true if all uses of the instruction I is within the Set.
Definition: LoopUtils.cpp:38
Not an induction variable.
Definition: LoopUtils.h:265
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
This POD struct holds information about a potential recurrence operation.
Definition: LoopUtils.h:105
void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
Bitwise or logical AND of numbers.
Definition: LoopUtils.h:72
static bool hasMultipleUsesOf(Instruction *I, SmallPtrSetImpl< Instruction * > &Insts)
Returns true if instruction I has multiple uses in Insts.
Definition: LoopUtils.cpp:462
Pointer induction var. Step = C / sizeof(elem).
Definition: LoopUtils.h:267
static bool AddReductionVar(PHINode *Phi, RecurrenceKind Kind, Loop *TheLoop, bool HasFunNoNaNAttr, RecurrenceDescriptor &RedDes)
Returns true if Phi is a reduction of type Kind and adds it to the RecurrenceDescriptor.
Definition: LoopUtils.cpp:164
Integer induction variable. Step = C.
Definition: LoopUtils.h:266
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Min/max implemented in terms of select(cmp()).
Definition: LoopUtils.h:77
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:275
This is an important base class in LLVM.
Definition: Constant.h:42
Instruction * getUnsafeAlgebraInst()
Returns induction operator that does not have "fast-math" property and requires FP unsafe mode...
Definition: LoopUtils.h:332
InstDesc(Instruction *I, MinMaxRecurrenceKind K, Instruction *UAI=nullptr)
Definition: LoopUtils.h:112
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:368
Instruction::BinaryOps getInductionOpcode() const
Returns binary opcode of the induction operator.
Definition: LoopUtils.h:340
static bool isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop, DominatorTree *DT)
Returns true if Phi is a first-order recurrence.
Definition: LoopUtils.cpp:524
iterator begin() const
Definition: SmallPtrSet.h:398
Optional< unsigned > getLoopEstimatedTripCount(Loop *L)
Get a loop's estimated trip count based on branch weight metadata.
Definition: LoopUtils.cpp:1076
bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl< BasicBlock * > &, SmallVectorImpl< Instruction * > &, PredIteratorCache &, LoopInfo *, DominatorTree *, const TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *)
Try to promote memory values to scalars by sinking stores out of the loop and moving loads to before ...
Definition: LICM.cpp:925
RecurrenceKind getRecurrenceKind()
Definition: LoopUtils.h:187
Optional< const MDOperand * > findStringMetadataForLoop(Loop *TheLoop, StringRef Name)
Find string metadata for loop.
Definition: LoopUtils.cpp:1001
static unsigned getRecurrenceBinOp(RecurrenceKind Kind)
Returns the opcode of binary operation corresponding to the RecurrenceKind.
Definition: LoopUtils.cpp:589
Value * getStartValue() const
Definition: LoopUtils.h:292
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Definition: LoopUtils.h:63
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
Definition: LCSSA.cpp:252
BinaryOps getOpcode() const
Definition: InstrTypes.h:541
MinMaxRecurrenceKind getMinMaxKind()
Definition: LoopUtils.h:122
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
A struct for saving information about induction variables.
Definition: LoopUtils.h:261
Bitwise or logical OR of numbers.
Definition: LoopUtils.h:71
bool formLCSSAForInstructions(SmallVectorImpl< Instruction * > &Worklist, DominatorTree &DT, LoopInfo &LI)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop...
Definition: LCSSA.cpp:72
static bool getSourceExtensionKind(Instruction *Start, Instruction *Exit, Type *RT, bool &IsSigned, SmallPtrSetImpl< Instruction * > &Visited, SmallPtrSetImpl< Instruction * > &CI)
Returns true if all the source operands of a recurrence are either SExtInsts or ZExtInsts.
Definition: LoopUtils.cpp:103
static bool isFloatingPointRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is a floating point kind.
Definition: LoopUtils.cpp:61
InductionKind getKind() const
Definition: LoopUtils.h:293
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
RecurrenceDescriptor(Value *Start, Instruction *Exit, RecurrenceKind K, MinMaxRecurrenceKind MK, Instruction *UAI, Type *RT, bool Signed, SmallPtrSetImpl< Instruction * > &CI)
Definition: LoopUtils.h:96
bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *ORE)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
Definition: LICM.cpp:391
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:41
static Constant * getRecurrenceIdentity(RecurrenceKind K, Type *Tp)
Returns identity corresponding to the RecurrenceKind.
Definition: LoopUtils.cpp:563
bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *ORE)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
Definition: LICM.cpp:331
static Value * createMinMaxOp(IRBuilder<> &Builder, MinMaxRecurrenceKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
Definition: LoopUtils.cpp:614
bool hasUnsafeAlgebra()
Returns true if the induction type is FP and the binary operator does not have the "fast-math" proper...
Definition: LoopUtils.h:325
iterator end() const
Definition: SmallPtrSet.h:405
Basic Alias true
Captures loop safety information.
Definition: LoopUtils.h:42
This class represents an analyzed expression in the program.
static bool isIntegerRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is an integer kind.
Definition: LoopUtils.cpp:46
Floating point induction variable.
Definition: LoopUtils.h:268
Instruction * getLoopExitInstr()
Definition: LoopUtils.h:193
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
#define I(x, y, z)
Definition: MD5.cpp:54
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:938
const SCEV * getStep() const
Definition: LoopUtils.h:294
InductionDescriptor()
Default constructor - creates an invalid induction.
Definition: LoopUtils.h:273
SmallPtrSet< Instruction *, 8 > & getCastInsts()
Returns a reference to the instructions used for type-promoting the recurrence.
Definition: LoopUtils.h:235
LLVM Value Representation.
Definition: Value.h:71
TrackingVH< Value > getRecurrenceStartValue()
Definition: LoopUtils.h:191
bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE=nullptr)
Returns true if the hoister and sinker can handle this instruction.
Definition: LICM.cpp:480
InductionKind
This enum represents the kinds of inductions that we support.
Definition: LoopUtils.h:264
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo)
Returns true if the instruction in a loop is guaranteed to execute at least once. ...
Definition: LoopUtils.cpp:1036
static bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr)
Returns true if Phi is an induction in the loop L.
Definition: LoopUtils.cpp:854
RecurrenceKind
This enum represents the kinds of recurrences that we support.
Definition: LoopUtils.h:67
Type * getRecurrenceType()
Returns the type of the recurrence.
Definition: LoopUtils.h:231