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