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" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IntrinsicInst.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 67933e6 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 29 | #include <climits> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 30 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "loop-unroll" |
| 34 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 35 | static cl::opt<unsigned> |
Owen Anderson | d85c9cc | 2010-09-10 17:57:00 +0000 | [diff] [blame] | 36 | UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 37 | cl::desc("The cut-off point for automatic loop unrolling")); |
| 38 | |
| 39 | static cl::opt<unsigned> |
| 40 | UnrollCount("unroll-count", cl::init(0), cl::Hidden, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 41 | cl::desc("Use this unroll count for all loops including those with " |
| 42 | "unroll_count pragma values, for testing purposes")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 43 | |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
| 45 | UnrollAllowPartial("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 Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
| 50 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, |
| 51 | cl::desc("Unroll loops with run-time trip counts")); |
| 52 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 53 | static cl::opt<unsigned> |
| 54 | PragmaUnrollThreshold("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 Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 58 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 59 | class LoopUnroll : public LoopPass { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 60 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 61 | static char ID; // Pass ID, replacement for typeid |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 62 | 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] | 63 | CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T); |
| 64 | CurrentCount = (C == -1) ? UnrollCount : unsigned(C); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 65 | CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 66 | CurrentRuntime = (R == -1) ? UnrollRuntime : (bool)R; |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 67 | |
| 68 | UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 69 | UserAllowPartial = (P != -1) || |
| 70 | (UnrollAllowPartial.getNumOccurrences() > 0); |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 71 | UserRuntime = (R != -1) || (UnrollRuntime.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 72 | UserCount = (C != -1) || (UnrollCount.getNumOccurrences() > 0); |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 73 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 74 | initializeLoopUnrollPass(*PassRegistry::getPassRegistry()); |
| 75 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 76 | |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 77 | /// 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 Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 81 | |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 82 | // Threshold to use when optsize is specified (and there is no |
| 83 | // explicit -unroll-threshold). |
| 84 | static const unsigned OptSizeUnrollThreshold = 50; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 85 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 86 | // 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 Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 90 | unsigned CurrentCount; |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 91 | unsigned CurrentThreshold; |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 92 | bool CurrentAllowPartial; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 93 | bool CurrentRuntime; |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 94 | bool UserCount; // CurrentCount is user-specified. |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 95 | bool UserThreshold; // CurrentThreshold is user-specified. |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 96 | bool UserAllowPartial; // CurrentAllowPartial is user-specified. |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 97 | bool UserRuntime; // CurrentRuntime is user-specified. |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 98 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 99 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 100 | |
| 101 | /// This transformation requires natural loop information & requires that |
| 102 | /// loop preheaders be inserted into the CFG... |
| 103 | /// |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 104 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 105 | AU.addRequired<LoopInfo>(); |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 106 | AU.addPreserved<LoopInfo>(); |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 107 | AU.addRequiredID(LoopSimplifyID); |
| 108 | AU.addPreservedID(LoopSimplifyID); |
| 109 | AU.addRequiredID(LCSSAID); |
| 110 | AU.addPreservedID(LCSSAID); |
Andrew Trick | 4d0040b | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 111 | AU.addRequired<ScalarEvolution>(); |
Chris Lattner | d6f46b8 | 2010-08-29 17:21:35 +0000 | [diff] [blame] | 112 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 113 | AU.addRequired<TargetTransformInfo>(); |
Devang Patel | f94b982 | 2008-07-03 07:04:22 +0000 | [diff] [blame] | 114 | // 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 Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 118 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 119 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 120 | |
| 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 Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 180 | }; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 183 | char LoopUnroll::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 184 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 185 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 186 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 187 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 188 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Devang Patel | 88b4fa2 | 2011-10-19 23:56:07 +0000 | [diff] [blame] | 189 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 191 | |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 192 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 193 | int Runtime) { |
| 194 | return new LoopUnroll(Threshold, Count, AllowPartial, Runtime); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 196 | |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 197 | Pass *llvm::createSimpleLoopUnrollPass() { |
| 198 | return llvm::createLoopUnrollPass(-1, -1, 0, 0); |
| 199 | } |
| 200 | |
Dan Gohman | 49d08a5 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 201 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | f765601 | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 202 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 203 | bool &NotDuplicatable, |
| 204 | const TargetTransformInfo &TTI) { |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 205 | CodeMetrics Metrics; |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 206 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 207 | I != E; ++I) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 208 | Metrics.analyzeBasicBlock(*I, TTI); |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 209 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 210 | NotDuplicatable = Metrics.notDuplicatable; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 211 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 212 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 213 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 214 | // 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 Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 218 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 219 | return LoopSize; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 222 | // Returns the value associated with the given metadata node name (for |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 223 | // example, "llvm.loop.unroll.count"). If no such named metadata node |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 224 | // exists, then nullptr is returned. |
| 225 | static 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. |
| 251 | static bool HasUnrollEnablePragma(const Loop *L) { |
| 252 | const ConstantInt *EnableValue = |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 253 | GetUnrollMetadataValue(L, "llvm.loop.unroll.enable"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 254 | return (EnableValue && EnableValue->getZExtValue()); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Returns true if the loop has an unroll(disable) pragma. |
| 258 | static bool HasUnrollDisablePragma(const Loop *L) { |
| 259 | const ConstantInt *EnableValue = |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 260 | GetUnrollMetadataValue(L, "llvm.loop.unroll.enable"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 261 | return (EnableValue && !EnableValue->getZExtValue()); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // If loop has an unroll_count pragma return the (necessarily |
| 265 | // positive) value from the pragma. Otherwise return 0. |
| 266 | static unsigned UnrollCountPragmaValue(const Loop *L) { |
| 267 | const ConstantInt *CountValue = |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 268 | GetUnrollMetadataValue(L, "llvm.loop.unroll.count"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 269 | if (CountValue) { |
| 270 | unsigned Count = CountValue->getZExtValue(); |
| 271 | assert(Count >= 1 && "Unroll count must be positive."); |
| 272 | return Count; |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 277 | // Remove existing unroll metadata and add unroll disable metadata to |
| 278 | // indicate the loop has already been unrolled. This prevents a loop |
| 279 | // from being unrolled more than is directed by a pragma if the loop |
| 280 | // unrolling pass is run more than once (which it generally is). |
| 281 | static void SetLoopAlreadyUnrolled(Loop *L) { |
| 282 | MDNode *LoopID = L->getLoopID(); |
| 283 | if (!LoopID) return; |
| 284 | |
| 285 | // First remove any existing loop unrolling metadata. |
| 286 | SmallVector<Value *, 4> Vals; |
| 287 | // Reserve first location for self reference to the LoopID metadata node. |
| 288 | Vals.push_back(nullptr); |
| 289 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 290 | bool IsUnrollMetadata = false; |
| 291 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 292 | if (MD) { |
| 293 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 294 | IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); |
| 295 | } |
| 296 | if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i)); |
| 297 | } |
| 298 | |
| 299 | // Add unroll(disable) metadata to disable future unrolling. |
| 300 | LLVMContext &Context = L->getHeader()->getContext(); |
Mark Heffernan | f3764da | 2014-07-18 21:29:41 +0000 | [diff] [blame^] | 301 | SmallVector<Value *, 2> DisableOperands; |
| 302 | DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.enable")); |
| 303 | DisableOperands.push_back(ConstantInt::get(Type::getInt1Ty(Context), 0)); |
| 304 | MDNode *DisableNode = MDNode::get(Context, DisableOperands); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 305 | Vals.push_back(DisableNode); |
| 306 | |
| 307 | MDNode *NewLoopID = MDNode::get(Context, Vals); |
| 308 | // Set operand 0 to refer to the loop id itself. |
| 309 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 310 | L->setLoopID(NewLoopID); |
| 311 | LoopID->replaceAllUsesWith(NewLoopID); |
| 312 | } |
| 313 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 314 | unsigned LoopUnroll::selectUnrollCount( |
| 315 | const Loop *L, unsigned TripCount, bool HasEnablePragma, |
| 316 | unsigned PragmaCount, const TargetTransformInfo::UnrollingPreferences &UP, |
| 317 | bool &SetExplicitly) { |
| 318 | SetExplicitly = true; |
| 319 | |
| 320 | // User-specified count (either as a command-line option or |
| 321 | // constructor parameter) has highest precedence. |
| 322 | unsigned Count = UserCount ? CurrentCount : 0; |
| 323 | |
| 324 | // If there is no user-specified count, unroll pragmas have the next |
| 325 | // highest precendence. |
| 326 | if (Count == 0) { |
| 327 | if (PragmaCount) { |
| 328 | Count = PragmaCount; |
| 329 | } else if (HasEnablePragma) { |
| 330 | // unroll(enable) pragma without an unroll_count pragma |
| 331 | // indicates to unroll loop fully. |
| 332 | Count = TripCount; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (Count == 0) |
| 337 | Count = UP.Count; |
| 338 | |
| 339 | if (Count == 0) { |
| 340 | SetExplicitly = false; |
| 341 | if (TripCount == 0) |
| 342 | // Runtime trip count. |
| 343 | Count = UnrollRuntimeCount; |
| 344 | else |
| 345 | // Conservative heuristic: if we know the trip count, see if we can |
| 346 | // completely unroll (subject to the threshold, checked below); otherwise |
| 347 | // try to find greatest modulo of the trip count which is still under |
| 348 | // threshold value. |
| 349 | Count = TripCount; |
| 350 | } |
| 351 | if (TripCount && Count > TripCount) |
| 352 | return TripCount; |
| 353 | return Count; |
| 354 | } |
| 355 | |
Devang Patel | 9779e56 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 356 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 357 | if (skipOptnoneFunction(L)) |
| 358 | return false; |
| 359 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 360 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 361 | ScalarEvolution *SE = &getAnalysis<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 362 | const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>(); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 363 | |
Dan Gohman | 2e1f804 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 364 | BasicBlock *Header = L->getHeader(); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 365 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 366 | << "] Loop %" << Header->getName() << "\n"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 367 | |
| 368 | if (HasUnrollDisablePragma(L)) { |
| 369 | return false; |
| 370 | } |
| 371 | bool HasEnablePragma = HasUnrollEnablePragma(L); |
| 372 | unsigned PragmaCount = UnrollCountPragmaValue(L); |
| 373 | bool HasPragma = HasEnablePragma || PragmaCount > 0; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 374 | |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 375 | TargetTransformInfo::UnrollingPreferences UP; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 376 | getUnrollingPreferences(L, TTI, UP); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 377 | |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 378 | // Find trip count and trip multiple if count is not available |
| 379 | unsigned TripCount = 0; |
Andrew Trick | 1cabe54 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 380 | unsigned TripMultiple = 1; |
Andrew Trick | a8bdb7c | 2011-11-28 19:22:09 +0000 | [diff] [blame] | 381 | // Find "latch trip count". UnrollLoop assumes that control cannot exit |
| 382 | // via the loop latch on any iteration prior to TripCount. The loop may exit |
| 383 | // early via an earlier branch. |
| 384 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 385 | if (LatchBlock) { |
| 386 | TripCount = SE->getSmallConstantTripCount(L, LatchBlock); |
| 387 | TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 388 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 389 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 390 | // Select an initial unroll count. This may be reduced later based |
| 391 | // on size thresholds. |
| 392 | bool CountSetExplicitly; |
| 393 | unsigned Count = selectUnrollCount(L, TripCount, HasEnablePragma, PragmaCount, |
| 394 | UP, CountSetExplicitly); |
Eli Bendersky | dc6de2c | 2014-06-12 18:05:39 +0000 | [diff] [blame] | 395 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 396 | unsigned NumInlineCandidates; |
| 397 | bool notDuplicatable; |
| 398 | unsigned LoopSize = |
| 399 | ApproximateLoopSize(L, NumInlineCandidates, notDuplicatable, TTI); |
| 400 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
| 401 | uint64_t UnrolledSize = (uint64_t)LoopSize * Count; |
| 402 | if (notDuplicatable) { |
| 403 | DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable" |
| 404 | << " instructions.\n"); |
| 405 | return false; |
| 406 | } |
| 407 | if (NumInlineCandidates != 0) { |
| 408 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
| 409 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 412 | unsigned Threshold, PartialThreshold; |
| 413 | selectThresholds(L, HasPragma, UP, Threshold, PartialThreshold); |
Benjamin Kramer | 9130cb8 | 2014-05-04 19:12:38 +0000 | [diff] [blame] | 414 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 415 | // Given Count, TripCount and thresholds determine the type of |
| 416 | // unrolling which is to be performed. |
| 417 | enum { Full = 0, Partial = 1, Runtime = 2 }; |
| 418 | int Unrolling; |
| 419 | if (TripCount && Count == TripCount) { |
| 420 | if (Threshold != NoThreshold && UnrolledSize > Threshold) { |
| 421 | DEBUG(dbgs() << " Too large to fully unroll with count: " << Count |
| 422 | << " because size: " << UnrolledSize << ">" << Threshold |
| 423 | << "\n"); |
| 424 | Unrolling = Partial; |
| 425 | } else { |
| 426 | Unrolling = Full; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 427 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 428 | } else if (TripCount && Count < TripCount) { |
| 429 | Unrolling = Partial; |
| 430 | } else { |
| 431 | Unrolling = Runtime; |
| 432 | } |
| 433 | |
| 434 | // Reduce count based on the type of unrolling and the threshold values. |
| 435 | unsigned OriginalCount = Count; |
| 436 | bool AllowRuntime = UserRuntime ? CurrentRuntime : UP.Runtime; |
| 437 | if (Unrolling == Partial) { |
| 438 | bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial; |
| 439 | if (!AllowPartial && !CountSetExplicitly) { |
| 440 | DEBUG(dbgs() << " will not try to unroll partially because " |
| 441 | << "-unroll-allow-partial not given\n"); |
| 442 | return false; |
| 443 | } |
| 444 | if (PartialThreshold != NoThreshold && UnrolledSize > PartialThreshold) { |
| 445 | // Reduce unroll count to be modulo of TripCount for partial unrolling. |
| 446 | Count = PartialThreshold / LoopSize; |
| 447 | while (Count != 0 && TripCount % Count != 0) |
| 448 | Count--; |
| 449 | } |
| 450 | } else if (Unrolling == Runtime) { |
| 451 | if (!AllowRuntime && !CountSetExplicitly) { |
| 452 | DEBUG(dbgs() << " will not try to unroll loop with runtime trip count " |
| 453 | << "-unroll-runtime not given\n"); |
| 454 | return false; |
| 455 | } |
| 456 | // Reduce unroll count to be the largest power-of-two factor of |
| 457 | // the original count which satisfies the threshold limit. |
| 458 | while (Count != 0 && UnrolledSize > PartialThreshold) { |
| 459 | Count >>= 1; |
| 460 | UnrolledSize = LoopSize * Count; |
| 461 | } |
| 462 | if (Count > UP.MaxCount) |
| 463 | Count = UP.MaxCount; |
| 464 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
| 465 | } |
| 466 | |
| 467 | if (HasPragma) { |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 468 | // Mark loop as unrolled to prevent unrolling beyond that |
| 469 | // requested by the pragma. |
| 470 | SetLoopAlreadyUnrolled(L); |
| 471 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 472 | // Emit optimization remarks if we are unable to unroll the loop |
| 473 | // as directed by a pragma. |
| 474 | DebugLoc LoopLoc = L->getStartLoc(); |
| 475 | Function *F = Header->getParent(); |
| 476 | LLVMContext &Ctx = F->getContext(); |
| 477 | if (HasEnablePragma && PragmaCount == 0) { |
| 478 | if (TripCount && Count != TripCount) { |
| 479 | emitOptimizationRemarkMissed( |
| 480 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 481 | "Unable to fully unroll loop as directed by unroll(enable) pragma " |
| 482 | "because unrolled size is too large."); |
| 483 | } else if (!TripCount) { |
| 484 | emitOptimizationRemarkMissed( |
| 485 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 486 | "Unable to fully unroll loop as directed by unroll(enable) pragma " |
| 487 | "because loop has a runtime trip count."); |
| 488 | } |
| 489 | } else if (PragmaCount > 0 && Count != OriginalCount) { |
| 490 | emitOptimizationRemarkMissed( |
| 491 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 492 | "Unable to unroll loop the number of times directed by " |
| 493 | "unroll_count pragma because unrolled size is too large."); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (Unrolling != Full && Count < 2) { |
| 498 | // Partial unrolling by 1 is a nop. For full unrolling, a factor |
| 499 | // of 1 makes sense because loop control can be eliminated. |
| 500 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 503 | // Unroll the loop. |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 504 | if (!UnrollLoop(L, Count, TripCount, AllowRuntime, TripMultiple, LI, this, &LPM)) |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 505 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 507 | return true; |
| 508 | } |