blob: 17f43ce5fd24aaf19e89ad85e31c88e2eb9c44f3 [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
173 const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0));
174 const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1));
175
Tobias Grosser120db6b2011-11-07 12:58:54 +0000176 bool affineLHS = isAffineExpr(&Context.CurRegion, ScevLHS, *SE);
177 bool affineRHS = isAffineExpr(&Context.CurRegion, ScevRHS, *SE);
Tobias Grosser75805372011-04-29 06:27:02 +0000178
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000179 if (!affineLHS || !affineRHS)
180 INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000181 }
182
183 // Allow loop exit conditions.
184 Loop *L = LI->getLoopFor(&BB);
185 if (L && L->getExitingBlock() == &BB)
186 return true;
187
188 // Allow perfectly nested conditions.
189 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000190 if (R->getEntry() != &BB)
191 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000192
193 return true;
194}
195
196bool ScopDetection::isValidCallInst(CallInst &CI) {
197 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
198 return false;
199
200 if (CI.doesNotAccessMemory())
201 return true;
202
203 Function *CalledFunction = CI.getCalledFunction();
204
205 // Indirect calls are not supported.
206 if (CalledFunction == 0)
207 return false;
208
209 // TODO: Intrinsics.
210 return false;
211}
212
213bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
214 DetectionContext &Context) const {
215 Value *Ptr = getPointerOperand(Inst), *BasePtr;
216 const SCEV *AccessFunction = SE->getSCEV(Ptr);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000217 const SCEVUnknown *BasePointer;
218 const Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000219
Tobias Grosserb8710b52011-11-10 12:44:50 +0000220 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
221
222 if (!BasePointer)
223 INVALID(AffFunc, "No base pointer");
224
225 BaseValue = BasePointer->getValue();
226
227 if (isa<UndefValue>(BaseValue))
228 INVALID(AffFunc, "Undefined base pointer");
229
230 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
231
232 if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000233 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000234
Tobias Grosserb8710b52011-11-10 12:44:50 +0000235 BasePtr = BasePointer->getValue();
236
Tobias Grosser75805372011-04-29 06:27:02 +0000237 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
238 // created by IndependentBlocks Pass.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000239 if (isa<IntToPtrInst>(BasePtr))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000240 INVALID(Other, "Find bad intToptr prt: " << *BasePtr);
Tobias Grosser75805372011-04-29 06:27:02 +0000241
242 // Check if the base pointer of the memory access does alias with
243 // any other pointer. This cannot be handled at the moment.
244 AliasSet &AS =
245 Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize,
246 Inst.getMetadata(LLVMContext::MD_tbaa));
247 if (!AS.isMustAlias()) {
248 DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS);
249
250 // STATSCOP triggers an assertion if we are in verifying mode.
251 // This is generally good to check that we do not change the SCoP after we
252 // run the SCoP detection and consequently to ensure that we can still
253 // represent that SCoP. However, in case of aliasing this does not work.
254 // The independent blocks pass may create memory references which seem to
255 // alias, if -basicaa is not available. They actually do not. As we do not
256 // not know this and we would fail here if we verify it.
257 if (!Context.Verifying) {
258 STATSCOP(Alias);
259 }
260
261 return false;
262 }
263
264 return true;
265}
266
267
268bool ScopDetection::hasScalarDependency(Instruction &Inst,
269 Region &RefRegion) const {
270 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
271 UI != UE; ++UI)
272 if (Instruction *Use = dyn_cast<Instruction>(*UI))
273 if (!RefRegion.contains(Use->getParent())) {
274 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
275 // PHINode is induction variable, the scalar to array transform may
276 // break it and introduce a non-indvar PHINode, which is not allow in
277 // Scop.
278 // This can be fix by:
279 // Introduce a IndependentBlockPrepare pass, which translate all
280 // PHINodes not in Scop to array.
281 // The IndependentBlockPrepare pass can also split the entry block of
282 // the function to hold the alloca instruction created by scalar to
283 // array. and split the exit block of the Scop so the new create load
284 // instruction for escape users will not break other Scops.
285 if (isa<PHINode>(Use))
286 return true;
287 }
288
289 return false;
290}
291
292bool ScopDetection::isValidInstruction(Instruction &Inst,
293 DetectionContext &Context) const {
294 // Only canonical IVs are allowed.
295 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000296 if (!isIndVar(PN, LI))
297 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000298
299 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000300 if (hasScalarDependency(Inst, Context.CurRegion))
301 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000302
303 // We only check the call instruction but not invoke instruction.
304 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
305 if (isValidCallInst(*CI))
306 return true;
307
Tobias Grosserb43ba822011-10-08 00:49:30 +0000308 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000309 }
310
311 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
312 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000313 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000314 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000315
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000316 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000317 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000318
319 return true;
320 }
321
322 // Check the access function.
323 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
324 return isValidMemoryAccess(Inst, Context);
325
326 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000327 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000328}
329
330bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
331 DetectionContext &Context) const {
332 if (!isValidCFG(BB, Context))
333 return false;
334
335 // Check all instructions, except the terminator instruction.
336 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
337 if (!isValidInstruction(*I, Context))
338 return false;
339
340 Loop *L = LI->getLoopFor(&BB);
341 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
342 return false;
343
344 return true;
345}
346
347bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
348 PHINode *IndVar = L->getCanonicalInductionVariable();
349 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000350 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000351 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000352 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000353
354 // Is the loop count affine?
355 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000356 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
Tobias Grosserbd54f322011-10-26 01:27:49 +0000357 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000358 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000359
360 return true;
361}
362
363Region *ScopDetection::expandRegion(Region &R) {
364 Region *CurrentRegion = &R;
365 Region *TmpRegion = R.getExpandedRegion();
366
367 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
368
369 while (TmpRegion) {
370 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
371 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
372
373 if (!allBlocksValid(Context))
374 break;
375
376 if (isValidExit(Context)) {
377 if (CurrentRegion != &R)
378 delete CurrentRegion;
379
380 CurrentRegion = TmpRegion;
381 }
382
383 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
384
385 if (TmpRegion != &R && TmpRegion != CurrentRegion)
386 delete TmpRegion;
387
388 TmpRegion = TmpRegion2;
389 }
390
391 if (&R == CurrentRegion)
392 return NULL;
393
394 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
395
396 return CurrentRegion;
397}
398
399
400void ScopDetection::findScops(Region &R) {
401 DetectionContext Context(R, *AA, false /*verifying*/);
402
403 if (isValidRegion(Context)) {
404 ++ValidRegion;
405 ValidRegions.insert(&R);
406 return;
407 }
408
Tobias Grosser4f129a62011-10-08 00:30:55 +0000409 InvalidRegions[&R] = LastFailure;
410
Tobias Grosser75805372011-04-29 06:27:02 +0000411 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
412 findScops(**I);
413
414 // Try to expand regions.
415 //
416 // As the region tree normally only contains canonical regions, non canonical
417 // regions that form a Scop are not found. Therefore, those non canonical
418 // regions are checked by expanding the canonical ones.
419
420 std::vector<Region*> ToExpand;
421
422 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
423 ToExpand.push_back(*I);
424
425 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
426 RE = ToExpand.end(); RI != RE; ++RI) {
427 Region *CurrentRegion = *RI;
428
429 // Skip invalid regions. Regions may become invalid, if they are element of
430 // an already expanded region.
431 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
432 continue;
433
434 Region *ExpandedR = expandRegion(*CurrentRegion);
435
436 if (!ExpandedR)
437 continue;
438
439 R.addSubRegion(ExpandedR, true);
440 ValidRegions.insert(ExpandedR);
441 ValidRegions.erase(CurrentRegion);
442
443 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
444 ++I)
445 ValidRegions.erase(*I);
446 }
447}
448
449bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
450 Region &R = Context.CurRegion;
451
452 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
453 ++I)
454 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
455 return false;
456
457 return true;
458}
459
460bool ScopDetection::isValidExit(DetectionContext &Context) const {
461 Region &R = Context.CurRegion;
462
463 // PHI nodes are not allowed in the exit basic block.
464 if (BasicBlock *Exit = R.getExit()) {
465 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000466 if (I != Exit->end() && isa<PHINode> (*I))
467 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000468 }
469
470 return true;
471}
472
473bool ScopDetection::isValidRegion(DetectionContext &Context) const {
474 Region &R = Context.CurRegion;
475
476 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
477
478 // The toplevel region is no valid region.
479 if (!R.getParent()) {
480 DEBUG(dbgs() << "Top level region is invalid";
481 dbgs() << "\n");
482 return false;
483 }
484
485 // SCoP can not contains the entry block of the function, because we need
486 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000487 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
488 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000489
490 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000491 if (!R.isSimple())
492 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000493
494 if (!allBlocksValid(Context))
495 return false;
496
497 if (!isValidExit(Context))
498 return false;
499
500 DEBUG(dbgs() << "OK\n");
501 return true;
502}
503
504bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000505 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000506}
507
508bool ScopDetection::runOnFunction(llvm::Function &F) {
509 AA = &getAnalysis<AliasAnalysis>();
510 SE = &getAnalysis<ScalarEvolution>();
511 LI = &getAnalysis<LoopInfo>();
512 RI = &getAnalysis<RegionInfo>();
513 Region *TopRegion = RI->getTopLevelRegion();
514
Tobias Grosser2ff87232011-10-23 11:17:06 +0000515 releaseMemory();
516
517 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
518 return false;
519
Tobias Grosser75805372011-04-29 06:27:02 +0000520 if(!isValidFunction(F))
521 return false;
522
523 findScops(*TopRegion);
524 return false;
525}
526
527
528void polly::ScopDetection::verifyRegion(const Region &R) const {
529 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
530 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
531 isValidRegion(Context);
532}
533
534void polly::ScopDetection::verifyAnalysis() const {
535 for (RegionSet::const_iterator I = ValidRegions.begin(),
536 E = ValidRegions.end(); I != E; ++I)
537 verifyRegion(**I);
538}
539
540void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
541 AU.addRequired<DominatorTree>();
542 AU.addRequired<PostDominatorTree>();
543 AU.addRequired<LoopInfo>();
544 AU.addRequired<ScalarEvolution>();
545 // We also need AA and RegionInfo when we are verifying analysis.
546 AU.addRequiredTransitive<AliasAnalysis>();
547 AU.addRequiredTransitive<RegionInfo>();
548 AU.setPreservesAll();
549}
550
551void ScopDetection::print(raw_ostream &OS, const Module *) const {
552 for (RegionSet::const_iterator I = ValidRegions.begin(),
553 E = ValidRegions.end(); I != E; ++I)
554 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
555
556 OS << "\n";
557}
558
559void ScopDetection::releaseMemory() {
560 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000561 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000562 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000563}
564
565char ScopDetection::ID = 0;
566
Tobias Grosser73600b82011-10-08 00:30:40 +0000567INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
568 "Polly - Detect static control parts (SCoPs)", false,
569 false)
570INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
571INITIALIZE_PASS_DEPENDENCY(DominatorTree)
572INITIALIZE_PASS_DEPENDENCY(LoopInfo)
573INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
574INITIALIZE_PASS_DEPENDENCY(RegionInfo)
575INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
576INITIALIZE_PASS_END(ScopDetection, "polly-detect",
577 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000578
Tobias Grosser83f5c432011-08-23 22:35:08 +0000579Pass *polly::createScopDetectionPass() {
580 return new ScopDetection();
581}