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