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