1 //===----- 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/CodeGen/BlockGenerators.h"
48 #include "polly/LinkAllPasses.h"
49 #include "polly/Options.h"
50 #include "polly/ScopDetection.h"
51 #include "polly/Support/SCEVValidator.h"
52 #include "polly/Support/ScopHelper.h"
53 #include "llvm/ADT/Statistic.h"
54 #include "llvm/Analysis/AliasAnalysis.h"
55 #include "llvm/Analysis/LoopInfo.h"
56 #include "llvm/Analysis/RegionIterator.h"
57 #include "llvm/Analysis/ScalarEvolution.h"
58 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
59 #include "llvm/DebugInfo.h"
60 #include "llvm/IR/LLVMContext.h"
61 #include "llvm/IR/DiagnosticInfo.h"
62 #include "llvm/IR/DiagnosticPrinter.h"
63 
64 #define DEBUG_TYPE "polly-detect"
65 #include "llvm/Support/Debug.h"
66 
67 #include <set>
68 
69 using namespace llvm;
70 using namespace polly;
71 
72 static cl::opt<bool>
73 DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops",
74                         cl::desc("Detect scops in functions without loops"),
75                         cl::Hidden, cl::init(false), cl::cat(PollyCategory));
76 
77 static cl::opt<bool>
78 DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
79                           cl::desc("Detect scops in regions without loops"),
80                           cl::Hidden, cl::init(false), cl::cat(PollyCategory));
81 
82 static cl::opt<std::string>
83 OnlyFunction("polly-only-func", cl::desc("Only run on a single function"),
84              cl::value_desc("function-name"), cl::ValueRequired, cl::init(""),
85              cl::cat(PollyCategory));
86 
87 static cl::opt<std::string>
88 OnlyRegion("polly-only-region",
89            cl::desc("Only run on certain regions (The provided identifier must "
90                     "appear in the name of the region's entry block"),
91            cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
92            cl::cat(PollyCategory));
93 
94 static cl::opt<bool>
95 IgnoreAliasing("polly-ignore-aliasing",
96                cl::desc("Ignore possible aliasing of the array bases"),
97                cl::Hidden, cl::init(false), cl::cat(PollyCategory));
98 
99 static cl::opt<bool>
100 ReportLevel("polly-report",
101             cl::desc("Print information about the activities of Polly"),
102             cl::init(false), cl::cat(PollyCategory));
103 
104 static cl::opt<bool>
105 AllowNonAffine("polly-allow-nonaffine",
106                cl::desc("Allow non affine access functions in arrays"),
107                cl::Hidden, cl::init(false), cl::cat(PollyCategory));
108 
109 static cl::opt<bool, true>
110 TrackFailures("polly-detect-track-failures",
111               cl::desc("Track failure strings in detecting scop regions"),
112               cl::location(PollyTrackFailures), cl::Hidden, cl::init(false),
113               cl::cat(PollyCategory));
114 
115 static cl::opt<bool>
116 VerifyScops("polly-detect-verify",
117             cl::desc("Verify the detected SCoPs after each transformation"),
118             cl::Hidden, cl::init(false), cl::cat(PollyCategory));
119 
120 bool polly::PollyTrackFailures = false;
121 
122 //===----------------------------------------------------------------------===//
123 // Statistics.
124 
125 STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
126 
127 #define BADSCOP_STAT(NAME, DESC)                                               \
128   STATISTIC(Bad##NAME##ForScop, "Number of bad regions for Scop: " DESC)
129 
130 #define INVALID(NAME, MESSAGE)                                                 \
131   do {                                                                         \
132     if (PollyTrackFailures) {                                                  \
133       std::string Buf;                                                         \
134       raw_string_ostream fmt(Buf);                                             \
135       fmt << MESSAGE;                                                          \
136       fmt.flush();                                                             \
137       LastFailure = Buf;                                                       \
138     }                                                                          \
139     DEBUG(dbgs() << MESSAGE);                                                  \
140     DEBUG(dbgs() << "\n");                                                     \
141     assert(!Context.Verifying && #NAME);                                       \
142     if (!Context.Verifying)                                                    \
143       ++Bad##NAME##ForScop;                                                    \
144   } while (0)
145 
146 #define INVALID_NOVERIFY(NAME, MESSAGE)                                        \
147   do {                                                                         \
148     if (PollyTrackFailures) {                                                  \
149       std::string Buf;                                                         \
150       raw_string_ostream fmt(Buf);                                             \
151       fmt << MESSAGE;                                                          \
152       fmt.flush();                                                             \
153       LastFailure = Buf;                                                       \
154     }                                                                          \
155     DEBUG(dbgs() << MESSAGE);                                                  \
156     DEBUG(dbgs() << "\n");                                                     \
157     /* DISABLED: assert(!Context.Verifying && #NAME); */                       \
158     if (!Context.Verifying)                                                    \
159       ++Bad##NAME##ForScop;                                                    \
160   } while (0)
161 
162 BADSCOP_STAT(CFG, "CFG too complex");
163 BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
164 BADSCOP_STAT(IndEdge, "Found invalid region entering edges");
165 BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
166 BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
167 BADSCOP_STAT(AffFunc, "Expression not affine");
168 BADSCOP_STAT(Alias, "Found base address alias");
169 BADSCOP_STAT(SimpleLoop, "Loop not in -loop-simplify form");
170 BADSCOP_STAT(Other, "Others");
171 
172 class DiagnosticScopFound : public DiagnosticInfo {
173 private:
174   static int PluginDiagnosticKind;
175 
176   Function &F;
177   std::string FileName;
178   unsigned EntryLine, ExitLine;
179 
180 public:
181   DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
182                       unsigned ExitLine)
183       : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
184         EntryLine(EntryLine), ExitLine(ExitLine) {}
185 
186   virtual void print(DiagnosticPrinter &DP) const;
187 
188   static bool classof(const DiagnosticInfo *DI) {
189     return DI->getKind() == PluginDiagnosticKind;
190   }
191 };
192 
193 int DiagnosticScopFound::PluginDiagnosticKind = 10;
194 
195 void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
196 
197   DP << "Polly detected an optimizable loop region (scop) in function '" << F
198      << "'\n";
199 
200   if (FileName.empty()) {
201     DP << "Scop location is unknown. Compile with debug info "
202           "(-g) to get more precise information. ";
203     return;
204   }
205 
206   DP << FileName << ":" << EntryLine << ": Start of scop\n";
207   DP << FileName << ":" << ExitLine << ": End of scop";
208 }
209 
210 //===----------------------------------------------------------------------===//
211 // ScopDetection.
212 bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
213   if (!ValidRegions.count(&R))
214     return false;
215 
216   if (Verify)
217     return isValidRegion(const_cast<Region &>(R));
218 
219   return true;
220 }
221 
222 std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
223   if (!InvalidRegions.count(R))
224     return "";
225 
226   return InvalidRegions.find(R)->second;
227 }
228 
229 bool ScopDetection::isValidCFG(BasicBlock &BB,
230                                DetectionContext &Context) const {
231   Region &RefRegion = Context.CurRegion;
232   TerminatorInst *TI = BB.getTerminator();
233 
234   // Return instructions are only valid if the region is the top level region.
235   if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
236     return true;
237 
238   BranchInst *Br = dyn_cast<BranchInst>(TI);
239 
240   if (!Br) {
241     INVALID(CFG, "Non branch instruction terminates BB: " + BB.getName());
242     return false;
243   }
244 
245   if (Br->isUnconditional())
246     return true;
247 
248   Value *Condition = Br->getCondition();
249 
250   // UndefValue is not allowed as condition.
251   if (isa<UndefValue>(Condition)) {
252     INVALID(AffFunc, "Condition based on 'undef' value in BB: " + BB.getName());
253     return false;
254   }
255 
256   // Only Constant and ICmpInst are allowed as condition.
257   if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) {
258     INVALID(AffFunc, "Condition in BB '" + BB.getName() +
259                          "' neither constant nor an icmp instruction");
260     return false;
261   }
262 
263   // Allow perfectly nested conditions.
264   assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
265 
266   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
267     // Unsigned comparisons are not allowed. They trigger overflow problems
268     // in the code generation.
269     //
270     // TODO: This is not sufficient and just hides bugs. However it does pretty
271     // well.
272     if (ICmp->isUnsigned())
273       return false;
274 
275     // Are both operands of the ICmp affine?
276     if (isa<UndefValue>(ICmp->getOperand(0)) ||
277         isa<UndefValue>(ICmp->getOperand(1))) {
278       INVALID(AffFunc, "undef operand in branch at BB: " + BB.getName());
279       return false;
280     }
281 
282     Loop *L = LI->getLoopFor(ICmp->getParent());
283     const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
284     const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
285 
286     if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
287         !isAffineExpr(&Context.CurRegion, RHS, *SE)) {
288       INVALID(AffFunc, "Non affine branch in BB '" << BB.getName()
289                                                    << "' with LHS: " << *LHS
290                                                    << " and RHS: " << *RHS);
291       return false;
292     }
293   }
294 
295   // Allow loop exit conditions.
296   Loop *L = LI->getLoopFor(&BB);
297   if (L && L->getExitingBlock() == &BB)
298     return true;
299 
300   // Allow perfectly nested conditions.
301   Region *R = RI->getRegionFor(&BB);
302   if (R->getEntry() != &BB) {
303     INVALID(CFG, "Not well structured condition at BB: " + BB.getName());
304     return false;
305   }
306 
307   return true;
308 }
309 
310 bool ScopDetection::isValidCallInst(CallInst &CI) {
311   if (CI.mayHaveSideEffects() || CI.doesNotReturn())
312     return false;
313 
314   if (CI.doesNotAccessMemory())
315     return true;
316 
317   Function *CalledFunction = CI.getCalledFunction();
318 
319   // Indirect calls are not supported.
320   if (CalledFunction == 0)
321     return false;
322 
323   // TODO: Intrinsics.
324   return false;
325 }
326 
327 std::string ScopDetection::formatInvalidAlias(AliasSet &AS) const {
328   std::string Message;
329   raw_string_ostream OS(Message);
330 
331   OS << "Possible aliasing: ";
332 
333   std::vector<Value *> Pointers;
334 
335   for (AliasSet::iterator AI = AS.begin(), AE = AS.end(); AI != AE; ++AI)
336     Pointers.push_back(AI.getPointer());
337 
338   std::sort(Pointers.begin(), Pointers.end());
339 
340   for (std::vector<Value *>::iterator PI = Pointers.begin(),
341                                       PE = Pointers.end();
342        ;) {
343     Value *V = *PI;
344 
345     if (V->getName().size() == 0)
346       OS << "\"" << *V << "\"";
347     else
348       OS << "\"" << V->getName() << "\"";
349 
350     ++PI;
351 
352     if (PI != PE)
353       OS << ", ";
354     else
355       break;
356   }
357 
358   return OS.str();
359 }
360 
361 bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
362   // A reference to function argument or constant value is invariant.
363   if (isa<Argument>(Val) || isa<Constant>(Val))
364     return true;
365 
366   const Instruction *I = dyn_cast<Instruction>(&Val);
367   if (!I)
368     return false;
369 
370   if (!Reg.contains(I))
371     return true;
372 
373   if (I->mayHaveSideEffects())
374     return false;
375 
376   // When Val is a Phi node, it is likely not invariant. We do not check whether
377   // Phi nodes are actually invariant, we assume that Phi nodes are usually not
378   // invariant. Recursively checking the operators of Phi nodes would lead to
379   // infinite recursion.
380   if (isa<PHINode>(*I))
381     return false;
382 
383   // Check that all operands of the instruction are
384   // themselves invariant.
385   const Instruction::const_op_iterator OE = I->op_end();
386   for (Instruction::const_op_iterator OI = I->op_begin(); OI != OE; ++OI) {
387     if (!isInvariant(**OI, Reg))
388       return false;
389   }
390 
391   // When the instruction is a load instruction, check that no write to memory
392   // in the region aliases with the load.
393   if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
394     AliasAnalysis::Location Loc = AA->getLocation(LI);
395     const Region::const_block_iterator BE = Reg.block_end();
396     // Check if any basic block in the region can modify the location pointed to
397     // by 'Loc'.  If so, 'Val' is (likely) not invariant in the region.
398     for (Region::const_block_iterator BI = Reg.block_begin(); BI != BE; ++BI) {
399       const BasicBlock &BB = **BI;
400       if (AA->canBasicBlockModify(BB, Loc))
401         return false;
402     }
403   }
404 
405   return true;
406 }
407 
408 bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
409                                         DetectionContext &Context) const {
410   Value *Ptr = getPointerOperand(Inst);
411   Loop *L = LI->getLoopFor(Inst.getParent());
412   const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
413   const SCEVUnknown *BasePointer;
414   Value *BaseValue;
415 
416   BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
417 
418   if (!BasePointer) {
419     INVALID(AffFunc, "No base pointer");
420     return false;
421   }
422 
423   BaseValue = BasePointer->getValue();
424 
425   if (isa<UndefValue>(BaseValue)) {
426     INVALID(AffFunc, "Undefined base pointer");
427     return false;
428   }
429 
430   // Check that the base address of the access is invariant in the current
431   // region.
432   if (!isInvariant(*BaseValue, Context.CurRegion)) {
433     // Verification of this property is difficult as the independent blocks
434     // pass may introduce aliasing that we did not have when running the
435     // scop detection.
436     INVALID_NOVERIFY(
437         AffFunc, "Base address not invariant in current region:" << *BaseValue);
438     return false;
439   }
440 
441   AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
442 
443   if (!AllowNonAffine &&
444       !isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue)) {
445     INVALID(AffFunc, "Non affine access function: " << *AccessFunction);
446     return false;
447   }
448 
449   // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
450   // created by IndependentBlocks Pass.
451   if (isa<IntToPtrInst>(BaseValue)) {
452     INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
453     return false;
454   }
455 
456   if (IgnoreAliasing)
457     return true;
458 
459   // Check if the base pointer of the memory access does alias with
460   // any other pointer. This cannot be handled at the moment.
461   AliasSet &AS =
462       Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
463                                         Inst.getMetadata(LLVMContext::MD_tbaa));
464 
465   // INVALID triggers an assertion in verifying mode, if it detects that a
466   // SCoP was detected by SCoP detection and that this SCoP was invalidated by
467   // a pass that stated it would preserve the SCoPs. We disable this check as
468   // the independent blocks pass may create memory references which seem to
469   // alias, if -basicaa is not available. They actually do not, but as we can
470   // not proof this without -basicaa we would fail. We disable this check to
471   // not cause irrelevant verification failures.
472   if (!AS.isMustAlias()) {
473     INVALID_NOVERIFY(Alias, formatInvalidAlias(AS));
474     return false;
475   }
476 
477   return true;
478 }
479 
480 bool ScopDetection::isValidInstruction(Instruction &Inst,
481                                        DetectionContext &Context) const {
482   if (PHINode *PN = dyn_cast<PHINode>(&Inst))
483     if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) {
484       if (SCEVCodegen) {
485         INVALID(IndVar,
486                 "SCEV of PHI node refers to SSA names in region: " << Inst);
487         return false;
488 
489       } else {
490         INVALID(IndVar, "Non canonical PHI node: " << Inst);
491         return false;
492       }
493     }
494 
495   // We only check the call instruction but not invoke instruction.
496   if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
497     if (isValidCallInst(*CI))
498       return true;
499 
500     INVALID(FuncCall, "Call instruction: " << Inst);
501     return false;
502   }
503 
504   if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
505     if (!isa<AllocaInst>(Inst))
506       return true;
507 
508     INVALID(Other, "Alloca instruction: " << Inst);
509     return false;
510   }
511 
512   // Check the access function.
513   if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
514     return isValidMemoryAccess(Inst, Context);
515 
516   // We do not know this instruction, therefore we assume it is invalid.
517   INVALID(Other, "Unknown instruction: " << Inst);
518   return false;
519 }
520 
521 bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
522   if (!SCEVCodegen) {
523     // If code generation is not in scev based mode, we need to ensure that
524     // each loop has a canonical induction variable.
525     PHINode *IndVar = L->getCanonicalInductionVariable();
526     if (!IndVar) {
527       INVALID(IndVar,
528               "No canonical IV at loop header: " << L->getHeader()->getName());
529       return false;
530     }
531   }
532 
533   // Is the loop count affine?
534   const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
535   if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) {
536     INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
537                                                  << L->getHeader()->getName());
538     return false;
539   }
540 
541   return true;
542 }
543 
544 Region *ScopDetection::expandRegion(Region &R) {
545   // Initial no valid region was found (greater than R)
546   Region *LastValidRegion = NULL;
547   Region *ExpandedRegion = R.getExpandedRegion();
548 
549   DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
550 
551   while (ExpandedRegion) {
552     DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
553     DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
554 
555     // Check the exit first (cheap)
556     if (isValidExit(Context)) {
557       // If the exit is valid check all blocks
558       //  - if true, a valid region was found => store it + keep expanding
559       //  - if false, .tbd. => stop  (should this really end the loop?)
560       if (!allBlocksValid(Context))
561         break;
562 
563       // Delete unnecessary regions (allocated by getExpandedRegion)
564       if (LastValidRegion)
565         delete LastValidRegion;
566 
567       // Store this region, because it is the greatest valid (encountered so
568       // far).
569       LastValidRegion = ExpandedRegion;
570 
571       // Create and test the next greater region (if any)
572       ExpandedRegion = ExpandedRegion->getExpandedRegion();
573 
574     } else {
575       // Create and test the next greater region (if any)
576       Region *TmpRegion = ExpandedRegion->getExpandedRegion();
577 
578       // Delete unnecessary regions (allocated by getExpandedRegion)
579       delete ExpandedRegion;
580 
581       ExpandedRegion = TmpRegion;
582     }
583   }
584 
585   DEBUG({
586     if (LastValidRegion)
587       dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
588     else
589       dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
590   });
591 
592   return LastValidRegion;
593 }
594 static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
595   for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
596        ++I)
597     if (R.contains(LI->getLoopFor(*I)))
598       return false;
599 
600   return true;
601 }
602 
603 // Remove all direct and indirect children of region R from the region set Regs,
604 // but do not recurse further if the first child has been found.
605 //
606 // Return the number of regions erased from Regs.
607 static unsigned eraseAllChildren(std::set<const Region *> &Regs,
608                                  const Region *R) {
609   unsigned Count = 0;
610   for (Region::const_iterator I = R->begin(), E = R->end(); I != E; ++I) {
611     if (Regs.find(*I) != Regs.end()) {
612       ++Count;
613       Regs.erase(*I);
614     } else {
615       Count += eraseAllChildren(Regs, *I);
616     }
617   }
618   return Count;
619 }
620 
621 void ScopDetection::findScops(Region &R) {
622 
623   if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
624     return;
625 
626   LastFailure = "";
627 
628   if (isValidRegion(R)) {
629     ++ValidRegion;
630     ValidRegions.insert(&R);
631     return;
632   }
633 
634   InvalidRegions[&R] = LastFailure;
635 
636   for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
637     findScops(**I);
638 
639   // Try to expand regions.
640   //
641   // As the region tree normally only contains canonical regions, non canonical
642   // regions that form a Scop are not found. Therefore, those non canonical
643   // regions are checked by expanding the canonical ones.
644 
645   std::vector<Region *> ToExpand;
646 
647   for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
648     ToExpand.push_back(*I);
649 
650   for (std::vector<Region *>::iterator RI = ToExpand.begin(),
651                                        RE = ToExpand.end();
652        RI != RE; ++RI) {
653     Region *CurrentRegion = *RI;
654 
655     // Skip invalid regions. Regions may become invalid, if they are element of
656     // an already expanded region.
657     if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
658       continue;
659 
660     Region *ExpandedR = expandRegion(*CurrentRegion);
661 
662     if (!ExpandedR)
663       continue;
664 
665     R.addSubRegion(ExpandedR, true);
666     ValidRegions.insert(ExpandedR);
667     ValidRegions.erase(CurrentRegion);
668 
669     // Erase all (direct and indirect) children of ExpandedR from the valid
670     // regions and update the number of valid regions.
671     ValidRegion -= eraseAllChildren(ValidRegions, ExpandedR);
672   }
673 }
674 
675 bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
676   Region &R = Context.CurRegion;
677 
678   for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
679        ++I) {
680     Loop *L = LI->getLoopFor(*I);
681     if (L && L->getHeader() == *I && !isValidLoop(L, Context))
682       return false;
683   }
684 
685   for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
686        ++I)
687     if (!isValidCFG(**I, Context))
688       return false;
689 
690   for (Region::block_iterator BI = R.block_begin(), E = R.block_end(); BI != E;
691        ++BI)
692     for (BasicBlock::iterator I = (*BI)->begin(), E = --(*BI)->end(); I != E;
693          ++I)
694       if (!isValidInstruction(*I, Context))
695         return false;
696 
697   return true;
698 }
699 
700 bool ScopDetection::isValidExit(DetectionContext &Context) const {
701   Region &R = Context.CurRegion;
702 
703   // PHI nodes are not allowed in the exit basic block.
704   if (BasicBlock *Exit = R.getExit()) {
705     BasicBlock::iterator I = Exit->begin();
706     if (I != Exit->end() && isa<PHINode>(*I)) {
707       INVALID(Other, "PHI node in exit BB");
708       return false;
709     }
710   }
711 
712   return true;
713 }
714 
715 bool ScopDetection::isValidRegion(Region &R) const {
716   DetectionContext Context(R, *AA, false /*verifying*/);
717   return isValidRegion(Context);
718 }
719 
720 bool ScopDetection::isValidRegion(DetectionContext &Context) const {
721   Region &R = Context.CurRegion;
722 
723   DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
724 
725   if (R.isTopLevelRegion()) {
726     DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n");
727     return false;
728   }
729 
730   if (!R.getEntry()->getName().count(OnlyRegion)) {
731     DEBUG({
732       dbgs() << "Region entry does not match -polly-region-only";
733       dbgs() << "\n";
734     });
735     return false;
736   }
737 
738   if (!R.getEnteringBlock()) {
739     BasicBlock *entry = R.getEntry();
740     Loop *L = LI->getLoopFor(entry);
741 
742     if (L) {
743       if (!L->isLoopSimplifyForm()) {
744         INVALID(SimpleLoop, "Loop not in simplify form is invalid!");
745         return false;
746       }
747 
748       for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
749            ++PI) {
750         // Region entering edges come from the same loop but outside the region
751         // are not allowed.
752         if (L->contains(*PI) && !R.contains(*PI)) {
753           INVALID(IndEdge, "Region has invalid entering edges!");
754           return false;
755         }
756       }
757     }
758   }
759 
760   // SCoP cannot contain the entry block of the function, because we need
761   // to insert alloca instruction there when translate scalar to array.
762   if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) {
763     INVALID(Other, "Region containing entry block of function is invalid!");
764     return false;
765   }
766 
767   if (!isValidExit(Context))
768     return false;
769 
770   if (!allBlocksValid(Context))
771     return false;
772 
773   DEBUG(dbgs() << "OK\n");
774   return true;
775 }
776 
777 bool ScopDetection::isValidFunction(llvm::Function &F) {
778   return !InvalidFunctions.count(&F);
779 }
780 
781 void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
782                                      unsigned &LineEnd, std::string &FileName) {
783   LineBegin = -1;
784   LineEnd = 0;
785 
786   for (Region::const_block_iterator RI = R->block_begin(), RE = R->block_end();
787        RI != RE; ++RI)
788     for (BasicBlock::iterator BI = (*RI)->begin(), BE = (*RI)->end(); BI != BE;
789          ++BI) {
790       DebugLoc DL = BI->getDebugLoc();
791       if (DL.isUnknown())
792         continue;
793 
794       DIScope Scope(DL.getScope(BI->getContext()));
795 
796       if (FileName.empty())
797         FileName = Scope.getFilename();
798 
799       unsigned NewLine = DL.getLine();
800 
801       LineBegin = std::min(LineBegin, NewLine);
802       LineEnd = std::max(LineEnd, NewLine);
803     }
804 }
805 
806 void ScopDetection::printLocations(llvm::Function &F) {
807   for (iterator RI = begin(), RE = end(); RI != RE; ++RI) {
808     unsigned LineEntry, LineExit;
809     std::string FileName;
810 
811     getDebugLocation(*RI, LineEntry, LineExit, FileName);
812     DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
813     F.getContext().diagnose(Diagnostic);
814   }
815 }
816 
817 bool ScopDetection::runOnFunction(llvm::Function &F) {
818   LI = &getAnalysis<LoopInfo>();
819   RI = &getAnalysis<RegionInfo>();
820   if (!DetectScopsWithoutLoops && LI->empty())
821     return false;
822 
823   AA = &getAnalysis<AliasAnalysis>();
824   SE = &getAnalysis<ScalarEvolution>();
825   Region *TopRegion = RI->getTopLevelRegion();
826 
827   releaseMemory();
828 
829   if (OnlyFunction != "" && F.getName() != OnlyFunction)
830     return false;
831 
832   if (!isValidFunction(F))
833     return false;
834 
835   findScops(*TopRegion);
836 
837   if (ReportLevel >= 1)
838     printLocations(F);
839 
840   return false;
841 }
842 
843 void polly::ScopDetection::verifyRegion(const Region &R) const {
844   assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
845   DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
846   isValidRegion(Context);
847 }
848 
849 void polly::ScopDetection::verifyAnalysis() const {
850   if (!VerifyScops)
851     return;
852 
853   for (RegionSet::const_iterator I = ValidRegions.begin(),
854                                  E = ValidRegions.end();
855        I != E; ++I)
856     verifyRegion(**I);
857 }
858 
859 void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
860   AU.addRequired<DominatorTreeWrapperPass>();
861   AU.addRequired<PostDominatorTree>();
862   AU.addRequired<LoopInfo>();
863   AU.addRequired<ScalarEvolution>();
864   // We also need AA and RegionInfo when we are verifying analysis.
865   AU.addRequiredTransitive<AliasAnalysis>();
866   AU.addRequiredTransitive<RegionInfo>();
867   AU.setPreservesAll();
868 }
869 
870 void ScopDetection::print(raw_ostream &OS, const Module *) const {
871   for (RegionSet::const_iterator I = ValidRegions.begin(),
872                                  E = ValidRegions.end();
873        I != E; ++I)
874     OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
875 
876   OS << "\n";
877 }
878 
879 void ScopDetection::releaseMemory() {
880   ValidRegions.clear();
881   InvalidRegions.clear();
882   // Do not clear the invalid function set.
883 }
884 
885 char ScopDetection::ID = 0;
886 
887 Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
888 
889 INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
890                       "Polly - Detect static control parts (SCoPs)", false,
891                       false);
892 INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
893 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
894 INITIALIZE_PASS_DEPENDENCY(LoopInfo);
895 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
896 INITIALIZE_PASS_DEPENDENCY(RegionInfo);
897 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
898 INITIALIZE_PASS_END(ScopDetection, "polly-detect",
899                     "Polly - Detect static control parts (SCoPs)", false, false)
900