1 //===- llvm/Transforms/Utils/UnrollLoop.h - Unrolling utilities -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
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"
25 class AssumptionCache
;
32 class OptimizationRemarkEmitter
;
33 class ScalarEvolution
;
35 using NewLoopsMap
= SmallDenseMap
<const Loop
*, Loop
*, 4>;
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";
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.
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.
60 /// The loop was fully unrolled into straight-line code. We no longer have
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
,
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
,
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
,
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
,
134 } // end namespace llvm
136 #endif // LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H