blob: ab9720c67416585b0dbc5c277d2a3177a8a0407a [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
Tobias Grosser2ff87232011-10-23 11:17:06 +000066static cl::opt<std::string>
67OnlyFunction("polly-detect-only",
68 cl::desc("Only detect scops in function"), cl::Hidden,
69 cl::value_desc("The function name to detect scops in"),
70 cl::ValueRequired, cl::init(""));
71
72
Tobias Grosser75805372011-04-29 06:27:02 +000073//===----------------------------------------------------------------------===//
74// Statistics.
75
76STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
77
78#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
79 "Number of bad regions for Scop: "\
80 DESC)
81
82#define STATSCOP(NAME); assert(!Context.Verifying && #NAME); \
83 if (!Context.Verifying) ++Bad##NAME##ForScop;
84
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000085#define INVALID(NAME, MESSAGE) \
86 do { \
Tobias Grosser4f129a62011-10-08 00:30:55 +000087 std::string Buf; \
88 raw_string_ostream fmt(Buf); \
89 fmt << MESSAGE; \
90 fmt.flush(); \
91 LastFailure = Buf; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000092 DEBUG(dbgs() << MESSAGE); \
93 DEBUG(dbgs() << "\n"); \
94 STATSCOP(NAME); \
95 return false; \
96 } while (0);
97
98
Tobias Grosser75805372011-04-29 06:27:02 +000099BADSCOP_STAT(CFG, "CFG too complex");
100BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
101BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
102BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
103BADSCOP_STAT(AffFunc, "Expression not affine");
104BADSCOP_STAT(Scalar, "Found scalar dependency");
105BADSCOP_STAT(Alias, "Found base address alias");
106BADSCOP_STAT(SimpleRegion, "Region not simple");
107BADSCOP_STAT(Other, "Others");
108
109//===----------------------------------------------------------------------===//
110// ScopDetection.
111
112bool ScopDetection::isMaxRegionInScop(const Region &R) const {
113 // The Region is valid only if it could be found in the set.
114 return ValidRegions.count(&R);
115}
116
Tobias Grosser4f129a62011-10-08 00:30:55 +0000117std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
118 if (!InvalidRegions.count(R))
119 return "";
120
121 return InvalidRegions.find(R)->second;
122}
123
124
Tobias Grosser75805372011-04-29 06:27:02 +0000125bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion,
126 Value **BasePtr) const {
127 assert(S && "S must not be null!");
128 bool isMemoryAccess = (BasePtr != 0);
129 if (isMemoryAccess) *BasePtr = 0;
130 DEBUG(dbgs() << "Checking " << *S << " ... ");
131
132 if (isa<SCEVCouldNotCompute>(S)) {
133 DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n");
134 return false;
135 }
136
137 for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E;
138 ++I) {
139 // The constant part must be a SCEVConstant.
140 // TODO: support sizeof in coefficient.
141 if (!isa<SCEVConstant>(I->second)) {
142 DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n");
143 return false;
144 }
145
146 const SCEV *Var = I->first;
147
148 // A constant offset is affine.
149 if(isa<SCEVConstant>(Var))
150 continue;
151
152 // Memory accesses are allowed to have a base pointer.
153 if (Var->getType()->isPointerTy()) {
154 if (!isMemoryAccess) {
155 DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n");
156 return false;
157 }
158
159 assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n");
160 const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var);
161
162 if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){
163 DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n");
164 return false;
165 }
166
167 // BaseAddr must be invariant in Scop.
168 if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) {
169 DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n");
170 return false;
171 }
172
173 assert(*BasePtr == 0 && "Found second base pointer.\n");
174 *BasePtr = BaseAddr->getValue();
175 continue;
176 }
177
178 if (isParameter(Var, RefRegion, *LI, *SE)
179 || isIndVar(Var, RefRegion, *LI, *SE))
180 continue;
181
182 DEBUG(dbgs() << "Non Affine: " ;
183 Var->print(dbgs());
184 dbgs() << " is neither parameter nor induction variable\n");
185 return false;
186 }
187
188 DEBUG(dbgs() << " is affine.\n");
189 return !isMemoryAccess || (*BasePtr != 0);
190}
191
192bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
193{
194 Region &RefRegion = Context.CurRegion;
195 TerminatorInst *TI = BB.getTerminator();
196
197 // Return instructions are only valid if the region is the top level region.
198 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
199 return true;
200
201 BranchInst *Br = dyn_cast<BranchInst>(TI);
202
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000203 if (!Br)
204 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000205
206 if (Br->isUnconditional()) return true;
207
208 Value *Condition = Br->getCondition();
209
210 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000211 if (isa<UndefValue>(Condition))
212 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
213 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000214
215 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000216 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
217 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
218 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000219
220 // Allow perfectly nested conditions.
221 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
222
223 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
224 // Unsigned comparisons are not allowed. They trigger overflow problems
225 // in the code generation.
226 //
227 // TODO: This is not sufficient and just hides bugs. However it does pretty
228 // well.
229 if(ICmp->isUnsigned())
230 return false;
231
232 // Are both operands of the ICmp affine?
233 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000234 || isa<UndefValue>(ICmp->getOperand(1)))
235 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000236
237 const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0));
238 const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1));
239
240 bool affineLHS = isValidAffineFunction(ScevLHS, RefRegion);
241 bool affineRHS = isValidAffineFunction(ScevRHS, RefRegion);
242
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000243 if (!affineLHS || !affineRHS)
244 INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000245 }
246
247 // Allow loop exit conditions.
248 Loop *L = LI->getLoopFor(&BB);
249 if (L && L->getExitingBlock() == &BB)
250 return true;
251
252 // Allow perfectly nested conditions.
253 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000254 if (R->getEntry() != &BB)
255 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000256
257 return true;
258}
259
260bool ScopDetection::isValidCallInst(CallInst &CI) {
261 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
262 return false;
263
264 if (CI.doesNotAccessMemory())
265 return true;
266
267 Function *CalledFunction = CI.getCalledFunction();
268
269 // Indirect calls are not supported.
270 if (CalledFunction == 0)
271 return false;
272
273 // TODO: Intrinsics.
274 return false;
275}
276
277bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
278 DetectionContext &Context) const {
279 Value *Ptr = getPointerOperand(Inst), *BasePtr;
280 const SCEV *AccessFunction = SE->getSCEV(Ptr);
281
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000282 if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr))
283 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000284
285 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
286 // created by IndependentBlocks Pass.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000287 if (isa<IntToPtrInst>(BasePtr))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000288 INVALID(Other, "Find bad intToptr prt: " << *BasePtr);
Tobias Grosser75805372011-04-29 06:27:02 +0000289
290 // Check if the base pointer of the memory access does alias with
291 // any other pointer. This cannot be handled at the moment.
292 AliasSet &AS =
293 Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize,
294 Inst.getMetadata(LLVMContext::MD_tbaa));
295 if (!AS.isMustAlias()) {
296 DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS);
297
298 // STATSCOP triggers an assertion if we are in verifying mode.
299 // This is generally good to check that we do not change the SCoP after we
300 // run the SCoP detection and consequently to ensure that we can still
301 // represent that SCoP. However, in case of aliasing this does not work.
302 // The independent blocks pass may create memory references which seem to
303 // alias, if -basicaa is not available. They actually do not. As we do not
304 // not know this and we would fail here if we verify it.
305 if (!Context.Verifying) {
306 STATSCOP(Alias);
307 }
308
309 return false;
310 }
311
312 return true;
313}
314
315
316bool ScopDetection::hasScalarDependency(Instruction &Inst,
317 Region &RefRegion) const {
318 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
319 UI != UE; ++UI)
320 if (Instruction *Use = dyn_cast<Instruction>(*UI))
321 if (!RefRegion.contains(Use->getParent())) {
322 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
323 // PHINode is induction variable, the scalar to array transform may
324 // break it and introduce a non-indvar PHINode, which is not allow in
325 // Scop.
326 // This can be fix by:
327 // Introduce a IndependentBlockPrepare pass, which translate all
328 // PHINodes not in Scop to array.
329 // The IndependentBlockPrepare pass can also split the entry block of
330 // the function to hold the alloca instruction created by scalar to
331 // array. and split the exit block of the Scop so the new create load
332 // instruction for escape users will not break other Scops.
333 if (isa<PHINode>(Use))
334 return true;
335 }
336
337 return false;
338}
339
340bool ScopDetection::isValidInstruction(Instruction &Inst,
341 DetectionContext &Context) const {
342 // Only canonical IVs are allowed.
343 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000344 if (!isIndVar(PN, LI))
345 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000346
347 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000348 if (hasScalarDependency(Inst, Context.CurRegion))
349 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000350
351 // We only check the call instruction but not invoke instruction.
352 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
353 if (isValidCallInst(*CI))
354 return true;
355
Tobias Grosserb43ba822011-10-08 00:49:30 +0000356 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000357 }
358
359 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
360 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000361 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000362 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000363
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000364 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000365 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000366
367 return true;
368 }
369
370 // Check the access function.
371 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
372 return isValidMemoryAccess(Inst, Context);
373
374 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000375 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000376}
377
378bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
379 DetectionContext &Context) const {
380 if (!isValidCFG(BB, Context))
381 return false;
382
383 // Check all instructions, except the terminator instruction.
384 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
385 if (!isValidInstruction(*I, Context))
386 return false;
387
388 Loop *L = LI->getLoopFor(&BB);
389 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
390 return false;
391
392 return true;
393}
394
395bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
396 PHINode *IndVar = L->getCanonicalInductionVariable();
397 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000398 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000399 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000400 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000401
402 // Is the loop count affine?
403 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000404 if (!isValidAffineFunction(LoopCount, Context.CurRegion))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000405 INVALID(LoopBound, "Non affine loop bound '" << LoopCount << "'for loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000406 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000407
408 return true;
409}
410
411Region *ScopDetection::expandRegion(Region &R) {
412 Region *CurrentRegion = &R;
413 Region *TmpRegion = R.getExpandedRegion();
414
415 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
416
417 while (TmpRegion) {
418 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
419 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
420
421 if (!allBlocksValid(Context))
422 break;
423
424 if (isValidExit(Context)) {
425 if (CurrentRegion != &R)
426 delete CurrentRegion;
427
428 CurrentRegion = TmpRegion;
429 }
430
431 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
432
433 if (TmpRegion != &R && TmpRegion != CurrentRegion)
434 delete TmpRegion;
435
436 TmpRegion = TmpRegion2;
437 }
438
439 if (&R == CurrentRegion)
440 return NULL;
441
442 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
443
444 return CurrentRegion;
445}
446
447
448void ScopDetection::findScops(Region &R) {
449 DetectionContext Context(R, *AA, false /*verifying*/);
450
451 if (isValidRegion(Context)) {
452 ++ValidRegion;
453 ValidRegions.insert(&R);
454 return;
455 }
456
Tobias Grosser4f129a62011-10-08 00:30:55 +0000457 InvalidRegions[&R] = LastFailure;
458
Tobias Grosser75805372011-04-29 06:27:02 +0000459 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
460 findScops(**I);
461
462 // Try to expand regions.
463 //
464 // As the region tree normally only contains canonical regions, non canonical
465 // regions that form a Scop are not found. Therefore, those non canonical
466 // regions are checked by expanding the canonical ones.
467
468 std::vector<Region*> ToExpand;
469
470 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
471 ToExpand.push_back(*I);
472
473 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
474 RE = ToExpand.end(); RI != RE; ++RI) {
475 Region *CurrentRegion = *RI;
476
477 // Skip invalid regions. Regions may become invalid, if they are element of
478 // an already expanded region.
479 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
480 continue;
481
482 Region *ExpandedR = expandRegion(*CurrentRegion);
483
484 if (!ExpandedR)
485 continue;
486
487 R.addSubRegion(ExpandedR, true);
488 ValidRegions.insert(ExpandedR);
489 ValidRegions.erase(CurrentRegion);
490
491 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
492 ++I)
493 ValidRegions.erase(*I);
494 }
495}
496
497bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
498 Region &R = Context.CurRegion;
499
500 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
501 ++I)
502 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
503 return false;
504
505 return true;
506}
507
508bool ScopDetection::isValidExit(DetectionContext &Context) const {
509 Region &R = Context.CurRegion;
510
511 // PHI nodes are not allowed in the exit basic block.
512 if (BasicBlock *Exit = R.getExit()) {
513 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000514 if (I != Exit->end() && isa<PHINode> (*I))
515 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000516 }
517
518 return true;
519}
520
521bool ScopDetection::isValidRegion(DetectionContext &Context) const {
522 Region &R = Context.CurRegion;
523
524 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
525
526 // The toplevel region is no valid region.
527 if (!R.getParent()) {
528 DEBUG(dbgs() << "Top level region is invalid";
529 dbgs() << "\n");
530 return false;
531 }
532
533 // SCoP can not contains the entry block of the function, because we need
534 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000535 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
536 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000537
538 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000539 if (!R.isSimple())
540 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000541
542 if (!allBlocksValid(Context))
543 return false;
544
545 if (!isValidExit(Context))
546 return false;
547
548 DEBUG(dbgs() << "OK\n");
549 return true;
550}
551
552bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000553 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000554}
555
556bool ScopDetection::runOnFunction(llvm::Function &F) {
557 AA = &getAnalysis<AliasAnalysis>();
558 SE = &getAnalysis<ScalarEvolution>();
559 LI = &getAnalysis<LoopInfo>();
560 RI = &getAnalysis<RegionInfo>();
561 Region *TopRegion = RI->getTopLevelRegion();
562
Tobias Grosser2ff87232011-10-23 11:17:06 +0000563 releaseMemory();
564
565 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
566 return false;
567
Tobias Grosser75805372011-04-29 06:27:02 +0000568 if(!isValidFunction(F))
569 return false;
570
571 findScops(*TopRegion);
572 return false;
573}
574
575
576void polly::ScopDetection::verifyRegion(const Region &R) const {
577 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
578 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
579 isValidRegion(Context);
580}
581
582void polly::ScopDetection::verifyAnalysis() const {
583 for (RegionSet::const_iterator I = ValidRegions.begin(),
584 E = ValidRegions.end(); I != E; ++I)
585 verifyRegion(**I);
586}
587
588void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
589 AU.addRequired<DominatorTree>();
590 AU.addRequired<PostDominatorTree>();
591 AU.addRequired<LoopInfo>();
592 AU.addRequired<ScalarEvolution>();
593 // We also need AA and RegionInfo when we are verifying analysis.
594 AU.addRequiredTransitive<AliasAnalysis>();
595 AU.addRequiredTransitive<RegionInfo>();
596 AU.setPreservesAll();
597}
598
599void ScopDetection::print(raw_ostream &OS, const Module *) const {
600 for (RegionSet::const_iterator I = ValidRegions.begin(),
601 E = ValidRegions.end(); I != E; ++I)
602 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
603
604 OS << "\n";
605}
606
607void ScopDetection::releaseMemory() {
608 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000609 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000610 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000611}
612
613char ScopDetection::ID = 0;
614
Tobias Grosser73600b82011-10-08 00:30:40 +0000615INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
616 "Polly - Detect static control parts (SCoPs)", false,
617 false)
618INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
619INITIALIZE_PASS_DEPENDENCY(DominatorTree)
620INITIALIZE_PASS_DEPENDENCY(LoopInfo)
621INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
622INITIALIZE_PASS_DEPENDENCY(RegionInfo)
623INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
624INITIALIZE_PASS_END(ScopDetection, "polly-detect",
625 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000626
Tobias Grosser83f5c432011-08-23 22:35:08 +0000627Pass *polly::createScopDetectionPass() {
628 return new ScopDetection();
629}