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" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 16 | |
| 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/Support/CFG.h" |
| 22 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 23 | |
| 24 | #define DEBUG_TYPE "polly-scop-helper" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | |
| 27 | using namespace llvm; |
| 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 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 70 | bool polly::hasInvokeEdge(const PHINode *PN) { |
| 71 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) |
| 72 | if (InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i))) |
| 73 | if (II->getParent() == PN->getIncomingBlock(i)) |
| 74 | return true; |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 79 | BasicBlock *polly::createSingleExitEdge(Region *R, Pass *P) { |
| 80 | BasicBlock *BB = R->getExit(); |
| 81 | |
Tobias Grosser | d535f55 | 2013-02-14 16:42:45 +0000 | [diff] [blame] | 82 | SmallVector<BasicBlock *, 4> Preds; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 83 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) |
| 84 | if (R->contains(*PI)) |
| 85 | Preds.push_back(*PI); |
| 86 | |
Benjamin Kramer | 66af99e | 2011-12-09 21:34:43 +0000 | [diff] [blame] | 87 | return SplitBlockPredecessors(BB, Preds, ".region", P); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame^] | 90 | void polly::simplifyRegion(Scop *S, Pass *P) { |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 91 | Region *R = &S->getRegion(); |
| 92 | |
| 93 | // Create single entry edge if the region has multiple entry edges. |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame^] | 94 | if (!R->getEnteringBlock()) { |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 95 | BasicBlock *OldEntry = R->getEntry(); |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame^] | 96 | BasicBlock *NewEntry = SplitBlock(OldEntry, OldEntry->begin(), P); |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 97 | |
| 98 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) |
| 99 | if ((*SI)->getBasicBlock() == OldEntry) { |
| 100 | (*SI)->setBasicBlock(NewEntry); |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | R->replaceEntryRecursive(NewEntry); |
| 105 | } |
| 106 | |
| 107 | // Create single exit edge if the region has multiple exit edges. |
| 108 | if (!R->getExitingBlock()) { |
| 109 | BasicBlock *NewExit = createSingleExitEdge(R, P); |
| 110 | |
| 111 | for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) |
| 112 | (*RI)->replaceExitRecursive(NewExit); |
| 113 | } |
| 114 | } |
| 115 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 116 | void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) { |
| 117 | // Find first non-alloca instruction. Every basic block has a non-alloc |
| 118 | // instruction, as every well formed basic block has a terminator. |
| 119 | BasicBlock::iterator I = EntryBlock->begin(); |
Tobias Grosser | d535f55 | 2013-02-14 16:42:45 +0000 | [diff] [blame] | 120 | while (isa<AllocaInst>(I)) |
| 121 | ++I; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 122 | |
| 123 | // SplitBlock updates DT, DF and LI. |
| 124 | BasicBlock *NewEntry = SplitBlock(EntryBlock, I, P); |
| 125 | if (RegionInfo *RI = P->getAnalysisIfAvailable<RegionInfo>()) |
| 126 | RI->splitBlock(NewEntry, EntryBlock); |
| 127 | } |