blob: 1c92702f74e2fffc883bd230c1b204fbca4e0c6a [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
77
Tobias Grosser75805372011-04-29 06:27:02 +000078//===----------------------------------------------------------------------===//
79// Statistics.
80
81STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
82
83#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
84 "Number of bad regions for Scop: "\
85 DESC)
86
87#define STATSCOP(NAME); assert(!Context.Verifying && #NAME); \
88 if (!Context.Verifying) ++Bad##NAME##ForScop;
89
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000090#define INVALID(NAME, MESSAGE) \
91 do { \
Tobias Grosser4f129a62011-10-08 00:30:55 +000092 std::string Buf; \
93 raw_string_ostream fmt(Buf); \
94 fmt << MESSAGE; \
95 fmt.flush(); \
96 LastFailure = Buf; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000097 DEBUG(dbgs() << MESSAGE); \
98 DEBUG(dbgs() << "\n"); \
99 STATSCOP(NAME); \
100 return false; \
101 } while (0);
102
103
Tobias Grosser75805372011-04-29 06:27:02 +0000104BADSCOP_STAT(CFG, "CFG too complex");
105BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
106BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
107BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
108BADSCOP_STAT(AffFunc, "Expression not affine");
109BADSCOP_STAT(Scalar, "Found scalar dependency");
110BADSCOP_STAT(Alias, "Found base address alias");
111BADSCOP_STAT(SimpleRegion, "Region not simple");
112BADSCOP_STAT(Other, "Others");
113
114//===----------------------------------------------------------------------===//
115// ScopDetection.
Tobias Grosser75805372011-04-29 06:27:02 +0000116bool ScopDetection::isMaxRegionInScop(const Region &R) const {
117 // The Region is valid only if it could be found in the set.
118 return ValidRegions.count(&R);
119}
120
Tobias Grosser4f129a62011-10-08 00:30:55 +0000121std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
122 if (!InvalidRegions.count(R))
123 return "";
124
125 return InvalidRegions.find(R)->second;
126}
127
Tobias Grosser75805372011-04-29 06:27:02 +0000128bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
129{
130 Region &RefRegion = Context.CurRegion;
131 TerminatorInst *TI = BB.getTerminator();
132
133 // Return instructions are only valid if the region is the top level region.
134 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
135 return true;
136
137 BranchInst *Br = dyn_cast<BranchInst>(TI);
138
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000139 if (!Br)
140 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000141
142 if (Br->isUnconditional()) return true;
143
144 Value *Condition = Br->getCondition();
145
146 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000147 if (isa<UndefValue>(Condition))
148 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
149 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000150
151 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000152 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
153 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
154 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000155
156 // Allow perfectly nested conditions.
157 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
158
159 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
160 // Unsigned comparisons are not allowed. They trigger overflow problems
161 // in the code generation.
162 //
163 // TODO: This is not sufficient and just hides bugs. However it does pretty
164 // well.
165 if(ICmp->isUnsigned())
166 return false;
167
168 // Are both operands of the ICmp affine?
169 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000170 || isa<UndefValue>(ICmp->getOperand(1)))
171 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000172
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000173 const SCEV *LHS = SE->getSCEV(ICmp->getOperand(0));
174 const SCEV *RHS = SE->getSCEV(ICmp->getOperand(1));
Tobias Grosser75805372011-04-29 06:27:02 +0000175
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000176 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
177 !isAffineExpr(&Context.CurRegion, RHS, *SE))
178 INVALID(AffFunc, "Non affine branch in BB '" << BB.getNameStr()
179 << "' with LHS: " << *LHS << " and RHS: " << *RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000180 }
181
182 // Allow loop exit conditions.
183 Loop *L = LI->getLoopFor(&BB);
184 if (L && L->getExitingBlock() == &BB)
185 return true;
186
187 // Allow perfectly nested conditions.
188 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000189 if (R->getEntry() != &BB)
190 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000191
192 return true;
193}
194
195bool ScopDetection::isValidCallInst(CallInst &CI) {
196 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
197 return false;
198
199 if (CI.doesNotAccessMemory())
200 return true;
201
202 Function *CalledFunction = CI.getCalledFunction();
203
204 // Indirect calls are not supported.
205 if (CalledFunction == 0)
206 return false;
207
208 // TODO: Intrinsics.
209 return false;
210}
211
212bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
213 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000214 Value *Ptr = getPointerOperand(Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000215 const SCEV *AccessFunction = SE->getSCEV(Ptr);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000216 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000217 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000218
Tobias Grosserb8710b52011-11-10 12:44:50 +0000219 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
220
221 if (!BasePointer)
222 INVALID(AffFunc, "No base pointer");
223
224 BaseValue = BasePointer->getValue();
225
226 if (isa<UndefValue>(BaseValue))
227 INVALID(AffFunc, "Undefined base pointer");
228
229 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
230
Tobias Grossere5e171e2011-11-10 12:45:03 +0000231 if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000232 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000233
234 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
235 // created by IndependentBlocks Pass.
Tobias Grossere5e171e2011-11-10 12:45:03 +0000236 if (isa<IntToPtrInst>(BaseValue))
237 INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000238
239 // Check if the base pointer of the memory access does alias with
240 // any other pointer. This cannot be handled at the moment.
241 AliasSet &AS =
Tobias Grossere5e171e2011-11-10 12:45:03 +0000242 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
Tobias Grosser75805372011-04-29 06:27:02 +0000243 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosser6f24d9d2011-11-10 12:47:21 +0000244 if (!AS.isMustAlias())
245 INVALID(Alias, "Possible aliasing found for value: " << *BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000246
247 return true;
248}
249
250
251bool ScopDetection::hasScalarDependency(Instruction &Inst,
252 Region &RefRegion) const {
253 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
254 UI != UE; ++UI)
255 if (Instruction *Use = dyn_cast<Instruction>(*UI))
256 if (!RefRegion.contains(Use->getParent())) {
257 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
258 // PHINode is induction variable, the scalar to array transform may
259 // break it and introduce a non-indvar PHINode, which is not allow in
260 // Scop.
261 // This can be fix by:
262 // Introduce a IndependentBlockPrepare pass, which translate all
263 // PHINodes not in Scop to array.
264 // The IndependentBlockPrepare pass can also split the entry block of
265 // the function to hold the alloca instruction created by scalar to
266 // array. and split the exit block of the Scop so the new create load
267 // instruction for escape users will not break other Scops.
268 if (isa<PHINode>(Use))
269 return true;
270 }
271
272 return false;
273}
274
275bool ScopDetection::isValidInstruction(Instruction &Inst,
276 DetectionContext &Context) const {
277 // Only canonical IVs are allowed.
278 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000279 if (!isIndVar(PN, LI))
280 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000281
282 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000283 if (hasScalarDependency(Inst, Context.CurRegion))
284 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000285
286 // We only check the call instruction but not invoke instruction.
287 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
288 if (isValidCallInst(*CI))
289 return true;
290
Tobias Grosserb43ba822011-10-08 00:49:30 +0000291 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000292 }
293
294 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
295 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000296 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000297 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000298
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000299 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000300 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000301
302 return true;
303 }
304
305 // Check the access function.
306 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
307 return isValidMemoryAccess(Inst, Context);
308
309 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000310 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000311}
312
313bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
314 DetectionContext &Context) const {
315 if (!isValidCFG(BB, Context))
316 return false;
317
318 // Check all instructions, except the terminator instruction.
319 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
320 if (!isValidInstruction(*I, Context))
321 return false;
322
323 Loop *L = LI->getLoopFor(&BB);
324 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
325 return false;
326
327 return true;
328}
329
330bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
331 PHINode *IndVar = L->getCanonicalInductionVariable();
332 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000333 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000334 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000335 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000336
337 // Is the loop count affine?
338 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000339 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
Tobias Grosserbd54f322011-10-26 01:27:49 +0000340 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000341 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000342
343 return true;
344}
345
346Region *ScopDetection::expandRegion(Region &R) {
347 Region *CurrentRegion = &R;
348 Region *TmpRegion = R.getExpandedRegion();
349
350 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
351
352 while (TmpRegion) {
353 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
354 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
355
356 if (!allBlocksValid(Context))
357 break;
358
359 if (isValidExit(Context)) {
360 if (CurrentRegion != &R)
361 delete CurrentRegion;
362
363 CurrentRegion = TmpRegion;
364 }
365
366 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
367
368 if (TmpRegion != &R && TmpRegion != CurrentRegion)
369 delete TmpRegion;
370
371 TmpRegion = TmpRegion2;
372 }
373
374 if (&R == CurrentRegion)
375 return NULL;
376
377 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
378
379 return CurrentRegion;
380}
381
382
383void ScopDetection::findScops(Region &R) {
384 DetectionContext Context(R, *AA, false /*verifying*/);
385
Tobias Grosser4eb73812011-11-10 12:45:15 +0000386 LastFailure = "";
387
Tobias Grosser75805372011-04-29 06:27:02 +0000388 if (isValidRegion(Context)) {
389 ++ValidRegion;
390 ValidRegions.insert(&R);
391 return;
392 }
393
Tobias Grosser4f129a62011-10-08 00:30:55 +0000394 InvalidRegions[&R] = LastFailure;
395
Tobias Grosser75805372011-04-29 06:27:02 +0000396 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
397 findScops(**I);
398
399 // Try to expand regions.
400 //
401 // As the region tree normally only contains canonical regions, non canonical
402 // regions that form a Scop are not found. Therefore, those non canonical
403 // regions are checked by expanding the canonical ones.
404
405 std::vector<Region*> ToExpand;
406
407 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
408 ToExpand.push_back(*I);
409
410 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
411 RE = ToExpand.end(); RI != RE; ++RI) {
412 Region *CurrentRegion = *RI;
413
414 // Skip invalid regions. Regions may become invalid, if they are element of
415 // an already expanded region.
416 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
417 continue;
418
419 Region *ExpandedR = expandRegion(*CurrentRegion);
420
421 if (!ExpandedR)
422 continue;
423
424 R.addSubRegion(ExpandedR, true);
425 ValidRegions.insert(ExpandedR);
426 ValidRegions.erase(CurrentRegion);
427
428 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
429 ++I)
430 ValidRegions.erase(*I);
431 }
432}
433
434bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
435 Region &R = Context.CurRegion;
436
437 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
438 ++I)
439 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
440 return false;
441
442 return true;
443}
444
445bool ScopDetection::isValidExit(DetectionContext &Context) const {
446 Region &R = Context.CurRegion;
447
448 // PHI nodes are not allowed in the exit basic block.
449 if (BasicBlock *Exit = R.getExit()) {
450 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000451 if (I != Exit->end() && isa<PHINode> (*I))
452 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000453 }
454
455 return true;
456}
457
458bool ScopDetection::isValidRegion(DetectionContext &Context) const {
459 Region &R = Context.CurRegion;
460
461 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
462
463 // The toplevel region is no valid region.
464 if (!R.getParent()) {
465 DEBUG(dbgs() << "Top level region is invalid";
466 dbgs() << "\n");
467 return false;
468 }
469
470 // SCoP can not contains the entry block of the function, because we need
471 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000472 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
473 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000474
475 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000476 if (!R.isSimple())
477 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000478
479 if (!allBlocksValid(Context))
480 return false;
481
482 if (!isValidExit(Context))
483 return false;
484
485 DEBUG(dbgs() << "OK\n");
486 return true;
487}
488
489bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000490 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000491}
492
493bool ScopDetection::runOnFunction(llvm::Function &F) {
494 AA = &getAnalysis<AliasAnalysis>();
495 SE = &getAnalysis<ScalarEvolution>();
496 LI = &getAnalysis<LoopInfo>();
497 RI = &getAnalysis<RegionInfo>();
498 Region *TopRegion = RI->getTopLevelRegion();
499
Tobias Grosser2ff87232011-10-23 11:17:06 +0000500 releaseMemory();
501
502 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
503 return false;
504
Tobias Grosser75805372011-04-29 06:27:02 +0000505 if(!isValidFunction(F))
506 return false;
507
508 findScops(*TopRegion);
509 return false;
510}
511
512
513void polly::ScopDetection::verifyRegion(const Region &R) const {
514 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
515 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
516 isValidRegion(Context);
517}
518
519void polly::ScopDetection::verifyAnalysis() const {
520 for (RegionSet::const_iterator I = ValidRegions.begin(),
521 E = ValidRegions.end(); I != E; ++I)
522 verifyRegion(**I);
523}
524
525void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
526 AU.addRequired<DominatorTree>();
527 AU.addRequired<PostDominatorTree>();
528 AU.addRequired<LoopInfo>();
529 AU.addRequired<ScalarEvolution>();
530 // We also need AA and RegionInfo when we are verifying analysis.
531 AU.addRequiredTransitive<AliasAnalysis>();
532 AU.addRequiredTransitive<RegionInfo>();
533 AU.setPreservesAll();
534}
535
536void ScopDetection::print(raw_ostream &OS, const Module *) const {
537 for (RegionSet::const_iterator I = ValidRegions.begin(),
538 E = ValidRegions.end(); I != E; ++I)
539 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
540
541 OS << "\n";
542}
543
544void ScopDetection::releaseMemory() {
545 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000546 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000547 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000548}
549
550char ScopDetection::ID = 0;
551
Tobias Grosser73600b82011-10-08 00:30:40 +0000552INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
553 "Polly - Detect static control parts (SCoPs)", false,
554 false)
555INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
556INITIALIZE_PASS_DEPENDENCY(DominatorTree)
557INITIALIZE_PASS_DEPENDENCY(LoopInfo)
558INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
559INITIALIZE_PASS_DEPENDENCY(RegionInfo)
560INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
561INITIALIZE_PASS_END(ScopDetection, "polly-detect",
562 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000563
Tobias Grosser83f5c432011-08-23 22:35:08 +0000564Pass *polly::createScopDetectionPass() {
565 return new ScopDetection();
566}