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" |
Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopUnrollAnalyzer.h" |
Dan Gohman | 0141c13 | 2010-07-26 18:11:16 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScalarEvolution.h" |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IntrinsicInst.h" |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Duncan Sands | 67933e6 | 2008-05-16 09:30:00 +0000 | [diff] [blame] | 37 | #include <climits> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 38 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 40 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "loop-unroll" |
| 42 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 43 | static cl::opt<unsigned> |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 44 | UnrollThreshold("unroll-threshold", cl::Hidden, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 45 | cl::desc("The baseline cost threshold for loop unrolling")); |
| 46 | |
| 47 | static cl::opt<unsigned> UnrollPercentDynamicCostSavedThreshold( |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 48 | "unroll-percent-dynamic-cost-saved-threshold", cl::Hidden, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 49 | cl::desc("The percentage of estimated dynamic cost which must be saved by " |
| 50 | "unrolling to allow unrolling up to the max threshold.")); |
| 51 | |
| 52 | static cl::opt<unsigned> UnrollDynamicCostSavingsDiscount( |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 53 | "unroll-dynamic-cost-savings-discount", cl::Hidden, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 54 | cl::desc("This is the amount discounted from the total unroll cost when " |
| 55 | "the unrolled form has a high dynamic cost savings (triggered by " |
| 56 | "the '-unroll-perecent-dynamic-cost-saved-threshold' flag).")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 57 | |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 58 | static cl::opt<unsigned> UnrollMaxIterationsCountToAnalyze( |
Chandler Carruth | 1fbc316 | 2015-02-13 05:31:46 +0000 | [diff] [blame] | 59 | "unroll-max-iteration-count-to-analyze", cl::init(0), cl::Hidden, |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 60 | cl::desc("Don't allow loop unrolling to simulate more than this number of" |
| 61 | "iterations when checking full unroll profitability")); |
| 62 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 63 | static cl::opt<unsigned> |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 64 | UnrollCount("unroll-count", cl::Hidden, |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 65 | cl::desc("Use this unroll count for all loops including those with " |
| 66 | "unroll_count pragma values, for testing purposes")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 67 | |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 69 | UnrollAllowPartial("unroll-allow-partial", cl::Hidden, |
Matthijs Kooijman | 98b5c16 | 2008-07-29 13:21:23 +0000 | [diff] [blame] | 70 | cl::desc("Allows loops to be partially unrolled until " |
| 71 | "-unroll-threshold loop size is reached.")); |
| 72 | |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 74 | UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::Hidden, |
Andrew Trick | d04d1529 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 75 | cl::desc("Unroll loops with run-time trip counts")); |
| 76 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 77 | static cl::opt<unsigned> |
| 78 | PragmaUnrollThreshold("pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden, |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 79 | cl::desc("Unrolled size limit for loops with an unroll(full) or " |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 80 | "unroll_count pragma.")); |
| 81 | |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 82 | |
| 83 | /// A magic value for use with the Threshold parameter to indicate |
| 84 | /// that the loop unroll should be performed regardless of how much |
| 85 | /// code expansion would result. |
| 86 | static const unsigned NoThreshold = UINT_MAX; |
| 87 | |
| 88 | /// Default unroll count for loops with run-time trip count if |
| 89 | /// -unroll-count is not set |
| 90 | static const unsigned DefaultUnrollRuntimeCount = 8; |
| 91 | |
| 92 | /// Gather the various unrolling parameters based on the defaults, compiler |
| 93 | /// flags, TTI overrides, pragmas, and user specified parameters. |
| 94 | static TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences( |
| 95 | Loop *L, const TargetTransformInfo &TTI, Optional<unsigned> UserThreshold, |
| 96 | Optional<unsigned> UserCount, Optional<bool> UserAllowPartial, |
| 97 | Optional<bool> UserRuntime, unsigned PragmaCount, bool PragmaFullUnroll, |
| 98 | bool PragmaEnableUnroll, unsigned TripCount) { |
| 99 | TargetTransformInfo::UnrollingPreferences UP; |
| 100 | |
| 101 | // Set up the defaults |
| 102 | UP.Threshold = 150; |
| 103 | UP.PercentDynamicCostSavedThreshold = 20; |
| 104 | UP.DynamicCostSavingsDiscount = 2000; |
| 105 | UP.OptSizeThreshold = 50; |
| 106 | UP.PartialThreshold = UP.Threshold; |
| 107 | UP.PartialOptSizeThreshold = UP.OptSizeThreshold; |
| 108 | UP.Count = 0; |
| 109 | UP.MaxCount = UINT_MAX; |
| 110 | UP.Partial = false; |
| 111 | UP.Runtime = false; |
| 112 | UP.AllowExpensiveTripCount = false; |
| 113 | |
| 114 | // Override with any target specific settings |
| 115 | TTI.getUnrollingPreferences(L, UP); |
| 116 | |
| 117 | // Apply size attributes |
| 118 | if (L->getHeader()->getParent()->optForSize()) { |
| 119 | UP.Threshold = UP.OptSizeThreshold; |
| 120 | UP.PartialThreshold = UP.PartialOptSizeThreshold; |
| 121 | } |
| 122 | |
| 123 | // Apply unroll count pragmas |
| 124 | if (PragmaCount) |
| 125 | UP.Count = PragmaCount; |
| 126 | else if (PragmaFullUnroll) |
| 127 | UP.Count = TripCount; |
| 128 | |
| 129 | // Apply any user values specified by cl::opt |
| 130 | if (UnrollThreshold.getNumOccurrences() > 0) { |
| 131 | UP.Threshold = UnrollThreshold; |
| 132 | UP.PartialThreshold = UnrollThreshold; |
| 133 | } |
| 134 | if (UnrollPercentDynamicCostSavedThreshold.getNumOccurrences() > 0) |
| 135 | UP.PercentDynamicCostSavedThreshold = |
| 136 | UnrollPercentDynamicCostSavedThreshold; |
| 137 | if (UnrollDynamicCostSavingsDiscount.getNumOccurrences() > 0) |
| 138 | UP.DynamicCostSavingsDiscount = UnrollDynamicCostSavingsDiscount; |
| 139 | if (UnrollCount.getNumOccurrences() > 0) |
| 140 | UP.Count = UnrollCount; |
| 141 | if (UnrollAllowPartial.getNumOccurrences() > 0) |
| 142 | UP.Partial = UnrollAllowPartial; |
| 143 | if (UnrollRuntime.getNumOccurrences() > 0) |
| 144 | UP.Runtime = UnrollRuntime; |
| 145 | |
| 146 | // Apply user values provided by argument |
| 147 | if (UserThreshold.hasValue()) { |
| 148 | UP.Threshold = *UserThreshold; |
| 149 | UP.PartialThreshold = *UserThreshold; |
| 150 | } |
| 151 | if (UserCount.hasValue()) |
| 152 | UP.Count = *UserCount; |
| 153 | if (UserAllowPartial.hasValue()) |
| 154 | UP.Partial = *UserAllowPartial; |
| 155 | if (UserRuntime.hasValue()) |
| 156 | UP.Runtime = *UserRuntime; |
| 157 | |
| 158 | if (PragmaCount > 0 || |
| 159 | ((PragmaFullUnroll || PragmaEnableUnroll) && TripCount != 0)) { |
| 160 | // If the loop has an unrolling pragma, we want to be more aggressive with |
| 161 | // unrolling limits. Set thresholds to at least the PragmaTheshold value |
| 162 | // which is larger than the default limits. |
| 163 | if (UP.Threshold != NoThreshold) |
| 164 | UP.Threshold = std::max<unsigned>(UP.Threshold, PragmaUnrollThreshold); |
| 165 | if (UP.PartialThreshold != NoThreshold) |
| 166 | UP.PartialThreshold = |
| 167 | std::max<unsigned>(UP.PartialThreshold, PragmaUnrollThreshold); |
| 168 | } |
| 169 | |
| 170 | return UP; |
| 171 | } |
| 172 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 173 | namespace { |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 174 | struct EstimatedUnrollCost { |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 175 | /// \brief The estimated cost after unrolling. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 176 | int UnrolledCost; |
Chandler Carruth | 302a133 | 2015-02-13 02:10:56 +0000 | [diff] [blame] | 177 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 178 | /// \brief The estimated dynamic cost of executing the instructions in the |
| 179 | /// rolled form. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 180 | int RolledDynamicCost; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 181 | }; |
| 182 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 183 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 184 | /// \brief Figure out if the loop is worth full unrolling. |
| 185 | /// |
| 186 | /// Complete loop unrolling can make some loads constant, and we need to know |
| 187 | /// if that would expose any further optimization opportunities. This routine |
Michael Zolotukhin | c4e4f33 | 2015-06-11 22:17:39 +0000 | [diff] [blame] | 188 | /// estimates this optimization. It computes cost of unrolled loop |
| 189 | /// (UnrolledCost) and dynamic cost of the original loop (RolledDynamicCost). By |
| 190 | /// dynamic cost we mean that we won't count costs of blocks that are known not |
| 191 | /// to be executed (i.e. if we have a branch in the loop and we know that at the |
| 192 | /// given iteration its condition would be resolved to true, we won't add up the |
| 193 | /// cost of the 'false'-block). |
| 194 | /// \returns Optional value, holding the RolledDynamicCost and UnrolledCost. If |
| 195 | /// the analysis failed (no benefits expected from the unrolling, or the loop is |
| 196 | /// too big to analyze), the returned value is None. |
Benjamin Kramer | fcdb1c1 | 2015-08-20 09:57:22 +0000 | [diff] [blame] | 197 | static Optional<EstimatedUnrollCost> |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 198 | analyzeLoopUnrollCost(const Loop *L, unsigned TripCount, DominatorTree &DT, |
| 199 | ScalarEvolution &SE, const TargetTransformInfo &TTI, |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 200 | int MaxUnrolledLoopSize) { |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 201 | // We want to be able to scale offsets by the trip count and add more offsets |
| 202 | // to them without checking for overflows, and we already don't want to |
| 203 | // analyze *massive* trip counts, so we force the max to be reasonably small. |
| 204 | assert(UnrollMaxIterationsCountToAnalyze < (INT_MAX / 2) && |
| 205 | "The unroll iterations max is too large!"); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 206 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 207 | // Don't simulate loops with a big or unknown tripcount |
| 208 | if (!UnrollMaxIterationsCountToAnalyze || !TripCount || |
| 209 | TripCount > UnrollMaxIterationsCountToAnalyze) |
| 210 | return None; |
Chandler Carruth | a6ae877 | 2015-05-12 23:32:56 +0000 | [diff] [blame] | 211 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 212 | SmallSetVector<BasicBlock *, 16> BBWorklist; |
| 213 | DenseMap<Value *, Constant *> SimplifiedValues; |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 214 | SmallVector<std::pair<Value *, Constant *>, 4> SimplifiedInputValues; |
Chandler Carruth | 3b057b3 | 2015-02-13 03:57:40 +0000 | [diff] [blame] | 215 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 216 | // The estimated cost of the unrolled form of the loop. We try to estimate |
| 217 | // this by simplifying as much as we can while computing the estimate. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 218 | int UnrolledCost = 0; |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 219 | // We also track the estimated dynamic (that is, actually executed) cost in |
| 220 | // the rolled form. This helps identify cases when the savings from unrolling |
| 221 | // aren't just exposing dead control flows, but actual reduced dynamic |
| 222 | // instructions due to the simplifications which we expect to occur after |
| 223 | // unrolling. |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 224 | int RolledDynamicCost = 0; |
Chandler Carruth | 8c86375 | 2015-02-13 03:48:38 +0000 | [diff] [blame] | 225 | |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 226 | // Ensure that we don't violate the loop structure invariants relied on by |
| 227 | // this analysis. |
| 228 | assert(L->isLoopSimplifyForm() && "Must put loop into normal form first."); |
| 229 | assert(L->isLCSSAForm(DT) && |
| 230 | "Must have loops in LCSSA form to track live-out values."); |
| 231 | |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 232 | DEBUG(dbgs() << "Starting LoopUnroll profitability analysis...\n"); |
| 233 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 234 | // Simulate execution of each iteration of the loop counting instructions, |
| 235 | // which would be simplified. |
| 236 | // Since the same load will take different values on different iterations, |
| 237 | // we literally have to go through all loop's iterations. |
| 238 | for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) { |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 239 | DEBUG(dbgs() << " Analyzing iteration " << Iteration << "\n"); |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 240 | |
| 241 | // Prepare for the iteration by collecting any simplified entry or backedge |
| 242 | // inputs. |
| 243 | for (Instruction &I : *L->getHeader()) { |
| 244 | auto *PHI = dyn_cast<PHINode>(&I); |
| 245 | if (!PHI) |
| 246 | break; |
| 247 | |
| 248 | // The loop header PHI nodes must have exactly two input: one from the |
| 249 | // loop preheader and one from the loop latch. |
| 250 | assert( |
| 251 | PHI->getNumIncomingValues() == 2 && |
| 252 | "Must have an incoming value only for the preheader and the latch."); |
| 253 | |
| 254 | Value *V = PHI->getIncomingValueForBlock( |
| 255 | Iteration == 0 ? L->getLoopPreheader() : L->getLoopLatch()); |
| 256 | Constant *C = dyn_cast<Constant>(V); |
| 257 | if (Iteration != 0 && !C) |
| 258 | C = SimplifiedValues.lookup(V); |
| 259 | if (C) |
| 260 | SimplifiedInputValues.push_back({PHI, C}); |
| 261 | } |
| 262 | |
| 263 | // Now clear and re-populate the map for the next iteration. |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 264 | SimplifiedValues.clear(); |
Chandler Carruth | 87adb7a | 2015-08-03 20:32:27 +0000 | [diff] [blame] | 265 | while (!SimplifiedInputValues.empty()) |
| 266 | SimplifiedValues.insert(SimplifiedInputValues.pop_back_val()); |
| 267 | |
Michael Zolotukhin | 9f520eb | 2016-02-26 02:57:05 +0000 | [diff] [blame] | 268 | UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, SE, L); |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 269 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 270 | BBWorklist.clear(); |
| 271 | BBWorklist.insert(L->getHeader()); |
| 272 | // Note that we *must not* cache the size, this loop grows the worklist. |
| 273 | for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) { |
| 274 | BasicBlock *BB = BBWorklist[Idx]; |
Chandler Carruth | f174a15 | 2015-05-22 02:47:29 +0000 | [diff] [blame] | 275 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 276 | // Visit all instructions in the given basic block and try to simplify |
| 277 | // it. We don't change the actual IR, just count optimization |
| 278 | // opportunities. |
| 279 | for (Instruction &I : *BB) { |
Chandler Carruth | b2fda0d | 2015-08-05 18:46:21 +0000 | [diff] [blame] | 280 | int InstCost = TTI.getUserCost(&I); |
Chandler Carruth | 17a0496 | 2015-02-13 03:49:41 +0000 | [diff] [blame] | 281 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 282 | // Visit the instruction to analyze its loop cost after unrolling, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 283 | // and if the visitor returns false, include this instruction in the |
| 284 | // unrolled cost. |
| 285 | if (!Analyzer.visit(I)) |
| 286 | UnrolledCost += InstCost; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 287 | else { |
| 288 | DEBUG(dbgs() << " " << I |
| 289 | << " would be simplified if loop is unrolled.\n"); |
| 290 | (void)0; |
| 291 | } |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 292 | |
| 293 | // Also track this instructions expected cost when executing the rolled |
| 294 | // loop form. |
| 295 | RolledDynamicCost += InstCost; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 296 | |
| 297 | // If unrolled body turns out to be too big, bail out. |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 298 | if (UnrolledCost > MaxUnrolledLoopSize) { |
| 299 | DEBUG(dbgs() << " Exceeded threshold.. exiting.\n" |
| 300 | << " UnrolledCost: " << UnrolledCost |
| 301 | << ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize |
| 302 | << "\n"); |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 303 | return None; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 304 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 305 | } |
Chandler Carruth | 415f412 | 2015-02-13 02:17:39 +0000 | [diff] [blame] | 306 | |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 307 | TerminatorInst *TI = BB->getTerminator(); |
| 308 | |
| 309 | // Add in the live successors by first checking whether we have terminator |
| 310 | // that may be simplified based on the values simplified by this call. |
| 311 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 312 | if (BI->isConditional()) { |
| 313 | if (Constant *SimpleCond = |
| 314 | SimplifiedValues.lookup(BI->getCondition())) { |
Michael Zolotukhin | 3a7d55b | 2015-07-29 18:10:29 +0000 | [diff] [blame] | 315 | BasicBlock *Succ = nullptr; |
| 316 | // Just take the first successor if condition is undef |
| 317 | if (isa<UndefValue>(SimpleCond)) |
| 318 | Succ = BI->getSuccessor(0); |
| 319 | else |
| 320 | Succ = BI->getSuccessor( |
| 321 | cast<ConstantInt>(SimpleCond)->isZero() ? 1 : 0); |
Michael Zolotukhin | a425c9d | 2015-07-28 19:21:21 +0000 | [diff] [blame] | 322 | if (L->contains(Succ)) |
| 323 | BBWorklist.insert(Succ); |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 324 | continue; |
| 325 | } |
| 326 | } |
| 327 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 328 | if (Constant *SimpleCond = |
| 329 | SimplifiedValues.lookup(SI->getCondition())) { |
Michael Zolotukhin | 3a7d55b | 2015-07-29 18:10:29 +0000 | [diff] [blame] | 330 | BasicBlock *Succ = nullptr; |
| 331 | // Just take the first successor if condition is undef |
| 332 | if (isa<UndefValue>(SimpleCond)) |
| 333 | Succ = SI->getSuccessor(0); |
| 334 | else |
Michael Zolotukhin | 9f06ef7 | 2015-07-29 18:10:33 +0000 | [diff] [blame] | 335 | Succ = SI->findCaseValue(cast<ConstantInt>(SimpleCond)) |
| 336 | .getCaseSuccessor(); |
Michael Zolotukhin | a425c9d | 2015-07-28 19:21:21 +0000 | [diff] [blame] | 337 | if (L->contains(Succ)) |
| 338 | BBWorklist.insert(Succ); |
Michael Zolotukhin | 57776b8 | 2015-07-24 01:53:04 +0000 | [diff] [blame] | 339 | continue; |
| 340 | } |
| 341 | } |
| 342 | |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 343 | // Add BB's successors to the worklist. |
| 344 | for (BasicBlock *Succ : successors(BB)) |
| 345 | if (L->contains(Succ)) |
| 346 | BBWorklist.insert(Succ); |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 347 | } |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 348 | |
| 349 | // If we found no optimization opportunities on the first iteration, we |
| 350 | // won't find them on later ones too. |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 351 | if (UnrolledCost == RolledDynamicCost) { |
| 352 | DEBUG(dbgs() << " No opportunities found.. exiting.\n" |
| 353 | << " UnrolledCost: " << UnrolledCost << "\n"); |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 354 | return None; |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 355 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 356 | } |
Michael Zolotukhin | 80d13ba | 2015-07-28 20:07:29 +0000 | [diff] [blame] | 357 | DEBUG(dbgs() << "Analysis finished:\n" |
| 358 | << "UnrolledCost: " << UnrolledCost << ", " |
| 359 | << "RolledDynamicCost: " << RolledDynamicCost << "\n"); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 360 | return {{UnrolledCost, RolledDynamicCost}}; |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 361 | } |
Michael Zolotukhin | a9aadd2 | 2015-02-05 02:34:00 +0000 | [diff] [blame] | 362 | |
Dan Gohman | 49d08a5 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 363 | /// ApproximateLoopSize - Approximate the size of the loop. |
Andrew Trick | f765601 | 2011-10-01 01:39:05 +0000 | [diff] [blame] | 364 | static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 365 | bool &NotDuplicatable, bool &Convergent, |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 366 | const TargetTransformInfo &TTI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 367 | AssumptionCache *AC) { |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 368 | SmallPtrSet<const Value *, 32> EphValues; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 369 | CodeMetrics::collectEphemeralValues(L, AC, EphValues); |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 370 | |
Dan Gohman | 969e83a | 2009-10-31 14:54:17 +0000 | [diff] [blame] | 371 | CodeMetrics Metrics; |
Sanjay Patel | 5c96723 | 2016-03-08 19:06:12 +0000 | [diff] [blame] | 372 | for (BasicBlock *BB : L->blocks()) |
| 373 | Metrics.analyzeBasicBlock(BB, TTI, EphValues); |
Owen Anderson | 04cf3fd | 2010-09-09 20:32:23 +0000 | [diff] [blame] | 374 | NumCalls = Metrics.NumInlineCandidates; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 375 | NotDuplicatable = Metrics.notDuplicatable; |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 376 | Convergent = Metrics.convergent; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 377 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 378 | unsigned LoopSize = Metrics.NumInsts; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 379 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 380 | // Don't allow an estimate of size zero. This would allows unrolling of loops |
| 381 | // 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] | 382 | // not a problem for code quality. Also, the code using this size may assume |
| 383 | // that each loop has at least three instructions (likely a conditional |
| 384 | // branch, a comparison feeding that branch, and some kind of loop increment |
| 385 | // feeding that comparison instruction). |
| 386 | LoopSize = std::max(LoopSize, 3u); |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 387 | |
Owen Anderson | 62ea1b7 | 2010-09-09 19:07:31 +0000 | [diff] [blame] | 388 | return LoopSize; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 391 | // Returns the loop hint metadata node with the given name (for example, |
| 392 | // "llvm.loop.unroll.count"). If no such metadata node exists, then nullptr is |
| 393 | // returned. |
Jingyue Wu | 49a766e | 2015-02-02 20:41:11 +0000 | [diff] [blame] | 394 | static MDNode *GetUnrollMetadataForLoop(const Loop *L, StringRef Name) { |
| 395 | if (MDNode *LoopID = L->getLoopID()) |
| 396 | return GetUnrollMetadata(LoopID, Name); |
| 397 | return nullptr; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 400 | // Returns true if the loop has an unroll(full) pragma. |
| 401 | static bool HasUnrollFullPragma(const Loop *L) { |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 402 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.full"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 405 | // Returns true if the loop has an unroll(enable) pragma. This metadata is used |
| 406 | // for both "#pragma unroll" and "#pragma clang loop unroll(enable)" directives. |
| 407 | static bool HasUnrollEnablePragma(const Loop *L) { |
| 408 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.enable"); |
| 409 | } |
| 410 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 411 | // Returns true if the loop has an unroll(disable) pragma. |
| 412 | static bool HasUnrollDisablePragma(const Loop *L) { |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 413 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.disable"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Kevin Qin | 715b01e | 2015-03-09 06:14:18 +0000 | [diff] [blame] | 416 | // Returns true if the loop has an runtime unroll(disable) pragma. |
| 417 | static bool HasRuntimeUnrollDisablePragma(const Loop *L) { |
| 418 | return GetUnrollMetadataForLoop(L, "llvm.loop.unroll.runtime.disable"); |
| 419 | } |
| 420 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 421 | // If loop has an unroll_count pragma return the (necessarily |
| 422 | // positive) value from the pragma. Otherwise return 0. |
| 423 | static unsigned UnrollCountPragmaValue(const Loop *L) { |
Jingyue Wu | 49a766e | 2015-02-02 20:41:11 +0000 | [diff] [blame] | 424 | MDNode *MD = GetUnrollMetadataForLoop(L, "llvm.loop.unroll.count"); |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 425 | if (MD) { |
| 426 | assert(MD->getNumOperands() == 2 && |
| 427 | "Unroll count hint metadata should have two operands."); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 428 | unsigned Count = |
| 429 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue(); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 430 | assert(Count >= 1 && "Unroll count must be positive."); |
| 431 | return Count; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 436 | // Remove existing unroll metadata and add unroll disable metadata to |
| 437 | // indicate the loop has already been unrolled. This prevents a loop |
| 438 | // from being unrolled more than is directed by a pragma if the loop |
| 439 | // unrolling pass is run more than once (which it generally is). |
| 440 | static void SetLoopAlreadyUnrolled(Loop *L) { |
| 441 | MDNode *LoopID = L->getLoopID(); |
| 442 | if (!LoopID) return; |
| 443 | |
| 444 | // First remove any existing loop unrolling metadata. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 445 | SmallVector<Metadata *, 4> MDs; |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 446 | // 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] | 447 | MDs.push_back(nullptr); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 448 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 449 | bool IsUnrollMetadata = false; |
| 450 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 451 | if (MD) { |
| 452 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 453 | IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); |
| 454 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 455 | if (!IsUnrollMetadata) |
| 456 | MDs.push_back(LoopID->getOperand(i)); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // Add unroll(disable) metadata to disable future unrolling. |
| 460 | LLVMContext &Context = L->getHeader()->getContext(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 461 | SmallVector<Metadata *, 1> DisableOperands; |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 462 | DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable")); |
Mark Heffernan | f3764da | 2014-07-18 21:29:41 +0000 | [diff] [blame] | 463 | MDNode *DisableNode = MDNode::get(Context, DisableOperands); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 464 | MDs.push_back(DisableNode); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 465 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 466 | MDNode *NewLoopID = MDNode::get(Context, MDs); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 467 | // Set operand 0 to refer to the loop id itself. |
| 468 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 469 | L->setLoopID(NewLoopID); |
Mark Heffernan | 053a686 | 2014-07-18 21:04:33 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Justin Bogner | 921b04e | 2016-01-12 01:06:32 +0000 | [diff] [blame] | 472 | static bool canUnrollCompletely(Loop *L, unsigned Threshold, |
| 473 | unsigned PercentDynamicCostSavedThreshold, |
| 474 | unsigned DynamicCostSavingsDiscount, |
| 475 | uint64_t UnrolledCost, |
| 476 | uint64_t RolledDynamicCost) { |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 477 | if (Threshold == NoThreshold) { |
| 478 | DEBUG(dbgs() << " Can fully unroll, because no threshold is set.\n"); |
| 479 | return true; |
| 480 | } |
| 481 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 482 | if (UnrolledCost <= Threshold) { |
| 483 | DEBUG(dbgs() << " Can fully unroll, because unrolled cost: " |
| 484 | << UnrolledCost << "<" << Threshold << "\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 485 | return true; |
| 486 | } |
| 487 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 488 | assert(UnrolledCost && "UnrolledCost can't be 0 at this point."); |
| 489 | assert(RolledDynamicCost >= UnrolledCost && |
| 490 | "Cannot have a higher unrolled cost than a rolled cost!"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 491 | |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 492 | // Compute the percentage of the dynamic cost in the rolled form that is |
| 493 | // saved when unrolled. If unrolling dramatically reduces the estimated |
| 494 | // dynamic cost of the loop, we use a higher threshold to allow more |
| 495 | // unrolling. |
| 496 | unsigned PercentDynamicCostSaved = |
| 497 | (uint64_t)(RolledDynamicCost - UnrolledCost) * 100ull / RolledDynamicCost; |
| 498 | |
| 499 | if (PercentDynamicCostSaved >= PercentDynamicCostSavedThreshold && |
| 500 | (int64_t)UnrolledCost - (int64_t)DynamicCostSavingsDiscount <= |
| 501 | (int64_t)Threshold) { |
| 502 | DEBUG(dbgs() << " Can fully unroll, because unrolling will reduce the " |
| 503 | "expected dynamic cost by " << PercentDynamicCostSaved |
| 504 | << "% (threshold: " << PercentDynamicCostSavedThreshold |
| 505 | << "%)\n" |
| 506 | << " and the unrolled cost (" << UnrolledCost |
| 507 | << ") is less than the max threshold (" |
| 508 | << DynamicCostSavingsDiscount << ").\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | |
| 512 | DEBUG(dbgs() << " Too large to fully unroll:\n"); |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 513 | DEBUG(dbgs() << " Threshold: " << Threshold << "\n"); |
| 514 | DEBUG(dbgs() << " Max threshold: " << DynamicCostSavingsDiscount << "\n"); |
| 515 | DEBUG(dbgs() << " Percent cost saved threshold: " |
| 516 | << PercentDynamicCostSavedThreshold << "%\n"); |
| 517 | DEBUG(dbgs() << " Unrolled cost: " << UnrolledCost << "\n"); |
| 518 | DEBUG(dbgs() << " Rolled dynamic cost: " << RolledDynamicCost << "\n"); |
| 519 | DEBUG(dbgs() << " Percent cost saved: " << PercentDynamicCostSaved |
| 520 | << "\n"); |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 521 | return false; |
| 522 | } |
| 523 | |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 524 | static bool tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, |
| 525 | ScalarEvolution *SE, const TargetTransformInfo &TTI, |
| 526 | AssumptionCache &AC, bool PreserveLCSSA, |
| 527 | Optional<unsigned> ProvidedCount, |
| 528 | Optional<unsigned> ProvidedThreshold, |
| 529 | Optional<bool> ProvidedAllowPartial, |
| 530 | Optional<bool> ProvidedRuntime) { |
Dan Gohman | 2e1f804 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 531 | BasicBlock *Header = L->getHeader(); |
David Greene | e0b9789 | 2010-01-05 01:27:44 +0000 | [diff] [blame] | 532 | DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 533 | << "] Loop %" << Header->getName() << "\n"); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 534 | |
| 535 | if (HasUnrollDisablePragma(L)) { |
| 536 | return false; |
| 537 | } |
Mark Heffernan | e6b4ba1 | 2014-07-23 17:31:37 +0000 | [diff] [blame] | 538 | bool PragmaFullUnroll = HasUnrollFullPragma(L); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 539 | bool PragmaEnableUnroll = HasUnrollEnablePragma(L); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 540 | unsigned PragmaCount = UnrollCountPragmaValue(L); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 541 | bool HasPragma = PragmaFullUnroll || PragmaEnableUnroll || PragmaCount > 0; |
Andrew Trick | 279e7a6 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 542 | |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 543 | // Find trip count and trip multiple if count is not available |
| 544 | unsigned TripCount = 0; |
Andrew Trick | 1cabe54 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 545 | unsigned TripMultiple = 1; |
Chandler Carruth | 6666c27 | 2014-10-11 00:12:11 +0000 | [diff] [blame] | 546 | // If there are multiple exiting blocks but one of them is the latch, use the |
| 547 | // latch for the trip count estimation. Otherwise insist on a single exiting |
| 548 | // block for the trip count estimation. |
| 549 | BasicBlock *ExitingBlock = L->getLoopLatch(); |
| 550 | if (!ExitingBlock || !L->isLoopExiting(ExitingBlock)) |
| 551 | ExitingBlock = L->getExitingBlock(); |
| 552 | if (ExitingBlock) { |
| 553 | TripCount = SE->getSmallConstantTripCount(L, ExitingBlock); |
| 554 | TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock); |
Andrew Trick | 2b6860f | 2011-08-11 23:36:16 +0000 | [diff] [blame] | 555 | } |
Hal Finkel | 8f2e700 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 556 | |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 557 | TargetTransformInfo::UnrollingPreferences UP = gatherUnrollingPreferences( |
| 558 | L, TTI, ProvidedThreshold, ProvidedCount, ProvidedAllowPartial, |
| 559 | ProvidedRuntime, PragmaCount, PragmaFullUnroll, PragmaEnableUnroll, |
| 560 | TripCount); |
| 561 | |
| 562 | unsigned Count = UP.Count; |
| 563 | bool CountSetExplicitly = Count != 0; |
| 564 | // Use a heuristic count if we didn't set anything explicitly. |
| 565 | if (!CountSetExplicitly) |
| 566 | Count = TripCount == 0 ? DefaultUnrollRuntimeCount : TripCount; |
| 567 | if (TripCount && Count > TripCount) |
| 568 | Count = TripCount; |
Eli Bendersky | dc6de2c | 2014-06-12 18:05:39 +0000 | [diff] [blame] | 569 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 570 | unsigned NumInlineCandidates; |
Sanjay Patel | f831fdb | 2016-03-08 19:07:42 +0000 | [diff] [blame] | 571 | bool NotDuplicatable; |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 572 | bool Convergent; |
| 573 | unsigned LoopSize = ApproximateLoopSize( |
| 574 | L, NumInlineCandidates, NotDuplicatable, Convergent, TTI, &AC); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 575 | DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 576 | |
| 577 | // When computing the unrolled size, note that the conditional branch on the |
| 578 | // backedge and the comparison feeding it are not replicated like the rest of |
| 579 | // the loop body (which is why 2 is subtracted). |
| 580 | uint64_t UnrolledSize = (uint64_t)(LoopSize-2) * Count + 2; |
Sanjay Patel | f831fdb | 2016-03-08 19:07:42 +0000 | [diff] [blame] | 581 | if (NotDuplicatable) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 582 | DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable" |
| 583 | << " instructions.\n"); |
| 584 | return false; |
| 585 | } |
| 586 | if (NumInlineCandidates != 0) { |
| 587 | DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); |
| 588 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 591 | // Given Count, TripCount and thresholds determine the type of |
| 592 | // unrolling which is to be performed. |
| 593 | enum { Full = 0, Partial = 1, Runtime = 2 }; |
| 594 | int Unrolling; |
| 595 | if (TripCount && Count == TripCount) { |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 596 | Unrolling = Partial; |
| 597 | // If the loop is really small, we don't need to run an expensive analysis. |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 598 | if (canUnrollCompletely(L, UP.Threshold, 100, UP.DynamicCostSavingsDiscount, |
Chandler Carruth | 9dabd14 | 2015-06-05 17:01:43 +0000 | [diff] [blame] | 599 | UnrolledSize, UnrolledSize)) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 600 | Unrolling = Full; |
Michael Zolotukhin | 8c68171 | 2015-05-12 17:20:03 +0000 | [diff] [blame] | 601 | } else { |
| 602 | // The loop isn't that small, but we still can fully unroll it if that |
| 603 | // helps to remove a significant number of instructions. |
| 604 | // To check that, run additional analysis on the loop. |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 605 | if (Optional<EstimatedUnrollCost> Cost = analyzeLoopUnrollCost( |
| 606 | L, TripCount, DT, *SE, TTI, |
| 607 | UP.Threshold + UP.DynamicCostSavingsDiscount)) |
| 608 | if (canUnrollCompletely(L, UP.Threshold, |
| 609 | UP.PercentDynamicCostSavedThreshold, |
| 610 | UP.DynamicCostSavingsDiscount, |
| 611 | Cost->UnrolledCost, Cost->RolledDynamicCost)) { |
Chandler Carruth | 0215608 | 2015-05-22 17:41:35 +0000 | [diff] [blame] | 612 | Unrolling = Full; |
| 613 | } |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 614 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 615 | } else if (TripCount && Count < TripCount) { |
| 616 | Unrolling = Partial; |
| 617 | } else { |
| 618 | Unrolling = Runtime; |
| 619 | } |
| 620 | |
| 621 | // Reduce count based on the type of unrolling and the threshold values. |
| 622 | unsigned OriginalCount = Count; |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 623 | bool AllowRuntime = PragmaEnableUnroll || (PragmaCount > 0) || UP.Runtime; |
Mark Heffernan | d7ebc24 | 2015-07-13 18:26:27 +0000 | [diff] [blame] | 624 | // Don't unroll a runtime trip count loop with unroll full pragma. |
| 625 | if (HasRuntimeUnrollDisablePragma(L) || PragmaFullUnroll) { |
Kevin Qin | 715b01e | 2015-03-09 06:14:18 +0000 | [diff] [blame] | 626 | AllowRuntime = false; |
| 627 | } |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 628 | bool DecreasedCountDueToConvergence = false; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 629 | if (Unrolling == Partial) { |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 630 | bool AllowPartial = PragmaEnableUnroll || UP.Partial; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 631 | if (!AllowPartial && !CountSetExplicitly) { |
| 632 | DEBUG(dbgs() << " will not try to unroll partially because " |
| 633 | << "-unroll-allow-partial not given\n"); |
| 634 | return false; |
| 635 | } |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 636 | if (UP.PartialThreshold != NoThreshold && |
| 637 | UnrolledSize > UP.PartialThreshold) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 638 | // Reduce unroll count to be modulo of TripCount for partial unrolling. |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 639 | Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 640 | while (Count != 0 && TripCount % Count != 0) |
| 641 | Count--; |
Fiona Glaser | 16332ba | 2016-04-06 16:43:45 +0000 | [diff] [blame] | 642 | if (AllowRuntime && Count <= 1) { |
Zia Ansari | a82a58a4 | 2016-04-04 19:24:46 +0000 | [diff] [blame] | 643 | // If there is no Count that is modulo of TripCount, set Count to |
| 644 | // largest power-of-two factor that satisfies the threshold limit. |
Fiona Glaser | 16332ba | 2016-04-06 16:43:45 +0000 | [diff] [blame] | 645 | // As we'll create fixup loop, do the type of unrolling only if |
| 646 | // runtime unrolling is allowed. |
| 647 | Count = DefaultUnrollRuntimeCount; |
Zia Ansari | a82a58a4 | 2016-04-04 19:24:46 +0000 | [diff] [blame] | 648 | UnrolledSize = (LoopSize - 2) * Count + 2; |
| 649 | while (Count != 0 && UnrolledSize > UP.PartialThreshold) { |
| 650 | Count >>= 1; |
| 651 | UnrolledSize = (LoopSize - 2) * Count + 2; |
| 652 | } |
| 653 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 654 | } |
| 655 | } else if (Unrolling == Runtime) { |
| 656 | if (!AllowRuntime && !CountSetExplicitly) { |
| 657 | DEBUG(dbgs() << " will not try to unroll loop with runtime trip count " |
| 658 | << "-unroll-runtime not given\n"); |
| 659 | return false; |
| 660 | } |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 661 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 662 | // Reduce unroll count to be the largest power-of-two factor of |
| 663 | // the original count which satisfies the threshold limit. |
Justin Bogner | a1dd493 | 2016-01-12 00:55:26 +0000 | [diff] [blame] | 664 | while (Count != 0 && UnrolledSize > UP.PartialThreshold) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 665 | Count >>= 1; |
Hal Finkel | 38dd590 | 2015-01-10 00:30:55 +0000 | [diff] [blame] | 666 | UnrolledSize = (LoopSize-2) * Count + 2; |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 667 | } |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 668 | |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 669 | if (Count > UP.MaxCount) |
| 670 | Count = UP.MaxCount; |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 671 | |
| 672 | // If the loop contains a convergent operation, the prelude we'd add |
| 673 | // to do the first few instructions before we hit the unrolled loop |
| 674 | // is unsafe -- it adds a control-flow dependency to the convergent |
| 675 | // operation. Therefore Count must divide TripMultiple. |
| 676 | // |
| 677 | // TODO: This is quite conservative. In practice, convergent_op() |
| 678 | // is likely to be called unconditionally in the loop. In this |
| 679 | // case, the program would be ill-formed (on most architectures) |
| 680 | // unless n were the same on all threads in a thread group. |
| 681 | // Assuming n is the same on all threads, any kind of unrolling is |
| 682 | // safe. But currently llvm's notion of convergence isn't powerful |
| 683 | // enough to express this. |
| 684 | unsigned OrigCount = Count; |
| 685 | while (Convergent && Count != 0 && TripMultiple % Count != 0) { |
| 686 | DecreasedCountDueToConvergence = true; |
| 687 | Count >>= 1; |
| 688 | } |
| 689 | if (OrigCount > Count) { |
| 690 | DEBUG(dbgs() << " loop contains a convergent instruction, so unroll " |
| 691 | "count must divide the trip multiple, " |
| 692 | << TripMultiple << ". Reducing unroll count from " |
| 693 | << OrigCount << " to " << Count << ".\n"); |
| 694 | } |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 695 | DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); |
| 696 | } |
| 697 | |
| 698 | if (HasPragma) { |
| 699 | // Emit optimization remarks if we are unable to unroll the loop |
| 700 | // as directed by a pragma. |
| 701 | DebugLoc LoopLoc = L->getStartLoc(); |
| 702 | Function *F = Header->getParent(); |
| 703 | LLVMContext &Ctx = F->getContext(); |
Justin Lebar | 6827de1 | 2016-03-14 23:15:34 +0000 | [diff] [blame] | 704 | if (PragmaCount > 0 && DecreasedCountDueToConvergence) { |
| 705 | emitOptimizationRemarkMissed( |
| 706 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 707 | Twine("Unable to unroll loop the number of times directed by " |
| 708 | "unroll_count pragma because the loop contains a convergent " |
| 709 | "instruction, and so must have an unroll count that divides " |
| 710 | "the loop trip multiple of ") + |
| 711 | Twine(TripMultiple) + ". Unrolling instead " + Twine(Count) + |
| 712 | " time(s)."); |
| 713 | } else if ((PragmaCount > 0) && Count != OriginalCount) { |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 714 | emitOptimizationRemarkMissed( |
| 715 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 716 | "Unable to unroll loop the number of times directed by " |
| 717 | "unroll_count pragma because unrolled size is too large."); |
Mark Heffernan | 8939154 | 2015-08-10 17:28:08 +0000 | [diff] [blame] | 718 | } else if (PragmaFullUnroll && !TripCount) { |
| 719 | emitOptimizationRemarkMissed( |
| 720 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 721 | "Unable to fully unroll loop as directed by unroll(full) pragma " |
| 722 | "because loop has a runtime trip count."); |
| 723 | } else if (PragmaEnableUnroll && Count != TripCount && Count < 2) { |
| 724 | emitOptimizationRemarkMissed( |
| 725 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 726 | "Unable to unroll loop as directed by unroll(enable) pragma because " |
| 727 | "unrolled size is too large."); |
| 728 | } else if ((PragmaFullUnroll || PragmaEnableUnroll) && TripCount && |
| 729 | Count != TripCount) { |
| 730 | emitOptimizationRemarkMissed( |
| 731 | Ctx, DEBUG_TYPE, *F, LoopLoc, |
| 732 | "Unable to fully unroll loop as directed by unroll pragma because " |
| 733 | "unrolled size is too large."); |
Eli Bendersky | ff90324 | 2014-06-16 23:53:02 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | if (Unrolling != Full && Count < 2) { |
| 738 | // Partial unrolling by 1 is a nop. For full unrolling, a factor |
| 739 | // of 1 makes sense because loop control can be eliminated. |
| 740 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 743 | // Unroll the loop. |
Sanjoy Das | e178f46 | 2015-04-14 03:20:38 +0000 | [diff] [blame] | 744 | if (!UnrollLoop(L, Count, TripCount, AllowRuntime, UP.AllowExpensiveTripCount, |
Justin Bogner | 883a3ea | 2015-12-16 18:40:20 +0000 | [diff] [blame] | 745 | TripMultiple, LI, SE, &DT, &AC, PreserveLCSSA)) |
Dan Gohman | 3dc2d92 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 746 | return false; |
Dan Gohman | 2980d9d | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 747 | |
David L Kreitzer | 8d441eb | 2016-03-25 14:24:52 +0000 | [diff] [blame] | 748 | // If loop has an unroll count pragma mark loop as unrolled to prevent |
| 749 | // unrolling beyond that requested by the pragma. |
| 750 | if (HasPragma && PragmaCount != 0) |
| 751 | SetLoopAlreadyUnrolled(L); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 752 | return true; |
| 753 | } |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 754 | |
| 755 | namespace { |
| 756 | class LoopUnroll : public LoopPass { |
| 757 | public: |
| 758 | static char ID; // Pass ID, replacement for typeid |
| 759 | LoopUnroll(Optional<unsigned> Threshold = None, |
| 760 | Optional<unsigned> Count = None, |
| 761 | Optional<bool> AllowPartial = None, Optional<bool> Runtime = None) |
| 762 | : LoopPass(ID), ProvidedCount(Count), ProvidedThreshold(Threshold), |
| 763 | ProvidedAllowPartial(AllowPartial), ProvidedRuntime(Runtime) { |
| 764 | initializeLoopUnrollPass(*PassRegistry::getPassRegistry()); |
| 765 | } |
| 766 | |
| 767 | Optional<unsigned> ProvidedCount; |
| 768 | Optional<unsigned> ProvidedThreshold; |
| 769 | Optional<bool> ProvidedAllowPartial; |
| 770 | Optional<bool> ProvidedRuntime; |
| 771 | |
| 772 | bool runOnLoop(Loop *L, LPPassManager &) override { |
| 773 | if (skipOptnoneFunction(L)) |
| 774 | return false; |
| 775 | |
| 776 | Function &F = *L->getHeader()->getParent(); |
| 777 | |
| 778 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 779 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 780 | ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 781 | const TargetTransformInfo &TTI = |
| 782 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 783 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 784 | bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 785 | |
| 786 | return tryToUnrollLoop(L, DT, LI, SE, TTI, AC, PreserveLCSSA, ProvidedCount, |
| 787 | ProvidedThreshold, ProvidedAllowPartial, |
| 788 | ProvidedRuntime); |
| 789 | } |
| 790 | |
| 791 | /// This transformation requires natural loop information & requires that |
| 792 | /// loop preheaders be inserted into the CFG... |
| 793 | /// |
| 794 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 795 | AU.addRequired<AssumptionCacheTracker>(); |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 796 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 797 | // FIXME: Loop passes are required to preserve domtree, and for now we just |
| 798 | // recreate dom info if anything gets unrolled. |
| 799 | getLoopAnalysisUsage(AU); |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 800 | } |
| 801 | }; |
| 802 | } |
| 803 | |
| 804 | char LoopUnroll::ID = 0; |
| 805 | INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 806 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 807 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 808 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Justin Bogner | b8d82ab | 2016-01-12 05:21:37 +0000 | [diff] [blame] | 809 | INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false) |
| 810 | |
| 811 | Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial, |
| 812 | int Runtime) { |
| 813 | // TODO: It would make more sense for this function to take the optionals |
| 814 | // directly, but that's dangerous since it would silently break out of tree |
| 815 | // callers. |
| 816 | return new LoopUnroll(Threshold == -1 ? None : Optional<unsigned>(Threshold), |
| 817 | Count == -1 ? None : Optional<unsigned>(Count), |
| 818 | AllowPartial == -1 ? None |
| 819 | : Optional<bool>(AllowPartial), |
| 820 | Runtime == -1 ? None : Optional<bool>(Runtime)); |
| 821 | } |
| 822 | |
| 823 | Pass *llvm::createSimpleLoopUnrollPass() { |
| 824 | return llvm::createLoopUnrollPass(-1, -1, 0, 0); |
| 825 | } |