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/ScopInfo.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/RegionInfo.h"
18 #include "llvm/Analysis/ScalarEvolution.h"
19 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
20 #include "llvm/IR/CFG.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "polly-scop-helper"
27 
28 // Helper function for Scop
29 // TODO: Add assertion to not allow parameter to be null
30 //===----------------------------------------------------------------------===//
31 // Temporary Hack for extended region tree.
32 // Cast the region to loop if there is a loop have the same header and exit.
33 Loop *polly::castToLoop(const Region &R, LoopInfo &LI) {
34   BasicBlock *entry = R.getEntry();
35 
36   if (!LI.isLoopHeader(entry))
37     return 0;
38 
39   Loop *L = LI.getLoopFor(entry);
40 
41   BasicBlock *exit = L->getExitBlock();
42 
43   // Is the loop with multiple exits?
44   if (!exit)
45     return 0;
46 
47   if (exit != R.getExit()) {
48     // SubRegion/ParentRegion with the same entry.
49     assert((R.getNode(R.getEntry())->isSubRegion() ||
50             R.getParent()->getEntry() == entry) &&
51            "Expect the loop is the smaller or bigger region");
52     return 0;
53   }
54 
55   return L;
56 }
57 
58 Value *polly::getPointerOperand(Instruction &Inst) {
59   if (LoadInst *load = dyn_cast<LoadInst>(&Inst))
60     return load->getPointerOperand();
61   else if (StoreInst *store = dyn_cast<StoreInst>(&Inst))
62     return store->getPointerOperand();
63   else if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(&Inst))
64     return gep->getPointerOperand();
65 
66   return 0;
67 }
68 
69 bool polly::hasInvokeEdge(const PHINode *PN) {
70   for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
71     if (InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)))
72       if (II->getParent() == PN->getIncomingBlock(i))
73         return true;
74 
75   return false;
76 }
77 
78 BasicBlock *polly::createSingleExitEdge(Region *R, Pass *P) {
79   BasicBlock *BB = R->getExit();
80 
81   SmallVector<BasicBlock *, 4> Preds;
82   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
83     if (R->contains(*PI))
84       Preds.push_back(*PI);
85 
86   return SplitBlockPredecessors(BB, Preds, ".region", P);
87 }
88 
89 static void replaceScopAndRegionEntry(polly::Scop *S, BasicBlock *OldEntry,
90                                       BasicBlock *NewEntry) {
91   if (polly::ScopStmt *Stmt = S->getStmtForBasicBlock(OldEntry))
92     Stmt->setBasicBlock(NewEntry);
93 
94   S->getRegion().replaceEntryRecursive(NewEntry);
95 }
96 
97 BasicBlock *polly::simplifyRegion(Scop *S, Pass *P) {
98   Region *R = &S->getRegion();
99 
100   // The entering block for the region.
101   BasicBlock *EnteringBB = R->getEnteringBlock();
102   BasicBlock *OldEntry = R->getEntry();
103   BasicBlock *NewEntry = nullptr;
104 
105   // Create single entry edge if the region has multiple entry edges.
106   if (!EnteringBB) {
107     NewEntry = SplitBlock(OldEntry, OldEntry->begin(), P);
108     EnteringBB = OldEntry;
109   }
110 
111   // Create an unconditional entry edge.
112   if (EnteringBB->getTerminator()->getNumSuccessors() != 1) {
113     BasicBlock *EntryBB = NewEntry ? NewEntry : OldEntry;
114     BasicBlock *SplitEdgeBB = SplitEdge(EnteringBB, EntryBB, P);
115 
116     // Once the edge between EnteringBB and EntryBB is split, two cases arise.
117     // The first is simple. The new block is inserted between EnteringBB and
118     // EntryBB. In this case no further action is needed. However it might
119     // happen (if the splitted edge is not critical) that the new block is
120     // inserted __after__ EntryBB causing the following situation:
121     //
122     // EnteringBB
123     //    _|_
124     //    | |
125     //    |  \-> some_other_BB_not_in_R
126     //    V
127     // EntryBB
128     //    |
129     //    V
130     // SplitEdgeBB
131     //
132     // In this case we need to swap the role of EntryBB and SplitEdgeBB.
133 
134     // Check which case SplitEdge produced:
135     if (SplitEdgeBB->getTerminator()->getSuccessor(0) == EntryBB) {
136       // First (simple) case.
137       EnteringBB = SplitEdgeBB;
138     } else {
139       // Second (complicated) case.
140       NewEntry = SplitEdgeBB;
141       EnteringBB = EntryBB;
142     }
143 
144     EnteringBB->setName("polly.entering.block");
145   }
146 
147   if (NewEntry)
148     replaceScopAndRegionEntry(S, OldEntry, NewEntry);
149 
150   // Create single exit edge if the region has multiple exit edges.
151   if (!R->getExitingBlock()) {
152     BasicBlock *NewExit = createSingleExitEdge(R, P);
153 
154     for (auto &&SubRegion : *R)
155       SubRegion->replaceExitRecursive(NewExit);
156   }
157 
158   return EnteringBB;
159 }
160 
161 void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
162   // Find first non-alloca instruction. Every basic block has a non-alloc
163   // instruction, as every well formed basic block has a terminator.
164   BasicBlock::iterator I = EntryBlock->begin();
165   while (isa<AllocaInst>(I))
166     ++I;
167 
168   // SplitBlock updates DT, DF and LI.
169   BasicBlock *NewEntry = SplitBlock(EntryBlock, I, P);
170   if (RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>())
171     RIP->getRegionInfo().splitBlock(NewEntry, EntryBlock);
172 }
173