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" |
Chandler Carruth | 3b057b3 | 2015-02-13 03:57:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionCache.h" |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CodeMetrics.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ScalarEvolution.h" |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Dominators.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 28 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/IntrinsicInst.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 67933e6 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 35 | #include <climits> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 36 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "loop-unroll" |
| 40 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 41 | static cl::opt<unsigned> |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 42 | UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, |
| 43 | cl::desc("The baseline cost threshold for loop unrolling")); |
| 44 | |
| 45 | static cl::opt<unsigned> UnrollPercentDynamicCostSavedThreshold( |
| 46 | "unroll-percent-dynamic-cost-saved-threshold", cl::init(20), cl::Hidden, |
| 47 | cl::desc("The percentage of estimated dynamic cost which must be saved by " |
| 48 | "unrolling to allow unrolling up to the max threshold.")); |
| 49 | |
| 50 | static cl::opt<unsigned> UnrollDynamicCostSavingsDiscount( |
| 51 | "unroll-dynamic-cost-savings-discount", cl::init(2000), cl::Hidden, |
| 52 | cl::desc("This is the amount discounted from the total unroll cost when " |
| 53 | "the unrolled form has a high dynamic cost savings (triggered by " |
| 54 | "the '-unroll-perecent-dynamic-cost-saved-threshold' flag).")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 55 | |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 56 | static cl::opt<unsigned> UnrollMaxIterationsCountToAnalyze( |
Chandler Carruth | 1fbc316 | 2015-02-13 05:31:46 +0000 | [diff] [blame] | 57 | "unroll-max-iteration-count-to-analyze", cl::init(0), cl::Hidden, |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 58 | cl::desc("Don't allow loop unrolling to simulate more than this number of" |
| 59 | "iterations when checking full unroll profitability")); |
| 60 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | static cl::opt<unsigned> |
| 62 | UnrollCount("unroll-count", cl::init(0), cl::Hidden, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 63 | cl::desc("Use this unroll count for all loops including those with " |
| 64 | "unroll_count pragma values, for testing purposes")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 66 | static cl::opt<bool> |
| 67 | UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, |
| 68 | cl::desc("Allows loops to be partially unrolled until " |
| 69 | "-unroll-threshold loop size is reached.")); |
| 70 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 71 | static cl::opt<bool> |
| 72 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, |
| 73 | cl::desc("Unroll loops with run-time trip counts")); |
| 74 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 75 | static cl::opt<unsigned> |
| 76 | PragmaUnrollThreshold("pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden, |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 77 | cl::desc("Unrolled size limit for loops with an unroll(full) or " |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 78 | "unroll_count pragma.")); |
| 79 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 80 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 81 | class LoopUnroll : public LoopPass { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 82 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 83 | static char ID; // Pass ID, replacement for typeid |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 84 | 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] | 85 | CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 86 | CurrentPercentDynamicCostSavedThreshold = |
| 87 | UnrollPercentDynamicCostSavedThreshold; |
| 88 | CurrentDynamicCostSavingsDiscount = UnrollDynamicCostSavingsDiscount; |
Chris Lattner | 35a65b2 | 2011-04-14 02:27:25 +0000 | [diff] [blame] | 89 | CurrentCount = (C == -1) ? UnrollCount : unsigned(C); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 90 | CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P; |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 91 | CurrentRuntime = (R == -1) ? UnrollRuntime : (bool)R; |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 92 | |
| 93 | UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 94 | UserPercentDynamicCostSavedThreshold = |
| 95 | (UnrollPercentDynamicCostSavedThreshold.getNumOccurrences() > 0); |
| 96 | UserDynamicCostSavingsDiscount = |
| 97 | (UnrollDynamicCostSavingsDiscount.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 98 | UserAllowPartial = (P != -1) || |
| 99 | (UnrollAllowPartial.getNumOccurrences() > 0); |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 100 | UserRuntime = (R != -1) || (UnrollRuntime.getNumOccurrences() > 0); |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 101 | UserCount = (C != -1) || (UnrollCount.getNumOccurrences() > 0); |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 102 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 103 | initializeLoopUnrollPass(*PassRegistry::getPassRegistry()); |
| 104 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 105 | |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 106 | /// A magic value for use with the Threshold parameter to indicate |
| 107 | /// that the loop unroll should be performed regardless of how much |
| 108 | /// code expansion would result. |
| 109 | static const unsigned NoThreshold = UINT_MAX; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 110 | |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 111 | // Threshold to use when optsize is specified (and there is no |
| 112 | // explicit -unroll-threshold). |
| 113 | static const unsigned OptSizeUnrollThreshold = 50; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 114 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 115 | // Default unroll count for loops with run-time trip count if |
| 116 | // -unroll-count is not set |
| 117 | static const unsigned UnrollRuntimeCount = 8; |
| 118 | |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 119 | unsigned CurrentCount; |
Owen Anderson | a4d9c78 | 2010-09-07 23:15:30 +0000 | [diff] [blame] | 120 | unsigned CurrentThreshold; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 121 | unsigned CurrentPercentDynamicCostSavedThreshold; |
| 122 | unsigned CurrentDynamicCostSavingsDiscount; |
| 123 | bool CurrentAllowPartial; |
| 124 | bool CurrentRuntime; |
| 125 | |
| 126 | // Flags for whether the 'current' settings are user-specified. |
| 127 | bool UserCount; |
| 128 | bool UserThreshold; |
| 129 | bool UserPercentDynamicCostSavedThreshold; |
| 130 | bool UserDynamicCostSavingsDiscount; |
| 131 | bool UserAllowPartial; |
| 132 | bool UserRuntime; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 133 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 134 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 135 | |
| 136 | /// This transformation requires natural loop information & requires that |
| 137 | /// loop preheaders be inserted into the CFG... |
| 138 | /// |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 139 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 140 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 141 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 142 | AU.addRequired<LoopInfoWrapperPass>(); |
| 143 | AU.addPreserved<LoopInfoWrapperPass>(); |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 144 | AU.addRequiredID(LoopSimplifyID); |
| 145 | AU.addPreservedID(LoopSimplifyID); |
| 146 | AU.addRequiredID(LCSSAID); |
| 147 | AU.addPreservedID(LCSSAID); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 148 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 149 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 150 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Devang Patel | f94b982 | 2008-07-03 07:04:22 +0000 | [diff] [blame] | 151 | // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. |
| 152 | // If loop unroll does not preserve dom info then LCSSA pass on next |
| 153 | // loop will receive invalid dom info. |
| 154 | // For now, recreate dom info, if loop is unrolled. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 155 | AU.addPreserved<DominatorTreeWrapperPass>(); |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 156 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 157 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 158 | |
| 159 | // Fill in the UnrollingPreferences parameter with values from the |
| 160 | // TargetTransformationInfo. |
Chandler Carruth | 21fc195 | 2015-02-01 14:37:03 +0000 | [diff] [blame] | 161 | void getUnrollingPreferences(Loop *L, const TargetTransformInfo &TTI, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 162 | TargetTransformInfo::UnrollingPreferences &UP) { |
| 163 | UP.Threshold = CurrentThreshold; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 164 | UP.PercentDynamicCostSavedThreshold = |
| 165 | CurrentPercentDynamicCostSavedThreshold; |
| 166 | UP.DynamicCostSavingsDiscount = CurrentDynamicCostSavingsDiscount; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 167 | UP.OptSizeThreshold = OptSizeUnrollThreshold; |
| 168 | UP.PartialThreshold = CurrentThreshold; |
| 169 | UP.PartialOptSizeThreshold = OptSizeUnrollThreshold; |
| 170 | UP.Count = CurrentCount; |
| 171 | UP.MaxCount = UINT_MAX; |
| 172 | UP.Partial = CurrentAllowPartial; |
| 173 | UP.Runtime = CurrentRuntime; |
Sanjoy Das | e178f46 | 2015-04-14 03:20:38 +0000 | [diff] [blame] | 174 | UP.AllowExpensiveTripCount = false; |
Chandler Carruth | 21fc195 | 2015-02-01 14:37:03 +0000 | [diff] [blame] | 175 | TTI.getUnrollingPreferences(L, UP); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Select and return an unroll count based on parameters from |
| 179 | // user, unroll preferences, unroll pragmas, or a heuristic. |
| 180 | // SetExplicitly is set to true if the unroll count is is set by |
| 181 | // the user or a pragma rather than selected heuristically. |
| 182 | unsigned |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 183 | selectUnrollCount(const Loop *L, unsigned TripCount, bool PragmaFullUnroll, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 184 | unsigned PragmaCount, |
| 185 | const TargetTransformInfo::UnrollingPreferences &UP, |
| 186 | bool &SetExplicitly); |
| 187 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 188 | // Select threshold values used to limit unrolling based on a |
| 189 | // total unrolled size. Parameters Threshold and PartialThreshold |
| 190 | // are set to the maximum unrolled size for fully and partially |
| 191 | // unrolled loops respectively. |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 192 | void selectThresholds(const Loop *L, bool UsePragmaThreshold, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 193 | const TargetTransformInfo::UnrollingPreferences &UP, |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 194 | unsigned &Threshold, unsigned &PartialThreshold, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 195 | unsigned &PercentDynamicCostSavedThreshold, |
| 196 | unsigned &DynamicCostSavingsDiscount) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 197 | // Determine the current unrolling threshold. While this is |
| 198 | // normally set from UnrollThreshold, it is overridden to a |
| 199 | // smaller value if the current function is marked as |
| 200 | // optimize-for-size, and the unroll threshold was not user |
| 201 | // specified. |
| 202 | Threshold = UserThreshold ? CurrentThreshold : UP.Threshold; |
| 203 | PartialThreshold = UserThreshold ? CurrentThreshold : UP.PartialThreshold; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 204 | PercentDynamicCostSavedThreshold = |
| 205 | UserPercentDynamicCostSavedThreshold |
| 206 | ? CurrentPercentDynamicCostSavedThreshold |
| 207 | : UP.PercentDynamicCostSavedThreshold; |
| 208 | DynamicCostSavingsDiscount = UserDynamicCostSavingsDiscount |
| 209 | ? CurrentDynamicCostSavingsDiscount |
| 210 | : UP.DynamicCostSavingsDiscount; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 211 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 212 | if (!UserThreshold && |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 213 | // FIXME: Use Function::optForSize(). |
Duncan P. N. Exon Smith | 2c79ad9 | 2015-02-14 01:11:29 +0000 | [diff] [blame] | 214 | L->getHeader()->getParent()->hasFnAttribute( |
| 215 | Attribute::OptimizeForSize)) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 216 | Threshold = UP.OptSizeThreshold; |
| 217 | PartialThreshold = UP.PartialOptSizeThreshold; |
| 218 | } |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 219 | if (UsePragmaThreshold) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 220 | // If the loop has an unrolling pragma, we want to be more |
| 221 | // aggressive with unrolling limits. Set thresholds to at |
| 222 | // least the PragmaTheshold value which is larger than the |
| 223 | // default limits. |
| 224 | if (Threshold != NoThreshold) |
| 225 | Threshold = std::max<unsigned>(Threshold, PragmaUnrollThreshold); |
| 226 | if (PartialThreshold != NoThreshold) |
| 227 | PartialThreshold = |
| 228 | std::max<unsigned>(PartialThreshold, PragmaUnrollThreshold); |
| 229 | } |
| 230 | } |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 231 | bool canUnrollCompletely(Loop *L, unsigned Threshold, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 232 | unsigned PercentDynamicCostSavedThreshold, |
| 233 | unsigned DynamicCostSavingsDiscount, |
Sanjoy Das | ad714b1 | 2015-06-06 05:24:10 +0000 | [diff] [blame] | 234 | uint64_t UnrolledCost, uint64_t RolledDynamicCost); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 235 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 237 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 238 | char LoopUnroll::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 239 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 240 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 241 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 242 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 243 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 244 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 245 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 246 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 247 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 248 | |
Hal Finkel | 081eaef | 2013-11-05 00:08:03 +0000 | [diff] [blame] | 249 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 250 | int Runtime) { |
| 251 | return new LoopUnroll(Threshold, Count, AllowPartial, Runtime); |
Junjie Gu | 7c3b459 | 2011-04-13 16:15:29 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 253 | |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 254 | Pass *llvm::createSimpleLoopUnrollPass() { |
| 255 | return llvm::createLoopUnrollPass(-1, -1, 0, 0); |
| 256 | } |
| 257 | |
Benjamin Kramer | 51f6096c | 2015-03-23 12:30:58 +0000 | [diff] [blame] | 258 | namespace { |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 259 | // This class is used to get an estimate of the optimization effects that we |
| 260 | // could get from complete loop unrolling. It comes from the fact that some |
| 261 | // loads might be replaced with concrete constant values and that could trigger |
| 262 | // a chain of instruction simplifications. |
| 263 | // |
| 264 | // E.g. we might have: |
| 265 | // int a[] = {0, 1, 0}; |
| 266 | // v = 0; |
| 267 | // for (i = 0; i < 3; i ++) |
| 268 | // v += b[i]*a[i]; |
| 269 | // If we completely unroll the loop, we would get: |
| 270 | // v = b[0]*a[0] + b[1]*a[1] + b[2]*a[2] |
| 271 | // Which then will be simplified to: |
| 272 | // v = b[0]* 0 + b[1]* 1 + b[2]* 0 |
| 273 | // And finally: |
| 274 | // v = b[1] |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 275 | class UnrolledInstAnalyzer : private InstVisitor<UnrolledInstAnalyzer, bool> { |
| 276 | typedef InstVisitor<UnrolledInstAnalyzer, bool> Base; |
| 277 | friend class InstVisitor<UnrolledInstAnalyzer, bool>; |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 278 | struct SimplifiedAddress { |
| 279 | Value *Base = nullptr; |
| 280 | ConstantInt *Offset = nullptr; |
| 281 | }; |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 282 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 283 | public: |
| 284 | UnrolledInstAnalyzer(unsigned Iteration, |
| 285 | DenseMap<Value *, Constant *> &SimplifiedValues, |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 286 | const Loop *L, ScalarEvolution &SE) |
| 287 | : Iteration(Iteration), SimplifiedValues(SimplifiedValues), L(L), SE(SE) { |
| 288 | IterationNumber = SE.getConstant(APInt(64, Iteration)); |
| 289 | } |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 290 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 291 | // Allow access to the initial visit method. |
| 292 | using Base::visit; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 293 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 294 | private: |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 295 | /// \brief A cache of pointer bases and constant-folded offsets corresponding |
| 296 | /// to GEP (or derived from GEP) instructions. |
| 297 | /// |
| 298 | /// In order to find the base pointer one needs to perform non-trivial |
| 299 | /// traversal of the corresponding SCEV expression, so it's good to have the |
| 300 | /// results saved. |
| 301 | DenseMap<Value *, SimplifiedAddress> SimplifiedAddresses; |
| 302 | |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 303 | /// \brief Number of currently simulated iteration. |
| 304 | /// |
| 305 | /// If an expression is ConstAddress+Constant, then the Constant is |
| 306 | /// Start + Iteration*Step, where Start and Step could be obtained from |
Chandler Carruth | e1a0462 | 2015-05-22 03:02:22 +0000 | [diff] [blame] | 307 | /// SCEVGEPCache. |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 308 | unsigned Iteration; |
| 309 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 310 | /// \brief SCEV expression corresponding to number of currently simulated |
| 311 | /// iteration. |
| 312 | const SCEV *IterationNumber; |
| 313 | |
| 314 | /// \brief A Value->Constant map for keeping values that we managed to |
| 315 | /// constant-fold on the given iteration. |
| 316 | /// |
| 317 | /// While we walk the loop instructions, we build up and maintain a mapping |
| 318 | /// of simplified values specific to this iteration. The idea is to propagate |
| 319 | /// any special information we have about loads that can be replaced with |
| 320 | /// constants after complete unrolling, and account for likely simplifications |
| 321 | /// post-unrolling. |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 322 | DenseMap<Value *, Constant *> &SimplifiedValues; |
| 323 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 324 | const Loop *L; |
| 325 | ScalarEvolution &SE; |
| 326 | |
| 327 | /// \brief Try to simplify instruction \param I using its SCEV expression. |
| 328 | /// |
| 329 | /// The idea is that some AddRec expressions become constants, which then |
| 330 | /// could trigger folding of other instructions. However, that only happens |
| 331 | /// for expressions whose start value is also constant, which isn't always the |
| 332 | /// case. In another common and important case the start value is just some |
| 333 | /// address (i.e. SCEVUnknown) - in this case we compute the offset and save |
| 334 | /// it along with the base address instead. |
| 335 | bool simplifyInstWithSCEV(Instruction *I) { |
| 336 | if (!SE.isSCEVable(I->getType())) |
| 337 | return false; |
| 338 | |
| 339 | const SCEV *S = SE.getSCEV(I); |
| 340 | if (auto *SC = dyn_cast<SCEVConstant>(S)) { |
| 341 | SimplifiedValues[I] = SC->getValue(); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | auto *AR = dyn_cast<SCEVAddRecExpr>(S); |
| 346 | if (!AR) |
| 347 | return false; |
| 348 | |
| 349 | const SCEV *ValueAtIteration = AR->evaluateAtIteration(IterationNumber, SE); |
| 350 | // Check if the AddRec expression becomes a constant. |
| 351 | if (auto *SC = dyn_cast<SCEVConstant>(ValueAtIteration)) { |
| 352 | SimplifiedValues[I] = SC->getValue(); |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | // Check if the offset from the base address becomes a constant. |
| 357 | auto *Base = dyn_cast<SCEVUnknown>(SE.getPointerBase(S)); |
| 358 | if (!Base) |
| 359 | return false; |
| 360 | auto *Offset = |
| 361 | dyn_cast<SCEVConstant>(SE.getMinusSCEV(ValueAtIteration, Base)); |
| 362 | if (!Offset) |
| 363 | return false; |
| 364 | SimplifiedAddress Address; |
| 365 | Address.Base = Base->getValue(); |
| 366 | Address.Offset = Offset->getValue(); |
| 367 | SimplifiedAddresses[I] = Address; |
| 368 | return true; |
| 369 | } |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 370 | |
| 371 | /// Base case for the instruction visitor. |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 372 | bool visitInstruction(Instruction &I) { |
| 373 | return simplifyInstWithSCEV(&I); |
| 374 | } |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 375 | |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 376 | /// Try to simplify binary operator I. |
| 377 | /// |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 378 | /// TODO: Probably it's worth to hoist the code for estimating the |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 379 | /// simplifications effects to a separate class, since we have a very similar |
| 380 | /// code in InlineCost already. |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 381 | bool visitBinaryOperator(BinaryOperator &I) { |
| 382 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 383 | if (!isa<Constant>(LHS)) |
| 384 | if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS)) |
| 385 | LHS = SimpleLHS; |
| 386 | if (!isa<Constant>(RHS)) |
| 387 | if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS)) |
| 388 | RHS = SimpleRHS; |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 389 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 390 | Value *SimpleV = nullptr; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 391 | const DataLayout &DL = I.getModule()->getDataLayout(); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 392 | if (auto FI = dyn_cast<FPMathOperator>(&I)) |
| 393 | SimpleV = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 394 | SimplifyFPBinOp(I.getOpcode(), LHS, RHS, FI->getFastMathFlags(), DL); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 395 | else |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 396 | SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, DL); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 397 | |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 398 | if (Constant *C = dyn_cast_or_null<Constant>(SimpleV)) |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 399 | SimplifiedValues[&I] = C; |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 400 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 401 | if (SimpleV) |
| 402 | return true; |
| 403 | return Base::visitBinaryOperator(I); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 406 | /// Try to fold load I. |
| 407 | bool visitLoad(LoadInst &I) { |
| 408 | Value *AddrOp = I.getPointerOperand(); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 409 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 410 | auto AddressIt = SimplifiedAddresses.find(AddrOp); |
| 411 | if (AddressIt == SimplifiedAddresses.end()) |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 412 | return false; |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 413 | ConstantInt *SimplifiedAddrOp = AddressIt->second.Offset; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 414 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 415 | auto *GV = dyn_cast<GlobalVariable>(AddressIt->second.Base); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 416 | // We're only interested in loads that can be completely folded to a |
| 417 | // constant. |
Michael Zolotukhin | 8bb31dd | 2015-09-22 21:41:29 +0000 | [diff] [blame] | 418 | if (!GV || !GV->hasDefinitiveInitializer() || !GV->isConstant()) |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 419 | return false; |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 420 | |
| 421 | ConstantDataSequential *CDS = |
| 422 | dyn_cast<ConstantDataSequential>(GV->getInitializer()); |
| 423 | if (!CDS) |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 424 | return false; |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 425 | |
Michael Zolotukhin | deade19 | 2015-09-22 22:27:12 +0000 | [diff] [blame] | 426 | // We might have a vector load from an array. FIXME: for now we just bail |
| 427 | // out in this case, but we should be able to resolve and simplify such |
| 428 | // loads. |
| 429 | if(!CDS->isElementTypeCompatible(I.getType())) |
| 430 | return false; |
| 431 | |
Chandler Carruth | a6ae877 | 2015-05-12 23:32:56 +0000 | [diff] [blame] | 432 | int ElemSize = CDS->getElementType()->getPrimitiveSizeInBits() / 8U; |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 433 | assert(SimplifiedAddrOp->getValue().getActiveBits() < 64 && |
| 434 | "Unexpectedly large index value."); |
| 435 | int64_t Index = SimplifiedAddrOp->getSExtValue() / ElemSize; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 436 | if (Index >= CDS->getNumElements()) { |
| 437 | // FIXME: For now we conservatively ignore out of bound accesses, but |
| 438 | // we're allowed to perform the optimization in this case. |
| 439 | return false; |
| 440 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 441 | |
| 442 | Constant *CV = CDS->getElementAsConstant(Index); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 443 | assert(CV && "Constant expected."); |
| 444 | SimplifiedValues[&I] = CV; |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 445 | |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 446 | return true; |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 447 | } |
Michael Zolotukhin | 31b3eaa | 2015-07-15 00:19:51 +0000 | [diff] [blame] | 448 | |
| 449 | bool visitCastInst(CastInst &I) { |
| 450 | // Propagate constants through casts. |
| 451 | Constant *COp = dyn_cast<Constant>(I.getOperand(0)); |
| 452 | if (!COp) |
| 453 | COp = SimplifiedValues.lookup(I.getOperand(0)); |
| 454 | if (COp) |
| 455 | if (Constant *C = |
| 456 | ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) { |
| 457 | SimplifiedValues[&I] = C; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | return Base::visitCastInst(I); |
| 462 | } |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 463 | |
| 464 | bool visitCmpInst(CmpInst &I) { |
| 465 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 466 | |
| 467 | // First try to handle simplified comparisons. |
| 468 | if (!isa<Constant>(LHS)) |
| 469 | if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS)) |
| 470 | LHS = SimpleLHS; |
| 471 | if (!isa<Constant>(RHS)) |
| 472 | if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS)) |
| 473 | RHS = SimpleRHS; |
| 474 | |
| 475 | if (!isa<Constant>(LHS) && !isa<Constant>(RHS)) { |
| 476 | auto SimplifiedLHS = SimplifiedAddresses.find(LHS); |
| 477 | if (SimplifiedLHS != SimplifiedAddresses.end()) { |
| 478 | auto SimplifiedRHS = SimplifiedAddresses.find(RHS); |
| 479 | if (SimplifiedRHS != SimplifiedAddresses.end()) { |
| 480 | SimplifiedAddress &LHSAddr = SimplifiedLHS->second; |
| 481 | SimplifiedAddress &RHSAddr = SimplifiedRHS->second; |
| 482 | if (LHSAddr.Base == RHSAddr.Base) { |
| 483 | LHS = LHSAddr.Offset; |
| 484 | RHS = RHSAddr.Offset; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
| 491 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 492 | if (Constant *C = ConstantExpr::getCompare(I.getPredicate(), CLHS, CRHS)) { |
| 493 | SimplifiedValues[&I] = C; |
| 494 | return true; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return Base::visitCmpInst(I); |
| 500 | } |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 501 | }; |
| 502 | } // namespace |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 503 | |
Chandler Carruth | 6c03dff | 2015-02-13 04:39:05 +0000 | [diff] [blame] | 504 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 505 | namespace { |
| 506 | struct EstimatedUnrollCost { |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 507 | /// \brief The estimated cost after unrolling. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 508 | int UnrolledCost; |
Chandler Carruth | 302a133 | 2015-02-13 02:10:56 +0000 | [diff] [blame] | 509 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 510 | /// \brief The estimated dynamic cost of executing the instructions in the |
| 511 | /// rolled form. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 512 | int RolledDynamicCost; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 513 | }; |
| 514 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 515 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 516 | /// \brief Figure out if the loop is worth full unrolling. |
| 517 | /// |
| 518 | /// Complete loop unrolling can make some loads constant, and we need to know |
| 519 | /// if that would expose any further optimization opportunities. This routine |
Michael Zolotukhin | c4e4f33 | 2015-06-11 22:17:39 +0000 | [diff] [blame] | 520 | /// estimates this optimization. It computes cost of unrolled loop |
| 521 | /// (UnrolledCost) and dynamic cost of the original loop (RolledDynamicCost). By |
| 522 | /// dynamic cost we mean that we won't count costs of blocks that are known not |
| 523 | /// to be executed (i.e. if we have a branch in the loop and we know that at the |
| 524 | /// given iteration its condition would be resolved to true, we won't add up the |
| 525 | /// cost of the 'false'-block). |
| 526 | /// \returns Optional value, holding the RolledDynamicCost and UnrolledCost. If |
| 527 | /// the analysis failed (no benefits expected from the unrolling, or the loop is |
| 528 | /// too big to analyze), the returned value is None. |
Benjamin Kramer | fcdb1c1 | 2015-08-20 09:57:22 +0000 | [diff] [blame] | 529 | static Optional<EstimatedUnrollCost> |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 530 | analyzeLoopUnrollCost(const Loop *L, unsigned TripCount, DominatorTree &DT, |
| 531 | ScalarEvolution &SE, const TargetTransformInfo &TTI, |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 532 | int MaxUnrolledLoopSize) { |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 533 | // We want to be able to scale offsets by the trip count and add more offsets |
| 534 | // to them without checking for overflows, and we already don't want to |
| 535 | // analyze *massive* trip counts, so we force the max to be reasonably small. |
| 536 | assert(UnrollMaxIterationsCountToAnalyze < (INT_MAX / 2) && |
| 537 | "The unroll iterations max is too large!"); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 538 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 539 | // Don't simulate loops with a big or unknown tripcount |
| 540 | if (!UnrollMaxIterationsCountToAnalyze || !TripCount || |
| 541 | TripCount > UnrollMaxIterationsCountToAnalyze) |
| 542 | return None; |
Chandler Carruth | a6ae877 | 2015-05-12 23:32:56 +0000 | [diff] [blame] | 543 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 544 | SmallSetVector<BasicBlock *, 16> BBWorklist; |
| 545 | DenseMap<Value *, Constant *> SimplifiedValues; |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 546 | SmallVector<std::pair<Value *, Constant *>, 4> SimplifiedInputValues; |
Chandler Carruth | 3b057b3 | 2015-02-13 03:57:40 +0000 | [diff] [blame] | 547 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 548 | // The estimated cost of the unrolled form of the loop. We try to estimate |
| 549 | // this by simplifying as much as we can while computing the estimate. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 550 | int UnrolledCost = 0; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 551 | // We also track the estimated dynamic (that is, actually executed) cost in |
| 552 | // the rolled form. This helps identify cases when the savings from unrolling |
| 553 | // aren't just exposing dead control flows, but actual reduced dynamic |
| 554 | // instructions due to the simplifications which we expect to occur after |
| 555 | // unrolling. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 556 | int RolledDynamicCost = 0; |
Chandler Carruth | 8c86375 | 2015-02-13 03:48:38 +0000 | [diff] [blame] | 557 | |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 558 | // Ensure that we don't violate the loop structure invariants relied on by |
| 559 | // this analysis. |
| 560 | assert(L->isLoopSimplifyForm() && "Must put loop into normal form first."); |
| 561 | assert(L->isLCSSAForm(DT) && |
| 562 | "Must have loops in LCSSA form to track live-out values."); |
| 563 | |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 564 | DEBUG(dbgs() << "Starting LoopUnroll profitability analysis...\n"); |
| 565 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 566 | // Simulate execution of each iteration of the loop counting instructions, |
| 567 | // which would be simplified. |
| 568 | // Since the same load will take different values on different iterations, |
| 569 | // we literally have to go through all loop's iterations. |
| 570 | for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) { |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 571 | DEBUG(dbgs() << " Analyzing iteration " << Iteration << "\n"); |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 572 | |
| 573 | // Prepare for the iteration by collecting any simplified entry or backedge |
| 574 | // inputs. |
| 575 | for (Instruction &I : *L->getHeader()) { |
| 576 | auto *PHI = dyn_cast<PHINode>(&I); |
| 577 | if (!PHI) |
| 578 | break; |
| 579 | |
| 580 | // The loop header PHI nodes must have exactly two input: one from the |
| 581 | // loop preheader and one from the loop latch. |
| 582 | assert( |
| 583 | PHI->getNumIncomingValues() == 2 && |
| 584 | "Must have an incoming value only for the preheader and the latch."); |
| 585 | |
| 586 | Value *V = PHI->getIncomingValueForBlock( |
| 587 | Iteration == 0 ? L->getLoopPreheader() : L->getLoopLatch()); |
| 588 | Constant *C = dyn_cast<Constant>(V); |
| 589 | if (Iteration != 0 && !C) |
| 590 | C = SimplifiedValues.lookup(V); |
| 591 | if (C) |
| 592 | SimplifiedInputValues.push_back({PHI, C}); |
| 593 | } |
| 594 | |
| 595 | // Now clear and re-populate the map for the next iteration. |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 596 | SimplifiedValues.clear(); |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 597 | while (!SimplifiedInputValues.empty()) |
| 598 | SimplifiedValues.insert(SimplifiedInputValues.pop_back_val()); |
| 599 | |
Michael Zolotukhin | a60bdb5 | 2015-06-08 03:28:06 +0000 | [diff] [blame] | 600 | UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, L, SE); |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 601 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 602 | BBWorklist.clear(); |
| 603 | BBWorklist.insert(L->getHeader()); |
| 604 | // Note that we *must not* cache the size, this loop grows the worklist. |
| 605 | for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) { |
| 606 | BasicBlock *BB = BBWorklist[Idx]; |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 607 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 608 | // Visit all instructions in the given basic block and try to simplify |
| 609 | // it. We don't change the actual IR, just count optimization |
| 610 | // opportunities. |
| 611 | for (Instruction &I : *BB) { |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 612 | int InstCost = TTI.getUserCost(&I); |
Chandler Carruth | 17a0496 | 2015-02-13 03:49:41 +0000 | [diff] [blame] | 613 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 614 | // Visit the instruction to analyze its loop cost after unrolling, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 615 | // and if the visitor returns false, include this instruction in the |
| 616 | // unrolled cost. |
| 617 | if (!Analyzer.visit(I)) |
| 618 | UnrolledCost += InstCost; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 619 | else { |
| 620 | DEBUG(dbgs() << " " << I |
| 621 | << " would be simplified if loop is unrolled.\n"); |
| 622 | (void)0; |
| 623 | } |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 624 | |
| 625 | // Also track this instructions expected cost when executing the rolled |
| 626 | // loop form. |
| 627 | RolledDynamicCost += InstCost; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 628 | |
| 629 | // If unrolled body turns out to be too big, bail out. |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 630 | if (UnrolledCost > MaxUnrolledLoopSize) { |
| 631 | DEBUG(dbgs() << " Exceeded threshold.. exiting.\n" |
| 632 | << " UnrolledCost: " << UnrolledCost |
| 633 | << ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize |
| 634 | << "\n"); |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 635 | return None; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 636 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 637 | } |
Chandler Carruth | 415f412 | 2015-02-13 02:17:39 +0000 | [diff] [blame] | 638 | |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 639 | TerminatorInst *TI = BB->getTerminator(); |
| 640 | |
| 641 | // Add in the live successors by first checking whether we have terminator |
| 642 | // that may be simplified based on the values simplified by this call. |
| 643 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 644 | if (BI->isConditional()) { |
| 645 | if (Constant *SimpleCond = |
| 646 | SimplifiedValues.lookup(BI->getCondition())) { |
Michael Zolotukhin | 3a7d55b | 2015-07-29 18:10:29 +0000 | [diff] [blame] | 647 | BasicBlock *Succ = nullptr; |
| 648 | // Just take the first successor if condition is undef |
| 649 | if (isa<UndefValue>(SimpleCond)) |
| 650 | Succ = BI->getSuccessor(0); |
| 651 | else |
| 652 | Succ = BI->getSuccessor( |
| 653 | cast<ConstantInt>(SimpleCond)->isZero() ? 1 : 0); |
Michael Zolotukhin | a425c9d | 2015-07-28 19:21:21 +0000 | [diff] [blame] | 654 | if (L->contains(Succ)) |
| 655 | BBWorklist.insert(Succ); |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 656 | continue; |
| 657 | } |
| 658 | } |
| 659 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 660 | if (Constant *SimpleCond = |
| 661 | SimplifiedValues.lookup(SI->getCondition())) { |
Michael Zolotukhin | 3a7d55b | 2015-07-29 18:10:29 +0000 | [diff] [blame] | 662 | BasicBlock *Succ = nullptr; |
| 663 | // Just take the first successor if condition is undef |
| 664 | if (isa<UndefValue>(SimpleCond)) |
| 665 | Succ = SI->getSuccessor(0); |
| 666 | else |
Michael Zolotukhin | 9f06ef7 | 2015-07-29 18:10:33 +0000 | [diff] [blame] | 667 | Succ = SI->findCaseValue(cast<ConstantInt>(SimpleCond)) |
| 668 | .getCaseSuccessor(); |
Michael Zolotukhin | a425c9d | 2015-07-28 19:21:21 +0000 | [diff] [blame] | 669 | if (L->contains(Succ)) |
| 670 | BBWorklist.insert(Succ); |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 671 | continue; |
| 672 | } |
| 673 | } |
| 674 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 675 | // Add BB's successors to the worklist. |
| 676 | for (BasicBlock *Succ : successors(BB)) |
| 677 | if (L->contains(Succ)) |
| 678 | BBWorklist.insert(Succ); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 679 | } |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 680 | |
| 681 | // If we found no optimization opportunities on the first iteration, we |
| 682 | // won't find them on later ones too. |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 683 | if (UnrolledCost == RolledDynamicCost) { |
| 684 | DEBUG(dbgs() << " No opportunities found.. exiting.\n" |
| 685 | << " UnrolledCost: " << UnrolledCost << "\n"); |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 686 | return None; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 687 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 688 | } |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 689 | DEBUG(dbgs() << "Analysis finished:\n" |
| 690 | << "UnrolledCost: " << UnrolledCost << ", " |
| 691 | << "RolledDynamicCost: " << RolledDynamicCost << "\n"); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 692 | return {{UnrolledCost, RolledDynamicCost}}; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 693 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 694 | |
Dan Gohman | 49d08a5 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 695 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | f765601 | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 696 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 697 | bool &NotDuplicatable, |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 698 | const TargetTransformInfo &TTI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 699 | AssumptionCache *AC) { |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 700 | SmallPtrSet<const Value *, 32> EphValues; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 701 | CodeMetrics::collectEphemeralValues(L, AC, EphValues); |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 702 | |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 703 | CodeMetrics Metrics; |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 704 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 705 | I != E; ++I) |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 706 | Metrics.analyzeBasicBlock(*I, TTI, EphValues); |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 707 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 708 | NotDuplicatable = Metrics.notDuplicatable; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 709 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 710 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 711 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 712 | // Don't allow an estimate of size zero. This would allows unrolling of loops |
| 713 | // with huge iteration counts, which is a compile time problem even if it's |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 714 | // not a problem for code quality. Also, the code using this size may assume |
| 715 | // that each loop has at least three instructions (likely a conditional |
| 716 | // branch, a comparison feeding that branch, and some kind of loop increment |
| 717 | // feeding that comparison instruction). |
| 718 | LoopSize = std::max(LoopSize, 3u); |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 719 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 720 | return LoopSize; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 723 | // Returns the loop hint metadata node with the given name (for example, |
| 724 | // "llvm.loop.unroll.count"). If no such metadata node exists, then nullptr is |
| 725 | // returned. |
Jingyue Wu | 49a766e | 2015-02-02 20:41:11 +0000 | [diff] [blame] | 726 | static MDNode *GetUnrollMetadataForLoop(const Loop *L, StringRef Name) { |
| 727 | if (MDNode *LoopID = L->getLoopID()) |
| 728 | return GetUnrollMetadata(LoopID, Name); |
| 729 | return nullptr; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 732 | // Returns true if the loop has an unroll(full) pragma. |
| 733 | static bool HasUnrollFullPragma(const Loop *L) { |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 734 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.full"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 737 | // Returns true if the loop has an unroll(enable) pragma. This metadata is used |
| 738 | // for both "#pragma unroll" and "#pragma clang loop unroll(enable)" directives. |
| 739 | static bool HasUnrollEnablePragma(const Loop *L) { |
| 740 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.enable"); |
| 741 | } |
| 742 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 743 | // Returns true if the loop has an unroll(disable) pragma. |
| 744 | static bool HasUnrollDisablePragma(const Loop *L) { |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 745 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.disable"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Kevin Qin | 715b01e | 2015-03-09 06:14:18 +0000 | [diff] [blame] | 748 | // Returns true if the loop has an runtime unroll(disable) pragma. |
| 749 | static bool HasRuntimeUnrollDisablePragma(const Loop *L) { |
| 750 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.runtime.disable"); |
| 751 | } |
| 752 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 753 | // If loop has an unroll_count pragma return the (necessarily |
| 754 | // positive) value from the pragma. Otherwise return 0. |
| 755 | static unsigned UnrollCountPragmaValue(const Loop *L) { |
Jingyue Wu | 49a766e | 2015-02-02 20:41:11 +0000 | [diff] [blame] | 756 | MDNode *MD = GetUnrollMetadataForLoop(L, "llvm.loop.unroll.count"); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 757 | if (MD) { |
| 758 | assert(MD->getNumOperands() == 2 && |
| 759 | "Unroll count hint metadata should have two operands."); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 760 | unsigned Count = |
| 761 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue(); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 762 | assert(Count >= 1 && "Unroll count must be positive."); |
| 763 | return Count; |
| 764 | } |
| 765 | return 0; |
| 766 | } |
| 767 | |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 768 | // Remove existing unroll metadata and add unroll disable metadata to |
| 769 | // indicate the loop has already been unrolled. This prevents a loop |
| 770 | // from being unrolled more than is directed by a pragma if the loop |
| 771 | // unrolling pass is run more than once (which it generally is). |
| 772 | static void SetLoopAlreadyUnrolled(Loop *L) { |
| 773 | MDNode *LoopID = L->getLoopID(); |
| 774 | if (!LoopID) return; |
| 775 | |
| 776 | // First remove any existing loop unrolling metadata. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 777 | SmallVector<Metadata *, 4> MDs; |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 778 | // Reserve first location for self reference to the LoopID metadata node. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 779 | MDs.push_back(nullptr); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 780 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 781 | bool IsUnrollMetadata = false; |
| 782 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 783 | if (MD) { |
| 784 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 785 | IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); |
| 786 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 787 | if (!IsUnrollMetadata) |
| 788 | MDs.push_back(LoopID->getOperand(i)); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | // Add unroll(disable) metadata to disable future unrolling. |
| 792 | LLVMContext &Context = L->getHeader()->getContext(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 793 | SmallVector<Metadata *, 1> DisableOperands; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 794 | DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable")); |
Mark Heffernan | f3764da | 2014-07-18 21:29:41 +0000 | [diff] [blame] | 795 | MDNode *DisableNode = MDNode::get(Context, DisableOperands); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 796 | MDs.push_back(DisableNode); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 797 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 798 | MDNode *NewLoopID = MDNode::get(Context, MDs); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 799 | // Set operand 0 to refer to the loop id itself. |
| 800 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 801 | L->setLoopID(NewLoopID); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 804 | bool LoopUnroll::canUnrollCompletely(Loop *L, unsigned Threshold, |
| 805 | unsigned PercentDynamicCostSavedThreshold, |
| 806 | unsigned DynamicCostSavingsDiscount, |
Sanjoy Das | ad714b1 | 2015-06-06 05:24:10 +0000 | [diff] [blame] | 807 | uint64_t UnrolledCost, |
| 808 | uint64_t RolledDynamicCost) { |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 809 | |
| 810 | if (Threshold == NoThreshold) { |
| 811 | DEBUG(dbgs() << " Can fully unroll, because no threshold is set.\n"); |
| 812 | return true; |
| 813 | } |
| 814 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 815 | if (UnrolledCost <= Threshold) { |
| 816 | DEBUG(dbgs() << " Can fully unroll, because unrolled cost: " |
| 817 | << UnrolledCost << "<" << Threshold << "\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 821 | assert(UnrolledCost && "UnrolledCost can't be 0 at this point."); |
| 822 | assert(RolledDynamicCost >= UnrolledCost && |
| 823 | "Cannot have a higher unrolled cost than a rolled cost!"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 824 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 825 | // Compute the percentage of the dynamic cost in the rolled form that is |
| 826 | // saved when unrolled. If unrolling dramatically reduces the estimated |
| 827 | // dynamic cost of the loop, we use a higher threshold to allow more |
| 828 | // unrolling. |
| 829 | unsigned PercentDynamicCostSaved = |
| 830 | (uint64_t)(RolledDynamicCost - UnrolledCost) * 100ull / RolledDynamicCost; |
| 831 | |
| 832 | if (PercentDynamicCostSaved >= PercentDynamicCostSavedThreshold && |
| 833 | (int64_t)UnrolledCost - (int64_t)DynamicCostSavingsDiscount <= |
| 834 | (int64_t)Threshold) { |
| 835 | DEBUG(dbgs() << " Can fully unroll, because unrolling will reduce the " |
| 836 | "expected dynamic cost by " << PercentDynamicCostSaved |
| 837 | << "% (threshold: " << PercentDynamicCostSavedThreshold |
| 838 | << "%)\n" |
| 839 | << " and the unrolled cost (" << UnrolledCost |
| 840 | << ") is less than the max threshold (" |
| 841 | << DynamicCostSavingsDiscount << ").\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 842 | return true; |
| 843 | } |
| 844 | |
| 845 | DEBUG(dbgs() << " Too large to fully unroll:\n"); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 846 | DEBUG(dbgs() << " Threshold: " << Threshold << "\n"); |
| 847 | DEBUG(dbgs() << " Max threshold: " << DynamicCostSavingsDiscount << "\n"); |
| 848 | DEBUG(dbgs() << " Percent cost saved threshold: " |
| 849 | << PercentDynamicCostSavedThreshold << "%\n"); |
| 850 | DEBUG(dbgs() << " Unrolled cost: " << UnrolledCost << "\n"); |
| 851 | DEBUG(dbgs() << " Rolled dynamic cost: " << RolledDynamicCost << "\n"); |
| 852 | DEBUG(dbgs() << " Percent cost saved: " << PercentDynamicCostSaved |
| 853 | << "\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 854 | return false; |
| 855 | } |
| 856 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 857 | unsigned LoopUnroll::selectUnrollCount( |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 858 | const Loop *L, unsigned TripCount, bool PragmaFullUnroll, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 859 | unsigned PragmaCount, const TargetTransformInfo::UnrollingPreferences &UP, |
| 860 | bool &SetExplicitly) { |
| 861 | SetExplicitly = true; |
| 862 | |
| 863 | // User-specified count (either as a command-line option or |
| 864 | // constructor parameter) has highest precedence. |
| 865 | unsigned Count = UserCount ? CurrentCount : 0; |
| 866 | |
| 867 | // If there is no user-specified count, unroll pragmas have the next |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 868 | // highest precedence. |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 869 | if (Count == 0) { |
| 870 | if (PragmaCount) { |
| 871 | Count = PragmaCount; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 872 | } else if (PragmaFullUnroll) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 873 | Count = TripCount; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (Count == 0) |
| 878 | Count = UP.Count; |
| 879 | |
| 880 | if (Count == 0) { |
| 881 | SetExplicitly = false; |
| 882 | if (TripCount == 0) |
| 883 | // Runtime trip count. |
| 884 | Count = UnrollRuntimeCount; |
| 885 | else |
| 886 | // Conservative heuristic: if we know the trip count, see if we can |
| 887 | // completely unroll (subject to the threshold, checked below); otherwise |
| 888 | // try to find greatest modulo of the trip count which is still under |
| 889 | // threshold value. |
| 890 | Count = TripCount; |
| 891 | } |
| 892 | if (TripCount && Count > TripCount) |
| 893 | return TripCount; |
| 894 | return Count; |
| 895 | } |
| 896 | |
Devang Patel | 9779e56 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 897 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 898 | if (skipOptnoneFunction(L)) |
| 899 | return false; |
| 900 | |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 901 | Function &F = *L->getHeader()->getParent(); |
| 902 | |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 903 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 904 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 905 | ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 906 | const TargetTransformInfo &TTI = |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 907 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 908 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 909 | |
Dan Gohman | 2e1f804 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 910 | BasicBlock *Header = L->getHeader(); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 911 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 912 | << "] Loop %" << Header->getName() << "\n"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 913 | |
| 914 | if (HasUnrollDisablePragma(L)) { |
| 915 | return false; |
| 916 | } |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 917 | bool PragmaFullUnroll = HasUnrollFullPragma(L); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 918 | bool PragmaEnableUnroll = HasUnrollEnablePragma(L); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 919 | unsigned PragmaCount = UnrollCountPragmaValue(L); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 920 | bool HasPragma = PragmaFullUnroll || PragmaEnableUnroll || PragmaCount > 0; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 921 | |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 922 | TargetTransformInfo::UnrollingPreferences UP; |
Chandler Carruth | 21fc195 | 2015-02-01 14:37:03 +0000 | [diff] [blame] | 923 | getUnrollingPreferences(L, TTI, UP); |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 924 | |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 925 | // Find trip count and trip multiple if count is not available |
| 926 | unsigned TripCount = 0; |
Andrew Trick | 1cabe54 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 927 | unsigned TripMultiple = 1; |
Chandler Carruth | 6666c27 | 2014-10-11 00:12:11 +0000 | [diff] [blame] | 928 | // If there are multiple exiting blocks but one of them is the latch, use the |
| 929 | // latch for the trip count estimation. Otherwise insist on a single exiting |
| 930 | // block for the trip count estimation. |
| 931 | BasicBlock *ExitingBlock = L->getLoopLatch(); |
| 932 | if (!ExitingBlock || !L->isLoopExiting(ExitingBlock)) |
| 933 | ExitingBlock = L->getExitingBlock(); |
| 934 | if (ExitingBlock) { |
| 935 | TripCount = SE->getSmallConstantTripCount(L, ExitingBlock); |
| 936 | TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 937 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 938 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 939 | // Select an initial unroll count. This may be reduced later based |
| 940 | // on size thresholds. |
| 941 | bool CountSetExplicitly; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 942 | unsigned Count = selectUnrollCount(L, TripCount, PragmaFullUnroll, |
| 943 | PragmaCount, UP, CountSetExplicitly); |
Eli Bendersky | dc6de2c | 2014-06-12 18:05:39 +0000 | [diff] [blame] | 944 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 945 | unsigned NumInlineCandidates; |
| 946 | bool notDuplicatable; |
| 947 | unsigned LoopSize = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 948 | ApproximateLoopSize(L, NumInlineCandidates, notDuplicatable, TTI, &AC); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 949 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 950 | |
| 951 | // When computing the unrolled size, note that the conditional branch on the |
| 952 | // backedge and the comparison feeding it are not replicated like the rest of |
| 953 | // the loop body (which is why 2 is subtracted). |
| 954 | uint64_t UnrolledSize = (uint64_t)(LoopSize-2) * Count + 2; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 955 | if (notDuplicatable) { |
| 956 | DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable" |
| 957 | << " instructions.\n"); |
| 958 | return false; |
| 959 | } |
| 960 | if (NumInlineCandidates != 0) { |
| 961 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
| 962 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 965 | unsigned Threshold, PartialThreshold; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 966 | unsigned PercentDynamicCostSavedThreshold; |
| 967 | unsigned DynamicCostSavingsDiscount; |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 968 | // Only use the high pragma threshold when we have a target unroll factor such |
| 969 | // as with "#pragma unroll N" or a pragma indicating full unrolling and the |
| 970 | // trip count is known. Otherwise we rely on the standard threshold to |
| 971 | // heuristically select a reasonable unroll count. |
| 972 | bool UsePragmaThreshold = |
| 973 | PragmaCount > 0 || |
| 974 | ((PragmaFullUnroll || PragmaEnableUnroll) && TripCount != 0); |
| 975 | |
| 976 | selectThresholds(L, UsePragmaThreshold, UP, Threshold, PartialThreshold, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 977 | PercentDynamicCostSavedThreshold, |
| 978 | DynamicCostSavingsDiscount); |
Benjamin Kramer | 9130cb8 | 2014-05-04 19:12:38 +0000 | [diff] [blame] | 979 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 980 | // Given Count, TripCount and thresholds determine the type of |
| 981 | // unrolling which is to be performed. |
| 982 | enum { Full = 0, Partial = 1, Runtime = 2 }; |
| 983 | int Unrolling; |
| 984 | if (TripCount && Count == TripCount) { |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 985 | Unrolling = Partial; |
| 986 | // If the loop is really small, we don't need to run an expensive analysis. |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 987 | if (canUnrollCompletely(L, Threshold, 100, DynamicCostSavingsDiscount, |
| 988 | UnrolledSize, UnrolledSize)) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 989 | Unrolling = Full; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 990 | } else { |
| 991 | // The loop isn't that small, but we still can fully unroll it if that |
| 992 | // helps to remove a significant number of instructions. |
| 993 | // To check that, run additional analysis on the loop. |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 994 | if (Optional<EstimatedUnrollCost> Cost = |
| 995 | analyzeLoopUnrollCost(L, TripCount, DT, *SE, TTI, |
| 996 | Threshold + DynamicCostSavingsDiscount)) |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 997 | if (canUnrollCompletely(L, Threshold, PercentDynamicCostSavedThreshold, |
| 998 | DynamicCostSavingsDiscount, Cost->UnrolledCost, |
| 999 | Cost->RolledDynamicCost)) { |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 1000 | Unrolling = Full; |
| 1001 | } |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 1002 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1003 | } else if (TripCount && Count < TripCount) { |
| 1004 | Unrolling = Partial; |
| 1005 | } else { |
| 1006 | Unrolling = Runtime; |
| 1007 | } |
| 1008 | |
| 1009 | // Reduce count based on the type of unrolling and the threshold values. |
| 1010 | unsigned OriginalCount = Count; |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 1011 | bool AllowRuntime = PragmaEnableUnroll || (PragmaCount > 0) || |
| 1012 | (UserRuntime ? CurrentRuntime : UP.Runtime); |
Mark Heffernan | d7ebc24 | 2015-07-13 18:26:27 +0000 | [diff] [blame] | 1013 | // Don't unroll a runtime trip count loop with unroll full pragma. |
| 1014 | if (HasRuntimeUnrollDisablePragma(L) || PragmaFullUnroll) { |
Kevin Qin | 715b01e | 2015-03-09 06:14:18 +0000 | [diff] [blame] | 1015 | AllowRuntime = false; |
| 1016 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1017 | if (Unrolling == Partial) { |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 1018 | bool AllowPartial = PragmaEnableUnroll || |
| 1019 | (UserAllowPartial ? CurrentAllowPartial : UP.Partial); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1020 | if (!AllowPartial && !CountSetExplicitly) { |
| 1021 | DEBUG(dbgs() << " will not try to unroll partially because " |
| 1022 | << "-unroll-allow-partial not given\n"); |
| 1023 | return false; |
| 1024 | } |
| 1025 | if (PartialThreshold != NoThreshold && UnrolledSize > PartialThreshold) { |
| 1026 | // Reduce unroll count to be modulo of TripCount for partial unrolling. |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 1027 | Count = (std::max(PartialThreshold, 3u)-2) / (LoopSize-2); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1028 | while (Count != 0 && TripCount % Count != 0) |
| 1029 | Count--; |
| 1030 | } |
| 1031 | } else if (Unrolling == Runtime) { |
| 1032 | if (!AllowRuntime && !CountSetExplicitly) { |
| 1033 | DEBUG(dbgs() << " will not try to unroll loop with runtime trip count " |
| 1034 | << "-unroll-runtime not given\n"); |
| 1035 | return false; |
| 1036 | } |
| 1037 | // Reduce unroll count to be the largest power-of-two factor of |
| 1038 | // the original count which satisfies the threshold limit. |
| 1039 | while (Count != 0 && UnrolledSize > PartialThreshold) { |
| 1040 | Count >>= 1; |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 1041 | UnrolledSize = (LoopSize-2) * Count + 2; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1042 | } |
| 1043 | if (Count > UP.MaxCount) |
| 1044 | Count = UP.MaxCount; |
| 1045 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
| 1046 | } |
| 1047 | |
| 1048 | if (HasPragma) { |
Mark Heffernan | 9e11244 | 2014-07-23 20:05:44 +0000 | [diff] [blame] | 1049 | if (PragmaCount != 0) |
| 1050 | // If loop has an unroll count pragma mark loop as unrolled to prevent |
| 1051 | // unrolling beyond that requested by the pragma. |
| 1052 | SetLoopAlreadyUnrolled(L); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 1053 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1054 | // Emit optimization remarks if we are unable to unroll the loop |
| 1055 | // as directed by a pragma. |
| 1056 | DebugLoc LoopLoc = L->getStartLoc(); |
| 1057 | Function *F = Header->getParent(); |
| 1058 | LLVMContext &Ctx = F->getContext(); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 1059 | if ((PragmaCount > 0) && Count != OriginalCount) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1060 | emitOptimizationRemarkMissed( |
| 1061 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 1062 | "Unable to unroll loop the number of times directed by " |
| 1063 | "unroll_count pragma because unrolled size is too large."); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 1064 | } else if (PragmaFullUnroll && !TripCount) { |
| 1065 | emitOptimizationRemarkMissed( |
| 1066 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 1067 | "Unable to fully unroll loop as directed by unroll(full) pragma " |
| 1068 | "because loop has a runtime trip count."); |
| 1069 | } else if (PragmaEnableUnroll && Count != TripCount && Count < 2) { |
| 1070 | emitOptimizationRemarkMissed( |
| 1071 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 1072 | "Unable to unroll loop as directed by unroll(enable) pragma because " |
| 1073 | "unrolled size is too large."); |
| 1074 | } else if ((PragmaFullUnroll || PragmaEnableUnroll) && TripCount && |
| 1075 | Count != TripCount) { |
| 1076 | emitOptimizationRemarkMissed( |
| 1077 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 1078 | "Unable to fully unroll loop as directed by unroll pragma because " |
| 1079 | "unrolled size is too large."); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (Unrolling != Full && Count < 2) { |
| 1084 | // Partial unrolling by 1 is a nop. For full unrolling, a factor |
| 1085 | // of 1 makes sense because loop control can be eliminated. |
| 1086 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 1089 | // Unroll the loop. |
Sanjoy Das | e178f46 | 2015-04-14 03:20:38 +0000 | [diff] [blame] | 1090 | if (!UnrollLoop(L, Count, TripCount, AllowRuntime, UP.AllowExpensiveTripCount, |
| 1091 | TripMultiple, LI, this, &LPM, &AC)) |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 1092 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 1094 | return true; |
| 1095 | } |