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