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" |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame^] | 16 | #include "llvm/Analysis/AssumptionTracker.h" |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CodeMetrics.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 67933e6 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 30 | #include <climits> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 31 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "loop-unroll" |
| 35 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 36 | static cl::opt<unsigned> |
Owen Anderson | d85c9cc | 2010-09-10 17:57:00 +0000 | [diff] [blame] | 37 | UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 38 | cl::desc("The cut-off point for automatic loop unrolling")); |
| 39 | |
| 40 | static cl::opt<unsigned> |
| 41 | UnrollCount("unroll-count", cl::init(0), cl::Hidden, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 42 | cl::desc("Use this unroll count for all loops including those with " |
| 43 | "unroll_count pragma values, for testing purposes")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 44 | |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, |
| 47 | cl::desc("Allows loops to be partially unrolled until " |
| 48 | "-unroll-threshold loop size is reached.")); |
| 49 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 50 | static cl::opt<bool> |
| 51 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, |
| 52 | cl::desc("Unroll loops with run-time trip counts")); |
| 53 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 54 | static cl::opt<unsigned> |
| 55 | PragmaUnrollThreshold("pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden, |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 56 | cl::desc("Unrolled size limit for loops with an unroll(full) or " |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 57 | "unroll_count pragma.")); |
| 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 { |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame^] | 106 | AU.addRequired<AssumptionTracker>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 107 | AU.addRequired<LoopInfo>(); |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 108 | AU.addPreserved<LoopInfo>(); |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 109 | AU.addRequiredID(LoopSimplifyID); |
| 110 | AU.addPreservedID(LoopSimplifyID); |
| 111 | AU.addRequiredID(LCSSAID); |
| 112 | AU.addPreservedID(LCSSAID); |
Andrew Trick | 4d0040b | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 113 | AU.addRequired<ScalarEvolution>(); |
Chris Lattner | d6f46b8 | 2010-08-29 17:21:35 +0000 | [diff] [blame] | 114 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 115 | AU.addRequired<TargetTransformInfo>(); |
Devang Patel | f94b982 | 2008-07-03 07:04:22 +0000 | [diff] [blame] | 116 | // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. |
| 117 | // If loop unroll does not preserve dom info then LCSSA pass on next |
| 118 | // loop will receive invalid dom info. |
| 119 | // For now, recreate dom info, if loop is unrolled. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 120 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 121 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 122 | |
| 123 | // Fill in the UnrollingPreferences parameter with values from the |
| 124 | // TargetTransformationInfo. |
| 125 | void getUnrollingPreferences(Loop *L, const TargetTransformInfo &TTI, |
| 126 | TargetTransformInfo::UnrollingPreferences &UP) { |
| 127 | UP.Threshold = CurrentThreshold; |
| 128 | UP.OptSizeThreshold = OptSizeUnrollThreshold; |
| 129 | UP.PartialThreshold = CurrentThreshold; |
| 130 | UP.PartialOptSizeThreshold = OptSizeUnrollThreshold; |
| 131 | UP.Count = CurrentCount; |
| 132 | UP.MaxCount = UINT_MAX; |
| 133 | UP.Partial = CurrentAllowPartial; |
| 134 | UP.Runtime = CurrentRuntime; |
| 135 | TTI.getUnrollingPreferences(L, UP); |
| 136 | } |
| 137 | |
| 138 | // Select and return an unroll count based on parameters from |
| 139 | // user, unroll preferences, unroll pragmas, or a heuristic. |
| 140 | // SetExplicitly is set to true if the unroll count is is set by |
| 141 | // the user or a pragma rather than selected heuristically. |
| 142 | unsigned |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 143 | selectUnrollCount(const Loop *L, unsigned TripCount, bool PragmaFullUnroll, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 144 | unsigned PragmaCount, |
| 145 | const TargetTransformInfo::UnrollingPreferences &UP, |
| 146 | bool &SetExplicitly); |
| 147 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 148 | // Select threshold values used to limit unrolling based on a |
| 149 | // total unrolled size. Parameters Threshold and PartialThreshold |
| 150 | // are set to the maximum unrolled size for fully and partially |
| 151 | // unrolled loops respectively. |
| 152 | void selectThresholds(const Loop *L, bool HasPragma, |
| 153 | const TargetTransformInfo::UnrollingPreferences &UP, |
| 154 | unsigned &Threshold, unsigned &PartialThreshold) { |
| 155 | // Determine the current unrolling threshold. While this is |
| 156 | // normally set from UnrollThreshold, it is overridden to a |
| 157 | // smaller value if the current function is marked as |
| 158 | // optimize-for-size, and the unroll threshold was not user |
| 159 | // specified. |
| 160 | Threshold = UserThreshold ? CurrentThreshold : UP.Threshold; |
| 161 | PartialThreshold = UserThreshold ? CurrentThreshold : UP.PartialThreshold; |
| 162 | if (!UserThreshold && |
| 163 | L->getHeader()->getParent()->getAttributes(). |
| 164 | hasAttribute(AttributeSet::FunctionIndex, |
| 165 | Attribute::OptimizeForSize)) { |
| 166 | Threshold = UP.OptSizeThreshold; |
| 167 | PartialThreshold = UP.PartialOptSizeThreshold; |
| 168 | } |
| 169 | if (HasPragma) { |
| 170 | // If the loop has an unrolling pragma, we want to be more |
| 171 | // aggressive with unrolling limits. Set thresholds to at |
| 172 | // least the PragmaTheshold value which is larger than the |
| 173 | // default limits. |
| 174 | if (Threshold != NoThreshold) |
| 175 | Threshold = std::max<unsigned>(Threshold, PragmaUnrollThreshold); |
| 176 | if (PartialThreshold != NoThreshold) |
| 177 | PartialThreshold = |
| 178 | std::max<unsigned>(PartialThreshold, PragmaUnrollThreshold); |
| 179 | } |
| 180 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 181 | }; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 184 | char LoopUnroll::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 185 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 186 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame^] | 187 | INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 188 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 189 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 190 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Devang Patel | 88b4fa2 | 2011-10-19 23:56:07 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 192 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 193 | |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 194 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 195 | int Runtime) { |
| 196 | return new LoopUnroll(Threshold, Count, AllowPartial, Runtime); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 198 | |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 199 | Pass *llvm::createSimpleLoopUnrollPass() { |
| 200 | return llvm::createLoopUnrollPass(-1, -1, 0, 0); |
| 201 | } |
| 202 | |
Dan Gohman | 49d08a5 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 203 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | f765601 | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 204 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 205 | bool &NotDuplicatable, |
| 206 | const TargetTransformInfo &TTI) { |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 207 | CodeMetrics Metrics; |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 208 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 209 | I != E; ++I) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 210 | Metrics.analyzeBasicBlock(*I, TTI); |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 211 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 212 | NotDuplicatable = Metrics.notDuplicatable; |
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 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 215 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 216 | // Don't allow an estimate of size zero. This would allows unrolling of loops |
| 217 | // with huge iteration counts, which is a compile time problem even if it's |
| 218 | // not a problem for code quality. |
| 219 | if (LoopSize == 0) LoopSize = 1; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 220 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 221 | return LoopSize; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 224 | // Returns the loop hint metadata node with the given name (for example, |
| 225 | // "llvm.loop.unroll.count"). If no such metadata node exists, then nullptr is |
| 226 | // returned. |
Benjamin Kramer | 89854eb | 2014-09-03 21:04:12 +0000 | [diff] [blame] | 227 | static const MDNode *GetUnrollMetadata(const Loop *L, StringRef Name) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 228 | MDNode *LoopID = L->getLoopID(); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 229 | if (!LoopID) |
| 230 | return nullptr; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 231 | |
| 232 | // First operand should refer to the loop id itself. |
| 233 | assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); |
| 234 | assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); |
| 235 | |
| 236 | for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { |
| 237 | const MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 238 | if (!MD) |
| 239 | continue; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 240 | |
| 241 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 242 | if (!S) |
| 243 | continue; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 244 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 245 | if (Name.equals(S->getString())) |
| 246 | return MD; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 247 | } |
| 248 | return nullptr; |
| 249 | } |
| 250 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 251 | // Returns true if the loop has an unroll(full) pragma. |
| 252 | static bool HasUnrollFullPragma(const Loop *L) { |
| 253 | return GetUnrollMetadata(L, "llvm.loop.unroll.full"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Returns true if the loop has an unroll(disable) pragma. |
| 257 | static bool HasUnrollDisablePragma(const Loop *L) { |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 258 | return GetUnrollMetadata(L, "llvm.loop.unroll.disable"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // If loop has an unroll_count pragma return the (necessarily |
| 262 | // positive) value from the pragma. Otherwise return 0. |
| 263 | static unsigned UnrollCountPragmaValue(const Loop *L) { |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 264 | const MDNode *MD = GetUnrollMetadata(L, "llvm.loop.unroll.count"); |
| 265 | if (MD) { |
| 266 | assert(MD->getNumOperands() == 2 && |
| 267 | "Unroll count hint metadata should have two operands."); |
| 268 | unsigned Count = cast<ConstantInt>(MD->getOperand(1))->getZExtValue(); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 269 | assert(Count >= 1 && "Unroll count must be positive."); |
| 270 | return Count; |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 275 | // Remove existing unroll metadata and add unroll disable metadata to |
| 276 | // indicate the loop has already been unrolled. This prevents a loop |
| 277 | // from being unrolled more than is directed by a pragma if the loop |
| 278 | // unrolling pass is run more than once (which it generally is). |
| 279 | static void SetLoopAlreadyUnrolled(Loop *L) { |
| 280 | MDNode *LoopID = L->getLoopID(); |
| 281 | if (!LoopID) return; |
| 282 | |
| 283 | // First remove any existing loop unrolling metadata. |
| 284 | SmallVector<Value *, 4> Vals; |
| 285 | // Reserve first location for self reference to the LoopID metadata node. |
| 286 | Vals.push_back(nullptr); |
| 287 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 288 | bool IsUnrollMetadata = false; |
| 289 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 290 | if (MD) { |
| 291 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 292 | IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); |
| 293 | } |
| 294 | if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i)); |
| 295 | } |
| 296 | |
| 297 | // Add unroll(disable) metadata to disable future unrolling. |
| 298 | LLVMContext &Context = L->getHeader()->getContext(); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 299 | SmallVector<Value *, 1> DisableOperands; |
| 300 | DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable")); |
Mark Heffernan | f3764da | 2014-07-18 21:29:41 +0000 | [diff] [blame] | 301 | MDNode *DisableNode = MDNode::get(Context, DisableOperands); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 302 | Vals.push_back(DisableNode); |
| 303 | |
| 304 | MDNode *NewLoopID = MDNode::get(Context, Vals); |
| 305 | // Set operand 0 to refer to the loop id itself. |
| 306 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 307 | L->setLoopID(NewLoopID); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 310 | unsigned LoopUnroll::selectUnrollCount( |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 311 | const Loop *L, unsigned TripCount, bool PragmaFullUnroll, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 312 | unsigned PragmaCount, const TargetTransformInfo::UnrollingPreferences &UP, |
| 313 | bool &SetExplicitly) { |
| 314 | SetExplicitly = true; |
| 315 | |
| 316 | // User-specified count (either as a command-line option or |
| 317 | // constructor parameter) has highest precedence. |
| 318 | unsigned Count = UserCount ? CurrentCount : 0; |
| 319 | |
| 320 | // If there is no user-specified count, unroll pragmas have the next |
| 321 | // highest precendence. |
| 322 | if (Count == 0) { |
| 323 | if (PragmaCount) { |
| 324 | Count = PragmaCount; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 325 | } else if (PragmaFullUnroll) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 326 | Count = TripCount; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (Count == 0) |
| 331 | Count = UP.Count; |
| 332 | |
| 333 | if (Count == 0) { |
| 334 | SetExplicitly = false; |
| 335 | if (TripCount == 0) |
| 336 | // Runtime trip count. |
| 337 | Count = UnrollRuntimeCount; |
| 338 | else |
| 339 | // Conservative heuristic: if we know the trip count, see if we can |
| 340 | // completely unroll (subject to the threshold, checked below); otherwise |
| 341 | // try to find greatest modulo of the trip count which is still under |
| 342 | // threshold value. |
| 343 | Count = TripCount; |
| 344 | } |
| 345 | if (TripCount && Count > TripCount) |
| 346 | return TripCount; |
| 347 | return Count; |
| 348 | } |
| 349 | |
Devang Patel | 9779e56 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 350 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 351 | if (skipOptnoneFunction(L)) |
| 352 | return false; |
| 353 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 354 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 355 | ScalarEvolution *SE = &getAnalysis<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 356 | const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>(); |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame^] | 357 | AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 358 | |
Dan Gohman | 2e1f804 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 359 | BasicBlock *Header = L->getHeader(); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 360 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 361 | << "] Loop %" << Header->getName() << "\n"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 362 | |
| 363 | if (HasUnrollDisablePragma(L)) { |
| 364 | return false; |
| 365 | } |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 366 | bool PragmaFullUnroll = HasUnrollFullPragma(L); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 367 | unsigned PragmaCount = UnrollCountPragmaValue(L); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 368 | bool HasPragma = PragmaFullUnroll || PragmaCount > 0; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 369 | |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 370 | TargetTransformInfo::UnrollingPreferences UP; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 371 | getUnrollingPreferences(L, TTI, UP); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 372 | |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 373 | // Find trip count and trip multiple if count is not available |
| 374 | unsigned TripCount = 0; |
Andrew Trick | 1cabe54 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 375 | unsigned TripMultiple = 1; |
Andrew Trick | a8bdb7c | 2011-11-28 19:22:09 +0000 | [diff] [blame] | 376 | // Find "latch trip count". UnrollLoop assumes that control cannot exit |
| 377 | // via the loop latch on any iteration prior to TripCount. The loop may exit |
| 378 | // early via an earlier branch. |
| 379 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 380 | if (LatchBlock) { |
| 381 | TripCount = SE->getSmallConstantTripCount(L, LatchBlock); |
| 382 | TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 383 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 384 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 385 | // Select an initial unroll count. This may be reduced later based |
| 386 | // on size thresholds. |
| 387 | bool CountSetExplicitly; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 388 | unsigned Count = selectUnrollCount(L, TripCount, PragmaFullUnroll, |
| 389 | PragmaCount, UP, CountSetExplicitly); |
Eli Bendersky | dc6de2c | 2014-06-12 18:05:39 +0000 | [diff] [blame] | 390 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 391 | unsigned NumInlineCandidates; |
| 392 | bool notDuplicatable; |
| 393 | unsigned LoopSize = |
| 394 | ApproximateLoopSize(L, NumInlineCandidates, notDuplicatable, TTI); |
| 395 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
| 396 | uint64_t UnrolledSize = (uint64_t)LoopSize * Count; |
| 397 | if (notDuplicatable) { |
| 398 | DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable" |
| 399 | << " instructions.\n"); |
| 400 | return false; |
| 401 | } |
| 402 | if (NumInlineCandidates != 0) { |
| 403 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
| 404 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 407 | unsigned Threshold, PartialThreshold; |
| 408 | selectThresholds(L, HasPragma, UP, Threshold, PartialThreshold); |
Benjamin Kramer | 9130cb8 | 2014-05-04 19:12:38 +0000 | [diff] [blame] | 409 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 410 | // Given Count, TripCount and thresholds determine the type of |
| 411 | // unrolling which is to be performed. |
| 412 | enum { Full = 0, Partial = 1, Runtime = 2 }; |
| 413 | int Unrolling; |
| 414 | if (TripCount && Count == TripCount) { |
| 415 | if (Threshold != NoThreshold && UnrolledSize > Threshold) { |
| 416 | DEBUG(dbgs() << " Too large to fully unroll with count: " << Count |
| 417 | << " because size: " << UnrolledSize << ">" << Threshold |
| 418 | << "\n"); |
| 419 | Unrolling = Partial; |
| 420 | } else { |
| 421 | Unrolling = Full; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 422 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 423 | } else if (TripCount && Count < TripCount) { |
| 424 | Unrolling = Partial; |
| 425 | } else { |
| 426 | Unrolling = Runtime; |
| 427 | } |
| 428 | |
| 429 | // Reduce count based on the type of unrolling and the threshold values. |
| 430 | unsigned OriginalCount = Count; |
| 431 | bool AllowRuntime = UserRuntime ? CurrentRuntime : UP.Runtime; |
| 432 | if (Unrolling == Partial) { |
| 433 | bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial; |
| 434 | if (!AllowPartial && !CountSetExplicitly) { |
| 435 | DEBUG(dbgs() << " will not try to unroll partially because " |
| 436 | << "-unroll-allow-partial not given\n"); |
| 437 | return false; |
| 438 | } |
| 439 | if (PartialThreshold != NoThreshold && UnrolledSize > PartialThreshold) { |
| 440 | // Reduce unroll count to be modulo of TripCount for partial unrolling. |
| 441 | Count = PartialThreshold / LoopSize; |
| 442 | while (Count != 0 && TripCount % Count != 0) |
| 443 | Count--; |
| 444 | } |
| 445 | } else if (Unrolling == Runtime) { |
| 446 | if (!AllowRuntime && !CountSetExplicitly) { |
| 447 | DEBUG(dbgs() << " will not try to unroll loop with runtime trip count " |
| 448 | << "-unroll-runtime not given\n"); |
| 449 | return false; |
| 450 | } |
| 451 | // Reduce unroll count to be the largest power-of-two factor of |
| 452 | // the original count which satisfies the threshold limit. |
| 453 | while (Count != 0 && UnrolledSize > PartialThreshold) { |
| 454 | Count >>= 1; |
| 455 | UnrolledSize = LoopSize * Count; |
| 456 | } |
| 457 | if (Count > UP.MaxCount) |
| 458 | Count = UP.MaxCount; |
| 459 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
| 460 | } |
| 461 | |
| 462 | if (HasPragma) { |
Mark Heffernan | 9e11244 | 2014-07-23 20:05:44 +0000 | [diff] [blame] | 463 | if (PragmaCount != 0) |
| 464 | // If loop has an unroll count pragma mark loop as unrolled to prevent |
| 465 | // unrolling beyond that requested by the pragma. |
| 466 | SetLoopAlreadyUnrolled(L); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 467 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 468 | // Emit optimization remarks if we are unable to unroll the loop |
| 469 | // as directed by a pragma. |
| 470 | DebugLoc LoopLoc = L->getStartLoc(); |
| 471 | Function *F = Header->getParent(); |
| 472 | LLVMContext &Ctx = F->getContext(); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 473 | if (PragmaFullUnroll && PragmaCount == 0) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 474 | if (TripCount && Count != TripCount) { |
| 475 | emitOptimizationRemarkMissed( |
| 476 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 477 | "Unable to fully unroll loop as directed by unroll(full) pragma " |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 478 | "because unrolled size is too large."); |
| 479 | } else if (!TripCount) { |
| 480 | emitOptimizationRemarkMissed( |
| 481 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 482 | "Unable to fully unroll loop as directed by unroll(full) pragma " |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 483 | "because loop has a runtime trip count."); |
| 484 | } |
| 485 | } else if (PragmaCount > 0 && Count != OriginalCount) { |
| 486 | emitOptimizationRemarkMissed( |
| 487 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 488 | "Unable to unroll loop the number of times directed by " |
| 489 | "unroll_count pragma because unrolled size is too large."); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (Unrolling != Full && Count < 2) { |
| 494 | // Partial unrolling by 1 is a nop. For full unrolling, a factor |
| 495 | // of 1 makes sense because loop control can be eliminated. |
| 496 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 499 | // Unroll the loop. |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame^] | 500 | if (!UnrollLoop(L, Count, TripCount, AllowRuntime, TripMultiple, LI, this, |
| 501 | &LPM, AT)) |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 502 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 504 | return true; |
| 505 | } |