1 //===- ScopHelper.cpp - Some Helper Functions for Scop.  ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Small functions that help with Scop and LLVM-IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "polly/Support/ScopHelper.h"
14 #include "polly/Options.h"
15 #include "polly/ScopInfo.h"
16 #include "polly/Support/SCEVValidator.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/Analysis/ScalarEvolutionExpander.h"
21 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
22 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
23 
24 using namespace llvm;
25 using namespace polly;
26 
27 #define DEBUG_TYPE "polly-scop-helper"
28 
29 static cl::opt<bool> PollyAllowErrorBlocks(
30     "polly-allow-error-blocks",
31     cl::desc("Allow to speculate on the execution of 'error blocks'."),
32     cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
33 
34 static cl::list<std::string> DebugFunctions(
35     "polly-debug-func",
36     cl::desc("Allow calls to the specified functions in SCoPs even if their "
37              "side-effects are unknown. This can be used to do debug output in "
38              "Polly-transformed code."),
39     cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory));
40 
41 // Ensures that there is just one predecessor to the entry node from outside the
42 // region.
43 // The identity of the region entry node is preserved.
44 static void simplifyRegionEntry(Region *R, DominatorTree *DT, LoopInfo *LI,
45                                 RegionInfo *RI) {
46   BasicBlock *EnteringBB = R->getEnteringBlock();
47   BasicBlock *Entry = R->getEntry();
48 
49   // Before (one of):
50   //
51   //                       \    /            //
52   //                      EnteringBB         //
53   //                        |    \------>    //
54   //   \   /                |                //
55   //   Entry <--\         Entry <--\         //
56   //   /   \    /         /   \    /         //
57   //        ....               ....          //
58 
59   // Create single entry edge if the region has multiple entry edges.
60   if (!EnteringBB) {
61     SmallVector<BasicBlock *, 4> Preds;
62     for (BasicBlock *P : predecessors(Entry))
63       if (!R->contains(P))
64         Preds.push_back(P);
65 
66     BasicBlock *NewEntering =
67         SplitBlockPredecessors(Entry, Preds, ".region_entering", DT, LI);
68 
69     if (RI) {
70       // The exit block of predecessing regions must be changed to NewEntering
71       for (BasicBlock *ExitPred : predecessors(NewEntering)) {
72         Region *RegionOfPred = RI->getRegionFor(ExitPred);
73         if (RegionOfPred->getExit() != Entry)
74           continue;
75 
76         while (!RegionOfPred->isTopLevelRegion() &&
77                RegionOfPred->getExit() == Entry) {
78           RegionOfPred->replaceExit(NewEntering);
79           RegionOfPred = RegionOfPred->getParent();
80         }
81       }
82 
83       // Make all ancestors use EnteringBB as entry; there might be edges to it
84       Region *AncestorR = R->getParent();
85       RI->setRegionFor(NewEntering, AncestorR);
86       while (!AncestorR->isTopLevelRegion() && AncestorR->getEntry() == Entry) {
87         AncestorR->replaceEntry(NewEntering);
88         AncestorR = AncestorR->getParent();
89       }
90     }
91 
92     EnteringBB = NewEntering;
93   }
94   assert(R->getEnteringBlock() == EnteringBB);
95 
96   // After:
97   //
98   //    \    /       //
99   //  EnteringBB     //
100   //      |          //
101   //      |          //
102   //    Entry <--\   //
103   //    /   \    /   //
104   //         ....    //
105 }
106 
107 // Ensure that the region has a single block that branches to the exit node.
108 static void simplifyRegionExit(Region *R, DominatorTree *DT, LoopInfo *LI,
109                                RegionInfo *RI) {
110   BasicBlock *ExitBB = R->getExit();
111   BasicBlock *ExitingBB = R->getExitingBlock();
112 
113   // Before:
114   //
115   //   (Region)   ______/  //
116   //      \  |   /         //
117   //       ExitBB          //
118   //       /    \          //
119 
120   if (!ExitingBB) {
121     SmallVector<BasicBlock *, 4> Preds;
122     for (BasicBlock *P : predecessors(ExitBB))
123       if (R->contains(P))
124         Preds.push_back(P);
125 
126     //  Preds[0] Preds[1]      otherBB //
127     //         \  |  ________/         //
128     //          \ | /                  //
129     //           BB                    //
130     ExitingBB =
131         SplitBlockPredecessors(ExitBB, Preds, ".region_exiting", DT, LI);
132     // Preds[0] Preds[1]      otherBB  //
133     //        \  /           /         //
134     // BB.region_exiting    /          //
135     //                  \  /           //
136     //                   BB            //
137 
138     if (RI)
139       RI->setRegionFor(ExitingBB, R);
140 
141     // Change the exit of nested regions, but not the region itself,
142     R->replaceExitRecursive(ExitingBB);
143     R->replaceExit(ExitBB);
144   }
145   assert(ExitingBB == R->getExitingBlock());
146 
147   // After:
148   //
149   //     \   /                //
150   //    ExitingBB     _____/  //
151   //          \      /        //
152   //           ExitBB         //
153   //           /    \         //
154 }
155 
156 void polly::simplifyRegion(Region *R, DominatorTree *DT, LoopInfo *LI,
157                            RegionInfo *RI) {
158   assert(R && !R->isTopLevelRegion());
159   assert(!RI || RI == R->getRegionInfo());
160   assert((!RI || DT) &&
161          "RegionInfo requires DominatorTree to be updated as well");
162 
163   simplifyRegionEntry(R, DT, LI, RI);
164   simplifyRegionExit(R, DT, LI, RI);
165   assert(R->isSimple());
166 }
167 
168 // Split the block into two successive blocks.
169 //
170 // Like llvm::SplitBlock, but also preserves RegionInfo
171 static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
172                               DominatorTree *DT, llvm::LoopInfo *LI,
173                               RegionInfo *RI) {
174   assert(Old && SplitPt);
175 
176   // Before:
177   //
178   //  \   /  //
179   //   Old   //
180   //  /   \  //
181 
182   BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
183 
184   if (RI) {
185     Region *R = RI->getRegionFor(Old);
186     RI->setRegionFor(NewBlock, R);
187   }
188 
189   // After:
190   //
191   //   \   /    //
192   //    Old     //
193   //     |      //
194   //  NewBlock  //
195   //   /   \    //
196 
197   return NewBlock;
198 }
199 
200 void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, DominatorTree *DT,
201                                      LoopInfo *LI, RegionInfo *RI) {
202   // Find first non-alloca instruction. Every basic block has a non-alloca
203   // instruction, as every well formed basic block has a terminator.
204   BasicBlock::iterator I = EntryBlock->begin();
205   while (isa<AllocaInst>(I))
206     ++I;
207 
208   // splitBlock updates DT, LI and RI.
209   splitBlock(EntryBlock, &*I, DT, LI, RI);
210 }
211 
212 void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
213   auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
214   auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
215   auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
216   auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
217   RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>();
218   RegionInfo *RI = RIP ? &RIP->getRegionInfo() : nullptr;
219 
220   // splitBlock updates DT, LI and RI.
221   polly::splitEntryBlockForAlloca(EntryBlock, DT, LI, RI);
222 }
223 
224 /// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
225 /// instruction but just use it, if it is referenced as a SCEVUnknown. We want
226 /// however to generate new code if the instruction is in the analyzed region
227 /// and we generate code outside/in front of that region. Hence, we generate the
228 /// code for the SDiv/SRem operands in front of the analyzed region and then
229 /// create a new SDiv/SRem operation there too.
230 struct ScopExpander : SCEVVisitor<ScopExpander, const SCEV *> {
231   friend struct SCEVVisitor<ScopExpander, const SCEV *>;
232 
233   explicit ScopExpander(const Region &R, ScalarEvolution &SE,
234                         const DataLayout &DL, const char *Name, ValueMapT *VMap,
235                         BasicBlock *RTCBB)
236       : Expander(SCEVExpander(SE, DL, Name)), SE(SE), Name(Name), R(R),
237         VMap(VMap), RTCBB(RTCBB) {}
238 
239   Value *expandCodeFor(const SCEV *E, Type *Ty, Instruction *I) {
240     // If we generate code in the region we will immediately fall back to the
241     // SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
242     // needed replace them by copies computed in the entering block.
243     if (!R.contains(I))
244       E = visit(E);
245     return Expander.expandCodeFor(E, Ty, I);
246   }
247 
248   const SCEV *visit(const SCEV *E) {
249     // Cache the expansion results for intermediate SCEV expressions. A SCEV
250     // expression can refer to an operand multiple times (e.g. "x*x), so
251     // a naive visitor takes exponential time.
252     if (SCEVCache.count(E))
253       return SCEVCache[E];
254     const SCEV *Result = SCEVVisitor::visit(E);
255     SCEVCache[E] = Result;
256     return Result;
257   }
258 
259 private:
260   SCEVExpander Expander;
261   ScalarEvolution &SE;
262   const char *Name;
263   const Region &R;
264   ValueMapT *VMap;
265   BasicBlock *RTCBB;
266   DenseMap<const SCEV *, const SCEV *> SCEVCache;
267 
268   const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
269                                Instruction *IP) {
270     if (!Inst || !R.contains(Inst))
271       return E;
272 
273     assert(!Inst->mayThrow() && !Inst->mayReadOrWriteMemory() &&
274            !isa<PHINode>(Inst));
275 
276     auto *InstClone = Inst->clone();
277     for (auto &Op : Inst->operands()) {
278       assert(SE.isSCEVable(Op->getType()));
279       auto *OpSCEV = SE.getSCEV(Op);
280       auto *OpClone = expandCodeFor(OpSCEV, Op->getType(), IP);
281       InstClone->replaceUsesOfWith(Op, OpClone);
282     }
283 
284     InstClone->setName(Name + Inst->getName());
285     InstClone->insertBefore(IP);
286     return SE.getSCEV(InstClone);
287   }
288 
289   const SCEV *visitUnknown(const SCEVUnknown *E) {
290 
291     // If a value mapping was given try if the underlying value is remapped.
292     Value *NewVal = VMap ? VMap->lookup(E->getValue()) : nullptr;
293     if (NewVal) {
294       auto *NewE = SE.getSCEV(NewVal);
295 
296       // While the mapped value might be different the SCEV representation might
297       // not be. To this end we will check before we go into recursion here.
298       if (E != NewE)
299         return visit(NewE);
300     }
301 
302     Instruction *Inst = dyn_cast<Instruction>(E->getValue());
303     Instruction *IP;
304     if (Inst && !R.contains(Inst))
305       IP = Inst;
306     else if (Inst && RTCBB->getParent() == Inst->getFunction())
307       IP = RTCBB->getTerminator();
308     else
309       IP = RTCBB->getParent()->getEntryBlock().getTerminator();
310 
311     if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
312                   Inst->getOpcode() != Instruction::SDiv))
313       return visitGenericInst(E, Inst, IP);
314 
315     const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
316     const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
317 
318     if (!SE.isKnownNonZero(RHSScev))
319       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
320 
321     Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
322     Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
323 
324     Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
325                                   LHS, RHS, Inst->getName() + Name, IP);
326     return SE.getSCEV(Inst);
327   }
328 
329   /// The following functions will just traverse the SCEV and rebuild it with
330   /// the new operands returned by the traversal.
331   ///
332   ///{
333   const SCEV *visitConstant(const SCEVConstant *E) { return E; }
334   const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
335     return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
336   }
337   const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
338     return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
339   }
340   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
341     return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
342   }
343   const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
344     auto *RHSScev = visit(E->getRHS());
345     if (!SE.isKnownNonZero(RHSScev))
346       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
347     return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
348   }
349   const SCEV *visitAddExpr(const SCEVAddExpr *E) {
350     SmallVector<const SCEV *, 4> NewOps;
351     for (const SCEV *Op : E->operands())
352       NewOps.push_back(visit(Op));
353     return SE.getAddExpr(NewOps);
354   }
355   const SCEV *visitMulExpr(const SCEVMulExpr *E) {
356     SmallVector<const SCEV *, 4> NewOps;
357     for (const SCEV *Op : E->operands())
358       NewOps.push_back(visit(Op));
359     return SE.getMulExpr(NewOps);
360   }
361   const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
362     SmallVector<const SCEV *, 4> NewOps;
363     for (const SCEV *Op : E->operands())
364       NewOps.push_back(visit(Op));
365     return SE.getUMaxExpr(NewOps);
366   }
367   const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
368     SmallVector<const SCEV *, 4> NewOps;
369     for (const SCEV *Op : E->operands())
370       NewOps.push_back(visit(Op));
371     return SE.getSMaxExpr(NewOps);
372   }
373   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
374     SmallVector<const SCEV *, 4> NewOps;
375     for (const SCEV *Op : E->operands())
376       NewOps.push_back(visit(Op));
377     return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
378   }
379   ///}
380 };
381 
382 Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
383                             const char *Name, const SCEV *E, Type *Ty,
384                             Instruction *IP, ValueMapT *VMap,
385                             BasicBlock *RTCBB) {
386   ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap, RTCBB);
387   return Expander.expandCodeFor(E, Ty, IP);
388 }
389 
390 bool polly::isErrorBlock(BasicBlock &BB, const Region &R, LoopInfo &LI,
391                          const DominatorTree &DT) {
392   if (!PollyAllowErrorBlocks)
393     return false;
394 
395   if (isa<UnreachableInst>(BB.getTerminator()))
396     return true;
397 
398   if (LI.isLoopHeader(&BB))
399     return false;
400 
401   // Basic blocks that are always executed are not considered error blocks,
402   // as their execution can not be a rare event.
403   bool DominatesAllPredecessors = true;
404   if (R.isTopLevelRegion()) {
405     for (BasicBlock &I : *R.getEntry()->getParent())
406       if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
407         DominatesAllPredecessors = false;
408   } else {
409     for (auto Pred : predecessors(R.getExit()))
410       if (R.contains(Pred) && !DT.dominates(&BB, Pred))
411         DominatesAllPredecessors = false;
412   }
413 
414   if (DominatesAllPredecessors)
415     return false;
416 
417   for (Instruction &Inst : BB)
418     if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
419       if (isDebugCall(CI))
420         continue;
421 
422       if (isIgnoredIntrinsic(CI))
423         continue;
424 
425       // memset, memcpy and memmove are modeled intrinsics.
426       if (isa<MemSetInst>(CI) || isa<MemTransferInst>(CI))
427         continue;
428 
429       if (!CI->doesNotAccessMemory())
430         return true;
431       if (CI->doesNotReturn())
432         return true;
433     }
434 
435   return false;
436 }
437 
438 Value *polly::getConditionFromTerminator(Instruction *TI) {
439   if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
440     if (BR->isUnconditional())
441       return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
442 
443     return BR->getCondition();
444   }
445 
446   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
447     return SI->getCondition();
448 
449   return nullptr;
450 }
451 
452 static bool hasVariantIndex(GetElementPtrInst *Gep, Loop *L, Region &R,
453                             ScalarEvolution &SE) {
454   for (const Use &Val : llvm::drop_begin(Gep->operands(), 1)) {
455     const SCEV *PtrSCEV = SE.getSCEVAtScope(Val, L);
456     Loop *OuterLoop = R.outermostLoopInRegion(L);
457     if (!SE.isLoopInvariant(PtrSCEV, OuterLoop))
458       return true;
459   }
460   return false;
461 }
462 
463 bool polly::isHoistableLoad(LoadInst *LInst, Region &R, LoopInfo &LI,
464                             ScalarEvolution &SE, const DominatorTree &DT,
465                             const InvariantLoadsSetTy &KnownInvariantLoads) {
466   Loop *L = LI.getLoopFor(LInst->getParent());
467   auto *Ptr = LInst->getPointerOperand();
468 
469   // A LoadInst is hoistable if the address it is loading from is also
470   // invariant; in this case: another invariant load (whether that address
471   // is also not written to has to be checked separately)
472   // TODO: This only checks for a LoadInst->GetElementPtrInst->LoadInst
473   // pattern generated by the Chapel frontend, but generally this applies
474   // for any chain of instruction that does not also depend on any
475   // induction variable
476   if (auto *GepInst = dyn_cast<GetElementPtrInst>(Ptr)) {
477     if (!hasVariantIndex(GepInst, L, R, SE)) {
478       if (auto *DecidingLoad =
479               dyn_cast<LoadInst>(GepInst->getPointerOperand())) {
480         if (KnownInvariantLoads.count(DecidingLoad))
481           return true;
482       }
483     }
484   }
485 
486   const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
487   while (L && R.contains(L)) {
488     if (!SE.isLoopInvariant(PtrSCEV, L))
489       return false;
490     L = L->getParentLoop();
491   }
492 
493   for (auto *User : Ptr->users()) {
494     auto *UserI = dyn_cast<Instruction>(User);
495     if (!UserI || !R.contains(UserI))
496       continue;
497     if (!UserI->mayWriteToMemory())
498       continue;
499 
500     auto &BB = *UserI->getParent();
501     if (DT.dominates(&BB, LInst->getParent()))
502       return false;
503 
504     bool DominatesAllPredecessors = true;
505     if (R.isTopLevelRegion()) {
506       for (BasicBlock &I : *R.getEntry()->getParent())
507         if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
508           DominatesAllPredecessors = false;
509     } else {
510       for (auto Pred : predecessors(R.getExit()))
511         if (R.contains(Pred) && !DT.dominates(&BB, Pred))
512           DominatesAllPredecessors = false;
513     }
514 
515     if (!DominatesAllPredecessors)
516       continue;
517 
518     return false;
519   }
520 
521   return true;
522 }
523 
524 bool polly::isIgnoredIntrinsic(const Value *V) {
525   if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
526     switch (IT->getIntrinsicID()) {
527     // Lifetime markers are supported/ignored.
528     case llvm::Intrinsic::lifetime_start:
529     case llvm::Intrinsic::lifetime_end:
530     // Invariant markers are supported/ignored.
531     case llvm::Intrinsic::invariant_start:
532     case llvm::Intrinsic::invariant_end:
533     // Some misc annotations are supported/ignored.
534     case llvm::Intrinsic::var_annotation:
535     case llvm::Intrinsic::ptr_annotation:
536     case llvm::Intrinsic::annotation:
537     case llvm::Intrinsic::donothing:
538     case llvm::Intrinsic::assume:
539     // Some debug info intrinsics are supported/ignored.
540     case llvm::Intrinsic::dbg_value:
541     case llvm::Intrinsic::dbg_declare:
542       return true;
543     default:
544       break;
545     }
546   }
547   return false;
548 }
549 
550 bool polly::canSynthesize(const Value *V, const Scop &S, ScalarEvolution *SE,
551                           Loop *Scope) {
552   if (!V || !SE->isSCEVable(V->getType()))
553     return false;
554 
555   const InvariantLoadsSetTy &ILS = S.getRequiredInvariantLoads();
556   if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope))
557     if (!isa<SCEVCouldNotCompute>(Scev))
558       if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false, ILS))
559         return true;
560 
561   return false;
562 }
563 
564 llvm::BasicBlock *polly::getUseBlock(const llvm::Use &U) {
565   Instruction *UI = dyn_cast<Instruction>(U.getUser());
566   if (!UI)
567     return nullptr;
568 
569   if (PHINode *PHI = dyn_cast<PHINode>(UI))
570     return PHI->getIncomingBlock(U);
571 
572   return UI->getParent();
573 }
574 
575 std::tuple<std::vector<const SCEV *>, std::vector<int>>
576 polly::getIndexExpressionsFromGEP(GetElementPtrInst *GEP, ScalarEvolution &SE) {
577   std::vector<const SCEV *> Subscripts;
578   std::vector<int> Sizes;
579 
580   Type *Ty = GEP->getPointerOperandType();
581 
582   bool DroppedFirstDim = false;
583 
584   for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
585 
586     const SCEV *Expr = SE.getSCEV(GEP->getOperand(i));
587 
588     if (i == 1) {
589       if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
590         Ty = PtrTy->getElementType();
591       } else if (auto *ArrayTy = dyn_cast<ArrayType>(Ty)) {
592         Ty = ArrayTy->getElementType();
593       } else {
594         Subscripts.clear();
595         Sizes.clear();
596         break;
597       }
598       if (auto *Const = dyn_cast<SCEVConstant>(Expr))
599         if (Const->getValue()->isZero()) {
600           DroppedFirstDim = true;
601           continue;
602         }
603       Subscripts.push_back(Expr);
604       continue;
605     }
606 
607     auto *ArrayTy = dyn_cast<ArrayType>(Ty);
608     if (!ArrayTy) {
609       Subscripts.clear();
610       Sizes.clear();
611       break;
612     }
613 
614     Subscripts.push_back(Expr);
615     if (!(DroppedFirstDim && i == 2))
616       Sizes.push_back(ArrayTy->getNumElements());
617 
618     Ty = ArrayTy->getElementType();
619   }
620 
621   return std::make_tuple(Subscripts, Sizes);
622 }
623 
624 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI,
625                                            const BoxedLoopsSetTy &BoxedLoops) {
626   while (BoxedLoops.count(L))
627     L = L->getParentLoop();
628   return L;
629 }
630 
631 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::BasicBlock *BB,
632                                            llvm::LoopInfo &LI,
633                                            const BoxedLoopsSetTy &BoxedLoops) {
634   Loop *L = LI.getLoopFor(BB);
635   return getFirstNonBoxedLoopFor(L, LI, BoxedLoops);
636 }
637 
638 bool polly::isDebugCall(Instruction *Inst) {
639   auto *CI = dyn_cast<CallInst>(Inst);
640   if (!CI)
641     return false;
642 
643   Function *CF = CI->getCalledFunction();
644   if (!CF)
645     return false;
646 
647   return std::find(DebugFunctions.begin(), DebugFunctions.end(),
648                    CF->getName()) != DebugFunctions.end();
649 }
650 
651 static bool hasDebugCall(BasicBlock *BB) {
652   for (Instruction &Inst : *BB) {
653     if (isDebugCall(&Inst))
654       return true;
655   }
656   return false;
657 }
658 
659 bool polly::hasDebugCall(ScopStmt *Stmt) {
660   // Quick skip if no debug functions have been defined.
661   if (DebugFunctions.empty())
662     return false;
663 
664   if (!Stmt)
665     return false;
666 
667   for (Instruction *Inst : Stmt->getInstructions())
668     if (isDebugCall(Inst))
669       return true;
670 
671   if (Stmt->isRegionStmt()) {
672     for (BasicBlock *RBB : Stmt->getRegion()->blocks())
673       if (RBB != Stmt->getEntryBlock() && ::hasDebugCall(RBB))
674         return true;
675   }
676 
677   return false;
678 }
679