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