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