LoopUtils.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines some loop transformation utilities.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
  18. #define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Analysis/IVDescriptors.h"
  21. #include "llvm/Analysis/TargetTransformInfo.h"
  22. #include "llvm/Transforms/Utils/ValueMapper.h"
  23. namespace llvm {
  24. template <typename T> class DomTreeNodeBase;
  25. using DomTreeNode = DomTreeNodeBase<BasicBlock>;
  26. class AAResults;
  27. class AliasSet;
  28. class AliasSetTracker;
  29. class BasicBlock;
  30. class BlockFrequencyInfo;
  31. class ICFLoopSafetyInfo;
  32. class IRBuilderBase;
  33. class Loop;
  34. class LoopInfo;
  35. class MemoryAccess;
  36. class MemorySSA;
  37. class MemorySSAUpdater;
  38. class OptimizationRemarkEmitter;
  39. class PredIteratorCache;
  40. class ScalarEvolution;
  41. class SCEV;
  42. class SCEVExpander;
  43. class TargetLibraryInfo;
  44. class LPPassManager;
  45. class Instruction;
  46. struct RuntimeCheckingPtrGroup;
  47. typedef std::pair<const RuntimeCheckingPtrGroup *,
  48. const RuntimeCheckingPtrGroup *>
  49. RuntimePointerCheck;
  50. template <typename T> class Optional;
  51. template <typename T, unsigned N> class SmallSetVector;
  52. template <typename T, unsigned N> class SmallVector;
  53. template <typename T> class SmallVectorImpl;
  54. template <typename T, unsigned N> class SmallPriorityWorklist;
  55. BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
  56. MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
  57. /// Ensure that all exit blocks of the loop are dedicated exits.
  58. ///
  59. /// For any loop exit block with non-loop predecessors, we split the loop
  60. /// predecessors to use a dedicated loop exit block. We update the dominator
  61. /// tree and loop info if provided, and will preserve LCSSA if requested.
  62. bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
  63. MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
  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.
  79. bool formLCSSAForInstructions(
  80. SmallVectorImpl<Instruction *> &Worklist, const DominatorTree &DT,
  81. const LoopInfo &LI, ScalarEvolution *SE, IRBuilderBase &Builder,
  82. SmallVectorImpl<PHINode *> *PHIsToRemove = nullptr);
  83. /// Put loop into LCSSA form.
  84. ///
  85. /// Looks at all instructions in the loop which have uses outside of the
  86. /// current loop. For each, an LCSSA PHI node is inserted and the uses outside
  87. /// the loop are rewritten to use this node. Sub-loops must be in LCSSA form
  88. /// already.
  89. ///
  90. /// LoopInfo and DominatorTree are required and preserved.
  91. ///
  92. /// If ScalarEvolution is passed in, it will be preserved.
  93. ///
  94. /// Returns true if any modifications are made to the loop.
  95. bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
  96. ScalarEvolution *SE);
  97. /// Put a loop nest into LCSSA form.
  98. ///
  99. /// This recursively forms LCSSA for a loop nest.
  100. ///
  101. /// LoopInfo and DominatorTree are required and preserved.
  102. ///
  103. /// If ScalarEvolution is passed in, it will be preserved.
  104. ///
  105. /// Returns true if any modifications are made to the loop.
  106. bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
  107. ScalarEvolution *SE);
  108. /// Flags controlling how much is checked when sinking or hoisting
  109. /// instructions. The number of memory access in the loop (and whether there
  110. /// are too many) is determined in the constructors when using MemorySSA.
  111. class SinkAndHoistLICMFlags {
  112. public:
  113. // Explicitly set limits.
  114. SinkAndHoistLICMFlags(unsigned LicmMssaOptCap,
  115. unsigned LicmMssaNoAccForPromotionCap, bool IsSink,
  116. Loop *L = nullptr, MemorySSA *MSSA = nullptr);
  117. // Use default limits.
  118. SinkAndHoistLICMFlags(bool IsSink, Loop *L = nullptr,
  119. MemorySSA *MSSA = nullptr);
  120. void setIsSink(bool B) { IsSink = B; }
  121. bool getIsSink() { return IsSink; }
  122. bool tooManyMemoryAccesses() { return NoOfMemAccTooLarge; }
  123. bool tooManyClobberingCalls() { return LicmMssaOptCounter >= LicmMssaOptCap; }
  124. void incrementClobberingCalls() { ++LicmMssaOptCounter; }
  125. protected:
  126. bool NoOfMemAccTooLarge = false;
  127. unsigned LicmMssaOptCounter = 0;
  128. unsigned LicmMssaOptCap;
  129. unsigned LicmMssaNoAccForPromotionCap;
  130. bool IsSink;
  131. };
  132. /// Walk the specified region of the CFG (defined by all blocks
  133. /// dominated by the specified block, and that are in the current loop) in
  134. /// reverse depth first order w.r.t the DominatorTree. This allows us to visit
  135. /// uses before definitions, allowing us to sink a loop body in one pass without
  136. /// iteration. Takes DomTreeNode, AAResults, LoopInfo, DominatorTree,
  137. /// BlockFrequencyInfo, TargetLibraryInfo, Loop, AliasSet information for all
  138. /// instructions of the loop and loop safety information as
  139. /// arguments. Diagnostics is emitted via \p ORE. It returns changed status.
  140. /// \p CurLoop is a loop to do sinking on. \p OutermostLoop is used only when
  141. /// this function is called by \p sinkRegionForLoopNest.
  142. bool sinkRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *,
  143. BlockFrequencyInfo *, TargetLibraryInfo *,
  144. TargetTransformInfo *, Loop *CurLoop, MemorySSAUpdater *,
  145. ICFLoopSafetyInfo *, SinkAndHoistLICMFlags &,
  146. OptimizationRemarkEmitter *, Loop *OutermostLoop = nullptr);
  147. /// Call sinkRegion on loops contained within the specified loop
  148. /// in order from innermost to outermost.
  149. bool sinkRegionForLoopNest(DomTreeNode *, AAResults *, LoopInfo *,
  150. DominatorTree *, BlockFrequencyInfo *,
  151. TargetLibraryInfo *, TargetTransformInfo *, Loop *,
  152. MemorySSAUpdater *, ICFLoopSafetyInfo *,
  153. SinkAndHoistLICMFlags &,
  154. OptimizationRemarkEmitter *);
  155. /// Walk the specified region of the CFG (defined by all blocks
  156. /// dominated by the specified block, and that are in the current loop) in depth
  157. /// first order w.r.t the DominatorTree. This allows us to visit definitions
  158. /// before uses, allowing us to hoist a loop body in one pass without iteration.
  159. /// Takes DomTreeNode, AAResults, LoopInfo, DominatorTree,
  160. /// BlockFrequencyInfo, TargetLibraryInfo, Loop, AliasSet information for all
  161. /// instructions of the loop and loop safety information as arguments.
  162. /// Diagnostics is emitted via \p ORE. It returns changed status.
  163. /// \p AllowSpeculation is whether values should be hoisted even if they are not
  164. /// guaranteed to execute in the loop, but are safe to speculatively execute.
  165. bool hoistRegion(DomTreeNode *, AAResults *, LoopInfo *, DominatorTree *,
  166. BlockFrequencyInfo *, TargetLibraryInfo *, Loop *,
  167. MemorySSAUpdater *, ScalarEvolution *, ICFLoopSafetyInfo *,
  168. SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *, bool,
  169. bool AllowSpeculation);
  170. /// This function deletes dead loops. The caller of this function needs to
  171. /// guarantee that the loop is infact dead.
  172. /// The function requires a bunch or prerequisites to be present:
  173. /// - The loop needs to be in LCSSA form
  174. /// - The loop needs to have a Preheader
  175. /// - A unique dedicated exit block must exist
  176. ///
  177. /// This also updates the relevant analysis information in \p DT, \p SE, \p LI
  178. /// and \p MSSA if pointers to those are provided.
  179. /// It also updates the loop PM if an updater struct is provided.
  180. void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
  181. LoopInfo *LI, MemorySSA *MSSA = nullptr);
  182. /// Remove the backedge of the specified loop. Handles loop nests and general
  183. /// loop structures subject to the precondition that the loop has no parent
  184. /// loop and has a single latch block. Preserves all listed analyses.
  185. void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
  186. LoopInfo &LI, MemorySSA *MSSA);
  187. /// Try to promote memory values to scalars by sinking stores out of
  188. /// the loop and moving loads to before the loop. We do this by looping over
  189. /// the stores in the loop, looking for stores to Must pointers which are
  190. /// loop invariant. It takes a set of must-alias values, Loop exit blocks
  191. /// vector, loop exit blocks insertion point vector, PredIteratorCache,
  192. /// LoopInfo, DominatorTree, Loop, AliasSet information for all instructions
  193. /// of the loop and loop safety information as arguments.
  194. /// Diagnostics is emitted via \p ORE. It returns changed status.
  195. /// \p AllowSpeculation is whether values should be hoisted even if they are not
  196. /// guaranteed to execute in the loop, but are safe to speculatively execute.
  197. bool promoteLoopAccessesToScalars(
  198. const SmallSetVector<Value *, 8> &, SmallVectorImpl<BasicBlock *> &,
  199. SmallVectorImpl<Instruction *> &, SmallVectorImpl<MemoryAccess *> &,
  200. PredIteratorCache &, LoopInfo *, DominatorTree *, const TargetLibraryInfo *,
  201. Loop *, MemorySSAUpdater *, ICFLoopSafetyInfo *,
  202. OptimizationRemarkEmitter *, bool AllowSpeculation);
  203. /// Does a BFS from a given node to all of its children inside a given loop.
  204. /// The returned vector of nodes includes the starting point.
  205. SmallVector<DomTreeNode *, 16> collectChildrenInLoop(DomTreeNode *N,
  206. const Loop *CurLoop);
  207. /// Returns the instructions that use values defined in the loop.
  208. SmallVector<Instruction *, 8> findDefsUsedOutsideOfLoop(Loop *L);
  209. /// Find a combination of metadata ("llvm.loop.vectorize.width" and
  210. /// "llvm.loop.vectorize.scalable.enable") for a loop and use it to construct a
  211. /// ElementCount. If the metadata "llvm.loop.vectorize.width" cannot be found
  212. /// then None is returned.
  213. Optional<ElementCount>
  214. getOptionalElementCountLoopAttribute(const Loop *TheLoop);
  215. /// Create a new loop identifier for a loop created from a loop transformation.
  216. ///
  217. /// @param OrigLoopID The loop ID of the loop before the transformation.
  218. /// @param FollowupAttrs List of attribute names that contain attributes to be
  219. /// added to the new loop ID.
  220. /// @param InheritOptionsAttrsPrefix Selects which attributes should be inherited
  221. /// from the original loop. The following values
  222. /// are considered:
  223. /// nullptr : Inherit all attributes from @p OrigLoopID.
  224. /// "" : Do not inherit any attribute from @p OrigLoopID; only use
  225. /// those specified by a followup attribute.
  226. /// "<prefix>": Inherit all attributes except those which start with
  227. /// <prefix>; commonly used to remove metadata for the
  228. /// applied transformation.
  229. /// @param AlwaysNew If true, do not try to reuse OrigLoopID and never return
  230. /// None.
  231. ///
  232. /// @return The loop ID for the after-transformation loop. The following values
  233. /// can be returned:
  234. /// None : No followup attribute was found; it is up to the
  235. /// transformation to choose attributes that make sense.
  236. /// @p OrigLoopID: The original identifier can be reused.
  237. /// nullptr : The new loop has no attributes.
  238. /// MDNode* : A new unique loop identifier.
  239. Optional<MDNode *>
  240. makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef<StringRef> FollowupAttrs,
  241. const char *InheritOptionsAttrsPrefix = "",
  242. bool AlwaysNew = false);
  243. /// Look for the loop attribute that disables all transformation heuristic.
  244. bool hasDisableAllTransformsHint(const Loop *L);
  245. /// Look for the loop attribute that disables the LICM transformation heuristics.
  246. bool hasDisableLICMTransformsHint(const Loop *L);
  247. /// The mode sets how eager a transformation should be applied.
  248. enum TransformationMode {
  249. /// The pass can use heuristics to determine whether a transformation should
  250. /// be applied.
  251. TM_Unspecified,
  252. /// The transformation should be applied without considering a cost model.
  253. TM_Enable,
  254. /// The transformation should not be applied.
  255. TM_Disable,
  256. /// Force is a flag and should not be used alone.
  257. TM_Force = 0x04,
  258. /// The transformation was directed by the user, e.g. by a #pragma in
  259. /// the source code. If the transformation could not be applied, a
  260. /// warning should be emitted.
  261. TM_ForcedByUser = TM_Enable | TM_Force,
  262. /// The transformation must not be applied. For instance, `#pragma clang loop
  263. /// unroll(disable)` explicitly forbids any unrolling to take place. Unlike
  264. /// general loop metadata, it must not be dropped. Most passes should not
  265. /// behave differently under TM_Disable and TM_SuppressedByUser.
  266. TM_SuppressedByUser = TM_Disable | TM_Force
  267. };
  268. /// @{
  269. /// Get the mode for LLVM's supported loop transformations.
  270. TransformationMode hasUnrollTransformation(const Loop *L);
  271. TransformationMode hasUnrollAndJamTransformation(const Loop *L);
  272. TransformationMode hasVectorizeTransformation(const Loop *L);
  273. TransformationMode hasDistributeTransformation(const Loop *L);
  274. TransformationMode hasLICMVersioningTransformation(const Loop *L);
  275. /// @}
  276. /// Set input string into loop metadata by keeping other values intact.
  277. /// If the string is already in loop metadata update value if it is
  278. /// different.
  279. void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
  280. unsigned V = 0);
  281. /// Returns a loop's estimated trip count based on branch weight metadata.
  282. /// In addition if \p EstimatedLoopInvocationWeight is not null it is
  283. /// initialized with weight of loop's latch leading to the exit.
  284. /// Returns 0 when the count is estimated to be 0, or None when a meaningful
  285. /// estimate can not be made.
  286. Optional<unsigned>
  287. getLoopEstimatedTripCount(Loop *L,
  288. unsigned *EstimatedLoopInvocationWeight = nullptr);
  289. /// Set a loop's branch weight metadata to reflect that loop has \p
  290. /// EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight exits
  291. /// through latch. Returns true if metadata is successfully updated, false
  292. /// otherwise. Note that loop must have a latch block which controls loop exit
  293. /// in order to succeed.
  294. bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
  295. unsigned EstimatedLoopInvocationWeight);
  296. /// Check inner loop (L) backedge count is known to be invariant on all
  297. /// iterations of its outer loop. If the loop has no parent, this is trivially
  298. /// true.
  299. bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE);
  300. /// Helper to consistently add the set of standard passes to a loop pass's \c
  301. /// AnalysisUsage.
  302. ///
  303. /// All loop passes should call this as part of implementing their \c
  304. /// getAnalysisUsage.
  305. void getLoopAnalysisUsage(AnalysisUsage &AU);
  306. /// Returns true if is legal to hoist or sink this instruction disregarding the
  307. /// possible introduction of faults. Reasoning about potential faulting
  308. /// instructions is the responsibility of the caller since it is challenging to
  309. /// do efficiently from within this routine.
  310. /// \p TargetExecutesOncePerLoop is true only when it is guaranteed that the
  311. /// target executes at most once per execution of the loop body. This is used
  312. /// to assess the legality of duplicating atomic loads. Generally, this is
  313. /// true when moving out of loop and not true when moving into loops.
  314. /// If \p ORE is set use it to emit optimization remarks.
  315. bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
  316. Loop *CurLoop, AliasSetTracker *CurAST,
  317. MemorySSAUpdater *MSSAU, bool TargetExecutesOncePerLoop,
  318. SinkAndHoistLICMFlags *LICMFlags = nullptr,
  319. OptimizationRemarkEmitter *ORE = nullptr);
  320. /// Returns the comparison predicate used when expanding a min/max reduction.
  321. CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK);
  322. /// See RecurrenceDescriptor::isSelectCmpPattern for a description of the
  323. /// pattern we are trying to match. In this pattern we are only ever selecting
  324. /// between two values: 1) an initial PHI start value, and 2) a loop invariant
  325. /// value. This function uses \p LoopExitInst to determine 2), which we then use
  326. /// to select between \p Left and \p Right. Any lane value in \p Left that
  327. /// matches 2) will be merged into \p Right.
  328. Value *createSelectCmpOp(IRBuilderBase &Builder, Value *StartVal, RecurKind RK,
  329. Value *Left, Value *Right);
  330. /// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
  331. /// The Builder's fast-math-flags must be set to propagate the expected values.
  332. Value *createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left,
  333. Value *Right);
  334. /// Generates an ordered vector reduction using extracts to reduce the value.
  335. Value *getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src,
  336. unsigned Op, RecurKind MinMaxKind = RecurKind::None);
  337. /// Generates a vector reduction using shufflevectors to reduce the value.
  338. /// Fast-math-flags are propagated using the IRBuilder's setting.
  339. Value *getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op,
  340. RecurKind MinMaxKind = RecurKind::None);
  341. /// Create a target reduction of the given vector. The reduction operation
  342. /// is described by the \p Opcode parameter. min/max reductions require
  343. /// additional information supplied in \p RdxKind.
  344. /// The target is queried to determine if intrinsics or shuffle sequences are
  345. /// required to implement the reduction.
  346. /// Fast-math-flags are propagated using the IRBuilder's setting.
  347. Value *createSimpleTargetReduction(IRBuilderBase &B,
  348. const TargetTransformInfo *TTI, Value *Src,
  349. RecurKind RdxKind);
  350. /// Create a target reduction of the given vector \p Src for a reduction of the
  351. /// kind RecurKind::SelectICmp or RecurKind::SelectFCmp. The reduction operation
  352. /// is described by \p Desc.
  353. Value *createSelectCmpTargetReduction(IRBuilderBase &B,
  354. const TargetTransformInfo *TTI,
  355. Value *Src,
  356. const RecurrenceDescriptor &Desc,
  357. PHINode *OrigPhi);
  358. /// Create a generic target reduction using a recurrence descriptor \p Desc
  359. /// The target is queried to determine if intrinsics or shuffle sequences are
  360. /// required to implement the reduction.
  361. /// Fast-math-flags are propagated using the RecurrenceDescriptor.
  362. Value *createTargetReduction(IRBuilderBase &B, const TargetTransformInfo *TTI,
  363. const RecurrenceDescriptor &Desc, Value *Src,
  364. PHINode *OrigPhi = nullptr);
  365. /// Create an ordered reduction intrinsic using the given recurrence
  366. /// descriptor \p Desc.
  367. Value *createOrderedReduction(IRBuilderBase &B,
  368. const RecurrenceDescriptor &Desc, Value *Src,
  369. Value *Start);
  370. /// Get the intersection (logical and) of all of the potential IR flags
  371. /// of each scalar operation (VL) that will be converted into a vector (I).
  372. /// If OpValue is non-null, we only consider operations similar to OpValue
  373. /// when intersecting.
  374. /// Flag set: NSW, NUW, exact, and all of fast-math.
  375. void propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue = nullptr);
  376. /// Returns true if we can prove that \p S is defined and always negative in
  377. /// loop \p L.
  378. bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE);
  379. /// Returns true if we can prove that \p S is defined and always non-negative in
  380. /// loop \p L.
  381. bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
  382. ScalarEvolution &SE);
  383. /// Returns true if \p S is defined and never is equal to signed/unsigned max.
  384. bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
  385. bool Signed);
  386. /// Returns true if \p S is defined and never is equal to signed/unsigned min.
  387. bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
  388. bool Signed);
  389. enum ReplaceExitVal { NeverRepl, OnlyCheapRepl, NoHardUse, AlwaysRepl };
  390. /// If the final value of any expressions that are recurrent in the loop can
  391. /// be computed, substitute the exit values from the loop into any instructions
  392. /// outside of the loop that use the final values of the current expressions.
  393. /// Return the number of loop exit values that have been replaced, and the
  394. /// corresponding phi node will be added to DeadInsts.
  395. int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
  396. ScalarEvolution *SE, const TargetTransformInfo *TTI,
  397. SCEVExpander &Rewriter, DominatorTree *DT,
  398. ReplaceExitVal ReplaceExitValue,
  399. SmallVector<WeakTrackingVH, 16> &DeadInsts);
  400. /// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
  401. /// \p OrigLoop and the following distribution of \p OrigLoop iteration among \p
  402. /// UnrolledLoop and \p RemainderLoop. \p UnrolledLoop receives weights that
  403. /// reflect TC/UF iterations, and \p RemainderLoop receives weights that reflect
  404. /// the remaining TC%UF iterations.
  405. ///
  406. /// Note that \p OrigLoop may be equal to either \p UnrolledLoop or \p
  407. /// RemainderLoop in which case weights for \p OrigLoop are updated accordingly.
  408. /// Note also behavior is undefined if \p UnrolledLoop and \p RemainderLoop are
  409. /// equal. \p UF must be greater than zero.
  410. /// If \p OrigLoop has no profile info associated nothing happens.
  411. ///
  412. /// This utility may be useful for such optimizations as unroller and
  413. /// vectorizer as it's typical transformation for them.
  414. void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
  415. Loop *RemainderLoop, uint64_t UF);
  416. /// Utility that implements appending of loops onto a worklist given a range.
  417. /// We want to process loops in postorder, but the worklist is a LIFO data
  418. /// structure, so we append to it in *reverse* postorder.
  419. /// For trees, a preorder traversal is a viable reverse postorder, so we
  420. /// actually append using a preorder walk algorithm.
  421. template <typename RangeT>
  422. void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist<Loop *, 4> &);
  423. /// Utility that implements appending of loops onto a worklist given a range.
  424. /// It has the same behavior as appendLoopsToWorklist, but assumes the range of
  425. /// loops has already been reversed, so it processes loops in the given order.
  426. template <typename RangeT>
  427. void appendReversedLoopsToWorklist(RangeT &&,
  428. SmallPriorityWorklist<Loop *, 4> &);
  429. /// Utility that implements appending of loops onto a worklist given LoopInfo.
  430. /// Calls the templated utility taking a Range of loops, handing it the Loops
  431. /// in LoopInfo, iterated in reverse. This is because the loops are stored in
  432. /// RPO w.r.t. the control flow graph in LoopInfo. For the purpose of unrolling,
  433. /// loop deletion, and LICM, we largely want to work forward across the CFG so
  434. /// that we visit defs before uses and can propagate simplifications from one
  435. /// loop nest into the next. Calls appendReversedLoopsToWorklist with the
  436. /// already reversed loops in LI.
  437. /// FIXME: Consider changing the order in LoopInfo.
  438. void appendLoopsToWorklist(LoopInfo &, SmallPriorityWorklist<Loop *, 4> &);
  439. /// Recursively clone the specified loop and all of its children,
  440. /// mapping the blocks with the specified map.
  441. Loop *cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
  442. LoopInfo *LI, LPPassManager *LPM);
  443. /// Add code that checks at runtime if the accessed arrays in \p PointerChecks
  444. /// overlap. Returns the final comparator value or NULL if no check is needed.
  445. Value *
  446. addRuntimeChecks(Instruction *Loc, Loop *TheLoop,
  447. const SmallVectorImpl<RuntimePointerCheck> &PointerChecks,
  448. SCEVExpander &Expander);
  449. /// Struct to hold information about a partially invariant condition.
  450. struct IVConditionInfo {
  451. /// Instructions that need to be duplicated and checked for the unswitching
  452. /// condition.
  453. SmallVector<Instruction *> InstToDuplicate;
  454. /// Constant to indicate for which value the condition is invariant.
  455. Constant *KnownValue = nullptr;
  456. /// True if the partially invariant path is no-op (=does not have any
  457. /// side-effects and no loop value is used outside the loop).
  458. bool PathIsNoop = true;
  459. /// If the partially invariant path reaches a single exit block, ExitForPath
  460. /// is set to that block. Otherwise it is nullptr.
  461. BasicBlock *ExitForPath = nullptr;
  462. };
  463. /// Check if the loop header has a conditional branch that is not
  464. /// loop-invariant, because it involves load instructions. If all paths from
  465. /// either the true or false successor to the header or loop exists do not
  466. /// modify the memory feeding the condition, perform 'partial unswitching'. That
  467. /// is, duplicate the instructions feeding the condition in the pre-header. Then
  468. /// unswitch on the duplicated condition. The condition is now known in the
  469. /// unswitched version for the 'invariant' path through the original loop.
  470. ///
  471. /// If the branch condition of the header is partially invariant, return a pair
  472. /// containing the instructions to duplicate and a boolean Constant to update
  473. /// the condition in the loops created for the true or false successors.
  474. Optional<IVConditionInfo> hasPartialIVCondition(Loop &L, unsigned MSSAThreshold,
  475. MemorySSA &MSSA, AAResults &AA);
  476. } // end namespace llvm
  477. #endif // LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
  478. #ifdef __GNUC__
  479. #pragma GCC diagnostic pop
  480. #endif