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