blob: 97e2656276eac501d9410a610a7bff5a7692e491 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===- 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 Grosser8edce4e2013-04-16 08:04:42 +000015#include "polly/ScopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000016
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
27using namespace llvm;
28
Tobias Grosser75805372011-04-29 06:27:02 +000029// 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.
34Loop *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 Grosserd535f552013-02-14 16:42:45 +000045 if (!exit)
46 return 0;
Tobias Grosser75805372011-04-29 06:27:02 +000047
48 if (exit != R.getExit()) {
49 // SubRegion/ParentRegion with the same entry.
Tobias Grosserd535f552013-02-14 16:42:45 +000050 assert((R.getNode(R.getEntry())->isSubRegion() ||
51 R.getParent()->getEntry() == entry) &&
52 "Expect the loop is the smaller or bigger region");
Tobias Grosser75805372011-04-29 06:27:02 +000053 return 0;
54 }
55
56 return L;
57}
58
59Value *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 Grosser75805372011-04-29 06:27:02 +000070bool 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 Grosser75805372011-04-29 06:27:02 +000079BasicBlock *polly::createSingleExitEdge(Region *R, Pass *P) {
80 BasicBlock *BB = R->getExit();
81
Tobias Grosserd535f552013-02-14 16:42:45 +000082 SmallVector<BasicBlock *, 4> Preds;
Tobias Grosser75805372011-04-29 06:27:02 +000083 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 Kramer66af99e2011-12-09 21:34:43 +000087 return SplitBlockPredecessors(BB, Preds, ".region", P);
Tobias Grosser75805372011-04-29 06:27:02 +000088}
89
Tobias Grossere602a072013-05-07 07:30:56 +000090void polly::simplifyRegion(Scop *S, Pass *P) {
Tobias Grosser8edce4e2013-04-16 08:04:42 +000091 Region *R = &S->getRegion();
92
93 // Create single entry edge if the region has multiple entry edges.
Tobias Grossere602a072013-05-07 07:30:56 +000094 if (!R->getEnteringBlock()) {
Tobias Grosser8edce4e2013-04-16 08:04:42 +000095 BasicBlock *OldEntry = R->getEntry();
Tobias Grossere602a072013-05-07 07:30:56 +000096 BasicBlock *NewEntry = SplitBlock(OldEntry, OldEntry->begin(), P);
Tobias Grosser8edce4e2013-04-16 08:04:42 +000097
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 Grosser75805372011-04-29 06:27:02 +0000116void 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 Grosserd535f552013-02-14 16:42:45 +0000120 while (isa<AllocaInst>(I))
121 ++I;
Tobias Grosser75805372011-04-29 06:27:02 +0000122
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}