LLVM 17.0.0git
LoopUtils.h
Go to the documentation of this file.
1//===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -------*- C++ -*-===//
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 file defines some loop transformation utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
14#define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
15
19
20namespace llvm {
21
22template <typename T> class DomTreeNodeBase;
23using DomTreeNode = DomTreeNodeBase<BasicBlock>;
24class AssumptionCache;
25class StringRef;
26class AnalysisUsage;
27class TargetTransformInfo;
28class AAResults;
29class BasicBlock;
30class ICFLoopSafetyInfo;
31class IRBuilderBase;
32class Loop;
33class LoopInfo;
34class MemoryAccess;
35class MemorySSA;
36class MemorySSAUpdater;
37class OptimizationRemarkEmitter;
38class PredIteratorCache;
39class ScalarEvolution;
40class SCEV;
41class SCEVExpander;
42class TargetLibraryInfo;
43class LPPassManager;
44class Instruction;
45struct RuntimeCheckingPtrGroup;
46typedef std::pair<const RuntimeCheckingPtrGroup *,
47 const RuntimeCheckingPtrGroup *>
49
50template <typename T, unsigned N> class SmallSetVector;
51template <typename T, unsigned N> class SmallPriorityWorklist;
52
53BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
54 MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
55
56/// Ensure that all exit blocks of the loop are dedicated exits.
57///
58/// For any loop exit block with non-loop predecessors, we split the loop
59/// predecessors to use a dedicated loop exit block. We update the dominator
60/// tree and loop info if provided, and will preserve LCSSA if requested.
61bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
62 MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
63
64/// Ensures LCSSA form for every instruction from the Worklist in the scope of
65/// innermost containing loop.
66///
67/// For the given instruction which have uses outside of the loop, an LCSSA PHI
68/// node is inserted and the uses outside the loop are rewritten to use this
69/// node.
70///
71/// LoopInfo and DominatorTree are required and, since the routine makes no
72/// changes to CFG, preserved.
73///
74/// Returns true if any modifications are made.
75///
76/// This function may introduce unused PHI nodes. If \p PHIsToRemove is not
77/// nullptr, those are added to it (before removing, the caller has to check if
78/// they still do not have any uses). Otherwise the PHIs are directly removed.
80 SmallVectorImpl<Instruction *> &Worklist, const DominatorTree &DT,
81 const LoopInfo &LI, ScalarEvolution *SE, IRBuilderBase &Builder,
82 SmallVectorImpl<PHINode *> *PHIsToRemove = nullptr);
83
84/// Put loop into LCSSA form.
85///
86/// Looks at all instructions in the loop which have uses outside of the
87/// current loop. For each, an LCSSA PHI node is inserted and the uses outside
88/// the loop are rewritten to use this node. Sub-loops must be in LCSSA form
89/// already.
90///
91/// LoopInfo and DominatorTree are required and preserved.
92///
93/// If ScalarEvolution is passed in, it will be preserved.
94///
95/// Returns true if any modifications are made to the loop.
96bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
97 ScalarEvolution *SE);
98
99/// Put a loop nest into LCSSA form.
100///
101/// This recursively forms LCSSA for a loop nest.
102///
103/// LoopInfo and DominatorTree are required and preserved.
104///
105/// If ScalarEvolution is passed in, it will be preserved.
106///
107/// Returns true if any modifications are made to the loop.
108bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
109 ScalarEvolution *SE);
110
111/// Flags controlling how much is checked when sinking or hoisting
112/// instructions. The number of memory access in the loop (and whether there
113/// are too many) is determined in the constructors when using MemorySSA.
115public:
116 // Explicitly set limits.
119 Loop &L, MemorySSA &MSSA);
120 // Use default limits.
122
123 void setIsSink(bool B) { IsSink = B; }
124 bool getIsSink() { return IsSink; }
128
129protected:
130 bool NoOfMemAccTooLarge = false;
131 unsigned LicmMssaOptCounter = 0;
134 bool IsSink;
135};
136
137/// Walk the specified region of the CFG (defined by all blocks
138/// dominated by the specified block, and that are in the current loop) in
139/// reverse depth first order w.r.t the DominatorTree. This allows us to visit
140/// uses before definitions, allowing us to sink a loop body in one pass without
141/// iteration. Takes DomTreeNode, AAResults, LoopInfo, DominatorTree,
142/// TargetLibraryInfo, Loop, AliasSet information for all
143/// instructions of the loop and loop safety information as
144/// arguments. Diagnostics is emitted via \p ORE. It returns changed status.
145/// \p CurLoop is a loop to do sinking on. \p OutermostLoop is used only when
146/// this function is called by \p sinkRegionForLoopNest.
151 Loop *OutermostLoop = nullptr);
152
153/// Call sinkRegion on loops contained within the specified loop
154/// in order from innermost to outermost.
160
161/// Walk the specified region of the CFG (defined by all blocks
162/// dominated by the specified block, and that are in the current loop) in depth
163/// first order w.r.t the DominatorTree. This allows us to visit definitions
164/// before uses, allowing us to hoist a loop body in one pass without iteration.
165/// Takes DomTreeNode, AAResults, LoopInfo, DominatorTree,
166/// TargetLibraryInfo, Loop, AliasSet information for all
167/// instructions of the loop and loop safety information as arguments.
168/// Diagnostics is emitted via \p ORE. It returns changed status.
169/// \p AllowSpeculation is whether values should be hoisted even if they are not
170/// guaranteed to execute in the loop, but are safe to speculatively execute.
175 bool AllowSpeculation);
176
177/// Return true if the induction variable \p IV in a Loop whose latch is
178/// \p LatchBlock would become dead if the exit test \p Cond were removed.
179/// Conservatively returns false if analysis is insufficient.
180bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond);
181
182/// This function deletes dead loops. The caller of this function needs to
183/// guarantee that the loop is infact dead.
184/// The function requires a bunch or prerequisites to be present:
185/// - The loop needs to be in LCSSA form
186/// - The loop needs to have a Preheader
187/// - A unique dedicated exit block must exist
188///
189/// This also updates the relevant analysis information in \p DT, \p SE, \p LI
190/// and \p MSSA if pointers to those are provided.
191/// It also updates the loop PM if an updater struct is provided.
192
194 LoopInfo *LI, MemorySSA *MSSA = nullptr);
195
196/// Remove the backedge of the specified loop. Handles loop nests and general
197/// loop structures subject to the precondition that the loop has no parent
198/// loop and has a single latch block. Preserves all listed analyses.
200 LoopInfo &LI, MemorySSA *MSSA);
201
202/// Try to promote memory values to scalars by sinking stores out of
203/// the loop and moving loads to before the loop. We do this by looping over
204/// the stores in the loop, looking for stores to Must pointers which are
205/// loop invariant. It takes a set of must-alias values, Loop exit blocks
206/// vector, loop exit blocks insertion point vector, PredIteratorCache,
207/// LoopInfo, DominatorTree, Loop, AliasSet information for all instructions
208/// of the loop and loop safety information as arguments.
209/// Diagnostics is emitted via \p ORE. It returns changed status.
210/// \p AllowSpeculation is whether values should be hoisted even if they are not
211/// guaranteed to execute in the loop, but are safe to speculatively execute.
218 bool AllowSpeculation, bool HasReadsOutsideSet);
219
220/// Does a BFS from a given node to all of its children inside a given loop.
221/// The returned vector of nodes includes the starting point.
223 const Loop *CurLoop);
224
225/// Returns the instructions that use values defined in the loop.
227
228/// Find a combination of metadata ("llvm.loop.vectorize.width" and
229/// "llvm.loop.vectorize.scalable.enable") for a loop and use it to construct a
230/// ElementCount. If the metadata "llvm.loop.vectorize.width" cannot be found
231/// then std::nullopt is returned.
232std::optional<ElementCount>
234
235/// Create a new loop identifier for a loop created from a loop transformation.
236///
237/// @param OrigLoopID The loop ID of the loop before the transformation.
238/// @param FollowupAttrs List of attribute names that contain attributes to be
239/// added to the new loop ID.
240/// @param InheritOptionsAttrsPrefix Selects which attributes should be inherited
241/// from the original loop. The following values
242/// are considered:
243/// nullptr : Inherit all attributes from @p OrigLoopID.
244/// "" : Do not inherit any attribute from @p OrigLoopID; only use
245/// those specified by a followup attribute.
246/// "<prefix>": Inherit all attributes except those which start with
247/// <prefix>; commonly used to remove metadata for the
248/// applied transformation.
249/// @param AlwaysNew If true, do not try to reuse OrigLoopID and never return
250/// std::nullopt.
251///
252/// @return The loop ID for the after-transformation loop. The following values
253/// can be returned:
254/// std::nullopt : No followup attribute was found; it is up to the
255/// transformation to choose attributes that make sense.
256/// @p OrigLoopID: The original identifier can be reused.
257/// nullptr : The new loop has no attributes.
258/// MDNode* : A new unique loop identifier.
259std::optional<MDNode *>
260makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef<StringRef> FollowupAttrs,
261 const char *InheritOptionsAttrsPrefix = "",
262 bool AlwaysNew = false);
263
264/// Look for the loop attribute that disables all transformation heuristic.
265bool hasDisableAllTransformsHint(const Loop *L);
266
267/// Look for the loop attribute that disables the LICM transformation heuristics.
269
270/// The mode sets how eager a transformation should be applied.
272 /// The pass can use heuristics to determine whether a transformation should
273 /// be applied.
275
276 /// The transformation should be applied without considering a cost model.
278
279 /// The transformation should not be applied.
281
282 /// Force is a flag and should not be used alone.
283 TM_Force = 0x04,
284
285 /// The transformation was directed by the user, e.g. by a #pragma in
286 /// the source code. If the transformation could not be applied, a
287 /// warning should be emitted.
289
290 /// The transformation must not be applied. For instance, `#pragma clang loop
291 /// unroll(disable)` explicitly forbids any unrolling to take place. Unlike
292 /// general loop metadata, it must not be dropped. Most passes should not
293 /// behave differently under TM_Disable and TM_SuppressedByUser.
296
297/// @{
298/// Get the mode for LLVM's supported loop transformations.
304/// @}
305
306/// Set input string into loop metadata by keeping other values intact.
307/// If the string is already in loop metadata update value if it is
308/// different.
309void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
310 unsigned V = 0);
311
312/// Returns a loop's estimated trip count based on branch weight metadata.
313/// In addition if \p EstimatedLoopInvocationWeight is not null it is
314/// initialized with weight of loop's latch leading to the exit.
315/// Returns 0 when the count is estimated to be 0, or std::nullopt when a
316/// meaningful estimate can not be made.
317std::optional<unsigned>
319 unsigned *EstimatedLoopInvocationWeight = nullptr);
320
321/// Set a loop's branch weight metadata to reflect that loop has \p
322/// EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight exits
323/// through latch. Returns true if metadata is successfully updated, false
324/// otherwise. Note that loop must have a latch block which controls loop exit
325/// in order to succeed.
326bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
327 unsigned EstimatedLoopInvocationWeight);
328
329/// Check inner loop (L) backedge count is known to be invariant on all
330/// iterations of its outer loop. If the loop has no parent, this is trivially
331/// true.
332bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE);
333
334/// Helper to consistently add the set of standard passes to a loop pass's \c
335/// AnalysisUsage.
336///
337/// All loop passes should call this as part of implementing their \c
338/// getAnalysisUsage.
339void getLoopAnalysisUsage(AnalysisUsage &AU);
340
341/// Returns true if is legal to hoist or sink this instruction disregarding the
342/// possible introduction of faults. Reasoning about potential faulting
343/// instructions is the responsibility of the caller since it is challenging to
344/// do efficiently from within this routine.
345/// \p TargetExecutesOncePerLoop is true only when it is guaranteed that the
346/// target executes at most once per execution of the loop body. This is used
347/// to assess the legality of duplicating atomic loads. Generally, this is
348/// true when moving out of loop and not true when moving into loops.
349/// If \p ORE is set use it to emit optimization remarks.
350bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
351 Loop *CurLoop, MemorySSAUpdater &MSSAU,
352 bool TargetExecutesOncePerLoop,
353 SinkAndHoistLICMFlags &LICMFlags,
354 OptimizationRemarkEmitter *ORE = nullptr);
355
356/// Returns the comparison predicate used when expanding a min/max reduction.
358
359/// See RecurrenceDescriptor::isSelectCmpPattern for a description of the
360/// pattern we are trying to match. In this pattern we are only ever selecting
361/// between two values: 1) an initial PHI start value, and 2) a loop invariant
362/// value. This function uses \p LoopExitInst to determine 2), which we then use
363/// to select between \p Left and \p Right. Any lane value in \p Left that
364/// matches 2) will be merged into \p Right.
365Value *createSelectCmpOp(IRBuilderBase &Builder, Value *StartVal, RecurKind RK,
366 Value *Left, Value *Right);
367
368/// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
369/// The Builder's fast-math-flags must be set to propagate the expected values.
370Value *createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left,
371 Value *Right);
372
373/// Generates an ordered vector reduction using extracts to reduce the value.
374Value *getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src,
375 unsigned Op, RecurKind MinMaxKind = RecurKind::None);
376
377/// Generates a vector reduction using shufflevectors to reduce the value.
378/// Fast-math-flags are propagated using the IRBuilder's setting.
379Value *getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op,
380 RecurKind MinMaxKind = RecurKind::None);
381
382/// Create a target reduction of the given vector. The reduction operation
383/// is described by the \p Opcode parameter. min/max reductions require
384/// additional information supplied in \p RdxKind.
385/// The target is queried to determine if intrinsics or shuffle sequences are
386/// required to implement the reduction.
387/// Fast-math-flags are propagated using the IRBuilder's setting.
388Value *createSimpleTargetReduction(IRBuilderBase &B,
389 const TargetTransformInfo *TTI, Value *Src,
390 RecurKind RdxKind);
391
392/// Create a target reduction of the given vector \p Src for a reduction of the
393/// kind RecurKind::SelectICmp or RecurKind::SelectFCmp. The reduction operation
394/// is described by \p Desc.
395Value *createSelectCmpTargetReduction(IRBuilderBase &B,
396 const TargetTransformInfo *TTI,
397 Value *Src,
398 const RecurrenceDescriptor &Desc,
399 PHINode *OrigPhi);
400
401/// Create a generic target reduction using a recurrence descriptor \p Desc
402/// The target is queried to determine if intrinsics or shuffle sequences are
403/// required to implement the reduction.
404/// Fast-math-flags are propagated using the RecurrenceDescriptor.
405Value *createTargetReduction(IRBuilderBase &B, const TargetTransformInfo *TTI,
406 const RecurrenceDescriptor &Desc, Value *Src,
407 PHINode *OrigPhi = nullptr);
408
409/// Create an ordered reduction intrinsic using the given recurrence
410/// descriptor \p Desc.
411Value *createOrderedReduction(IRBuilderBase &B,
412 const RecurrenceDescriptor &Desc, Value *Src,
413 Value *Start);
414
415/// Get the intersection (logical and) of all of the potential IR flags
416/// of each scalar operation (VL) that will be converted into a vector (I).
417/// If OpValue is non-null, we only consider operations similar to OpValue
418/// when intersecting.
419/// Flag set: NSW, NUW (if IncludeWrapFlags is true), exact, and all of
420/// fast-math.
421void propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue = nullptr,
422 bool IncludeWrapFlags = true);
423
424/// Returns true if we can prove that \p S is defined and always negative in
425/// loop \p L.
426bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE);
427
428/// Returns true if we can prove that \p S is defined and always non-negative in
429/// loop \p L.
430bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
431 ScalarEvolution &SE);
432
433/// Returns true if \p S is defined and never is equal to signed/unsigned max.
434bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
435 bool Signed);
436
437/// Returns true if \p S is defined and never is equal to signed/unsigned min.
438bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
439 bool Signed);
440
448
449/// If the final value of any expressions that are recurrent in the loop can
450/// be computed, substitute the exit values from the loop into any instructions
451/// outside of the loop that use the final values of the current expressions.
452/// Return the number of loop exit values that have been replaced, and the
453/// corresponding phi node will be added to DeadInsts.
454int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
455 ScalarEvolution *SE, const TargetTransformInfo *TTI,
456 SCEVExpander &Rewriter, DominatorTree *DT,
458 SmallVector<WeakTrackingVH, 16> &DeadInsts);
459
460/// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
461/// \p OrigLoop and the following distribution of \p OrigLoop iteration among \p
462/// UnrolledLoop and \p RemainderLoop. \p UnrolledLoop receives weights that
463/// reflect TC/UF iterations, and \p RemainderLoop receives weights that reflect
464/// the remaining TC%UF iterations.
465///
466/// Note that \p OrigLoop may be equal to either \p UnrolledLoop or \p
467/// RemainderLoop in which case weights for \p OrigLoop are updated accordingly.
468/// Note also behavior is undefined if \p UnrolledLoop and \p RemainderLoop are
469/// equal. \p UF must be greater than zero.
470/// If \p OrigLoop has no profile info associated nothing happens.
471///
472/// This utility may be useful for such optimizations as unroller and
473/// vectorizer as it's typical transformation for them.
474void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
475 Loop *RemainderLoop, uint64_t UF);
476
477/// Utility that implements appending of loops onto a worklist given a range.
478/// We want to process loops in postorder, but the worklist is a LIFO data
479/// structure, so we append to it in *reverse* postorder.
480/// For trees, a preorder traversal is a viable reverse postorder, so we
481/// actually append using a preorder walk algorithm.
482template <typename RangeT>
483void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist<Loop *, 4> &);
484/// Utility that implements appending of loops onto a worklist given a range.
485/// It has the same behavior as appendLoopsToWorklist, but assumes the range of
486/// loops has already been reversed, so it processes loops in the given order.
487template <typename RangeT>
488void appendReversedLoopsToWorklist(RangeT &&,
489 SmallPriorityWorklist<Loop *, 4> &);
490
491/// Utility that implements appending of loops onto a worklist given LoopInfo.
492/// Calls the templated utility taking a Range of loops, handing it the Loops
493/// in LoopInfo, iterated in reverse. This is because the loops are stored in
494/// RPO w.r.t. the control flow graph in LoopInfo. For the purpose of unrolling,
495/// loop deletion, and LICM, we largely want to work forward across the CFG so
496/// that we visit defs before uses and can propagate simplifications from one
497/// loop nest into the next. Calls appendReversedLoopsToWorklist with the
498/// already reversed loops in LI.
499/// FIXME: Consider changing the order in LoopInfo.
500void appendLoopsToWorklist(LoopInfo &, SmallPriorityWorklist<Loop *, 4> &);
501
502/// Recursively clone the specified loop and all of its children,
503/// mapping the blocks with the specified map.
504Loop *cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
505 LoopInfo *LI, LPPassManager *LPM);
506
507/// Add code that checks at runtime if the accessed arrays in \p PointerChecks
508/// overlap. Returns the final comparator value or NULL if no check is needed.
509Value *
510addRuntimeChecks(Instruction *Loc, Loop *TheLoop,
511 const SmallVectorImpl<RuntimePointerCheck> &PointerChecks,
512 SCEVExpander &Expander);
513
515 Instruction *Loc, ArrayRef<PointerDiffInfo> Checks, SCEVExpander &Expander,
516 function_ref<Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC);
517
518/// Struct to hold information about a partially invariant condition.
520 /// Instructions that need to be duplicated and checked for the unswitching
521 /// condition.
523
524 /// Constant to indicate for which value the condition is invariant.
526
527 /// True if the partially invariant path is no-op (=does not have any
528 /// side-effects and no loop value is used outside the loop).
529 bool PathIsNoop = true;
530
531 /// If the partially invariant path reaches a single exit block, ExitForPath
532 /// is set to that block. Otherwise it is nullptr.
534};
535
536/// Check if the loop header has a conditional branch that is not
537/// loop-invariant, because it involves load instructions. If all paths from
538/// either the true or false successor to the header or loop exists do not
539/// modify the memory feeding the condition, perform 'partial unswitching'. That
540/// is, duplicate the instructions feeding the condition in the pre-header. Then
541/// unswitch on the duplicated condition. The condition is now known in the
542/// unswitched version for the 'invariant' path through the original loop.
543///
544/// If the branch condition of the header is partially invariant, return a pair
545/// containing the instructions to duplicate and a boolean Constant to update
546/// the condition in the loops created for the true or false successors.
547std::optional<IVConditionInfo> hasPartialIVCondition(const Loop &L,
548 unsigned MSSAThreshold,
549 const MemorySSA &MSSA,
550 AAResults &AA);
551
552} // end namespace llvm
553
554#endif // LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
SmallVector< MachineOperand, 4 > Cond
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
early cse Early CSE w MemorySSA
Definition: EarlyCSE.cpp:1809
static cl::opt< ReplaceExitVal > ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl, "never", "never replace exit value"), clEnumValN(OnlyCheapRepl, "cheap", "only replace exit value when the cost is cheap"), clEnumValN(UnusedIndVarInLoop, "unusedindvarinloop", "only replace exit value when it is an unused " "induction variable in the loop and has cheap replacement cost"), clEnumValN(NoHardUse, "noharduse", "only replace exit values when loop def likely dead"), clEnumValN(AlwaysRepl, "always", "always replace exit value whenever possible")))
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< unsigned > MSSAThreshold("simple-loop-unswitch-memoryssa-threshold", cl::desc("Max number of memory uses to explore during " "partial unswitching analysis"), cl::init(100), cl::Hidden)
static const uint32_t IV[8]
Definition: blake3_impl.h:77
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:718
This is an important base class in LLVM.
Definition: Constant.h:41
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
This implementation of LoopSafetyInfo use ImplicitControlFlowTracking to give precise answers on "may...
Definition: MustExecute.h:132
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
Metadata node.
Definition: Metadata.h:943
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:700
The optimization diagnostic interface.
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries.
The main scalar evolution driver.
Flags controlling how much is checked when sinking or hoisting instructions.
Definition: LoopUtils.h:114
unsigned LicmMssaNoAccForPromotionCap
Definition: LoopUtils.h:133
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:301
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM Value Representation.
Definition: Value.h:74
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< ElementCount > getOptionalElementCountLoopAttribute(const Loop *TheLoop)
Find a combination of metadata ("llvm.loop.vectorize.width" and "llvm.loop.vectorize....
Definition: LoopUtils.cpp:250
SmallVector< DomTreeNode *, 16 > collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop)
Does a BFS from a given node to all of its children inside a given loop.
Definition: LoopUtils.cpp:450
std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Returns a loop's estimated trip count based on branch weight metadata.
Definition: LoopUtils.cpp:824
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, Loop *CurLoop, MemorySSAUpdater &MSSAU, bool TargetExecutesOncePerLoop, SinkAndHoistLICMFlags &LICMFlags, OptimizationRemarkEmitter *ORE=nullptr)
Returns true if is legal to hoist or sink this instruction disregarding the possible introduction of ...
Definition: LICM.cpp:1160
Value * createSelectCmpOp(IRBuilderBase &Builder, Value *StartVal, RecurKind RK, Value *Left, Value *Right)
See RecurrenceDescriptor::isSelectCmpPattern for a description of the pattern we are trying to match.
Definition: LoopUtils.cpp:915
Value * createSimpleTargetReduction(IRBuilderBase &B, const TargetTransformInfo *TTI, Value *Src, RecurKind RdxKind)
Create a target reduction of the given vector.
Definition: LoopUtils.cpp:1038
void appendReversedLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
Definition: LoopUtils.cpp:1512
Value * createTargetReduction(IRBuilderBase &B, const TargetTransformInfo *TTI, const RecurrenceDescriptor &Desc, Value *Src, PHINode *OrigPhi=nullptr)
Create a generic target reduction using a recurrence descriptor Desc The target is queried to determi...
Definition: LoopUtils.cpp:1076
bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:410
std::optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
Definition: LoopUtils.cpp:263
Value * createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
Definition: LoopUtils.cpp:924
void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
Definition: LoopUtils.cpp:214
bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned max.
Definition: LoopUtils.cpp:1150
TransformationMode hasVectorizeTransformation(const Loop *L)
Definition: LoopUtils.cpp:391
bool hoistRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *, AssumptionCache *, TargetLibraryInfo *, Loop *, MemorySSAUpdater &, ScalarEvolution *, ICFLoopSafetyInfo *, SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *, bool, bool AllowSpeculation)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block,...
Definition: LICM.cpp:860
SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
Definition: LoopUtils.cpp:123
TransformationMode hasUnrollAndJamTransformation(const Loop *L)
Definition: LoopUtils.cpp:373
void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI, MemorySSA *MSSA=nullptr)
This function deletes dead loops.
Definition: LoopUtils.cpp:482
Value * getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op, RecurKind MinMaxKind=RecurKind::None)
Generates a vector reduction using shufflevectors to reduce the value.
Definition: LoopUtils.cpp:958
bool hasDisableAllTransformsHint(const Loop *L)
Look for the loop attribute that disables all transformation heuristic.
Definition: LoopUtils.cpp:344
Value * createOrderedReduction(IRBuilderBase &B, const RecurrenceDescriptor &Desc, Value *Src, Value *Start)
Create an ordered reduction intrinsic using the given recurrence descriptor Desc.
Definition: LoopUtils.cpp:1093
TransformationMode hasUnrollTransformation(const Loop *L)
Definition: LoopUtils.cpp:352
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:96
TransformationMode hasDistributeTransformation(const Loop *L)
Definition: LoopUtils.cpp:427
void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA)
Remove the backedge of the specified loop.
Definition: LoopUtils.cpp:699
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:141
void propagateIRFlags(Value *I, ArrayRef< Value * > VL, Value *OpValue=nullptr, bool IncludeWrapFlags=true)
Get the intersection (logical and) of all of the potential IR flags of each scalar operation (VL) tha...
Definition: LoopUtils.cpp:1105
TargetTransformInfo TTI
CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK)
Returns the comparison predicate used when expanding a min/max reduction.
Definition: LoopUtils.cpp:896
TransformationMode hasLICMVersioningTransformation(const Loop *L)
Definition: LoopUtils.cpp:437
TransformationMode
The mode sets how eager a transformation should be applied.
Definition: LoopUtils.h:271
@ TM_Unspecified
The pass can use heuristics to determine whether a transformation should be applied.
Definition: LoopUtils.h:274
@ TM_SuppressedByUser
The transformation must not be applied.
Definition: LoopUtils.h:294
@ TM_ForcedByUser
The transformation was directed by the user, e.g.
Definition: LoopUtils.h:288
@ TM_Disable
The transformation should not be applied.
Definition: LoopUtils.h:280
@ TM_Force
Force is a flag and should not be used alone.
Definition: LoopUtils.h:283
@ TM_Enable
The transformation should be applied without considering a cost model.
Definition: LoopUtils.h:277
bool hasDisableLICMTransformsHint(const Loop *L)
Look for the loop attribute that disables the LICM transformation heuristics.
Definition: LoopUtils.cpp:348
Value * addRuntimeChecks(Instruction *Loc, Loop *TheLoop, const SmallVectorImpl< RuntimePointerCheck > &PointerChecks, SCEVExpander &Expander)
Add code that checks at runtime if the accessed arrays in PointerChecks overlap.
Definition: LoopUtils.cpp:1626
bool formLCSSAForInstructions(SmallVectorImpl< Instruction * > &Worklist, const DominatorTree &DT, const LoopInfo &LI, ScalarEvolution *SE, IRBuilderBase &Builder, SmallVectorImpl< PHINode * > *PHIsToRemove=nullptr)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop.
Definition: LCSSA.cpp:78
RecurKind
These are the kinds of recurrences that we support.
Definition: IVDescriptors.h:35
@ None
Not a recurrence.
bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount, unsigned EstimatedLoopInvocationWeight)
Set a loop's branch weight metadata to reflect that loop has EstimatedTripCount iterations and Estima...
Definition: LoopUtils.cpp:842
void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop, Loop *RemainderLoop, uint64_t UF)
Set weights for UnrolledLoop and RemainderLoop based on weights for OrigLoop and the following distri...
Definition: LoopUtils.cpp:1484
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
Definition: LoopUtils.cpp:57
void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
Definition: LoopUtils.cpp:1537
bool promoteLoopAccessesToScalars(const SmallSetVector< Value *, 8 > &, SmallVectorImpl< BasicBlock * > &, SmallVectorImpl< Instruction * > &, SmallVectorImpl< MemoryAccess * > &, PredIteratorCache &, LoopInfo *, DominatorTree *, AssumptionCache *AC, const TargetLibraryInfo *, TargetTransformInfo *, Loop *, MemorySSAUpdater &, ICFLoopSafetyInfo *, OptimizationRemarkEmitter *, bool AllowSpeculation, bool HasReadsOutsideSet)
Try to promote memory values to scalars by sinking stores out of the loop and moving loads to before ...
Definition: LICM.cpp:1962
bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always negative in loop L.
Definition: LoopUtils.cpp:1125
Value * createSelectCmpTargetReduction(IRBuilderBase &B, const TargetTransformInfo *TTI, Value *Src, const RecurrenceDescriptor &Desc, PHINode *OrigPhi)
Create a target reduction of the given vector Src for a reduction of the kind RecurKind::SelectICmp o...
Definition: LoopUtils.cpp:998
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE)
Check inner loop (L) backedge count is known to be invariant on all iterations of its outer loop.
Definition: LoopUtils.cpp:874
bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond)
Return true if the induction variable IV in a Loop whose latch is LatchBlock would become dead if the...
Definition: LoopUtils.cpp:469
int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, SCEVExpander &Rewriter, DominatorTree *DT, ReplaceExitVal ReplaceExitValue, SmallVector< WeakTrackingVH, 16 > &DeadInsts)
If the final value of any expressions that are recurrent in the loop can be computed,...
Definition: LoopUtils.cpp:1272
bool sinkRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, TargetTransformInfo *, Loop *CurLoop, MemorySSAUpdater &, ICFLoopSafetyInfo *, SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *, Loop *OutermostLoop=nullptr)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block,...
Definition: LICM.cpp:540
Value * addDiffRuntimeChecks(Instruction *Loc, ArrayRef< PointerDiffInfo > Checks, SCEVExpander &Expander, function_ref< Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC)
Definition: LoopUtils.cpp:1681
ReplaceExitVal
Definition: LoopUtils.h:441
@ UnusedIndVarInLoop
Definition: LoopUtils.h:445
@ OnlyCheapRepl
Definition: LoopUtils.h:443
@ NeverRepl
Definition: LoopUtils.h:442
@ NoHardUse
Definition: LoopUtils.h:444
@ AlwaysRepl
Definition: LoopUtils.h:446
std::optional< IVConditionInfo > hasPartialIVCondition(const Loop &L, unsigned MSSAThreshold, const MemorySSA &MSSA, AAResults &AA)
Check if the loop header has a conditional branch that is not loop-invariant, because it involves loa...
Definition: LoopUtils.cpp:1720
bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
Definition: LCSSA.cpp:341
bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned min.
Definition: LoopUtils.cpp:1139
bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always non-negative in loop L.
Definition: LoopUtils.cpp:1132
bool sinkRegionForLoopNest(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, TargetTransformInfo *, Loop *, MemorySSAUpdater &, ICFLoopSafetyInfo *, SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *)
Call sinkRegion on loops contained within the specified loop in order from innermost to outermost.
Definition: LICM.cpp:607
Value * getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src, unsigned Op, RecurKind MinMaxKind=RecurKind::None)
Generates an ordered vector reduction using extracts to reduce the value.
Definition: LoopUtils.cpp:933
Loop * cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
Recursively clone the specified loop and all of its children, mapping the blocks with the specified m...
Definition: LoopUtils.cpp:1554
#define N
Struct to hold information about a partially invariant condition.
Definition: LoopUtils.h:519
BasicBlock * ExitForPath
If the partially invariant path reaches a single exit block, ExitForPath is set to that block.
Definition: LoopUtils.h:533
SmallVector< Instruction * > InstToDuplicate
Instructions that need to be duplicated and checked for the unswitching condition.
Definition: LoopUtils.h:522
Constant * KnownValue
Constant to indicate for which value the condition is invariant.
Definition: LoopUtils.h:525
bool PathIsNoop
True if the partially invariant path is no-op (=does not have any side-effects and no loop value is u...
Definition: LoopUtils.h:529