Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Transforms / Utils / UnrollLoop.h
blob8f3824e150a841eaeef477df6851ccf490bad930
1 //===- llvm/Transforms/Utils/UnrollLoop.h - Unrolling 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 unrolling utilities. It does not define any
10 // actual pass or policy, but provides a single function to perform loop
11 // unrolling.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
16 #define LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Transforms/Utils/ValueMapper.h"
23 namespace llvm {
25 class AssumptionCache;
26 class BasicBlock;
27 class DependenceInfo;
28 class DominatorTree;
29 class Loop;
30 class LoopInfo;
31 class MDNode;
32 class OptimizationRemarkEmitter;
33 class ScalarEvolution;
35 using NewLoopsMap = SmallDenseMap<const Loop *, Loop *, 4>;
37 /// @{
38 /// Metadata attribute names
39 const char *const LLVMLoopUnrollFollowupAll = "llvm.loop.unroll.followup_all";
40 const char *const LLVMLoopUnrollFollowupUnrolled =
41 "llvm.loop.unroll.followup_unrolled";
42 const char *const LLVMLoopUnrollFollowupRemainder =
43 "llvm.loop.unroll.followup_remainder";
44 /// @}
46 const Loop* addClonedBlockToLoopInfo(BasicBlock *OriginalBB,
47 BasicBlock *ClonedBB, LoopInfo *LI,
48 NewLoopsMap &NewLoops);
50 /// Represents the result of a \c UnrollLoop invocation.
51 enum class LoopUnrollResult {
52 /// The loop was not modified.
53 Unmodified,
55 /// The loop was partially unrolled -- we still have a loop, but with a
56 /// smaller trip count. We may also have emitted epilogue loop if the loop
57 /// had a non-constant trip count.
58 PartiallyUnrolled,
60 /// The loop was fully unrolled into straight-line code. We no longer have
61 /// any back-edges.
62 FullyUnrolled
65 LoopUnrollResult UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
66 bool Force, bool AllowRuntime,
67 bool AllowExpensiveTripCount, bool PreserveCondBr,
68 bool PreserveOnlyFirst, unsigned TripMultiple,
69 unsigned PeelCount, bool UnrollRemainder,
70 LoopInfo *LI, ScalarEvolution *SE,
71 DominatorTree *DT, AssumptionCache *AC,
72 OptimizationRemarkEmitter *ORE, bool PreserveLCSSA,
73 Loop **RemainderLoop = nullptr);
75 bool UnrollRuntimeLoopRemainder(Loop *L, unsigned Count,
76 bool AllowExpensiveTripCount,
77 bool UseEpilogRemainder, bool UnrollRemainder,
78 LoopInfo *LI, ScalarEvolution *SE,
79 DominatorTree *DT, AssumptionCache *AC,
80 bool PreserveLCSSA,
81 Loop **ResultLoop = nullptr);
83 void computePeelCount(Loop *L, unsigned LoopSize,
84 TargetTransformInfo::UnrollingPreferences &UP,
85 unsigned &TripCount, ScalarEvolution &SE);
87 bool canPeel(Loop *L);
89 bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
90 DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA);
92 LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
93 unsigned TripMultiple, bool UnrollRemainder,
94 LoopInfo *LI, ScalarEvolution *SE,
95 DominatorTree *DT, AssumptionCache *AC,
96 OptimizationRemarkEmitter *ORE,
97 Loop **EpilogueLoop = nullptr);
99 bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
100 DependenceInfo &DI);
102 bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
103 DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
104 const SmallPtrSetImpl<const Value *> &EphValues,
105 OptimizationRemarkEmitter *ORE, unsigned &TripCount,
106 unsigned MaxTripCount, unsigned &TripMultiple,
107 unsigned LoopSize,
108 TargetTransformInfo::UnrollingPreferences &UP,
109 bool &UseUpperBound);
111 BasicBlock *foldBlockIntoPredecessor(BasicBlock *BB, LoopInfo *LI,
112 ScalarEvolution *SE, DominatorTree *DT);
114 void remapInstruction(Instruction *I, ValueToValueMapTy &VMap);
116 void simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
117 ScalarEvolution *SE, DominatorTree *DT,
118 AssumptionCache *AC);
120 MDNode *GetUnrollMetadata(MDNode *LoopID, StringRef Name);
122 TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
123 Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, int OptLevel,
124 Optional<unsigned> UserThreshold, Optional<unsigned> UserCount,
125 Optional<bool> UserAllowPartial, Optional<bool> UserRuntime,
126 Optional<bool> UserUpperBound, Optional<bool> UserAllowPeeling);
128 unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
129 bool &NotDuplicatable, bool &Convergent,
130 const TargetTransformInfo &TTI,
131 const SmallPtrSetImpl<const Value *> &EphValues,
132 unsigned BEInsns);
134 } // end namespace llvm
136 #endif // LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H