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