blob: 083ad439f84a4039ebbed6e67ffacf11a6eff523 [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"
51#include "polly/Support/AffineSCEVIterator.h"
52
53#include "llvm/LLVMContext.h"
54#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/AliasAnalysis.h"
56#include "llvm/Analysis/RegionIterator.h"
57#include "llvm/Support/CommandLine.h"
58#include "llvm/Assembly/Writer.h"
59
60#define DEBUG_TYPE "polly-detect"
61#include "llvm/Support/Debug.h"
62
63using namespace llvm;
64using namespace polly;
65
66//===----------------------------------------------------------------------===//
67// Statistics.
68
69STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
70
71#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
72 "Number of bad regions for Scop: "\
73 DESC)
74
75#define STATSCOP(NAME); assert(!Context.Verifying && #NAME); \
76 if (!Context.Verifying) ++Bad##NAME##ForScop;
77
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000078#define INVALID(NAME, MESSAGE) \
79 do { \
Tobias Grosser4f129a62011-10-08 00:30:55 +000080 std::string Buf; \
81 raw_string_ostream fmt(Buf); \
82 fmt << MESSAGE; \
83 fmt.flush(); \
84 LastFailure = Buf; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000085 DEBUG(dbgs() << MESSAGE); \
86 DEBUG(dbgs() << "\n"); \
87 STATSCOP(NAME); \
88 return false; \
89 } while (0);
90
91
Tobias Grosser75805372011-04-29 06:27:02 +000092BADSCOP_STAT(CFG, "CFG too complex");
93BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
94BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
95BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
96BADSCOP_STAT(AffFunc, "Expression not affine");
97BADSCOP_STAT(Scalar, "Found scalar dependency");
98BADSCOP_STAT(Alias, "Found base address alias");
99BADSCOP_STAT(SimpleRegion, "Region not simple");
100BADSCOP_STAT(Other, "Others");
101
102//===----------------------------------------------------------------------===//
103// ScopDetection.
104
105bool ScopDetection::isMaxRegionInScop(const Region &R) const {
106 // The Region is valid only if it could be found in the set.
107 return ValidRegions.count(&R);
108}
109
Tobias Grosser4f129a62011-10-08 00:30:55 +0000110std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
111 if (!InvalidRegions.count(R))
112 return "";
113
114 return InvalidRegions.find(R)->second;
115}
116
117
Tobias Grosser75805372011-04-29 06:27:02 +0000118bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion,
119 Value **BasePtr) const {
120 assert(S && "S must not be null!");
121 bool isMemoryAccess = (BasePtr != 0);
122 if (isMemoryAccess) *BasePtr = 0;
123 DEBUG(dbgs() << "Checking " << *S << " ... ");
124
125 if (isa<SCEVCouldNotCompute>(S)) {
126 DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n");
127 return false;
128 }
129
130 for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E;
131 ++I) {
132 // The constant part must be a SCEVConstant.
133 // TODO: support sizeof in coefficient.
134 if (!isa<SCEVConstant>(I->second)) {
135 DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n");
136 return false;
137 }
138
139 const SCEV *Var = I->first;
140
141 // A constant offset is affine.
142 if(isa<SCEVConstant>(Var))
143 continue;
144
145 // Memory accesses are allowed to have a base pointer.
146 if (Var->getType()->isPointerTy()) {
147 if (!isMemoryAccess) {
148 DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n");
149 return false;
150 }
151
152 assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n");
153 const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var);
154
155 if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){
156 DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n");
157 return false;
158 }
159
160 // BaseAddr must be invariant in Scop.
161 if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) {
162 DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n");
163 return false;
164 }
165
166 assert(*BasePtr == 0 && "Found second base pointer.\n");
167 *BasePtr = BaseAddr->getValue();
168 continue;
169 }
170
171 if (isParameter(Var, RefRegion, *LI, *SE)
172 || isIndVar(Var, RefRegion, *LI, *SE))
173 continue;
174
175 DEBUG(dbgs() << "Non Affine: " ;
176 Var->print(dbgs());
177 dbgs() << " is neither parameter nor induction variable\n");
178 return false;
179 }
180
181 DEBUG(dbgs() << " is affine.\n");
182 return !isMemoryAccess || (*BasePtr != 0);
183}
184
185bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
186{
187 Region &RefRegion = Context.CurRegion;
188 TerminatorInst *TI = BB.getTerminator();
189
190 // Return instructions are only valid if the region is the top level region.
191 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
192 return true;
193
194 BranchInst *Br = dyn_cast<BranchInst>(TI);
195
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000196 if (!Br)
197 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000198
199 if (Br->isUnconditional()) return true;
200
201 Value *Condition = Br->getCondition();
202
203 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000204 if (isa<UndefValue>(Condition))
205 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
206 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000207
208 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000209 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
210 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
211 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000212
213 // Allow perfectly nested conditions.
214 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
215
216 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
217 // Unsigned comparisons are not allowed. They trigger overflow problems
218 // in the code generation.
219 //
220 // TODO: This is not sufficient and just hides bugs. However it does pretty
221 // well.
222 if(ICmp->isUnsigned())
223 return false;
224
225 // Are both operands of the ICmp affine?
226 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000227 || isa<UndefValue>(ICmp->getOperand(1)))
228 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000229
230 const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0));
231 const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1));
232
233 bool affineLHS = isValidAffineFunction(ScevLHS, RefRegion);
234 bool affineRHS = isValidAffineFunction(ScevRHS, RefRegion);
235
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000236 if (!affineLHS || !affineRHS)
237 INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000238 }
239
240 // Allow loop exit conditions.
241 Loop *L = LI->getLoopFor(&BB);
242 if (L && L->getExitingBlock() == &BB)
243 return true;
244
245 // Allow perfectly nested conditions.
246 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000247 if (R->getEntry() != &BB)
248 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000249
250 return true;
251}
252
253bool ScopDetection::isValidCallInst(CallInst &CI) {
254 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
255 return false;
256
257 if (CI.doesNotAccessMemory())
258 return true;
259
260 Function *CalledFunction = CI.getCalledFunction();
261
262 // Indirect calls are not supported.
263 if (CalledFunction == 0)
264 return false;
265
266 // TODO: Intrinsics.
267 return false;
268}
269
270bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
271 DetectionContext &Context) const {
272 Value *Ptr = getPointerOperand(Inst), *BasePtr;
273 const SCEV *AccessFunction = SE->getSCEV(Ptr);
274
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000275 if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr))
276 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000277
278 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
279 // created by IndependentBlocks Pass.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000280 if (isa<IntToPtrInst>(BasePtr))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000281 INVALID(Other, "Find bad intToptr prt: " << *BasePtr);
Tobias Grosser75805372011-04-29 06:27:02 +0000282
283 // Check if the base pointer of the memory access does alias with
284 // any other pointer. This cannot be handled at the moment.
285 AliasSet &AS =
286 Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize,
287 Inst.getMetadata(LLVMContext::MD_tbaa));
288 if (!AS.isMustAlias()) {
289 DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS);
290
291 // STATSCOP triggers an assertion if we are in verifying mode.
292 // This is generally good to check that we do not change the SCoP after we
293 // run the SCoP detection and consequently to ensure that we can still
294 // represent that SCoP. However, in case of aliasing this does not work.
295 // The independent blocks pass may create memory references which seem to
296 // alias, if -basicaa is not available. They actually do not. As we do not
297 // not know this and we would fail here if we verify it.
298 if (!Context.Verifying) {
299 STATSCOP(Alias);
300 }
301
302 return false;
303 }
304
305 return true;
306}
307
308
309bool ScopDetection::hasScalarDependency(Instruction &Inst,
310 Region &RefRegion) const {
311 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
312 UI != UE; ++UI)
313 if (Instruction *Use = dyn_cast<Instruction>(*UI))
314 if (!RefRegion.contains(Use->getParent())) {
315 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
316 // PHINode is induction variable, the scalar to array transform may
317 // break it and introduce a non-indvar PHINode, which is not allow in
318 // Scop.
319 // This can be fix by:
320 // Introduce a IndependentBlockPrepare pass, which translate all
321 // PHINodes not in Scop to array.
322 // The IndependentBlockPrepare pass can also split the entry block of
323 // the function to hold the alloca instruction created by scalar to
324 // array. and split the exit block of the Scop so the new create load
325 // instruction for escape users will not break other Scops.
326 if (isa<PHINode>(Use))
327 return true;
328 }
329
330 return false;
331}
332
333bool ScopDetection::isValidInstruction(Instruction &Inst,
334 DetectionContext &Context) const {
335 // Only canonical IVs are allowed.
336 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000337 if (!isIndVar(PN, LI))
338 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000339
340 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000341 if (hasScalarDependency(Inst, Context.CurRegion))
342 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000343
344 // We only check the call instruction but not invoke instruction.
345 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
346 if (isValidCallInst(*CI))
347 return true;
348
Tobias Grosserb43ba822011-10-08 00:49:30 +0000349 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000350 }
351
352 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
353 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000354 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000355 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000356
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000357 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000358 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000359
360 return true;
361 }
362
363 // Check the access function.
364 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
365 return isValidMemoryAccess(Inst, Context);
366
367 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000368 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000369}
370
371bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
372 DetectionContext &Context) const {
373 if (!isValidCFG(BB, Context))
374 return false;
375
376 // Check all instructions, except the terminator instruction.
377 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
378 if (!isValidInstruction(*I, Context))
379 return false;
380
381 Loop *L = LI->getLoopFor(&BB);
382 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
383 return false;
384
385 return true;
386}
387
388bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
389 PHINode *IndVar = L->getCanonicalInductionVariable();
390 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000391 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000392 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000393 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000394
395 // Is the loop count affine?
396 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000397 if (!isValidAffineFunction(LoopCount, Context.CurRegion))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000398 INVALID(LoopBound, "Non affine loop bound '" << LoopCount << "'for loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000399 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000400
401 return true;
402}
403
404Region *ScopDetection::expandRegion(Region &R) {
405 Region *CurrentRegion = &R;
406 Region *TmpRegion = R.getExpandedRegion();
407
408 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
409
410 while (TmpRegion) {
411 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
412 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
413
414 if (!allBlocksValid(Context))
415 break;
416
417 if (isValidExit(Context)) {
418 if (CurrentRegion != &R)
419 delete CurrentRegion;
420
421 CurrentRegion = TmpRegion;
422 }
423
424 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
425
426 if (TmpRegion != &R && TmpRegion != CurrentRegion)
427 delete TmpRegion;
428
429 TmpRegion = TmpRegion2;
430 }
431
432 if (&R == CurrentRegion)
433 return NULL;
434
435 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
436
437 return CurrentRegion;
438}
439
440
441void ScopDetection::findScops(Region &R) {
442 DetectionContext Context(R, *AA, false /*verifying*/);
443
444 if (isValidRegion(Context)) {
445 ++ValidRegion;
446 ValidRegions.insert(&R);
447 return;
448 }
449
Tobias Grosser4f129a62011-10-08 00:30:55 +0000450 InvalidRegions[&R] = LastFailure;
451
Tobias Grosser75805372011-04-29 06:27:02 +0000452 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
453 findScops(**I);
454
455 // Try to expand regions.
456 //
457 // As the region tree normally only contains canonical regions, non canonical
458 // regions that form a Scop are not found. Therefore, those non canonical
459 // regions are checked by expanding the canonical ones.
460
461 std::vector<Region*> ToExpand;
462
463 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
464 ToExpand.push_back(*I);
465
466 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
467 RE = ToExpand.end(); RI != RE; ++RI) {
468 Region *CurrentRegion = *RI;
469
470 // Skip invalid regions. Regions may become invalid, if they are element of
471 // an already expanded region.
472 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
473 continue;
474
475 Region *ExpandedR = expandRegion(*CurrentRegion);
476
477 if (!ExpandedR)
478 continue;
479
480 R.addSubRegion(ExpandedR, true);
481 ValidRegions.insert(ExpandedR);
482 ValidRegions.erase(CurrentRegion);
483
484 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
485 ++I)
486 ValidRegions.erase(*I);
487 }
488}
489
490bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
491 Region &R = Context.CurRegion;
492
493 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
494 ++I)
495 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
496 return false;
497
498 return true;
499}
500
501bool ScopDetection::isValidExit(DetectionContext &Context) const {
502 Region &R = Context.CurRegion;
503
504 // PHI nodes are not allowed in the exit basic block.
505 if (BasicBlock *Exit = R.getExit()) {
506 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000507 if (I != Exit->end() && isa<PHINode> (*I))
508 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000509 }
510
511 return true;
512}
513
514bool ScopDetection::isValidRegion(DetectionContext &Context) const {
515 Region &R = Context.CurRegion;
516
517 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
518
519 // The toplevel region is no valid region.
520 if (!R.getParent()) {
521 DEBUG(dbgs() << "Top level region is invalid";
522 dbgs() << "\n");
523 return false;
524 }
525
526 // SCoP can not contains the entry block of the function, because we need
527 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000528 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
529 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000530
531 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000532 if (!R.isSimple())
533 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000534
535 if (!allBlocksValid(Context))
536 return false;
537
538 if (!isValidExit(Context))
539 return false;
540
541 DEBUG(dbgs() << "OK\n");
542 return true;
543}
544
545bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000546 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000547}
548
549bool ScopDetection::runOnFunction(llvm::Function &F) {
550 AA = &getAnalysis<AliasAnalysis>();
551 SE = &getAnalysis<ScalarEvolution>();
552 LI = &getAnalysis<LoopInfo>();
553 RI = &getAnalysis<RegionInfo>();
554 Region *TopRegion = RI->getTopLevelRegion();
555
556 if(!isValidFunction(F))
557 return false;
558
559 findScops(*TopRegion);
560 return false;
561}
562
563
564void polly::ScopDetection::verifyRegion(const Region &R) const {
565 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
566 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
567 isValidRegion(Context);
568}
569
570void polly::ScopDetection::verifyAnalysis() const {
571 for (RegionSet::const_iterator I = ValidRegions.begin(),
572 E = ValidRegions.end(); I != E; ++I)
573 verifyRegion(**I);
574}
575
576void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
577 AU.addRequired<DominatorTree>();
578 AU.addRequired<PostDominatorTree>();
579 AU.addRequired<LoopInfo>();
580 AU.addRequired<ScalarEvolution>();
581 // We also need AA and RegionInfo when we are verifying analysis.
582 AU.addRequiredTransitive<AliasAnalysis>();
583 AU.addRequiredTransitive<RegionInfo>();
584 AU.setPreservesAll();
585}
586
587void ScopDetection::print(raw_ostream &OS, const Module *) const {
588 for (RegionSet::const_iterator I = ValidRegions.begin(),
589 E = ValidRegions.end(); I != E; ++I)
590 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
591
592 OS << "\n";
593}
594
595void ScopDetection::releaseMemory() {
596 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000597 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000598 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000599}
600
601char ScopDetection::ID = 0;
602
Tobias Grosser73600b82011-10-08 00:30:40 +0000603INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
604 "Polly - Detect static control parts (SCoPs)", false,
605 false)
606INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
607INITIALIZE_PASS_DEPENDENCY(DominatorTree)
608INITIALIZE_PASS_DEPENDENCY(LoopInfo)
609INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
610INITIALIZE_PASS_DEPENDENCY(RegionInfo)
611INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
612INITIALIZE_PASS_END(ScopDetection, "polly-detect",
613 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000614
Tobias Grosser83f5c432011-08-23 22:35:08 +0000615Pass *polly::createScopDetectionPass() {
616 return new ScopDetection();
617}