blob: 69c63ae1b13b536e3b646fe048402c96d4fbeb65 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===----- 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 Grosserecfe21b2013-03-20 18:03:18 +000047#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser75805372011-04-29 06:27:02 +000048#include "polly/LinkAllPasses.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000049#include "polly/Options.h"
Andreas Simbuerger01a37a02014-04-02 11:54:01 +000050#include "polly/ScopDetectionDiagnostic.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000051#include "polly/ScopDetection.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000052#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000053#include "polly/Support/ScopHelper.h"
Tobias Grosser75805372011-04-29 06:27:02 +000054#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000056#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000057#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000058#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosserb8710b52011-11-10 12:44:50 +000059#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000060#include "llvm/IR/DebugInfo.h"
Tobias Grosser8519f892013-12-18 10:49:53 +000061#include "llvm/IR/DiagnosticInfo.h"
62#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000063#include "llvm/IR/LLVMContext.h"
Tobias Grosser75805372011-04-29 06:27:02 +000064
65#define DEBUG_TYPE "polly-detect"
66#include "llvm/Support/Debug.h"
67
Tobias Grosser60b54f12011-11-08 15:41:28 +000068#include <set>
69
Tobias Grosser75805372011-04-29 06:27:02 +000070using namespace llvm;
71using namespace polly;
72
Sebastian Pop8fe6d112013-05-30 17:47:32 +000073static cl::opt<bool>
74DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops",
75 cl::desc("Detect scops in functions without loops"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000076 cl::Hidden, cl::init(false), cl::ZeroOrMore,
77 cl::cat(PollyCategory));
Sebastian Pop8fe6d112013-05-30 17:47:32 +000078
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000079static cl::opt<bool>
80DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
81 cl::desc("Detect scops in regions without loops"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000082 cl::Hidden, cl::init(false), cl::ZeroOrMore,
83 cl::cat(PollyCategory));
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000084
Tobias Grosser2ff87232011-10-23 11:17:06 +000085static cl::opt<std::string>
Tobias Grosser637bd632013-05-07 07:31:10 +000086OnlyFunction("polly-only-func", cl::desc("Only run on a single function"),
87 cl::value_desc("function-name"), cl::ValueRequired, cl::init(""),
88 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000089
Tobias Grosser4449e522014-01-27 14:24:53 +000090static cl::opt<std::string>
91OnlyRegion("polly-only-region",
92 cl::desc("Only run on certain regions (The provided identifier must "
93 "appear in the name of the region's entry block"),
94 cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
95 cl::cat(PollyCategory));
96
Tobias Grosser60cd9322011-11-10 12:47:26 +000097static cl::opt<bool>
98IgnoreAliasing("polly-ignore-aliasing",
99 cl::desc("Ignore possible aliasing of the array bases"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000100 cl::Hidden, cl::init(false), cl::ZeroOrMore,
101 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +0000102
Tobias Grosser637bd632013-05-07 07:31:10 +0000103static cl::opt<bool>
104ReportLevel("polly-report",
105 cl::desc("Print information about the activities of Polly"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000106 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser531891e2012-11-01 16:45:20 +0000107
108static cl::opt<bool>
Tobias Grossera1879642011-12-20 10:43:14 +0000109AllowNonAffine("polly-allow-nonaffine",
110 cl::desc("Allow non affine access functions in arrays"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000111 cl::Hidden, cl::init(false), cl::ZeroOrMore,
112 cl::cat(PollyCategory));
Tobias Grossera1879642011-12-20 10:43:14 +0000113
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000114static cl::opt<bool, true>
115TrackFailures("polly-detect-track-failures",
116 cl::desc("Track failure strings in detecting scop regions"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000117 cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore,
118 cl::init(false), cl::cat(PollyCategory));
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000119
Tobias Grossera1689932014-02-18 18:49:49 +0000120static cl::opt<bool>
121VerifyScops("polly-detect-verify",
122 cl::desc("Verify the detected SCoPs after each transformation"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000123 cl::Hidden, cl::init(false), cl::ZeroOrMore,
124 cl::cat(PollyCategory));
Tobias Grossera1689932014-02-18 18:49:49 +0000125
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000126bool polly::PollyTrackFailures = false;
127
Tobias Grosser75805372011-04-29 06:27:02 +0000128//===----------------------------------------------------------------------===//
129// Statistics.
130
131STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
132
Tobias Grosser8519f892013-12-18 10:49:53 +0000133class DiagnosticScopFound : public DiagnosticInfo {
134private:
135 static int PluginDiagnosticKind;
136
137 Function &F;
138 std::string FileName;
139 unsigned EntryLine, ExitLine;
140
141public:
Tobias Grosser1b12f462013-12-18 11:14:36 +0000142 DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
143 unsigned ExitLine)
Tobias Grosser8519f892013-12-18 10:49:53 +0000144 : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
Tobias Grosser1b12f462013-12-18 11:14:36 +0000145 EntryLine(EntryLine), ExitLine(ExitLine) {}
Tobias Grosser8519f892013-12-18 10:49:53 +0000146
147 virtual void print(DiagnosticPrinter &DP) const;
148
149 static bool classof(const DiagnosticInfo *DI) {
150 return DI->getKind() == PluginDiagnosticKind;
151 }
152};
153
154int DiagnosticScopFound::PluginDiagnosticKind = 10;
155
Tobias Grosser8519f892013-12-18 10:49:53 +0000156void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
Tobias Grosser1b12f462013-12-18 11:14:36 +0000157 DP << "Polly detected an optimizable loop region (scop) in function '" << F
158 << "'\n";
Tobias Grosser8519f892013-12-18 10:49:53 +0000159
160 if (FileName.empty()) {
161 DP << "Scop location is unknown. Compile with debug info "
162 "(-g) to get more precise information. ";
Tobias Grosser1b12f462013-12-18 11:14:36 +0000163 return;
Tobias Grosser8519f892013-12-18 10:49:53 +0000164 }
165
166 DP << FileName << ":" << EntryLine << ": Start of scop\n";
167 DP << FileName << ":" << ExitLine << ": End of scop";
168}
169
Tobias Grosser75805372011-04-29 06:27:02 +0000170//===----------------------------------------------------------------------===//
171// ScopDetection.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000172
173template <class RR, typename... Args>
174inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert,
175 Args &&... Arguments) const {
176
177 if (!Context.Verifying) {
178 RR RejectReason = RR(Arguments...);
179 if (PollyTrackFailures)
180 LastFailure = RejectReason.getMessage();
181
182 DEBUG(dbgs() << RejectReason.getMessage());
183 DEBUG(dbgs() << "\n");
184 } else {
185 assert(!Assert && "Verification of detected scop failed");
186 }
187
188 return false;
189}
190
Tobias Grossera1689932014-02-18 18:49:49 +0000191bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
192 if (!ValidRegions.count(&R))
193 return false;
194
195 if (Verify)
196 return isValidRegion(const_cast<Region &>(R));
197
198 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000199}
200
Tobias Grosser4f129a62011-10-08 00:30:55 +0000201std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
202 if (!InvalidRegions.count(R))
203 return "";
204
205 return InvalidRegions.find(R)->second;
206}
207
Tobias Grossere602a072013-05-07 07:30:56 +0000208bool ScopDetection::isValidCFG(BasicBlock &BB,
209 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000210 Region &RefRegion = Context.CurRegion;
211 TerminatorInst *TI = BB.getTerminator();
212
213 // Return instructions are only valid if the region is the top level region.
214 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
215 return true;
216
217 BranchInst *Br = dyn_cast<BranchInst>(TI);
218
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000219 if (!Br)
220 return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000221
Tobias Grosser74394f02013-01-14 22:40:23 +0000222 if (Br->isUnconditional())
223 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000224
225 Value *Condition = Br->getCondition();
226
227 // UndefValue is not allowed as condition.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000228 if (isa<UndefValue>(Condition))
229 return invalid<ReportUndefCond>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000230
231 // Only Constant and ICmpInst are allowed as condition.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000232 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
233 return invalid<ReportInvalidCond>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000234
235 // Allow perfectly nested conditions.
236 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
237
238 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
239 // Unsigned comparisons are not allowed. They trigger overflow problems
240 // in the code generation.
241 //
242 // TODO: This is not sufficient and just hides bugs. However it does pretty
243 // well.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000244 if (ICmp->isUnsigned())
Tobias Grosser75805372011-04-29 06:27:02 +0000245 return false;
246
247 // Are both operands of the ICmp affine?
Tobias Grosser74394f02013-01-14 22:40:23 +0000248 if (isa<UndefValue>(ICmp->getOperand(0)) ||
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000249 isa<UndefValue>(ICmp->getOperand(1)))
250 return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000251
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000252 Loop *L = LI->getLoopFor(ICmp->getParent());
253 const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
254 const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
Tobias Grosser75805372011-04-29 06:27:02 +0000255
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000256 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000257 !isAffineExpr(&Context.CurRegion, RHS, *SE))
258 return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS,
259 RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000260 }
261
262 // Allow loop exit conditions.
263 Loop *L = LI->getLoopFor(&BB);
264 if (L && L->getExitingBlock() == &BB)
265 return true;
266
267 // Allow perfectly nested conditions.
268 Region *R = RI->getRegionFor(&BB);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000269 if (R->getEntry() != &BB)
270 return invalid<ReportCondition>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000271
272 return true;
273}
274
275bool ScopDetection::isValidCallInst(CallInst &CI) {
276 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
277 return false;
278
279 if (CI.doesNotAccessMemory())
280 return true;
281
282 Function *CalledFunction = CI.getCalledFunction();
283
284 // Indirect calls are not supported.
285 if (CalledFunction == 0)
286 return false;
287
288 // TODO: Intrinsics.
289 return false;
290}
291
Tobias Grosser458fb782014-01-28 12:58:58 +0000292bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
293 // A reference to function argument or constant value is invariant.
294 if (isa<Argument>(Val) || isa<Constant>(Val))
295 return true;
296
297 const Instruction *I = dyn_cast<Instruction>(&Val);
298 if (!I)
299 return false;
300
301 if (!Reg.contains(I))
302 return true;
303
304 if (I->mayHaveSideEffects())
305 return false;
306
307 // When Val is a Phi node, it is likely not invariant. We do not check whether
308 // Phi nodes are actually invariant, we assume that Phi nodes are usually not
309 // invariant. Recursively checking the operators of Phi nodes would lead to
310 // infinite recursion.
311 if (isa<PHINode>(*I))
312 return false;
313
Tobias Grosser26108892014-04-02 20:18:19 +0000314 for (const Use &Operand : I->operands())
Tobias Grosser1d191902014-03-03 13:13:55 +0000315 if (!isInvariant(*Operand, Reg))
Tobias Grosser458fb782014-01-28 12:58:58 +0000316 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000317
318 // When the instruction is a load instruction, check that no write to memory
319 // in the region aliases with the load.
320 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
321 AliasAnalysis::Location Loc = AA->getLocation(LI);
322 const Region::const_block_iterator BE = Reg.block_end();
323 // Check if any basic block in the region can modify the location pointed to
324 // by 'Loc'. If so, 'Val' is (likely) not invariant in the region.
Tobias Grosser26108892014-04-02 20:18:19 +0000325 for (const BasicBlock *BB : Reg.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000326 if (AA->canBasicBlockModify(*BB, Loc))
Tobias Grosser458fb782014-01-28 12:58:58 +0000327 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000328 }
329
330 return true;
331}
332
Tobias Grosser75805372011-04-29 06:27:02 +0000333bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
334 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000335 Value *Ptr = getPointerOperand(Inst);
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000336 Loop *L = LI->getLoopFor(Inst.getParent());
337 const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000338 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000339 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000340
Tobias Grosserb8710b52011-11-10 12:44:50 +0000341 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
342
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000343 if (!BasePointer)
344 return invalid<ReportNoBasePtr>(Context, /*Assert=*/true);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000345
346 BaseValue = BasePointer->getValue();
347
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000348 if (isa<UndefValue>(BaseValue))
349 return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000350
Tobias Grosser458fb782014-01-28 12:58:58 +0000351 // Check that the base address of the access is invariant in the current
352 // region.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000353 if (!isInvariant(*BaseValue, Context.CurRegion))
Tobias Grosserab2227a2014-01-28 13:43:24 +0000354 // Verification of this property is difficult as the independent blocks
355 // pass may introduce aliasing that we did not have when running the
356 // scop detection.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000357 return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue);
Tobias Grosser458fb782014-01-28 12:58:58 +0000358
Tobias Grosserb8710b52011-11-10 12:44:50 +0000359 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
360
Tobias Grosser58032cb2013-06-23 01:29:29 +0000361 if (!AllowNonAffine &&
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000362 !isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue))
363 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true,
364 AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000365
366 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
367 // created by IndependentBlocks Pass.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000368 if (isa<IntToPtrInst>(BaseValue))
369 return invalid<ReportIntToPtr>(Context, /*Assert=*/true, BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000370
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000371 if (IgnoreAliasing)
372 return true;
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000373
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000374 // Check if the base pointer of the memory access does alias with
375 // any other pointer. This cannot be handled at the moment.
Tobias Grosser298a7642013-07-14 18:09:43 +0000376 AliasSet &AS =
377 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
378 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosser428b3e42013-02-04 15:46:25 +0000379
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000380 // INVALID triggers an assertion in verifying mode, if it detects that a
381 // SCoP was detected by SCoP detection and that this SCoP was invalidated by
382 // a pass that stated it would preserve the SCoPs. We disable this check as
383 // the independent blocks pass may create memory references which seem to
384 // alias, if -basicaa is not available. They actually do not, but as we can
385 // not proof this without -basicaa we would fail. We disable this check to
386 // not cause irrelevant verification failures.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000387 if (!AS.isMustAlias())
388 return invalid<ReportAlias>(Context, /*Assert=*/true, &AS);
Tobias Grosser75805372011-04-29 06:27:02 +0000389
390 return true;
391}
392
Tobias Grosser75805372011-04-29 06:27:02 +0000393bool ScopDetection::isValidInstruction(Instruction &Inst,
394 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000395 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000396 if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) {
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000397 if (SCEVCodegen)
398 return invalid<ReportPhiNodeRefInRegion>(Context, /*Assert=*/true,
399 &Inst);
400 else
401 return invalid<ReportNonCanonicalPhiNode>(Context, /*Assert=*/true,
402 &Inst);
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000403 }
Tobias Grosser75805372011-04-29 06:27:02 +0000404
Tobias Grosser75805372011-04-29 06:27:02 +0000405 // We only check the call instruction but not invoke instruction.
406 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
407 if (isValidCallInst(*CI))
408 return true;
409
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000410 return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000411 }
412
413 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000414 if (!isa<AllocaInst>(Inst))
415 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000416
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000417 return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000418 }
419
420 // Check the access function.
421 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
422 return isValidMemoryAccess(Inst, Context);
423
424 // We do not know this instruction, therefore we assume it is invalid.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000425 return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000426}
427
Tobias Grosser75805372011-04-29 06:27:02 +0000428bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000429 if (!SCEVCodegen) {
430 // If code generation is not in scev based mode, we need to ensure that
431 // each loop has a canonical induction variable.
432 PHINode *IndVar = L->getCanonicalInductionVariable();
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000433 if (!IndVar)
434 return invalid<ReportLoopHeader>(Context, /*Assert=*/true, L);
Tobias Grosser826b2af2013-03-21 16:14:50 +0000435 }
Tobias Grosser75805372011-04-29 06:27:02 +0000436
437 // Is the loop count affine?
438 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000439 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
440 return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount);
Tobias Grosser75805372011-04-29 06:27:02 +0000441
442 return true;
443}
444
445Region *ScopDetection::expandRegion(Region &R) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000446 // Initial no valid region was found (greater than R)
447 Region *LastValidRegion = NULL;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000448 Region *ExpandedRegion = R.getExpandedRegion();
Tobias Grosser75805372011-04-29 06:27:02 +0000449
450 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
451
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000452 while (ExpandedRegion) {
453 DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
454 DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000455
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000456 // Check the exit first (cheap)
Tobias Grosser75805372011-04-29 06:27:02 +0000457 if (isValidExit(Context)) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000458 // If the exit is valid check all blocks
459 // - if true, a valid region was found => store it + keep expanding
460 // - if false, .tbd. => stop (should this really end the loop?)
461 if (!allBlocksValid(Context))
462 break;
Tobias Grosser75805372011-04-29 06:27:02 +0000463
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000464 // Delete unnecessary regions (allocated by getExpandedRegion)
465 if (LastValidRegion)
466 delete LastValidRegion;
467
Tobias Grosserd7e58642013-04-10 06:55:45 +0000468 // Store this region, because it is the greatest valid (encountered so
469 // far).
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000470 LastValidRegion = ExpandedRegion;
471
472 // Create and test the next greater region (if any)
473 ExpandedRegion = ExpandedRegion->getExpandedRegion();
474
475 } else {
476 // Create and test the next greater region (if any)
477 Region *TmpRegion = ExpandedRegion->getExpandedRegion();
478
479 // Delete unnecessary regions (allocated by getExpandedRegion)
480 delete ExpandedRegion;
481
482 ExpandedRegion = TmpRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000483 }
Tobias Grosser75805372011-04-29 06:27:02 +0000484 }
485
Tobias Grosser378a9f22013-11-16 19:34:11 +0000486 DEBUG({
487 if (LastValidRegion)
488 dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
489 else
490 dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
491 });
Tobias Grosser75805372011-04-29 06:27:02 +0000492
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000493 return LastValidRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000494}
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000495static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
Tobias Grosser26108892014-04-02 20:18:19 +0000496 for (const BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000497 if (R.contains(LI->getLoopFor(BB)))
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000498 return false;
499
500 return true;
501}
Tobias Grosser75805372011-04-29 06:27:02 +0000502
Tobias Grosser28a70c52014-01-29 19:05:30 +0000503// Remove all direct and indirect children of region R from the region set Regs,
504// but do not recurse further if the first child has been found.
505//
506// Return the number of regions erased from Regs.
507static unsigned eraseAllChildren(std::set<const Region *> &Regs,
508 const Region *R) {
509 unsigned Count = 0;
Tobias Grosser26108892014-04-02 20:18:19 +0000510 for (const Region *SubRegion : *R) {
Tobias Grosser00dc3092014-03-02 12:02:46 +0000511 if (Regs.find(SubRegion) != Regs.end()) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000512 ++Count;
Tobias Grosser00dc3092014-03-02 12:02:46 +0000513 Regs.erase(SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000514 } else {
Tobias Grosser00dc3092014-03-02 12:02:46 +0000515 Count += eraseAllChildren(Regs, SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000516 }
517 }
518 return Count;
519}
520
Tobias Grosser75805372011-04-29 06:27:02 +0000521void ScopDetection::findScops(Region &R) {
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000522 if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
523 return;
524
Tobias Grosser4eb73812011-11-10 12:45:15 +0000525 LastFailure = "";
526
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000527 if (isValidRegion(R)) {
Tobias Grosser75805372011-04-29 06:27:02 +0000528 ++ValidRegion;
529 ValidRegions.insert(&R);
530 return;
531 }
532
Tobias Grosser4f129a62011-10-08 00:30:55 +0000533 InvalidRegions[&R] = LastFailure;
534
Tobias Grosser26108892014-04-02 20:18:19 +0000535 for (Region *SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000536 findScops(*SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000537
538 // Try to expand regions.
539 //
540 // As the region tree normally only contains canonical regions, non canonical
541 // regions that form a Scop are not found. Therefore, those non canonical
542 // regions are checked by expanding the canonical ones.
543
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000544 std::vector<Region *> ToExpand;
Tobias Grosser75805372011-04-29 06:27:02 +0000545
Tobias Grosser26108892014-04-02 20:18:19 +0000546 for (Region *SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000547 ToExpand.push_back(SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000548
Tobias Grosser26108892014-04-02 20:18:19 +0000549 for (Region *CurrentRegion : ToExpand) {
Tobias Grosser75805372011-04-29 06:27:02 +0000550 // Skip invalid regions. Regions may become invalid, if they are element of
551 // an already expanded region.
552 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
553 continue;
554
555 Region *ExpandedR = expandRegion(*CurrentRegion);
556
557 if (!ExpandedR)
558 continue;
559
560 R.addSubRegion(ExpandedR, true);
561 ValidRegions.insert(ExpandedR);
562 ValidRegions.erase(CurrentRegion);
563
Tobias Grosser28a70c52014-01-29 19:05:30 +0000564 // Erase all (direct and indirect) children of ExpandedR from the valid
565 // regions and update the number of valid regions.
566 ValidRegion -= eraseAllChildren(ValidRegions, ExpandedR);
Tobias Grosser75805372011-04-29 06:27:02 +0000567 }
568}
569
570bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
571 Region &R = Context.CurRegion;
572
Tobias Grosser26108892014-04-02 20:18:19 +0000573 for (const BasicBlock *BB : R.blocks()) {
Tobias Grosser1d191902014-03-03 13:13:55 +0000574 Loop *L = LI->getLoopFor(BB);
575 if (L && L->getHeader() == BB && !isValidLoop(L, Context))
Sebastian Popb88ea5e2013-06-11 22:20:32 +0000576 return false;
577 }
578
Tobias Grosser26108892014-04-02 20:18:19 +0000579 for (BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000580 if (!isValidCFG(*BB, Context))
Sebastian Pop9e3d2dd2013-06-11 22:20:27 +0000581 return false;
582
Tobias Grosser26108892014-04-02 20:18:19 +0000583 for (BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000584 for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I)
Sebastian Pop8ca899c2013-06-14 20:20:43 +0000585 if (!isValidInstruction(*I, Context))
586 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000587
588 return true;
589}
590
591bool ScopDetection::isValidExit(DetectionContext &Context) const {
592 Region &R = Context.CurRegion;
593
594 // PHI nodes are not allowed in the exit basic block.
595 if (BasicBlock *Exit = R.getExit()) {
596 BasicBlock::iterator I = Exit->begin();
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000597 if (I != Exit->end() && isa<PHINode>(*I))
598 return invalid<ReportPHIinExit>(Context, /*Assert=*/true);
Tobias Grosser75805372011-04-29 06:27:02 +0000599 }
600
601 return true;
602}
603
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000604bool ScopDetection::isValidRegion(Region &R) const {
605 DetectionContext Context(R, *AA, false /*verifying*/);
606 return isValidRegion(Context);
607}
608
Tobias Grosser75805372011-04-29 06:27:02 +0000609bool ScopDetection::isValidRegion(DetectionContext &Context) const {
610 Region &R = Context.CurRegion;
611
612 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
613
Tobias Grosseraeabcf22013-04-02 06:41:48 +0000614 if (R.isTopLevelRegion()) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000615 DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000616 return false;
617 }
618
Tobias Grosser4449e522014-01-27 14:24:53 +0000619 if (!R.getEntry()->getName().count(OnlyRegion)) {
620 DEBUG({
621 dbgs() << "Region entry does not match -polly-region-only";
622 dbgs() << "\n";
623 });
624 return false;
625 }
626
Tobias Grossere602a072013-05-07 07:30:56 +0000627 if (!R.getEnteringBlock()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000628 BasicBlock *entry = R.getEntry();
629 Loop *L = LI->getLoopFor(entry);
630
631 if (L) {
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000632 if (!L->isLoopSimplifyForm())
633 return invalid<ReportSimpleLoop>(Context, /*Assert=*/true);
Sebastian Pop9d632342013-06-11 22:20:40 +0000634
635 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
636 ++PI) {
637 // Region entering edges come from the same loop but outside the region
638 // are not allowed.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000639 if (L->contains(*PI) && !R.contains(*PI))
640 return invalid<ReportIndEdge>(Context, /*Assert=*/true);
Sebastian Pop9d632342013-06-11 22:20:40 +0000641 }
642 }
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000643 }
644
Tobias Grosserd654c252012-04-10 18:12:19 +0000645 // SCoP cannot contain the entry block of the function, because we need
Tobias Grosser75805372011-04-29 06:27:02 +0000646 // to insert alloca instruction there when translate scalar to array.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000647 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
648 return invalid<ReportEntry>(Context, /*Assert=*/true);
Tobias Grosser75805372011-04-29 06:27:02 +0000649
Hongbin Zheng94868e62012-04-07 12:29:17 +0000650 if (!isValidExit(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000651 return false;
652
Hongbin Zheng94868e62012-04-07 12:29:17 +0000653 if (!allBlocksValid(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000654 return false;
655
656 DEBUG(dbgs() << "OK\n");
657 return true;
658}
659
660bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000661 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000662}
663
Tobias Grosser531891e2012-11-01 16:45:20 +0000664void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
665 unsigned &LineEnd, std::string &FileName) {
666 LineBegin = -1;
667 LineEnd = 0;
668
Tobias Grosser26108892014-04-02 20:18:19 +0000669 for (const BasicBlock *BB : R->blocks())
670 for (const Instruction &Inst : *BB) {
671 DebugLoc DL = Inst.getDebugLoc();
Tobias Grosser531891e2012-11-01 16:45:20 +0000672 if (DL.isUnknown())
673 continue;
674
Tobias Grosser26108892014-04-02 20:18:19 +0000675 DIScope Scope(DL.getScope(Inst.getContext()));
Tobias Grosser531891e2012-11-01 16:45:20 +0000676
677 if (FileName.empty())
678 FileName = Scope.getFilename();
679
680 unsigned NewLine = DL.getLine();
681
682 LineBegin = std::min(LineBegin, NewLine);
683 LineEnd = std::max(LineEnd, NewLine);
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000684 }
Tobias Grosser531891e2012-11-01 16:45:20 +0000685}
686
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000687void ScopDetection::printLocations(llvm::Function &F) {
Tobias Grosser26108892014-04-02 20:18:19 +0000688 for (const Region *R : *this) {
Tobias Grosser531891e2012-11-01 16:45:20 +0000689 unsigned LineEntry, LineExit;
690 std::string FileName;
691
Tobias Grosser00dc3092014-03-02 12:02:46 +0000692 getDebugLocation(R, LineEntry, LineExit, FileName);
Tobias Grosser8519f892013-12-18 10:49:53 +0000693 DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
694 F.getContext().diagnose(Diagnostic);
Tobias Grosser531891e2012-11-01 16:45:20 +0000695 }
696}
697
Tobias Grosser75805372011-04-29 06:27:02 +0000698bool ScopDetection::runOnFunction(llvm::Function &F) {
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000699 LI = &getAnalysis<LoopInfo>();
Tobias Grosser9a26f292014-01-02 22:28:53 +0000700 RI = &getAnalysis<RegionInfo>();
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000701 if (!DetectScopsWithoutLoops && LI->empty())
702 return false;
703
Tobias Grosser75805372011-04-29 06:27:02 +0000704 AA = &getAnalysis<AliasAnalysis>();
705 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000706 Region *TopRegion = RI->getTopLevelRegion();
707
Tobias Grosser2ff87232011-10-23 11:17:06 +0000708 releaseMemory();
709
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000710 if (OnlyFunction != "" && F.getName() != OnlyFunction)
Tobias Grosser2ff87232011-10-23 11:17:06 +0000711 return false;
712
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000713 if (!isValidFunction(F))
Tobias Grosser75805372011-04-29 06:27:02 +0000714 return false;
715
716 findScops(*TopRegion);
Tobias Grosser531891e2012-11-01 16:45:20 +0000717
718 if (ReportLevel >= 1)
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000719 printLocations(F);
Tobias Grosser531891e2012-11-01 16:45:20 +0000720
Tobias Grosser75805372011-04-29 06:27:02 +0000721 return false;
722}
723
Tobias Grosser75805372011-04-29 06:27:02 +0000724void polly::ScopDetection::verifyRegion(const Region &R) const {
725 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000726 DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
Tobias Grosser75805372011-04-29 06:27:02 +0000727 isValidRegion(Context);
728}
729
730void polly::ScopDetection::verifyAnalysis() const {
Tobias Grossera1689932014-02-18 18:49:49 +0000731 if (!VerifyScops)
732 return;
733
Tobias Grosser26108892014-04-02 20:18:19 +0000734 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000735 verifyRegion(*R);
Tobias Grosser75805372011-04-29 06:27:02 +0000736}
737
738void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser42aff302014-01-13 22:29:56 +0000739 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +0000740 AU.addRequired<PostDominatorTree>();
741 AU.addRequired<LoopInfo>();
742 AU.addRequired<ScalarEvolution>();
743 // We also need AA and RegionInfo when we are verifying analysis.
744 AU.addRequiredTransitive<AliasAnalysis>();
745 AU.addRequiredTransitive<RegionInfo>();
746 AU.setPreservesAll();
747}
748
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000749void ScopDetection::print(raw_ostream &OS, const Module *) const {
Tobias Grosser26108892014-04-02 20:18:19 +0000750 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000751 OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
Tobias Grosser75805372011-04-29 06:27:02 +0000752
753 OS << "\n";
754}
755
756void ScopDetection::releaseMemory() {
757 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000758 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000759 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000760}
761
762char ScopDetection::ID = 0;
763
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000764Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
765
Tobias Grosser73600b82011-10-08 00:30:40 +0000766INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
767 "Polly - Detect static control parts (SCoPs)", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000768 false);
769INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Tobias Grosser42aff302014-01-13 22:29:56 +0000770INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000771INITIALIZE_PASS_DEPENDENCY(LoopInfo);
772INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
773INITIALIZE_PASS_DEPENDENCY(RegionInfo);
774INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Tobias Grosser73600b82011-10-08 00:30:40 +0000775INITIALIZE_PASS_END(ScopDetection, "polly-detect",
776 "Polly - Detect static control parts (SCoPs)", false, false)