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/ScalarEvolutionExpressions.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.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.
simplifyRegionEntry(Region * R,DominatorTree * DT,LoopInfo * LI,RegionInfo * RI)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.
simplifyRegionExit(Region * R,DominatorTree * DT,LoopInfo * LI,RegionInfo * RI)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 
simplifyRegion(Region * R,DominatorTree * DT,LoopInfo * LI,RegionInfo * RI)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
splitBlock(BasicBlock * Old,Instruction * SplitPt,DominatorTree * DT,llvm::LoopInfo * LI,RegionInfo * RI)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 
splitEntryBlockForAlloca(BasicBlock * EntryBlock,DominatorTree * DT,LoopInfo * LI,RegionInfo * RI)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 
splitEntryBlockForAlloca(BasicBlock * EntryBlock,Pass * P)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 
recordAssumption(polly::RecordedAssumptionsTy * RecordedAssumptions,polly::AssumptionKind Kind,isl::set Set,DebugLoc Loc,polly::AssumptionSign Sign,BasicBlock * BB)224 void polly::recordAssumption(polly::RecordedAssumptionsTy *RecordedAssumptions,
225                              polly::AssumptionKind Kind, isl::set Set,
226                              DebugLoc Loc, polly::AssumptionSign Sign,
227                              BasicBlock *BB) {
228   assert((Set.is_params() || BB) &&
229          "Assumptions without a basic block must be parameter sets");
230   if (RecordedAssumptions)
231     RecordedAssumptions->push_back({Kind, Sign, Set, Loc, BB});
232 }
233 
234 /// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
235 /// instruction but just use it, if it is referenced as a SCEVUnknown. We want
236 /// however to generate new code if the instruction is in the analyzed region
237 /// and we generate code outside/in front of that region. Hence, we generate the
238 /// code for the SDiv/SRem operands in front of the analyzed region and then
239 /// create a new SDiv/SRem operation there too.
240 struct ScopExpander : SCEVVisitor<ScopExpander, const SCEV *> {
241   friend struct SCEVVisitor<ScopExpander, const SCEV *>;
242 
ScopExpanderScopExpander243   explicit ScopExpander(const Region &R, ScalarEvolution &SE,
244                         const DataLayout &DL, const char *Name, ValueMapT *VMap,
245                         BasicBlock *RTCBB)
246       : Expander(SE, DL, Name, /*PreserveLCSSA=*/false), SE(SE), Name(Name),
247         R(R), VMap(VMap), RTCBB(RTCBB) {}
248 
expandCodeForScopExpander249   Value *expandCodeFor(const SCEV *E, Type *Ty, Instruction *I) {
250     // If we generate code in the region we will immediately fall back to the
251     // SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
252     // needed replace them by copies computed in the entering block.
253     if (!R.contains(I))
254       E = visit(E);
255     return Expander.expandCodeFor(E, Ty, I);
256   }
257 
visitScopExpander258   const SCEV *visit(const SCEV *E) {
259     // Cache the expansion results for intermediate SCEV expressions. A SCEV
260     // expression can refer to an operand multiple times (e.g. "x*x), so
261     // a naive visitor takes exponential time.
262     if (SCEVCache.count(E))
263       return SCEVCache[E];
264     const SCEV *Result = SCEVVisitor::visit(E);
265     SCEVCache[E] = Result;
266     return Result;
267   }
268 
269 private:
270   SCEVExpander Expander;
271   ScalarEvolution &SE;
272   const char *Name;
273   const Region &R;
274   ValueMapT *VMap;
275   BasicBlock *RTCBB;
276   DenseMap<const SCEV *, const SCEV *> SCEVCache;
277 
visitGenericInstScopExpander278   const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
279                                Instruction *IP) {
280     if (!Inst || !R.contains(Inst))
281       return E;
282 
283     assert(!Inst->mayThrow() && !Inst->mayReadOrWriteMemory() &&
284            !isa<PHINode>(Inst));
285 
286     auto *InstClone = Inst->clone();
287     for (auto &Op : Inst->operands()) {
288       assert(SE.isSCEVable(Op->getType()));
289       auto *OpSCEV = SE.getSCEV(Op);
290       auto *OpClone = expandCodeFor(OpSCEV, Op->getType(), IP);
291       InstClone->replaceUsesOfWith(Op, OpClone);
292     }
293 
294     InstClone->setName(Name + Inst->getName());
295     InstClone->insertBefore(IP);
296     return SE.getSCEV(InstClone);
297   }
298 
visitUnknownScopExpander299   const SCEV *visitUnknown(const SCEVUnknown *E) {
300 
301     // If a value mapping was given try if the underlying value is remapped.
302     Value *NewVal = VMap ? VMap->lookup(E->getValue()) : nullptr;
303     if (NewVal) {
304       auto *NewE = SE.getSCEV(NewVal);
305 
306       // While the mapped value might be different the SCEV representation might
307       // not be. To this end we will check before we go into recursion here.
308       if (E != NewE)
309         return visit(NewE);
310     }
311 
312     Instruction *Inst = dyn_cast<Instruction>(E->getValue());
313     Instruction *IP;
314     if (Inst && !R.contains(Inst))
315       IP = Inst;
316     else if (Inst && RTCBB->getParent() == Inst->getFunction())
317       IP = RTCBB->getTerminator();
318     else
319       IP = RTCBB->getParent()->getEntryBlock().getTerminator();
320 
321     if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
322                   Inst->getOpcode() != Instruction::SDiv))
323       return visitGenericInst(E, Inst, IP);
324 
325     const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
326     const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
327 
328     if (!SE.isKnownNonZero(RHSScev))
329       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
330 
331     Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
332     Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
333 
334     Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
335                                   LHS, RHS, Inst->getName() + Name, IP);
336     return SE.getSCEV(Inst);
337   }
338 
339   /// The following functions will just traverse the SCEV and rebuild it with
340   /// the new operands returned by the traversal.
341   ///
342   ///{
visitConstantScopExpander343   const SCEV *visitConstant(const SCEVConstant *E) { return E; }
visitPtrToIntExprScopExpander344   const SCEV *visitPtrToIntExpr(const SCEVPtrToIntExpr *E) {
345     return SE.getPtrToIntExpr(visit(E->getOperand()), E->getType());
346   }
visitTruncateExprScopExpander347   const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
348     return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
349   }
visitZeroExtendExprScopExpander350   const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
351     return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
352   }
visitSignExtendExprScopExpander353   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
354     return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
355   }
visitUDivExprScopExpander356   const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
357     auto *RHSScev = visit(E->getRHS());
358     if (!SE.isKnownNonZero(RHSScev))
359       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
360     return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
361   }
visitAddExprScopExpander362   const SCEV *visitAddExpr(const SCEVAddExpr *E) {
363     SmallVector<const SCEV *, 4> NewOps;
364     for (const SCEV *Op : E->operands())
365       NewOps.push_back(visit(Op));
366     return SE.getAddExpr(NewOps);
367   }
visitMulExprScopExpander368   const SCEV *visitMulExpr(const SCEVMulExpr *E) {
369     SmallVector<const SCEV *, 4> NewOps;
370     for (const SCEV *Op : E->operands())
371       NewOps.push_back(visit(Op));
372     return SE.getMulExpr(NewOps);
373   }
visitUMaxExprScopExpander374   const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
375     SmallVector<const SCEV *, 4> NewOps;
376     for (const SCEV *Op : E->operands())
377       NewOps.push_back(visit(Op));
378     return SE.getUMaxExpr(NewOps);
379   }
visitSMaxExprScopExpander380   const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
381     SmallVector<const SCEV *, 4> NewOps;
382     for (const SCEV *Op : E->operands())
383       NewOps.push_back(visit(Op));
384     return SE.getSMaxExpr(NewOps);
385   }
visitUMinExprScopExpander386   const SCEV *visitUMinExpr(const SCEVUMinExpr *E) {
387     SmallVector<const SCEV *, 4> NewOps;
388     for (const SCEV *Op : E->operands())
389       NewOps.push_back(visit(Op));
390     return SE.getUMinExpr(NewOps);
391   }
visitSMinExprScopExpander392   const SCEV *visitSMinExpr(const SCEVSMinExpr *E) {
393     SmallVector<const SCEV *, 4> NewOps;
394     for (const SCEV *Op : E->operands())
395       NewOps.push_back(visit(Op));
396     return SE.getSMinExpr(NewOps);
397   }
visitAddRecExprScopExpander398   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
399     SmallVector<const SCEV *, 4> NewOps;
400     for (const SCEV *Op : E->operands())
401       NewOps.push_back(visit(Op));
402     return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
403   }
404   ///}
405 };
406 
expandCodeFor(Scop & S,ScalarEvolution & SE,const DataLayout & DL,const char * Name,const SCEV * E,Type * Ty,Instruction * IP,ValueMapT * VMap,BasicBlock * RTCBB)407 Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
408                             const char *Name, const SCEV *E, Type *Ty,
409                             Instruction *IP, ValueMapT *VMap,
410                             BasicBlock *RTCBB) {
411   ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap, RTCBB);
412   return Expander.expandCodeFor(E, Ty, IP);
413 }
414 
isErrorBlock(BasicBlock & BB,const Region & R,LoopInfo & LI,const DominatorTree & DT)415 bool polly::isErrorBlock(BasicBlock &BB, const Region &R, LoopInfo &LI,
416                          const DominatorTree &DT) {
417   if (!PollyAllowErrorBlocks)
418     return false;
419 
420   if (isa<UnreachableInst>(BB.getTerminator()))
421     return true;
422 
423   if (LI.isLoopHeader(&BB))
424     return false;
425 
426   // Basic blocks that are always executed are not considered error blocks,
427   // as their execution can not be a rare event.
428   bool DominatesAllPredecessors = true;
429   if (R.isTopLevelRegion()) {
430     for (BasicBlock &I : *R.getEntry()->getParent())
431       if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
432         DominatesAllPredecessors = false;
433   } else {
434     for (auto Pred : predecessors(R.getExit()))
435       if (R.contains(Pred) && !DT.dominates(&BB, Pred))
436         DominatesAllPredecessors = false;
437   }
438 
439   if (DominatesAllPredecessors)
440     return false;
441 
442   for (Instruction &Inst : BB)
443     if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
444       if (isDebugCall(CI))
445         continue;
446 
447       if (isIgnoredIntrinsic(CI))
448         continue;
449 
450       // memset, memcpy and memmove are modeled intrinsics.
451       if (isa<MemSetInst>(CI) || isa<MemTransferInst>(CI))
452         continue;
453 
454       if (!CI->doesNotAccessMemory())
455         return true;
456       if (CI->doesNotReturn())
457         return true;
458     }
459 
460   return false;
461 }
462 
getConditionFromTerminator(Instruction * TI)463 Value *polly::getConditionFromTerminator(Instruction *TI) {
464   if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
465     if (BR->isUnconditional())
466       return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
467 
468     return BR->getCondition();
469   }
470 
471   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
472     return SI->getCondition();
473 
474   return nullptr;
475 }
476 
getLoopSurroundingScop(Scop & S,LoopInfo & LI)477 Loop *polly::getLoopSurroundingScop(Scop &S, LoopInfo &LI) {
478   // Start with the smallest loop containing the entry and expand that
479   // loop until it contains all blocks in the region. If there is a loop
480   // containing all blocks in the region check if it is itself contained
481   // and if so take the parent loop as it will be the smallest containing
482   // the region but not contained by it.
483   Loop *L = LI.getLoopFor(S.getEntry());
484   while (L) {
485     bool AllContained = true;
486     for (auto *BB : S.blocks())
487       AllContained &= L->contains(BB);
488     if (AllContained)
489       break;
490     L = L->getParentLoop();
491   }
492 
493   return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr;
494 }
495 
getNumBlocksInLoop(Loop * L)496 unsigned polly::getNumBlocksInLoop(Loop *L) {
497   unsigned NumBlocks = L->getNumBlocks();
498   SmallVector<BasicBlock *, 4> ExitBlocks;
499   L->getExitBlocks(ExitBlocks);
500 
501   for (auto ExitBlock : ExitBlocks) {
502     if (isa<UnreachableInst>(ExitBlock->getTerminator()))
503       NumBlocks++;
504   }
505   return NumBlocks;
506 }
507 
getNumBlocksInRegionNode(RegionNode * RN)508 unsigned polly::getNumBlocksInRegionNode(RegionNode *RN) {
509   if (!RN->isSubRegion())
510     return 1;
511 
512   Region *R = RN->getNodeAs<Region>();
513   return std::distance(R->block_begin(), R->block_end());
514 }
515 
getRegionNodeLoop(RegionNode * RN,LoopInfo & LI)516 Loop *polly::getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) {
517   if (!RN->isSubRegion()) {
518     BasicBlock *BB = RN->getNodeAs<BasicBlock>();
519     Loop *L = LI.getLoopFor(BB);
520 
521     // Unreachable statements are not considered to belong to a LLVM loop, as
522     // they are not part of an actual loop in the control flow graph.
523     // Nevertheless, we handle certain unreachable statements that are common
524     // when modeling run-time bounds checks as being part of the loop to be
525     // able to model them and to later eliminate the run-time bounds checks.
526     //
527     // Specifically, for basic blocks that terminate in an unreachable and
528     // where the immediate predecessor is part of a loop, we assume these
529     // basic blocks belong to the loop the predecessor belongs to. This
530     // allows us to model the following code.
531     //
532     // for (i = 0; i < N; i++) {
533     //   if (i > 1024)
534     //     abort();            <- this abort might be translated to an
535     //                            unreachable
536     //
537     //   A[i] = ...
538     // }
539     if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode())
540       L = LI.getLoopFor(BB->getPrevNode());
541     return L;
542   }
543 
544   Region *NonAffineSubRegion = RN->getNodeAs<Region>();
545   Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry());
546   while (L && NonAffineSubRegion->contains(L))
547     L = L->getParentLoop();
548   return L;
549 }
550 
hasVariantIndex(GetElementPtrInst * Gep,Loop * L,Region & R,ScalarEvolution & SE)551 static bool hasVariantIndex(GetElementPtrInst *Gep, Loop *L, Region &R,
552                             ScalarEvolution &SE) {
553   for (const Use &Val : llvm::drop_begin(Gep->operands(), 1)) {
554     const SCEV *PtrSCEV = SE.getSCEVAtScope(Val, L);
555     Loop *OuterLoop = R.outermostLoopInRegion(L);
556     if (!SE.isLoopInvariant(PtrSCEV, OuterLoop))
557       return true;
558   }
559   return false;
560 }
561 
isHoistableLoad(LoadInst * LInst,Region & R,LoopInfo & LI,ScalarEvolution & SE,const DominatorTree & DT,const InvariantLoadsSetTy & KnownInvariantLoads)562 bool polly::isHoistableLoad(LoadInst *LInst, Region &R, LoopInfo &LI,
563                             ScalarEvolution &SE, const DominatorTree &DT,
564                             const InvariantLoadsSetTy &KnownInvariantLoads) {
565   Loop *L = LI.getLoopFor(LInst->getParent());
566   auto *Ptr = LInst->getPointerOperand();
567 
568   // A LoadInst is hoistable if the address it is loading from is also
569   // invariant; in this case: another invariant load (whether that address
570   // is also not written to has to be checked separately)
571   // TODO: This only checks for a LoadInst->GetElementPtrInst->LoadInst
572   // pattern generated by the Chapel frontend, but generally this applies
573   // for any chain of instruction that does not also depend on any
574   // induction variable
575   if (auto *GepInst = dyn_cast<GetElementPtrInst>(Ptr)) {
576     if (!hasVariantIndex(GepInst, L, R, SE)) {
577       if (auto *DecidingLoad =
578               dyn_cast<LoadInst>(GepInst->getPointerOperand())) {
579         if (KnownInvariantLoads.count(DecidingLoad))
580           return true;
581       }
582     }
583   }
584 
585   const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
586   while (L && R.contains(L)) {
587     if (!SE.isLoopInvariant(PtrSCEV, L))
588       return false;
589     L = L->getParentLoop();
590   }
591 
592   for (auto *User : Ptr->users()) {
593     auto *UserI = dyn_cast<Instruction>(User);
594     if (!UserI || !R.contains(UserI))
595       continue;
596     if (!UserI->mayWriteToMemory())
597       continue;
598 
599     auto &BB = *UserI->getParent();
600     if (DT.dominates(&BB, LInst->getParent()))
601       return false;
602 
603     bool DominatesAllPredecessors = true;
604     if (R.isTopLevelRegion()) {
605       for (BasicBlock &I : *R.getEntry()->getParent())
606         if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
607           DominatesAllPredecessors = false;
608     } else {
609       for (auto Pred : predecessors(R.getExit()))
610         if (R.contains(Pred) && !DT.dominates(&BB, Pred))
611           DominatesAllPredecessors = false;
612     }
613 
614     if (!DominatesAllPredecessors)
615       continue;
616 
617     return false;
618   }
619 
620   return true;
621 }
622 
isIgnoredIntrinsic(const Value * V)623 bool polly::isIgnoredIntrinsic(const Value *V) {
624   if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
625     switch (IT->getIntrinsicID()) {
626     // Lifetime markers are supported/ignored.
627     case llvm::Intrinsic::lifetime_start:
628     case llvm::Intrinsic::lifetime_end:
629     // Invariant markers are supported/ignored.
630     case llvm::Intrinsic::invariant_start:
631     case llvm::Intrinsic::invariant_end:
632     // Some misc annotations are supported/ignored.
633     case llvm::Intrinsic::var_annotation:
634     case llvm::Intrinsic::ptr_annotation:
635     case llvm::Intrinsic::annotation:
636     case llvm::Intrinsic::donothing:
637     case llvm::Intrinsic::assume:
638     // Some debug info intrinsics are supported/ignored.
639     case llvm::Intrinsic::dbg_value:
640     case llvm::Intrinsic::dbg_declare:
641       return true;
642     default:
643       break;
644     }
645   }
646   return false;
647 }
648 
canSynthesize(const Value * V,const Scop & S,ScalarEvolution * SE,Loop * Scope)649 bool polly::canSynthesize(const Value *V, const Scop &S, ScalarEvolution *SE,
650                           Loop *Scope) {
651   if (!V || !SE->isSCEVable(V->getType()))
652     return false;
653 
654   const InvariantLoadsSetTy &ILS = S.getRequiredInvariantLoads();
655   if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope))
656     if (!isa<SCEVCouldNotCompute>(Scev))
657       if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false, ILS))
658         return true;
659 
660   return false;
661 }
662 
getUseBlock(const llvm::Use & U)663 llvm::BasicBlock *polly::getUseBlock(const llvm::Use &U) {
664   Instruction *UI = dyn_cast<Instruction>(U.getUser());
665   if (!UI)
666     return nullptr;
667 
668   if (PHINode *PHI = dyn_cast<PHINode>(UI))
669     return PHI->getIncomingBlock(U);
670 
671   return UI->getParent();
672 }
673 
getFirstNonBoxedLoopFor(llvm::Loop * L,llvm::LoopInfo & LI,const BoxedLoopsSetTy & BoxedLoops)674 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI,
675                                            const BoxedLoopsSetTy &BoxedLoops) {
676   while (BoxedLoops.count(L))
677     L = L->getParentLoop();
678   return L;
679 }
680 
getFirstNonBoxedLoopFor(llvm::BasicBlock * BB,llvm::LoopInfo & LI,const BoxedLoopsSetTy & BoxedLoops)681 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::BasicBlock *BB,
682                                            llvm::LoopInfo &LI,
683                                            const BoxedLoopsSetTy &BoxedLoops) {
684   Loop *L = LI.getLoopFor(BB);
685   return getFirstNonBoxedLoopFor(L, LI, BoxedLoops);
686 }
687 
isDebugCall(Instruction * Inst)688 bool polly::isDebugCall(Instruction *Inst) {
689   auto *CI = dyn_cast<CallInst>(Inst);
690   if (!CI)
691     return false;
692 
693   Function *CF = CI->getCalledFunction();
694   if (!CF)
695     return false;
696 
697   return std::find(DebugFunctions.begin(), DebugFunctions.end(),
698                    CF->getName()) != DebugFunctions.end();
699 }
700 
hasDebugCall(BasicBlock * BB)701 static bool hasDebugCall(BasicBlock *BB) {
702   for (Instruction &Inst : *BB) {
703     if (isDebugCall(&Inst))
704       return true;
705   }
706   return false;
707 }
708 
hasDebugCall(ScopStmt * Stmt)709 bool polly::hasDebugCall(ScopStmt *Stmt) {
710   // Quick skip if no debug functions have been defined.
711   if (DebugFunctions.empty())
712     return false;
713 
714   if (!Stmt)
715     return false;
716 
717   for (Instruction *Inst : Stmt->getInstructions())
718     if (isDebugCall(Inst))
719       return true;
720 
721   if (Stmt->isRegionStmt()) {
722     for (BasicBlock *RBB : Stmt->getRegion()->blocks())
723       if (RBB != Stmt->getEntryBlock() && ::hasDebugCall(RBB))
724         return true;
725   }
726 
727   return false;
728 }
729