blob: 0af5a71c126d680ded5d4d1b74f26d6d202818a7 [file] [log] [blame]
Chris Lattner946b2552004-04-18 05:20:17 +00001//===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner946b2552004-04-18 05:20:17 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner946b2552004-04-18 05:20:17 +00008//===----------------------------------------------------------------------===//
9//
10// This pass implements a simple loop unroller. It works best when loops have
11// been canonicalized by the -indvars pass, allowing it to determine the trip
12// counts of loops easily.
Chris Lattner946b2552004-04-18 05:20:17 +000013//===----------------------------------------------------------------------===//
14
Chris Lattner946b2552004-04-18 05:20:17 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattner679572e2011-01-02 07:35:53 +000016#include "llvm/Analysis/CodeMetrics.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Analysis/LoopPass.h"
Dan Gohman0141c132010-07-26 18:11:16 +000018#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruthbb9caa92013-01-21 13:04:33 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Eli Benderskyff903242014-06-16 23:53:02 +000021#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/IntrinsicInst.h"
Eli Benderskyff903242014-06-16 23:53:02 +000024#include "llvm/IR/Metadata.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000027#include "llvm/Support/raw_ostream.h"
Dan Gohman3dc2d922008-05-14 00:24:14 +000028#include "llvm/Transforms/Utils/UnrollLoop.h"
Duncan Sands67933e62008-05-16 09:30:00 +000029#include <climits>
Chris Lattner946b2552004-04-18 05:20:17 +000030
Dan Gohman3dc2d922008-05-14 00:24:14 +000031using namespace llvm;
Chris Lattner946b2552004-04-18 05:20:17 +000032
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "loop-unroll"
34
Dan Gohmand78c4002008-05-13 00:00:25 +000035static cl::opt<unsigned>
Owen Andersond85c9cc2010-09-10 17:57:00 +000036UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden,
Dan Gohmand78c4002008-05-13 00:00:25 +000037 cl::desc("The cut-off point for automatic loop unrolling"));
38
39static cl::opt<unsigned>
40UnrollCount("unroll-count", cl::init(0), cl::Hidden,
Eli Benderskyff903242014-06-16 23:53:02 +000041 cl::desc("Use this unroll count for all loops including those with "
42 "unroll_count pragma values, for testing purposes"));
Dan Gohmand78c4002008-05-13 00:00:25 +000043
Matthijs Kooijman98b5c162008-07-29 13:21:23 +000044static cl::opt<bool>
45UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden,
46 cl::desc("Allows loops to be partially unrolled until "
47 "-unroll-threshold loop size is reached."));
48
Andrew Trickd04d15292011-12-09 06:19:40 +000049static cl::opt<bool>
50UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden,
51 cl::desc("Unroll loops with run-time trip counts"));
52
Eli Benderskyff903242014-06-16 23:53:02 +000053static cl::opt<unsigned>
54PragmaUnrollThreshold("pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden,
55 cl::desc("Unrolled size limit for loops with an unroll(enable) or "
56 "unroll_count pragma."));
57
Chris Lattner79a42ac2006-12-19 21:40:18 +000058namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000059 class LoopUnroll : public LoopPass {
Chris Lattner946b2552004-04-18 05:20:17 +000060 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000061 static char ID; // Pass ID, replacement for typeid
Hal Finkel081eaef2013-11-05 00:08:03 +000062 LoopUnroll(int T = -1, int C = -1, int P = -1, int R = -1) : LoopPass(ID) {
Chris Lattner35a65b22011-04-14 02:27:25 +000063 CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T);
64 CurrentCount = (C == -1) ? UnrollCount : unsigned(C);
Junjie Gu7c3b4592011-04-13 16:15:29 +000065 CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P;
Hal Finkel081eaef2013-11-05 00:08:03 +000066 CurrentRuntime = (R == -1) ? UnrollRuntime : (bool)R;
Junjie Gu7c3b4592011-04-13 16:15:29 +000067
68 UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0);
Hal Finkel8f2e7002013-09-11 19:25:43 +000069 UserAllowPartial = (P != -1) ||
70 (UnrollAllowPartial.getNumOccurrences() > 0);
Hal Finkel081eaef2013-11-05 00:08:03 +000071 UserRuntime = (R != -1) || (UnrollRuntime.getNumOccurrences() > 0);
Hal Finkel8f2e7002013-09-11 19:25:43 +000072 UserCount = (C != -1) || (UnrollCount.getNumOccurrences() > 0);
Andrew Trick279e7a62011-07-23 00:29:16 +000073
Owen Anderson6c18d1a2010-10-19 17:21:58 +000074 initializeLoopUnrollPass(*PassRegistry::getPassRegistry());
75 }
Devang Patel09f162c2007-05-01 21:15:47 +000076
Dan Gohman2980d9d2007-05-11 20:53:41 +000077 /// A magic value for use with the Threshold parameter to indicate
78 /// that the loop unroll should be performed regardless of how much
79 /// code expansion would result.
80 static const unsigned NoThreshold = UINT_MAX;
Andrew Trick279e7a62011-07-23 00:29:16 +000081
Owen Andersona4d9c782010-09-07 23:15:30 +000082 // Threshold to use when optsize is specified (and there is no
83 // explicit -unroll-threshold).
84 static const unsigned OptSizeUnrollThreshold = 50;
Andrew Trick279e7a62011-07-23 00:29:16 +000085
Andrew Trickd04d15292011-12-09 06:19:40 +000086 // Default unroll count for loops with run-time trip count if
87 // -unroll-count is not set
88 static const unsigned UnrollRuntimeCount = 8;
89
Junjie Gu7c3b4592011-04-13 16:15:29 +000090 unsigned CurrentCount;
Owen Andersona4d9c782010-09-07 23:15:30 +000091 unsigned CurrentThreshold;
Junjie Gu7c3b4592011-04-13 16:15:29 +000092 bool CurrentAllowPartial;
Hal Finkel081eaef2013-11-05 00:08:03 +000093 bool CurrentRuntime;
Hal Finkel8f2e7002013-09-11 19:25:43 +000094 bool UserCount; // CurrentCount is user-specified.
Junjie Gu7c3b4592011-04-13 16:15:29 +000095 bool UserThreshold; // CurrentThreshold is user-specified.
Hal Finkel8f2e7002013-09-11 19:25:43 +000096 bool UserAllowPartial; // CurrentAllowPartial is user-specified.
Hal Finkel081eaef2013-11-05 00:08:03 +000097 bool UserRuntime; // CurrentRuntime is user-specified.
Dan Gohman2980d9d2007-05-11 20:53:41 +000098
Craig Topper3e4c6972014-03-05 09:10:37 +000099 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Chris Lattner946b2552004-04-18 05:20:17 +0000100
101 /// This transformation requires natural loop information & requires that
102 /// loop preheaders be inserted into the CFG...
103 ///
Craig Topper3e4c6972014-03-05 09:10:37 +0000104 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner946b2552004-04-18 05:20:17 +0000105 AU.addRequired<LoopInfo>();
Chris Lattnerf2cc8412004-04-18 05:38:37 +0000106 AU.addPreserved<LoopInfo>();
Dan Gohman0141c132010-07-26 18:11:16 +0000107 AU.addRequiredID(LoopSimplifyID);
108 AU.addPreservedID(LoopSimplifyID);
109 AU.addRequiredID(LCSSAID);
110 AU.addPreservedID(LCSSAID);
Andrew Trick4d0040b2011-08-10 04:29:49 +0000111 AU.addRequired<ScalarEvolution>();
Chris Lattnerd6f46b82010-08-29 17:21:35 +0000112 AU.addPreserved<ScalarEvolution>();
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000113 AU.addRequired<TargetTransformInfo>();
Devang Patelf94b9822008-07-03 07:04:22 +0000114 // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info.
115 // If loop unroll does not preserve dom info then LCSSA pass on next
116 // loop will receive invalid dom info.
117 // For now, recreate dom info, if loop is unrolled.
Chandler Carruth73523022014-01-13 13:07:17 +0000118 AU.addPreserved<DominatorTreeWrapperPass>();
Chris Lattner946b2552004-04-18 05:20:17 +0000119 }
Eli Benderskyff903242014-06-16 23:53:02 +0000120
121 // Fill in the UnrollingPreferences parameter with values from the
122 // TargetTransformationInfo.
123 void getUnrollingPreferences(Loop *L, const TargetTransformInfo &TTI,
124 TargetTransformInfo::UnrollingPreferences &UP) {
125 UP.Threshold = CurrentThreshold;
126 UP.OptSizeThreshold = OptSizeUnrollThreshold;
127 UP.PartialThreshold = CurrentThreshold;
128 UP.PartialOptSizeThreshold = OptSizeUnrollThreshold;
129 UP.Count = CurrentCount;
130 UP.MaxCount = UINT_MAX;
131 UP.Partial = CurrentAllowPartial;
132 UP.Runtime = CurrentRuntime;
133 TTI.getUnrollingPreferences(L, UP);
134 }
135
136 // Select and return an unroll count based on parameters from
137 // user, unroll preferences, unroll pragmas, or a heuristic.
138 // SetExplicitly is set to true if the unroll count is is set by
139 // the user or a pragma rather than selected heuristically.
140 unsigned
141 selectUnrollCount(const Loop *L, unsigned TripCount, bool HasEnablePragma,
142 unsigned PragmaCount,
143 const TargetTransformInfo::UnrollingPreferences &UP,
144 bool &SetExplicitly);
145
146
147 // Select threshold values used to limit unrolling based on a
148 // total unrolled size. Parameters Threshold and PartialThreshold
149 // are set to the maximum unrolled size for fully and partially
150 // unrolled loops respectively.
151 void selectThresholds(const Loop *L, bool HasPragma,
152 const TargetTransformInfo::UnrollingPreferences &UP,
153 unsigned &Threshold, unsigned &PartialThreshold) {
154 // Determine the current unrolling threshold. While this is
155 // normally set from UnrollThreshold, it is overridden to a
156 // smaller value if the current function is marked as
157 // optimize-for-size, and the unroll threshold was not user
158 // specified.
159 Threshold = UserThreshold ? CurrentThreshold : UP.Threshold;
160 PartialThreshold = UserThreshold ? CurrentThreshold : UP.PartialThreshold;
161 if (!UserThreshold &&
162 L->getHeader()->getParent()->getAttributes().
163 hasAttribute(AttributeSet::FunctionIndex,
164 Attribute::OptimizeForSize)) {
165 Threshold = UP.OptSizeThreshold;
166 PartialThreshold = UP.PartialOptSizeThreshold;
167 }
168 if (HasPragma) {
169 // If the loop has an unrolling pragma, we want to be more
170 // aggressive with unrolling limits. Set thresholds to at
171 // least the PragmaTheshold value which is larger than the
172 // default limits.
173 if (Threshold != NoThreshold)
174 Threshold = std::max<unsigned>(Threshold, PragmaUnrollThreshold);
175 if (PartialThreshold != NoThreshold)
176 PartialThreshold =
177 std::max<unsigned>(PartialThreshold, PragmaUnrollThreshold);
178 }
179 }
Chris Lattner946b2552004-04-18 05:20:17 +0000180 };
Chris Lattner946b2552004-04-18 05:20:17 +0000181}
182
Dan Gohmand78c4002008-05-13 00:00:25 +0000183char LoopUnroll::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000184INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000185INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000186INITIALIZE_PASS_DEPENDENCY(LoopInfo)
187INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
188INITIALIZE_PASS_DEPENDENCY(LCSSA)
Devang Patel88b4fa22011-10-19 23:56:07 +0000189INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000190INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000191
Hal Finkel081eaef2013-11-05 00:08:03 +0000192Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial,
193 int Runtime) {
194 return new LoopUnroll(Threshold, Count, AllowPartial, Runtime);
Junjie Gu7c3b4592011-04-13 16:15:29 +0000195}
Chris Lattner946b2552004-04-18 05:20:17 +0000196
Hal Finkel86b30642014-03-31 23:23:51 +0000197Pass *llvm::createSimpleLoopUnrollPass() {
198 return llvm::createLoopUnrollPass(-1, -1, 0, 0);
199}
200
Dan Gohman49d08a52007-05-08 15:14:19 +0000201/// ApproximateLoopSize - Approximate the size of the loop.
Andrew Trickf7656012011-10-01 01:39:05 +0000202static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000203 bool &NotDuplicatable,
204 const TargetTransformInfo &TTI) {
Dan Gohman969e83a2009-10-31 14:54:17 +0000205 CodeMetrics Metrics;
Dan Gohman90071072008-06-22 20:18:58 +0000206 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
Dan Gohman969e83a2009-10-31 14:54:17 +0000207 I != E; ++I)
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000208 Metrics.analyzeBasicBlock(*I, TTI);
Owen Anderson04cf3fd2010-09-09 20:32:23 +0000209 NumCalls = Metrics.NumInlineCandidates;
James Molloy4f6fb952012-12-20 16:04:27 +0000210 NotDuplicatable = Metrics.notDuplicatable;
Andrew Trick279e7a62011-07-23 00:29:16 +0000211
Owen Anderson62ea1b72010-09-09 19:07:31 +0000212 unsigned LoopSize = Metrics.NumInsts;
Andrew Trick279e7a62011-07-23 00:29:16 +0000213
Owen Anderson62ea1b72010-09-09 19:07:31 +0000214 // Don't allow an estimate of size zero. This would allows unrolling of loops
215 // with huge iteration counts, which is a compile time problem even if it's
216 // not a problem for code quality.
217 if (LoopSize == 0) LoopSize = 1;
Andrew Trick279e7a62011-07-23 00:29:16 +0000218
Owen Anderson62ea1b72010-09-09 19:07:31 +0000219 return LoopSize;
Chris Lattner946b2552004-04-18 05:20:17 +0000220}
221
Eli Benderskyff903242014-06-16 23:53:02 +0000222// Returns the value associated with the given metadata node name (for
223// example, "llvm.loopunroll.count"). If no such named metadata node
224// exists, then nullptr is returned.
225static const ConstantInt *GetUnrollMetadataValue(const Loop *L,
226 StringRef Name) {
227 MDNode *LoopID = L->getLoopID();
228 if (!LoopID) return nullptr;
229
230 // First operand should refer to the loop id itself.
231 assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
232 assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
233
234 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
235 const MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
236 if (!MD) continue;
237
238 const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
239 if (!S) continue;
240
241 if (Name.equals(S->getString())) {
242 assert(MD->getNumOperands() == 2 &&
243 "Unroll hint metadata should have two operands.");
244 return cast<ConstantInt>(MD->getOperand(1));
245 }
246 }
247 return nullptr;
248}
249
250// Returns true if the loop has an unroll(enable) pragma.
251static bool HasUnrollEnablePragma(const Loop *L) {
252 const ConstantInt *EnableValue =
253 GetUnrollMetadataValue(L, "llvm.loopunroll.enable");
254 return (EnableValue && EnableValue->getZExtValue());
255 return false;
256}
257
258// Returns true if the loop has an unroll(disable) pragma.
259static bool HasUnrollDisablePragma(const Loop *L) {
260 const ConstantInt *EnableValue =
261 GetUnrollMetadataValue(L, "llvm.loopunroll.enable");
262 return (EnableValue && !EnableValue->getZExtValue());
263 return false;
264}
265
266// If loop has an unroll_count pragma return the (necessarily
267// positive) value from the pragma. Otherwise return 0.
268static unsigned UnrollCountPragmaValue(const Loop *L) {
269 const ConstantInt *CountValue =
270 GetUnrollMetadataValue(L, "llvm.loopunroll.count");
271 if (CountValue) {
272 unsigned Count = CountValue->getZExtValue();
273 assert(Count >= 1 && "Unroll count must be positive.");
274 return Count;
275 }
276 return 0;
277}
278
279unsigned LoopUnroll::selectUnrollCount(
280 const Loop *L, unsigned TripCount, bool HasEnablePragma,
281 unsigned PragmaCount, const TargetTransformInfo::UnrollingPreferences &UP,
282 bool &SetExplicitly) {
283 SetExplicitly = true;
284
285 // User-specified count (either as a command-line option or
286 // constructor parameter) has highest precedence.
287 unsigned Count = UserCount ? CurrentCount : 0;
288
289 // If there is no user-specified count, unroll pragmas have the next
290 // highest precendence.
291 if (Count == 0) {
292 if (PragmaCount) {
293 Count = PragmaCount;
294 } else if (HasEnablePragma) {
295 // unroll(enable) pragma without an unroll_count pragma
296 // indicates to unroll loop fully.
297 Count = TripCount;
298 }
299 }
300
301 if (Count == 0)
302 Count = UP.Count;
303
304 if (Count == 0) {
305 SetExplicitly = false;
306 if (TripCount == 0)
307 // Runtime trip count.
308 Count = UnrollRuntimeCount;
309 else
310 // Conservative heuristic: if we know the trip count, see if we can
311 // completely unroll (subject to the threshold, checked below); otherwise
312 // try to find greatest modulo of the trip count which is still under
313 // threshold value.
314 Count = TripCount;
315 }
316 if (TripCount && Count > TripCount)
317 return TripCount;
318 return Count;
319}
320
Devang Patel9779e562007-03-07 01:38:05 +0000321bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000322 if (skipOptnoneFunction(L))
323 return false;
324
Dan Gohman3dc2d922008-05-14 00:24:14 +0000325 LoopInfo *LI = &getAnalysis<LoopInfo>();
Andrew Trick2b6860f2011-08-11 23:36:16 +0000326 ScalarEvolution *SE = &getAnalysis<ScalarEvolution>();
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000327 const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
Dan Gohman2980d9d2007-05-11 20:53:41 +0000328
Dan Gohman2e1f8042007-05-08 15:19:19 +0000329 BasicBlock *Header = L->getHeader();
David Greenee0b97892010-01-05 01:27:44 +0000330 DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName()
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000331 << "] Loop %" << Header->getName() << "\n");
Eli Benderskyff903242014-06-16 23:53:02 +0000332
333 if (HasUnrollDisablePragma(L)) {
334 return false;
335 }
336 bool HasEnablePragma = HasUnrollEnablePragma(L);
337 unsigned PragmaCount = UnrollCountPragmaValue(L);
338 bool HasPragma = HasEnablePragma || PragmaCount > 0;
Andrew Trick279e7a62011-07-23 00:29:16 +0000339
Hal Finkel8f2e7002013-09-11 19:25:43 +0000340 TargetTransformInfo::UnrollingPreferences UP;
Eli Benderskyff903242014-06-16 23:53:02 +0000341 getUnrollingPreferences(L, TTI, UP);
Dan Gohman2980d9d2007-05-11 20:53:41 +0000342
Andrew Trick2b6860f2011-08-11 23:36:16 +0000343 // Find trip count and trip multiple if count is not available
344 unsigned TripCount = 0;
Andrew Trick1cabe542011-07-23 00:33:05 +0000345 unsigned TripMultiple = 1;
Andrew Tricka8bdb7c2011-11-28 19:22:09 +0000346 // Find "latch trip count". UnrollLoop assumes that control cannot exit
347 // via the loop latch on any iteration prior to TripCount. The loop may exit
348 // early via an earlier branch.
349 BasicBlock *LatchBlock = L->getLoopLatch();
350 if (LatchBlock) {
351 TripCount = SE->getSmallConstantTripCount(L, LatchBlock);
352 TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock);
Andrew Trick2b6860f2011-08-11 23:36:16 +0000353 }
Hal Finkel8f2e7002013-09-11 19:25:43 +0000354
Eli Benderskyff903242014-06-16 23:53:02 +0000355 // Select an initial unroll count. This may be reduced later based
356 // on size thresholds.
357 bool CountSetExplicitly;
358 unsigned Count = selectUnrollCount(L, TripCount, HasEnablePragma, PragmaCount,
359 UP, CountSetExplicitly);
Eli Benderskydc6de2c2014-06-12 18:05:39 +0000360
Eli Benderskyff903242014-06-16 23:53:02 +0000361 unsigned NumInlineCandidates;
362 bool notDuplicatable;
363 unsigned LoopSize =
364 ApproximateLoopSize(L, NumInlineCandidates, notDuplicatable, TTI);
365 DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
366 uint64_t UnrolledSize = (uint64_t)LoopSize * Count;
367 if (notDuplicatable) {
368 DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable"
369 << " instructions.\n");
370 return false;
371 }
372 if (NumInlineCandidates != 0) {
373 DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n");
374 return false;
Dan Gohman2980d9d2007-05-11 20:53:41 +0000375 }
376
Eli Benderskyff903242014-06-16 23:53:02 +0000377 unsigned Threshold, PartialThreshold;
378 selectThresholds(L, HasPragma, UP, Threshold, PartialThreshold);
Benjamin Kramer9130cb82014-05-04 19:12:38 +0000379
Eli Benderskyff903242014-06-16 23:53:02 +0000380 // Given Count, TripCount and thresholds determine the type of
381 // unrolling which is to be performed.
382 enum { Full = 0, Partial = 1, Runtime = 2 };
383 int Unrolling;
384 if (TripCount && Count == TripCount) {
385 if (Threshold != NoThreshold && UnrolledSize > Threshold) {
386 DEBUG(dbgs() << " Too large to fully unroll with count: " << Count
387 << " because size: " << UnrolledSize << ">" << Threshold
388 << "\n");
389 Unrolling = Partial;
390 } else {
391 Unrolling = Full;
Dan Gohman2980d9d2007-05-11 20:53:41 +0000392 }
Eli Benderskyff903242014-06-16 23:53:02 +0000393 } else if (TripCount && Count < TripCount) {
394 Unrolling = Partial;
395 } else {
396 Unrolling = Runtime;
397 }
398
399 // Reduce count based on the type of unrolling and the threshold values.
400 unsigned OriginalCount = Count;
401 bool AllowRuntime = UserRuntime ? CurrentRuntime : UP.Runtime;
402 if (Unrolling == Partial) {
403 bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial;
404 if (!AllowPartial && !CountSetExplicitly) {
405 DEBUG(dbgs() << " will not try to unroll partially because "
406 << "-unroll-allow-partial not given\n");
407 return false;
408 }
409 if (PartialThreshold != NoThreshold && UnrolledSize > PartialThreshold) {
410 // Reduce unroll count to be modulo of TripCount for partial unrolling.
411 Count = PartialThreshold / LoopSize;
412 while (Count != 0 && TripCount % Count != 0)
413 Count--;
414 }
415 } else if (Unrolling == Runtime) {
416 if (!AllowRuntime && !CountSetExplicitly) {
417 DEBUG(dbgs() << " will not try to unroll loop with runtime trip count "
418 << "-unroll-runtime not given\n");
419 return false;
420 }
421 // Reduce unroll count to be the largest power-of-two factor of
422 // the original count which satisfies the threshold limit.
423 while (Count != 0 && UnrolledSize > PartialThreshold) {
424 Count >>= 1;
425 UnrolledSize = LoopSize * Count;
426 }
427 if (Count > UP.MaxCount)
428 Count = UP.MaxCount;
429 DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n");
430 }
431
432 if (HasPragma) {
433 // Emit optimization remarks if we are unable to unroll the loop
434 // as directed by a pragma.
435 DebugLoc LoopLoc = L->getStartLoc();
436 Function *F = Header->getParent();
437 LLVMContext &Ctx = F->getContext();
438 if (HasEnablePragma && PragmaCount == 0) {
439 if (TripCount && Count != TripCount) {
440 emitOptimizationRemarkMissed(
441 Ctx, DEBUG_TYPE, *F, LoopLoc,
442 "Unable to fully unroll loop as directed by unroll(enable) pragma "
443 "because unrolled size is too large.");
444 } else if (!TripCount) {
445 emitOptimizationRemarkMissed(
446 Ctx, DEBUG_TYPE, *F, LoopLoc,
447 "Unable to fully unroll loop as directed by unroll(enable) pragma "
448 "because loop has a runtime trip count.");
449 }
450 } else if (PragmaCount > 0 && Count != OriginalCount) {
451 emitOptimizationRemarkMissed(
452 Ctx, DEBUG_TYPE, *F, LoopLoc,
453 "Unable to unroll loop the number of times directed by "
454 "unroll_count pragma because unrolled size is too large.");
455 }
456 }
457
458 if (Unrolling != Full && Count < 2) {
459 // Partial unrolling by 1 is a nop. For full unrolling, a factor
460 // of 1 makes sense because loop control can be eliminated.
461 return false;
Dan Gohman2980d9d2007-05-11 20:53:41 +0000462 }
463
Dan Gohman3dc2d922008-05-14 00:24:14 +0000464 // Unroll the loop.
Eli Benderskyff903242014-06-16 23:53:02 +0000465 if (!UnrollLoop(L, Count, TripCount, AllowRuntime, TripMultiple, LI, this, &LPM))
Dan Gohman3dc2d922008-05-14 00:24:14 +0000466 return false;
Dan Gohman2980d9d2007-05-11 20:53:41 +0000467
Chris Lattner946b2552004-04-18 05:20:17 +0000468 return true;
469}