blob: 182db20ab1dc95aa691d3aafa99df5ecfc35bf51 [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#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/Analysis/RegionInfo.h"
18#include "llvm/Analysis/ScalarEvolution.h"
19#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthc3478b92014-03-04 11:47:37 +000020#include "llvm/IR/CFG.h"
Chandler Carruth95fef942014-04-22 03:30:19 +000021#include "llvm/Support/Debug.h"
Tobias Grosser75805372011-04-29 06:27:02 +000022#include "llvm/Transforms/Utils/BasicBlockUtils.h"
23
Tobias Grosser75805372011-04-29 06:27:02 +000024using namespace llvm;
25
Chandler Carruth95fef942014-04-22 03:30:19 +000026#define DEBUG_TYPE "polly-scop-helper"
27
Tobias Grosser75805372011-04-29 06:27:02 +000028// 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.
33Loop *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?
Tobias Grosserd535f552013-02-14 16:42:45 +000044 if (!exit)
45 return 0;
Tobias Grosser75805372011-04-29 06:27:02 +000046
47 if (exit != R.getExit()) {
48 // SubRegion/ParentRegion with the same entry.
Tobias Grosserd535f552013-02-14 16:42:45 +000049 assert((R.getNode(R.getEntry())->isSubRegion() ||
50 R.getParent()->getEntry() == entry) &&
51 "Expect the loop is the smaller or bigger region");
Tobias Grosser75805372011-04-29 06:27:02 +000052 return 0;
53 }
54
55 return L;
56}
57
58Value *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
Tobias Grosser75805372011-04-29 06:27:02 +000069bool 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
Tobias Grosser75805372011-04-29 06:27:02 +000078BasicBlock *polly::createSingleExitEdge(Region *R, Pass *P) {
79 BasicBlock *BB = R->getExit();
80
Tobias Grosserd535f552013-02-14 16:42:45 +000081 SmallVector<BasicBlock *, 4> Preds;
Tobias Grosser75805372011-04-29 06:27:02 +000082 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
Benjamin Kramer66af99e2011-12-09 21:34:43 +000086 return SplitBlockPredecessors(BB, Preds, ".region", P);
Tobias Grosser75805372011-04-29 06:27:02 +000087}
88
Tobias Grossere602a072013-05-07 07:30:56 +000089void polly::simplifyRegion(Scop *S, Pass *P) {
Tobias Grosser8edce4e2013-04-16 08:04:42 +000090 Region *R = &S->getRegion();
91
92 // Create single entry edge if the region has multiple entry edges.
Tobias Grossere602a072013-05-07 07:30:56 +000093 if (!R->getEnteringBlock()) {
Tobias Grosser8edce4e2013-04-16 08:04:42 +000094 BasicBlock *OldEntry = R->getEntry();
Tobias Grossere602a072013-05-07 07:30:56 +000095 BasicBlock *NewEntry = SplitBlock(OldEntry, OldEntry->begin(), P);
Tobias Grosser8edce4e2013-04-16 08:04:42 +000096
97 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI)
98 if ((*SI)->getBasicBlock() == OldEntry) {
99 (*SI)->setBasicBlock(NewEntry);
100 break;
101 }
102
103 R->replaceEntryRecursive(NewEntry);
104 }
105
106 // Create single exit edge if the region has multiple exit edges.
107 if (!R->getExitingBlock()) {
108 BasicBlock *NewExit = createSingleExitEdge(R, P);
109
110 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
111 (*RI)->replaceExitRecursive(NewExit);
112 }
113}
114
Tobias Grosser75805372011-04-29 06:27:02 +0000115void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
116 // Find first non-alloca instruction. Every basic block has a non-alloc
117 // instruction, as every well formed basic block has a terminator.
118 BasicBlock::iterator I = EntryBlock->begin();
Tobias Grosserd535f552013-02-14 16:42:45 +0000119 while (isa<AllocaInst>(I))
120 ++I;
Tobias Grosser75805372011-04-29 06:27:02 +0000121
122 // SplitBlock updates DT, DF and LI.
123 BasicBlock *NewEntry = SplitBlock(EntryBlock, I, P);
124 if (RegionInfo *RI = P->getAnalysisIfAvailable<RegionInfo>())
125 RI->splitBlock(NewEntry, EntryBlock);
126}