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