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