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 | 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" |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 50 | #include "polly/ScopDetectionDiagnostic.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 51 | #include "polly/ScopDetection.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 52 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 53 | #include "polly/Support/ScopHelper.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" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 64 | #include "llvm/IR/LLVMContext.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 65 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 66 | #include <set> |
| 67 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 68 | using namespace llvm; |
| 69 | using namespace polly; |
| 70 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 71 | #define DEBUG_TYPE "polly-detect" |
| 72 | |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 74 | DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops", |
| 75 | cl::desc("Detect scops in functions without loops"), |
| 76 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 77 | cl::cat(PollyCategory)); |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 78 | |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 79 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 80 | DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops", |
| 81 | cl::desc("Detect scops in regions without loops"), |
| 82 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 83 | cl::cat(PollyCategory)); |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 84 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 85 | static cl::opt<std::string> OnlyFunction( |
| 86 | "polly-only-func", |
| 87 | cl::desc("Only run on functions that contain a certain string"), |
| 88 | cl::value_desc("string"), cl::ValueRequired, cl::init(""), |
| 89 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 90 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 91 | static cl::opt<std::string> OnlyRegion( |
| 92 | "polly-only-region", |
| 93 | cl::desc("Only run on certain regions (The provided identifier must " |
| 94 | "appear in the name of the region's entry block"), |
| 95 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 96 | cl::cat(PollyCategory)); |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 97 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 98 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 99 | IgnoreAliasing("polly-ignore-aliasing", |
| 100 | cl::desc("Ignore possible aliasing of the array bases"), |
| 101 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 102 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 103 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 104 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 105 | ReportLevel("polly-report", |
| 106 | cl::desc("Print information about the activities of Polly"), |
| 107 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 108 | |
| 109 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 110 | AllowNonAffine("polly-allow-nonaffine", |
| 111 | cl::desc("Allow non affine access functions in arrays"), |
| 112 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 113 | cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 114 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 115 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 116 | TrackFailures("polly-detect-track-failures", |
| 117 | cl::desc("Track failure strings in detecting scop regions"), |
| 118 | cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore, |
Andreas Simbuerger | 3efe40b | 2014-08-17 10:09:03 +0000 | [diff] [blame^] | 119 | cl::init(true), cl::cat(PollyCategory)); |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 120 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 121 | static cl::opt<bool> KeepGoing("polly-detect-keep-going", |
| 122 | cl::desc("Do not fail on the first error."), |
| 123 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 124 | cl::cat(PollyCategory)); |
| 125 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 126 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 127 | PollyDelinearizeX("polly-delinearize", |
| 128 | cl::desc("Delinearize array access functions"), |
| 129 | cl::location(PollyDelinearize), cl::Hidden, |
| 130 | cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 131 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 132 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 133 | VerifyScops("polly-detect-verify", |
| 134 | cl::desc("Verify the detected SCoPs after each transformation"), |
| 135 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 136 | cl::cat(PollyCategory)); |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 137 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 138 | bool polly::PollyTrackFailures = false; |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 139 | bool polly::PollyDelinearize = false; |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 140 | StringRef polly::PollySkipFnAttr = "polly.skip.fn"; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 141 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Statistics. |
| 144 | |
| 145 | STATISTIC(ValidRegion, "Number of regions that a valid part of Scop"); |
| 146 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 147 | class DiagnosticScopFound : public DiagnosticInfo { |
| 148 | private: |
| 149 | static int PluginDiagnosticKind; |
| 150 | |
| 151 | Function &F; |
| 152 | std::string FileName; |
| 153 | unsigned EntryLine, ExitLine; |
| 154 | |
| 155 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 156 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 157 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 158 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 159 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 160 | |
| 161 | virtual void print(DiagnosticPrinter &DP) const; |
| 162 | |
| 163 | static bool classof(const DiagnosticInfo *DI) { |
| 164 | return DI->getKind() == PluginDiagnosticKind; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | int DiagnosticScopFound::PluginDiagnosticKind = 10; |
| 169 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 170 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 171 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 172 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 173 | |
| 174 | if (FileName.empty()) { |
| 175 | DP << "Scop location is unknown. Compile with debug info " |
| 176 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 177 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 181 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 182 | } |
| 183 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | // ScopDetection. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 186 | |
| 187 | template <class RR, typename... Args> |
| 188 | inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert, |
| 189 | Args &&... Arguments) const { |
| 190 | |
| 191 | if (!Context.Verifying) { |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 192 | RejectLog &Log = Context.Log; |
| 193 | std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 194 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 195 | if (PollyTrackFailures) |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 196 | Log.report(RejectReason); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 197 | |
| 198 | DEBUG(dbgs() << RejectReason->getMessage()); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 199 | DEBUG(dbgs() << "\n"); |
| 200 | } else { |
| 201 | assert(!Assert && "Verification of detected scop failed"); |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 207 | bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const { |
| 208 | if (!ValidRegions.count(&R)) |
| 209 | return false; |
| 210 | |
| 211 | if (Verify) |
| 212 | return isValidRegion(const_cast<Region &>(R)); |
| 213 | |
| 214 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 217 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 218 | if (!RejectLogs.count(R)) |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 219 | return ""; |
| 220 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 221 | // Get the first error we found. Even in keep-going mode, this is the first |
| 222 | // reason that caused the candidate to be rejected. |
| 223 | RejectLog Errors = RejectLogs.at(R); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 224 | |
| 225 | // This can happen when we marked a region invalid, but didn't track |
| 226 | // an error for it. |
| 227 | if (Errors.size() == 0) |
| 228 | return ""; |
| 229 | |
| 230 | RejectReasonPtr RR = *Errors.begin(); |
| 231 | return RR->getMessage(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 234 | bool ScopDetection::isValidCFG(BasicBlock &BB, |
| 235 | DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 236 | Region &RefRegion = Context.CurRegion; |
| 237 | TerminatorInst *TI = BB.getTerminator(); |
| 238 | |
| 239 | // Return instructions are only valid if the region is the top level region. |
| 240 | if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0) |
| 241 | return true; |
| 242 | |
| 243 | BranchInst *Br = dyn_cast<BranchInst>(TI); |
| 244 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 245 | if (!Br) |
| 246 | return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 247 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 248 | if (Br->isUnconditional()) |
| 249 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 250 | |
| 251 | Value *Condition = Br->getCondition(); |
| 252 | |
| 253 | // UndefValue is not allowed as condition. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 254 | if (isa<UndefValue>(Condition)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 255 | return invalid<ReportUndefCond>(Context, /*Assert=*/true, Br, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 256 | |
| 257 | // Only Constant and ICmpInst are allowed as condition. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 258 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 259 | return invalid<ReportInvalidCond>(Context, /*Assert=*/true, Br, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 260 | |
| 261 | // Allow perfectly nested conditions. |
| 262 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 263 | |
| 264 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 265 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 266 | // in the code generation. |
| 267 | // |
| 268 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 269 | // well. |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 270 | if (ICmp->isUnsigned()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 271 | return false; |
| 272 | |
| 273 | // Are both operands of the ICmp affine? |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 274 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 275 | isa<UndefValue>(ICmp->getOperand(1))) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 276 | return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 277 | |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 278 | Loop *L = LI->getLoopFor(ICmp->getParent()); |
| 279 | const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L); |
| 280 | const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 281 | |
Tobias Grosser | a66fa6c | 2011-11-10 12:45:11 +0000 | [diff] [blame] | 282 | if (!isAffineExpr(&Context.CurRegion, LHS, *SE) || |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 283 | !isAffineExpr(&Context.CurRegion, RHS, *SE)) |
| 284 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 285 | RHS, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Allow loop exit conditions. |
| 289 | Loop *L = LI->getLoopFor(&BB); |
| 290 | if (L && L->getExitingBlock() == &BB) |
| 291 | return true; |
| 292 | |
| 293 | // Allow perfectly nested conditions. |
| 294 | Region *R = RI->getRegionFor(&BB); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 295 | if (R->getEntry() != &BB) |
| 296 | return invalid<ReportCondition>(Context, /*Assert=*/true, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 297 | |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
| 302 | if (CI.mayHaveSideEffects() || CI.doesNotReturn()) |
| 303 | return false; |
| 304 | |
| 305 | if (CI.doesNotAccessMemory()) |
| 306 | return true; |
| 307 | |
| 308 | Function *CalledFunction = CI.getCalledFunction(); |
| 309 | |
| 310 | // Indirect calls are not supported. |
| 311 | if (CalledFunction == 0) |
| 312 | return false; |
| 313 | |
| 314 | // TODO: Intrinsics. |
| 315 | return false; |
| 316 | } |
| 317 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 318 | bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const { |
| 319 | // A reference to function argument or constant value is invariant. |
| 320 | if (isa<Argument>(Val) || isa<Constant>(Val)) |
| 321 | return true; |
| 322 | |
| 323 | const Instruction *I = dyn_cast<Instruction>(&Val); |
| 324 | if (!I) |
| 325 | return false; |
| 326 | |
| 327 | if (!Reg.contains(I)) |
| 328 | return true; |
| 329 | |
| 330 | if (I->mayHaveSideEffects()) |
| 331 | return false; |
| 332 | |
| 333 | // When Val is a Phi node, it is likely not invariant. We do not check whether |
| 334 | // Phi nodes are actually invariant, we assume that Phi nodes are usually not |
| 335 | // invariant. Recursively checking the operators of Phi nodes would lead to |
| 336 | // infinite recursion. |
| 337 | if (isa<PHINode>(*I)) |
| 338 | return false; |
| 339 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 340 | for (const Use &Operand : I->operands()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 341 | if (!isInvariant(*Operand, Reg)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 342 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 343 | |
| 344 | // When the instruction is a load instruction, check that no write to memory |
| 345 | // in the region aliases with the load. |
| 346 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 347 | AliasAnalysis::Location Loc = AA->getLocation(LI); |
| 348 | const Region::const_block_iterator BE = Reg.block_end(); |
| 349 | // Check if any basic block in the region can modify the location pointed to |
| 350 | // by 'Loc'. If so, 'Val' is (likely) not invariant in the region. |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 351 | for (const BasicBlock *BB : Reg.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 352 | if (AA->canBasicBlockModify(*BB, Loc)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 353 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return true; |
| 357 | } |
| 358 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 359 | MapInsnToMemAcc InsnToMemAcc; |
| 360 | |
Sebastian Pop | b57c099 | 2014-05-12 20:24:26 +0000 | [diff] [blame] | 361 | bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const { |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 362 | for (auto P : Context.NonAffineAccesses) { |
| 363 | const SCEVUnknown *BasePointer = P.first; |
| 364 | Value *BaseValue = BasePointer->getValue(); |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 365 | ArrayShape *Shape = new ArrayShape(BasePointer); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 366 | |
| 367 | // First step: collect parametric terms in all array references. |
| 368 | SmallVector<const SCEV *, 4> Terms; |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 369 | for (PairInsnAddRec PIAF : Context.NonAffineAccesses[BasePointer]) |
| 370 | PIAF.second->collectParametricTerms(*SE, Terms); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 371 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 372 | // Also collect terms from the affine memory accesses. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 373 | for (PairInsnAddRec PIAF : Context.AffineAccesses[BasePointer]) |
| 374 | PIAF.second->collectParametricTerms(*SE, Terms); |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 375 | |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 376 | // Second step: find array shape. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 377 | SE->findArrayDimensions(Terms, Shape->DelinearizedSizes, |
| 378 | Context.ElementSize[BasePointer]); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 379 | |
| 380 | // Third step: compute the access functions for each subscript. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 381 | for (PairInsnAddRec PIAF : Context.NonAffineAccesses[BasePointer]) { |
| 382 | const SCEVAddRecExpr *AF = PIAF.second; |
| 383 | const Instruction *Insn = PIAF.first; |
| 384 | if (Shape->DelinearizedSizes.empty()) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 385 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, |
| 386 | Insn); |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 387 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 388 | MemAcc *Acc = new MemAcc(Insn, Shape); |
Tobias Grosser | d79029a | 2014-06-03 20:20:41 +0000 | [diff] [blame] | 389 | InsnToMemAcc.insert({Insn, Acc}); |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 390 | AF->computeAccessFunctions(*SE, Acc->DelinearizedSubscripts, |
| 391 | Shape->DelinearizedSizes); |
| 392 | if (Shape->DelinearizedSizes.empty() || |
| 393 | Acc->DelinearizedSubscripts.empty()) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 394 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, |
| 395 | Insn); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 396 | |
| 397 | // Check that the delinearized subscripts are affine. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 398 | for (const SCEV *S : Acc->DelinearizedSubscripts) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 399 | if (!isAffineExpr(&Context.CurRegion, S, *SE, BaseValue)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 400 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, |
| 401 | Insn); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 404 | return true; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 407 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 408 | DetectionContext &Context) const { |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 409 | Value *Ptr = getPointerOperand(Inst); |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 410 | Loop *L = LI->getLoopFor(Inst.getParent()); |
| 411 | const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 412 | const SCEVUnknown *BasePointer; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 413 | Value *BaseValue; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 414 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 415 | BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction)); |
| 416 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 417 | if (!BasePointer) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 418 | return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 419 | |
| 420 | BaseValue = BasePointer->getValue(); |
| 421 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 422 | if (isa<UndefValue>(BaseValue)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 423 | return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 424 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 425 | // Check that the base address of the access is invariant in the current |
| 426 | // region. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 427 | if (!isInvariant(*BaseValue, Context.CurRegion)) |
Tobias Grosser | ab2227a | 2014-01-28 13:43:24 +0000 | [diff] [blame] | 428 | // Verification of this property is difficult as the independent blocks |
| 429 | // pass may introduce aliasing that we did not have when running the |
| 430 | // scop detection. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 431 | return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue, |
| 432 | &Inst); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 433 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 434 | AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer); |
| 435 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 436 | if (AllowNonAffine) { |
| 437 | // Do not check whether AccessFunction is affine. |
Sebastian Pop | cd3bb59 | 2014-04-10 16:08:11 +0000 | [diff] [blame] | 438 | } else if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE, |
| 439 | BaseValue)) { |
| 440 | const SCEVAddRecExpr *AF = dyn_cast<SCEVAddRecExpr>(AccessFunction); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 441 | |
Sebastian Pop | cd3bb59 | 2014-04-10 16:08:11 +0000 | [diff] [blame] | 442 | if (!PollyDelinearize || !AF) |
| 443 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 444 | AccessFunction, &Inst); |
Sebastian Pop | cd3bb59 | 2014-04-10 16:08:11 +0000 | [diff] [blame] | 445 | |
Sebastian Pop | bc9009a | 2014-05-27 22:42:09 +0000 | [diff] [blame] | 446 | const SCEV *ElementSize = SE->getElementSize(&Inst); |
| 447 | Context.ElementSize[BasePointer] = ElementSize; |
| 448 | |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 449 | // Collect all non affine memory accesses, and check whether they are linear |
| 450 | // at the end of scop detection. That way we can delinearize all the memory |
| 451 | // accesses to the same array in a unique step. |
| 452 | if (Context.NonAffineAccesses[BasePointer].size() == 0) |
| 453 | Context.NonAffineAccesses[BasePointer] = AFs(); |
Tobias Grosser | d79029a | 2014-06-03 20:20:41 +0000 | [diff] [blame] | 454 | Context.NonAffineAccesses[BasePointer].push_back({&Inst, AF}); |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 455 | } else if (const SCEVAddRecExpr *AF = |
| 456 | dyn_cast<SCEVAddRecExpr>(AccessFunction)) { |
| 457 | if (Context.AffineAccesses[BasePointer].size() == 0) |
| 458 | Context.AffineAccesses[BasePointer] = AFs(); |
Tobias Grosser | d79029a | 2014-06-03 20:20:41 +0000 | [diff] [blame] | 459 | Context.AffineAccesses[BasePointer].push_back({&Inst, AF}); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 460 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 461 | |
| 462 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 463 | // created by IndependentBlocks Pass. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 464 | if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BaseValue)) |
| 465 | return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 466 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 467 | if (IgnoreAliasing) |
| 468 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 469 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 470 | // Check if the base pointer of the memory access does alias with |
| 471 | // any other pointer. This cannot be handled at the moment. |
Tobias Grosser | 298a764 | 2013-07-14 18:09:43 +0000 | [diff] [blame] | 472 | AliasSet &AS = |
| 473 | Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize, |
| 474 | Inst.getMetadata(LLVMContext::MD_tbaa)); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 475 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 476 | // INVALID triggers an assertion in verifying mode, if it detects that a |
| 477 | // SCoP was detected by SCoP detection and that this SCoP was invalidated by |
| 478 | // a pass that stated it would preserve the SCoPs. We disable this check as |
| 479 | // the independent blocks pass may create memory references which seem to |
| 480 | // alias, if -basicaa is not available. They actually do not, but as we can |
| 481 | // not proof this without -basicaa we would fail. We disable this check to |
| 482 | // not cause irrelevant verification failures. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 483 | if (!AS.isMustAlias()) |
Andreas Simbuerger | e2c9243 | 2014-06-26 10:19:57 +0000 | [diff] [blame] | 484 | return invalid<ReportAlias>(Context, /*Assert=*/false, &Inst, AS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 489 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 490 | DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 491 | if (PHINode *PN = dyn_cast<PHINode>(&Inst)) |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 492 | if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) { |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 493 | if (SCEVCodegen) |
| 494 | return invalid<ReportPhiNodeRefInRegion>(Context, /*Assert=*/true, |
| 495 | &Inst); |
| 496 | else |
| 497 | return invalid<ReportNonCanonicalPhiNode>(Context, /*Assert=*/true, |
| 498 | &Inst); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 499 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 500 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 501 | // We only check the call instruction but not invoke instruction. |
| 502 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 503 | if (isValidCallInst(*CI)) |
| 504 | return true; |
| 505 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 506 | return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 510 | if (!isa<AllocaInst>(Inst)) |
| 511 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 512 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 513 | return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Check the access function. |
| 517 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) |
| 518 | return isValidMemoryAccess(Inst, Context); |
| 519 | |
| 520 | // We do not know this instruction, therefore we assume it is invalid. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 521 | return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 524 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 525 | if (!SCEVCodegen) { |
| 526 | // If code generation is not in scev based mode, we need to ensure that |
| 527 | // each loop has a canonical induction variable. |
| 528 | PHINode *IndVar = L->getCanonicalInductionVariable(); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 529 | if (!IndVar) |
| 530 | return invalid<ReportLoopHeader>(Context, /*Assert=*/true, L); |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 531 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 532 | |
| 533 | // Is the loop count affine? |
| 534 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 535 | if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) |
| 536 | return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 537 | |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 542 | // Initial no valid region was found (greater than R) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 543 | Region *LastValidRegion = nullptr; |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 544 | Region *ExpandedRegion = R.getExpandedRegion(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 545 | |
| 546 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 547 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 548 | while (ExpandedRegion) { |
| 549 | DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */); |
| 550 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 551 | // Only expand when we did not collect errors. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 552 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 553 | // Check the exit first (cheap) |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 554 | if (isValidExit(Context) && !Context.Log.hasErrors()) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 555 | // If the exit is valid check all blocks |
| 556 | // - if true, a valid region was found => store it + keep expanding |
| 557 | // - if false, .tbd. => stop (should this really end the loop?) |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 558 | if (!allBlocksValid(Context) || Context.Log.hasErrors()) |
| 559 | break; |
| 560 | |
| 561 | if (Context.Log.hasErrors()) |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 562 | break; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 563 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 564 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 565 | if (LastValidRegion) |
| 566 | delete LastValidRegion; |
| 567 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 568 | // Store this region, because it is the greatest valid (encountered so |
| 569 | // far). |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 570 | LastValidRegion = ExpandedRegion; |
| 571 | |
| 572 | // Create and test the next greater region (if any) |
| 573 | ExpandedRegion = ExpandedRegion->getExpandedRegion(); |
| 574 | |
| 575 | } else { |
| 576 | // Create and test the next greater region (if any) |
| 577 | Region *TmpRegion = ExpandedRegion->getExpandedRegion(); |
| 578 | |
| 579 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 580 | delete ExpandedRegion; |
| 581 | |
| 582 | ExpandedRegion = TmpRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 583 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 586 | DEBUG({ |
| 587 | if (LastValidRegion) |
| 588 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 589 | else |
| 590 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 591 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 592 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 593 | return LastValidRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 594 | } |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 595 | static bool regionWithoutLoops(Region &R, LoopInfo *LI) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 596 | for (const BasicBlock *BB : R.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 597 | if (R.contains(LI->getLoopFor(BB))) |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 598 | return false; |
| 599 | |
| 600 | return true; |
| 601 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 602 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 603 | // Remove all direct and indirect children of region R from the region set Regs, |
| 604 | // but do not recurse further if the first child has been found. |
| 605 | // |
| 606 | // Return the number of regions erased from Regs. |
| 607 | static unsigned eraseAllChildren(std::set<const Region *> &Regs, |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 608 | const Region &R) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 609 | unsigned Count = 0; |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 610 | for (auto &SubRegion : R) { |
| 611 | if (Regs.find(SubRegion.get()) != Regs.end()) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 612 | ++Count; |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 613 | Regs.erase(SubRegion.get()); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 614 | } else { |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 615 | Count += eraseAllChildren(Regs, *SubRegion); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | return Count; |
| 619 | } |
| 620 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 621 | void ScopDetection::findScops(Region &R) { |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 622 | if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI)) |
| 623 | return; |
| 624 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 625 | bool IsValidRegion = isValidRegion(R); |
| 626 | bool HasErrors = RejectLogs.count(&R) > 0; |
| 627 | |
| 628 | if (IsValidRegion && !HasErrors) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 629 | ++ValidRegion; |
| 630 | ValidRegions.insert(&R); |
| 631 | return; |
| 632 | } |
| 633 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 634 | for (auto &SubRegion : R) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 635 | findScops(*SubRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 636 | |
| 637 | // Try to expand regions. |
| 638 | // |
| 639 | // As the region tree normally only contains canonical regions, non canonical |
| 640 | // regions that form a Scop are not found. Therefore, those non canonical |
| 641 | // regions are checked by expanding the canonical ones. |
| 642 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 643 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 644 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 645 | for (auto &SubRegion : R) |
| 646 | ToExpand.push_back(SubRegion.get()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 647 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 648 | for (Region *CurrentRegion : ToExpand) { |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 649 | // Skip regions that had errors. |
| 650 | bool HadErrors = RejectLogs.hasErrors(CurrentRegion); |
| 651 | if (HadErrors) |
| 652 | continue; |
| 653 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 654 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 655 | // an already expanded region. |
| 656 | if (ValidRegions.find(CurrentRegion) == ValidRegions.end()) |
| 657 | continue; |
| 658 | |
| 659 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 660 | |
| 661 | if (!ExpandedR) |
| 662 | continue; |
| 663 | |
| 664 | R.addSubRegion(ExpandedR, true); |
| 665 | ValidRegions.insert(ExpandedR); |
| 666 | ValidRegions.erase(CurrentRegion); |
| 667 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 668 | // Erase all (direct and indirect) children of ExpandedR from the valid |
| 669 | // regions and update the number of valid regions. |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 670 | ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
| 675 | Region &R = Context.CurRegion; |
| 676 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 677 | for (const BasicBlock *BB : R.blocks()) { |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 678 | Loop *L = LI->getLoopFor(BB); |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 679 | if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing)) |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 683 | for (BasicBlock *BB : R.blocks()) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 684 | if (!isValidCFG(*BB, Context) && !KeepGoing) |
Sebastian Pop | 9e3d2dd | 2013-06-11 22:20:27 +0000 | [diff] [blame] | 685 | return false; |
| 686 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 687 | for (BasicBlock *BB : R.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 688 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 689 | if (!isValidInstruction(*I, Context) && !KeepGoing) |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 690 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 691 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 692 | if (!hasAffineMemoryAccesses(Context)) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 693 | return false; |
| 694 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 695 | return true; |
| 696 | } |
| 697 | |
| 698 | bool ScopDetection::isValidExit(DetectionContext &Context) const { |
| 699 | Region &R = Context.CurRegion; |
| 700 | |
| 701 | // PHI nodes are not allowed in the exit basic block. |
| 702 | if (BasicBlock *Exit = R.getExit()) { |
| 703 | BasicBlock::iterator I = Exit->begin(); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 704 | if (I != Exit->end() && isa<PHINode>(*I)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 705 | return invalid<ReportPHIinExit>(Context, /*Assert=*/true, I); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | return true; |
| 709 | } |
| 710 | |
Tobias Grosser | 9b1100b | 2014-02-18 18:49:46 +0000 | [diff] [blame] | 711 | bool ScopDetection::isValidRegion(Region &R) const { |
| 712 | DetectionContext Context(R, *AA, false /*verifying*/); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 713 | |
| 714 | bool RegionIsValid = isValidRegion(Context); |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 715 | bool HasErrors = !RegionIsValid || Context.Log.size() > 0; |
| 716 | |
Andreas Simbuerger | 5bf774c | 2014-06-26 13:36:52 +0000 | [diff] [blame] | 717 | if (PollyTrackFailures && HasErrors) |
| 718 | RejectLogs.insert(std::make_pair(&R, Context.Log)); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 719 | |
| 720 | return RegionIsValid; |
Tobias Grosser | 9b1100b | 2014-02-18 18:49:46 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 723 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
| 724 | Region &R = Context.CurRegion; |
| 725 | |
| 726 | DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t"); |
| 727 | |
Tobias Grosser | aeabcf2 | 2013-04-02 06:41:48 +0000 | [diff] [blame] | 728 | if (R.isTopLevelRegion()) { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 729 | DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 733 | if (!R.getEntry()->getName().count(OnlyRegion)) { |
| 734 | DEBUG({ |
| 735 | dbgs() << "Region entry does not match -polly-region-only"; |
| 736 | dbgs() << "\n"; |
| 737 | }); |
| 738 | return false; |
| 739 | } |
| 740 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 741 | if (!R.getEnteringBlock()) { |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 742 | BasicBlock *entry = R.getEntry(); |
| 743 | Loop *L = LI->getLoopFor(entry); |
| 744 | |
| 745 | if (L) { |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 746 | if (!L->isLoopSimplifyForm()) |
| 747 | return invalid<ReportSimpleLoop>(Context, /*Assert=*/true); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 748 | |
| 749 | for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE; |
| 750 | ++PI) { |
| 751 | // Region entering edges come from the same loop but outside the region |
| 752 | // are not allowed. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 753 | if (L->contains(*PI) && !R.contains(*PI)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 754 | return invalid<ReportIndEdge>(Context, /*Assert=*/true, *PI); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 759 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 760 | // to insert alloca instruction there when translate scalar to array. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 761 | if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 762 | return invalid<ReportEntry>(Context, /*Assert=*/true, R.getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 763 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 764 | if (!isValidExit(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 765 | return false; |
| 766 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 767 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 768 | return false; |
| 769 | |
| 770 | DEBUG(dbgs() << "OK\n"); |
| 771 | return true; |
| 772 | } |
| 773 | |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 774 | void ScopDetection::markFunctionAsInvalid(Function *F) const { |
| 775 | F->addFnAttr(PollySkipFnAttr); |
| 776 | } |
| 777 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 778 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 779 | return !F.hasFnAttribute(PollySkipFnAttr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 782 | void ScopDetection::printLocations(llvm::Function &F) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 783 | for (const Region *R : *this) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 784 | unsigned LineEntry, LineExit; |
| 785 | std::string FileName; |
| 786 | |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 787 | getDebugLocation(R, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 788 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 789 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 793 | void |
| 794 | ScopDetection::emitMissedRemarksForValidRegions(const Function &F, |
| 795 | const RegionSet &ValidRegions) { |
| 796 | for (const Region *R : ValidRegions) { |
| 797 | const Region *Parent = R->getParent(); |
| 798 | if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent)) |
| 799 | emitRejectionRemarks(F, RejectLogs.at(Parent)); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | void ScopDetection::emitMissedRemarksForLeaves(const Function &F, |
| 804 | const Region *R) { |
| 805 | for (const std::unique_ptr<Region> &Child : *R) { |
| 806 | bool IsValid = ValidRegions.count(Child.get()); |
| 807 | if (IsValid) |
| 808 | continue; |
| 809 | |
| 810 | bool IsLeaf = Child->begin() == Child->end(); |
| 811 | if (!IsLeaf) |
| 812 | emitMissedRemarksForLeaves(F, Child.get()); |
| 813 | else { |
| 814 | if (RejectLogs.count(Child.get())) { |
| 815 | emitRejectionRemarks(F, RejectLogs.at(Child.get())); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 821 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 822 | LI = &getAnalysis<LoopInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 823 | RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 824 | if (!DetectScopsWithoutLoops && LI->empty()) |
| 825 | return false; |
| 826 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 827 | AA = &getAnalysis<AliasAnalysis>(); |
| 828 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 829 | Region *TopRegion = RI->getTopLevelRegion(); |
| 830 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 831 | releaseMemory(); |
| 832 | |
Tobias Grosser | a3ab27e | 2014-05-07 11:23:32 +0000 | [diff] [blame] | 833 | if (OnlyFunction != "" && !F.getName().count(OnlyFunction)) |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 834 | return false; |
| 835 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 836 | if (!isValidFunction(F)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 837 | return false; |
| 838 | |
| 839 | findScops(*TopRegion); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 840 | |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 841 | // Only makes sense when we tracked errors. |
| 842 | if (PollyTrackFailures) { |
| 843 | emitMissedRemarksForValidRegions(F, ValidRegions); |
| 844 | emitMissedRemarksForLeaves(F, TopRegion); |
| 845 | } |
| 846 | |
| 847 | for (const Region *R : ValidRegions) |
| 848 | emitValidRemarks(F, R); |
| 849 | |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 850 | if (ReportLevel >= 1) |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 851 | printLocations(F); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 852 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 853 | return false; |
| 854 | } |
| 855 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 856 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 857 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 858 | DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 859 | isValidRegion(Context); |
| 860 | } |
| 861 | |
| 862 | void polly::ScopDetection::verifyAnalysis() const { |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 863 | if (!VerifyScops) |
| 864 | return; |
| 865 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 866 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 867 | verifyRegion(*R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 871 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 872 | AU.addRequired<PostDominatorTree>(); |
| 873 | AU.addRequired<LoopInfo>(); |
| 874 | AU.addRequired<ScalarEvolution>(); |
| 875 | // We also need AA and RegionInfo when we are verifying analysis. |
| 876 | AU.addRequiredTransitive<AliasAnalysis>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 877 | AU.addRequiredTransitive<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 878 | AU.setPreservesAll(); |
| 879 | } |
| 880 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 881 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 882 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 883 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 884 | |
| 885 | OS << "\n"; |
| 886 | } |
| 887 | |
| 888 | void ScopDetection::releaseMemory() { |
| 889 | ValidRegions.clear(); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 890 | RejectLogs.clear(); |
| 891 | |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 892 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | char ScopDetection::ID = 0; |
| 896 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 897 | Pass *polly::createScopDetectionPass() { return new ScopDetection(); } |
| 898 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 899 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 900 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 901 | false); |
| 902 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 903 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 904 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
| 905 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTree); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 906 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 907 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 908 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 909 | "Polly - Detect static control parts (SCoPs)", false, false) |