1*9880d681SAndroid Build Coastguard Worker //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass implements a simple loop unroller. It works best when loops have
11*9880d681SAndroid Build Coastguard Worker // been canonicalized by the -indvars pass, allowing it to determine the trip
12*9880d681SAndroid Build Coastguard Worker // counts of loops easily.
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CodeMetrics.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InstructionSimplify.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPass.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopUnrollAnalyzer.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolution.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DiagnosticInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstVisitor.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Metadata.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/LoopUtils.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/UnrollLoop.h"
37*9880d681SAndroid Build Coastguard Worker #include <climits>
38*9880d681SAndroid Build Coastguard Worker #include <utility>
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker using namespace llvm;
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "loop-unroll"
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
45*9880d681SAndroid Build Coastguard Worker UnrollThreshold("unroll-threshold", cl::Hidden,
46*9880d681SAndroid Build Coastguard Worker cl::desc("The baseline cost threshold for loop unrolling"));
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollPercentDynamicCostSavedThreshold(
49*9880d681SAndroid Build Coastguard Worker "unroll-percent-dynamic-cost-saved-threshold", cl::init(50), cl::Hidden,
50*9880d681SAndroid Build Coastguard Worker cl::desc("The percentage of estimated dynamic cost which must be saved by "
51*9880d681SAndroid Build Coastguard Worker "unrolling to allow unrolling up to the max threshold."));
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollDynamicCostSavingsDiscount(
54*9880d681SAndroid Build Coastguard Worker "unroll-dynamic-cost-savings-discount", cl::init(100), cl::Hidden,
55*9880d681SAndroid Build Coastguard Worker cl::desc("This is the amount discounted from the total unroll cost when "
56*9880d681SAndroid Build Coastguard Worker "the unrolled form has a high dynamic cost savings (triggered by "
57*9880d681SAndroid Build Coastguard Worker "the '-unroll-perecent-dynamic-cost-saved-threshold' flag)."));
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollMaxIterationsCountToAnalyze(
60*9880d681SAndroid Build Coastguard Worker "unroll-max-iteration-count-to-analyze", cl::init(10), cl::Hidden,
61*9880d681SAndroid Build Coastguard Worker cl::desc("Don't allow loop unrolling to simulate more than this number of"
62*9880d681SAndroid Build Coastguard Worker "iterations when checking full unroll profitability"));
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollCount(
65*9880d681SAndroid Build Coastguard Worker "unroll-count", cl::Hidden,
66*9880d681SAndroid Build Coastguard Worker cl::desc("Use this unroll count for all loops including those with "
67*9880d681SAndroid Build Coastguard Worker "unroll_count pragma values, for testing purposes"));
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollMaxCount(
70*9880d681SAndroid Build Coastguard Worker "unroll-max-count", cl::Hidden,
71*9880d681SAndroid Build Coastguard Worker cl::desc("Set the max unroll count for partial and runtime unrolling, for"
72*9880d681SAndroid Build Coastguard Worker "testing purposes"));
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> UnrollFullMaxCount(
75*9880d681SAndroid Build Coastguard Worker "unroll-full-max-count", cl::Hidden,
76*9880d681SAndroid Build Coastguard Worker cl::desc(
77*9880d681SAndroid Build Coastguard Worker "Set the max unroll count for full unrolling, for testing purposes"));
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
80*9880d681SAndroid Build Coastguard Worker UnrollAllowPartial("unroll-allow-partial", cl::Hidden,
81*9880d681SAndroid Build Coastguard Worker cl::desc("Allows loops to be partially unrolled until "
82*9880d681SAndroid Build Coastguard Worker "-unroll-threshold loop size is reached."));
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> UnrollAllowRemainder(
85*9880d681SAndroid Build Coastguard Worker "unroll-allow-remainder", cl::Hidden,
86*9880d681SAndroid Build Coastguard Worker cl::desc("Allow generation of a loop remainder (extra iterations) "
87*9880d681SAndroid Build Coastguard Worker "when unrolling a loop."));
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
90*9880d681SAndroid Build Coastguard Worker UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::Hidden,
91*9880d681SAndroid Build Coastguard Worker cl::desc("Unroll loops with run-time trip counts"));
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> PragmaUnrollThreshold(
94*9880d681SAndroid Build Coastguard Worker "pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden,
95*9880d681SAndroid Build Coastguard Worker cl::desc("Unrolled size limit for loops with an unroll(full) or "
96*9880d681SAndroid Build Coastguard Worker "unroll_count pragma."));
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker /// A magic value for use with the Threshold parameter to indicate
99*9880d681SAndroid Build Coastguard Worker /// that the loop unroll should be performed regardless of how much
100*9880d681SAndroid Build Coastguard Worker /// code expansion would result.
101*9880d681SAndroid Build Coastguard Worker static const unsigned NoThreshold = UINT_MAX;
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker /// Default unroll count for loops with run-time trip count if
104*9880d681SAndroid Build Coastguard Worker /// -unroll-count is not set
105*9880d681SAndroid Build Coastguard Worker static const unsigned DefaultUnrollRuntimeCount = 8;
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard Worker /// Gather the various unrolling parameters based on the defaults, compiler
108*9880d681SAndroid Build Coastguard Worker /// flags, TTI overrides and user specified parameters.
gatherUnrollingPreferences(Loop * L,const TargetTransformInfo & TTI,Optional<unsigned> UserThreshold,Optional<unsigned> UserCount,Optional<bool> UserAllowPartial,Optional<bool> UserRuntime)109*9880d681SAndroid Build Coastguard Worker static TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
110*9880d681SAndroid Build Coastguard Worker Loop *L, const TargetTransformInfo &TTI, Optional<unsigned> UserThreshold,
111*9880d681SAndroid Build Coastguard Worker Optional<unsigned> UserCount, Optional<bool> UserAllowPartial,
112*9880d681SAndroid Build Coastguard Worker Optional<bool> UserRuntime) {
113*9880d681SAndroid Build Coastguard Worker TargetTransformInfo::UnrollingPreferences UP;
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker // Set up the defaults
116*9880d681SAndroid Build Coastguard Worker UP.Threshold = 150;
117*9880d681SAndroid Build Coastguard Worker UP.PercentDynamicCostSavedThreshold = 50;
118*9880d681SAndroid Build Coastguard Worker UP.DynamicCostSavingsDiscount = 100;
119*9880d681SAndroid Build Coastguard Worker UP.OptSizeThreshold = 0;
120*9880d681SAndroid Build Coastguard Worker UP.PartialThreshold = UP.Threshold;
121*9880d681SAndroid Build Coastguard Worker UP.PartialOptSizeThreshold = 0;
122*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
123*9880d681SAndroid Build Coastguard Worker UP.MaxCount = UINT_MAX;
124*9880d681SAndroid Build Coastguard Worker UP.FullUnrollMaxCount = UINT_MAX;
125*9880d681SAndroid Build Coastguard Worker UP.Partial = false;
126*9880d681SAndroid Build Coastguard Worker UP.Runtime = false;
127*9880d681SAndroid Build Coastguard Worker UP.AllowRemainder = true;
128*9880d681SAndroid Build Coastguard Worker UP.AllowExpensiveTripCount = false;
129*9880d681SAndroid Build Coastguard Worker UP.Force = false;
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker // Override with any target specific settings
132*9880d681SAndroid Build Coastguard Worker TTI.getUnrollingPreferences(L, UP);
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker // Apply size attributes
135*9880d681SAndroid Build Coastguard Worker if (L->getHeader()->getParent()->optForSize()) {
136*9880d681SAndroid Build Coastguard Worker UP.Threshold = UP.OptSizeThreshold;
137*9880d681SAndroid Build Coastguard Worker UP.PartialThreshold = UP.PartialOptSizeThreshold;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Apply any user values specified by cl::opt
141*9880d681SAndroid Build Coastguard Worker if (UnrollThreshold.getNumOccurrences() > 0) {
142*9880d681SAndroid Build Coastguard Worker UP.Threshold = UnrollThreshold;
143*9880d681SAndroid Build Coastguard Worker UP.PartialThreshold = UnrollThreshold;
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker if (UnrollPercentDynamicCostSavedThreshold.getNumOccurrences() > 0)
146*9880d681SAndroid Build Coastguard Worker UP.PercentDynamicCostSavedThreshold =
147*9880d681SAndroid Build Coastguard Worker UnrollPercentDynamicCostSavedThreshold;
148*9880d681SAndroid Build Coastguard Worker if (UnrollDynamicCostSavingsDiscount.getNumOccurrences() > 0)
149*9880d681SAndroid Build Coastguard Worker UP.DynamicCostSavingsDiscount = UnrollDynamicCostSavingsDiscount;
150*9880d681SAndroid Build Coastguard Worker if (UnrollMaxCount.getNumOccurrences() > 0)
151*9880d681SAndroid Build Coastguard Worker UP.MaxCount = UnrollMaxCount;
152*9880d681SAndroid Build Coastguard Worker if (UnrollFullMaxCount.getNumOccurrences() > 0)
153*9880d681SAndroid Build Coastguard Worker UP.FullUnrollMaxCount = UnrollFullMaxCount;
154*9880d681SAndroid Build Coastguard Worker if (UnrollAllowPartial.getNumOccurrences() > 0)
155*9880d681SAndroid Build Coastguard Worker UP.Partial = UnrollAllowPartial;
156*9880d681SAndroid Build Coastguard Worker if (UnrollAllowRemainder.getNumOccurrences() > 0)
157*9880d681SAndroid Build Coastguard Worker UP.AllowRemainder = UnrollAllowRemainder;
158*9880d681SAndroid Build Coastguard Worker if (UnrollRuntime.getNumOccurrences() > 0)
159*9880d681SAndroid Build Coastguard Worker UP.Runtime = UnrollRuntime;
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker // Apply user values provided by argument
162*9880d681SAndroid Build Coastguard Worker if (UserThreshold.hasValue()) {
163*9880d681SAndroid Build Coastguard Worker UP.Threshold = *UserThreshold;
164*9880d681SAndroid Build Coastguard Worker UP.PartialThreshold = *UserThreshold;
165*9880d681SAndroid Build Coastguard Worker }
166*9880d681SAndroid Build Coastguard Worker if (UserCount.hasValue())
167*9880d681SAndroid Build Coastguard Worker UP.Count = *UserCount;
168*9880d681SAndroid Build Coastguard Worker if (UserAllowPartial.hasValue())
169*9880d681SAndroid Build Coastguard Worker UP.Partial = *UserAllowPartial;
170*9880d681SAndroid Build Coastguard Worker if (UserRuntime.hasValue())
171*9880d681SAndroid Build Coastguard Worker UP.Runtime = *UserRuntime;
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker return UP;
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker namespace {
177*9880d681SAndroid Build Coastguard Worker /// A struct to densely store the state of an instruction after unrolling at
178*9880d681SAndroid Build Coastguard Worker /// each iteration.
179*9880d681SAndroid Build Coastguard Worker ///
180*9880d681SAndroid Build Coastguard Worker /// This is designed to work like a tuple of <Instruction *, int> for the
181*9880d681SAndroid Build Coastguard Worker /// purposes of hashing and lookup, but to be able to associate two boolean
182*9880d681SAndroid Build Coastguard Worker /// states with each key.
183*9880d681SAndroid Build Coastguard Worker struct UnrolledInstState {
184*9880d681SAndroid Build Coastguard Worker Instruction *I;
185*9880d681SAndroid Build Coastguard Worker int Iteration : 30;
186*9880d681SAndroid Build Coastguard Worker unsigned IsFree : 1;
187*9880d681SAndroid Build Coastguard Worker unsigned IsCounted : 1;
188*9880d681SAndroid Build Coastguard Worker };
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker /// Hashing and equality testing for a set of the instruction states.
191*9880d681SAndroid Build Coastguard Worker struct UnrolledInstStateKeyInfo {
192*9880d681SAndroid Build Coastguard Worker typedef DenseMapInfo<Instruction *> PtrInfo;
193*9880d681SAndroid Build Coastguard Worker typedef DenseMapInfo<std::pair<Instruction *, int>> PairInfo;
getEmptyKey__anon565193f80111::UnrolledInstStateKeyInfo194*9880d681SAndroid Build Coastguard Worker static inline UnrolledInstState getEmptyKey() {
195*9880d681SAndroid Build Coastguard Worker return {PtrInfo::getEmptyKey(), 0, 0, 0};
196*9880d681SAndroid Build Coastguard Worker }
getTombstoneKey__anon565193f80111::UnrolledInstStateKeyInfo197*9880d681SAndroid Build Coastguard Worker static inline UnrolledInstState getTombstoneKey() {
198*9880d681SAndroid Build Coastguard Worker return {PtrInfo::getTombstoneKey(), 0, 0, 0};
199*9880d681SAndroid Build Coastguard Worker }
getHashValue__anon565193f80111::UnrolledInstStateKeyInfo200*9880d681SAndroid Build Coastguard Worker static inline unsigned getHashValue(const UnrolledInstState &S) {
201*9880d681SAndroid Build Coastguard Worker return PairInfo::getHashValue({S.I, S.Iteration});
202*9880d681SAndroid Build Coastguard Worker }
isEqual__anon565193f80111::UnrolledInstStateKeyInfo203*9880d681SAndroid Build Coastguard Worker static inline bool isEqual(const UnrolledInstState &LHS,
204*9880d681SAndroid Build Coastguard Worker const UnrolledInstState &RHS) {
205*9880d681SAndroid Build Coastguard Worker return PairInfo::isEqual({LHS.I, LHS.Iteration}, {RHS.I, RHS.Iteration});
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker };
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker namespace {
211*9880d681SAndroid Build Coastguard Worker struct EstimatedUnrollCost {
212*9880d681SAndroid Build Coastguard Worker /// \brief The estimated cost after unrolling.
213*9880d681SAndroid Build Coastguard Worker int UnrolledCost;
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker /// \brief The estimated dynamic cost of executing the instructions in the
216*9880d681SAndroid Build Coastguard Worker /// rolled form.
217*9880d681SAndroid Build Coastguard Worker int RolledDynamicCost;
218*9880d681SAndroid Build Coastguard Worker };
219*9880d681SAndroid Build Coastguard Worker }
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker /// \brief Figure out if the loop is worth full unrolling.
222*9880d681SAndroid Build Coastguard Worker ///
223*9880d681SAndroid Build Coastguard Worker /// Complete loop unrolling can make some loads constant, and we need to know
224*9880d681SAndroid Build Coastguard Worker /// if that would expose any further optimization opportunities. This routine
225*9880d681SAndroid Build Coastguard Worker /// estimates this optimization. It computes cost of unrolled loop
226*9880d681SAndroid Build Coastguard Worker /// (UnrolledCost) and dynamic cost of the original loop (RolledDynamicCost). By
227*9880d681SAndroid Build Coastguard Worker /// dynamic cost we mean that we won't count costs of blocks that are known not
228*9880d681SAndroid Build Coastguard Worker /// to be executed (i.e. if we have a branch in the loop and we know that at the
229*9880d681SAndroid Build Coastguard Worker /// given iteration its condition would be resolved to true, we won't add up the
230*9880d681SAndroid Build Coastguard Worker /// cost of the 'false'-block).
231*9880d681SAndroid Build Coastguard Worker /// \returns Optional value, holding the RolledDynamicCost and UnrolledCost. If
232*9880d681SAndroid Build Coastguard Worker /// the analysis failed (no benefits expected from the unrolling, or the loop is
233*9880d681SAndroid Build Coastguard Worker /// too big to analyze), the returned value is None.
234*9880d681SAndroid Build Coastguard Worker static Optional<EstimatedUnrollCost>
analyzeLoopUnrollCost(const Loop * L,unsigned TripCount,DominatorTree & DT,ScalarEvolution & SE,const TargetTransformInfo & TTI,int MaxUnrolledLoopSize)235*9880d681SAndroid Build Coastguard Worker analyzeLoopUnrollCost(const Loop *L, unsigned TripCount, DominatorTree &DT,
236*9880d681SAndroid Build Coastguard Worker ScalarEvolution &SE, const TargetTransformInfo &TTI,
237*9880d681SAndroid Build Coastguard Worker int MaxUnrolledLoopSize) {
238*9880d681SAndroid Build Coastguard Worker // We want to be able to scale offsets by the trip count and add more offsets
239*9880d681SAndroid Build Coastguard Worker // to them without checking for overflows, and we already don't want to
240*9880d681SAndroid Build Coastguard Worker // analyze *massive* trip counts, so we force the max to be reasonably small.
241*9880d681SAndroid Build Coastguard Worker assert(UnrollMaxIterationsCountToAnalyze < (INT_MAX / 2) &&
242*9880d681SAndroid Build Coastguard Worker "The unroll iterations max is too large!");
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker // Only analyze inner loops. We can't properly estimate cost of nested loops
245*9880d681SAndroid Build Coastguard Worker // and we won't visit inner loops again anyway.
246*9880d681SAndroid Build Coastguard Worker if (!L->empty())
247*9880d681SAndroid Build Coastguard Worker return None;
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker // Don't simulate loops with a big or unknown tripcount
250*9880d681SAndroid Build Coastguard Worker if (!UnrollMaxIterationsCountToAnalyze || !TripCount ||
251*9880d681SAndroid Build Coastguard Worker TripCount > UnrollMaxIterationsCountToAnalyze)
252*9880d681SAndroid Build Coastguard Worker return None;
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard Worker SmallSetVector<BasicBlock *, 16> BBWorklist;
255*9880d681SAndroid Build Coastguard Worker SmallSetVector<std::pair<BasicBlock *, BasicBlock *>, 4> ExitWorklist;
256*9880d681SAndroid Build Coastguard Worker DenseMap<Value *, Constant *> SimplifiedValues;
257*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<Value *, Constant *>, 4> SimplifiedInputValues;
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard Worker // The estimated cost of the unrolled form of the loop. We try to estimate
260*9880d681SAndroid Build Coastguard Worker // this by simplifying as much as we can while computing the estimate.
261*9880d681SAndroid Build Coastguard Worker int UnrolledCost = 0;
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker // We also track the estimated dynamic (that is, actually executed) cost in
264*9880d681SAndroid Build Coastguard Worker // the rolled form. This helps identify cases when the savings from unrolling
265*9880d681SAndroid Build Coastguard Worker // aren't just exposing dead control flows, but actual reduced dynamic
266*9880d681SAndroid Build Coastguard Worker // instructions due to the simplifications which we expect to occur after
267*9880d681SAndroid Build Coastguard Worker // unrolling.
268*9880d681SAndroid Build Coastguard Worker int RolledDynamicCost = 0;
269*9880d681SAndroid Build Coastguard Worker
270*9880d681SAndroid Build Coastguard Worker // We track the simplification of each instruction in each iteration. We use
271*9880d681SAndroid Build Coastguard Worker // this to recursively merge costs into the unrolled cost on-demand so that
272*9880d681SAndroid Build Coastguard Worker // we don't count the cost of any dead code. This is essentially a map from
273*9880d681SAndroid Build Coastguard Worker // <instruction, int> to <bool, bool>, but stored as a densely packed struct.
274*9880d681SAndroid Build Coastguard Worker DenseSet<UnrolledInstState, UnrolledInstStateKeyInfo> InstCostMap;
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker // A small worklist used to accumulate cost of instructions from each
277*9880d681SAndroid Build Coastguard Worker // observable and reached root in the loop.
278*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction *, 16> CostWorklist;
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker // PHI-used worklist used between iterations while accumulating cost.
281*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction *, 4> PHIUsedList;
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker // Helper function to accumulate cost for instructions in the loop.
284*9880d681SAndroid Build Coastguard Worker auto AddCostRecursively = [&](Instruction &RootI, int Iteration) {
285*9880d681SAndroid Build Coastguard Worker assert(Iteration >= 0 && "Cannot have a negative iteration!");
286*9880d681SAndroid Build Coastguard Worker assert(CostWorklist.empty() && "Must start with an empty cost list");
287*9880d681SAndroid Build Coastguard Worker assert(PHIUsedList.empty() && "Must start with an empty phi used list");
288*9880d681SAndroid Build Coastguard Worker CostWorklist.push_back(&RootI);
289*9880d681SAndroid Build Coastguard Worker for (;; --Iteration) {
290*9880d681SAndroid Build Coastguard Worker do {
291*9880d681SAndroid Build Coastguard Worker Instruction *I = CostWorklist.pop_back_val();
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker // InstCostMap only uses I and Iteration as a key, the other two values
294*9880d681SAndroid Build Coastguard Worker // don't matter here.
295*9880d681SAndroid Build Coastguard Worker auto CostIter = InstCostMap.find({I, Iteration, 0, 0});
296*9880d681SAndroid Build Coastguard Worker if (CostIter == InstCostMap.end())
297*9880d681SAndroid Build Coastguard Worker // If an input to a PHI node comes from a dead path through the loop
298*9880d681SAndroid Build Coastguard Worker // we may have no cost data for it here. What that actually means is
299*9880d681SAndroid Build Coastguard Worker // that it is free.
300*9880d681SAndroid Build Coastguard Worker continue;
301*9880d681SAndroid Build Coastguard Worker auto &Cost = *CostIter;
302*9880d681SAndroid Build Coastguard Worker if (Cost.IsCounted)
303*9880d681SAndroid Build Coastguard Worker // Already counted this instruction.
304*9880d681SAndroid Build Coastguard Worker continue;
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker // Mark that we are counting the cost of this instruction now.
307*9880d681SAndroid Build Coastguard Worker Cost.IsCounted = true;
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker // If this is a PHI node in the loop header, just add it to the PHI set.
310*9880d681SAndroid Build Coastguard Worker if (auto *PhiI = dyn_cast<PHINode>(I))
311*9880d681SAndroid Build Coastguard Worker if (PhiI->getParent() == L->getHeader()) {
312*9880d681SAndroid Build Coastguard Worker assert(Cost.IsFree && "Loop PHIs shouldn't be evaluated as they "
313*9880d681SAndroid Build Coastguard Worker "inherently simplify during unrolling.");
314*9880d681SAndroid Build Coastguard Worker if (Iteration == 0)
315*9880d681SAndroid Build Coastguard Worker continue;
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard Worker // Push the incoming value from the backedge into the PHI used list
318*9880d681SAndroid Build Coastguard Worker // if it is an in-loop instruction. We'll use this to populate the
319*9880d681SAndroid Build Coastguard Worker // cost worklist for the next iteration (as we count backwards).
320*9880d681SAndroid Build Coastguard Worker if (auto *OpI = dyn_cast<Instruction>(
321*9880d681SAndroid Build Coastguard Worker PhiI->getIncomingValueForBlock(L->getLoopLatch())))
322*9880d681SAndroid Build Coastguard Worker if (L->contains(OpI))
323*9880d681SAndroid Build Coastguard Worker PHIUsedList.push_back(OpI);
324*9880d681SAndroid Build Coastguard Worker continue;
325*9880d681SAndroid Build Coastguard Worker }
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Worker // First accumulate the cost of this instruction.
328*9880d681SAndroid Build Coastguard Worker if (!Cost.IsFree) {
329*9880d681SAndroid Build Coastguard Worker UnrolledCost += TTI.getUserCost(I);
330*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Adding cost of instruction (iteration " << Iteration
331*9880d681SAndroid Build Coastguard Worker << "): ");
332*9880d681SAndroid Build Coastguard Worker DEBUG(I->dump());
333*9880d681SAndroid Build Coastguard Worker }
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // We must count the cost of every operand which is not free,
336*9880d681SAndroid Build Coastguard Worker // recursively. If we reach a loop PHI node, simply add it to the set
337*9880d681SAndroid Build Coastguard Worker // to be considered on the next iteration (backwards!).
338*9880d681SAndroid Build Coastguard Worker for (Value *Op : I->operands()) {
339*9880d681SAndroid Build Coastguard Worker // Check whether this operand is free due to being a constant or
340*9880d681SAndroid Build Coastguard Worker // outside the loop.
341*9880d681SAndroid Build Coastguard Worker auto *OpI = dyn_cast<Instruction>(Op);
342*9880d681SAndroid Build Coastguard Worker if (!OpI || !L->contains(OpI))
343*9880d681SAndroid Build Coastguard Worker continue;
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker // Otherwise accumulate its cost.
346*9880d681SAndroid Build Coastguard Worker CostWorklist.push_back(OpI);
347*9880d681SAndroid Build Coastguard Worker }
348*9880d681SAndroid Build Coastguard Worker } while (!CostWorklist.empty());
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker if (PHIUsedList.empty())
351*9880d681SAndroid Build Coastguard Worker // We've exhausted the search.
352*9880d681SAndroid Build Coastguard Worker break;
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker assert(Iteration > 0 &&
355*9880d681SAndroid Build Coastguard Worker "Cannot track PHI-used values past the first iteration!");
356*9880d681SAndroid Build Coastguard Worker CostWorklist.append(PHIUsedList.begin(), PHIUsedList.end());
357*9880d681SAndroid Build Coastguard Worker PHIUsedList.clear();
358*9880d681SAndroid Build Coastguard Worker }
359*9880d681SAndroid Build Coastguard Worker };
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker // Ensure that we don't violate the loop structure invariants relied on by
362*9880d681SAndroid Build Coastguard Worker // this analysis.
363*9880d681SAndroid Build Coastguard Worker assert(L->isLoopSimplifyForm() && "Must put loop into normal form first.");
364*9880d681SAndroid Build Coastguard Worker assert(L->isLCSSAForm(DT) &&
365*9880d681SAndroid Build Coastguard Worker "Must have loops in LCSSA form to track live-out values.");
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Starting LoopUnroll profitability analysis...\n");
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker // Simulate execution of each iteration of the loop counting instructions,
370*9880d681SAndroid Build Coastguard Worker // which would be simplified.
371*9880d681SAndroid Build Coastguard Worker // Since the same load will take different values on different iterations,
372*9880d681SAndroid Build Coastguard Worker // we literally have to go through all loop's iterations.
373*9880d681SAndroid Build Coastguard Worker for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) {
374*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Analyzing iteration " << Iteration << "\n");
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker // Prepare for the iteration by collecting any simplified entry or backedge
377*9880d681SAndroid Build Coastguard Worker // inputs.
378*9880d681SAndroid Build Coastguard Worker for (Instruction &I : *L->getHeader()) {
379*9880d681SAndroid Build Coastguard Worker auto *PHI = dyn_cast<PHINode>(&I);
380*9880d681SAndroid Build Coastguard Worker if (!PHI)
381*9880d681SAndroid Build Coastguard Worker break;
382*9880d681SAndroid Build Coastguard Worker
383*9880d681SAndroid Build Coastguard Worker // The loop header PHI nodes must have exactly two input: one from the
384*9880d681SAndroid Build Coastguard Worker // loop preheader and one from the loop latch.
385*9880d681SAndroid Build Coastguard Worker assert(
386*9880d681SAndroid Build Coastguard Worker PHI->getNumIncomingValues() == 2 &&
387*9880d681SAndroid Build Coastguard Worker "Must have an incoming value only for the preheader and the latch.");
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker Value *V = PHI->getIncomingValueForBlock(
390*9880d681SAndroid Build Coastguard Worker Iteration == 0 ? L->getLoopPreheader() : L->getLoopLatch());
391*9880d681SAndroid Build Coastguard Worker Constant *C = dyn_cast<Constant>(V);
392*9880d681SAndroid Build Coastguard Worker if (Iteration != 0 && !C)
393*9880d681SAndroid Build Coastguard Worker C = SimplifiedValues.lookup(V);
394*9880d681SAndroid Build Coastguard Worker if (C)
395*9880d681SAndroid Build Coastguard Worker SimplifiedInputValues.push_back({PHI, C});
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker // Now clear and re-populate the map for the next iteration.
399*9880d681SAndroid Build Coastguard Worker SimplifiedValues.clear();
400*9880d681SAndroid Build Coastguard Worker while (!SimplifiedInputValues.empty())
401*9880d681SAndroid Build Coastguard Worker SimplifiedValues.insert(SimplifiedInputValues.pop_back_val());
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, SE, L);
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker BBWorklist.clear();
406*9880d681SAndroid Build Coastguard Worker BBWorklist.insert(L->getHeader());
407*9880d681SAndroid Build Coastguard Worker // Note that we *must not* cache the size, this loop grows the worklist.
408*9880d681SAndroid Build Coastguard Worker for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) {
409*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = BBWorklist[Idx];
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker // Visit all instructions in the given basic block and try to simplify
412*9880d681SAndroid Build Coastguard Worker // it. We don't change the actual IR, just count optimization
413*9880d681SAndroid Build Coastguard Worker // opportunities.
414*9880d681SAndroid Build Coastguard Worker for (Instruction &I : *BB) {
415*9880d681SAndroid Build Coastguard Worker // Track this instruction's expected baseline cost when executing the
416*9880d681SAndroid Build Coastguard Worker // rolled loop form.
417*9880d681SAndroid Build Coastguard Worker RolledDynamicCost += TTI.getUserCost(&I);
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker // Visit the instruction to analyze its loop cost after unrolling,
420*9880d681SAndroid Build Coastguard Worker // and if the visitor returns true, mark the instruction as free after
421*9880d681SAndroid Build Coastguard Worker // unrolling and continue.
422*9880d681SAndroid Build Coastguard Worker bool IsFree = Analyzer.visit(I);
423*9880d681SAndroid Build Coastguard Worker bool Inserted = InstCostMap.insert({&I, (int)Iteration,
424*9880d681SAndroid Build Coastguard Worker (unsigned)IsFree,
425*9880d681SAndroid Build Coastguard Worker /*IsCounted*/ false}).second;
426*9880d681SAndroid Build Coastguard Worker (void)Inserted;
427*9880d681SAndroid Build Coastguard Worker assert(Inserted && "Cannot have a state for an unvisited instruction!");
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker if (IsFree)
430*9880d681SAndroid Build Coastguard Worker continue;
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker // If the instruction might have a side-effect recursively account for
433*9880d681SAndroid Build Coastguard Worker // the cost of it and all the instructions leading up to it.
434*9880d681SAndroid Build Coastguard Worker if (I.mayHaveSideEffects())
435*9880d681SAndroid Build Coastguard Worker AddCostRecursively(I, Iteration);
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker // Can't properly model a cost of a call.
438*9880d681SAndroid Build Coastguard Worker // FIXME: With a proper cost model we should be able to do it.
439*9880d681SAndroid Build Coastguard Worker if(isa<CallInst>(&I))
440*9880d681SAndroid Build Coastguard Worker return None;
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker // If unrolled body turns out to be too big, bail out.
443*9880d681SAndroid Build Coastguard Worker if (UnrolledCost > MaxUnrolledLoopSize) {
444*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Exceeded threshold.. exiting.\n"
445*9880d681SAndroid Build Coastguard Worker << " UnrolledCost: " << UnrolledCost
446*9880d681SAndroid Build Coastguard Worker << ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize
447*9880d681SAndroid Build Coastguard Worker << "\n");
448*9880d681SAndroid Build Coastguard Worker return None;
449*9880d681SAndroid Build Coastguard Worker }
450*9880d681SAndroid Build Coastguard Worker }
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard Worker TerminatorInst *TI = BB->getTerminator();
453*9880d681SAndroid Build Coastguard Worker
454*9880d681SAndroid Build Coastguard Worker // Add in the live successors by first checking whether we have terminator
455*9880d681SAndroid Build Coastguard Worker // that may be simplified based on the values simplified by this call.
456*9880d681SAndroid Build Coastguard Worker BasicBlock *KnownSucc = nullptr;
457*9880d681SAndroid Build Coastguard Worker if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
458*9880d681SAndroid Build Coastguard Worker if (BI->isConditional()) {
459*9880d681SAndroid Build Coastguard Worker if (Constant *SimpleCond =
460*9880d681SAndroid Build Coastguard Worker SimplifiedValues.lookup(BI->getCondition())) {
461*9880d681SAndroid Build Coastguard Worker // Just take the first successor if condition is undef
462*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(SimpleCond))
463*9880d681SAndroid Build Coastguard Worker KnownSucc = BI->getSuccessor(0);
464*9880d681SAndroid Build Coastguard Worker else if (ConstantInt *SimpleCondVal =
465*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantInt>(SimpleCond))
466*9880d681SAndroid Build Coastguard Worker KnownSucc = BI->getSuccessor(SimpleCondVal->isZero() ? 1 : 0);
467*9880d681SAndroid Build Coastguard Worker }
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
470*9880d681SAndroid Build Coastguard Worker if (Constant *SimpleCond =
471*9880d681SAndroid Build Coastguard Worker SimplifiedValues.lookup(SI->getCondition())) {
472*9880d681SAndroid Build Coastguard Worker // Just take the first successor if condition is undef
473*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(SimpleCond))
474*9880d681SAndroid Build Coastguard Worker KnownSucc = SI->getSuccessor(0);
475*9880d681SAndroid Build Coastguard Worker else if (ConstantInt *SimpleCondVal =
476*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantInt>(SimpleCond))
477*9880d681SAndroid Build Coastguard Worker KnownSucc = SI->findCaseValue(SimpleCondVal).getCaseSuccessor();
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker }
480*9880d681SAndroid Build Coastguard Worker if (KnownSucc) {
481*9880d681SAndroid Build Coastguard Worker if (L->contains(KnownSucc))
482*9880d681SAndroid Build Coastguard Worker BBWorklist.insert(KnownSucc);
483*9880d681SAndroid Build Coastguard Worker else
484*9880d681SAndroid Build Coastguard Worker ExitWorklist.insert({BB, KnownSucc});
485*9880d681SAndroid Build Coastguard Worker continue;
486*9880d681SAndroid Build Coastguard Worker }
487*9880d681SAndroid Build Coastguard Worker
488*9880d681SAndroid Build Coastguard Worker // Add BB's successors to the worklist.
489*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Succ : successors(BB))
490*9880d681SAndroid Build Coastguard Worker if (L->contains(Succ))
491*9880d681SAndroid Build Coastguard Worker BBWorklist.insert(Succ);
492*9880d681SAndroid Build Coastguard Worker else
493*9880d681SAndroid Build Coastguard Worker ExitWorklist.insert({BB, Succ});
494*9880d681SAndroid Build Coastguard Worker AddCostRecursively(*TI, Iteration);
495*9880d681SAndroid Build Coastguard Worker }
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker // If we found no optimization opportunities on the first iteration, we
498*9880d681SAndroid Build Coastguard Worker // won't find them on later ones too.
499*9880d681SAndroid Build Coastguard Worker if (UnrolledCost == RolledDynamicCost) {
500*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " No opportunities found.. exiting.\n"
501*9880d681SAndroid Build Coastguard Worker << " UnrolledCost: " << UnrolledCost << "\n");
502*9880d681SAndroid Build Coastguard Worker return None;
503*9880d681SAndroid Build Coastguard Worker }
504*9880d681SAndroid Build Coastguard Worker }
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard Worker while (!ExitWorklist.empty()) {
507*9880d681SAndroid Build Coastguard Worker BasicBlock *ExitingBB, *ExitBB;
508*9880d681SAndroid Build Coastguard Worker std::tie(ExitingBB, ExitBB) = ExitWorklist.pop_back_val();
509*9880d681SAndroid Build Coastguard Worker
510*9880d681SAndroid Build Coastguard Worker for (Instruction &I : *ExitBB) {
511*9880d681SAndroid Build Coastguard Worker auto *PN = dyn_cast<PHINode>(&I);
512*9880d681SAndroid Build Coastguard Worker if (!PN)
513*9880d681SAndroid Build Coastguard Worker break;
514*9880d681SAndroid Build Coastguard Worker
515*9880d681SAndroid Build Coastguard Worker Value *Op = PN->getIncomingValueForBlock(ExitingBB);
516*9880d681SAndroid Build Coastguard Worker if (auto *OpI = dyn_cast<Instruction>(Op))
517*9880d681SAndroid Build Coastguard Worker if (L->contains(OpI))
518*9880d681SAndroid Build Coastguard Worker AddCostRecursively(*OpI, TripCount - 1);
519*9880d681SAndroid Build Coastguard Worker }
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Analysis finished:\n"
523*9880d681SAndroid Build Coastguard Worker << "UnrolledCost: " << UnrolledCost << ", "
524*9880d681SAndroid Build Coastguard Worker << "RolledDynamicCost: " << RolledDynamicCost << "\n");
525*9880d681SAndroid Build Coastguard Worker return {{UnrolledCost, RolledDynamicCost}};
526*9880d681SAndroid Build Coastguard Worker }
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard Worker /// ApproximateLoopSize - Approximate the size of the loop.
ApproximateLoopSize(const Loop * L,unsigned & NumCalls,bool & NotDuplicatable,bool & Convergent,const TargetTransformInfo & TTI,AssumptionCache * AC)529*9880d681SAndroid Build Coastguard Worker static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
530*9880d681SAndroid Build Coastguard Worker bool &NotDuplicatable, bool &Convergent,
531*9880d681SAndroid Build Coastguard Worker const TargetTransformInfo &TTI,
532*9880d681SAndroid Build Coastguard Worker AssumptionCache *AC) {
533*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const Value *, 32> EphValues;
534*9880d681SAndroid Build Coastguard Worker CodeMetrics::collectEphemeralValues(L, AC, EphValues);
535*9880d681SAndroid Build Coastguard Worker
536*9880d681SAndroid Build Coastguard Worker CodeMetrics Metrics;
537*9880d681SAndroid Build Coastguard Worker for (BasicBlock *BB : L->blocks())
538*9880d681SAndroid Build Coastguard Worker Metrics.analyzeBasicBlock(BB, TTI, EphValues);
539*9880d681SAndroid Build Coastguard Worker NumCalls = Metrics.NumInlineCandidates;
540*9880d681SAndroid Build Coastguard Worker NotDuplicatable = Metrics.notDuplicatable;
541*9880d681SAndroid Build Coastguard Worker Convergent = Metrics.convergent;
542*9880d681SAndroid Build Coastguard Worker
543*9880d681SAndroid Build Coastguard Worker unsigned LoopSize = Metrics.NumInsts;
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker // Don't allow an estimate of size zero. This would allows unrolling of loops
546*9880d681SAndroid Build Coastguard Worker // with huge iteration counts, which is a compile time problem even if it's
547*9880d681SAndroid Build Coastguard Worker // not a problem for code quality. Also, the code using this size may assume
548*9880d681SAndroid Build Coastguard Worker // that each loop has at least three instructions (likely a conditional
549*9880d681SAndroid Build Coastguard Worker // branch, a comparison feeding that branch, and some kind of loop increment
550*9880d681SAndroid Build Coastguard Worker // feeding that comparison instruction).
551*9880d681SAndroid Build Coastguard Worker LoopSize = std::max(LoopSize, 3u);
552*9880d681SAndroid Build Coastguard Worker
553*9880d681SAndroid Build Coastguard Worker return LoopSize;
554*9880d681SAndroid Build Coastguard Worker }
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker // Returns the loop hint metadata node with the given name (for example,
557*9880d681SAndroid Build Coastguard Worker // "llvm.loop.unroll.count"). If no such metadata node exists, then nullptr is
558*9880d681SAndroid Build Coastguard Worker // returned.
GetUnrollMetadataForLoop(const Loop * L,StringRef Name)559*9880d681SAndroid Build Coastguard Worker static MDNode *GetUnrollMetadataForLoop(const Loop *L, StringRef Name) {
560*9880d681SAndroid Build Coastguard Worker if (MDNode *LoopID = L->getLoopID())
561*9880d681SAndroid Build Coastguard Worker return GetUnrollMetadata(LoopID, Name);
562*9880d681SAndroid Build Coastguard Worker return nullptr;
563*9880d681SAndroid Build Coastguard Worker }
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard Worker // Returns true if the loop has an unroll(full) pragma.
HasUnrollFullPragma(const Loop * L)566*9880d681SAndroid Build Coastguard Worker static bool HasUnrollFullPragma(const Loop *L) {
567*9880d681SAndroid Build Coastguard Worker return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.full");
568*9880d681SAndroid Build Coastguard Worker }
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker // Returns true if the loop has an unroll(enable) pragma. This metadata is used
571*9880d681SAndroid Build Coastguard Worker // for both "#pragma unroll" and "#pragma clang loop unroll(enable)" directives.
HasUnrollEnablePragma(const Loop * L)572*9880d681SAndroid Build Coastguard Worker static bool HasUnrollEnablePragma(const Loop *L) {
573*9880d681SAndroid Build Coastguard Worker return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.enable");
574*9880d681SAndroid Build Coastguard Worker }
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker // Returns true if the loop has an unroll(disable) pragma.
HasUnrollDisablePragma(const Loop * L)577*9880d681SAndroid Build Coastguard Worker static bool HasUnrollDisablePragma(const Loop *L) {
578*9880d681SAndroid Build Coastguard Worker return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.disable");
579*9880d681SAndroid Build Coastguard Worker }
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard Worker // Returns true if the loop has an runtime unroll(disable) pragma.
HasRuntimeUnrollDisablePragma(const Loop * L)582*9880d681SAndroid Build Coastguard Worker static bool HasRuntimeUnrollDisablePragma(const Loop *L) {
583*9880d681SAndroid Build Coastguard Worker return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.runtime.disable");
584*9880d681SAndroid Build Coastguard Worker }
585*9880d681SAndroid Build Coastguard Worker
586*9880d681SAndroid Build Coastguard Worker // If loop has an unroll_count pragma return the (necessarily
587*9880d681SAndroid Build Coastguard Worker // positive) value from the pragma. Otherwise return 0.
UnrollCountPragmaValue(const Loop * L)588*9880d681SAndroid Build Coastguard Worker static unsigned UnrollCountPragmaValue(const Loop *L) {
589*9880d681SAndroid Build Coastguard Worker MDNode *MD = GetUnrollMetadataForLoop(L, "llvm.loop.unroll.count");
590*9880d681SAndroid Build Coastguard Worker if (MD) {
591*9880d681SAndroid Build Coastguard Worker assert(MD->getNumOperands() == 2 &&
592*9880d681SAndroid Build Coastguard Worker "Unroll count hint metadata should have two operands.");
593*9880d681SAndroid Build Coastguard Worker unsigned Count =
594*9880d681SAndroid Build Coastguard Worker mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue();
595*9880d681SAndroid Build Coastguard Worker assert(Count >= 1 && "Unroll count must be positive.");
596*9880d681SAndroid Build Coastguard Worker return Count;
597*9880d681SAndroid Build Coastguard Worker }
598*9880d681SAndroid Build Coastguard Worker return 0;
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker
601*9880d681SAndroid Build Coastguard Worker // Remove existing unroll metadata and add unroll disable metadata to
602*9880d681SAndroid Build Coastguard Worker // indicate the loop has already been unrolled. This prevents a loop
603*9880d681SAndroid Build Coastguard Worker // from being unrolled more than is directed by a pragma if the loop
604*9880d681SAndroid Build Coastguard Worker // unrolling pass is run more than once (which it generally is).
SetLoopAlreadyUnrolled(Loop * L)605*9880d681SAndroid Build Coastguard Worker static void SetLoopAlreadyUnrolled(Loop *L) {
606*9880d681SAndroid Build Coastguard Worker MDNode *LoopID = L->getLoopID();
607*9880d681SAndroid Build Coastguard Worker // First remove any existing loop unrolling metadata.
608*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 4> MDs;
609*9880d681SAndroid Build Coastguard Worker // Reserve first location for self reference to the LoopID metadata node.
610*9880d681SAndroid Build Coastguard Worker MDs.push_back(nullptr);
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker if (LoopID) {
613*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
614*9880d681SAndroid Build Coastguard Worker bool IsUnrollMetadata = false;
615*9880d681SAndroid Build Coastguard Worker MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
616*9880d681SAndroid Build Coastguard Worker if (MD) {
617*9880d681SAndroid Build Coastguard Worker const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
618*9880d681SAndroid Build Coastguard Worker IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
619*9880d681SAndroid Build Coastguard Worker }
620*9880d681SAndroid Build Coastguard Worker if (!IsUnrollMetadata)
621*9880d681SAndroid Build Coastguard Worker MDs.push_back(LoopID->getOperand(i));
622*9880d681SAndroid Build Coastguard Worker }
623*9880d681SAndroid Build Coastguard Worker }
624*9880d681SAndroid Build Coastguard Worker
625*9880d681SAndroid Build Coastguard Worker // Add unroll(disable) metadata to disable future unrolling.
626*9880d681SAndroid Build Coastguard Worker LLVMContext &Context = L->getHeader()->getContext();
627*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 1> DisableOperands;
628*9880d681SAndroid Build Coastguard Worker DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
629*9880d681SAndroid Build Coastguard Worker MDNode *DisableNode = MDNode::get(Context, DisableOperands);
630*9880d681SAndroid Build Coastguard Worker MDs.push_back(DisableNode);
631*9880d681SAndroid Build Coastguard Worker
632*9880d681SAndroid Build Coastguard Worker MDNode *NewLoopID = MDNode::get(Context, MDs);
633*9880d681SAndroid Build Coastguard Worker // Set operand 0 to refer to the loop id itself.
634*9880d681SAndroid Build Coastguard Worker NewLoopID->replaceOperandWith(0, NewLoopID);
635*9880d681SAndroid Build Coastguard Worker L->setLoopID(NewLoopID);
636*9880d681SAndroid Build Coastguard Worker }
637*9880d681SAndroid Build Coastguard Worker
canUnrollCompletely(Loop * L,unsigned Threshold,unsigned PercentDynamicCostSavedThreshold,unsigned DynamicCostSavingsDiscount,uint64_t UnrolledCost,uint64_t RolledDynamicCost)638*9880d681SAndroid Build Coastguard Worker static bool canUnrollCompletely(Loop *L, unsigned Threshold,
639*9880d681SAndroid Build Coastguard Worker unsigned PercentDynamicCostSavedThreshold,
640*9880d681SAndroid Build Coastguard Worker unsigned DynamicCostSavingsDiscount,
641*9880d681SAndroid Build Coastguard Worker uint64_t UnrolledCost,
642*9880d681SAndroid Build Coastguard Worker uint64_t RolledDynamicCost) {
643*9880d681SAndroid Build Coastguard Worker if (Threshold == NoThreshold) {
644*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Can fully unroll, because no threshold is set.\n");
645*9880d681SAndroid Build Coastguard Worker return true;
646*9880d681SAndroid Build Coastguard Worker }
647*9880d681SAndroid Build Coastguard Worker
648*9880d681SAndroid Build Coastguard Worker if (UnrolledCost <= Threshold) {
649*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Can fully unroll, because unrolled cost: "
650*9880d681SAndroid Build Coastguard Worker << UnrolledCost << "<" << Threshold << "\n");
651*9880d681SAndroid Build Coastguard Worker return true;
652*9880d681SAndroid Build Coastguard Worker }
653*9880d681SAndroid Build Coastguard Worker
654*9880d681SAndroid Build Coastguard Worker assert(UnrolledCost && "UnrolledCost can't be 0 at this point.");
655*9880d681SAndroid Build Coastguard Worker assert(RolledDynamicCost >= UnrolledCost &&
656*9880d681SAndroid Build Coastguard Worker "Cannot have a higher unrolled cost than a rolled cost!");
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker // Compute the percentage of the dynamic cost in the rolled form that is
659*9880d681SAndroid Build Coastguard Worker // saved when unrolled. If unrolling dramatically reduces the estimated
660*9880d681SAndroid Build Coastguard Worker // dynamic cost of the loop, we use a higher threshold to allow more
661*9880d681SAndroid Build Coastguard Worker // unrolling.
662*9880d681SAndroid Build Coastguard Worker unsigned PercentDynamicCostSaved =
663*9880d681SAndroid Build Coastguard Worker (uint64_t)(RolledDynamicCost - UnrolledCost) * 100ull / RolledDynamicCost;
664*9880d681SAndroid Build Coastguard Worker
665*9880d681SAndroid Build Coastguard Worker if (PercentDynamicCostSaved >= PercentDynamicCostSavedThreshold &&
666*9880d681SAndroid Build Coastguard Worker (int64_t)UnrolledCost - (int64_t)DynamicCostSavingsDiscount <=
667*9880d681SAndroid Build Coastguard Worker (int64_t)Threshold) {
668*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Can fully unroll, because unrolling will reduce the "
669*9880d681SAndroid Build Coastguard Worker "expected dynamic cost by "
670*9880d681SAndroid Build Coastguard Worker << PercentDynamicCostSaved << "% (threshold: "
671*9880d681SAndroid Build Coastguard Worker << PercentDynamicCostSavedThreshold << "%)\n"
672*9880d681SAndroid Build Coastguard Worker << " and the unrolled cost (" << UnrolledCost
673*9880d681SAndroid Build Coastguard Worker << ") is less than the max threshold ("
674*9880d681SAndroid Build Coastguard Worker << DynamicCostSavingsDiscount << ").\n");
675*9880d681SAndroid Build Coastguard Worker return true;
676*9880d681SAndroid Build Coastguard Worker }
677*9880d681SAndroid Build Coastguard Worker
678*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Too large to fully unroll:\n");
679*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Threshold: " << Threshold << "\n");
680*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Max threshold: " << DynamicCostSavingsDiscount << "\n");
681*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Percent cost saved threshold: "
682*9880d681SAndroid Build Coastguard Worker << PercentDynamicCostSavedThreshold << "%\n");
683*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Unrolled cost: " << UnrolledCost << "\n");
684*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Rolled dynamic cost: " << RolledDynamicCost << "\n");
685*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Percent cost saved: " << PercentDynamicCostSaved
686*9880d681SAndroid Build Coastguard Worker << "\n");
687*9880d681SAndroid Build Coastguard Worker return false;
688*9880d681SAndroid Build Coastguard Worker }
689*9880d681SAndroid Build Coastguard Worker
690*9880d681SAndroid Build Coastguard Worker // Returns true if unroll count was set explicitly.
691*9880d681SAndroid Build Coastguard Worker // Calculates unroll count and writes it to UP.Count.
computeUnrollCount(Loop * L,const TargetTransformInfo & TTI,DominatorTree & DT,LoopInfo * LI,ScalarEvolution * SE,unsigned TripCount,unsigned TripMultiple,unsigned LoopSize,TargetTransformInfo::UnrollingPreferences & UP)692*9880d681SAndroid Build Coastguard Worker static bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
693*9880d681SAndroid Build Coastguard Worker DominatorTree &DT, LoopInfo *LI,
694*9880d681SAndroid Build Coastguard Worker ScalarEvolution *SE, unsigned TripCount,
695*9880d681SAndroid Build Coastguard Worker unsigned TripMultiple, unsigned LoopSize,
696*9880d681SAndroid Build Coastguard Worker TargetTransformInfo::UnrollingPreferences &UP) {
697*9880d681SAndroid Build Coastguard Worker // BEInsns represents number of instructions optimized when "back edge"
698*9880d681SAndroid Build Coastguard Worker // becomes "fall through" in unrolled loop.
699*9880d681SAndroid Build Coastguard Worker // For now we count a conditional branch on a backedge and a comparison
700*9880d681SAndroid Build Coastguard Worker // feeding it.
701*9880d681SAndroid Build Coastguard Worker unsigned BEInsns = 2;
702*9880d681SAndroid Build Coastguard Worker // Check for explicit Count.
703*9880d681SAndroid Build Coastguard Worker // 1st priority is unroll count set by "unroll-count" option.
704*9880d681SAndroid Build Coastguard Worker bool UserUnrollCount = UnrollCount.getNumOccurrences() > 0;
705*9880d681SAndroid Build Coastguard Worker if (UserUnrollCount) {
706*9880d681SAndroid Build Coastguard Worker UP.Count = UnrollCount;
707*9880d681SAndroid Build Coastguard Worker UP.AllowExpensiveTripCount = true;
708*9880d681SAndroid Build Coastguard Worker UP.Force = true;
709*9880d681SAndroid Build Coastguard Worker if (UP.AllowRemainder &&
710*9880d681SAndroid Build Coastguard Worker (LoopSize - BEInsns) * UP.Count + BEInsns < UP.Threshold)
711*9880d681SAndroid Build Coastguard Worker return true;
712*9880d681SAndroid Build Coastguard Worker }
713*9880d681SAndroid Build Coastguard Worker
714*9880d681SAndroid Build Coastguard Worker // 2nd priority is unroll count set by pragma.
715*9880d681SAndroid Build Coastguard Worker unsigned PragmaCount = UnrollCountPragmaValue(L);
716*9880d681SAndroid Build Coastguard Worker if (PragmaCount > 0) {
717*9880d681SAndroid Build Coastguard Worker UP.Count = PragmaCount;
718*9880d681SAndroid Build Coastguard Worker UP.Runtime = true;
719*9880d681SAndroid Build Coastguard Worker UP.AllowExpensiveTripCount = true;
720*9880d681SAndroid Build Coastguard Worker UP.Force = true;
721*9880d681SAndroid Build Coastguard Worker if (UP.AllowRemainder &&
722*9880d681SAndroid Build Coastguard Worker (LoopSize - BEInsns) * UP.Count + BEInsns < PragmaUnrollThreshold)
723*9880d681SAndroid Build Coastguard Worker return true;
724*9880d681SAndroid Build Coastguard Worker }
725*9880d681SAndroid Build Coastguard Worker bool PragmaFullUnroll = HasUnrollFullPragma(L);
726*9880d681SAndroid Build Coastguard Worker if (PragmaFullUnroll && TripCount != 0) {
727*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
728*9880d681SAndroid Build Coastguard Worker if ((LoopSize - BEInsns) * UP.Count + BEInsns < PragmaUnrollThreshold)
729*9880d681SAndroid Build Coastguard Worker return false;
730*9880d681SAndroid Build Coastguard Worker }
731*9880d681SAndroid Build Coastguard Worker
732*9880d681SAndroid Build Coastguard Worker bool PragmaEnableUnroll = HasUnrollEnablePragma(L);
733*9880d681SAndroid Build Coastguard Worker bool ExplicitUnroll = PragmaCount > 0 || PragmaFullUnroll ||
734*9880d681SAndroid Build Coastguard Worker PragmaEnableUnroll || UserUnrollCount;
735*9880d681SAndroid Build Coastguard Worker
736*9880d681SAndroid Build Coastguard Worker uint64_t UnrolledSize;
737*9880d681SAndroid Build Coastguard Worker DebugLoc LoopLoc = L->getStartLoc();
738*9880d681SAndroid Build Coastguard Worker Function *F = L->getHeader()->getParent();
739*9880d681SAndroid Build Coastguard Worker LLVMContext &Ctx = F->getContext();
740*9880d681SAndroid Build Coastguard Worker
741*9880d681SAndroid Build Coastguard Worker if (ExplicitUnroll && TripCount != 0) {
742*9880d681SAndroid Build Coastguard Worker // If the loop has an unrolling pragma, we want to be more aggressive with
743*9880d681SAndroid Build Coastguard Worker // unrolling limits. Set thresholds to at least the PragmaThreshold value
744*9880d681SAndroid Build Coastguard Worker // which is larger than the default limits.
745*9880d681SAndroid Build Coastguard Worker UP.Threshold = std::max<unsigned>(UP.Threshold, PragmaUnrollThreshold);
746*9880d681SAndroid Build Coastguard Worker UP.PartialThreshold =
747*9880d681SAndroid Build Coastguard Worker std::max<unsigned>(UP.PartialThreshold, PragmaUnrollThreshold);
748*9880d681SAndroid Build Coastguard Worker }
749*9880d681SAndroid Build Coastguard Worker
750*9880d681SAndroid Build Coastguard Worker // 3rd priority is full unroll count.
751*9880d681SAndroid Build Coastguard Worker // Full unroll make sense only when TripCount could be staticaly calculated.
752*9880d681SAndroid Build Coastguard Worker // Also we need to check if we exceed FullUnrollMaxCount.
753*9880d681SAndroid Build Coastguard Worker if (TripCount && TripCount <= UP.FullUnrollMaxCount) {
754*9880d681SAndroid Build Coastguard Worker // When computing the unrolled size, note that BEInsns are not replicated
755*9880d681SAndroid Build Coastguard Worker // like the rest of the loop body.
756*9880d681SAndroid Build Coastguard Worker UnrolledSize = (uint64_t)(LoopSize - BEInsns) * TripCount + BEInsns;
757*9880d681SAndroid Build Coastguard Worker if (canUnrollCompletely(L, UP.Threshold, 100, UP.DynamicCostSavingsDiscount,
758*9880d681SAndroid Build Coastguard Worker UnrolledSize, UnrolledSize)) {
759*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
760*9880d681SAndroid Build Coastguard Worker return ExplicitUnroll;
761*9880d681SAndroid Build Coastguard Worker } else {
762*9880d681SAndroid Build Coastguard Worker // The loop isn't that small, but we still can fully unroll it if that
763*9880d681SAndroid Build Coastguard Worker // helps to remove a significant number of instructions.
764*9880d681SAndroid Build Coastguard Worker // To check that, run additional analysis on the loop.
765*9880d681SAndroid Build Coastguard Worker if (Optional<EstimatedUnrollCost> Cost = analyzeLoopUnrollCost(
766*9880d681SAndroid Build Coastguard Worker L, TripCount, DT, *SE, TTI,
767*9880d681SAndroid Build Coastguard Worker UP.Threshold + UP.DynamicCostSavingsDiscount))
768*9880d681SAndroid Build Coastguard Worker if (canUnrollCompletely(L, UP.Threshold,
769*9880d681SAndroid Build Coastguard Worker UP.PercentDynamicCostSavedThreshold,
770*9880d681SAndroid Build Coastguard Worker UP.DynamicCostSavingsDiscount,
771*9880d681SAndroid Build Coastguard Worker Cost->UnrolledCost, Cost->RolledDynamicCost)) {
772*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
773*9880d681SAndroid Build Coastguard Worker return ExplicitUnroll;
774*9880d681SAndroid Build Coastguard Worker }
775*9880d681SAndroid Build Coastguard Worker }
776*9880d681SAndroid Build Coastguard Worker }
777*9880d681SAndroid Build Coastguard Worker
778*9880d681SAndroid Build Coastguard Worker // 4rd priority is partial unrolling.
779*9880d681SAndroid Build Coastguard Worker // Try partial unroll only when TripCount could be staticaly calculated.
780*9880d681SAndroid Build Coastguard Worker if (TripCount) {
781*9880d681SAndroid Build Coastguard Worker if (UP.Count == 0)
782*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
783*9880d681SAndroid Build Coastguard Worker UP.Partial |= ExplicitUnroll;
784*9880d681SAndroid Build Coastguard Worker if (!UP.Partial) {
785*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " will not try to unroll partially because "
786*9880d681SAndroid Build Coastguard Worker << "-unroll-allow-partial not given\n");
787*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
788*9880d681SAndroid Build Coastguard Worker return false;
789*9880d681SAndroid Build Coastguard Worker }
790*9880d681SAndroid Build Coastguard Worker if (UP.PartialThreshold != NoThreshold) {
791*9880d681SAndroid Build Coastguard Worker // Reduce unroll count to be modulo of TripCount for partial unrolling.
792*9880d681SAndroid Build Coastguard Worker UnrolledSize = (uint64_t)(LoopSize - BEInsns) * UP.Count + BEInsns;
793*9880d681SAndroid Build Coastguard Worker if (UnrolledSize > UP.PartialThreshold)
794*9880d681SAndroid Build Coastguard Worker UP.Count = (std::max(UP.PartialThreshold, 3u) - BEInsns) /
795*9880d681SAndroid Build Coastguard Worker (LoopSize - BEInsns);
796*9880d681SAndroid Build Coastguard Worker if (UP.Count > UP.MaxCount)
797*9880d681SAndroid Build Coastguard Worker UP.Count = UP.MaxCount;
798*9880d681SAndroid Build Coastguard Worker while (UP.Count != 0 && TripCount % UP.Count != 0)
799*9880d681SAndroid Build Coastguard Worker UP.Count--;
800*9880d681SAndroid Build Coastguard Worker if (UP.AllowRemainder && UP.Count <= 1) {
801*9880d681SAndroid Build Coastguard Worker // If there is no Count that is modulo of TripCount, set Count to
802*9880d681SAndroid Build Coastguard Worker // largest power-of-two factor that satisfies the threshold limit.
803*9880d681SAndroid Build Coastguard Worker // As we'll create fixup loop, do the type of unrolling only if
804*9880d681SAndroid Build Coastguard Worker // remainder loop is allowed.
805*9880d681SAndroid Build Coastguard Worker UP.Count = DefaultUnrollRuntimeCount;
806*9880d681SAndroid Build Coastguard Worker UnrolledSize = (LoopSize - BEInsns) * UP.Count + BEInsns;
807*9880d681SAndroid Build Coastguard Worker while (UP.Count != 0 && UnrolledSize > UP.PartialThreshold) {
808*9880d681SAndroid Build Coastguard Worker UP.Count >>= 1;
809*9880d681SAndroid Build Coastguard Worker UnrolledSize = (LoopSize - BEInsns) * UP.Count + BEInsns;
810*9880d681SAndroid Build Coastguard Worker }
811*9880d681SAndroid Build Coastguard Worker }
812*9880d681SAndroid Build Coastguard Worker if (UP.Count < 2) {
813*9880d681SAndroid Build Coastguard Worker if (PragmaEnableUnroll)
814*9880d681SAndroid Build Coastguard Worker emitOptimizationRemarkMissed(
815*9880d681SAndroid Build Coastguard Worker Ctx, DEBUG_TYPE, *F, LoopLoc,
816*9880d681SAndroid Build Coastguard Worker "Unable to unroll loop as directed by unroll(enable) pragma "
817*9880d681SAndroid Build Coastguard Worker "because unrolled size is too large.");
818*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
819*9880d681SAndroid Build Coastguard Worker }
820*9880d681SAndroid Build Coastguard Worker } else {
821*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
822*9880d681SAndroid Build Coastguard Worker }
823*9880d681SAndroid Build Coastguard Worker if ((PragmaFullUnroll || PragmaEnableUnroll) && TripCount &&
824*9880d681SAndroid Build Coastguard Worker UP.Count != TripCount)
825*9880d681SAndroid Build Coastguard Worker emitOptimizationRemarkMissed(
826*9880d681SAndroid Build Coastguard Worker Ctx, DEBUG_TYPE, *F, LoopLoc,
827*9880d681SAndroid Build Coastguard Worker "Unable to fully unroll loop as directed by unroll pragma because "
828*9880d681SAndroid Build Coastguard Worker "unrolled size is too large.");
829*9880d681SAndroid Build Coastguard Worker return ExplicitUnroll;
830*9880d681SAndroid Build Coastguard Worker }
831*9880d681SAndroid Build Coastguard Worker assert(TripCount == 0 &&
832*9880d681SAndroid Build Coastguard Worker "All cases when TripCount is constant should be covered here.");
833*9880d681SAndroid Build Coastguard Worker if (PragmaFullUnroll)
834*9880d681SAndroid Build Coastguard Worker emitOptimizationRemarkMissed(
835*9880d681SAndroid Build Coastguard Worker Ctx, DEBUG_TYPE, *F, LoopLoc,
836*9880d681SAndroid Build Coastguard Worker "Unable to fully unroll loop as directed by unroll(full) pragma "
837*9880d681SAndroid Build Coastguard Worker "because loop has a runtime trip count.");
838*9880d681SAndroid Build Coastguard Worker
839*9880d681SAndroid Build Coastguard Worker // 5th priority is runtime unrolling.
840*9880d681SAndroid Build Coastguard Worker // Don't unroll a runtime trip count loop when it is disabled.
841*9880d681SAndroid Build Coastguard Worker if (HasRuntimeUnrollDisablePragma(L)) {
842*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
843*9880d681SAndroid Build Coastguard Worker return false;
844*9880d681SAndroid Build Coastguard Worker }
845*9880d681SAndroid Build Coastguard Worker // Reduce count based on the type of unrolling and the threshold values.
846*9880d681SAndroid Build Coastguard Worker UP.Runtime |= PragmaEnableUnroll || PragmaCount > 0 || UserUnrollCount;
847*9880d681SAndroid Build Coastguard Worker if (!UP.Runtime) {
848*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " will not try to unroll loop with runtime trip count "
849*9880d681SAndroid Build Coastguard Worker << "-unroll-runtime not given\n");
850*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
851*9880d681SAndroid Build Coastguard Worker return false;
852*9880d681SAndroid Build Coastguard Worker }
853*9880d681SAndroid Build Coastguard Worker if (UP.Count == 0)
854*9880d681SAndroid Build Coastguard Worker UP.Count = DefaultUnrollRuntimeCount;
855*9880d681SAndroid Build Coastguard Worker UnrolledSize = (LoopSize - BEInsns) * UP.Count + BEInsns;
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard Worker // Reduce unroll count to be the largest power-of-two factor of
858*9880d681SAndroid Build Coastguard Worker // the original count which satisfies the threshold limit.
859*9880d681SAndroid Build Coastguard Worker while (UP.Count != 0 && UnrolledSize > UP.PartialThreshold) {
860*9880d681SAndroid Build Coastguard Worker UP.Count >>= 1;
861*9880d681SAndroid Build Coastguard Worker UnrolledSize = (LoopSize - BEInsns) * UP.Count + BEInsns;
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker
864*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
865*9880d681SAndroid Build Coastguard Worker unsigned OrigCount = UP.Count;
866*9880d681SAndroid Build Coastguard Worker #endif
867*9880d681SAndroid Build Coastguard Worker
868*9880d681SAndroid Build Coastguard Worker if (!UP.AllowRemainder && UP.Count != 0 && (TripMultiple % UP.Count) != 0) {
869*9880d681SAndroid Build Coastguard Worker while (UP.Count != 0 && TripMultiple % UP.Count != 0)
870*9880d681SAndroid Build Coastguard Worker UP.Count >>= 1;
871*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Remainder loop is restricted (that could architecture "
872*9880d681SAndroid Build Coastguard Worker "specific or because the loop contains a convergent "
873*9880d681SAndroid Build Coastguard Worker "instruction), so unroll count must divide the trip "
874*9880d681SAndroid Build Coastguard Worker "multiple, "
875*9880d681SAndroid Build Coastguard Worker << TripMultiple << ". Reducing unroll count from "
876*9880d681SAndroid Build Coastguard Worker << OrigCount << " to " << UP.Count << ".\n");
877*9880d681SAndroid Build Coastguard Worker if (PragmaCount > 0 && !UP.AllowRemainder)
878*9880d681SAndroid Build Coastguard Worker emitOptimizationRemarkMissed(
879*9880d681SAndroid Build Coastguard Worker Ctx, DEBUG_TYPE, *F, LoopLoc,
880*9880d681SAndroid Build Coastguard Worker Twine("Unable to unroll loop the number of times directed by "
881*9880d681SAndroid Build Coastguard Worker "unroll_count pragma because remainder loop is restricted "
882*9880d681SAndroid Build Coastguard Worker "(that could architecture specific or because the loop "
883*9880d681SAndroid Build Coastguard Worker "contains a convergent instruction) and so must have an unroll "
884*9880d681SAndroid Build Coastguard Worker "count that divides the loop trip multiple of ") +
885*9880d681SAndroid Build Coastguard Worker Twine(TripMultiple) + ". Unrolling instead " + Twine(UP.Count) +
886*9880d681SAndroid Build Coastguard Worker " time(s).");
887*9880d681SAndroid Build Coastguard Worker }
888*9880d681SAndroid Build Coastguard Worker
889*9880d681SAndroid Build Coastguard Worker if (UP.Count > UP.MaxCount)
890*9880d681SAndroid Build Coastguard Worker UP.Count = UP.MaxCount;
891*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " partially unrolling with count: " << UP.Count << "\n");
892*9880d681SAndroid Build Coastguard Worker if (UP.Count < 2)
893*9880d681SAndroid Build Coastguard Worker UP.Count = 0;
894*9880d681SAndroid Build Coastguard Worker return ExplicitUnroll;
895*9880d681SAndroid Build Coastguard Worker }
896*9880d681SAndroid Build Coastguard Worker
tryToUnrollLoop(Loop * L,DominatorTree & DT,LoopInfo * LI,ScalarEvolution * SE,const TargetTransformInfo & TTI,AssumptionCache & AC,bool PreserveLCSSA,Optional<unsigned> ProvidedCount,Optional<unsigned> ProvidedThreshold,Optional<bool> ProvidedAllowPartial,Optional<bool> ProvidedRuntime)897*9880d681SAndroid Build Coastguard Worker static bool tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
898*9880d681SAndroid Build Coastguard Worker ScalarEvolution *SE, const TargetTransformInfo &TTI,
899*9880d681SAndroid Build Coastguard Worker AssumptionCache &AC, bool PreserveLCSSA,
900*9880d681SAndroid Build Coastguard Worker Optional<unsigned> ProvidedCount,
901*9880d681SAndroid Build Coastguard Worker Optional<unsigned> ProvidedThreshold,
902*9880d681SAndroid Build Coastguard Worker Optional<bool> ProvidedAllowPartial,
903*9880d681SAndroid Build Coastguard Worker Optional<bool> ProvidedRuntime) {
904*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Loop Unroll: F[" << L->getHeader()->getParent()->getName()
905*9880d681SAndroid Build Coastguard Worker << "] Loop %" << L->getHeader()->getName() << "\n");
906*9880d681SAndroid Build Coastguard Worker if (HasUnrollDisablePragma(L)) {
907*9880d681SAndroid Build Coastguard Worker return false;
908*9880d681SAndroid Build Coastguard Worker }
909*9880d681SAndroid Build Coastguard Worker
910*9880d681SAndroid Build Coastguard Worker unsigned NumInlineCandidates;
911*9880d681SAndroid Build Coastguard Worker bool NotDuplicatable;
912*9880d681SAndroid Build Coastguard Worker bool Convergent;
913*9880d681SAndroid Build Coastguard Worker unsigned LoopSize = ApproximateLoopSize(
914*9880d681SAndroid Build Coastguard Worker L, NumInlineCandidates, NotDuplicatable, Convergent, TTI, &AC);
915*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
916*9880d681SAndroid Build Coastguard Worker if (NotDuplicatable) {
917*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable"
918*9880d681SAndroid Build Coastguard Worker << " instructions.\n");
919*9880d681SAndroid Build Coastguard Worker return false;
920*9880d681SAndroid Build Coastguard Worker }
921*9880d681SAndroid Build Coastguard Worker if (NumInlineCandidates != 0) {
922*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n");
923*9880d681SAndroid Build Coastguard Worker return false;
924*9880d681SAndroid Build Coastguard Worker }
925*9880d681SAndroid Build Coastguard Worker if (!L->isLoopSimplifyForm()) {
926*9880d681SAndroid Build Coastguard Worker DEBUG(
927*9880d681SAndroid Build Coastguard Worker dbgs() << " Not unrolling loop which is not in loop-simplify form.\n");
928*9880d681SAndroid Build Coastguard Worker return false;
929*9880d681SAndroid Build Coastguard Worker }
930*9880d681SAndroid Build Coastguard Worker
931*9880d681SAndroid Build Coastguard Worker // Find trip count and trip multiple if count is not available
932*9880d681SAndroid Build Coastguard Worker unsigned TripCount = 0;
933*9880d681SAndroid Build Coastguard Worker unsigned TripMultiple = 1;
934*9880d681SAndroid Build Coastguard Worker // If there are multiple exiting blocks but one of them is the latch, use the
935*9880d681SAndroid Build Coastguard Worker // latch for the trip count estimation. Otherwise insist on a single exiting
936*9880d681SAndroid Build Coastguard Worker // block for the trip count estimation.
937*9880d681SAndroid Build Coastguard Worker BasicBlock *ExitingBlock = L->getLoopLatch();
938*9880d681SAndroid Build Coastguard Worker if (!ExitingBlock || !L->isLoopExiting(ExitingBlock))
939*9880d681SAndroid Build Coastguard Worker ExitingBlock = L->getExitingBlock();
940*9880d681SAndroid Build Coastguard Worker if (ExitingBlock) {
941*9880d681SAndroid Build Coastguard Worker TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
942*9880d681SAndroid Build Coastguard Worker TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
943*9880d681SAndroid Build Coastguard Worker }
944*9880d681SAndroid Build Coastguard Worker
945*9880d681SAndroid Build Coastguard Worker TargetTransformInfo::UnrollingPreferences UP = gatherUnrollingPreferences(
946*9880d681SAndroid Build Coastguard Worker L, TTI, ProvidedThreshold, ProvidedCount, ProvidedAllowPartial,
947*9880d681SAndroid Build Coastguard Worker ProvidedRuntime);
948*9880d681SAndroid Build Coastguard Worker
949*9880d681SAndroid Build Coastguard Worker // If the loop contains a convergent operation, the prelude we'd add
950*9880d681SAndroid Build Coastguard Worker // to do the first few instructions before we hit the unrolled loop
951*9880d681SAndroid Build Coastguard Worker // is unsafe -- it adds a control-flow dependency to the convergent
952*9880d681SAndroid Build Coastguard Worker // operation. Therefore restrict remainder loop (try unrollig without).
953*9880d681SAndroid Build Coastguard Worker //
954*9880d681SAndroid Build Coastguard Worker // TODO: This is quite conservative. In practice, convergent_op()
955*9880d681SAndroid Build Coastguard Worker // is likely to be called unconditionally in the loop. In this
956*9880d681SAndroid Build Coastguard Worker // case, the program would be ill-formed (on most architectures)
957*9880d681SAndroid Build Coastguard Worker // unless n were the same on all threads in a thread group.
958*9880d681SAndroid Build Coastguard Worker // Assuming n is the same on all threads, any kind of unrolling is
959*9880d681SAndroid Build Coastguard Worker // safe. But currently llvm's notion of convergence isn't powerful
960*9880d681SAndroid Build Coastguard Worker // enough to express this.
961*9880d681SAndroid Build Coastguard Worker if (Convergent)
962*9880d681SAndroid Build Coastguard Worker UP.AllowRemainder = false;
963*9880d681SAndroid Build Coastguard Worker
964*9880d681SAndroid Build Coastguard Worker bool IsCountSetExplicitly = computeUnrollCount(L, TTI, DT, LI, SE, TripCount,
965*9880d681SAndroid Build Coastguard Worker TripMultiple, LoopSize, UP);
966*9880d681SAndroid Build Coastguard Worker if (!UP.Count)
967*9880d681SAndroid Build Coastguard Worker return false;
968*9880d681SAndroid Build Coastguard Worker // Unroll factor (Count) must be less or equal to TripCount.
969*9880d681SAndroid Build Coastguard Worker if (TripCount && UP.Count > TripCount)
970*9880d681SAndroid Build Coastguard Worker UP.Count = TripCount;
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard Worker // Unroll the loop.
973*9880d681SAndroid Build Coastguard Worker if (!UnrollLoop(L, UP.Count, TripCount, UP.Force, UP.Runtime,
974*9880d681SAndroid Build Coastguard Worker UP.AllowExpensiveTripCount, TripMultiple, LI, SE, &DT, &AC,
975*9880d681SAndroid Build Coastguard Worker PreserveLCSSA))
976*9880d681SAndroid Build Coastguard Worker return false;
977*9880d681SAndroid Build Coastguard Worker
978*9880d681SAndroid Build Coastguard Worker // If loop has an unroll count pragma or unrolled by explicitly set count
979*9880d681SAndroid Build Coastguard Worker // mark loop as unrolled to prevent unrolling beyond that requested.
980*9880d681SAndroid Build Coastguard Worker if (IsCountSetExplicitly)
981*9880d681SAndroid Build Coastguard Worker SetLoopAlreadyUnrolled(L);
982*9880d681SAndroid Build Coastguard Worker return true;
983*9880d681SAndroid Build Coastguard Worker }
984*9880d681SAndroid Build Coastguard Worker
985*9880d681SAndroid Build Coastguard Worker namespace {
986*9880d681SAndroid Build Coastguard Worker class LoopUnroll : public LoopPass {
987*9880d681SAndroid Build Coastguard Worker public:
988*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid
LoopUnroll(Optional<unsigned> Threshold=None,Optional<unsigned> Count=None,Optional<bool> AllowPartial=None,Optional<bool> Runtime=None)989*9880d681SAndroid Build Coastguard Worker LoopUnroll(Optional<unsigned> Threshold = None,
990*9880d681SAndroid Build Coastguard Worker Optional<unsigned> Count = None,
991*9880d681SAndroid Build Coastguard Worker Optional<bool> AllowPartial = None, Optional<bool> Runtime = None)
992*9880d681SAndroid Build Coastguard Worker : LoopPass(ID), ProvidedCount(std::move(Count)),
993*9880d681SAndroid Build Coastguard Worker ProvidedThreshold(Threshold), ProvidedAllowPartial(AllowPartial),
994*9880d681SAndroid Build Coastguard Worker ProvidedRuntime(Runtime) {
995*9880d681SAndroid Build Coastguard Worker initializeLoopUnrollPass(*PassRegistry::getPassRegistry());
996*9880d681SAndroid Build Coastguard Worker }
997*9880d681SAndroid Build Coastguard Worker
998*9880d681SAndroid Build Coastguard Worker Optional<unsigned> ProvidedCount;
999*9880d681SAndroid Build Coastguard Worker Optional<unsigned> ProvidedThreshold;
1000*9880d681SAndroid Build Coastguard Worker Optional<bool> ProvidedAllowPartial;
1001*9880d681SAndroid Build Coastguard Worker Optional<bool> ProvidedRuntime;
1002*9880d681SAndroid Build Coastguard Worker
runOnLoop(Loop * L,LPPassManager &)1003*9880d681SAndroid Build Coastguard Worker bool runOnLoop(Loop *L, LPPassManager &) override {
1004*9880d681SAndroid Build Coastguard Worker if (skipLoop(L))
1005*9880d681SAndroid Build Coastguard Worker return false;
1006*9880d681SAndroid Build Coastguard Worker
1007*9880d681SAndroid Build Coastguard Worker Function &F = *L->getHeader()->getParent();
1008*9880d681SAndroid Build Coastguard Worker
1009*9880d681SAndroid Build Coastguard Worker auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1010*9880d681SAndroid Build Coastguard Worker LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1011*9880d681SAndroid Build Coastguard Worker ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1012*9880d681SAndroid Build Coastguard Worker const TargetTransformInfo &TTI =
1013*9880d681SAndroid Build Coastguard Worker getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1014*9880d681SAndroid Build Coastguard Worker auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1015*9880d681SAndroid Build Coastguard Worker bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
1016*9880d681SAndroid Build Coastguard Worker
1017*9880d681SAndroid Build Coastguard Worker return tryToUnrollLoop(L, DT, LI, SE, TTI, AC, PreserveLCSSA, ProvidedCount,
1018*9880d681SAndroid Build Coastguard Worker ProvidedThreshold, ProvidedAllowPartial,
1019*9880d681SAndroid Build Coastguard Worker ProvidedRuntime);
1020*9880d681SAndroid Build Coastguard Worker }
1021*9880d681SAndroid Build Coastguard Worker
1022*9880d681SAndroid Build Coastguard Worker /// This transformation requires natural loop information & requires that
1023*9880d681SAndroid Build Coastguard Worker /// loop preheaders be inserted into the CFG...
1024*9880d681SAndroid Build Coastguard Worker ///
getAnalysisUsage(AnalysisUsage & AU) const1025*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
1026*9880d681SAndroid Build Coastguard Worker AU.addRequired<AssumptionCacheTracker>();
1027*9880d681SAndroid Build Coastguard Worker AU.addRequired<TargetTransformInfoWrapperPass>();
1028*9880d681SAndroid Build Coastguard Worker // FIXME: Loop passes are required to preserve domtree, and for now we just
1029*9880d681SAndroid Build Coastguard Worker // recreate dom info if anything gets unrolled.
1030*9880d681SAndroid Build Coastguard Worker getLoopAnalysisUsage(AU);
1031*9880d681SAndroid Build Coastguard Worker }
1032*9880d681SAndroid Build Coastguard Worker };
1033*9880d681SAndroid Build Coastguard Worker }
1034*9880d681SAndroid Build Coastguard Worker
1035*9880d681SAndroid Build Coastguard Worker char LoopUnroll::ID = 0;
1036*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)1037*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1038*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(LoopPass)
1039*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
1040*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
1041*9880d681SAndroid Build Coastguard Worker
1042*9880d681SAndroid Build Coastguard Worker Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial,
1043*9880d681SAndroid Build Coastguard Worker int Runtime) {
1044*9880d681SAndroid Build Coastguard Worker // TODO: It would make more sense for this function to take the optionals
1045*9880d681SAndroid Build Coastguard Worker // directly, but that's dangerous since it would silently break out of tree
1046*9880d681SAndroid Build Coastguard Worker // callers.
1047*9880d681SAndroid Build Coastguard Worker return new LoopUnroll(Threshold == -1 ? None : Optional<unsigned>(Threshold),
1048*9880d681SAndroid Build Coastguard Worker Count == -1 ? None : Optional<unsigned>(Count),
1049*9880d681SAndroid Build Coastguard Worker AllowPartial == -1 ? None
1050*9880d681SAndroid Build Coastguard Worker : Optional<bool>(AllowPartial),
1051*9880d681SAndroid Build Coastguard Worker Runtime == -1 ? None : Optional<bool>(Runtime));
1052*9880d681SAndroid Build Coastguard Worker }
1053*9880d681SAndroid Build Coastguard Worker
createSimpleLoopUnrollPass()1054*9880d681SAndroid Build Coastguard Worker Pass *llvm::createSimpleLoopUnrollPass() {
1055*9880d681SAndroid Build Coastguard Worker return llvm::createLoopUnrollPass(-1, -1, 0, 0);
1056*9880d681SAndroid Build Coastguard Worker }
1057