Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 1 | //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 946b255 | 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 | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/CodeMetrics.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IntrinsicInst.h" |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 67933e6 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 28 | #include <climits> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 29 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "loop-unroll" |
| 33 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 34 | static cl::opt<unsigned> |
Owen Anderson | d85c9cc | 2010-09-10 17:57:00 +0000 | [diff] [blame] | 35 | UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 36 | cl::desc("The cut-off point for automatic loop unrolling")); |
| 37 | |
| 38 | static cl::opt<unsigned> |
| 39 | UnrollCount("unroll-count", cl::init(0), cl::Hidden, |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 40 | cl::desc("Use this unroll count for all loops including those with " |
| 41 | "unroll_count pragma values, for testing purposes")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, |
| 45 | cl::desc("Allows loops to be partially unrolled until " |
| 46 | "-unroll-threshold loop size is reached.")); |
| 47 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
| 49 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, |
| 50 | cl::desc("Unroll loops with run-time trip counts")); |
| 51 | |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 52 | // Maximum allowed unroll count for a loop being fully unrolled |
| 53 | // because of a pragma unroll(enable) statement (ie, metadata |
| 54 | // "llvm.loopunroll.enable" is true). This prevents unexpected |
| 55 | // behavior like crashing when using this pragma on high trip count |
| 56 | // loops. |
| 57 | static const unsigned PragmaFullUnrollCountLimit = 1024; |
| 58 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 59 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 60 | class LoopUnroll : public LoopPass { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 61 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 62 | static char ID; // Pass ID, replacement for typeid |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 63 | LoopUnroll(int T = -1, int C = -1, int P = -1, int R = -1) : LoopPass(ID) { |
Chris Lattner | 35a65b2 | 2011-04-14 02:27:25 +0000 | [diff] [blame] | 64 | CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T); |
| 65 | CurrentCount = (C == -1) ? UnrollCount : unsigned(C); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 66 | CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 67 | CurrentRuntime = (R == -1) ? UnrollRuntime : (bool)R; |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 68 | |
| 69 | UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 70 | UserAllowPartial = (P != -1) || |
| 71 | (UnrollAllowPartial.getNumOccurrences() > 0); |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 72 | UserRuntime = (R != -1) || (UnrollRuntime.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 73 | UserCount = (C != -1) || (UnrollCount.getNumOccurrences() > 0); |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 74 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 75 | initializeLoopUnrollPass(*PassRegistry::getPassRegistry()); |
| 76 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 77 | |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 78 | /// A magic value for use with the Threshold parameter to indicate |
| 79 | /// that the loop unroll should be performed regardless of how much |
| 80 | /// code expansion would result. |
| 81 | static const unsigned NoThreshold = UINT_MAX; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 82 | |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 83 | // Threshold to use when optsize is specified (and there is no |
| 84 | // explicit -unroll-threshold). |
| 85 | static const unsigned OptSizeUnrollThreshold = 50; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 86 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 87 | // Default unroll count for loops with run-time trip count if |
| 88 | // -unroll-count is not set |
| 89 | static const unsigned UnrollRuntimeCount = 8; |
| 90 | |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 91 | unsigned CurrentCount; |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 92 | unsigned CurrentThreshold; |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 93 | bool CurrentAllowPartial; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 94 | bool CurrentRuntime; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 95 | bool UserCount; // CurrentCount is user-specified. |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 96 | bool UserThreshold; // CurrentThreshold is user-specified. |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 97 | bool UserAllowPartial; // CurrentAllowPartial is user-specified. |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 98 | bool UserRuntime; // CurrentRuntime is user-specified. |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 99 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 100 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 101 | |
| 102 | /// This transformation requires natural loop information & requires that |
| 103 | /// loop preheaders be inserted into the CFG... |
| 104 | /// |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 105 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 106 | AU.addRequired<LoopInfo>(); |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 107 | AU.addPreserved<LoopInfo>(); |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 108 | AU.addRequiredID(LoopSimplifyID); |
| 109 | AU.addPreservedID(LoopSimplifyID); |
| 110 | AU.addRequiredID(LCSSAID); |
| 111 | AU.addPreservedID(LCSSAID); |
Andrew Trick | 4d0040b | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 112 | AU.addRequired<ScalarEvolution>(); |
Chris Lattner | d6f46b8 | 2010-08-29 17:21:35 +0000 | [diff] [blame] | 113 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 114 | AU.addRequired<TargetTransformInfo>(); |
Devang Patel | f94b982 | 2008-07-03 07:04:22 +0000 | [diff] [blame] | 115 | // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. |
| 116 | // If loop unroll does not preserve dom info then LCSSA pass on next |
| 117 | // loop will receive invalid dom info. |
| 118 | // For now, recreate dom info, if loop is unrolled. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 119 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 120 | } |
| 121 | }; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 124 | char LoopUnroll::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 125 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 126 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 128 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 129 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Devang Patel | 88b4fa2 | 2011-10-19 23:56:07 +0000 | [diff] [blame] | 130 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 131 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 132 | |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 133 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 134 | int Runtime) { |
| 135 | return new LoopUnroll(Threshold, Count, AllowPartial, Runtime); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 137 | |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 138 | Pass *llvm::createSimpleLoopUnrollPass() { |
| 139 | return llvm::createLoopUnrollPass(-1, -1, 0, 0); |
| 140 | } |
| 141 | |
Dan Gohman | 49d08a5 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 142 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | f765601 | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 143 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 144 | bool &NotDuplicatable, |
| 145 | const TargetTransformInfo &TTI) { |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 146 | CodeMetrics Metrics; |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 147 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 148 | I != E; ++I) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 149 | Metrics.analyzeBasicBlock(*I, TTI); |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 150 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 151 | NotDuplicatable = Metrics.notDuplicatable; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 152 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 153 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 154 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 155 | // Don't allow an estimate of size zero. This would allows unrolling of loops |
| 156 | // with huge iteration counts, which is a compile time problem even if it's |
| 157 | // not a problem for code quality. |
| 158 | if (LoopSize == 0) LoopSize = 1; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 159 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 160 | return LoopSize; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 163 | // Returns the value associated with the given metadata node name (for |
| 164 | // example, "llvm.loopunroll.count"). If no such named metadata node |
| 165 | // exists, then nullptr is returned. |
| 166 | static const ConstantInt *GetUnrollMetadataValue(const Loop *L, |
| 167 | StringRef Name) { |
| 168 | MDNode *LoopID = L->getLoopID(); |
| 169 | if (!LoopID) return nullptr; |
| 170 | |
| 171 | // First operand should refer to the loop id itself. |
| 172 | assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); |
| 173 | assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); |
| 174 | |
| 175 | for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { |
| 176 | const MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 177 | if (!MD) continue; |
| 178 | |
| 179 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 180 | if (!S) continue; |
| 181 | |
| 182 | if (Name.equals(S->getString())) { |
| 183 | assert(MD->getNumOperands() == 2 && |
| 184 | "Unroll hint metadata should have two operands."); |
| 185 | return cast<ConstantInt>(MD->getOperand(1)); |
| 186 | } |
| 187 | } |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | // Returns true if the loop has an unroll(enable) pragma. |
| 192 | static bool HasUnrollEnablePragma(const Loop *L) { |
| 193 | const ConstantInt *EnableValue = |
| 194 | GetUnrollMetadataValue(L, "llvm.loopunroll.enable"); |
| 195 | return (EnableValue && EnableValue->getZExtValue()); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | // Returns true if the loop has an unroll(disable) pragma. |
| 200 | static bool HasUnrollDisablePragma(const Loop *L) { |
| 201 | const ConstantInt *EnableValue = |
| 202 | GetUnrollMetadataValue(L, "llvm.loopunroll.enable"); |
| 203 | return (EnableValue && !EnableValue->getZExtValue()); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // Check for unroll_count(N) pragma. If found, return true and set |
| 208 | // Count to the integer parameter of the pragma. |
| 209 | static bool HasUnrollCountPragma(const Loop *L, int &Count) { |
| 210 | const ConstantInt *CountValue = |
| 211 | GetUnrollMetadataValue(L, "llvm.loopunroll.count"); |
| 212 | if (CountValue) { |
| 213 | Count = CountValue->getZExtValue(); |
| 214 | assert(Count >= 1 && "Unroll count must be positive."); |
| 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
Devang Patel | 9779e56 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 220 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 221 | if (skipOptnoneFunction(L)) |
| 222 | return false; |
| 223 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 224 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 225 | ScalarEvolution *SE = &getAnalysis<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 226 | const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>(); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 227 | |
Dan Gohman | 2e1f804 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 228 | BasicBlock *Header = L->getHeader(); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 229 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 230 | << "] Loop %" << Header->getName() << "\n"); |
Mike Stump | d934cc0 | 2009-07-27 23:14:11 +0000 | [diff] [blame] | 231 | (void)Header; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 232 | |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 233 | TargetTransformInfo::UnrollingPreferences UP; |
| 234 | UP.Threshold = CurrentThreshold; |
| 235 | UP.OptSizeThreshold = OptSizeUnrollThreshold; |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 236 | UP.PartialThreshold = CurrentThreshold; |
| 237 | UP.PartialOptSizeThreshold = OptSizeUnrollThreshold; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 238 | UP.Count = CurrentCount; |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 239 | UP.MaxCount = UINT_MAX; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 240 | UP.Partial = CurrentAllowPartial; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 241 | UP.Runtime = CurrentRuntime; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 242 | TTI.getUnrollingPreferences(L, UP); |
| 243 | |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 244 | // Determine the current unrolling threshold. While this is normally set |
| 245 | // from UnrollThreshold, it is overridden to a smaller value if the current |
| 246 | // function is marked as optimize-for-size, and the unroll threshold was |
| 247 | // not user specified. |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 248 | unsigned Threshold = UserThreshold ? CurrentThreshold : UP.Threshold; |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 249 | unsigned PartialThreshold = |
| 250 | UserThreshold ? CurrentThreshold : UP.PartialThreshold; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 251 | if (!UserThreshold && |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 252 | Header->getParent()->getAttributes(). |
| 253 | hasAttribute(AttributeSet::FunctionIndex, |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 254 | Attribute::OptimizeForSize)) { |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 255 | Threshold = UP.OptSizeThreshold; |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 256 | PartialThreshold = UP.PartialOptSizeThreshold; |
| 257 | } |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 258 | |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 259 | // Find trip count and trip multiple if count is not available |
| 260 | unsigned TripCount = 0; |
Andrew Trick | 1cabe54 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 261 | unsigned TripMultiple = 1; |
Andrew Trick | a8bdb7c | 2011-11-28 19:22:09 +0000 | [diff] [blame] | 262 | // Find "latch trip count". UnrollLoop assumes that control cannot exit |
| 263 | // via the loop latch on any iteration prior to TripCount. The loop may exit |
| 264 | // early via an earlier branch. |
| 265 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 266 | if (LatchBlock) { |
| 267 | TripCount = SE->getSmallConstantTripCount(L, LatchBlock); |
| 268 | TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 269 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 270 | |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 271 | // User-specified count (either as a command-line option or |
| 272 | // constructor parameter) has highest precedence. |
| 273 | unsigned Count = UserCount ? CurrentCount : 0; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 274 | |
Eli Bendersky | 899bef0 | 2014-06-11 23:15:35 +0000 | [diff] [blame] | 275 | // If there is no user-specified count, unroll pragmas have the next |
| 276 | // highest precendence. |
| 277 | if (Count == 0) { |
| 278 | if (HasUnrollDisablePragma(L)) { |
| 279 | // Loop has unroll(disable) pragma. |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | int PragmaCount; |
| 284 | if (HasUnrollCountPragma(L, PragmaCount)) { |
| 285 | if (PragmaCount == 1) { |
| 286 | // Nothing to do. |
| 287 | return false; |
| 288 | } |
| 289 | Count = PragmaCount; |
| 290 | Threshold = NoThreshold; |
| 291 | } else if (HasUnrollEnablePragma(L)) { |
| 292 | // Loop has unroll(enable) pragma without a unroll_count pragma, |
| 293 | // so unroll loop fully if possible. |
| 294 | if (TripCount == 0) { |
| 295 | DEBUG(dbgs() << " Loop has unroll(enable) pragma but loop cannot be " |
| 296 | "fully unrolled because trip count is unknown.\n"); |
| 297 | // Continue with standard heuristic unrolling. |
| 298 | } else if (TripCount > PragmaFullUnrollCountLimit) { |
| 299 | DEBUG(dbgs() << " Loop has unroll(enable) pragma but loop cannot be " |
| 300 | "fully unrolled because loop count is greater than " |
| 301 | << PragmaFullUnrollCountLimit); |
| 302 | // Continue with standard heuristic unrolling. |
| 303 | } else { |
| 304 | Count = TripCount; |
| 305 | Threshold = NoThreshold; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (Count == 0) |
| 311 | Count = UP.Count; |
| 312 | |
| 313 | bool Runtime = UserRuntime ? CurrentRuntime : UP.Runtime; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 314 | if (Runtime && Count == 0 && TripCount == 0) |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 315 | Count = UnrollRuntimeCount; |
| 316 | |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 317 | if (Count == 0) { |
| 318 | // Conservative heuristic: if we know the trip count, see if we can |
| 319 | // completely unroll (subject to the threshold, checked below); otherwise |
Andreas Bolka | 177a2f5 | 2009-08-13 02:40:50 +0000 | [diff] [blame] | 320 | // try to find greatest modulo of the trip count which is still under |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 321 | // threshold value. |
Andreas Bolka | aef4325 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 322 | if (TripCount == 0) |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 323 | return false; |
Andreas Bolka | aef4325 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 324 | Count = TripCount; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 327 | // Enforce the threshold. |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 328 | if (Threshold != NoThreshold && PartialThreshold != NoThreshold) { |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 329 | unsigned NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 330 | bool notDuplicatable; |
| 331 | unsigned LoopSize = ApproximateLoopSize(L, NumInlineCandidates, |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 332 | notDuplicatable, TTI); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 333 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 334 | if (notDuplicatable) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 335 | DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable" |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 336 | << " instructions.\n"); |
| 337 | return false; |
| 338 | } |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 339 | if (NumInlineCandidates != 0) { |
| 340 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
Owen Anderson | 6270515 | 2010-09-09 20:02:23 +0000 | [diff] [blame] | 341 | return false; |
Jakob Stoklund Olesen | 5f9ead2 | 2010-02-05 23:21:31 +0000 | [diff] [blame] | 342 | } |
Owen Anderson | d67ca0e | 2010-09-29 18:05:19 +0000 | [diff] [blame] | 343 | uint64_t Size = (uint64_t)LoopSize*Count; |
Benjamin Kramer | 9130cb8 | 2014-05-04 19:12:38 +0000 | [diff] [blame] | 344 | if (TripCount != 1 && |
| 345 | (Size > Threshold || (Count != TripCount && Size > PartialThreshold))) { |
| 346 | if (Size > Threshold) |
| 347 | DEBUG(dbgs() << " Too large to fully unroll with count: " << Count |
| 348 | << " because size: " << Size << ">" << Threshold << "\n"); |
| 349 | |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 350 | bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial; |
| 351 | if (!AllowPartial && !(Runtime && TripCount == 0)) { |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 352 | DEBUG(dbgs() << " will not try to unroll partially because " |
Andreas Bolka | 438ba80 | 2009-08-13 02:45:03 +0000 | [diff] [blame] | 353 | << "-unroll-allow-partial not given\n"); |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 354 | return false; |
| 355 | } |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 356 | if (TripCount) { |
| 357 | // Reduce unroll count to be modulo of TripCount for partial unrolling |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 358 | Count = PartialThreshold / LoopSize; |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 359 | while (Count != 0 && TripCount%Count != 0) |
| 360 | Count--; |
| 361 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 362 | else if (Runtime) { |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 363 | // Reduce unroll count to be a lower power-of-two value |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 364 | while (Count != 0 && Size > PartialThreshold) { |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 365 | Count >>= 1; |
| 366 | Size = LoopSize*Count; |
| 367 | } |
Andreas Bolka | aef4325 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 368 | } |
Hal Finkel | 6386cb8 | 2014-04-01 18:50:30 +0000 | [diff] [blame] | 369 | if (Count > UP.MaxCount) |
| 370 | Count = UP.MaxCount; |
Andreas Bolka | aef4325 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 371 | if (Count < 2) { |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 372 | DEBUG(dbgs() << " could not unroll partially\n"); |
Andreas Bolka | aef4325 | 2009-08-13 03:00:57 +0000 | [diff] [blame] | 373 | return false; |
| 374 | } |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 375 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 379 | // Unroll the loop. |
Chandler Carruth | aa7fa5e | 2014-01-23 11:23:19 +0000 | [diff] [blame] | 380 | if (!UnrollLoop(L, Count, TripCount, Runtime, TripMultiple, LI, this, &LPM)) |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 381 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 383 | return true; |
| 384 | } |