|
|
|||
File indexing completed on 2026-05-10 08:44:43
0001 //===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file defines some loop transformation utilities. 0010 // 0011 //===----------------------------------------------------------------------===// 0012 0013 #ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H 0014 #define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H 0015 0016 #include "llvm/Analysis/IVDescriptors.h" 0017 #include "llvm/Analysis/LoopAccessAnalysis.h" 0018 #include "llvm/Analysis/TargetTransformInfo.h" 0019 #include "llvm/IR/VectorBuilder.h" 0020 #include "llvm/Transforms/Utils/ValueMapper.h" 0021 0022 namespace llvm { 0023 0024 template <typename T> class DomTreeNodeBase; 0025 using DomTreeNode = DomTreeNodeBase<BasicBlock>; 0026 class AssumptionCache; 0027 class StringRef; 0028 class AnalysisUsage; 0029 class TargetTransformInfo; 0030 class AAResults; 0031 class BasicBlock; 0032 class ICFLoopSafetyInfo; 0033 class IRBuilderBase; 0034 class Loop; 0035 class LoopInfo; 0036 class MemoryAccess; 0037 class MemorySSA; 0038 class MemorySSAUpdater; 0039 class OptimizationRemarkEmitter; 0040 class PredIteratorCache; 0041 class ScalarEvolution; 0042 class SCEV; 0043 class SCEVExpander; 0044 class TargetLibraryInfo; 0045 class LPPassManager; 0046 class Instruction; 0047 struct RuntimeCheckingPtrGroup; 0048 typedef std::pair<const RuntimeCheckingPtrGroup *, 0049 const RuntimeCheckingPtrGroup *> 0050 RuntimePointerCheck; 0051 0052 template <typename T, unsigned N> class SmallSetVector; 0053 template <typename T, unsigned N> class SmallPriorityWorklist; 0054 0055 BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, 0056 MemorySSAUpdater *MSSAU, bool PreserveLCSSA); 0057 0058 /// Ensure that all exit blocks of the loop are dedicated exits. 0059 /// 0060 /// For any loop exit block with non-loop predecessors, we split the loop 0061 /// predecessors to use a dedicated loop exit block. We update the dominator 0062 /// tree and loop info if provided, and will preserve LCSSA if requested. 0063 bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, 0064 MemorySSAUpdater *MSSAU, bool PreserveLCSSA); 0065 0066 /// Ensures LCSSA form for every instruction from the Worklist in the scope of 0067 /// innermost containing loop. 0068 /// 0069 /// For the given instruction which have uses outside of the loop, an LCSSA PHI 0070 /// node is inserted and the uses outside the loop are rewritten to use this 0071 /// node. 0072 /// 0073 /// LoopInfo and DominatorTree are required and, since the routine makes no 0074 /// changes to CFG, preserved. 0075 /// 0076 /// Returns true if any modifications are made. 0077 /// 0078 /// This function may introduce unused PHI nodes. If \p PHIsToRemove is not 0079 /// nullptr, those are added to it (before removing, the caller has to check if 0080 /// they still do not have any uses). Otherwise the PHIs are directly removed. 0081 /// 0082 /// If \p InsertedPHIs is not nullptr, inserted phis will be added to this 0083 /// vector. 0084 bool formLCSSAForInstructions( 0085 SmallVectorImpl<Instruction *> &Worklist, const DominatorTree &DT, 0086 const LoopInfo &LI, ScalarEvolution *SE, 0087 SmallVectorImpl<PHINode *> *PHIsToRemove = nullptr, 0088 SmallVectorImpl<PHINode *> *InsertedPHIs = nullptr); 0089 0090 /// Put loop into LCSSA form. 0091 /// 0092 /// Looks at all instructions in the loop which have uses outside of the 0093 /// current loop. For each, an LCSSA PHI node is inserted and the uses outside 0094 /// the loop are rewritten to use this node. Sub-loops must be in LCSSA form 0095 /// already. 0096 /// 0097 /// LoopInfo and DominatorTree are required and preserved. 0098 /// 0099 /// If ScalarEvolution is passed in, it will be preserved. 0100 /// 0101 /// Returns true if any modifications are made to the loop. 0102 bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, 0103 ScalarEvolution *SE); 0104 0105 /// Put a loop nest into LCSSA form. 0106 /// 0107 /// This recursively forms LCSSA for a loop nest. 0108 /// 0109 /// LoopInfo and DominatorTree are required and preserved. 0110 /// 0111 /// If ScalarEvolution is passed in, it will be preserved. 0112 /// 0113 /// Returns true if any modifications are made to the loop. 0114 bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, 0115 ScalarEvolution *SE); 0116 0117 /// Flags controlling how much is checked when sinking or hoisting 0118 /// instructions. The number of memory access in the loop (and whether there 0119 /// are too many) is determined in the constructors when using MemorySSA. 0120 class SinkAndHoistLICMFlags { 0121 public: 0122 // Explicitly set limits. 0123 SinkAndHoistLICMFlags(unsigned LicmMssaOptCap, 0124 unsigned LicmMssaNoAccForPromotionCap, bool IsSink, 0125 Loop &L, MemorySSA &MSSA); 0126 // Use default limits. 0127 SinkAndHoistLICMFlags(bool IsSink, Loop &L, MemorySSA &MSSA); 0128 0129 void setIsSink(bool B) { IsSink = B; } 0130 bool getIsSink() { return IsSink; } 0131 bool tooManyMemoryAccesses() { return NoOfMemAccTooLarge; } 0132 bool tooManyClobberingCalls() { return LicmMssaOptCounter >= LicmMssaOptCap; } 0133 void incrementClobberingCalls() { ++LicmMssaOptCounter; } 0134 0135 protected: 0136 bool NoOfMemAccTooLarge = false; 0137 unsigned LicmMssaOptCounter = 0; 0138 unsigned LicmMssaOptCap; 0139 unsigned LicmMssaNoAccForPromotionCap; 0140 bool IsSink; 0141 }; 0142 0143 /// Walk the specified region of the CFG (defined by all blocks 0144 /// dominated by the specified block, and that are in the current loop) in 0145 /// reverse depth first order w.r.t the DominatorTree. This allows us to visit 0146 /// uses before definitions, allowing us to sink a loop body in one pass without 0147 /// iteration. Takes DomTreeNode, AAResults, LoopInfo, DominatorTree, 0148 /// TargetLibraryInfo, Loop, AliasSet information for all 0149 /// instructions of the loop and loop safety information as 0150 /// arguments. Diagnostics is emitted via \p ORE. It returns changed status. 0151 /// \p CurLoop is a loop to do sinking on. \p OutermostLoop is used only when 0152 /// this function is called by \p sinkRegionForLoopNest. 0153 bool sinkRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *, 0154 TargetLibraryInfo *, TargetTransformInfo *, Loop *CurLoop, 0155 MemorySSAUpdater &, ICFLoopSafetyInfo *, 0156 SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *, 0157 Loop *OutermostLoop = nullptr); 0158 0159 /// Call sinkRegion on loops contained within the specified loop 0160 /// in order from innermost to outermost. 0161 bool sinkRegionForLoopNest(DomTreeNode *, AAResults *, LoopInfo *, 0162 DominatorTree *, TargetLibraryInfo *, 0163 TargetTransformInfo *, Loop *, MemorySSAUpdater &, 0164 ICFLoopSafetyInfo *, SinkAndHoistLICMFlags &, 0165 OptimizationRemarkEmitter *); 0166 0167 /// Walk the specified region of the CFG (defined by all blocks 0168 /// dominated by the specified block, and that are in the current loop) in depth 0169 /// first order w.r.t the DominatorTree. This allows us to visit definitions 0170 /// before uses, allowing us to hoist a loop body in one pass without iteration. 0171 /// Takes DomTreeNode, AAResults, LoopInfo, DominatorTree, 0172 /// TargetLibraryInfo, Loop, AliasSet information for all 0173 /// instructions of the loop and loop safety information as arguments. 0174 /// Diagnostics is emitted via \p ORE. It returns changed status. 0175 /// \p AllowSpeculation is whether values should be hoisted even if they are not 0176 /// guaranteed to execute in the loop, but are safe to speculatively execute. 0177 bool hoistRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *, 0178 AssumptionCache *, TargetLibraryInfo *, Loop *, 0179 MemorySSAUpdater &, ScalarEvolution *, ICFLoopSafetyInfo *, 0180 SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *, bool, 0181 bool AllowSpeculation); 0182 0183 /// Return true if the induction variable \p IV in a Loop whose latch is 0184 /// \p LatchBlock would become dead if the exit test \p Cond were removed. 0185 /// Conservatively returns false if analysis is insufficient. 0186 bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond); 0187 0188 /// This function deletes dead loops. The caller of this function needs to 0189 /// guarantee that the loop is infact dead. 0190 /// The function requires a bunch or prerequisites to be present: 0191 /// - The loop needs to be in LCSSA form 0192 /// - The loop needs to have a Preheader 0193 /// - A unique dedicated exit block must exist 0194 /// 0195 /// This also updates the relevant analysis information in \p DT, \p SE, \p LI 0196 /// and \p MSSA if pointers to those are provided. 0197 /// It also updates the loop PM if an updater struct is provided. 0198 0199 void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, 0200 LoopInfo *LI, MemorySSA *MSSA = nullptr); 0201 0202 /// Remove the backedge of the specified loop. Handles loop nests and general 0203 /// loop structures subject to the precondition that the loop has no parent 0204 /// loop and has a single latch block. Preserves all listed analyses. 0205 void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE, 0206 LoopInfo &LI, MemorySSA *MSSA); 0207 0208 /// Try to promote memory values to scalars by sinking stores out of 0209 /// the loop and moving loads to before the loop. We do this by looping over 0210 /// the stores in the loop, looking for stores to Must pointers which are 0211 /// loop invariant. It takes a set of must-alias values, Loop exit blocks 0212 /// vector, loop exit blocks insertion point vector, PredIteratorCache, 0213 /// LoopInfo, DominatorTree, Loop, AliasSet information for all instructions 0214 /// of the loop and loop safety information as arguments. 0215 /// Diagnostics is emitted via \p ORE. It returns changed status. 0216 /// \p AllowSpeculation is whether values should be hoisted even if they are not 0217 /// guaranteed to execute in the loop, but are safe to speculatively execute. 0218 bool promoteLoopAccessesToScalars( 0219 const SmallSetVector<Value *, 8> &, SmallVectorImpl<BasicBlock *> &, 0220 SmallVectorImpl<BasicBlock::iterator> &, SmallVectorImpl<MemoryAccess *> &, 0221 PredIteratorCache &, LoopInfo *, DominatorTree *, AssumptionCache *AC, 0222 const TargetLibraryInfo *, TargetTransformInfo *, Loop *, 0223 MemorySSAUpdater &, ICFLoopSafetyInfo *, OptimizationRemarkEmitter *, 0224 bool AllowSpeculation, bool HasReadsOutsideSet); 0225 0226 /// Does a BFS from a given node to all of its children inside a given loop. 0227 /// The returned vector of basic blocks includes the starting point. 0228 SmallVector<BasicBlock *, 16> 0229 collectChildrenInLoop(DominatorTree *DT, DomTreeNode *N, const Loop *CurLoop); 0230 0231 /// Returns the instructions that use values defined in the loop. 0232 SmallVector<Instruction *, 8> findDefsUsedOutsideOfLoop(Loop *L); 0233 0234 /// Find a combination of metadata ("llvm.loop.vectorize.width" and 0235 /// "llvm.loop.vectorize.scalable.enable") for a loop and use it to construct a 0236 /// ElementCount. If the metadata "llvm.loop.vectorize.width" cannot be found 0237 /// then std::nullopt is returned. 0238 std::optional<ElementCount> 0239 getOptionalElementCountLoopAttribute(const Loop *TheLoop); 0240 0241 /// Create a new loop identifier for a loop created from a loop transformation. 0242 /// 0243 /// @param OrigLoopID The loop ID of the loop before the transformation. 0244 /// @param FollowupAttrs List of attribute names that contain attributes to be 0245 /// added to the new loop ID. 0246 /// @param InheritOptionsAttrsPrefix Selects which attributes should be inherited 0247 /// from the original loop. The following values 0248 /// are considered: 0249 /// nullptr : Inherit all attributes from @p OrigLoopID. 0250 /// "" : Do not inherit any attribute from @p OrigLoopID; only use 0251 /// those specified by a followup attribute. 0252 /// "<prefix>": Inherit all attributes except those which start with 0253 /// <prefix>; commonly used to remove metadata for the 0254 /// applied transformation. 0255 /// @param AlwaysNew If true, do not try to reuse OrigLoopID and never return 0256 /// std::nullopt. 0257 /// 0258 /// @return The loop ID for the after-transformation loop. The following values 0259 /// can be returned: 0260 /// std::nullopt : No followup attribute was found; it is up to the 0261 /// transformation to choose attributes that make sense. 0262 /// @p OrigLoopID: The original identifier can be reused. 0263 /// nullptr : The new loop has no attributes. 0264 /// MDNode* : A new unique loop identifier. 0265 std::optional<MDNode *> 0266 makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef<StringRef> FollowupAttrs, 0267 const char *InheritOptionsAttrsPrefix = "", 0268 bool AlwaysNew = false); 0269 0270 /// Look for the loop attribute that disables all transformation heuristic. 0271 bool hasDisableAllTransformsHint(const Loop *L); 0272 0273 /// Look for the loop attribute that disables the LICM transformation heuristics. 0274 bool hasDisableLICMTransformsHint(const Loop *L); 0275 0276 /// The mode sets how eager a transformation should be applied. 0277 enum TransformationMode { 0278 /// The pass can use heuristics to determine whether a transformation should 0279 /// be applied. 0280 TM_Unspecified, 0281 0282 /// The transformation should be applied without considering a cost model. 0283 TM_Enable, 0284 0285 /// The transformation should not be applied. 0286 TM_Disable, 0287 0288 /// Force is a flag and should not be used alone. 0289 TM_Force = 0x04, 0290 0291 /// The transformation was directed by the user, e.g. by a #pragma in 0292 /// the source code. If the transformation could not be applied, a 0293 /// warning should be emitted. 0294 TM_ForcedByUser = TM_Enable | TM_Force, 0295 0296 /// The transformation must not be applied. For instance, `#pragma clang loop 0297 /// unroll(disable)` explicitly forbids any unrolling to take place. Unlike 0298 /// general loop metadata, it must not be dropped. Most passes should not 0299 /// behave differently under TM_Disable and TM_SuppressedByUser. 0300 TM_SuppressedByUser = TM_Disable | TM_Force 0301 }; 0302 0303 /// @{ 0304 /// Get the mode for LLVM's supported loop transformations. 0305 TransformationMode hasUnrollTransformation(const Loop *L); 0306 TransformationMode hasUnrollAndJamTransformation(const Loop *L); 0307 TransformationMode hasVectorizeTransformation(const Loop *L); 0308 TransformationMode hasDistributeTransformation(const Loop *L); 0309 TransformationMode hasLICMVersioningTransformation(const Loop *L); 0310 /// @} 0311 0312 /// Set input string into loop metadata by keeping other values intact. 0313 /// If the string is already in loop metadata update value if it is 0314 /// different. 0315 void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, 0316 unsigned V = 0); 0317 0318 /// Returns a loop's estimated trip count based on branch weight metadata. 0319 /// In addition if \p EstimatedLoopInvocationWeight is not null it is 0320 /// initialized with weight of loop's latch leading to the exit. 0321 /// Returns 0 when the count is estimated to be 0, or std::nullopt when a 0322 /// meaningful estimate can not be made. 0323 std::optional<unsigned> 0324 getLoopEstimatedTripCount(Loop *L, 0325 unsigned *EstimatedLoopInvocationWeight = nullptr); 0326 0327 /// Set a loop's branch weight metadata to reflect that loop has \p 0328 /// EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight exits 0329 /// through latch. Returns true if metadata is successfully updated, false 0330 /// otherwise. Note that loop must have a latch block which controls loop exit 0331 /// in order to succeed. 0332 bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount, 0333 unsigned EstimatedLoopInvocationWeight); 0334 0335 /// Check inner loop (L) backedge count is known to be invariant on all 0336 /// iterations of its outer loop. If the loop has no parent, this is trivially 0337 /// true. 0338 bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE); 0339 0340 /// Helper to consistently add the set of standard passes to a loop pass's \c 0341 /// AnalysisUsage. 0342 /// 0343 /// All loop passes should call this as part of implementing their \c 0344 /// getAnalysisUsage. 0345 void getLoopAnalysisUsage(AnalysisUsage &AU); 0346 0347 /// Returns true if is legal to hoist or sink this instruction disregarding the 0348 /// possible introduction of faults. Reasoning about potential faulting 0349 /// instructions is the responsibility of the caller since it is challenging to 0350 /// do efficiently from within this routine. 0351 /// \p TargetExecutesOncePerLoop is true only when it is guaranteed that the 0352 /// target executes at most once per execution of the loop body. This is used 0353 /// to assess the legality of duplicating atomic loads. Generally, this is 0354 /// true when moving out of loop and not true when moving into loops. 0355 /// If \p ORE is set use it to emit optimization remarks. 0356 bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, 0357 Loop *CurLoop, MemorySSAUpdater &MSSAU, 0358 bool TargetExecutesOncePerLoop, 0359 SinkAndHoistLICMFlags &LICMFlags, 0360 OptimizationRemarkEmitter *ORE = nullptr); 0361 0362 /// Returns the llvm.vector.reduce intrinsic that corresponds to the recurrence 0363 /// kind. 0364 constexpr Intrinsic::ID getReductionIntrinsicID(RecurKind RK); 0365 0366 /// Returns the arithmetic instruction opcode used when expanding a reduction. 0367 unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID); 0368 0369 /// Returns the min/max intrinsic used when expanding a min/max reduction. 0370 Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID); 0371 0372 /// Returns the min/max intrinsic used when expanding a min/max reduction. 0373 Intrinsic::ID getMinMaxReductionIntrinsicOp(RecurKind RK); 0374 0375 /// Returns the recurence kind used when expanding a min/max reduction. 0376 RecurKind getMinMaxReductionRecurKind(Intrinsic::ID RdxID); 0377 0378 /// Returns the comparison predicate used when expanding a min/max reduction. 0379 CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK); 0380 0381 /// Given information about an @llvm.vector.reduce.* intrinsic, return 0382 /// the identity value for the reduction. 0383 Value *getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags FMF); 0384 0385 /// Given information about an recurrence kind, return the identity 0386 /// for the @llvm.vector.reduce.* used to generate it. 0387 Value *getRecurrenceIdentity(RecurKind K, Type *Tp, FastMathFlags FMF); 0388 0389 /// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind. 0390 /// The Builder's fast-math-flags must be set to propagate the expected values. 0391 Value *createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left, 0392 Value *Right); 0393 0394 /// Generates an ordered vector reduction using extracts to reduce the value. 0395 Value *getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src, 0396 unsigned Op, RecurKind MinMaxKind = RecurKind::None); 0397 0398 /// Generates a vector reduction using shufflevectors to reduce the value. 0399 /// Fast-math-flags are propagated using the IRBuilder's setting. 0400 Value *getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op, 0401 TargetTransformInfo::ReductionShuffle RS, 0402 RecurKind MinMaxKind = RecurKind::None); 0403 0404 /// Create a reduction of the given vector. The reduction operation 0405 /// is described by the \p Opcode parameter. min/max reductions require 0406 /// additional information supplied in \p RdxKind. 0407 /// Fast-math-flags are propagated using the IRBuilder's setting. 0408 Value *createSimpleReduction(IRBuilderBase &B, Value *Src, 0409 RecurKind RdxKind); 0410 /// Overloaded function to generate vector-predication intrinsics for 0411 /// reduction. 0412 Value *createSimpleReduction(VectorBuilder &VB, Value *Src, 0413 const RecurrenceDescriptor &Desc); 0414 0415 /// Create a reduction of the given vector \p Src for a reduction of the 0416 /// kind RecurKind::IAnyOf or RecurKind::FAnyOf. The reduction operation is 0417 /// described by \p Desc. 0418 Value *createAnyOfReduction(IRBuilderBase &B, Value *Src, 0419 const RecurrenceDescriptor &Desc, 0420 PHINode *OrigPhi); 0421 0422 /// Create a reduction of the given vector \p Src for a reduction of the 0423 /// kind RecurKind::IFindLastIV or RecurKind::FFindLastIV. The reduction 0424 /// operation is described by \p Desc. 0425 Value *createFindLastIVReduction(IRBuilderBase &B, Value *Src, 0426 const RecurrenceDescriptor &Desc); 0427 0428 /// Create a generic reduction using a recurrence descriptor \p Desc 0429 /// Fast-math-flags are propagated using the RecurrenceDescriptor. 0430 Value *createReduction(IRBuilderBase &B, const RecurrenceDescriptor &Desc, 0431 Value *Src, PHINode *OrigPhi = nullptr); 0432 0433 /// Create an ordered reduction intrinsic using the given recurrence 0434 /// descriptor \p Desc. 0435 Value *createOrderedReduction(IRBuilderBase &B, 0436 const RecurrenceDescriptor &Desc, Value *Src, 0437 Value *Start); 0438 /// Overloaded function to generate vector-predication intrinsics for ordered 0439 /// reduction. 0440 Value *createOrderedReduction(VectorBuilder &VB, 0441 const RecurrenceDescriptor &Desc, Value *Src, 0442 Value *Start); 0443 0444 /// Get the intersection (logical and) of all of the potential IR flags 0445 /// of each scalar operation (VL) that will be converted into a vector (I). 0446 /// If OpValue is non-null, we only consider operations similar to OpValue 0447 /// when intersecting. 0448 /// Flag set: NSW, NUW (if IncludeWrapFlags is true), exact, and all of 0449 /// fast-math. 0450 void propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue = nullptr, 0451 bool IncludeWrapFlags = true); 0452 0453 /// Returns true if we can prove that \p S is defined and always negative in 0454 /// loop \p L. 0455 bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE); 0456 0457 /// Returns true if we can prove that \p S is defined and always non-negative in 0458 /// loop \p L. 0459 bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L, 0460 ScalarEvolution &SE); 0461 /// Returns true if we can prove that \p S is defined and always positive in 0462 /// loop \p L. 0463 bool isKnownPositiveInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE); 0464 0465 /// Returns true if we can prove that \p S is defined and always non-positive in 0466 /// loop \p L. 0467 bool isKnownNonPositiveInLoop(const SCEV *S, const Loop *L, 0468 ScalarEvolution &SE); 0469 0470 /// Returns true if \p S is defined and never is equal to signed/unsigned max. 0471 bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, 0472 bool Signed); 0473 0474 /// Returns true if \p S is defined and never is equal to signed/unsigned min. 0475 bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, 0476 bool Signed); 0477 0478 enum ReplaceExitVal { 0479 NeverRepl, 0480 OnlyCheapRepl, 0481 NoHardUse, 0482 UnusedIndVarInLoop, 0483 AlwaysRepl 0484 }; 0485 0486 /// If the final value of any expressions that are recurrent in the loop can 0487 /// be computed, substitute the exit values from the loop into any instructions 0488 /// outside of the loop that use the final values of the current expressions. 0489 /// Return the number of loop exit values that have been replaced, and the 0490 /// corresponding phi node will be added to DeadInsts. 0491 int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, 0492 ScalarEvolution *SE, const TargetTransformInfo *TTI, 0493 SCEVExpander &Rewriter, DominatorTree *DT, 0494 ReplaceExitVal ReplaceExitValue, 0495 SmallVector<WeakTrackingVH, 16> &DeadInsts); 0496 0497 /// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for 0498 /// \p OrigLoop and the following distribution of \p OrigLoop iteration among \p 0499 /// UnrolledLoop and \p RemainderLoop. \p UnrolledLoop receives weights that 0500 /// reflect TC/UF iterations, and \p RemainderLoop receives weights that reflect 0501 /// the remaining TC%UF iterations. 0502 /// 0503 /// Note that \p OrigLoop may be equal to either \p UnrolledLoop or \p 0504 /// RemainderLoop in which case weights for \p OrigLoop are updated accordingly. 0505 /// Note also behavior is undefined if \p UnrolledLoop and \p RemainderLoop are 0506 /// equal. \p UF must be greater than zero. 0507 /// If \p OrigLoop has no profile info associated nothing happens. 0508 /// 0509 /// This utility may be useful for such optimizations as unroller and 0510 /// vectorizer as it's typical transformation for them. 0511 void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop, 0512 Loop *RemainderLoop, uint64_t UF); 0513 0514 /// Utility that implements appending of loops onto a worklist given a range. 0515 /// We want to process loops in postorder, but the worklist is a LIFO data 0516 /// structure, so we append to it in *reverse* postorder. 0517 /// For trees, a preorder traversal is a viable reverse postorder, so we 0518 /// actually append using a preorder walk algorithm. 0519 template <typename RangeT> 0520 void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist<Loop *, 4> &); 0521 /// Utility that implements appending of loops onto a worklist given a range. 0522 /// It has the same behavior as appendLoopsToWorklist, but assumes the range of 0523 /// loops has already been reversed, so it processes loops in the given order. 0524 template <typename RangeT> 0525 void appendReversedLoopsToWorklist(RangeT &&, 0526 SmallPriorityWorklist<Loop *, 4> &); 0527 0528 /// Utility that implements appending of loops onto a worklist given LoopInfo. 0529 /// Calls the templated utility taking a Range of loops, handing it the Loops 0530 /// in LoopInfo, iterated in reverse. This is because the loops are stored in 0531 /// RPO w.r.t. the control flow graph in LoopInfo. For the purpose of unrolling, 0532 /// loop deletion, and LICM, we largely want to work forward across the CFG so 0533 /// that we visit defs before uses and can propagate simplifications from one 0534 /// loop nest into the next. Calls appendReversedLoopsToWorklist with the 0535 /// already reversed loops in LI. 0536 /// FIXME: Consider changing the order in LoopInfo. 0537 void appendLoopsToWorklist(LoopInfo &, SmallPriorityWorklist<Loop *, 4> &); 0538 0539 /// Recursively clone the specified loop and all of its children, 0540 /// mapping the blocks with the specified map. 0541 Loop *cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, 0542 LoopInfo *LI, LPPassManager *LPM); 0543 0544 /// Add code that checks at runtime if the accessed arrays in \p PointerChecks 0545 /// overlap. Returns the final comparator value or NULL if no check is needed. 0546 Value * 0547 addRuntimeChecks(Instruction *Loc, Loop *TheLoop, 0548 const SmallVectorImpl<RuntimePointerCheck> &PointerChecks, 0549 SCEVExpander &Expander, bool HoistRuntimeChecks = false); 0550 0551 Value *addDiffRuntimeChecks( 0552 Instruction *Loc, ArrayRef<PointerDiffInfo> Checks, SCEVExpander &Expander, 0553 function_ref<Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC); 0554 0555 /// Struct to hold information about a partially invariant condition. 0556 struct IVConditionInfo { 0557 /// Instructions that need to be duplicated and checked for the unswitching 0558 /// condition. 0559 SmallVector<Instruction *> InstToDuplicate; 0560 0561 /// Constant to indicate for which value the condition is invariant. 0562 Constant *KnownValue = nullptr; 0563 0564 /// True if the partially invariant path is no-op (=does not have any 0565 /// side-effects and no loop value is used outside the loop). 0566 bool PathIsNoop = true; 0567 0568 /// If the partially invariant path reaches a single exit block, ExitForPath 0569 /// is set to that block. Otherwise it is nullptr. 0570 BasicBlock *ExitForPath = nullptr; 0571 }; 0572 0573 /// Check if the loop header has a conditional branch that is not 0574 /// loop-invariant, because it involves load instructions. If all paths from 0575 /// either the true or false successor to the header or loop exists do not 0576 /// modify the memory feeding the condition, perform 'partial unswitching'. That 0577 /// is, duplicate the instructions feeding the condition in the pre-header. Then 0578 /// unswitch on the duplicated condition. The condition is now known in the 0579 /// unswitched version for the 'invariant' path through the original loop. 0580 /// 0581 /// If the branch condition of the header is partially invariant, return a pair 0582 /// containing the instructions to duplicate and a boolean Constant to update 0583 /// the condition in the loops created for the true or false successors. 0584 std::optional<IVConditionInfo> hasPartialIVCondition(const Loop &L, 0585 unsigned MSSAThreshold, 0586 const MemorySSA &MSSA, 0587 AAResults &AA); 0588 0589 } // end namespace llvm 0590 0591 #endif // LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|