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