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