blob: a2dd85645e1717679e9ed1aa249b6a74c4f71611 [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
47#include "polly/ScopDetection.h"
48
49#include "polly/LinkAllPasses.h"
50#include "polly/Support/ScopHelper.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000051#include "polly/Support/SCEVValidator.h"
Tobias Grosser75805372011-04-29 06:27:02 +000052
53#include "llvm/LLVMContext.h"
54#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"
Tobias Grosser75805372011-04-29 06:27:02 +000060#include "llvm/Support/CommandLine.h"
61#include "llvm/Assembly/Writer.h"
62
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
Tobias Grosser2ff87232011-10-23 11:17:06 +000071static cl::opt<std::string>
72OnlyFunction("polly-detect-only",
73 cl::desc("Only detect scops in function"), cl::Hidden,
74 cl::value_desc("The function name to detect scops in"),
75 cl::ValueRequired, cl::init(""));
76
Tobias Grosser60cd9322011-11-10 12:47:26 +000077static cl::opt<bool>
78IgnoreAliasing("polly-ignore-aliasing",
79 cl::desc("Ignore possible aliasing of the array bases"),
80 cl::Hidden, cl::init(false));
Tobias Grosser2ff87232011-10-23 11:17:06 +000081
Tobias Grosser75805372011-04-29 06:27:02 +000082//===----------------------------------------------------------------------===//
83// Statistics.
84
85STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
86
87#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
88 "Number of bad regions for Scop: "\
89 DESC)
90
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000091#define INVALID(NAME, MESSAGE) \
92 do { \
Tobias Grosser4f129a62011-10-08 00:30:55 +000093 std::string Buf; \
94 raw_string_ostream fmt(Buf); \
95 fmt << MESSAGE; \
96 fmt.flush(); \
97 LastFailure = Buf; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000098 DEBUG(dbgs() << MESSAGE); \
99 DEBUG(dbgs() << "\n"); \
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000100 assert(!Context.Verifying && #NAME); \
101 if (!Context.Verifying) ++Bad##NAME##ForScop; \
102 return false; \
103 } while (0);
104
105
106#define INVALID_NOVERIFY(NAME, MESSAGE) \
107 do { \
108 std::string Buf; \
109 raw_string_ostream fmt(Buf); \
110 fmt << MESSAGE; \
111 fmt.flush(); \
112 LastFailure = Buf; \
113 DEBUG(dbgs() << MESSAGE); \
114 DEBUG(dbgs() << "\n"); \
115 /* DISABLED: assert(!Context.Verifying && #NAME); */ \
116 if (!Context.Verifying) ++Bad##NAME##ForScop; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000117 return false; \
118 } while (0);
119
120
Tobias Grosser75805372011-04-29 06:27:02 +0000121BADSCOP_STAT(CFG, "CFG too complex");
122BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
123BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
124BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
125BADSCOP_STAT(AffFunc, "Expression not affine");
126BADSCOP_STAT(Scalar, "Found scalar dependency");
127BADSCOP_STAT(Alias, "Found base address alias");
128BADSCOP_STAT(SimpleRegion, "Region not simple");
129BADSCOP_STAT(Other, "Others");
130
131//===----------------------------------------------------------------------===//
132// ScopDetection.
Tobias Grosser75805372011-04-29 06:27:02 +0000133bool ScopDetection::isMaxRegionInScop(const Region &R) const {
134 // The Region is valid only if it could be found in the set.
135 return ValidRegions.count(&R);
136}
137
Tobias Grosser4f129a62011-10-08 00:30:55 +0000138std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
139 if (!InvalidRegions.count(R))
140 return "";
141
142 return InvalidRegions.find(R)->second;
143}
144
Tobias Grosser75805372011-04-29 06:27:02 +0000145bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
146{
147 Region &RefRegion = Context.CurRegion;
148 TerminatorInst *TI = BB.getTerminator();
149
150 // Return instructions are only valid if the region is the top level region.
151 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
152 return true;
153
154 BranchInst *Br = dyn_cast<BranchInst>(TI);
155
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000156 if (!Br)
157 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000158
159 if (Br->isUnconditional()) return true;
160
161 Value *Condition = Br->getCondition();
162
163 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000164 if (isa<UndefValue>(Condition))
165 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
166 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000167
168 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000169 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
170 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
171 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000172
173 // Allow perfectly nested conditions.
174 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
175
176 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
177 // Unsigned comparisons are not allowed. They trigger overflow problems
178 // in the code generation.
179 //
180 // TODO: This is not sufficient and just hides bugs. However it does pretty
181 // well.
182 if(ICmp->isUnsigned())
183 return false;
184
185 // Are both operands of the ICmp affine?
186 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000187 || isa<UndefValue>(ICmp->getOperand(1)))
188 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000189
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000190 const SCEV *LHS = SE->getSCEV(ICmp->getOperand(0));
191 const SCEV *RHS = SE->getSCEV(ICmp->getOperand(1));
Tobias Grosser75805372011-04-29 06:27:02 +0000192
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000193 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
194 !isAffineExpr(&Context.CurRegion, RHS, *SE))
195 INVALID(AffFunc, "Non affine branch in BB '" << BB.getNameStr()
196 << "' with LHS: " << *LHS << " and RHS: " << *RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000197 }
198
199 // Allow loop exit conditions.
200 Loop *L = LI->getLoopFor(&BB);
201 if (L && L->getExitingBlock() == &BB)
202 return true;
203
204 // Allow perfectly nested conditions.
205 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000206 if (R->getEntry() != &BB)
207 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000208
209 return true;
210}
211
212bool ScopDetection::isValidCallInst(CallInst &CI) {
213 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
214 return false;
215
216 if (CI.doesNotAccessMemory())
217 return true;
218
219 Function *CalledFunction = CI.getCalledFunction();
220
221 // Indirect calls are not supported.
222 if (CalledFunction == 0)
223 return false;
224
225 // TODO: Intrinsics.
226 return false;
227}
228
229bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
230 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000231 Value *Ptr = getPointerOperand(Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000232 const SCEV *AccessFunction = SE->getSCEV(Ptr);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000233 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000234 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000235
Tobias Grosserb8710b52011-11-10 12:44:50 +0000236 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
237
238 if (!BasePointer)
239 INVALID(AffFunc, "No base pointer");
240
241 BaseValue = BasePointer->getValue();
242
243 if (isa<UndefValue>(BaseValue))
244 INVALID(AffFunc, "Undefined base pointer");
245
246 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
247
Tobias Grossere5e171e2011-11-10 12:45:03 +0000248 if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000249 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000250
251 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
252 // created by IndependentBlocks Pass.
Tobias Grossere5e171e2011-11-10 12:45:03 +0000253 if (isa<IntToPtrInst>(BaseValue))
254 INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000255
256 // Check if the base pointer of the memory access does alias with
257 // any other pointer. This cannot be handled at the moment.
258 AliasSet &AS =
Tobias Grossere5e171e2011-11-10 12:45:03 +0000259 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
Tobias Grosser75805372011-04-29 06:27:02 +0000260 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000261
262 // INVALID triggers an assertion in verifying mode, if it detects that a SCoP
263 // was detected by SCoP detection and that this SCoP was invalidated by a pass
264 // that stated it would preserve the SCoPs.
265 // We disable this check as the independent blocks pass may create memory
266 // references which seem to alias, if -basicaa is not available. They actually
267 // do not, but as we can not proof this without -basicaa we would fail. We
268 // disable this check to not cause irrelevant verification failures.
Tobias Grosser60cd9322011-11-10 12:47:26 +0000269 if (!AS.isMustAlias() && !IgnoreAliasing)
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000270 INVALID_NOVERIFY(Alias,
271 "Possible aliasing found for value: " << *BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000272
273 return true;
274}
275
276
277bool ScopDetection::hasScalarDependency(Instruction &Inst,
278 Region &RefRegion) const {
279 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
280 UI != UE; ++UI)
281 if (Instruction *Use = dyn_cast<Instruction>(*UI))
282 if (!RefRegion.contains(Use->getParent())) {
283 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
284 // PHINode is induction variable, the scalar to array transform may
285 // break it and introduce a non-indvar PHINode, which is not allow in
286 // Scop.
287 // This can be fix by:
288 // Introduce a IndependentBlockPrepare pass, which translate all
289 // PHINodes not in Scop to array.
290 // The IndependentBlockPrepare pass can also split the entry block of
291 // the function to hold the alloca instruction created by scalar to
292 // array. and split the exit block of the Scop so the new create load
293 // instruction for escape users will not break other Scops.
294 if (isa<PHINode>(Use))
295 return true;
296 }
297
298 return false;
299}
300
301bool ScopDetection::isValidInstruction(Instruction &Inst,
302 DetectionContext &Context) const {
303 // Only canonical IVs are allowed.
304 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000305 if (!isIndVar(PN, LI))
306 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000307
308 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000309 if (hasScalarDependency(Inst, Context.CurRegion))
310 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000311
312 // We only check the call instruction but not invoke instruction.
313 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
314 if (isValidCallInst(*CI))
315 return true;
316
Tobias Grosserb43ba822011-10-08 00:49:30 +0000317 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000318 }
319
320 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
321 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000322 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000323 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000324
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000325 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000326 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000327
328 return true;
329 }
330
331 // Check the access function.
332 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
333 return isValidMemoryAccess(Inst, Context);
334
335 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000336 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000337}
338
339bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
340 DetectionContext &Context) const {
341 if (!isValidCFG(BB, Context))
342 return false;
343
344 // Check all instructions, except the terminator instruction.
345 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
346 if (!isValidInstruction(*I, Context))
347 return false;
348
349 Loop *L = LI->getLoopFor(&BB);
350 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
351 return false;
352
353 return true;
354}
355
356bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
357 PHINode *IndVar = L->getCanonicalInductionVariable();
358 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000359 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000360 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000361 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000362
363 // Is the loop count affine?
364 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000365 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
Tobias Grosserbd54f322011-10-26 01:27:49 +0000366 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000367 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000368
369 return true;
370}
371
372Region *ScopDetection::expandRegion(Region &R) {
373 Region *CurrentRegion = &R;
374 Region *TmpRegion = R.getExpandedRegion();
375
376 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
377
378 while (TmpRegion) {
379 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
380 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
381
382 if (!allBlocksValid(Context))
383 break;
384
385 if (isValidExit(Context)) {
386 if (CurrentRegion != &R)
387 delete CurrentRegion;
388
389 CurrentRegion = TmpRegion;
390 }
391
392 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
393
394 if (TmpRegion != &R && TmpRegion != CurrentRegion)
395 delete TmpRegion;
396
397 TmpRegion = TmpRegion2;
398 }
399
400 if (&R == CurrentRegion)
401 return NULL;
402
403 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
404
405 return CurrentRegion;
406}
407
408
409void ScopDetection::findScops(Region &R) {
410 DetectionContext Context(R, *AA, false /*verifying*/);
411
Tobias Grosser4eb73812011-11-10 12:45:15 +0000412 LastFailure = "";
413
Tobias Grosser75805372011-04-29 06:27:02 +0000414 if (isValidRegion(Context)) {
415 ++ValidRegion;
416 ValidRegions.insert(&R);
417 return;
418 }
419
Tobias Grosser4f129a62011-10-08 00:30:55 +0000420 InvalidRegions[&R] = LastFailure;
421
Tobias Grosser75805372011-04-29 06:27:02 +0000422 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
423 findScops(**I);
424
425 // Try to expand regions.
426 //
427 // As the region tree normally only contains canonical regions, non canonical
428 // regions that form a Scop are not found. Therefore, those non canonical
429 // regions are checked by expanding the canonical ones.
430
431 std::vector<Region*> ToExpand;
432
433 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
434 ToExpand.push_back(*I);
435
436 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
437 RE = ToExpand.end(); RI != RE; ++RI) {
438 Region *CurrentRegion = *RI;
439
440 // Skip invalid regions. Regions may become invalid, if they are element of
441 // an already expanded region.
442 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
443 continue;
444
445 Region *ExpandedR = expandRegion(*CurrentRegion);
446
447 if (!ExpandedR)
448 continue;
449
450 R.addSubRegion(ExpandedR, true);
451 ValidRegions.insert(ExpandedR);
452 ValidRegions.erase(CurrentRegion);
453
454 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
455 ++I)
456 ValidRegions.erase(*I);
457 }
458}
459
460bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
461 Region &R = Context.CurRegion;
462
463 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
464 ++I)
465 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
466 return false;
467
468 return true;
469}
470
471bool ScopDetection::isValidExit(DetectionContext &Context) const {
472 Region &R = Context.CurRegion;
473
474 // PHI nodes are not allowed in the exit basic block.
475 if (BasicBlock *Exit = R.getExit()) {
476 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000477 if (I != Exit->end() && isa<PHINode> (*I))
478 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000479 }
480
481 return true;
482}
483
484bool ScopDetection::isValidRegion(DetectionContext &Context) const {
485 Region &R = Context.CurRegion;
486
487 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
488
489 // The toplevel region is no valid region.
490 if (!R.getParent()) {
491 DEBUG(dbgs() << "Top level region is invalid";
492 dbgs() << "\n");
493 return false;
494 }
495
496 // SCoP can not contains the entry block of the function, because we need
497 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000498 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
499 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000500
501 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000502 if (!R.isSimple())
503 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000504
505 if (!allBlocksValid(Context))
506 return false;
507
508 if (!isValidExit(Context))
509 return false;
510
511 DEBUG(dbgs() << "OK\n");
512 return true;
513}
514
515bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000516 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000517}
518
519bool ScopDetection::runOnFunction(llvm::Function &F) {
520 AA = &getAnalysis<AliasAnalysis>();
521 SE = &getAnalysis<ScalarEvolution>();
522 LI = &getAnalysis<LoopInfo>();
523 RI = &getAnalysis<RegionInfo>();
524 Region *TopRegion = RI->getTopLevelRegion();
525
Tobias Grosser2ff87232011-10-23 11:17:06 +0000526 releaseMemory();
527
528 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
529 return false;
530
Tobias Grosser75805372011-04-29 06:27:02 +0000531 if(!isValidFunction(F))
532 return false;
533
534 findScops(*TopRegion);
535 return false;
536}
537
538
539void polly::ScopDetection::verifyRegion(const Region &R) const {
540 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
541 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
542 isValidRegion(Context);
543}
544
545void polly::ScopDetection::verifyAnalysis() const {
546 for (RegionSet::const_iterator I = ValidRegions.begin(),
547 E = ValidRegions.end(); I != E; ++I)
548 verifyRegion(**I);
549}
550
551void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
552 AU.addRequired<DominatorTree>();
553 AU.addRequired<PostDominatorTree>();
554 AU.addRequired<LoopInfo>();
555 AU.addRequired<ScalarEvolution>();
556 // We also need AA and RegionInfo when we are verifying analysis.
557 AU.addRequiredTransitive<AliasAnalysis>();
558 AU.addRequiredTransitive<RegionInfo>();
559 AU.setPreservesAll();
560}
561
562void ScopDetection::print(raw_ostream &OS, const Module *) const {
563 for (RegionSet::const_iterator I = ValidRegions.begin(),
564 E = ValidRegions.end(); I != E; ++I)
565 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
566
567 OS << "\n";
568}
569
570void ScopDetection::releaseMemory() {
571 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000572 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000573 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000574}
575
576char ScopDetection::ID = 0;
577
Tobias Grosser73600b82011-10-08 00:30:40 +0000578INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
579 "Polly - Detect static control parts (SCoPs)", false,
580 false)
581INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
582INITIALIZE_PASS_DEPENDENCY(DominatorTree)
583INITIALIZE_PASS_DEPENDENCY(LoopInfo)
584INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
585INITIALIZE_PASS_DEPENDENCY(RegionInfo)
586INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
587INITIALIZE_PASS_END(ScopDetection, "polly-detect",
588 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000589
Tobias Grosser83f5c432011-08-23 22:35:08 +0000590Pass *polly::createScopDetectionPass() {
591 return new ScopDetection();
592}