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