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