1 //===- ScopHelper.cpp - Some Helper Functions for Scop.  ------------------===//
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 // Small functions that help with Scop and LLVM-IR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "polly/Support/ScopHelper.h"
15 #include "polly/Options.h"
16 #include "polly/ScopInfo.h"
17 #include "polly/Support/SCEVValidator.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/RegionInfo.h"
20 #include "llvm/Analysis/ScalarEvolution.h"
21 #include "llvm/Analysis/ScalarEvolutionExpander.h"
22 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 
28 using namespace llvm;
29 using namespace polly;
30 
31 #define DEBUG_TYPE "polly-scop-helper"
32 
33 static cl::opt<bool> PollyAllowErrorBlocks(
34     "polly-allow-error-blocks",
35     cl::desc("Allow to speculate on the execution of 'error blocks'."),
36     cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
37 
38 bool polly::hasInvokeEdge(const PHINode *PN) {
39   for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
40     if (InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)))
41       if (II->getParent() == PN->getIncomingBlock(i))
42         return true;
43 
44   return false;
45 }
46 
47 // Ensures that there is just one predecessor to the entry node from outside the
48 // region.
49 // The identity of the region entry node is preserved.
50 static void simplifyRegionEntry(Region *R, DominatorTree *DT, LoopInfo *LI,
51                                 RegionInfo *RI) {
52   BasicBlock *EnteringBB = R->getEnteringBlock();
53   BasicBlock *Entry = R->getEntry();
54 
55   // Before (one of):
56   //
57   //                       \    /            //
58   //                      EnteringBB         //
59   //                        |    \------>    //
60   //   \   /                |                //
61   //   Entry <--\         Entry <--\         //
62   //   /   \    /         /   \    /         //
63   //        ....               ....          //
64 
65   // Create single entry edge if the region has multiple entry edges.
66   if (!EnteringBB) {
67     SmallVector<BasicBlock *, 4> Preds;
68     for (BasicBlock *P : predecessors(Entry))
69       if (!R->contains(P))
70         Preds.push_back(P);
71 
72     BasicBlock *NewEntering =
73         SplitBlockPredecessors(Entry, Preds, ".region_entering", DT, LI);
74 
75     if (RI) {
76       // The exit block of predecessing regions must be changed to NewEntering
77       for (BasicBlock *ExitPred : predecessors(NewEntering)) {
78         Region *RegionOfPred = RI->getRegionFor(ExitPred);
79         if (RegionOfPred->getExit() != Entry)
80           continue;
81 
82         while (!RegionOfPred->isTopLevelRegion() &&
83                RegionOfPred->getExit() == Entry) {
84           RegionOfPred->replaceExit(NewEntering);
85           RegionOfPred = RegionOfPred->getParent();
86         }
87       }
88 
89       // Make all ancestors use EnteringBB as entry; there might be edges to it
90       Region *AncestorR = R->getParent();
91       RI->setRegionFor(NewEntering, AncestorR);
92       while (!AncestorR->isTopLevelRegion() && AncestorR->getEntry() == Entry) {
93         AncestorR->replaceEntry(NewEntering);
94         AncestorR = AncestorR->getParent();
95       }
96     }
97 
98     EnteringBB = NewEntering;
99   }
100   assert(R->getEnteringBlock() == EnteringBB);
101 
102   // After:
103   //
104   //    \    /       //
105   //  EnteringBB     //
106   //      |          //
107   //      |          //
108   //    Entry <--\   //
109   //    /   \    /   //
110   //         ....    //
111 }
112 
113 // Ensure that the region has a single block that branches to the exit node.
114 static void simplifyRegionExit(Region *R, DominatorTree *DT, LoopInfo *LI,
115                                RegionInfo *RI) {
116   BasicBlock *ExitBB = R->getExit();
117   BasicBlock *ExitingBB = R->getExitingBlock();
118 
119   // Before:
120   //
121   //   (Region)   ______/  //
122   //      \  |   /         //
123   //       ExitBB          //
124   //       /    \          //
125 
126   if (!ExitingBB) {
127     SmallVector<BasicBlock *, 4> Preds;
128     for (BasicBlock *P : predecessors(ExitBB))
129       if (R->contains(P))
130         Preds.push_back(P);
131 
132     //  Preds[0] Preds[1]      otherBB //
133     //         \  |  ________/         //
134     //          \ | /                  //
135     //           BB                    //
136     ExitingBB =
137         SplitBlockPredecessors(ExitBB, Preds, ".region_exiting", DT, LI);
138     // Preds[0] Preds[1]      otherBB  //
139     //        \  /           /         //
140     // BB.region_exiting    /          //
141     //                  \  /           //
142     //                   BB            //
143 
144     if (RI)
145       RI->setRegionFor(ExitingBB, R);
146 
147     // Change the exit of nested regions, but not the region itself,
148     R->replaceExitRecursive(ExitingBB);
149     R->replaceExit(ExitBB);
150   }
151   assert(ExitingBB == R->getExitingBlock());
152 
153   // After:
154   //
155   //     \   /                //
156   //    ExitingBB     _____/  //
157   //          \      /        //
158   //           ExitBB         //
159   //           /    \         //
160 }
161 
162 void polly::simplifyRegion(Region *R, DominatorTree *DT, LoopInfo *LI,
163                            RegionInfo *RI) {
164   assert(R && !R->isTopLevelRegion());
165   assert(!RI || RI == R->getRegionInfo());
166   assert((!RI || DT) &&
167          "RegionInfo requires DominatorTree to be updated as well");
168 
169   simplifyRegionEntry(R, DT, LI, RI);
170   simplifyRegionExit(R, DT, LI, RI);
171   assert(R->isSimple());
172 }
173 
174 // Split the block into two successive blocks.
175 //
176 // Like llvm::SplitBlock, but also preserves RegionInfo
177 static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
178                               DominatorTree *DT, llvm::LoopInfo *LI,
179                               RegionInfo *RI) {
180   assert(Old && SplitPt);
181 
182   // Before:
183   //
184   //  \   /  //
185   //   Old   //
186   //  /   \  //
187 
188   BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
189 
190   if (RI) {
191     Region *R = RI->getRegionFor(Old);
192     RI->setRegionFor(NewBlock, R);
193   }
194 
195   // After:
196   //
197   //   \   /    //
198   //    Old     //
199   //     |      //
200   //  NewBlock  //
201   //   /   \    //
202 
203   return NewBlock;
204 }
205 
206 void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
207   // Find first non-alloca instruction. Every basic block has a non-alloc
208   // instruction, as every well formed basic block has a terminator.
209   BasicBlock::iterator I = EntryBlock->begin();
210   while (isa<AllocaInst>(I))
211     ++I;
212 
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   splitBlock(EntryBlock, &*I, 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 private:
249   SCEVExpander Expander;
250   ScalarEvolution &SE;
251   const char *Name;
252   const Region &R;
253   ValueMapT *VMap;
254   BasicBlock *RTCBB;
255 
256   const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
257                                Instruction *IP) {
258     if (!Inst || !R.contains(Inst))
259       return E;
260 
261     assert(!Inst->mayThrow() && !Inst->mayReadOrWriteMemory() &&
262            !isa<PHINode>(Inst));
263 
264     auto *InstClone = Inst->clone();
265     for (auto &Op : Inst->operands()) {
266       assert(SE.isSCEVable(Op->getType()));
267       auto *OpSCEV = SE.getSCEV(Op);
268       auto *OpClone = expandCodeFor(OpSCEV, Op->getType(), IP);
269       InstClone->replaceUsesOfWith(Op, OpClone);
270     }
271 
272     InstClone->setName(Name + Inst->getName());
273     InstClone->insertBefore(IP);
274     return SE.getSCEV(InstClone);
275   }
276 
277   const SCEV *visitUnknown(const SCEVUnknown *E) {
278 
279     // If a value mapping was given try if the underlying value is remapped.
280     Value *NewVal = VMap ? VMap->lookup(E->getValue()) : nullptr;
281     if (NewVal) {
282       auto *NewE = SE.getSCEV(NewVal);
283 
284       // While the mapped value might be different the SCEV representation might
285       // not be. To this end we will check before we go into recursion here.
286       if (E != NewE)
287         return visit(NewE);
288     }
289 
290     Instruction *Inst = dyn_cast<Instruction>(E->getValue());
291     Instruction *IP;
292     if (Inst && !R.contains(Inst))
293       IP = Inst;
294     else if (Inst && RTCBB->getParent() == Inst->getFunction())
295       IP = RTCBB->getTerminator();
296     else
297       IP = RTCBB->getParent()->getEntryBlock().getTerminator();
298 
299     if (!Inst ||
300         (Inst->getOpcode() != Instruction::SRem &&
301          Inst->getOpcode() != Instruction::SDiv))
302       return visitGenericInst(E, Inst, IP);
303 
304     const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
305     const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
306 
307     if (!SE.isKnownNonZero(RHSScev))
308       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
309 
310     Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
311     Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
312 
313     Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
314                                   LHS, RHS, Inst->getName() + Name, IP);
315     return SE.getSCEV(Inst);
316   }
317 
318   /// The following functions will just traverse the SCEV and rebuild it with
319   /// the new operands returned by the traversal.
320   ///
321   ///{
322   const SCEV *visitConstant(const SCEVConstant *E) { return E; }
323   const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
324     return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
325   }
326   const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
327     return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
328   }
329   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
330     return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
331   }
332   const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
333     auto *RHSScev = visit(E->getRHS());
334     if (!SE.isKnownNonZero(RHSScev))
335       RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
336     return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
337   }
338   const SCEV *visitAddExpr(const SCEVAddExpr *E) {
339     SmallVector<const SCEV *, 4> NewOps;
340     for (const SCEV *Op : E->operands())
341       NewOps.push_back(visit(Op));
342     return SE.getAddExpr(NewOps);
343   }
344   const SCEV *visitMulExpr(const SCEVMulExpr *E) {
345     SmallVector<const SCEV *, 4> NewOps;
346     for (const SCEV *Op : E->operands())
347       NewOps.push_back(visit(Op));
348     return SE.getMulExpr(NewOps);
349   }
350   const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
351     SmallVector<const SCEV *, 4> NewOps;
352     for (const SCEV *Op : E->operands())
353       NewOps.push_back(visit(Op));
354     return SE.getUMaxExpr(NewOps);
355   }
356   const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
357     SmallVector<const SCEV *, 4> NewOps;
358     for (const SCEV *Op : E->operands())
359       NewOps.push_back(visit(Op));
360     return SE.getSMaxExpr(NewOps);
361   }
362   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
363     SmallVector<const SCEV *, 4> NewOps;
364     for (const SCEV *Op : E->operands())
365       NewOps.push_back(visit(Op));
366     return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
367   }
368   ///}
369 };
370 
371 Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
372                             const char *Name, const SCEV *E, Type *Ty,
373                             Instruction *IP, ValueMapT *VMap,
374                             BasicBlock *RTCBB) {
375   ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap, RTCBB);
376   return Expander.expandCodeFor(E, Ty, IP);
377 }
378 
379 bool polly::isErrorBlock(BasicBlock &BB, const Region &R, LoopInfo &LI,
380                          const DominatorTree &DT) {
381   if (!PollyAllowErrorBlocks)
382     return false;
383 
384   if (isa<UnreachableInst>(BB.getTerminator()))
385     return true;
386 
387   if (LI.isLoopHeader(&BB))
388     return false;
389 
390   // Basic blocks that are always executed are not considered error blocks,
391   // as their execution can not be a rare event.
392   bool DominatesAllPredecessors = true;
393   for (auto Pred : predecessors(R.getExit()))
394     if (R.contains(Pred) && !DT.dominates(&BB, Pred))
395       DominatesAllPredecessors = false;
396 
397   if (DominatesAllPredecessors)
398     return false;
399 
400   // FIXME: This is a simple heuristic to determine if the load is executed
401   //        in a conditional. However, we actually would need the control
402   //        condition, i.e., the post dominance frontier. Alternatively we
403   //        could walk up the dominance tree until we find a block that is
404   //        not post dominated by the load and check if it is a conditional
405   //        or a loop header.
406   auto *DTNode = DT.getNode(&BB);
407   auto *IDomBB = DTNode->getIDom()->getBlock();
408   if (LI.isLoopHeader(IDomBB))
409     return false;
410 
411   for (Instruction &Inst : BB)
412     if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
413       if (isIgnoredIntrinsic(CI))
414         return false;
415 
416       if (!CI->doesNotAccessMemory())
417         return true;
418       if (CI->doesNotReturn())
419         return true;
420     }
421 
422   return false;
423 }
424 
425 Value *polly::getConditionFromTerminator(TerminatorInst *TI) {
426   if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
427     if (BR->isUnconditional())
428       return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
429 
430     return BR->getCondition();
431   }
432 
433   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
434     return SI->getCondition();
435 
436   return nullptr;
437 }
438 
439 bool polly::isHoistableLoad(LoadInst *LInst, Region &R, LoopInfo &LI,
440                             ScalarEvolution &SE, const DominatorTree &DT) {
441   Loop *L = LI.getLoopFor(LInst->getParent());
442   auto *Ptr = LInst->getPointerOperand();
443   const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
444   while (L && R.contains(L)) {
445     if (!SE.isLoopInvariant(PtrSCEV, L))
446       return false;
447     L = L->getParentLoop();
448   }
449 
450   for (auto *User : Ptr->users()) {
451     auto *UserI = dyn_cast<Instruction>(User);
452     if (!UserI || !R.contains(UserI))
453       continue;
454     if (!UserI->mayWriteToMemory())
455       continue;
456 
457     auto &BB = *UserI->getParent();
458     if (DT.dominates(&BB, LInst->getParent()))
459       return false;
460 
461     bool DominatesAllPredecessors = true;
462     for (auto Pred : predecessors(R.getExit()))
463       if (R.contains(Pred) && !DT.dominates(&BB, Pred))
464         DominatesAllPredecessors = false;
465 
466     if (!DominatesAllPredecessors)
467       continue;
468 
469     return false;
470   }
471 
472   return true;
473 }
474 
475 bool polly::isIgnoredIntrinsic(const Value *V) {
476   if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
477     switch (IT->getIntrinsicID()) {
478     // Lifetime markers are supported/ignored.
479     case llvm::Intrinsic::lifetime_start:
480     case llvm::Intrinsic::lifetime_end:
481     // Invariant markers are supported/ignored.
482     case llvm::Intrinsic::invariant_start:
483     case llvm::Intrinsic::invariant_end:
484     // Some misc annotations are supported/ignored.
485     case llvm::Intrinsic::var_annotation:
486     case llvm::Intrinsic::ptr_annotation:
487     case llvm::Intrinsic::annotation:
488     case llvm::Intrinsic::donothing:
489     case llvm::Intrinsic::assume:
490     case llvm::Intrinsic::expect:
491     // Some debug info intrisics are supported/ignored.
492     case llvm::Intrinsic::dbg_value:
493     case llvm::Intrinsic::dbg_declare:
494       return true;
495     default:
496       break;
497     }
498   }
499   return false;
500 }
501 
502 bool polly::canSynthesize(const Value *V, const Scop &S, ScalarEvolution *SE,
503                           Loop *Scope) {
504   if (!V || !SE->isSCEVable(V->getType()))
505     return false;
506 
507   if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope))
508     if (!isa<SCEVCouldNotCompute>(Scev))
509       if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false))
510         return true;
511 
512   return false;
513 }
514 
515 llvm::BasicBlock *polly::getUseBlock(llvm::Use &U) {
516   Instruction *UI = dyn_cast<Instruction>(U.getUser());
517   if (!UI)
518     return nullptr;
519 
520   if (PHINode *PHI = dyn_cast<PHINode>(UI))
521     return PHI->getIncomingBlock(U);
522 
523   return UI->getParent();
524 }
525 
526 std::tuple<std::vector<const SCEV *>, std::vector<int>>
527 polly::getIndexExpressionsFromGEP(GetElementPtrInst *GEP, ScalarEvolution &SE) {
528   std::vector<const SCEV *> Subscripts;
529   std::vector<int> Sizes;
530 
531   Type *Ty = GEP->getPointerOperandType();
532 
533   bool DroppedFirstDim = false;
534 
535   for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
536 
537     const SCEV *Expr = SE.getSCEV(GEP->getOperand(i));
538 
539     if (i == 1) {
540       if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
541         Ty = PtrTy->getElementType();
542       } else if (auto *ArrayTy = dyn_cast<ArrayType>(Ty)) {
543         Ty = ArrayTy->getElementType();
544       } else {
545         Subscripts.clear();
546         Sizes.clear();
547         break;
548       }
549       if (auto *Const = dyn_cast<SCEVConstant>(Expr))
550         if (Const->getValue()->isZero()) {
551           DroppedFirstDim = true;
552           continue;
553         }
554       Subscripts.push_back(Expr);
555       continue;
556     }
557 
558     auto *ArrayTy = dyn_cast<ArrayType>(Ty);
559     if (!ArrayTy) {
560       Subscripts.clear();
561       Sizes.clear();
562       break;
563     }
564 
565     Subscripts.push_back(Expr);
566     if (!(DroppedFirstDim && i == 2))
567       Sizes.push_back(ArrayTy->getNumElements());
568 
569     Ty = ArrayTy->getElementType();
570   }
571 
572   return std::make_tuple(Subscripts, Sizes);
573 }
574 
575 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI,
576                                            const BoxedLoopsSetTy &BoxedLoops) {
577   while (BoxedLoops.count(L))
578     L = L->getParentLoop();
579   return L;
580 }
581 
582 llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::BasicBlock *BB,
583                                            llvm::LoopInfo &LI,
584                                            const BoxedLoopsSetTy &BoxedLoops) {
585   Loop *L = LI.getLoopFor(BB);
586   return getFirstNonBoxedLoopFor(L, LI, BoxedLoops);
587 }
588