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