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