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