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