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" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 50 | #include "polly/ScopDetection.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 51 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 52 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/Statistic.h" |
| 54 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 55 | #include "llvm/Analysis/LoopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 56 | #include "llvm/Analysis/RegionIterator.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 59 | #include "llvm/DebugInfo.h" |
| 60 | #include "llvm/IR/LLVMContext.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" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 63 | |
| 64 | #define DEBUG_TYPE "polly-detect" |
| 65 | #include "llvm/Support/Debug.h" |
| 66 | |
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 | |
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"), |
| 75 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 76 | |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 77 | static cl::opt<bool> |
| 78 | DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops", |
| 79 | cl::desc("Detect scops in regions without loops"), |
| 80 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 81 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 82 | static cl::opt<std::string> |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 83 | OnlyFunction("polly-only-func", cl::desc("Only run on a single function"), |
| 84 | cl::value_desc("function-name"), cl::ValueRequired, cl::init(""), |
| 85 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 86 | |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 87 | static cl::opt<std::string> |
| 88 | OnlyRegion("polly-only-region", |
| 89 | cl::desc("Only run on certain regions (The provided identifier must " |
| 90 | "appear in the name of the region's entry block"), |
| 91 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 92 | cl::cat(PollyCategory)); |
| 93 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 94 | static cl::opt<bool> |
| 95 | IgnoreAliasing("polly-ignore-aliasing", |
| 96 | cl::desc("Ignore possible aliasing of the array bases"), |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 97 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 98 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 99 | static cl::opt<bool> |
| 100 | ReportLevel("polly-report", |
| 101 | cl::desc("Print information about the activities of Polly"), |
| 102 | cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 103 | |
| 104 | static cl::opt<bool> |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 105 | AllowNonAffine("polly-allow-nonaffine", |
| 106 | cl::desc("Allow non affine access functions in arrays"), |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 107 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 108 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 109 | static cl::opt<bool, true> |
| 110 | TrackFailures("polly-detect-track-failures", |
| 111 | cl::desc("Track failure strings in detecting scop regions"), |
| 112 | cl::location(PollyTrackFailures), cl::Hidden, cl::init(false), |
| 113 | cl::cat(PollyCategory)); |
| 114 | |
| 115 | bool polly::PollyTrackFailures = false; |
| 116 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
| 118 | // Statistics. |
| 119 | |
| 120 | STATISTIC(ValidRegion, "Number of regions that a valid part of Scop"); |
| 121 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 122 | #define BADSCOP_STAT(NAME, DESC) \ |
| 123 | STATISTIC(Bad##NAME##ForScop, "Number of bad regions for Scop: " DESC) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 124 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 125 | #define INVALID(NAME, MESSAGE) \ |
| 126 | do { \ |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 127 | if (PollyTrackFailures) { \ |
| 128 | std::string Buf; \ |
| 129 | raw_string_ostream fmt(Buf); \ |
| 130 | fmt << MESSAGE; \ |
| 131 | fmt.flush(); \ |
| 132 | LastFailure = Buf; \ |
| 133 | } \ |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 134 | DEBUG(dbgs() << MESSAGE); \ |
| 135 | DEBUG(dbgs() << "\n"); \ |
Tobias Grosser | 1c84d80 | 2013-11-16 01:07:06 +0000 | [diff] [blame] | 136 | assert(!Context.Verifying && #NAME); \ |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 137 | if (!Context.Verifying) \ |
| 138 | ++Bad##NAME##ForScop; \ |
Tobias Grosser | 84b81de | 2013-03-19 21:44:07 +0000 | [diff] [blame] | 139 | } while (0) |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 140 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 141 | #define INVALID_NOVERIFY(NAME, MESSAGE) \ |
| 142 | do { \ |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 143 | if (PollyTrackFailures) { \ |
| 144 | std::string Buf; \ |
| 145 | raw_string_ostream fmt(Buf); \ |
| 146 | fmt << MESSAGE; \ |
| 147 | fmt.flush(); \ |
| 148 | LastFailure = Buf; \ |
| 149 | } \ |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 150 | DEBUG(dbgs() << MESSAGE); \ |
| 151 | DEBUG(dbgs() << "\n"); \ |
| 152 | /* DISABLED: assert(!Context.Verifying && #NAME); */ \ |
| 153 | if (!Context.Verifying) \ |
| 154 | ++Bad##NAME##ForScop; \ |
Tobias Grosser | 84b81de | 2013-03-19 21:44:07 +0000 | [diff] [blame] | 155 | } while (0) |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 156 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 157 | BADSCOP_STAT(CFG, "CFG too complex"); |
| 158 | BADSCOP_STAT(IndVar, "Non canonical induction variable in loop"); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 159 | BADSCOP_STAT(IndEdge, "Found invalid region entering edges"); |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 160 | BADSCOP_STAT(LoopBound, "Loop bounds can not be computed"); |
| 161 | BADSCOP_STAT(FuncCall, "Function call with side effects appeared"); |
| 162 | BADSCOP_STAT(AffFunc, "Expression not affine"); |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 163 | BADSCOP_STAT(Alias, "Found base address alias"); |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 164 | BADSCOP_STAT(SimpleLoop, "Loop not in -loop-simplify form"); |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 165 | BADSCOP_STAT(Other, "Others"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 166 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 167 | class DiagnosticScopFound : public DiagnosticInfo { |
| 168 | private: |
| 169 | static int PluginDiagnosticKind; |
| 170 | |
| 171 | Function &F; |
| 172 | std::string FileName; |
| 173 | unsigned EntryLine, ExitLine; |
| 174 | |
| 175 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 176 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 177 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 178 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 179 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 180 | |
| 181 | virtual void print(DiagnosticPrinter &DP) const; |
| 182 | |
| 183 | static bool classof(const DiagnosticInfo *DI) { |
| 184 | return DI->getKind() == PluginDiagnosticKind; |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | int DiagnosticScopFound::PluginDiagnosticKind = 10; |
| 189 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 190 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
| 191 | |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 192 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 193 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 194 | |
| 195 | if (FileName.empty()) { |
| 196 | DP << "Scop location is unknown. Compile with debug info " |
| 197 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 198 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 202 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 203 | } |
| 204 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | // ScopDetection. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 207 | bool ScopDetection::isMaxRegionInScop(const Region &R) const { |
| 208 | // The Region is valid only if it could be found in the set. |
| 209 | return ValidRegions.count(&R); |
| 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 | |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 230 | if (!Br) { |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 231 | INVALID(CFG, "Non branch instruction terminates BB: " + BB.getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 234 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 235 | if (Br->isUnconditional()) |
| 236 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 237 | |
| 238 | Value *Condition = Br->getCondition(); |
| 239 | |
| 240 | // UndefValue is not allowed as condition. |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 241 | if (isa<UndefValue>(Condition)) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 242 | INVALID(AffFunc, "Condition based on 'undef' value in BB: " + BB.getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 245 | |
| 246 | // Only Constant and ICmpInst are allowed as condition. |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 247 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 248 | INVALID(AffFunc, "Condition in BB '" + BB.getName() + |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 249 | "' neither constant nor an icmp instruction"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 252 | |
| 253 | // Allow perfectly nested conditions. |
| 254 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 255 | |
| 256 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 257 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 258 | // in the code generation. |
| 259 | // |
| 260 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 261 | // well. |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 262 | if (ICmp->isUnsigned()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 263 | return false; |
| 264 | |
| 265 | // Are both operands of the ICmp affine? |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 266 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 267 | isa<UndefValue>(ICmp->getOperand(1))) { |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 268 | INVALID(AffFunc, "undef operand in branch at BB: " + BB.getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 271 | |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 272 | Loop *L = LI->getLoopFor(ICmp->getParent()); |
| 273 | const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L); |
| 274 | const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 275 | |
Tobias Grosser | a66fa6c | 2011-11-10 12:45:11 +0000 | [diff] [blame] | 276 | if (!isAffineExpr(&Context.CurRegion, LHS, *SE) || |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 277 | !isAffineExpr(&Context.CurRegion, RHS, *SE)) { |
Tobias Grosser | 983e785 | 2013-07-28 09:05:20 +0000 | [diff] [blame] | 278 | INVALID(AffFunc, "Non affine branch in BB '" << BB.getName() |
| 279 | << "' with LHS: " << *LHS |
| 280 | << " and RHS: " << *RHS); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Allow loop exit conditions. |
| 286 | Loop *L = LI->getLoopFor(&BB); |
| 287 | if (L && L->getExitingBlock() == &BB) |
| 288 | return true; |
| 289 | |
| 290 | // Allow perfectly nested conditions. |
| 291 | Region *R = RI->getRegionFor(&BB); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 292 | if (R->getEntry() != &BB) { |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 293 | INVALID(CFG, "Not well structured condition at BB: " + BB.getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 294 | return false; |
| 295 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
| 301 | if (CI.mayHaveSideEffects() || CI.doesNotReturn()) |
| 302 | return false; |
| 303 | |
| 304 | if (CI.doesNotAccessMemory()) |
| 305 | return true; |
| 306 | |
| 307 | Function *CalledFunction = CI.getCalledFunction(); |
| 308 | |
| 309 | // Indirect calls are not supported. |
| 310 | if (CalledFunction == 0) |
| 311 | return false; |
| 312 | |
| 313 | // TODO: Intrinsics. |
| 314 | return false; |
| 315 | } |
| 316 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 317 | std::string ScopDetection::formatInvalidAlias(AliasSet &AS) const { |
| 318 | std::string Message; |
| 319 | raw_string_ostream OS(Message); |
| 320 | |
| 321 | OS << "Possible aliasing: "; |
| 322 | |
| 323 | std::vector<Value *> Pointers; |
| 324 | |
| 325 | for (AliasSet::iterator AI = AS.begin(), AE = AS.end(); AI != AE; ++AI) |
| 326 | Pointers.push_back(AI.getPointer()); |
| 327 | |
| 328 | std::sort(Pointers.begin(), Pointers.end()); |
| 329 | |
| 330 | for (std::vector<Value *>::iterator PI = Pointers.begin(), |
| 331 | PE = Pointers.end(); |
| 332 | ;) { |
| 333 | Value *V = *PI; |
| 334 | |
| 335 | if (V->getName().size() == 0) |
| 336 | OS << "\"" << *V << "\""; |
| 337 | else |
| 338 | OS << "\"" << V->getName() << "\""; |
| 339 | |
| 340 | ++PI; |
| 341 | |
| 342 | if (PI != PE) |
| 343 | OS << ", "; |
| 344 | else |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | return OS.str(); |
| 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 | |
| 373 | // Check that all operands of the instruction are |
| 374 | // themselves invariant. |
| 375 | const Instruction::const_op_iterator OE = I->op_end(); |
| 376 | for (Instruction::const_op_iterator OI = I->op_begin(); OI != OE; ++OI) { |
| 377 | if (!isInvariant(**OI, Reg)) |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | // When the instruction is a load instruction, check that no write to memory |
| 382 | // in the region aliases with the load. |
| 383 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 384 | AliasAnalysis::Location Loc = AA->getLocation(LI); |
| 385 | const Region::const_block_iterator BE = Reg.block_end(); |
| 386 | // Check if any basic block in the region can modify the location pointed to |
| 387 | // by 'Loc'. If so, 'Val' is (likely) not invariant in the region. |
| 388 | for (Region::const_block_iterator BI = Reg.block_begin(); BI != BE; ++BI) { |
| 389 | const BasicBlock &BB = **BI; |
| 390 | if (AA->canBasicBlockModify(BB, Loc)) |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 398 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 399 | DetectionContext &Context) const { |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 400 | Value *Ptr = getPointerOperand(Inst); |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 401 | Loop *L = LI->getLoopFor(Inst.getParent()); |
| 402 | const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 403 | const SCEVUnknown *BasePointer; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 404 | Value *BaseValue; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 405 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 406 | BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction)); |
| 407 | |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 408 | if (!BasePointer) { |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 409 | INVALID(AffFunc, "No base pointer"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 412 | |
| 413 | BaseValue = BasePointer->getValue(); |
| 414 | |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 415 | if (isa<UndefValue>(BaseValue)) { |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 416 | INVALID(AffFunc, "Undefined base pointer"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 417 | return false; |
| 418 | } |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 419 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 420 | // Check that the base address of the access is invariant in the current |
| 421 | // region. |
| 422 | if (!isInvariant(*BaseValue, Context.CurRegion)) { |
Tobias Grosser | ab2227a | 2014-01-28 13:43:24 +0000 | [diff] [blame] | 423 | // Verification of this property is difficult as the independent blocks |
| 424 | // pass may introduce aliasing that we did not have when running the |
| 425 | // scop detection. |
| 426 | INVALID_NOVERIFY( |
| 427 | AffFunc, "Base address not invariant in current region:" << *BaseValue); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 431 | AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer); |
| 432 | |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 433 | if (!AllowNonAffine && |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 434 | !isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue)) { |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 435 | INVALID(AffFunc, "Non affine access function: " << *AccessFunction); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 436 | return false; |
| 437 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 438 | |
| 439 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 440 | // created by IndependentBlocks Pass. |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 441 | if (isa<IntToPtrInst>(BaseValue)) { |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 442 | INVALID(Other, "Find bad intToptr prt: " << *BaseValue); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 445 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 446 | if (IgnoreAliasing) |
| 447 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 448 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 449 | // Check if the base pointer of the memory access does alias with |
| 450 | // any other pointer. This cannot be handled at the moment. |
Tobias Grosser | 298a764 | 2013-07-14 18:09:43 +0000 | [diff] [blame] | 451 | AliasSet &AS = |
| 452 | Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize, |
| 453 | Inst.getMetadata(LLVMContext::MD_tbaa)); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 454 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 455 | // INVALID triggers an assertion in verifying mode, if it detects that a |
| 456 | // SCoP was detected by SCoP detection and that this SCoP was invalidated by |
| 457 | // a pass that stated it would preserve the SCoPs. We disable this check as |
| 458 | // the independent blocks pass may create memory references which seem to |
| 459 | // alias, if -basicaa is not available. They actually do not, but as we can |
| 460 | // not proof this without -basicaa we would fail. We disable this check to |
| 461 | // not cause irrelevant verification failures. |
| 462 | if (!AS.isMustAlias()) { |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 463 | INVALID_NOVERIFY(Alias, formatInvalidAlias(AS)); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 464 | return false; |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 465 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 470 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 471 | DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 472 | if (PHINode *PN = dyn_cast<PHINode>(&Inst)) |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 473 | if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 474 | if (SCEVCodegen) { |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 475 | INVALID(IndVar, |
| 476 | "SCEV of PHI node refers to SSA names in region: " << Inst); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 477 | return false; |
| 478 | |
| 479 | } else { |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 480 | INVALID(IndVar, "Non canonical PHI node: " << Inst); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 481 | return false; |
| 482 | } |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 483 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 484 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 485 | // We only check the call instruction but not invoke instruction. |
| 486 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 487 | if (isValidCallInst(*CI)) |
| 488 | return true; |
| 489 | |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 490 | INVALID(FuncCall, "Call instruction: " << Inst); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 491 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 495 | if (!isa<AllocaInst>(Inst)) |
| 496 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 497 | |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 498 | INVALID(Other, "Alloca instruction: " << Inst); |
| 499 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // Check the access function. |
| 503 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) |
| 504 | return isValidMemoryAccess(Inst, Context); |
| 505 | |
| 506 | // We do not know this instruction, therefore we assume it is invalid. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 507 | INVALID(Other, "Unknown instruction: " << Inst); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 508 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 511 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 512 | if (!SCEVCodegen) { |
| 513 | // If code generation is not in scev based mode, we need to ensure that |
| 514 | // each loop has a canonical induction variable. |
| 515 | PHINode *IndVar = L->getCanonicalInductionVariable(); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 516 | if (!IndVar) { |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 517 | INVALID(IndVar, |
| 518 | "No canonical IV at loop header: " << L->getHeader()->getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 521 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 522 | |
| 523 | // Is the loop count affine? |
| 524 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 525 | if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) { |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 526 | INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: " |
| 527 | << L->getHeader()->getName()); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 528 | return false; |
| 529 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 535 | // Initial no valid region was found (greater than R) |
| 536 | Region *LastValidRegion = NULL; |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 537 | Region *ExpandedRegion = R.getExpandedRegion(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 538 | |
| 539 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 540 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 541 | while (ExpandedRegion) { |
| 542 | DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */); |
| 543 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 544 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 545 | // Check the exit first (cheap) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 546 | if (isValidExit(Context)) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 547 | // If the exit is valid check all blocks |
| 548 | // - if true, a valid region was found => store it + keep expanding |
| 549 | // - if false, .tbd. => stop (should this really end the loop?) |
| 550 | if (!allBlocksValid(Context)) |
| 551 | break; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 552 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 553 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 554 | if (LastValidRegion) |
| 555 | delete LastValidRegion; |
| 556 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 557 | // Store this region, because it is the greatest valid (encountered so |
| 558 | // far). |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 559 | LastValidRegion = ExpandedRegion; |
| 560 | |
| 561 | // Create and test the next greater region (if any) |
| 562 | ExpandedRegion = ExpandedRegion->getExpandedRegion(); |
| 563 | |
| 564 | } else { |
| 565 | // Create and test the next greater region (if any) |
| 566 | Region *TmpRegion = ExpandedRegion->getExpandedRegion(); |
| 567 | |
| 568 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 569 | delete ExpandedRegion; |
| 570 | |
| 571 | ExpandedRegion = TmpRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 572 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 575 | DEBUG({ |
| 576 | if (LastValidRegion) |
| 577 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 578 | else |
| 579 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 580 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 581 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 582 | return LastValidRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 583 | } |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 584 | static bool regionWithoutLoops(Region &R, LoopInfo *LI) { |
| 585 | for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E; |
| 586 | ++I) |
| 587 | if (R.contains(LI->getLoopFor(*I))) |
| 588 | return false; |
| 589 | |
| 590 | return true; |
| 591 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 592 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 593 | // Remove all direct and indirect children of region R from the region set Regs, |
| 594 | // but do not recurse further if the first child has been found. |
| 595 | // |
| 596 | // Return the number of regions erased from Regs. |
| 597 | static unsigned eraseAllChildren(std::set<const Region *> &Regs, |
| 598 | const Region *R) { |
| 599 | unsigned Count = 0; |
| 600 | for (Region::const_iterator I = R->begin(), E = R->end(); I != E; ++I) { |
| 601 | if (Regs.find(*I) != Regs.end()) { |
| 602 | ++Count; |
| 603 | Regs.erase(*I); |
| 604 | } else { |
| 605 | Count += eraseAllChildren(Regs, *I); |
| 606 | } |
| 607 | } |
| 608 | return Count; |
| 609 | } |
| 610 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 611 | void ScopDetection::findScops(Region &R) { |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 612 | |
| 613 | if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI)) |
| 614 | return; |
| 615 | |
Tobias Grosser | 4eb7381 | 2011-11-10 12:45:15 +0000 | [diff] [blame] | 616 | LastFailure = ""; |
| 617 | |
Tobias Grosser | 9b1100b | 2014-02-18 18:49:46 +0000 | [diff] [blame^] | 618 | if (isValidRegion(R)) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 619 | ++ValidRegion; |
| 620 | ValidRegions.insert(&R); |
| 621 | return; |
| 622 | } |
| 623 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 624 | InvalidRegions[&R] = LastFailure; |
| 625 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 626 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 627 | findScops(**I); |
| 628 | |
| 629 | // Try to expand regions. |
| 630 | // |
| 631 | // As the region tree normally only contains canonical regions, non canonical |
| 632 | // regions that form a Scop are not found. Therefore, those non canonical |
| 633 | // regions are checked by expanding the canonical ones. |
| 634 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 635 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 636 | |
| 637 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 638 | ToExpand.push_back(*I); |
| 639 | |
Tobias Grosser | af3c00b8 | 2013-02-05 09:40:22 +0000 | [diff] [blame] | 640 | for (std::vector<Region *>::iterator RI = ToExpand.begin(), |
| 641 | RE = ToExpand.end(); |
| 642 | RI != RE; ++RI) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 643 | Region *CurrentRegion = *RI; |
| 644 | |
| 645 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 646 | // an already expanded region. |
| 647 | if (ValidRegions.find(CurrentRegion) == ValidRegions.end()) |
| 648 | continue; |
| 649 | |
| 650 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 651 | |
| 652 | if (!ExpandedR) |
| 653 | continue; |
| 654 | |
| 655 | R.addSubRegion(ExpandedR, true); |
| 656 | ValidRegions.insert(ExpandedR); |
| 657 | ValidRegions.erase(CurrentRegion); |
| 658 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 659 | // Erase all (direct and indirect) children of ExpandedR from the valid |
| 660 | // regions and update the number of valid regions. |
| 661 | ValidRegion -= eraseAllChildren(ValidRegions, ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
| 666 | Region &R = Context.CurRegion; |
| 667 | |
| 668 | for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E; |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 669 | ++I) { |
| 670 | Loop *L = LI->getLoopFor(*I); |
| 671 | if (L && L->getHeader() == *I && !isValidLoop(L, Context)) |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 676 | ++I) |
Sebastian Pop | 9e3d2dd | 2013-06-11 22:20:27 +0000 | [diff] [blame] | 677 | if (!isValidCFG(**I, Context)) |
| 678 | return false; |
| 679 | |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 680 | for (Region::block_iterator BI = R.block_begin(), E = R.block_end(); BI != E; |
| 681 | ++BI) |
| 682 | for (BasicBlock::iterator I = (*BI)->begin(), E = --(*BI)->end(); I != E; |
| 683 | ++I) |
| 684 | if (!isValidInstruction(*I, Context)) |
| 685 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool ScopDetection::isValidExit(DetectionContext &Context) const { |
| 691 | Region &R = Context.CurRegion; |
| 692 | |
| 693 | // PHI nodes are not allowed in the exit basic block. |
| 694 | if (BasicBlock *Exit = R.getExit()) { |
| 695 | BasicBlock::iterator I = Exit->begin(); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 696 | if (I != Exit->end() && isa<PHINode>(*I)) { |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 697 | INVALID(Other, "PHI node in exit BB"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 698 | return false; |
| 699 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | return true; |
| 703 | } |
| 704 | |
Tobias Grosser | 9b1100b | 2014-02-18 18:49:46 +0000 | [diff] [blame^] | 705 | bool ScopDetection::isValidRegion(Region &R) const { |
| 706 | DetectionContext Context(R, *AA, false /*verifying*/); |
| 707 | return isValidRegion(Context); |
| 708 | } |
| 709 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 710 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
| 711 | Region &R = Context.CurRegion; |
| 712 | |
| 713 | DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t"); |
| 714 | |
Tobias Grosser | aeabcf2 | 2013-04-02 06:41:48 +0000 | [diff] [blame] | 715 | if (R.isTopLevelRegion()) { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 716 | DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 717 | return false; |
| 718 | } |
| 719 | |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 720 | if (!R.getEntry()->getName().count(OnlyRegion)) { |
| 721 | DEBUG({ |
| 722 | dbgs() << "Region entry does not match -polly-region-only"; |
| 723 | dbgs() << "\n"; |
| 724 | }); |
| 725 | return false; |
| 726 | } |
| 727 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 728 | if (!R.getEnteringBlock()) { |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 729 | BasicBlock *entry = R.getEntry(); |
| 730 | Loop *L = LI->getLoopFor(entry); |
| 731 | |
| 732 | if (L) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 733 | if (!L->isLoopSimplifyForm()) { |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 734 | INVALID(SimpleLoop, "Loop not in simplify form is invalid!"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 735 | return false; |
| 736 | } |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 737 | |
| 738 | for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE; |
| 739 | ++PI) { |
| 740 | // Region entering edges come from the same loop but outside the region |
| 741 | // are not allowed. |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 742 | if (L->contains(*PI) && !R.contains(*PI)) { |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 743 | INVALID(IndEdge, "Region has invalid entering edges!"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 744 | return false; |
| 745 | } |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 750 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 751 | // to insert alloca instruction there when translate scalar to array. |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 752 | if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) { |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 753 | INVALID(Other, "Region containing entry block of function is invalid!"); |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 754 | return false; |
| 755 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 756 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 757 | if (!isValidExit(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 758 | return false; |
| 759 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 760 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 761 | return false; |
| 762 | |
| 763 | DEBUG(dbgs() << "OK\n"); |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 768 | return !InvalidFunctions.count(&F); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 771 | void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin, |
| 772 | unsigned &LineEnd, std::string &FileName) { |
| 773 | LineBegin = -1; |
| 774 | LineEnd = 0; |
| 775 | |
| 776 | for (Region::const_block_iterator RI = R->block_begin(), RE = R->block_end(); |
| 777 | RI != RE; ++RI) |
| 778 | for (BasicBlock::iterator BI = (*RI)->begin(), BE = (*RI)->end(); BI != BE; |
| 779 | ++BI) { |
| 780 | DebugLoc DL = BI->getDebugLoc(); |
| 781 | if (DL.isUnknown()) |
| 782 | continue; |
| 783 | |
| 784 | DIScope Scope(DL.getScope(BI->getContext())); |
| 785 | |
| 786 | if (FileName.empty()) |
| 787 | FileName = Scope.getFilename(); |
| 788 | |
| 789 | unsigned NewLine = DL.getLine(); |
| 790 | |
| 791 | LineBegin = std::min(LineBegin, NewLine); |
| 792 | LineEnd = std::max(LineEnd, NewLine); |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 793 | } |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 796 | void ScopDetection::printLocations(llvm::Function &F) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 797 | for (iterator RI = begin(), RE = end(); RI != RE; ++RI) { |
| 798 | unsigned LineEntry, LineExit; |
| 799 | std::string FileName; |
| 800 | |
| 801 | getDebugLocation(*RI, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 802 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 803 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 807 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 808 | LI = &getAnalysis<LoopInfo>(); |
Tobias Grosser | 9a26f29 | 2014-01-02 22:28:53 +0000 | [diff] [blame] | 809 | RI = &getAnalysis<RegionInfo>(); |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 810 | if (!DetectScopsWithoutLoops && LI->empty()) |
| 811 | return false; |
| 812 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 813 | AA = &getAnalysis<AliasAnalysis>(); |
| 814 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 815 | Region *TopRegion = RI->getTopLevelRegion(); |
| 816 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 817 | releaseMemory(); |
| 818 | |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 819 | if (OnlyFunction != "" && F.getName() != OnlyFunction) |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 820 | return false; |
| 821 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 822 | if (!isValidFunction(F)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 823 | return false; |
| 824 | |
| 825 | findScops(*TopRegion); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 826 | |
| 827 | if (ReportLevel >= 1) |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 828 | printLocations(F); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 829 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 830 | return false; |
| 831 | } |
| 832 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 833 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 834 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 835 | DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 836 | isValidRegion(Context); |
| 837 | } |
| 838 | |
| 839 | void polly::ScopDetection::verifyAnalysis() const { |
| 840 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
Tobias Grosser | af3c00b8 | 2013-02-05 09:40:22 +0000 | [diff] [blame] | 841 | E = ValidRegions.end(); |
| 842 | I != E; ++I) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 843 | verifyRegion(**I); |
| 844 | } |
| 845 | |
| 846 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 847 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 848 | AU.addRequired<PostDominatorTree>(); |
| 849 | AU.addRequired<LoopInfo>(); |
| 850 | AU.addRequired<ScalarEvolution>(); |
| 851 | // We also need AA and RegionInfo when we are verifying analysis. |
| 852 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 853 | AU.addRequiredTransitive<RegionInfo>(); |
| 854 | AU.setPreservesAll(); |
| 855 | } |
| 856 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 857 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 858 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
Tobias Grosser | af3c00b8 | 2013-02-05 09:40:22 +0000 | [diff] [blame] | 859 | E = ValidRegions.end(); |
| 860 | I != E; ++I) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 861 | OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n'; |
| 862 | |
| 863 | OS << "\n"; |
| 864 | } |
| 865 | |
| 866 | void ScopDetection::releaseMemory() { |
| 867 | ValidRegions.clear(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 868 | InvalidRegions.clear(); |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 869 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | char ScopDetection::ID = 0; |
| 873 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 874 | Pass *polly::createScopDetectionPass() { return new ScopDetection(); } |
| 875 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 876 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 877 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 878 | false); |
| 879 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 880 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 881 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
| 882 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTree); |
| 883 | INITIALIZE_PASS_DEPENDENCY(RegionInfo); |
| 884 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 885 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 886 | "Polly - Detect static control parts (SCoPs)", false, false) |