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/ScopDetectionDiagnostic.h"
51 #include "polly/ScopDetection.h"
52 #include "polly/Support/SCEVValidator.h"
53 #include "polly/Support/ScopHelper.h"
54 #include "polly/CodeGen/CodeGeneration.h"
55 #include "llvm/ADT/Statistic.h"
56 #include "llvm/Analysis/AliasAnalysis.h"
57 #include "llvm/Analysis/LoopInfo.h"
58 #include "llvm/Analysis/PostDominators.h"
59 #include "llvm/Analysis/RegionIterator.h"
60 #include "llvm/Analysis/ScalarEvolution.h"
61 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
62 #include "llvm/IR/DebugInfo.h"
63 #include "llvm/IR/IntrinsicInst.h"
64 #include "llvm/IR/DiagnosticInfo.h"
65 #include "llvm/IR/DiagnosticPrinter.h"
66 #include "llvm/IR/LLVMContext.h"
67 #include "llvm/Support/Debug.h"
68 #include <set>
69 
70 using namespace llvm;
71 using namespace polly;
72 
73 #define DEBUG_TYPE "polly-detect"
74 
75 static cl::opt<bool>
76     DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops",
77                             cl::desc("Detect scops in functions without loops"),
78                             cl::Hidden, cl::init(false), cl::ZeroOrMore,
79                             cl::cat(PollyCategory));
80 
81 static cl::opt<bool>
82     DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
83                               cl::desc("Detect scops in regions without loops"),
84                               cl::Hidden, cl::init(false), cl::ZeroOrMore,
85                               cl::cat(PollyCategory));
86 
87 static cl::opt<bool> DetectUnprofitable("polly-detect-unprofitable",
88                                         cl::desc("Detect unprofitable scops"),
89                                         cl::Hidden, cl::init(false),
90                                         cl::ZeroOrMore, cl::cat(PollyCategory));
91 
92 static cl::opt<std::string> OnlyFunction(
93     "polly-only-func",
94     cl::desc("Only run on functions that contain a certain string"),
95     cl::value_desc("string"), cl::ValueRequired, cl::init(""),
96     cl::cat(PollyCategory));
97 
98 static cl::opt<std::string> OnlyRegion(
99     "polly-only-region",
100     cl::desc("Only run on certain regions (The provided identifier must "
101              "appear in the name of the region's entry block"),
102     cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
103     cl::cat(PollyCategory));
104 
105 static cl::opt<bool>
106     IgnoreAliasing("polly-ignore-aliasing",
107                    cl::desc("Ignore possible aliasing of the array bases"),
108                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
109                    cl::cat(PollyCategory));
110 
111 bool polly::PollyUseRuntimeAliasChecks;
112 static cl::opt<bool, true> XPollyUseRuntimeAliasChecks(
113     "polly-use-runtime-alias-checks",
114     cl::desc("Use runtime alias checks to resolve possible aliasing."),
115     cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore,
116     cl::init(true), cl::cat(PollyCategory));
117 
118 static cl::opt<bool>
119     ReportLevel("polly-report",
120                 cl::desc("Print information about the activities of Polly"),
121                 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
122 
123 static cl::opt<bool>
124     AllowNonAffine("polly-allow-nonaffine",
125                    cl::desc("Allow non affine access functions in arrays"),
126                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
127                    cl::cat(PollyCategory));
128 
129 static cl::opt<bool> AllowNonAffineSubRegions(
130     "polly-allow-nonaffine-branches",
131     cl::desc("Allow non affine conditions for branches"), cl::Hidden,
132     cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
133 
134 static cl::opt<bool> AllowUnsigned("polly-allow-unsigned",
135                                    cl::desc("Allow unsigned expressions"),
136                                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
137                                    cl::cat(PollyCategory));
138 
139 static cl::opt<bool, true>
140     TrackFailures("polly-detect-track-failures",
141                   cl::desc("Track failure strings in detecting scop regions"),
142                   cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore,
143                   cl::init(true), cl::cat(PollyCategory));
144 
145 static cl::opt<bool> KeepGoing("polly-detect-keep-going",
146                                cl::desc("Do not fail on the first error."),
147                                cl::Hidden, cl::ZeroOrMore, cl::init(false),
148                                cl::cat(PollyCategory));
149 
150 static cl::opt<bool, true>
151     PollyDelinearizeX("polly-delinearize",
152                       cl::desc("Delinearize array access functions"),
153                       cl::location(PollyDelinearize), cl::Hidden,
154                       cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory));
155 
156 static cl::opt<bool>
157     VerifyScops("polly-detect-verify",
158                 cl::desc("Verify the detected SCoPs after each transformation"),
159                 cl::Hidden, cl::init(false), cl::ZeroOrMore,
160                 cl::cat(PollyCategory));
161 
162 static cl::opt<bool, true> XPollyModelPHINodes(
163     "polly-model-phi-nodes",
164     cl::desc("Allow PHI nodes in the input [Unsafe with code-generation!]."),
165     cl::location(PollyModelPHINodes), cl::Hidden, cl::ZeroOrMore,
166     cl::init(false), cl::cat(PollyCategory));
167 
168 bool polly::PollyModelPHINodes = false;
169 bool polly::PollyTrackFailures = false;
170 bool polly::PollyDelinearize = false;
171 StringRef polly::PollySkipFnAttr = "polly.skip.fn";
172 
173 //===----------------------------------------------------------------------===//
174 // Statistics.
175 
176 STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
177 
178 class DiagnosticScopFound : public DiagnosticInfo {
179 private:
180   static int PluginDiagnosticKind;
181 
182   Function &F;
183   std::string FileName;
184   unsigned EntryLine, ExitLine;
185 
186 public:
187   DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
188                       unsigned ExitLine)
189       : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
190         EntryLine(EntryLine), ExitLine(ExitLine) {}
191 
192   virtual void print(DiagnosticPrinter &DP) const;
193 
194   static bool classof(const DiagnosticInfo *DI) {
195     return DI->getKind() == PluginDiagnosticKind;
196   }
197 };
198 
199 int DiagnosticScopFound::PluginDiagnosticKind = 10;
200 
201 void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
202   DP << "Polly detected an optimizable loop region (scop) in function '" << F
203      << "'\n";
204 
205   if (FileName.empty()) {
206     DP << "Scop location is unknown. Compile with debug info "
207           "(-g) to get more precise information. ";
208     return;
209   }
210 
211   DP << FileName << ":" << EntryLine << ": Start of scop\n";
212   DP << FileName << ":" << ExitLine << ": End of scop";
213 }
214 
215 //===----------------------------------------------------------------------===//
216 // ScopDetection.
217 
218 ScopDetection::ScopDetection() : FunctionPass(ID) {
219   if (!PollyUseRuntimeAliasChecks)
220     return;
221 
222   // Disable runtime alias checks if we ignore aliasing all together.
223   if (IgnoreAliasing) {
224     PollyUseRuntimeAliasChecks = false;
225     return;
226   }
227 
228   if (AllowNonAffine) {
229     DEBUG(errs() << "WARNING: We disable runtime alias checks as non affine "
230                     "accesses are enabled.\n");
231     PollyUseRuntimeAliasChecks = false;
232   }
233 }
234 
235 template <class RR, typename... Args>
236 inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert,
237                                    Args &&... Arguments) const {
238 
239   if (!Context.Verifying) {
240     RejectLog &Log = Context.Log;
241     std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...);
242 
243     if (PollyTrackFailures)
244       Log.report(RejectReason);
245 
246     DEBUG(dbgs() << RejectReason->getMessage());
247     DEBUG(dbgs() << "\n");
248   } else {
249     assert(!Assert && "Verification of detected scop failed");
250   }
251 
252   return false;
253 }
254 
255 bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
256   if (!ValidRegions.count(&R))
257     return false;
258 
259   if (Verify) {
260     NonAffineSubRegionSetTy DummyNonAffineSubRegionSet;
261     DetectionContext Context(const_cast<Region &>(R), *AA,
262                              DummyNonAffineSubRegionSet, false /*verifying*/);
263     return isValidRegion(Context);
264   }
265 
266   return true;
267 }
268 
269 std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
270   if (!RejectLogs.count(R))
271     return "";
272 
273   // Get the first error we found. Even in keep-going mode, this is the first
274   // reason that caused the candidate to be rejected.
275   RejectLog Errors = RejectLogs.at(R);
276 
277   // This can happen when we marked a region invalid, but didn't track
278   // an error for it.
279   if (Errors.size() == 0)
280     return "";
281 
282   RejectReasonPtr RR = *Errors.begin();
283   return RR->getMessage();
284 }
285 
286 static bool containsLoop(Region *R, LoopInfo *LI) {
287   for (BasicBlock *BB : R->blocks()) {
288     Loop *L = LI->getLoopFor(BB);
289     if (R->contains(L))
290       return true;
291   }
292   return false;
293 }
294 
295 bool ScopDetection::isValidCFG(BasicBlock &BB,
296                                DetectionContext &Context) const {
297   Region &CurRegion = Context.CurRegion;
298 
299   TerminatorInst *TI = BB.getTerminator();
300 
301   // Return instructions are only valid if the region is the top level region.
302   if (isa<ReturnInst>(TI) && !CurRegion.getExit() && TI->getNumOperands() == 0)
303     return true;
304 
305   BranchInst *Br = dyn_cast<BranchInst>(TI);
306 
307   if (!Br)
308     return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB);
309 
310   if (Br->isUnconditional())
311     return true;
312 
313   Value *Condition = Br->getCondition();
314 
315   // UndefValue is not allowed as condition.
316   if (isa<UndefValue>(Condition))
317     return invalid<ReportUndefCond>(Context, /*Assert=*/true, Br, &BB);
318 
319   // Only Constant and ICmpInst are allowed as condition.
320   if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) {
321     if (AllowNonAffineSubRegions && !containsLoop(RI->getRegionFor(&BB), LI))
322       Context.NonAffineSubRegionSet.insert(RI->getRegionFor(&BB));
323     else
324       return invalid<ReportInvalidCond>(Context, /*Assert=*/true, Br, &BB);
325   }
326 
327   // Allow perfectly nested conditions.
328   assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
329 
330   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
331     // Unsigned comparisons are not allowed. They trigger overflow problems
332     // in the code generation.
333     //
334     // TODO: This is not sufficient and just hides bugs. However it does pretty
335     // well.
336     if (ICmp->isUnsigned() && !AllowUnsigned)
337       return false;
338 
339     // Are both operands of the ICmp affine?
340     if (isa<UndefValue>(ICmp->getOperand(0)) ||
341         isa<UndefValue>(ICmp->getOperand(1)))
342       return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp);
343 
344     Loop *L = LI->getLoopFor(ICmp->getParent());
345     const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
346     const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
347 
348     if (!isAffineExpr(&CurRegion, LHS, *SE) ||
349         !isAffineExpr(&CurRegion, RHS, *SE)) {
350       if (AllowNonAffineSubRegions && !containsLoop(RI->getRegionFor(&BB), LI))
351         Context.NonAffineSubRegionSet.insert(RI->getRegionFor(&BB));
352       else
353         return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS,
354                                            RHS, ICmp);
355     }
356   }
357 
358   // Allow loop exit conditions.
359   Loop *L = LI->getLoopFor(&BB);
360   if (L && L->getExitingBlock() == &BB)
361     return true;
362 
363   // Allow perfectly nested conditions.
364   Region *R = RI->getRegionFor(&BB);
365   if (R->getEntry() != &BB)
366     return invalid<ReportCondition>(Context, /*Assert=*/true, &BB);
367 
368   return true;
369 }
370 
371 bool ScopDetection::isValidCallInst(CallInst &CI) {
372   if (CI.doesNotReturn())
373     return false;
374 
375   if (CI.doesNotAccessMemory())
376     return true;
377 
378   Function *CalledFunction = CI.getCalledFunction();
379 
380   // Indirect calls are not supported.
381   if (CalledFunction == 0)
382     return false;
383 
384   // Check if we can handle the intrinsic call.
385   if (auto *IT = dyn_cast<IntrinsicInst>(&CI)) {
386     switch (IT->getIntrinsicID()) {
387     // Lifetime markers are supported/ignored.
388     case llvm::Intrinsic::lifetime_start:
389     case llvm::Intrinsic::lifetime_end:
390     // Invariant markers are supported/ignored.
391     case llvm::Intrinsic::invariant_start:
392     case llvm::Intrinsic::invariant_end:
393     // Some misc annotations are supported/ignored.
394     case llvm::Intrinsic::var_annotation:
395     case llvm::Intrinsic::ptr_annotation:
396     case llvm::Intrinsic::annotation:
397     case llvm::Intrinsic::donothing:
398     case llvm::Intrinsic::assume:
399     case llvm::Intrinsic::expect:
400       return true;
401     default:
402       // Other intrinsics which may access the memory are not yet supported.
403       break;
404     }
405   }
406 
407   return false;
408 }
409 
410 bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
411   // A reference to function argument or constant value is invariant.
412   if (isa<Argument>(Val) || isa<Constant>(Val))
413     return true;
414 
415   const Instruction *I = dyn_cast<Instruction>(&Val);
416   if (!I)
417     return false;
418 
419   if (!Reg.contains(I))
420     return true;
421 
422   if (I->mayHaveSideEffects())
423     return false;
424 
425   // When Val is a Phi node, it is likely not invariant. We do not check whether
426   // Phi nodes are actually invariant, we assume that Phi nodes are usually not
427   // invariant. Recursively checking the operators of Phi nodes would lead to
428   // infinite recursion.
429   if (isa<PHINode>(*I))
430     return false;
431 
432   for (const Use &Operand : I->operands())
433     if (!isInvariant(*Operand, Reg))
434       return false;
435 
436   // When the instruction is a load instruction, check that no write to memory
437   // in the region aliases with the load.
438   if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
439     AliasAnalysis::Location Loc = AA->getLocation(LI);
440 
441     // Check if any basic block in the region can modify the location pointed to
442     // by 'Loc'.  If so, 'Val' is (likely) not invariant in the region.
443     for (const BasicBlock *BB : Reg.blocks())
444       if (AA->canBasicBlockModify(*BB, Loc))
445         return false;
446   }
447 
448   return true;
449 }
450 
451 MapInsnToMemAcc InsnToMemAcc;
452 
453 bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const {
454   Region &CurRegion = Context.CurRegion;
455 
456   for (const SCEVUnknown *BasePointer : Context.NonAffineAccesses) {
457     Value *BaseValue = BasePointer->getValue();
458     ArrayShape *Shape = new ArrayShape(BasePointer);
459     bool BasePtrHasNonAffine = false;
460 
461     // First step: collect parametric terms in all array references.
462     SmallVector<const SCEV *, 4> Terms;
463     for (const auto &Pair : Context.Accesses[BasePointer]) {
464       const SCEVAddRecExpr *AccessFunction =
465           dyn_cast<SCEVAddRecExpr>(Pair.second);
466 
467       if (AccessFunction)
468         AccessFunction->collectParametricTerms(*SE, Terms);
469     }
470 
471     // Second step: find array shape.
472     SE->findArrayDimensions(Terms, Shape->DelinearizedSizes,
473                             Context.ElementSize[BasePointer]);
474 
475     // No array shape derived.
476     if (Shape->DelinearizedSizes.empty()) {
477       if (AllowNonAffine)
478         continue;
479 
480       for (const auto &Pair : Context.Accesses[BasePointer]) {
481         const Instruction *Insn = Pair.first;
482         const SCEV *AF = Pair.second;
483 
484         if (!isAffineExpr(&CurRegion, AF, *SE, BaseValue)) {
485           invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn,
486                                          BaseValue);
487           if (!KeepGoing)
488             return false;
489         }
490       }
491       continue;
492     }
493 
494     // Third step: compute the access functions for each subscript.
495     //
496     // We first store the resulting memory accesses in TempMemoryAccesses. Only
497     // if the access functions for all memory accesses have been successfully
498     // delinearized we continue. Otherwise, we either report a failure or, if
499     // non-affine accesses are allowed, we drop the information. In case the
500     // information is dropped the memory accesses need to be overapproximated
501     // when translated to a polyhedral representation.
502     MapInsnToMemAcc TempMemoryAccesses;
503     for (const auto &Pair : Context.Accesses[BasePointer]) {
504       const Instruction *Insn = Pair.first;
505       const SCEVAddRecExpr *AF = dyn_cast<SCEVAddRecExpr>(Pair.second);
506       bool IsNonAffine = false;
507       MemAcc *Acc = new MemAcc(Insn, Shape);
508       TempMemoryAccesses.insert({Insn, Acc});
509 
510       if (!AF) {
511         if (isAffineExpr(&CurRegion, Pair.second, *SE, BaseValue))
512           Acc->DelinearizedSubscripts.push_back(Pair.second);
513         else
514           IsNonAffine = true;
515       } else {
516         AF->computeAccessFunctions(*SE, Acc->DelinearizedSubscripts,
517                                    Shape->DelinearizedSizes);
518         if (Acc->DelinearizedSubscripts.size() == 0)
519           IsNonAffine = true;
520         for (const SCEV *S : Acc->DelinearizedSubscripts)
521           if (!isAffineExpr(&CurRegion, S, *SE, BaseValue))
522             IsNonAffine = true;
523       }
524 
525       // (Possibly) report non affine access
526       if (IsNonAffine) {
527         BasePtrHasNonAffine = true;
528         if (!AllowNonAffine)
529           invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second,
530                                          Insn, BaseValue);
531         if (!KeepGoing && !AllowNonAffine)
532           return false;
533       }
534     }
535 
536     if (!BasePtrHasNonAffine)
537       InsnToMemAcc.insert(TempMemoryAccesses.begin(), TempMemoryAccesses.end());
538   }
539   return true;
540 }
541 
542 bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
543                                         DetectionContext &Context) const {
544   Region &CurRegion = Context.CurRegion;
545 
546   Value *Ptr = getPointerOperand(Inst);
547   Loop *L = LI->getLoopFor(Inst.getParent());
548   const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
549   const SCEVUnknown *BasePointer;
550   Value *BaseValue;
551 
552   BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
553 
554   if (!BasePointer)
555     return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, &Inst);
556 
557   BaseValue = BasePointer->getValue();
558 
559   if (isa<UndefValue>(BaseValue))
560     return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, &Inst);
561 
562   // Check that the base address of the access is invariant in the current
563   // region.
564   if (!isInvariant(*BaseValue, CurRegion))
565     // Verification of this property is difficult as the independent blocks
566     // pass may introduce aliasing that we did not have when running the
567     // scop detection.
568     return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue,
569                                          &Inst);
570 
571   AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
572 
573   const SCEV *Size = SE->getElementSize(&Inst);
574   if (Context.ElementSize.count(BasePointer)) {
575     if (Context.ElementSize[BasePointer] != Size)
576       return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true,
577                                                       &Inst, BaseValue);
578   } else {
579     Context.ElementSize[BasePointer] = Size;
580   }
581 
582   if (PollyDelinearize) {
583     Context.Accesses[BasePointer].push_back({&Inst, AccessFunction});
584 
585     if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue))
586       Context.NonAffineAccesses.insert(BasePointer);
587   } else if (!AllowNonAffine) {
588     if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue))
589       return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true,
590                                             AccessFunction, &Inst, BaseValue);
591   }
592 
593   // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
594   // created by IndependentBlocks Pass.
595   if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BaseValue))
596     return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst);
597 
598   if (IgnoreAliasing)
599     return true;
600 
601   // Check if the base pointer of the memory access does alias with
602   // any other pointer. This cannot be handled at the moment.
603   AAMDNodes AATags;
604   Inst.getAAMetadata(AATags);
605   AliasSet &AS = Context.AST.getAliasSetForPointer(
606       BaseValue, AliasAnalysis::UnknownSize, AATags);
607 
608   // INVALID triggers an assertion in verifying mode, if it detects that a
609   // SCoP was detected by SCoP detection and that this SCoP was invalidated by
610   // a pass that stated it would preserve the SCoPs. We disable this check as
611   // the independent blocks pass may create memory references which seem to
612   // alias, if -basicaa is not available. They actually do not, but as we can
613   // not proof this without -basicaa we would fail. We disable this check to
614   // not cause irrelevant verification failures.
615   if (!AS.isMustAlias()) {
616     if (PollyUseRuntimeAliasChecks) {
617       bool CanBuildRunTimeCheck = true;
618       // The run-time alias check places code that involves the base pointer at
619       // the beginning of the SCoP. This breaks if the base pointer is defined
620       // inside the scop. Hence, we can only create a run-time check if we are
621       // sure the base pointer is not an instruction defined inside the scop.
622       for (const auto &Ptr : AS) {
623         Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue());
624         if (Inst && CurRegion.contains(Inst)) {
625           CanBuildRunTimeCheck = false;
626           break;
627         }
628       }
629 
630       if (CanBuildRunTimeCheck)
631         return true;
632     }
633     return invalid<ReportAlias>(Context, /*Assert=*/false, &Inst, AS);
634   }
635 
636   return true;
637 }
638 
639 bool ScopDetection::isValidInstruction(Instruction &Inst,
640                                        DetectionContext &Context) const {
641   if (PHINode *PN = dyn_cast<PHINode>(&Inst))
642     if (!PollyModelPHINodes && !canSynthesize(PN, LI, SE, &Context.CurRegion)) {
643       return invalid<ReportPhiNodeRefInRegion>(Context, /*Assert=*/true, &Inst);
644     }
645 
646   // We only check the call instruction but not invoke instruction.
647   if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
648     if (isValidCallInst(*CI))
649       return true;
650 
651     return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst);
652   }
653 
654   if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
655     if (!isa<AllocaInst>(Inst))
656       return true;
657 
658     return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst);
659   }
660 
661   // Check the access function.
662   if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) {
663     Context.hasStores |= isa<StoreInst>(Inst);
664     Context.hasLoads |= isa<LoadInst>(Inst);
665     return isValidMemoryAccess(Inst, Context);
666   }
667 
668   // We do not know this instruction, therefore we assume it is invalid.
669   return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst);
670 }
671 
672 bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
673   // Is the loop count affine?
674   const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
675   if (isAffineExpr(&Context.CurRegion, LoopCount, *SE))
676     return true;
677 
678   return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount);
679 }
680 
681 Region *ScopDetection::expandRegion(Region &R) {
682   // Initial no valid region was found (greater than R)
683   Region *LastValidRegion = nullptr;
684   Region *ExpandedRegion = R.getExpandedRegion();
685 
686   DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
687 
688   while (ExpandedRegion) {
689     DetectionContext Context(*ExpandedRegion, *AA,
690                              NonAffineSubRegionMap[ExpandedRegion],
691                              false /* verifying */);
692     DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
693     // Only expand when we did not collect errors.
694 
695     // Check the exit first (cheap)
696     if (isValidExit(Context) && !Context.Log.hasErrors()) {
697       // If the exit is valid check all blocks
698       //  - if true, a valid region was found => store it + keep expanding
699       //  - if false, .tbd. => stop  (should this really end the loop?)
700       if (!allBlocksValid(Context) || Context.Log.hasErrors())
701         break;
702 
703       if (Context.Log.hasErrors())
704         break;
705 
706       // Delete unnecessary regions (allocated by getExpandedRegion)
707       if (LastValidRegion)
708         delete LastValidRegion;
709 
710       // Store this region, because it is the greatest valid (encountered so
711       // far).
712       LastValidRegion = ExpandedRegion;
713 
714       // Create and test the next greater region (if any)
715       ExpandedRegion = ExpandedRegion->getExpandedRegion();
716 
717     } else {
718       // Create and test the next greater region (if any)
719       Region *TmpRegion = ExpandedRegion->getExpandedRegion();
720 
721       // Delete unnecessary regions (allocated by getExpandedRegion)
722       delete ExpandedRegion;
723 
724       ExpandedRegion = TmpRegion;
725     }
726   }
727 
728   DEBUG({
729     if (LastValidRegion)
730       dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
731     else
732       dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
733   });
734 
735   return LastValidRegion;
736 }
737 static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
738   for (const BasicBlock *BB : R.blocks())
739     if (R.contains(LI->getLoopFor(BB)))
740       return false;
741 
742   return true;
743 }
744 
745 // Remove all direct and indirect children of region R from the region set Regs,
746 // but do not recurse further if the first child has been found.
747 //
748 // Return the number of regions erased from Regs.
749 static unsigned eraseAllChildren(ScopDetection::RegionSet &Regs,
750                                  const Region &R) {
751   unsigned Count = 0;
752   for (auto &SubRegion : R) {
753     if (Regs.count(SubRegion.get())) {
754       ++Count;
755       Regs.remove(SubRegion.get());
756     } else {
757       Count += eraseAllChildren(Regs, *SubRegion);
758     }
759   }
760   return Count;
761 }
762 
763 void ScopDetection::findScops(Region &R) {
764   if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
765     return;
766 
767   DetectionContext Context(R, *AA, NonAffineSubRegionMap[&R],
768                            false /*verifying*/);
769   bool RegionIsValid = isValidRegion(Context);
770   bool HasErrors = !RegionIsValid || Context.Log.size() > 0;
771 
772   if (PollyTrackFailures && HasErrors)
773     RejectLogs.insert(std::make_pair(&R, Context.Log));
774 
775   if (!HasErrors) {
776     ++ValidRegion;
777     ValidRegions.insert(&R);
778     return;
779   }
780 
781   for (auto &SubRegion : R)
782     findScops(*SubRegion);
783 
784   // Try to expand regions.
785   //
786   // As the region tree normally only contains canonical regions, non canonical
787   // regions that form a Scop are not found. Therefore, those non canonical
788   // regions are checked by expanding the canonical ones.
789 
790   std::vector<Region *> ToExpand;
791 
792   for (auto &SubRegion : R)
793     ToExpand.push_back(SubRegion.get());
794 
795   for (Region *CurrentRegion : ToExpand) {
796     // Skip regions that had errors.
797     bool HadErrors = RejectLogs.hasErrors(CurrentRegion);
798     if (HadErrors)
799       continue;
800 
801     // Skip invalid regions. Regions may become invalid, if they are element of
802     // an already expanded region.
803     if (!ValidRegions.count(CurrentRegion))
804       continue;
805 
806     Region *ExpandedR = expandRegion(*CurrentRegion);
807 
808     if (!ExpandedR)
809       continue;
810 
811     R.addSubRegion(ExpandedR, true);
812     ValidRegions.insert(ExpandedR);
813     ValidRegions.remove(CurrentRegion);
814 
815     // Erase all (direct and indirect) children of ExpandedR from the valid
816     // regions and update the number of valid regions.
817     ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR);
818   }
819 }
820 
821 bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
822   Region &CurRegion = Context.CurRegion;
823 
824   for (const BasicBlock *BB : CurRegion.blocks()) {
825     Loop *L = LI->getLoopFor(BB);
826     if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing))
827       return false;
828   }
829 
830   for (BasicBlock *BB : CurRegion.blocks())
831     if (!isValidCFG(*BB, Context) && !KeepGoing)
832       return false;
833 
834   for (BasicBlock *BB : CurRegion.blocks())
835     for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I)
836       if (!isValidInstruction(*I, Context) && !KeepGoing)
837         return false;
838 
839   if (!hasAffineMemoryAccesses(Context))
840     return false;
841 
842   return true;
843 }
844 
845 bool ScopDetection::isValidExit(DetectionContext &Context) const {
846 
847   // PHI nodes are not allowed in the exit basic block.
848   if (BasicBlock *Exit = Context.CurRegion.getExit()) {
849     BasicBlock::iterator I = Exit->begin();
850     if (I != Exit->end() && isa<PHINode>(*I))
851       return invalid<ReportPHIinExit>(Context, /*Assert=*/true, I);
852   }
853 
854   return true;
855 }
856 
857 bool ScopDetection::isValidRegion(DetectionContext &Context) const {
858   Region &CurRegion = Context.CurRegion;
859 
860   DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t");
861 
862   if (CurRegion.isTopLevelRegion()) {
863     DEBUG(dbgs() << "Top level region is invalid\n");
864     return false;
865   }
866 
867   if (!CurRegion.getEntry()->getName().count(OnlyRegion)) {
868     DEBUG({
869       dbgs() << "Region entry does not match -polly-region-only";
870       dbgs() << "\n";
871     });
872     return false;
873   }
874 
875   if (!CurRegion.getEnteringBlock()) {
876     BasicBlock *entry = CurRegion.getEntry();
877     Loop *L = LI->getLoopFor(entry);
878 
879     if (L) {
880       if (!L->isLoopSimplifyForm())
881         return invalid<ReportSimpleLoop>(Context, /*Assert=*/true);
882 
883       for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
884            ++PI) {
885         // Region entering edges come from the same loop but outside the region
886         // are not allowed.
887         if (L->contains(*PI) && !CurRegion.contains(*PI))
888           return invalid<ReportIndEdge>(Context, /*Assert=*/true, *PI);
889       }
890     }
891   }
892 
893   // SCoP cannot contain the entry block of the function, because we need
894   // to insert alloca instruction there when translate scalar to array.
895   if (CurRegion.getEntry() ==
896       &(CurRegion.getEntry()->getParent()->getEntryBlock()))
897     return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry());
898 
899   if (!isValidExit(Context))
900     return false;
901 
902   if (!allBlocksValid(Context))
903     return false;
904 
905   // We can probably not do a lot on scops that only write or only read
906   // data.
907   if (!DetectUnprofitable && (!Context.hasStores || !Context.hasLoads))
908     invalid<ReportUnprofitable>(Context, /*Assert=*/true);
909 
910   DEBUG(dbgs() << "OK\n");
911   return true;
912 }
913 
914 void ScopDetection::markFunctionAsInvalid(Function *F) const {
915   F->addFnAttr(PollySkipFnAttr);
916 }
917 
918 bool ScopDetection::isValidFunction(llvm::Function &F) {
919   return !F.hasFnAttribute(PollySkipFnAttr);
920 }
921 
922 void ScopDetection::printLocations(llvm::Function &F) {
923   for (const Region *R : *this) {
924     unsigned LineEntry, LineExit;
925     std::string FileName;
926 
927     getDebugLocation(R, LineEntry, LineExit, FileName);
928     DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
929     F.getContext().diagnose(Diagnostic);
930   }
931 }
932 
933 void ScopDetection::emitMissedRemarksForValidRegions(
934     const Function &F, const RegionSet &ValidRegions) {
935   for (const Region *R : ValidRegions) {
936     const Region *Parent = R->getParent();
937     if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent))
938       emitRejectionRemarks(F, RejectLogs.at(Parent));
939   }
940 }
941 
942 void ScopDetection::emitMissedRemarksForLeaves(const Function &F,
943                                                const Region *R) {
944   for (const std::unique_ptr<Region> &Child : *R) {
945     bool IsValid = ValidRegions.count(Child.get());
946     if (IsValid)
947       continue;
948 
949     bool IsLeaf = Child->begin() == Child->end();
950     if (!IsLeaf)
951       emitMissedRemarksForLeaves(F, Child.get());
952     else {
953       if (RejectLogs.count(Child.get())) {
954         emitRejectionRemarks(F, RejectLogs.at(Child.get()));
955       }
956     }
957   }
958 }
959 
960 bool ScopDetection::runOnFunction(llvm::Function &F) {
961   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
962   RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
963   if (!DetectScopsWithoutLoops && LI->empty())
964     return false;
965 
966   AA = &getAnalysis<AliasAnalysis>();
967   SE = &getAnalysis<ScalarEvolution>();
968   Region *TopRegion = RI->getTopLevelRegion();
969 
970   releaseMemory();
971 
972   if (OnlyFunction != "" && !F.getName().count(OnlyFunction))
973     return false;
974 
975   if (!isValidFunction(F))
976     return false;
977 
978   findScops(*TopRegion);
979 
980   // Only makes sense when we tracked errors.
981   if (PollyTrackFailures) {
982     emitMissedRemarksForValidRegions(F, ValidRegions);
983     emitMissedRemarksForLeaves(F, TopRegion);
984   }
985 
986   for (const Region *R : ValidRegions)
987     emitValidRemarks(F, R);
988 
989   if (ReportLevel)
990     printLocations(F);
991 
992   return false;
993 }
994 
995 bool ScopDetection::isNonAffineSubRegion(const Region *SubR,
996                                          const Region *ScopR) const {
997   return NonAffineSubRegionMap.lookup(ScopR).count(SubR);
998 }
999 
1000 void polly::ScopDetection::verifyRegion(const Region &R) const {
1001   assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
1002   NonAffineSubRegionSetTy DummyNonAffineSubRegionSet;
1003   DetectionContext Context(const_cast<Region &>(R), *AA,
1004                            DummyNonAffineSubRegionSet, true /*verifying*/);
1005   isValidRegion(Context);
1006 }
1007 
1008 void polly::ScopDetection::verifyAnalysis() const {
1009   if (!VerifyScops)
1010     return;
1011 
1012   for (const Region *R : ValidRegions)
1013     verifyRegion(*R);
1014 }
1015 
1016 void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
1017   AU.addRequired<DominatorTreeWrapperPass>();
1018   AU.addRequired<PostDominatorTree>();
1019   AU.addRequired<LoopInfoWrapperPass>();
1020   AU.addRequired<ScalarEvolution>();
1021   // We also need AA and RegionInfo when we are verifying analysis.
1022   AU.addRequiredTransitive<AliasAnalysis>();
1023   AU.addRequiredTransitive<RegionInfoPass>();
1024   AU.setPreservesAll();
1025 }
1026 
1027 void ScopDetection::print(raw_ostream &OS, const Module *) const {
1028   for (const Region *R : ValidRegions)
1029     OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
1030 
1031   OS << "\n";
1032 }
1033 
1034 void ScopDetection::releaseMemory() {
1035   ValidRegions.clear();
1036   RejectLogs.clear();
1037   NonAffineSubRegionMap.clear();
1038 
1039   // Do not clear the invalid function set.
1040 }
1041 
1042 char ScopDetection::ID = 0;
1043 
1044 Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
1045 
1046 INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
1047                       "Polly - Detect static control parts (SCoPs)", false,
1048                       false);
1049 INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
1050 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
1051 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
1052 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
1053 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
1054 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1055 INITIALIZE_PASS_END(ScopDetection, "polly-detect",
1056                     "Polly - Detect static control parts (SCoPs)", false, false)
1057