blob: ba8ff460ec1d6c7ffa6ae0172bf660494c78d0ef [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"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000050#include "polly/ScopDetection.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000051#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000052#include "polly/Support/ScopHelper.h"
Tobias Grosser75805372011-04-29 06:27:02 +000053#include "llvm/ADT/Statistic.h"
54#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000055#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000056#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000057#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosserb8710b52011-11-10 12:44:50 +000058#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000059#include "llvm/Assembly/Writer.h"
Tobias Grosser83628182013-05-07 08:11:54 +000060#include "llvm/DebugInfo.h"
61#include "llvm/IR/LLVMContext.h"
Tobias Grosser75805372011-04-29 06:27:02 +000062
63#define DEBUG_TYPE "polly-detect"
64#include "llvm/Support/Debug.h"
65
Tobias Grosser60b54f12011-11-08 15:41:28 +000066#include <set>
67
Tobias Grosser75805372011-04-29 06:27:02 +000068using namespace llvm;
69using namespace polly;
70
Sebastian Pop8fe6d112013-05-30 17:47:32 +000071static cl::opt<bool>
72DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops",
73 cl::desc("Detect scops in functions without loops"),
74 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
75
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000076static cl::opt<bool>
77DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
78 cl::desc("Detect scops in regions without loops"),
79 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
80
Tobias Grosser2ff87232011-10-23 11:17:06 +000081static cl::opt<std::string>
Tobias Grosser637bd632013-05-07 07:31:10 +000082OnlyFunction("polly-only-func", cl::desc("Only run on a single function"),
83 cl::value_desc("function-name"), cl::ValueRequired, cl::init(""),
84 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000085
Tobias Grosser60cd9322011-11-10 12:47:26 +000086static cl::opt<bool>
87IgnoreAliasing("polly-ignore-aliasing",
88 cl::desc("Ignore possible aliasing of the array bases"),
Tobias Grosser637bd632013-05-07 07:31:10 +000089 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000090
Tobias Grosser637bd632013-05-07 07:31:10 +000091static cl::opt<bool>
92ReportLevel("polly-report",
93 cl::desc("Print information about the activities of Polly"),
94 cl::init(false), cl::cat(PollyCategory));
Tobias Grosser531891e2012-11-01 16:45:20 +000095
96static cl::opt<bool>
Tobias Grossera1879642011-12-20 10:43:14 +000097AllowNonAffine("polly-allow-nonaffine",
98 cl::desc("Allow non affine access functions in arrays"),
Tobias Grosser637bd632013-05-07 07:31:10 +000099 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
Tobias Grossera1879642011-12-20 10:43:14 +0000100
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000101static cl::opt<bool, true>
102TrackFailures("polly-detect-track-failures",
103 cl::desc("Track failure strings in detecting scop regions"),
104 cl::location(PollyTrackFailures), cl::Hidden, cl::init(false),
105 cl::cat(PollyCategory));
106
107bool polly::PollyTrackFailures = false;
108
Tobias Grosser75805372011-04-29 06:27:02 +0000109//===----------------------------------------------------------------------===//
110// Statistics.
111
112STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
113
Tobias Grosser74394f02013-01-14 22:40:23 +0000114#define BADSCOP_STAT(NAME, DESC) \
115 STATISTIC(Bad##NAME##ForScop, "Number of bad regions for Scop: " DESC)
Tobias Grosser75805372011-04-29 06:27:02 +0000116
Tobias Grosser74394f02013-01-14 22:40:23 +0000117#define INVALID(NAME, MESSAGE) \
118 do { \
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000119 if (PollyTrackFailures) { \
120 std::string Buf; \
121 raw_string_ostream fmt(Buf); \
122 fmt << MESSAGE; \
123 fmt.flush(); \
124 LastFailure = Buf; \
125 } \
Tobias Grosser74394f02013-01-14 22:40:23 +0000126 DEBUG(dbgs() << MESSAGE); \
127 DEBUG(dbgs() << "\n"); \
Tobias Grosser58032cb2013-06-23 01:29:29 +0000128 assert(!Context.Verifying &&#NAME); \
Tobias Grosser74394f02013-01-14 22:40:23 +0000129 if (!Context.Verifying) \
130 ++Bad##NAME##ForScop; \
Tobias Grosser84b81de2013-03-19 21:44:07 +0000131 } while (0)
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000132
Tobias Grosser74394f02013-01-14 22:40:23 +0000133#define INVALID_NOVERIFY(NAME, MESSAGE) \
134 do { \
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000135 if (PollyTrackFailures) { \
136 std::string Buf; \
137 raw_string_ostream fmt(Buf); \
138 fmt << MESSAGE; \
139 fmt.flush(); \
140 LastFailure = Buf; \
141 } \
Tobias Grosser74394f02013-01-14 22:40:23 +0000142 DEBUG(dbgs() << MESSAGE); \
143 DEBUG(dbgs() << "\n"); \
144 /* DISABLED: assert(!Context.Verifying && #NAME); */ \
145 if (!Context.Verifying) \
146 ++Bad##NAME##ForScop; \
Tobias Grosser84b81de2013-03-19 21:44:07 +0000147 } while (0)
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000148
Tobias Grosser74394f02013-01-14 22:40:23 +0000149BADSCOP_STAT(CFG, "CFG too complex");
150BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
Sebastian Pop9d632342013-06-11 22:20:40 +0000151BADSCOP_STAT(IndEdge, "Found invalid region entering edges");
Tobias Grosser74394f02013-01-14 22:40:23 +0000152BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
153BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
154BADSCOP_STAT(AffFunc, "Expression not affine");
Tobias Grosser74394f02013-01-14 22:40:23 +0000155BADSCOP_STAT(Alias, "Found base address alias");
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000156BADSCOP_STAT(SimpleLoop, "Loop not in -loop-simplify form");
Tobias Grosser74394f02013-01-14 22:40:23 +0000157BADSCOP_STAT(Other, "Others");
Tobias Grosser75805372011-04-29 06:27:02 +0000158
159//===----------------------------------------------------------------------===//
160// ScopDetection.
Tobias Grosser75805372011-04-29 06:27:02 +0000161bool ScopDetection::isMaxRegionInScop(const Region &R) const {
162 // The Region is valid only if it could be found in the set.
163 return ValidRegions.count(&R);
164}
165
Tobias Grosser4f129a62011-10-08 00:30:55 +0000166std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
167 if (!InvalidRegions.count(R))
168 return "";
169
170 return InvalidRegions.find(R)->second;
171}
172
Tobias Grossere602a072013-05-07 07:30:56 +0000173bool ScopDetection::isValidCFG(BasicBlock &BB,
174 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000175 Region &RefRegion = Context.CurRegion;
176 TerminatorInst *TI = BB.getTerminator();
177
178 // Return instructions are only valid if the region is the top level region.
179 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
180 return true;
181
182 BranchInst *Br = dyn_cast<BranchInst>(TI);
183
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000184 if (!Br) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000185 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000186 return false;
187 }
Tobias Grosser75805372011-04-29 06:27:02 +0000188
Tobias Grosser74394f02013-01-14 22:40:23 +0000189 if (Br->isUnconditional())
190 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000191
192 Value *Condition = Br->getCondition();
193
194 // UndefValue is not allowed as condition.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000195 if (isa<UndefValue>(Condition)) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000196 INVALID(AffFunc, "Condition based on 'undef' value in BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000197 return false;
198 }
Tobias Grosser75805372011-04-29 06:27:02 +0000199
200 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000201 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000202 INVALID(AffFunc, "Condition in BB '" + BB.getName() +
Tobias Grosserd7e58642013-04-10 06:55:45 +0000203 "' neither constant nor an icmp instruction");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000204 return false;
205 }
Tobias Grosser75805372011-04-29 06:27:02 +0000206
207 // Allow perfectly nested conditions.
208 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
209
210 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
211 // Unsigned comparisons are not allowed. They trigger overflow problems
212 // in the code generation.
213 //
214 // TODO: This is not sufficient and just hides bugs. However it does pretty
215 // well.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000216 if (ICmp->isUnsigned())
Tobias Grosser75805372011-04-29 06:27:02 +0000217 return false;
218
219 // Are both operands of the ICmp affine?
Tobias Grosser74394f02013-01-14 22:40:23 +0000220 if (isa<UndefValue>(ICmp->getOperand(0)) ||
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000221 isa<UndefValue>(ICmp->getOperand(1))) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000222 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000223 return false;
224 }
Tobias Grosser75805372011-04-29 06:27:02 +0000225
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000226 Loop *L = LI->getLoopFor(ICmp->getParent());
227 const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
228 const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
Tobias Grosser75805372011-04-29 06:27:02 +0000229
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000230 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000231 !isAffineExpr(&Context.CurRegion, RHS, *SE)) {
Tobias Grosser983e7852013-07-28 09:05:20 +0000232 INVALID(AffFunc, "Non affine branch in BB '" << BB.getName()
233 << "' with LHS: " << *LHS
234 << " and RHS: " << *RHS);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000235 return false;
236 }
Tobias Grosser75805372011-04-29 06:27:02 +0000237 }
238
239 // Allow loop exit conditions.
240 Loop *L = LI->getLoopFor(&BB);
241 if (L && L->getExitingBlock() == &BB)
242 return true;
243
244 // Allow perfectly nested conditions.
245 Region *R = RI->getRegionFor(&BB);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000246 if (R->getEntry() != &BB) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000247 INVALID(CFG, "Not well structured condition at BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000248 return false;
249 }
Tobias Grosser75805372011-04-29 06:27:02 +0000250
251 return true;
252}
253
254bool ScopDetection::isValidCallInst(CallInst &CI) {
255 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
256 return false;
257
258 if (CI.doesNotAccessMemory())
259 return true;
260
261 Function *CalledFunction = CI.getCalledFunction();
262
263 // Indirect calls are not supported.
264 if (CalledFunction == 0)
265 return false;
266
267 // TODO: Intrinsics.
268 return false;
269}
270
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000271std::string ScopDetection::formatInvalidAlias(AliasSet &AS) const {
272 std::string Message;
273 raw_string_ostream OS(Message);
274
275 OS << "Possible aliasing: ";
276
277 std::vector<Value *> Pointers;
278
279 for (AliasSet::iterator AI = AS.begin(), AE = AS.end(); AI != AE; ++AI)
280 Pointers.push_back(AI.getPointer());
281
282 std::sort(Pointers.begin(), Pointers.end());
283
284 for (std::vector<Value *>::iterator PI = Pointers.begin(),
285 PE = Pointers.end();
286 ;) {
287 Value *V = *PI;
288
289 if (V->getName().size() == 0)
290 OS << "\"" << *V << "\"";
291 else
292 OS << "\"" << V->getName() << "\"";
293
294 ++PI;
295
296 if (PI != PE)
297 OS << ", ";
298 else
299 break;
300 }
301
302 return OS.str();
303}
304
Tobias Grosser75805372011-04-29 06:27:02 +0000305bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
306 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000307 Value *Ptr = getPointerOperand(Inst);
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000308 Loop *L = LI->getLoopFor(Inst.getParent());
309 const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000310 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000311 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000312
Tobias Grosserb8710b52011-11-10 12:44:50 +0000313 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
314
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000315 if (!BasePointer) {
Tobias Grosserb8710b52011-11-10 12:44:50 +0000316 INVALID(AffFunc, "No base pointer");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000317 return false;
318 }
Tobias Grosserb8710b52011-11-10 12:44:50 +0000319
320 BaseValue = BasePointer->getValue();
321
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000322 if (isa<UndefValue>(BaseValue)) {
Tobias Grosserb8710b52011-11-10 12:44:50 +0000323 INVALID(AffFunc, "Undefined base pointer");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000324 return false;
325 }
Tobias Grosserb8710b52011-11-10 12:44:50 +0000326
327 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
328
Tobias Grosser58032cb2013-06-23 01:29:29 +0000329 if (!AllowNonAffine &&
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000330 !isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue)) {
Tobias Grossereeb776a2012-09-08 14:00:37 +0000331 INVALID(AffFunc, "Non affine access function: " << *AccessFunction);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000332 return false;
333 }
Tobias Grosser75805372011-04-29 06:27:02 +0000334
335 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
336 // created by IndependentBlocks Pass.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000337 if (isa<IntToPtrInst>(BaseValue)) {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000338 INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000339 return false;
340 }
Tobias Grosser75805372011-04-29 06:27:02 +0000341
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000342 if (IgnoreAliasing)
343 return true;
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000344
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000345 // Check if the base pointer of the memory access does alias with
346 // any other pointer. This cannot be handled at the moment.
Tobias Grosser298a7642013-07-14 18:09:43 +0000347 AliasSet &AS =
348 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
349 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosser428b3e42013-02-04 15:46:25 +0000350
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000351 // INVALID triggers an assertion in verifying mode, if it detects that a
352 // SCoP was detected by SCoP detection and that this SCoP was invalidated by
353 // a pass that stated it would preserve the SCoPs. We disable this check as
354 // the independent blocks pass may create memory references which seem to
355 // alias, if -basicaa is not available. They actually do not, but as we can
356 // not proof this without -basicaa we would fail. We disable this check to
357 // not cause irrelevant verification failures.
358 if (!AS.isMustAlias()) {
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000359 INVALID_NOVERIFY(Alias, formatInvalidAlias(AS));
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000360 return false;
Tobias Grosser428b3e42013-02-04 15:46:25 +0000361 }
Tobias Grosser75805372011-04-29 06:27:02 +0000362
363 return true;
364}
365
Tobias Grosser75805372011-04-29 06:27:02 +0000366bool ScopDetection::isValidInstruction(Instruction &Inst,
367 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000368 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000369 if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000370 if (SCEVCodegen) {
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000371 INVALID(IndVar,
372 "SCEV of PHI node refers to SSA names in region: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000373 return false;
374
375 } else {
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000376 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000377 return false;
378 }
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000379 }
Tobias Grosser75805372011-04-29 06:27:02 +0000380
Tobias Grosser75805372011-04-29 06:27:02 +0000381 // We only check the call instruction but not invoke instruction.
382 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
383 if (isValidCallInst(*CI))
384 return true;
385
Tobias Grosserb43ba822011-10-08 00:49:30 +0000386 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000387 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000388 }
389
390 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000391 if (!isa<AllocaInst>(Inst))
392 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000393
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000394 INVALID(Other, "Alloca instruction: " << Inst);
395 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000396 }
397
398 // Check the access function.
399 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
400 return isValidMemoryAccess(Inst, Context);
401
402 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000403 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000404 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000405}
406
Tobias Grosser75805372011-04-29 06:27:02 +0000407bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000408 if (!SCEVCodegen) {
409 // If code generation is not in scev based mode, we need to ensure that
410 // each loop has a canonical induction variable.
411 PHINode *IndVar = L->getCanonicalInductionVariable();
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000412 if (!IndVar) {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000413 INVALID(IndVar,
414 "No canonical IV at loop header: " << L->getHeader()->getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000415 return false;
416 }
Tobias Grosser826b2af2013-03-21 16:14:50 +0000417 }
Tobias Grosser75805372011-04-29 06:27:02 +0000418
419 // Is the loop count affine?
420 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000421 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) {
Tobias Grosser58032cb2013-06-23 01:29:29 +0000422 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
423 << L->getHeader()->getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000424 return false;
425 }
Tobias Grosser75805372011-04-29 06:27:02 +0000426
427 return true;
428}
429
430Region *ScopDetection::expandRegion(Region &R) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000431 // Initial no valid region was found (greater than R)
432 Region *LastValidRegion = NULL;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000433 Region *ExpandedRegion = R.getExpandedRegion();
Tobias Grosser75805372011-04-29 06:27:02 +0000434
435 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
436
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000437 while (ExpandedRegion) {
438 DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
439 DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000440
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000441 // Check the exit first (cheap)
Tobias Grosser75805372011-04-29 06:27:02 +0000442 if (isValidExit(Context)) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000443 // If the exit is valid check all blocks
444 // - if true, a valid region was found => store it + keep expanding
445 // - if false, .tbd. => stop (should this really end the loop?)
446 if (!allBlocksValid(Context))
447 break;
Tobias Grosser75805372011-04-29 06:27:02 +0000448
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000449 // Delete unnecessary regions (allocated by getExpandedRegion)
450 if (LastValidRegion)
451 delete LastValidRegion;
452
Tobias Grosserd7e58642013-04-10 06:55:45 +0000453 // Store this region, because it is the greatest valid (encountered so
454 // far).
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000455 LastValidRegion = ExpandedRegion;
456
457 // Create and test the next greater region (if any)
458 ExpandedRegion = ExpandedRegion->getExpandedRegion();
459
460 } else {
461 // Create and test the next greater region (if any)
462 Region *TmpRegion = ExpandedRegion->getExpandedRegion();
463
464 // Delete unnecessary regions (allocated by getExpandedRegion)
465 delete ExpandedRegion;
466
467 ExpandedRegion = TmpRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000468 }
Tobias Grosser75805372011-04-29 06:27:02 +0000469 }
470
Tobias Grosser983e7852013-07-28 09:05:20 +0000471 DEBUG(if (LastValidRegion) dbgs() << "\tto " << LastValidRegion->getNameStr()
472 << "\n";
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000473 else dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";);
Tobias Grosser75805372011-04-29 06:27:02 +0000474
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000475 return LastValidRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000476}
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000477static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
478 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
479 ++I)
480 if (R.contains(LI->getLoopFor(*I)))
481 return false;
482
483 return true;
484}
Tobias Grosser75805372011-04-29 06:27:02 +0000485
Tobias Grosser75805372011-04-29 06:27:02 +0000486void ScopDetection::findScops(Region &R) {
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000487
488 if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
489 return;
490
Tobias Grosser75805372011-04-29 06:27:02 +0000491 DetectionContext Context(R, *AA, false /*verifying*/);
492
Tobias Grosser4eb73812011-11-10 12:45:15 +0000493 LastFailure = "";
494
Tobias Grosser75805372011-04-29 06:27:02 +0000495 if (isValidRegion(Context)) {
496 ++ValidRegion;
497 ValidRegions.insert(&R);
498 return;
499 }
500
Tobias Grosser4f129a62011-10-08 00:30:55 +0000501 InvalidRegions[&R] = LastFailure;
502
Tobias Grosser75805372011-04-29 06:27:02 +0000503 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
504 findScops(**I);
505
506 // Try to expand regions.
507 //
508 // As the region tree normally only contains canonical regions, non canonical
509 // regions that form a Scop are not found. Therefore, those non canonical
510 // regions are checked by expanding the canonical ones.
511
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000512 std::vector<Region *> ToExpand;
Tobias Grosser75805372011-04-29 06:27:02 +0000513
514 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
515 ToExpand.push_back(*I);
516
Tobias Grosseraf3c00b82013-02-05 09:40:22 +0000517 for (std::vector<Region *>::iterator RI = ToExpand.begin(),
518 RE = ToExpand.end();
519 RI != RE; ++RI) {
Tobias Grosser75805372011-04-29 06:27:02 +0000520 Region *CurrentRegion = *RI;
521
522 // Skip invalid regions. Regions may become invalid, if they are element of
523 // an already expanded region.
524 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
525 continue;
526
527 Region *ExpandedR = expandRegion(*CurrentRegion);
528
529 if (!ExpandedR)
530 continue;
531
532 R.addSubRegion(ExpandedR, true);
533 ValidRegions.insert(ExpandedR);
534 ValidRegions.erase(CurrentRegion);
535
536 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
537 ++I)
538 ValidRegions.erase(*I);
539 }
540}
541
542bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
543 Region &R = Context.CurRegion;
544
545 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
Sebastian Popb88ea5e2013-06-11 22:20:32 +0000546 ++I) {
547 Loop *L = LI->getLoopFor(*I);
548 if (L && L->getHeader() == *I && !isValidLoop(L, Context))
549 return false;
550 }
551
552 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
Tobias Grosser75805372011-04-29 06:27:02 +0000553 ++I)
Sebastian Pop9e3d2dd2013-06-11 22:20:27 +0000554 if (!isValidCFG(**I, Context))
555 return false;
556
Sebastian Pop8ca899c2013-06-14 20:20:43 +0000557 for (Region::block_iterator BI = R.block_begin(), E = R.block_end(); BI != E;
558 ++BI)
559 for (BasicBlock::iterator I = (*BI)->begin(), E = --(*BI)->end(); I != E;
560 ++I)
561 if (!isValidInstruction(*I, Context))
562 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000563
564 return true;
565}
566
567bool ScopDetection::isValidExit(DetectionContext &Context) const {
568 Region &R = Context.CurRegion;
569
570 // PHI nodes are not allowed in the exit basic block.
571 if (BasicBlock *Exit = R.getExit()) {
572 BasicBlock::iterator I = Exit->begin();
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000573 if (I != Exit->end() && isa<PHINode>(*I)) {
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000574 INVALID(Other, "PHI node in exit BB");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000575 return false;
576 }
Tobias Grosser75805372011-04-29 06:27:02 +0000577 }
578
579 return true;
580}
581
582bool ScopDetection::isValidRegion(DetectionContext &Context) const {
583 Region &R = Context.CurRegion;
584
585 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
586
587 // The toplevel region is no valid region.
Tobias Grosseraeabcf22013-04-02 06:41:48 +0000588 if (R.isTopLevelRegion()) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000589 DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000590 return false;
591 }
592
Tobias Grossere602a072013-05-07 07:30:56 +0000593 if (!R.getEnteringBlock()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000594 BasicBlock *entry = R.getEntry();
595 Loop *L = LI->getLoopFor(entry);
596
597 if (L) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000598 if (!L->isLoopSimplifyForm()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000599 INVALID(SimpleLoop, "Loop not in simplify form is invalid!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000600 return false;
601 }
Sebastian Pop9d632342013-06-11 22:20:40 +0000602
603 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
604 ++PI) {
605 // Region entering edges come from the same loop but outside the region
606 // are not allowed.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000607 if (L->contains(*PI) && !R.contains(*PI)) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000608 INVALID(IndEdge, "Region has invalid entering edges!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000609 return false;
610 }
Sebastian Pop9d632342013-06-11 22:20:40 +0000611 }
612 }
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000613 }
614
Tobias Grosserd654c252012-04-10 18:12:19 +0000615 // SCoP cannot contain the entry block of the function, because we need
Tobias Grosser75805372011-04-29 06:27:02 +0000616 // to insert alloca instruction there when translate scalar to array.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000617 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) {
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000618 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000619 return false;
620 }
Tobias Grosser75805372011-04-29 06:27:02 +0000621
Hongbin Zheng94868e62012-04-07 12:29:17 +0000622 if (!isValidExit(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000623 return false;
624
Hongbin Zheng94868e62012-04-07 12:29:17 +0000625 if (!allBlocksValid(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000626 return false;
627
628 DEBUG(dbgs() << "OK\n");
629 return true;
630}
631
632bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000633 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000634}
635
Tobias Grosser531891e2012-11-01 16:45:20 +0000636void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
637 unsigned &LineEnd, std::string &FileName) {
638 LineBegin = -1;
639 LineEnd = 0;
640
641 for (Region::const_block_iterator RI = R->block_begin(), RE = R->block_end();
642 RI != RE; ++RI)
643 for (BasicBlock::iterator BI = (*RI)->begin(), BE = (*RI)->end(); BI != BE;
644 ++BI) {
645 DebugLoc DL = BI->getDebugLoc();
646 if (DL.isUnknown())
647 continue;
648
649 DIScope Scope(DL.getScope(BI->getContext()));
650
651 if (FileName.empty())
652 FileName = Scope.getFilename();
653
654 unsigned NewLine = DL.getLine();
655
656 LineBegin = std::min(LineBegin, NewLine);
657 LineEnd = std::max(LineEnd, NewLine);
658 break;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000659 }
Tobias Grosser531891e2012-11-01 16:45:20 +0000660}
661
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000662void ScopDetection::printLocations(llvm::Function &F) {
663 int NumberOfScops = std::distance(begin(), end());
664
665 if (NumberOfScops)
666 outs() << ":: Static control regions in " << F.getName() << "\n";
667
Tobias Grosser531891e2012-11-01 16:45:20 +0000668 for (iterator RI = begin(), RE = end(); RI != RE; ++RI) {
669 unsigned LineEntry, LineExit;
670 std::string FileName;
671
672 getDebugLocation(*RI, LineEntry, LineExit, FileName);
673
674 if (FileName.empty()) {
675 outs() << "Scop detected at unknown location. Compile with debug info "
Tobias Grosseraf3c00b82013-02-05 09:40:22 +0000676 "(-g) to get more precise information. \n";
Tobias Grosser531891e2012-11-01 16:45:20 +0000677 return;
678 }
679
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000680 outs() << FileName << ":" << LineEntry
681 << ": Start of static control region\n";
682 outs() << FileName << ":" << LineExit << ": End of static control region\n";
Tobias Grosser531891e2012-11-01 16:45:20 +0000683 }
684}
685
Tobias Grosser75805372011-04-29 06:27:02 +0000686bool ScopDetection::runOnFunction(llvm::Function &F) {
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000687 LI = &getAnalysis<LoopInfo>();
688 if (!DetectScopsWithoutLoops && LI->empty())
689 return false;
690
Tobias Grosser75805372011-04-29 06:27:02 +0000691 AA = &getAnalysis<AliasAnalysis>();
692 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000693 RI = &getAnalysis<RegionInfo>();
694 Region *TopRegion = RI->getTopLevelRegion();
695
Tobias Grosser2ff87232011-10-23 11:17:06 +0000696 releaseMemory();
697
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000698 if (OnlyFunction != "" && F.getName() != OnlyFunction)
Tobias Grosser2ff87232011-10-23 11:17:06 +0000699 return false;
700
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000701 if (!isValidFunction(F))
Tobias Grosser75805372011-04-29 06:27:02 +0000702 return false;
703
704 findScops(*TopRegion);
Tobias Grosser531891e2012-11-01 16:45:20 +0000705
706 if (ReportLevel >= 1)
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000707 printLocations(F);
Tobias Grosser531891e2012-11-01 16:45:20 +0000708
Tobias Grosser75805372011-04-29 06:27:02 +0000709 return false;
710}
711
Tobias Grosser75805372011-04-29 06:27:02 +0000712void polly::ScopDetection::verifyRegion(const Region &R) const {
713 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000714 DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
Tobias Grosser75805372011-04-29 06:27:02 +0000715 isValidRegion(Context);
716}
717
718void polly::ScopDetection::verifyAnalysis() const {
719 for (RegionSet::const_iterator I = ValidRegions.begin(),
Tobias Grosseraf3c00b82013-02-05 09:40:22 +0000720 E = ValidRegions.end();
721 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +0000722 verifyRegion(**I);
723}
724
725void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
726 AU.addRequired<DominatorTree>();
727 AU.addRequired<PostDominatorTree>();
728 AU.addRequired<LoopInfo>();
729 AU.addRequired<ScalarEvolution>();
730 // We also need AA and RegionInfo when we are verifying analysis.
731 AU.addRequiredTransitive<AliasAnalysis>();
732 AU.addRequiredTransitive<RegionInfo>();
733 AU.setPreservesAll();
734}
735
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000736void ScopDetection::print(raw_ostream &OS, const Module *) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000737 for (RegionSet::const_iterator I = ValidRegions.begin(),
Tobias Grosseraf3c00b82013-02-05 09:40:22 +0000738 E = ValidRegions.end();
739 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +0000740 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
741
742 OS << "\n";
743}
744
745void ScopDetection::releaseMemory() {
746 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000747 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000748 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000749}
750
751char ScopDetection::ID = 0;
752
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000753Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
754
Tobias Grosser73600b82011-10-08 00:30:40 +0000755INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
756 "Polly - Detect static control parts (SCoPs)", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000757 false);
758INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
759INITIALIZE_PASS_DEPENDENCY(DominatorTree);
760INITIALIZE_PASS_DEPENDENCY(LoopInfo);
761INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
762INITIALIZE_PASS_DEPENDENCY(RegionInfo);
763INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Tobias Grosser73600b82011-10-08 00:30:40 +0000764INITIALIZE_PASS_END(ScopDetection, "polly-detect",
765 "Polly - Detect static control parts (SCoPs)", false, false)