Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Detect the maximal Scops of a function. |
| 11 | // |
| 12 | // A static control part (Scop) is a subgraph of the control flow graph (CFG) |
| 13 | // that only has statically known control flow and can therefore be described |
| 14 | // within the polyhedral model. |
| 15 | // |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 16 | // Every Scop fulfills these restrictions: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 17 | // |
| 18 | // * It is a single entry single exit region |
| 19 | // |
| 20 | // * Only affine linear bounds in the loops |
| 21 | // |
| 22 | // Every natural loop in a Scop must have a number of loop iterations that can |
| 23 | // be described as an affine linear function in surrounding loop iterators or |
| 24 | // parameters. (A parameter is a scalar that does not change its value during |
| 25 | // execution of the Scop). |
| 26 | // |
| 27 | // * Only comparisons of affine linear expressions in conditions |
| 28 | // |
| 29 | // * All loops and conditions perfectly nested |
| 30 | // |
| 31 | // The control flow needs to be structured such that it could be written using |
| 32 | // just 'for' and 'if' statements, without the need for any 'goto', 'break' or |
| 33 | // 'continue'. |
| 34 | // |
| 35 | // * Side effect free functions call |
| 36 | // |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 37 | // Function calls and intrinsics that do not have side effects (readnone) |
| 38 | // or memory intrinsics (memset, memcpy, memmove) are allowed. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 39 | // |
| 40 | // The Scop detection finds the largest Scops by checking if the largest |
| 41 | // region is a Scop. If this is not the case, its canonical subregions are |
| 42 | // checked until a region is a Scop. It is now tried to extend this Scop by |
| 43 | // creating a larger non canonical region. |
| 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 47 | #include "polly/ScopDetection.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 48 | #include "polly/CodeGen/CodeGeneration.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 49 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 50 | #include "polly/Options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 51 | #include "polly/ScopDetectionDiagnostic.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 52 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | a63b7ce | 2015-05-03 05:21:36 +0000 | [diff] [blame] | 53 | #include "polly/Support/ScopLocation.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/Statistic.h" |
| 55 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 56 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/LoopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/RegionIterator.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 60 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 61 | #include "llvm/IR/DebugInfo.h" |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 62 | #include "llvm/IR/DiagnosticInfo.h" |
| 63 | #include "llvm/IR/DiagnosticPrinter.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 64 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 65 | #include "llvm/IR/LLVMContext.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 66 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 67 | #include <set> |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 68 | #include <stack> |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 69 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 70 | using namespace llvm; |
| 71 | using namespace polly; |
| 72 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 73 | #define DEBUG_TYPE "polly-detect" |
| 74 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 75 | // This option is set to a very high value, as analyzing such loops increases |
| 76 | // compile time on several cases. For experiments that enable this option, |
| 77 | // a value of around 40 has been working to avoid run-time regressions with |
| 78 | // Polly while still exposing interesting optimization opportunities. |
| 79 | static cl::opt<int> ProfitabilityMinPerLoopInstructions( |
| 80 | "polly-detect-profitability-min-per-loop-insts", |
| 81 | cl::desc("The minimal number of per-loop instructions before a single loop " |
| 82 | "region is considered profitable"), |
| 83 | cl::Hidden, cl::ValueRequired, cl::init(100000000), cl::cat(PollyCategory)); |
| 84 | |
Tobias Grosser | 575aca8 | 2015-10-06 16:10:29 +0000 | [diff] [blame] | 85 | bool polly::PollyProcessUnprofitable; |
| 86 | static cl::opt<bool, true> XPollyProcessUnprofitable( |
| 87 | "polly-process-unprofitable", |
| 88 | cl::desc( |
| 89 | "Process scops that are unlikely to benefit from Polly optimizations."), |
| 90 | cl::location(PollyProcessUnprofitable), cl::init(false), cl::ZeroOrMore, |
| 91 | cl::cat(PollyCategory)); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 92 | |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 93 | static cl::list<std::string> OnlyFunctions( |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 94 | "polly-only-func", |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 95 | cl::desc("Only run on functions that contain a certain string. " |
| 96 | "Multiple strings can be comma separated. " |
| 97 | "Scop detection will run on all functions that contain " |
| 98 | "any of the strings provided."), |
| 99 | cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 100 | |
Tobias Grosser | d8945ba | 2017-05-19 12:13:02 +0000 | [diff] [blame] | 101 | static cl::opt<bool> |
| 102 | AllowFullFunction("polly-detect-full-functions", |
| 103 | cl::desc("Allow the detection of full functions"), |
| 104 | cl::init(false), cl::cat(PollyCategory)); |
| 105 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 106 | static cl::opt<std::string> OnlyRegion( |
| 107 | "polly-only-region", |
| 108 | cl::desc("Only run on certain regions (The provided identifier must " |
| 109 | "appear in the name of the region's entry block"), |
| 110 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 111 | cl::cat(PollyCategory)); |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 112 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 113 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 114 | IgnoreAliasing("polly-ignore-aliasing", |
| 115 | cl::desc("Ignore possible aliasing of the array bases"), |
| 116 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 117 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 118 | |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 119 | bool polly::PollyAllowUnsignedOperations; |
| 120 | static cl::opt<bool, true> XPollyAllowUnsignedOperations( |
| 121 | "polly-allow-unsigned-operations", |
| 122 | cl::desc("Allow unsigned operations such as comparisons or zero-extends."), |
| 123 | cl::location(PollyAllowUnsignedOperations), cl::Hidden, cl::ZeroOrMore, |
| 124 | cl::init(true), cl::cat(PollyCategory)); |
| 125 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 126 | bool polly::PollyUseRuntimeAliasChecks; |
| 127 | static cl::opt<bool, true> XPollyUseRuntimeAliasChecks( |
| 128 | "polly-use-runtime-alias-checks", |
| 129 | cl::desc("Use runtime alias checks to resolve possible aliasing."), |
| 130 | cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore, |
| 131 | cl::init(true), cl::cat(PollyCategory)); |
| 132 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 133 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 134 | ReportLevel("polly-report", |
| 135 | cl::desc("Print information about the activities of Polly"), |
| 136 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 137 | |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 138 | static cl::opt<bool> AllowDifferentTypes( |
| 139 | "polly-allow-differing-element-types", |
| 140 | cl::desc("Allow different element types for array accesses"), cl::Hidden, |
Tobias Grosser | a2ee003 | 2016-02-16 14:37:24 +0000 | [diff] [blame] | 141 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 142 | |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 143 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 144 | AllowNonAffine("polly-allow-nonaffine", |
| 145 | cl::desc("Allow non affine access functions in arrays"), |
| 146 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 147 | cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 148 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 149 | static cl::opt<bool> |
| 150 | AllowModrefCall("polly-allow-modref-calls", |
| 151 | cl::desc("Allow functions with known modref behavior"), |
| 152 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 153 | cl::cat(PollyCategory)); |
| 154 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 155 | static cl::opt<bool> AllowNonAffineSubRegions( |
| 156 | "polly-allow-nonaffine-branches", |
| 157 | cl::desc("Allow non affine conditions for branches"), cl::Hidden, |
Johannes Doerfert | a36842f | 2015-02-26 11:09:24 +0000 | [diff] [blame] | 158 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 159 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 160 | static cl::opt<bool> |
| 161 | AllowNonAffineSubLoops("polly-allow-nonaffine-loops", |
| 162 | cl::desc("Allow non affine conditions for loops"), |
| 163 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 164 | cl::cat(PollyCategory)); |
| 165 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 166 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 167 | TrackFailures("polly-detect-track-failures", |
| 168 | cl::desc("Track failure strings in detecting scop regions"), |
| 169 | cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore, |
Andreas Simbuerger | 3efe40b | 2014-08-17 10:09:03 +0000 | [diff] [blame] | 170 | cl::init(true), cl::cat(PollyCategory)); |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 171 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 172 | static cl::opt<bool> KeepGoing("polly-detect-keep-going", |
| 173 | cl::desc("Do not fail on the first error."), |
| 174 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 175 | cl::cat(PollyCategory)); |
| 176 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 177 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 178 | PollyDelinearizeX("polly-delinearize", |
| 179 | cl::desc("Delinearize array access functions"), |
| 180 | cl::location(PollyDelinearize), cl::Hidden, |
Tobias Grosser | 6973cb6 | 2015-03-08 15:21:18 +0000 | [diff] [blame] | 181 | cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory)); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 182 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 183 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 184 | VerifyScops("polly-detect-verify", |
| 185 | cl::desc("Verify the detected SCoPs after each transformation"), |
| 186 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 187 | cl::cat(PollyCategory)); |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 188 | |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 189 | bool polly::PollyInvariantLoadHoisting; |
| 190 | static cl::opt<bool, true> XPollyInvariantLoadHoisting( |
| 191 | "polly-invariant-load-hoisting", cl::desc("Hoist invariant loads."), |
| 192 | cl::location(PollyInvariantLoadHoisting), cl::Hidden, cl::ZeroOrMore, |
Tobias Grosser | 74814e1 | 2016-08-15 16:43:36 +0000 | [diff] [blame] | 193 | cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 194 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 195 | /// The minimal trip count under which loops are considered unprofitable. |
Johannes Doerfert | e526de5 | 2015-09-21 19:10:11 +0000 | [diff] [blame] | 196 | static const unsigned MIN_LOOP_TRIP_COUNT = 8; |
| 197 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 198 | bool polly::PollyTrackFailures = false; |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 199 | bool polly::PollyDelinearize = false; |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 200 | StringRef polly::PollySkipFnAttr = "polly.skip.fn"; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 201 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Statistics. |
| 204 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 205 | STATISTIC(NumScopRegions, "Number of scops"); |
| 206 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
| 207 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 208 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 209 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 210 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 211 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 212 | STATISTIC(NumScopsDepthLarger, |
| 213 | "Number of scops with maximal loop depth 6 and larger"); |
| 214 | STATISTIC(NumProfScopRegions, "Number of scops (profitable scops only)"); |
| 215 | STATISTIC(NumLoopsInProfScop, |
| 216 | "Number of loops in scops (profitable scops only)"); |
| 217 | STATISTIC(NumLoopsOverall, "Number of total loops"); |
| 218 | STATISTIC(NumProfScopsDepthOne, |
| 219 | "Number of scops with maximal loop depth 1 (profitable scops only)"); |
| 220 | STATISTIC(NumProfScopsDepthTwo, |
| 221 | "Number of scops with maximal loop depth 2 (profitable scops only)"); |
| 222 | STATISTIC(NumProfScopsDepthThree, |
| 223 | "Number of scops with maximal loop depth 3 (profitable scops only)"); |
| 224 | STATISTIC(NumProfScopsDepthFour, |
| 225 | "Number of scops with maximal loop depth 4 (profitable scops only)"); |
| 226 | STATISTIC(NumProfScopsDepthFive, |
| 227 | "Number of scops with maximal loop depth 5 (profitable scops only)"); |
Tobias Grosser | 21a059a | 2017-01-16 14:08:10 +0000 | [diff] [blame] | 228 | STATISTIC(NumProfScopsDepthLarger, |
| 229 | "Number of scops with maximal loop depth 6 and larger " |
| 230 | "(profitable scops only)"); |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 231 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 232 | STATISTIC(MaxNumLoopsInProfScop, |
| 233 | "Maximal number of loops in scops (profitable scops only)"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 234 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 235 | static void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 236 | bool OnlyProfitable); |
| 237 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 238 | class DiagnosticScopFound : public DiagnosticInfo { |
| 239 | private: |
| 240 | static int PluginDiagnosticKind; |
| 241 | |
| 242 | Function &F; |
| 243 | std::string FileName; |
| 244 | unsigned EntryLine, ExitLine; |
| 245 | |
| 246 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 247 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 248 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 249 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 250 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 251 | |
| 252 | virtual void print(DiagnosticPrinter &DP) const; |
| 253 | |
| 254 | static bool classof(const DiagnosticInfo *DI) { |
| 255 | return DI->getKind() == PluginDiagnosticKind; |
| 256 | } |
| 257 | }; |
| 258 | |
Tobias Grosser | db6db50 | 2016-04-01 07:15:19 +0000 | [diff] [blame] | 259 | int DiagnosticScopFound::PluginDiagnosticKind = |
| 260 | getNextAvailablePluginDiagnosticKind(); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 261 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 262 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 263 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 264 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 265 | |
| 266 | if (FileName.empty()) { |
| 267 | DP << "Scop location is unknown. Compile with debug info " |
| 268 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 269 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 273 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 274 | } |
| 275 | |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 276 | static bool IsFnNameListedInOnlyFunctions(StringRef FnName) { |
| 277 | for (auto Name : OnlyFunctions) |
| 278 | if (FnName.count(Name) > 0) |
| 279 | return true; |
| 280 | return false; |
| 281 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 282 | //===----------------------------------------------------------------------===// |
| 283 | // ScopDetection. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 284 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 285 | ScopDetection::ScopDetection(Function &F, const DominatorTree &DT, |
| 286 | ScalarEvolution &SE, LoopInfo &LI, RegionInfo &RI, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 287 | AliasAnalysis &AA, OptimizationRemarkEmitter &ORE) |
| 288 | : DT(DT), SE(SE), LI(LI), RI(RI), AA(AA), ORE(ORE) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 289 | |
| 290 | if (!PollyProcessUnprofitable && LI.empty()) |
| 291 | return; |
| 292 | |
| 293 | Region *TopRegion = RI.getTopLevelRegion(); |
| 294 | |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 295 | if (OnlyFunctions.size() > 0 && !IsFnNameListedInOnlyFunctions(F.getName())) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 296 | return; |
| 297 | |
| 298 | if (!isValidFunction(F)) |
| 299 | return; |
| 300 | |
| 301 | findScops(*TopRegion); |
| 302 | |
| 303 | NumScopRegions += ValidRegions.size(); |
| 304 | |
| 305 | // Prune non-profitable regions. |
| 306 | for (auto &DIt : DetectionContextMap) { |
| 307 | auto &DC = DIt.getSecond(); |
| 308 | if (DC.Log.hasErrors()) |
| 309 | continue; |
| 310 | if (!ValidRegions.count(&DC.CurRegion)) |
| 311 | continue; |
| 312 | LoopStats Stats = countBeneficialLoops(&DC.CurRegion, SE, LI, 0); |
| 313 | updateLoopCountStatistic(Stats, false /* OnlyProfitable */); |
| 314 | if (isProfitableRegion(DC)) { |
| 315 | updateLoopCountStatistic(Stats, true /* OnlyProfitable */); |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | ValidRegions.remove(&DC.CurRegion); |
| 320 | } |
| 321 | |
| 322 | NumProfScopRegions += ValidRegions.size(); |
| 323 | NumLoopsOverall += countBeneficialLoops(TopRegion, SE, LI, 0).NumLoops; |
| 324 | |
| 325 | // Only makes sense when we tracked errors. |
| 326 | if (PollyTrackFailures) |
| 327 | emitMissedRemarks(F); |
| 328 | |
| 329 | if (ReportLevel) |
| 330 | printLocations(F); |
| 331 | |
| 332 | assert(ValidRegions.size() <= DetectionContextMap.size() && |
| 333 | "Cached more results than valid regions"); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 336 | template <class RR, typename... Args> |
| 337 | inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert, |
| 338 | Args &&... Arguments) const { |
| 339 | |
| 340 | if (!Context.Verifying) { |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 341 | RejectLog &Log = Context.Log; |
| 342 | std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 343 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 344 | if (PollyTrackFailures) |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 345 | Log.report(RejectReason); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 346 | |
| 347 | DEBUG(dbgs() << RejectReason->getMessage()); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 348 | DEBUG(dbgs() << "\n"); |
| 349 | } else { |
| 350 | assert(!Assert && "Verification of detected scop failed"); |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 356 | bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const { |
| 357 | if (!ValidRegions.count(&R)) |
| 358 | return false; |
| 359 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 360 | if (Verify) { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 361 | DetectionContextMap.erase(getBBPairForRegion(&R)); |
| 362 | const auto &It = DetectionContextMap.insert(std::make_pair( |
| 363 | getBBPairForRegion(&R), |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 364 | DetectionContext(const_cast<Region &>(R), AA, false /*verifying*/))); |
Tobias Grosser | 907090c | 2015-10-25 10:55:35 +0000 | [diff] [blame] | 365 | DetectionContext &Context = It.first->second; |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 366 | return isValidRegion(Context); |
| 367 | } |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 368 | |
| 369 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 372 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 373 | // Get the first error we found. Even in keep-going mode, this is the first |
| 374 | // reason that caused the candidate to be rejected. |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 375 | auto *Log = lookupRejectionLog(R); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 376 | |
| 377 | // This can happen when we marked a region invalid, but didn't track |
| 378 | // an error for it. |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 379 | if (!Log || !Log->hasErrors()) |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 380 | return ""; |
| 381 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 382 | RejectReasonPtr RR = *Log->begin(); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 383 | return RR->getMessage(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 386 | bool ScopDetection::addOverApproximatedRegion(Region *AR, |
| 387 | DetectionContext &Context) const { |
| 388 | |
| 389 | // If we already know about Ar we can exit. |
| 390 | if (!Context.NonAffineSubRegionSet.insert(AR)) |
| 391 | return true; |
| 392 | |
| 393 | // All loops in the region have to be overapproximated too if there |
| 394 | // are accesses that depend on the iteration count. |
Michael Kruse | 41f046a | 2016-06-27 19:00:49 +0000 | [diff] [blame] | 395 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 396 | for (BasicBlock *BB : AR->blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 397 | Loop *L = LI.getLoopFor(BB); |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 398 | if (AR->contains(L)) |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 399 | Context.BoxedLoopsSet.insert(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 400 | } |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 401 | |
| 402 | return (AllowNonAffineSubLoops || Context.BoxedLoopsSet.empty()); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 405 | bool ScopDetection::onlyValidRequiredInvariantLoads( |
| 406 | InvariantLoadsSetTy &RequiredILS, DetectionContext &Context) const { |
| 407 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7b5a4df | 2017-04-11 04:59:13 +0000 | [diff] [blame] | 408 | const DataLayout &DL = CurRegion.getEntry()->getModule()->getDataLayout(); |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 409 | |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 410 | if (!PollyInvariantLoadHoisting && !RequiredILS.empty()) |
| 411 | return false; |
| 412 | |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 413 | for (LoadInst *Load : RequiredILS) { |
Tobias Grosser | 3f25a7e | 2017-05-04 10:16:20 +0000 | [diff] [blame] | 414 | // If we already know a load has been accepted as required invariant, we |
| 415 | // already run the validation below once and consequently don't need to |
| 416 | // run it again. Hence, we return early. For certain test cases (e.g., |
| 417 | // COSMO this avoids us spending 50% of scop-detection time in this |
| 418 | // very function (and its children). |
| 419 | if (Context.RequiredILS.count(Load)) |
| 420 | continue; |
| 421 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 422 | if (!isHoistableLoad(Load, CurRegion, LI, SE, DT)) |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 423 | return false; |
| 424 | |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 425 | for (auto NonAffineRegion : Context.NonAffineSubRegionSet) { |
| 426 | |
| 427 | if (isSafeToLoadUnconditionally(Load->getPointerOperand(), |
| 428 | Load->getAlignment(), DL)) |
| 429 | continue; |
| 430 | |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 431 | if (NonAffineRegion->contains(Load) && |
| 432 | Load->getParent() != NonAffineRegion->getEntry()) |
| 433 | return false; |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 434 | } |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 437 | Context.RequiredILS.insert(RequiredILS.begin(), RequiredILS.end()); |
| 438 | |
| 439 | return true; |
| 440 | } |
| 441 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 442 | bool ScopDetection::involvesMultiplePtrs(const SCEV *S0, const SCEV *S1, |
| 443 | Loop *Scope) const { |
| 444 | SetVector<Value *> Values; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 445 | findValues(S0, SE, Values); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 446 | if (S1) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 447 | findValues(S1, SE, Values); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 448 | |
| 449 | SmallPtrSet<Value *, 8> PtrVals; |
| 450 | for (auto *V : Values) { |
| 451 | if (auto *P2I = dyn_cast<PtrToIntInst>(V)) |
| 452 | V = P2I->getOperand(0); |
| 453 | |
| 454 | if (!V->getType()->isPointerTy()) |
| 455 | continue; |
| 456 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 457 | auto *PtrSCEV = SE.getSCEVAtScope(V, Scope); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 458 | if (isa<SCEVConstant>(PtrSCEV)) |
| 459 | continue; |
| 460 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 461 | auto *BasePtr = dyn_cast<SCEVUnknown>(SE.getPointerBase(PtrSCEV)); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 462 | if (!BasePtr) |
| 463 | return true; |
| 464 | |
| 465 | auto *BasePtrVal = BasePtr->getValue(); |
| 466 | if (PtrVals.insert(BasePtrVal).second) { |
| 467 | for (auto *PtrVal : PtrVals) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 468 | if (PtrVal != BasePtrVal && !AA.isNoAlias(PtrVal, BasePtrVal)) |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return false; |
| 474 | } |
| 475 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 476 | bool ScopDetection::isAffine(const SCEV *S, Loop *Scope, |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 477 | DetectionContext &Context) const { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 478 | |
| 479 | InvariantLoadsSetTy AccessILS; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 480 | if (!isAffineExpr(&Context.CurRegion, Scope, S, SE, &AccessILS)) |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 481 | return false; |
| 482 | |
| 483 | if (!onlyValidRequiredInvariantLoads(AccessILS, Context)) |
| 484 | return false; |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 489 | bool ScopDetection::isValidSwitch(BasicBlock &BB, SwitchInst *SI, |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 490 | Value *Condition, bool IsLoopBranch, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 491 | DetectionContext &Context) const { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 492 | Loop *L = LI.getLoopFor(&BB); |
| 493 | const SCEV *ConditionSCEV = SE.getSCEVAtScope(Condition, L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 494 | |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 495 | if (IsLoopBranch && L->isLoopLatch(&BB)) |
| 496 | return false; |
| 497 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 498 | // Check for invalid usage of different pointers in one expression. |
| 499 | if (involvesMultiplePtrs(ConditionSCEV, nullptr, L)) |
| 500 | return false; |
| 501 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 502 | if (isAffine(ConditionSCEV, L, Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 503 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 504 | |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 505 | if (AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 506 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 507 | return true; |
| 508 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 509 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, |
| 510 | ConditionSCEV, ConditionSCEV, SI); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | bool ScopDetection::isValidBranch(BasicBlock &BB, BranchInst *BI, |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 514 | Value *Condition, bool IsLoopBranch, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 515 | DetectionContext &Context) const { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 516 | |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 517 | // Constant integer conditions are always affine. |
| 518 | if (isa<ConstantInt>(Condition)) |
| 519 | return true; |
| 520 | |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 521 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 522 | auto Opcode = BinOp->getOpcode(); |
| 523 | if (Opcode == Instruction::And || Opcode == Instruction::Or) { |
| 524 | Value *Op0 = BinOp->getOperand(0); |
| 525 | Value *Op1 = BinOp->getOperand(1); |
| 526 | return isValidBranch(BB, BI, Op0, IsLoopBranch, Context) && |
| 527 | isValidBranch(BB, BI, Op1, IsLoopBranch, Context); |
| 528 | } |
| 529 | } |
| 530 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 531 | // Non constant conditions of branches need to be ICmpInst. |
| 532 | if (!isa<ICmpInst>(Condition)) { |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 533 | if (!IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 534 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 535 | return true; |
| 536 | return invalid<ReportInvalidCond>(Context, /*Assert=*/true, BI, &BB); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 537 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 538 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 539 | ICmpInst *ICmp = cast<ICmpInst>(Condition); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 540 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 541 | // Are both operands of the ICmp affine? |
| 542 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
| 543 | isa<UndefValue>(ICmp->getOperand(1))) |
| 544 | return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 545 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 546 | Loop *L = LI.getLoopFor(&BB); |
| 547 | const SCEV *LHS = SE.getSCEVAtScope(ICmp->getOperand(0), L); |
| 548 | const SCEV *RHS = SE.getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 549 | |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 550 | // If unsigned operations are not allowed try to approximate the region. |
| 551 | if (ICmp->isUnsigned() && !PollyAllowUnsignedOperations) |
| 552 | return !IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 553 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context); |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 554 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 555 | // Check for invalid usage of different pointers in one expression. |
| 556 | if (ICmp->isEquality() && involvesMultiplePtrs(LHS, nullptr, L) && |
| 557 | involvesMultiplePtrs(RHS, nullptr, L)) |
| 558 | return false; |
| 559 | |
| 560 | // Check for invalid usage of different pointers in a relational comparison. |
| 561 | if (ICmp->isRelational() && involvesMultiplePtrs(LHS, RHS, L)) |
| 562 | return false; |
| 563 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 564 | if (isAffine(LHS, L, Context) && isAffine(RHS, L, Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 565 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 566 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 567 | if (!IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 568 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 569 | return true; |
| 570 | |
| 571 | if (IsLoopBranch) |
| 572 | return false; |
| 573 | |
| 574 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, RHS, |
| 575 | ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 578 | bool ScopDetection::isValidCFG(BasicBlock &BB, bool IsLoopBranch, |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 579 | bool AllowUnreachable, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 580 | DetectionContext &Context) const { |
| 581 | Region &CurRegion = Context.CurRegion; |
| 582 | |
| 583 | TerminatorInst *TI = BB.getTerminator(); |
| 584 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 585 | if (AllowUnreachable && isa<UnreachableInst>(TI)) |
| 586 | return true; |
| 587 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 588 | // Return instructions are only valid if the region is the top level region. |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 589 | if (isa<ReturnInst>(TI) && CurRegion.isTopLevelRegion()) |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 590 | return true; |
| 591 | |
| 592 | Value *Condition = getConditionFromTerminator(TI); |
| 593 | |
| 594 | if (!Condition) |
| 595 | return invalid<ReportInvalidTerminator>(Context, /*Assert=*/true, &BB); |
| 596 | |
| 597 | // UndefValue is not allowed as condition. |
| 598 | if (isa<UndefValue>(Condition)) |
| 599 | return invalid<ReportUndefCond>(Context, /*Assert=*/true, TI, &BB); |
| 600 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 601 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 602 | return isValidBranch(BB, BI, Condition, IsLoopBranch, Context); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 603 | |
| 604 | SwitchInst *SI = dyn_cast<SwitchInst>(TI); |
| 605 | assert(SI && "Terminator was neither branch nor switch"); |
| 606 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 607 | return isValidSwitch(BB, SI, Condition, IsLoopBranch, Context); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 610 | bool ScopDetection::isValidCallInst(CallInst &CI, |
| 611 | DetectionContext &Context) const { |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 612 | if (CI.doesNotReturn()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 613 | return false; |
| 614 | |
| 615 | if (CI.doesNotAccessMemory()) |
| 616 | return true; |
| 617 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 618 | if (auto *II = dyn_cast<IntrinsicInst>(&CI)) |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 619 | if (isValidIntrinsicInst(*II, Context)) |
| 620 | return true; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 621 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 622 | Function *CalledFunction = CI.getCalledFunction(); |
| 623 | |
| 624 | // Indirect calls are not supported. |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 625 | if (CalledFunction == nullptr) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 626 | return false; |
| 627 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 628 | if (AllowModrefCall) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 629 | switch (AA.getModRefBehavior(CalledFunction)) { |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 630 | case FMRB_UnknownModRefBehavior: |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 631 | return false; |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 632 | case FMRB_DoesNotAccessMemory: |
| 633 | case FMRB_OnlyReadsMemory: |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 634 | // Implicitly disable delinearization since we have an unknown |
| 635 | // accesses with an unknown access function. |
| 636 | Context.HasUnknownAccess = true; |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 637 | Context.AST.add(&CI); |
| 638 | return true; |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 639 | case FMRB_OnlyReadsArgumentPointees: |
| 640 | case FMRB_OnlyAccessesArgumentPointees: |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 641 | for (const auto &Arg : CI.arg_operands()) { |
| 642 | if (!Arg->getType()->isPointerTy()) |
| 643 | continue; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 644 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 645 | // Bail if a pointer argument has a base address not known to |
| 646 | // ScalarEvolution. Note that a zero pointer is acceptable. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 647 | auto *ArgSCEV = SE.getSCEVAtScope(Arg, LI.getLoopFor(CI.getParent())); |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 648 | if (ArgSCEV->isZero()) |
| 649 | continue; |
| 650 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 651 | auto *BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(ArgSCEV)); |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 652 | if (!BP) |
| 653 | return false; |
| 654 | |
| 655 | // Implicitly disable delinearization since we have an unknown |
| 656 | // accesses with an unknown access function. |
| 657 | Context.HasUnknownAccess = true; |
| 658 | } |
| 659 | |
| 660 | Context.AST.add(&CI); |
| 661 | return true; |
Weiming Zhao | 7614e17 | 2016-07-11 18:27:52 +0000 | [diff] [blame] | 662 | case FMRB_DoesNotReadMemory: |
Tobias Grosser | 70d2709 | 2016-11-13 19:27:24 +0000 | [diff] [blame] | 663 | case FMRB_OnlyAccessesInaccessibleMem: |
| 664 | case FMRB_OnlyAccessesInaccessibleOrArgMem: |
Weiming Zhao | 7614e17 | 2016-07-11 18:27:52 +0000 | [diff] [blame] | 665 | return false; |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 666 | } |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | |
| 672 | bool ScopDetection::isValidIntrinsicInst(IntrinsicInst &II, |
| 673 | DetectionContext &Context) const { |
| 674 | if (isIgnoredIntrinsic(&II)) |
Tobias Grosser | 9c0ffe3 | 2015-08-30 16:57:15 +0000 | [diff] [blame] | 675 | return true; |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 676 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 677 | // The closest loop surrounding the call instruction. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 678 | Loop *L = LI.getLoopFor(II.getParent()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 679 | |
| 680 | // The access function and base pointer for memory intrinsics. |
| 681 | const SCEV *AF; |
| 682 | const SCEVUnknown *BP; |
| 683 | |
| 684 | switch (II.getIntrinsicID()) { |
| 685 | // Memory intrinsics that can be represented are supported. |
| 686 | case llvm::Intrinsic::memmove: |
| 687 | case llvm::Intrinsic::memcpy: |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 688 | AF = SE.getSCEVAtScope(cast<MemTransferInst>(II).getSource(), L); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 689 | if (!AF->isZero()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 690 | BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(AF)); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 691 | // Bail if the source pointer is not valid. |
| 692 | if (!isValidAccess(&II, AF, BP, Context)) |
| 693 | return false; |
| 694 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 695 | // Fall through |
| 696 | case llvm::Intrinsic::memset: |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 697 | AF = SE.getSCEVAtScope(cast<MemIntrinsic>(II).getDest(), L); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 698 | if (!AF->isZero()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 699 | BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(AF)); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 700 | // Bail if the destination pointer is not valid. |
| 701 | if (!isValidAccess(&II, AF, BP, Context)) |
| 702 | return false; |
| 703 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 704 | |
| 705 | // Bail if the length is not affine. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 706 | if (!isAffine(SE.getSCEVAtScope(cast<MemIntrinsic>(II).getLength(), L), L, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 707 | Context)) |
| 708 | return false; |
| 709 | |
| 710 | return true; |
| 711 | default: |
| 712 | break; |
| 713 | } |
| 714 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 718 | bool ScopDetection::isInvariant(Value &Val, const Region &Reg, |
| 719 | DetectionContext &Ctx) const { |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 720 | // A reference to function argument or constant value is invariant. |
| 721 | if (isa<Argument>(Val) || isa<Constant>(Val)) |
| 722 | return true; |
| 723 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 724 | Instruction *I = dyn_cast<Instruction>(&Val); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 725 | if (!I) |
| 726 | return false; |
| 727 | |
| 728 | if (!Reg.contains(I)) |
| 729 | return true; |
| 730 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 731 | // Loads within the SCoP may read arbitrary values, need to hoist them. If it |
| 732 | // is not hoistable, it will be rejected later, but here we assume it is and |
| 733 | // that makes the value invariant. |
| 734 | if (auto LI = dyn_cast<LoadInst>(I)) { |
| 735 | Ctx.RequiredILS.insert(LI); |
| 736 | return true; |
| 737 | } |
| 738 | |
Michael Kruse | 6744efa8d | 2017-03-08 15:14:46 +0000 | [diff] [blame] | 739 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 742 | /// Remove smax of smax(0, size) expressions from a SCEV expression and |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 743 | /// register the '...' components. |
| 744 | /// |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 745 | /// Array access expressions as they are generated by GFortran contain smax(0, |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 746 | /// size) expressions that confuse the 'normal' delinearization algorithm. |
| 747 | /// However, if we extract such expressions before the normal delinearization |
| 748 | /// takes place they can actually help to identify array size expressions in |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 749 | /// Fortran accesses. For the subsequently following delinearization the smax(0, |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 750 | /// size) component can be replaced by just 'size'. This is correct as we will |
| 751 | /// always add and verify the assumption that for all subscript expressions |
| 752 | /// 'exp' the inequality 0 <= exp < size holds. Hence, we will also verify |
| 753 | /// that 0 <= size, which means smax(0, size) == size. |
Tobias Grosser | ebb626e | 2016-10-29 06:19:34 +0000 | [diff] [blame] | 754 | class SCEVRemoveMax : public SCEVRewriteVisitor<SCEVRemoveMax> { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 755 | public: |
Tobias Grosser | ebb626e | 2016-10-29 06:19:34 +0000 | [diff] [blame] | 756 | static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE, |
| 757 | std::vector<const SCEV *> *Terms = nullptr) { |
| 758 | SCEVRemoveMax Rewriter(SE, Terms); |
| 759 | return Rewriter.visit(Scev); |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | SCEVRemoveMax(ScalarEvolution &SE, std::vector<const SCEV *> *Terms) |
Tobias Grosser | ebb626e | 2016-10-29 06:19:34 +0000 | [diff] [blame] | 763 | : SCEVRewriteVisitor(SE), Terms(Terms) {} |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 764 | |
| 765 | const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
Michael Kruse | 8fc2896 | 2015-12-20 14:42:32 +0000 | [diff] [blame] | 766 | if ((Expr->getNumOperands() == 2) && Expr->getOperand(0)->isZero()) { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 767 | auto Res = visit(Expr->getOperand(1)); |
| 768 | if (Terms) |
| 769 | (*Terms).push_back(Res); |
| 770 | return Res; |
| 771 | } |
| 772 | |
| 773 | return Expr; |
| 774 | } |
| 775 | |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 776 | private: |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 777 | std::vector<const SCEV *> *Terms; |
| 778 | }; |
| 779 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 780 | SmallVector<const SCEV *, 4> |
| 781 | ScopDetection::getDelinearizationTerms(DetectionContext &Context, |
| 782 | const SCEVUnknown *BasePointer) const { |
| 783 | SmallVector<const SCEV *, 4> Terms; |
| 784 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 785 | std::vector<const SCEV *> MaxTerms; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 786 | SCEVRemoveMax::rewrite(Pair.second, SE, &MaxTerms); |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 787 | if (MaxTerms.size() > 0) { |
| 788 | Terms.insert(Terms.begin(), MaxTerms.begin(), MaxTerms.end()); |
| 789 | continue; |
| 790 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 791 | // In case the outermost expression is a plain add, we check if any of its |
| 792 | // terms has the form 4 * %inst * %param * %param ..., aka a term that |
| 793 | // contains a product between a parameter and an instruction that is |
| 794 | // inside the scop. Such instructions, if allowed at all, are instructions |
| 795 | // SCEV can not represent, but Polly is still looking through. As a |
| 796 | // result, these instructions can depend on induction variables and are |
| 797 | // most likely no array sizes. However, terms that are multiplied with |
| 798 | // them are likely candidates for array sizes. |
| 799 | if (auto *AF = dyn_cast<SCEVAddExpr>(Pair.second)) { |
| 800 | for (auto Op : AF->operands()) { |
| 801 | if (auto *AF2 = dyn_cast<SCEVAddRecExpr>(Op)) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 802 | SE.collectParametricTerms(AF2, Terms); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 803 | if (auto *AF2 = dyn_cast<SCEVMulExpr>(Op)) { |
| 804 | SmallVector<const SCEV *, 0> Operands; |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 805 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 806 | for (auto *MulOp : AF2->operands()) { |
| 807 | if (auto *Const = dyn_cast<SCEVConstant>(MulOp)) |
| 808 | Operands.push_back(Const); |
| 809 | if (auto *Unknown = dyn_cast<SCEVUnknown>(MulOp)) { |
| 810 | if (auto *Inst = dyn_cast<Instruction>(Unknown->getValue())) { |
| 811 | if (!Context.CurRegion.contains(Inst)) |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 812 | Operands.push_back(MulOp); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 813 | |
| 814 | } else { |
| 815 | Operands.push_back(MulOp); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 818 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 819 | if (Operands.size()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 820 | Terms.push_back(SE.getMulExpr(Operands)); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 823 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 824 | if (Terms.empty()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 825 | SE.collectParametricTerms(Pair.second, Terms); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 826 | } |
| 827 | return Terms; |
| 828 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 829 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 830 | bool ScopDetection::hasValidArraySizes(DetectionContext &Context, |
| 831 | SmallVectorImpl<const SCEV *> &Sizes, |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 832 | const SCEVUnknown *BasePointer, |
| 833 | Loop *Scope) const { |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 834 | // If no sizes were found, all sizes are trivially valid. We allow this case |
| 835 | // to make it possible to pass known-affine accesses to the delinearization to |
| 836 | // try to recover some interesting multi-dimensional accesses, but to still |
| 837 | // allow the already known to be affine access in case the delinearization |
| 838 | // fails. In such situations, the delinearization will just return a Sizes |
| 839 | // array of size zero. |
| 840 | if (Sizes.size() == 0) |
| 841 | return true; |
| 842 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 843 | Value *BaseValue = BasePointer->getValue(); |
| 844 | Region &CurRegion = Context.CurRegion; |
| 845 | for (const SCEV *DelinearizedSize : Sizes) { |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 846 | if (!isAffine(DelinearizedSize, Scope, Context)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 847 | Sizes.clear(); |
| 848 | break; |
Tobias Grosser | 5528dcd | 2015-10-25 08:40:38 +0000 | [diff] [blame] | 849 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 850 | if (auto *Unknown = dyn_cast<SCEVUnknown>(DelinearizedSize)) { |
| 851 | auto *V = dyn_cast<Value>(Unknown->getValue()); |
| 852 | if (auto *Load = dyn_cast<LoadInst>(V)) { |
| 853 | if (Context.CurRegion.contains(Load) && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 854 | isHoistableLoad(Load, CurRegion, LI, SE, DT)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 855 | Context.RequiredILS.insert(Load); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 856 | continue; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 857 | } |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 858 | } |
Siddharth Bhat | a1b2086 | 2017-07-13 12:18:56 +0000 | [diff] [blame] | 859 | if (hasScalarDepsInsideRegion(DelinearizedSize, &CurRegion, Scope, false, |
| 860 | Context.RequiredILS)) |
Tobias Grosser | bfaf1ae | 2015-12-21 09:09:39 +0000 | [diff] [blame] | 861 | return invalid<ReportNonAffineAccess>( |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 862 | Context, /*Assert=*/true, DelinearizedSize, |
| 863 | Context.Accesses[BasePointer].front().first, BaseValue); |
| 864 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 865 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 866 | // No array shape derived. |
| 867 | if (Sizes.empty()) { |
| 868 | if (AllowNonAffine) |
| 869 | return true; |
| 870 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 871 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 872 | const Instruction *Insn = Pair.first; |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 873 | const SCEV *AF = Pair.second; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 874 | |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 875 | if (!isAffine(AF, Scope, Context)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 876 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn, |
| 877 | BaseValue); |
| 878 | if (!KeepGoing) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 879 | return false; |
| 880 | } |
| 881 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 882 | return false; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 883 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 884 | return true; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 887 | // We first store the resulting memory accesses in TempMemoryAccesses. Only |
| 888 | // if the access functions for all memory accesses have been successfully |
| 889 | // delinearized we continue. Otherwise, we either report a failure or, if |
| 890 | // non-affine accesses are allowed, we drop the information. In case the |
| 891 | // information is dropped the memory accesses need to be overapproximated |
| 892 | // when translated to a polyhedral representation. |
| 893 | bool ScopDetection::computeAccessFunctions( |
| 894 | DetectionContext &Context, const SCEVUnknown *BasePointer, |
| 895 | std::shared_ptr<ArrayShape> Shape) const { |
| 896 | Value *BaseValue = BasePointer->getValue(); |
| 897 | bool BasePtrHasNonAffine = false; |
| 898 | MapInsnToMemAcc TempMemoryAccesses; |
| 899 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 900 | const Instruction *Insn = Pair.first; |
| 901 | auto *AF = Pair.second; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 902 | AF = SCEVRemoveMax::rewrite(AF, SE); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 903 | bool IsNonAffine = false; |
| 904 | TempMemoryAccesses.insert(std::make_pair(Insn, MemAcc(Insn, Shape))); |
| 905 | MemAcc *Acc = &TempMemoryAccesses.find(Insn)->second; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 906 | auto *Scope = LI.getLoopFor(Insn->getParent()); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 907 | |
| 908 | if (!AF) { |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 909 | if (isAffine(Pair.second, Scope, Context)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 910 | Acc->DelinearizedSubscripts.push_back(Pair.second); |
| 911 | else |
| 912 | IsNonAffine = true; |
| 913 | } else { |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 914 | if (Shape->DelinearizedSizes.size() == 0) { |
| 915 | Acc->DelinearizedSubscripts.push_back(AF); |
| 916 | } else { |
| 917 | SE.computeAccessFunctions(AF, Acc->DelinearizedSubscripts, |
| 918 | Shape->DelinearizedSizes); |
| 919 | if (Acc->DelinearizedSubscripts.size() == 0) |
| 920 | IsNonAffine = true; |
| 921 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 922 | for (const SCEV *S : Acc->DelinearizedSubscripts) |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 923 | if (!isAffine(S, Scope, Context)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 924 | IsNonAffine = true; |
| 925 | } |
| 926 | |
| 927 | // (Possibly) report non affine access |
| 928 | if (IsNonAffine) { |
| 929 | BasePtrHasNonAffine = true; |
| 930 | if (!AllowNonAffine) |
| 931 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second, |
| 932 | Insn, BaseValue); |
| 933 | if (!KeepGoing && !AllowNonAffine) |
| 934 | return false; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | if (!BasePtrHasNonAffine) |
Hongbin Zheng | 2262320 | 2016-02-15 00:20:58 +0000 | [diff] [blame] | 939 | Context.InsnToMemAcc.insert(TempMemoryAccesses.begin(), |
| 940 | TempMemoryAccesses.end()); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 945 | bool ScopDetection::hasBaseAffineAccesses(DetectionContext &Context, |
| 946 | const SCEVUnknown *BasePointer, |
| 947 | Loop *Scope) const { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 948 | auto Shape = std::shared_ptr<ArrayShape>(new ArrayShape(BasePointer)); |
| 949 | |
| 950 | auto Terms = getDelinearizationTerms(Context, BasePointer); |
| 951 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 952 | SE.findArrayDimensions(Terms, Shape->DelinearizedSizes, |
| 953 | Context.ElementSize[BasePointer]); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 954 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 955 | if (!hasValidArraySizes(Context, Shape->DelinearizedSizes, BasePointer, |
| 956 | Scope)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 957 | return false; |
| 958 | |
| 959 | return computeAccessFunctions(Context, BasePointer, Shape); |
| 960 | } |
| 961 | |
| 962 | bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 963 | // TODO: If we have an unknown access and other non-affine accesses we do |
| 964 | // not try to delinearize them for now. |
| 965 | if (Context.HasUnknownAccess && !Context.NonAffineAccesses.empty()) |
| 966 | return AllowNonAffine; |
| 967 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 968 | for (auto &Pair : Context.NonAffineAccesses) { |
| 969 | auto *BasePointer = Pair.first; |
| 970 | auto *Scope = Pair.second; |
| 971 | if (!hasBaseAffineAccesses(Context, BasePointer, Scope)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 972 | if (KeepGoing) |
| 973 | continue; |
| 974 | else |
| 975 | return false; |
| 976 | } |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 977 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 978 | return true; |
| 979 | } |
| 980 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 981 | bool ScopDetection::isValidAccess(Instruction *Inst, const SCEV *AF, |
| 982 | const SCEVUnknown *BP, |
| 983 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 984 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 985 | if (!BP) |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 986 | return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 987 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 988 | auto *BV = BP->getValue(); |
| 989 | if (isa<UndefValue>(BV)) |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 990 | return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 991 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 992 | // FIXME: Think about allowing IntToPtrInst |
| 993 | if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BV)) |
| 994 | return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst); |
| 995 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 996 | // Check that the base address of the access is invariant in the current |
| 997 | // region. |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 998 | if (!isInvariant(*BV, Context.CurRegion, Context)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 999 | return invalid<ReportVariantBasePtr>(Context, /*Assert=*/true, BV, Inst); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 1000 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1001 | AF = SE.getMinusSCEV(AF, BP); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 1002 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1003 | const SCEV *Size; |
| 1004 | if (!isa<MemIntrinsic>(Inst)) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1005 | Size = SE.getElementSize(Inst); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 1006 | } else { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1007 | auto *SizeTy = |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1008 | SE.getEffectiveSCEVType(PointerType::getInt8PtrTy(SE.getContext())); |
| 1009 | Size = SE.getConstant(SizeTy, 8); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 1010 | } |
Tobias Grosser | bcd4eff | 2014-09-13 14:47:40 +0000 | [diff] [blame] | 1011 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1012 | if (Context.ElementSize[BP]) { |
| 1013 | if (!AllowDifferentTypes && Context.ElementSize[BP] != Size) |
| 1014 | return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true, |
| 1015 | Inst, BV); |
| 1016 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1017 | Context.ElementSize[BP] = SE.getSMinExpr(Size, Context.ElementSize[BP]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | Context.ElementSize[BP] = Size; |
| 1020 | } |
| 1021 | |
| 1022 | bool IsVariantInNonAffineLoop = false; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1023 | SetVector<const Loop *> Loops; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1024 | findLoops(AF, Loops); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1025 | for (const Loop *L : Loops) |
| 1026 | if (Context.BoxedLoopsSet.count(L)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1027 | IsVariantInNonAffineLoop = true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1028 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1029 | auto *Scope = LI.getLoopFor(Inst->getParent()); |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 1030 | bool IsAffine = !IsVariantInNonAffineLoop && isAffine(AF, Scope, Context); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1031 | // Do not try to delinearize memory intrinsics and force them to be affine. |
| 1032 | if (isa<MemIntrinsic>(Inst) && !IsAffine) { |
| 1033 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst, |
| 1034 | BV); |
| 1035 | } else if (PollyDelinearize && !IsVariantInNonAffineLoop) { |
| 1036 | Context.Accesses[BP].push_back({Inst, AF}); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 1037 | |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 1038 | if (!IsAffine || hasIVParams(AF)) |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1039 | Context.NonAffineAccesses.insert( |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1040 | std::make_pair(BP, LI.getLoopFor(Inst->getParent()))); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1041 | } else if (!AllowNonAffine && !IsAffine) { |
| 1042 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst, |
| 1043 | BV); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 1044 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1045 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1046 | if (IgnoreAliasing) |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 1047 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 1048 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 1049 | // Check if the base pointer of the memory access does alias with |
| 1050 | // any other pointer. This cannot be handled at the moment. |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 1051 | AAMDNodes AATags; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1052 | Inst->getAAMetadata(AATags); |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 1053 | AliasSet &AS = Context.AST.getAliasSetForPointer( |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1054 | BP->getValue(), MemoryLocation::UnknownSize, AATags); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 1055 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1056 | if (!AS.isMustAlias()) { |
| 1057 | if (PollyUseRuntimeAliasChecks) { |
| 1058 | bool CanBuildRunTimeCheck = true; |
| 1059 | // The run-time alias check places code that involves the base pointer at |
| 1060 | // the beginning of the SCoP. This breaks if the base pointer is defined |
| 1061 | // inside the scop. Hence, we can only create a run-time check if we are |
| 1062 | // sure the base pointer is not an instruction defined inside the scop. |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1063 | // However, we can ignore loads that will be hoisted. |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1064 | for (const auto &Ptr : AS) { |
| 1065 | Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1066 | if (Inst && Context.CurRegion.contains(Inst)) { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1067 | auto *Load = dyn_cast<LoadInst>(Inst); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1068 | if (Load && isHoistableLoad(Load, Context.CurRegion, LI, SE, DT)) { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1069 | Context.RequiredILS.insert(Load); |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1073 | CanBuildRunTimeCheck = false; |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (CanBuildRunTimeCheck) |
| 1079 | return true; |
| 1080 | } |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1081 | return invalid<ReportAlias>(Context, /*Assert=*/true, Inst, AS); |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1082 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1083 | |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1087 | bool ScopDetection::isValidMemoryAccess(MemAccInst Inst, |
| 1088 | DetectionContext &Context) const { |
| 1089 | Value *Ptr = Inst.getPointerOperand(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1090 | Loop *L = LI.getLoopFor(Inst->getParent()); |
| 1091 | const SCEV *AccessFunction = SE.getSCEVAtScope(Ptr, L); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1092 | const SCEVUnknown *BasePointer; |
| 1093 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1094 | BasePointer = dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFunction)); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1095 | |
| 1096 | return isValidAccess(Inst, AccessFunction, BasePointer, Context); |
| 1097 | } |
| 1098 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1099 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 1100 | DetectionContext &Context) const { |
Tobias Grosser | b12b006 | 2015-11-11 12:44:18 +0000 | [diff] [blame] | 1101 | for (auto &Op : Inst.operands()) { |
| 1102 | auto *OpInst = dyn_cast<Instruction>(&Op); |
| 1103 | |
| 1104 | if (!OpInst) |
| 1105 | continue; |
| 1106 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1107 | if (isErrorBlock(*OpInst->getParent(), Context.CurRegion, LI, DT)) |
Tobias Grosser | b12b006 | 2015-11-11 12:44:18 +0000 | [diff] [blame] | 1108 | return false; |
| 1109 | } |
| 1110 | |
Johannes Doerfert | 81c41b9 | 2016-04-09 21:55:58 +0000 | [diff] [blame] | 1111 | if (isa<LandingPadInst>(&Inst) || isa<ResumeInst>(&Inst)) |
| 1112 | return false; |
| 1113 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1114 | // We only check the call instruction but not invoke instruction. |
| 1115 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1116 | if (isValidCallInst(*CI, Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1117 | return true; |
| 1118 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1119 | return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Tobias Grosser | 1f0236d | 2016-11-21 09:07:30 +0000 | [diff] [blame] | 1122 | if (!Inst.mayReadOrWriteMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 1123 | if (!isa<AllocaInst>(Inst)) |
| 1124 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1125 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1126 | return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // Check the access function. |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1130 | if (auto MemInst = MemAccInst::dyn_cast(Inst)) { |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 1131 | Context.hasStores |= isa<StoreInst>(MemInst); |
| 1132 | Context.hasLoads |= isa<LoadInst>(MemInst); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1133 | if (!MemInst.isSimple()) |
| 1134 | return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true, |
| 1135 | &Inst); |
Tobias Grosser | bf45e74 | 2015-10-25 13:48:40 +0000 | [diff] [blame] | 1136 | |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1137 | return isValidMemoryAccess(MemInst, Context); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 1138 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1139 | |
| 1140 | // We do not know this instruction, therefore we assume it is invalid. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1141 | return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 1144 | /// Check whether @p L has exiting blocks. |
| 1145 | /// |
| 1146 | /// @param L The loop of interest |
| 1147 | /// |
| 1148 | /// @return True if the loop has exiting blocks, false otherwise. |
| 1149 | static bool hasExitingBlocks(Loop *L) { |
| 1150 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 1151 | L->getExitingBlocks(ExitingBlocks); |
| 1152 | return !ExitingBlocks.empty(); |
| 1153 | } |
| 1154 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1155 | bool ScopDetection::canUseISLTripCount(Loop *L, |
| 1156 | DetectionContext &Context) const { |
Johannes Doerfert | 30ffb6f | 2015-10-04 14:53:18 +0000 | [diff] [blame] | 1157 | // Ensure the loop has valid exiting blocks as well as latches, otherwise we |
| 1158 | // need to overapproximate it as a boxed loop. |
| 1159 | SmallVector<BasicBlock *, 4> LoopControlBlocks; |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 1160 | L->getExitingBlocks(LoopControlBlocks); |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 1161 | L->getLoopLatches(LoopControlBlocks); |
Johannes Doerfert | 30ffb6f | 2015-10-04 14:53:18 +0000 | [diff] [blame] | 1162 | for (BasicBlock *ControlBB : LoopControlBlocks) { |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 1163 | if (!isValidCFG(*ControlBB, true, false, Context)) |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1164 | return false; |
| 1165 | } |
| 1166 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1167 | // We can use ISL to compute the trip count of L. |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1171 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 1172 | // Loops that contain part but not all of the blocks of a region cannot be |
| 1173 | // handled by the schedule generation. Such loop constructs can happen |
| 1174 | // because a region can contain BBs that have no path to the exit block |
| 1175 | // (Infinite loops, UnreachableInst), but such blocks are never part of a |
| 1176 | // loop. |
| 1177 | // |
| 1178 | // _______________ |
| 1179 | // | Loop Header | <-----------. |
| 1180 | // --------------- | |
| 1181 | // | | |
| 1182 | // _______________ ______________ |
| 1183 | // | RegionEntry |-----> | RegionExit |-----> |
| 1184 | // --------------- -------------- |
| 1185 | // | |
| 1186 | // _______________ |
| 1187 | // | EndlessLoop | <--. |
| 1188 | // --------------- | |
| 1189 | // | | |
| 1190 | // \------------/ |
| 1191 | // |
| 1192 | // In the example above, the loop (LoopHeader,RegionEntry,RegionExit) is |
| 1193 | // neither entirely contained in the region RegionEntry->RegionExit |
| 1194 | // (containing RegionEntry,EndlessLoop) nor is the region entirely contained |
| 1195 | // in the loop. |
| 1196 | // The block EndlessLoop is contained in the region because Region::contains |
| 1197 | // tests whether it is not dominated by RegionExit. This is probably to not |
| 1198 | // having to query the PostdominatorTree. Instead of an endless loop, a dead |
| 1199 | // end can also be formed by an UnreachableInst. This case is already caught |
| 1200 | // by isErrorBlock(). We hence only have to reject endless loops here. |
| 1201 | if (!hasExitingBlocks(L)) |
| 1202 | return invalid<ReportLoopHasNoExit>(Context, /*Assert=*/true, L); |
| 1203 | |
Johannes Doerfert | f61df69 | 2015-10-04 14:56:08 +0000 | [diff] [blame] | 1204 | if (canUseISLTripCount(L, Context)) |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1205 | return true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1206 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1207 | if (AllowNonAffineSubLoops && AllowNonAffineSubRegions) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1208 | Region *R = RI.getRegionFor(L->getHeader()); |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 1209 | while (R != &Context.CurRegion && !R->contains(L)) |
| 1210 | R = R->getParent(); |
| 1211 | |
| 1212 | if (addOverApproximatedRegion(R, Context)) |
| 1213 | return true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1214 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1215 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1216 | const SCEV *LoopCount = SE.getBackedgeTakenCount(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1217 | return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1220 | /// Return the number of loops in @p L (incl. @p L) that have a trip |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1221 | /// count that is not known to be less than @MinProfitableTrips. |
| 1222 | ScopDetection::LoopStats |
| 1223 | ScopDetection::countBeneficialSubLoops(Loop *L, ScalarEvolution &SE, |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1224 | unsigned MinProfitableTrips) { |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1225 | auto *TripCount = SE.getBackedgeTakenCount(L); |
| 1226 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1227 | int NumLoops = 1; |
| 1228 | int MaxLoopDepth = 1; |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1229 | if (auto *TripCountC = dyn_cast<SCEVConstant>(TripCount)) |
Johannes Doerfert | f61df69 | 2015-10-04 14:56:08 +0000 | [diff] [blame] | 1230 | if (TripCountC->getType()->getScalarSizeInBits() <= 64) |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1231 | if (TripCountC->getValue()->getZExtValue() <= MinProfitableTrips) |
| 1232 | NumLoops -= 1; |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1233 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1234 | for (auto &SubLoop : *L) { |
| 1235 | LoopStats Stats = countBeneficialSubLoops(SubLoop, SE, MinProfitableTrips); |
| 1236 | NumLoops += Stats.NumLoops; |
Tobias Grosser | 65ce936 | 2017-02-17 08:08:54 +0000 | [diff] [blame] | 1237 | MaxLoopDepth = std::max(MaxLoopDepth, Stats.MaxDepth + 1); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1238 | } |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1239 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1240 | return {NumLoops, MaxLoopDepth}; |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1243 | ScopDetection::LoopStats |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1244 | ScopDetection::countBeneficialLoops(Region *R, ScalarEvolution &SE, |
| 1245 | LoopInfo &LI, unsigned MinProfitableTrips) { |
Johannes Doerfert | f61df69 | 2015-10-04 14:56:08 +0000 | [diff] [blame] | 1246 | int LoopNum = 0; |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1247 | int MaxLoopDepth = 0; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1248 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1249 | auto L = LI.getLoopFor(R->getEntry()); |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1250 | L = L ? R->outermostLoopInRegion(L) : nullptr; |
| 1251 | L = L ? L->getParentLoop() : nullptr; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1252 | |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1253 | auto SubLoops = |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1254 | L ? L->getSubLoopsVector() : std::vector<Loop *>(LI.begin(), LI.end()); |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1255 | |
| 1256 | for (auto &SubLoop : SubLoops) |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1257 | if (R->contains(SubLoop)) { |
| 1258 | LoopStats Stats = |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1259 | countBeneficialSubLoops(SubLoop, SE, MinProfitableTrips); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1260 | LoopNum += Stats.NumLoops; |
| 1261 | MaxLoopDepth = std::max(MaxLoopDepth, Stats.MaxDepth); |
| 1262 | } |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1263 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1264 | return {LoopNum, MaxLoopDepth}; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1267 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1268 | // Initial no valid region was found (greater than R) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1269 | std::unique_ptr<Region> LastValidRegion; |
| 1270 | auto ExpandedRegion = std::unique_ptr<Region>(R.getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1271 | |
| 1272 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 1273 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1274 | while (ExpandedRegion) { |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1275 | const auto &It = DetectionContextMap.insert(std::make_pair( |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1276 | getBBPairForRegion(ExpandedRegion.get()), |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1277 | DetectionContext(*ExpandedRegion, AA, false /*verifying*/))); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1278 | DetectionContext &Context = It.first->second; |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1279 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1280 | // Only expand when we did not collect errors. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1281 | |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame] | 1282 | if (!Context.Log.hasErrors()) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1283 | // If the exit is valid check all blocks |
| 1284 | // - if true, a valid region was found => store it + keep expanding |
| 1285 | // - if false, .tbd. => stop (should this really end the loop?) |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1286 | if (!allBlocksValid(Context) || Context.Log.hasErrors()) { |
| 1287 | removeCachedResults(*ExpandedRegion); |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1288 | DetectionContextMap.erase(It.first); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1289 | break; |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1290 | } |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1291 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 1292 | // Store this region, because it is the greatest valid (encountered so |
| 1293 | // far). |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1294 | if (LastValidRegion) { |
| 1295 | removeCachedResults(*LastValidRegion); |
| 1296 | DetectionContextMap.erase(getBBPairForRegion(LastValidRegion.get())); |
| 1297 | } |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1298 | LastValidRegion = std::move(ExpandedRegion); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1299 | |
| 1300 | // Create and test the next greater region (if any) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1301 | ExpandedRegion = |
| 1302 | std::unique_ptr<Region>(LastValidRegion->getExpandedRegion()); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1303 | |
| 1304 | } else { |
| 1305 | // Create and test the next greater region (if any) |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1306 | removeCachedResults(*ExpandedRegion); |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1307 | DetectionContextMap.erase(It.first); |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1308 | ExpandedRegion = |
| 1309 | std::unique_ptr<Region>(ExpandedRegion->getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1310 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 1313 | DEBUG({ |
| 1314 | if (LastValidRegion) |
| 1315 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 1316 | else |
| 1317 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 1318 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1319 | |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1320 | return LastValidRegion.release(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1321 | } |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1322 | static bool regionWithoutLoops(Region &R, LoopInfo &LI) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1323 | for (const BasicBlock *BB : R.blocks()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1324 | if (R.contains(LI.getLoopFor(BB))) |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 1325 | return false; |
| 1326 | |
| 1327 | return true; |
| 1328 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1329 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1330 | void ScopDetection::removeCachedResultsRecursively(const Region &R) { |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1331 | for (auto &SubRegion : R) { |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1332 | if (ValidRegions.count(SubRegion.get())) { |
| 1333 | removeCachedResults(*SubRegion.get()); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1334 | } else |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1335 | removeCachedResultsRecursively(*SubRegion); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 1336 | } |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1339 | void ScopDetection::removeCachedResults(const Region &R) { |
| 1340 | ValidRegions.remove(&R); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1343 | void ScopDetection::findScops(Region &R) { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1344 | const auto &It = DetectionContextMap.insert(std::make_pair( |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1345 | getBBPairForRegion(&R), DetectionContext(R, AA, false /*verifying*/))); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1346 | DetectionContext &Context = It.first->second; |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1347 | |
| 1348 | bool RegionIsValid = false; |
Michael Kruse | 0b56681 | 2016-02-29 16:54:18 +0000 | [diff] [blame] | 1349 | if (!PollyProcessUnprofitable && regionWithoutLoops(R, LI)) |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1350 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R); |
Michael Kruse | 0b56681 | 2016-02-29 16:54:18 +0000 | [diff] [blame] | 1351 | else |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1352 | RegionIsValid = isValidRegion(Context); |
| 1353 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 1354 | bool HasErrors = !RegionIsValid || Context.Log.size() > 0; |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 1355 | |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1356 | if (HasErrors) { |
| 1357 | removeCachedResults(R); |
| 1358 | } else { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1359 | ValidRegions.insert(&R); |
| 1360 | return; |
| 1361 | } |
| 1362 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1363 | for (auto &SubRegion : R) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1364 | findScops(*SubRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Try to expand regions. |
| 1367 | // |
| 1368 | // As the region tree normally only contains canonical regions, non canonical |
| 1369 | // regions that form a Scop are not found. Therefore, those non canonical |
| 1370 | // regions are checked by expanding the canonical ones. |
| 1371 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 1372 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1373 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1374 | for (auto &SubRegion : R) |
| 1375 | ToExpand.push_back(SubRegion.get()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1376 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1377 | for (Region *CurrentRegion : ToExpand) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1378 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 1379 | // an already expanded region. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 1380 | if (!ValidRegions.count(CurrentRegion)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1381 | continue; |
| 1382 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1383 | // Skip regions that had errors. |
| 1384 | bool HadErrors = lookupRejectionLog(CurrentRegion)->hasErrors(); |
| 1385 | if (HadErrors) |
| 1386 | continue; |
| 1387 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1388 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 1389 | |
| 1390 | if (!ExpandedR) |
| 1391 | continue; |
| 1392 | |
| 1393 | R.addSubRegion(ExpandedR, true); |
| 1394 | ValidRegions.insert(ExpandedR); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1395 | removeCachedResults(*CurrentRegion); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1396 | removeCachedResultsRecursively(*ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1401 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1402 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1403 | for (const BasicBlock *BB : CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1404 | Loop *L = LI.getLoopFor(BB); |
Tobias Grosser | a3aa423 | 2017-07-15 22:42:17 +0000 | [diff] [blame] | 1405 | if (L && L->getHeader() == BB) { |
| 1406 | if (CurRegion.contains(L)) { |
| 1407 | if (!isValidLoop(L, Context) && !KeepGoing) |
| 1408 | return false; |
| 1409 | } else { |
| 1410 | SmallVector<BasicBlock *, 1> Latches; |
| 1411 | L->getLoopLatches(Latches); |
| 1412 | for (BasicBlock *Latch : Latches) |
| 1413 | if (CurRegion.contains(Latch)) |
| 1414 | return invalid<ReportLoopOnlySomeLatches>(Context, /*Assert=*/true, |
| 1415 | L); |
| 1416 | } |
| 1417 | } |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1420 | for (BasicBlock *BB : CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1421 | bool IsErrorBlock = isErrorBlock(*BB, CurRegion, LI, DT); |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Also check exception blocks (and possibly register them as non-affine |
| 1424 | // regions). Even though exception blocks are not modeled, we use them |
| 1425 | // to forward-propagate domain constraints during ScopInfo construction. |
| 1426 | if (!isValidCFG(*BB, false, IsErrorBlock, Context) && !KeepGoing) |
| 1427 | return false; |
| 1428 | |
| 1429 | if (IsErrorBlock) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1430 | continue; |
| 1431 | |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 1432 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 1433 | if (!isValidInstruction(*I, Context) && !KeepGoing) |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 1434 | return false; |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1435 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1436 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 1437 | if (!hasAffineMemoryAccesses(Context)) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 1438 | return false; |
| 1439 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1440 | return true; |
| 1441 | } |
| 1442 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1443 | bool ScopDetection::hasSufficientCompute(DetectionContext &Context, |
| 1444 | int NumLoops) const { |
| 1445 | int InstCount = 0; |
| 1446 | |
Tobias Grosser | b316dc1 | 2016-09-08 14:08:05 +0000 | [diff] [blame] | 1447 | if (NumLoops == 0) |
| 1448 | return false; |
| 1449 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1450 | for (auto *BB : Context.CurRegion.blocks()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1451 | if (Context.CurRegion.contains(LI.getLoopFor(BB))) |
Tobias Grosser | c6424ae | 2015-12-22 17:38:59 +0000 | [diff] [blame] | 1452 | InstCount += BB->size(); |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1453 | |
| 1454 | InstCount = InstCount / NumLoops; |
| 1455 | |
| 1456 | return InstCount >= ProfitabilityMinPerLoopInstructions; |
| 1457 | } |
| 1458 | |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1459 | bool ScopDetection::hasPossiblyDistributableLoop( |
| 1460 | DetectionContext &Context) const { |
| 1461 | for (auto *BB : Context.CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1462 | auto *L = LI.getLoopFor(BB); |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1463 | if (!Context.CurRegion.contains(L)) |
| 1464 | continue; |
| 1465 | if (Context.BoxedLoopsSet.count(L)) |
| 1466 | continue; |
| 1467 | unsigned StmtsWithStoresInLoops = 0; |
| 1468 | for (auto *LBB : L->blocks()) { |
| 1469 | bool MemStore = false; |
| 1470 | for (auto &I : *LBB) |
| 1471 | MemStore |= isa<StoreInst>(&I); |
| 1472 | StmtsWithStoresInLoops += MemStore; |
| 1473 | } |
| 1474 | return (StmtsWithStoresInLoops > 1); |
| 1475 | } |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1479 | bool ScopDetection::isProfitableRegion(DetectionContext &Context) const { |
| 1480 | Region &CurRegion = Context.CurRegion; |
| 1481 | |
| 1482 | if (PollyProcessUnprofitable) |
| 1483 | return true; |
| 1484 | |
| 1485 | // We can probably not do a lot on scops that only write or only read |
| 1486 | // data. |
| 1487 | if (!Context.hasStores || !Context.hasLoads) |
| 1488 | return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
| 1489 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1490 | int NumLoops = |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1491 | countBeneficialLoops(&CurRegion, SE, LI, MIN_LOOP_TRIP_COUNT).NumLoops; |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1492 | int NumAffineLoops = NumLoops - Context.BoxedLoopsSet.size(); |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1493 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1494 | // Scops with at least two loops may allow either loop fusion or tiling and |
| 1495 | // are consequently interesting to look at. |
| 1496 | if (NumAffineLoops >= 2) |
| 1497 | return true; |
| 1498 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 1499 | // A loop with multiple non-trivial blocks might be amendable to distribution. |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1500 | if (NumAffineLoops == 1 && hasPossiblyDistributableLoop(Context)) |
| 1501 | return true; |
| 1502 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1503 | // Scops that contain a loop with a non-trivial amount of computation per |
| 1504 | // loop-iteration are interesting as we may be able to parallelize such |
| 1505 | // loops. Individual loops that have only a small amount of computation |
| 1506 | // per-iteration are performance-wise very fragile as any change to the |
| 1507 | // loop induction variables may affect performance. To not cause spurious |
| 1508 | // performance regressions, we do not consider such loops. |
| 1509 | if (NumAffineLoops == 1 && hasSufficientCompute(Context, NumLoops)) |
| 1510 | return true; |
| 1511 | |
| 1512 | return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1515 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1516 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1517 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1518 | DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1519 | |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 1520 | if (!AllowFullFunction && CurRegion.isTopLevelRegion()) { |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 1521 | DEBUG(dbgs() << "Top level region is invalid\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1522 | return false; |
| 1523 | } |
| 1524 | |
Tobias Grosser | 134a572 | 2017-03-07 15:50:43 +0000 | [diff] [blame] | 1525 | DebugLoc DbgLoc; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 1526 | if (CurRegion.getExit() && |
| 1527 | isa<UnreachableInst>(CurRegion.getExit()->getTerminator())) { |
Tobias Grosser | 134a572 | 2017-03-07 15:50:43 +0000 | [diff] [blame] | 1528 | DEBUG(dbgs() << "Unreachable in exit\n"); |
| 1529 | return invalid<ReportUnreachableInExit>(Context, /*Assert=*/true, |
| 1530 | CurRegion.getExit(), DbgLoc); |
| 1531 | } |
| 1532 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1533 | if (!CurRegion.getEntry()->getName().count(OnlyRegion)) { |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 1534 | DEBUG({ |
| 1535 | dbgs() << "Region entry does not match -polly-region-only"; |
| 1536 | dbgs() << "\n"; |
| 1537 | }); |
| 1538 | return false; |
| 1539 | } |
| 1540 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 1541 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1542 | // to insert alloca instruction there when translate scalar to array. |
Tobias Grosser | d8945ba | 2017-05-19 12:13:02 +0000 | [diff] [blame] | 1543 | if (!AllowFullFunction && |
| 1544 | CurRegion.getEntry() == |
| 1545 | &(CurRegion.getEntry()->getParent()->getEntryBlock())) |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1546 | return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1547 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 1548 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1549 | return false; |
| 1550 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1551 | if (!isReducibleRegion(CurRegion, DbgLoc)) |
| 1552 | return invalid<ReportIrreducibleRegion>(Context, /*Assert=*/true, |
| 1553 | &CurRegion, DbgLoc); |
| 1554 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1555 | DEBUG(dbgs() << "OK\n"); |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
Tobias Grosser | 629109b | 2016-08-03 12:00:07 +0000 | [diff] [blame] | 1559 | void ScopDetection::markFunctionAsInvalid(Function *F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 1560 | F->addFnAttr(PollySkipFnAttr); |
| 1561 | } |
| 1562 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1563 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 1564 | return !F.hasFnAttribute(PollySkipFnAttr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 1567 | void ScopDetection::printLocations(llvm::Function &F) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1568 | for (const Region *R : *this) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1569 | unsigned LineEntry, LineExit; |
| 1570 | std::string FileName; |
| 1571 | |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1572 | getDebugLocation(R, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 1573 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 1574 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1578 | void ScopDetection::emitMissedRemarks(const Function &F) { |
| 1579 | for (auto &DIt : DetectionContextMap) { |
| 1580 | auto &DC = DIt.getSecond(); |
| 1581 | if (DC.Log.hasErrors()) |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1582 | emitRejectionRemarks(DIt.getFirst(), DC.Log, ORE); |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1586 | bool ScopDetection::isReducibleRegion(Region &R, DebugLoc &DbgLoc) const { |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1587 | /// Enum for coloring BBs in Region. |
Tobias Grosser | ef6ae70 | 2016-06-11 09:00:37 +0000 | [diff] [blame] | 1588 | /// |
| 1589 | /// WHITE - Unvisited BB in DFS walk. |
| 1590 | /// GREY - BBs which are currently on the DFS stack for processing. |
| 1591 | /// BLACK - Visited and completely processed BB. |
| 1592 | enum Color { WHITE, GREY, BLACK }; |
| 1593 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1594 | BasicBlock *REntry = R.getEntry(); |
| 1595 | BasicBlock *RExit = R.getExit(); |
| 1596 | // Map to match the color of a BasicBlock during the DFS walk. |
| 1597 | DenseMap<const BasicBlock *, Color> BBColorMap; |
| 1598 | // Stack keeping track of current BB and index of next child to be processed. |
| 1599 | std::stack<std::pair<BasicBlock *, unsigned>> DFSStack; |
| 1600 | |
| 1601 | unsigned AdjacentBlockIndex = 0; |
| 1602 | BasicBlock *CurrBB, *SuccBB; |
| 1603 | CurrBB = REntry; |
| 1604 | |
| 1605 | // Initialize the map for all BB with WHITE color. |
| 1606 | for (auto *BB : R.blocks()) |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1607 | BBColorMap[BB] = WHITE; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Process the entry block of the Region. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1610 | BBColorMap[CurrBB] = GREY; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1611 | DFSStack.push(std::make_pair(CurrBB, 0)); |
| 1612 | |
| 1613 | while (!DFSStack.empty()) { |
| 1614 | // Get next BB on stack to be processed. |
| 1615 | CurrBB = DFSStack.top().first; |
| 1616 | AdjacentBlockIndex = DFSStack.top().second; |
| 1617 | DFSStack.pop(); |
| 1618 | |
| 1619 | // Loop to iterate over the successors of current BB. |
| 1620 | const TerminatorInst *TInst = CurrBB->getTerminator(); |
| 1621 | unsigned NSucc = TInst->getNumSuccessors(); |
| 1622 | for (unsigned I = AdjacentBlockIndex; I < NSucc; |
| 1623 | ++I, ++AdjacentBlockIndex) { |
| 1624 | SuccBB = TInst->getSuccessor(I); |
| 1625 | |
| 1626 | // Checks for region exit block and self-loops in BB. |
| 1627 | if (SuccBB == RExit || SuccBB == CurrBB) |
| 1628 | continue; |
| 1629 | |
| 1630 | // WHITE indicates an unvisited BB in DFS walk. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1631 | if (BBColorMap[SuccBB] == WHITE) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1632 | // Push the current BB and the index of the next child to be visited. |
| 1633 | DFSStack.push(std::make_pair(CurrBB, I + 1)); |
| 1634 | // Push the next BB to be processed. |
| 1635 | DFSStack.push(std::make_pair(SuccBB, 0)); |
| 1636 | // First time the BB is being processed. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1637 | BBColorMap[SuccBB] = GREY; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1638 | break; |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1639 | } else if (BBColorMap[SuccBB] == GREY) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1640 | // GREY indicates a loop in the control flow. |
| 1641 | // If the destination dominates the source, it is a natural loop |
| 1642 | // else, an irreducible control flow in the region is detected. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1643 | if (!DT.dominates(SuccBB, CurrBB)) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1644 | // Get debug info of instruction which causes irregular control flow. |
| 1645 | DbgLoc = TInst->getDebugLoc(); |
| 1646 | return false; |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | // If all children of current BB have been processed, |
| 1652 | // then mark that BB as fully processed. |
| 1653 | if (AdjacentBlockIndex == NSucc) |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1654 | BBColorMap[CurrBB] = BLACK; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1660 | static void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 1661 | bool OnlyProfitable) { |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1662 | if (!OnlyProfitable) { |
| 1663 | NumLoopsInScop += Stats.NumLoops; |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 1664 | MaxNumLoopsInScop = |
| 1665 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1666 | if (Stats.MaxDepth == 1) |
| 1667 | NumScopsDepthOne++; |
| 1668 | else if (Stats.MaxDepth == 2) |
| 1669 | NumScopsDepthTwo++; |
| 1670 | else if (Stats.MaxDepth == 3) |
| 1671 | NumScopsDepthThree++; |
| 1672 | else if (Stats.MaxDepth == 4) |
| 1673 | NumScopsDepthFour++; |
| 1674 | else if (Stats.MaxDepth == 5) |
| 1675 | NumScopsDepthFive++; |
| 1676 | else |
| 1677 | NumScopsDepthLarger++; |
| 1678 | } else { |
| 1679 | NumLoopsInProfScop += Stats.NumLoops; |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 1680 | MaxNumLoopsInProfScop = |
| 1681 | std::max(MaxNumLoopsInProfScop.getValue(), (unsigned)Stats.NumLoops); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1682 | if (Stats.MaxDepth == 1) |
| 1683 | NumProfScopsDepthOne++; |
| 1684 | else if (Stats.MaxDepth == 2) |
| 1685 | NumProfScopsDepthTwo++; |
| 1686 | else if (Stats.MaxDepth == 3) |
| 1687 | NumProfScopsDepthThree++; |
| 1688 | else if (Stats.MaxDepth == 4) |
| 1689 | NumProfScopsDepthFour++; |
| 1690 | else if (Stats.MaxDepth == 5) |
| 1691 | NumProfScopsDepthFive++; |
| 1692 | else |
| 1693 | NumProfScopsDepthLarger++; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Johannes Doerfert | 1dafea4 | 2016-05-23 09:07:08 +0000 | [diff] [blame] | 1697 | ScopDetection::DetectionContext * |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1698 | ScopDetection::getDetectionContext(const Region *R) const { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1699 | auto DCMIt = DetectionContextMap.find(getBBPairForRegion(R)); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1700 | if (DCMIt == DetectionContextMap.end()) |
| 1701 | return nullptr; |
| 1702 | return &DCMIt->second; |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1705 | const RejectLog *ScopDetection::lookupRejectionLog(const Region *R) const { |
| 1706 | const DetectionContext *DC = getDetectionContext(R); |
| 1707 | return DC ? &DC->Log : nullptr; |
| 1708 | } |
| 1709 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1710 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 1711 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1712 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1713 | DetectionContext Context(const_cast<Region &>(R), AA, true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1714 | isValidRegion(Context); |
| 1715 | } |
| 1716 | |
| 1717 | void polly::ScopDetection::verifyAnalysis() const { |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 1718 | if (!VerifyScops) |
| 1719 | return; |
| 1720 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1721 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1722 | verifyRegion(*R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1725 | bool ScopDetectionWrapperPass::runOnFunction(llvm::Function &F) { |
| 1726 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 1727 | auto &RI = getAnalysis<RegionInfoPass>().getRegionInfo(); |
| 1728 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 1729 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 1730 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1731 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
| 1732 | Result.reset(new ScopDetection(F, DT, SE, LI, RI, AA, ORE)); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | void ScopDetectionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1737 | AU.addRequired<LoopInfoWrapperPass>(); |
Michael Kruse | 6a19d59 | 2016-10-17 13:29:20 +0000 | [diff] [blame] | 1738 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1739 | AU.addRequired<DominatorTreeWrapperPass>(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1740 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1741 | // We also need AA and RegionInfo when we are verifying analysis. |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1742 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1743 | AU.addRequiredTransitive<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1744 | AU.setPreservesAll(); |
| 1745 | } |
| 1746 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1747 | void ScopDetectionWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 1748 | for (const Region *R : Result->ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1749 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1750 | |
| 1751 | OS << "\n"; |
| 1752 | } |
| 1753 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1754 | ScopDetectionWrapperPass::ScopDetectionWrapperPass() : FunctionPass(ID) { |
| 1755 | // Disable runtime alias checks if we ignore aliasing all together. |
| 1756 | if (IgnoreAliasing) |
| 1757 | PollyUseRuntimeAliasChecks = false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1760 | void ScopDetectionWrapperPass::releaseMemory() { Result.reset(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1761 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1762 | char ScopDetectionWrapperPass::ID; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1763 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1764 | AnalysisKey ScopAnalysis::Key; |
| 1765 | |
| 1766 | ScopDetection ScopAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { |
| 1767 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 1768 | auto &RI = FAM.getResult<RegionInfoAnalysis>(F); |
| 1769 | auto &AA = FAM.getResult<AAManager>(F); |
| 1770 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 1771 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1772 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 1773 | return {F, DT, SE, LI, RI, AA, ORE}; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | PreservedAnalyses ScopAnalysisPrinterPass::run(Function &F, |
| 1777 | FunctionAnalysisManager &FAM) { |
| 1778 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 1779 | for (const Region *R : SD.ValidRegions) |
| 1780 | Stream << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
| 1781 | |
| 1782 | Stream << "\n"; |
| 1783 | return PreservedAnalyses::all(); |
| 1784 | } |
| 1785 | |
| 1786 | Pass *polly::createScopDetectionWrapperPassPass() { |
| 1787 | return new ScopDetectionWrapperPass(); |
| 1788 | } |
| 1789 | |
| 1790 | INITIALIZE_PASS_BEGIN(ScopDetectionWrapperPass, "polly-detect", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1791 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1792 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1793 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1794 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1795 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1796 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 1797 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1798 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1799 | INITIALIZE_PASS_END(ScopDetectionWrapperPass, "polly-detect", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1800 | "Polly - Detect static control parts (SCoPs)", false, false) |