Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 1 | //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 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 Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "loop-unroll" |
| 16 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | d9e0797 | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CodeMetrics.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 052f000 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/IntrinsicInst.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 1a2d667 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 27 | #include <climits> |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 28 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 30 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 31 | static cl::opt<unsigned> |
Owen Anderson | 8ea298a | 2010-09-10 17:57:00 +0000 | [diff] [blame] | 32 | UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 33 | cl::desc("The cut-off point for automatic loop unrolling")); |
| 34 | |
| 35 | static cl::opt<unsigned> |
| 36 | UnrollCount("unroll-count", cl::init(0), cl::Hidden, |
| 37 | cl::desc("Use this unroll count for all loops, for testing purposes")); |
| 38 | |
Matthijs Kooijman | 75cf9cc | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, |
| 41 | cl::desc("Allows loops to be partially unrolled until " |
| 42 | "-unroll-threshold loop size is reached.")); |
| 43 | |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
| 45 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, |
| 46 | cl::desc("Unroll loops with run-time trip counts")); |
| 47 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 48 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 49 | class LoopUnroll : public LoopPass { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 50 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 51 | static char ID; // Pass ID, replacement for typeid |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 52 | LoopUnroll(int T = -1, int C = -1, int P = -1, int R = -1) : LoopPass(ID) { |
Chris Lattner | b3b1571 | 2011-04-14 02:27:25 +0000 | [diff] [blame] | 53 | CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T); |
| 54 | CurrentCount = (C == -1) ? UnrollCount : unsigned(C); |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 55 | CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P; |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 56 | CurrentRuntime = (R == -1) ? UnrollRuntime : (bool)R; |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 57 | |
| 58 | UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0); |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 59 | UserAllowPartial = (P != -1) || |
| 60 | (UnrollAllowPartial.getNumOccurrences() > 0); |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 61 | UserRuntime = (R != -1) || (UnrollRuntime.getNumOccurrences() > 0); |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 62 | UserCount = (C != -1) || (UnrollCount.getNumOccurrences() > 0); |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 63 | |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 64 | initializeLoopUnrollPass(*PassRegistry::getPassRegistry()); |
| 65 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 66 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 67 | /// A magic value for use with the Threshold parameter to indicate |
| 68 | /// that the loop unroll should be performed regardless of how much |
| 69 | /// code expansion would result. |
| 70 | static const unsigned NoThreshold = UINT_MAX; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 71 | |
Owen Anderson | e9f1882 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 72 | // Threshold to use when optsize is specified (and there is no |
| 73 | // explicit -unroll-threshold). |
| 74 | static const unsigned OptSizeUnrollThreshold = 50; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 75 | |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 76 | // Default unroll count for loops with run-time trip count if |
| 77 | // -unroll-count is not set |
| 78 | static const unsigned UnrollRuntimeCount = 8; |
| 79 | |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 80 | unsigned CurrentCount; |
Owen Anderson | e9f1882 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 81 | unsigned CurrentThreshold; |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 82 | bool CurrentAllowPartial; |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 83 | bool CurrentRuntime; |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 84 | bool UserCount; // CurrentCount is user-specified. |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 85 | bool UserThreshold; // CurrentThreshold is user-specified. |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 86 | bool UserAllowPartial; // CurrentAllowPartial is user-specified. |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 87 | bool UserRuntime; // CurrentRuntime is user-specified. |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 88 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 89 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 90 | |
| 91 | /// This transformation requires natural loop information & requires that |
| 92 | /// loop preheaders be inserted into the CFG... |
| 93 | /// |
| 94 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 95 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 96 | AU.addPreserved<LoopInfo>(); |
Dan Gohman | 052f000 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 97 | AU.addRequiredID(LoopSimplifyID); |
| 98 | AU.addPreservedID(LoopSimplifyID); |
| 99 | AU.addRequiredID(LCSSAID); |
| 100 | AU.addPreservedID(LCSSAID); |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 101 | AU.addRequired<ScalarEvolution>(); |
Chris Lattner | 9377901 | 2010-08-29 17:21:35 +0000 | [diff] [blame] | 102 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 103 | AU.addRequired<TargetTransformInfo>(); |
Devang Patel | 98260a4 | 2008-07-03 07:04:22 +0000 | [diff] [blame] | 104 | // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. |
| 105 | // If loop unroll does not preserve dom info then LCSSA pass on next |
| 106 | // loop will receive invalid dom info. |
| 107 | // For now, recreate dom info, if loop is unrolled. |
| 108 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 109 | } |
| 110 | }; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 113 | char LoopUnroll::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 114 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 115 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 116 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 117 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 118 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Devang Patel | 9be72d4 | 2011-10-19 23:56:07 +0000 | [diff] [blame] | 119 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 120 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 121 | |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 122 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 123 | int Runtime) { |
| 124 | return new LoopUnroll(Threshold, Count, AllowPartial, Runtime); |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 126 | |
Dan Gohman | f742c23 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 127 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | b2ab2fa | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 128 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 129 | bool &NotDuplicatable, |
| 130 | const TargetTransformInfo &TTI) { |
Dan Gohman | a2aabe3 | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 131 | CodeMetrics Metrics; |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 132 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Dan Gohman | a2aabe3 | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 133 | I != E; ++I) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 134 | Metrics.analyzeBasicBlock(*I, TTI); |
Owen Anderson | f9a26b8 | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 135 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 136 | NotDuplicatable = Metrics.notDuplicatable; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 137 | |
Owen Anderson | 6add2fb | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 138 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 139 | |
Owen Anderson | 6add2fb | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 140 | // Don't allow an estimate of size zero. This would allows unrolling of loops |
| 141 | // with huge iteration counts, which is a compile time problem even if it's |
| 142 | // not a problem for code quality. |
| 143 | if (LoopSize == 0) LoopSize = 1; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 144 | |
Owen Anderson | 6add2fb | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 145 | return LoopSize; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 148 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 149 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Andrew Trick | b1831c6 | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 150 | ScalarEvolution *SE = &getAnalysis<ScalarEvolution>(); |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 151 | const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>(); |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 152 | |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 153 | BasicBlock *Header = L->getHeader(); |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 154 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 155 | << "] Loop %" << Header->getName() << "\n"); |
Mike Stump | 02efa78 | 2009-07-27 23:14:11 +0000 | [diff] [blame] | 156 | (void)Header; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 157 | |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 158 | TargetTransformInfo::UnrollingPreferences UP; |
| 159 | UP.Threshold = CurrentThreshold; |
| 160 | UP.OptSizeThreshold = OptSizeUnrollThreshold; |
| 161 | UP.Count = CurrentCount; |
| 162 | UP.Partial = CurrentAllowPartial; |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 163 | UP.Runtime = CurrentRuntime; |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 164 | TTI.getUnrollingPreferences(L, UP); |
| 165 | |
Owen Anderson | e9f1882 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 166 | // Determine the current unrolling threshold. While this is normally set |
| 167 | // from UnrollThreshold, it is overridden to a smaller value if the current |
| 168 | // function is marked as optimize-for-size, and the unroll threshold was |
| 169 | // not user specified. |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 170 | unsigned Threshold = UserThreshold ? CurrentThreshold : UP.Threshold; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 171 | if (!UserThreshold && |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 172 | Header->getParent()->getAttributes(). |
| 173 | hasAttribute(AttributeSet::FunctionIndex, |
| 174 | Attribute::OptimizeForSize)) |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 175 | Threshold = UP.OptSizeThreshold; |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 176 | |
Andrew Trick | b1831c6 | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 177 | // Find trip count and trip multiple if count is not available |
| 178 | unsigned TripCount = 0; |
Andrew Trick | 2045ce1 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 179 | unsigned TripMultiple = 1; |
Andrew Trick | ed38f1c | 2011-11-28 19:22:09 +0000 | [diff] [blame] | 180 | // Find "latch trip count". UnrollLoop assumes that control cannot exit |
| 181 | // via the loop latch on any iteration prior to TripCount. The loop may exit |
| 182 | // early via an earlier branch. |
| 183 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 184 | if (LatchBlock) { |
| 185 | TripCount = SE->getSmallConstantTripCount(L, LatchBlock); |
| 186 | TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock); |
Andrew Trick | b1831c6 | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 187 | } |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 188 | |
Hal Finkel | c88eb08 | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 189 | bool Runtime = UserRuntime ? CurrentRuntime : UP.Runtime; |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 190 | |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 191 | // Use a default unroll-count if the user doesn't specify a value |
| 192 | // and the trip count is a run-time value. The default is different |
| 193 | // for run-time or compile-time trip count loops. |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 194 | unsigned Count = UserCount ? CurrentCount : UP.Count; |
| 195 | if (Runtime && Count == 0 && TripCount == 0) |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 196 | Count = UnrollRuntimeCount; |
| 197 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 198 | if (Count == 0) { |
| 199 | // Conservative heuristic: if we know the trip count, see if we can |
| 200 | // completely unroll (subject to the threshold, checked below); otherwise |
Andreas Bolka | f1f2dc2 | 2009-08-13 02:40:50 +0000 | [diff] [blame] | 201 | // try to find greatest modulo of the trip count which is still under |
Matthijs Kooijman | 75cf9cc | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 202 | // threshold value. |
Andreas Bolka | ec40eb4 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 203 | if (TripCount == 0) |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 204 | return false; |
Andreas Bolka | ec40eb4 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 205 | Count = TripCount; |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 208 | // Enforce the threshold. |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 209 | if (Threshold != NoThreshold) { |
Owen Anderson | f9a26b8 | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 210 | unsigned NumInlineCandidates; |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 211 | bool notDuplicatable; |
| 212 | unsigned LoopSize = ApproximateLoopSize(L, NumInlineCandidates, |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 213 | notDuplicatable, TTI); |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 214 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 215 | if (notDuplicatable) { |
| 216 | DEBUG(dbgs() << " Not unrolling loop which contains non duplicatable" |
| 217 | << " instructions.\n"); |
| 218 | return false; |
| 219 | } |
Owen Anderson | f9a26b8 | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 220 | if (NumInlineCandidates != 0) { |
| 221 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
Owen Anderson | 547462b | 2010-09-09 20:02:23 +0000 | [diff] [blame] | 222 | return false; |
Jakob Stoklund Olesen | d450e5b | 2010-02-05 23:21:31 +0000 | [diff] [blame] | 223 | } |
Owen Anderson | 69daca5 | 2010-09-29 18:05:19 +0000 | [diff] [blame] | 224 | uint64_t Size = (uint64_t)LoopSize*Count; |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 225 | if (TripCount != 1 && Size > Threshold) { |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 226 | DEBUG(dbgs() << " Too large to fully unroll with count: " << Count |
Junjie Gu | 32644d9 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 227 | << " because size: " << Size << ">" << Threshold << "\n"); |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 228 | bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial; |
| 229 | if (!AllowPartial && !(Runtime && TripCount == 0)) { |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 230 | DEBUG(dbgs() << " will not try to unroll partially because " |
Andreas Bolka | 52e539c | 2009-08-13 02:45:03 +0000 | [diff] [blame] | 231 | << "-unroll-allow-partial not given\n"); |
Matthijs Kooijman | 75cf9cc | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 234 | if (TripCount) { |
| 235 | // Reduce unroll count to be modulo of TripCount for partial unrolling |
Hongbin Zheng | 00b73a5 | 2012-04-04 11:44:08 +0000 | [diff] [blame] | 236 | Count = Threshold / LoopSize; |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 237 | while (Count != 0 && TripCount%Count != 0) |
| 238 | Count--; |
| 239 | } |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 240 | else if (Runtime) { |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 241 | // Reduce unroll count to be a lower power-of-two value |
Hongbin Zheng | 00b73a5 | 2012-04-04 11:44:08 +0000 | [diff] [blame] | 242 | while (Count != 0 && Size > Threshold) { |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 243 | Count >>= 1; |
| 244 | Size = LoopSize*Count; |
| 245 | } |
Andreas Bolka | ec40eb4 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 246 | } |
| 247 | if (Count < 2) { |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 248 | DEBUG(dbgs() << " could not unroll partially\n"); |
Andreas Bolka | ec40eb4 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
David Greene | d241e38 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 251 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 255 | // Unroll the loop. |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 256 | if (!UnrollLoop(L, Count, TripCount, Runtime, TripMultiple, LI, &LPM)) |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 257 | return false; |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 259 | return true; |
| 260 | } |