blob: 8fee4eff587a02a110ee67991469b8bcc488b116 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
2//
3// This file implements the Method class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/BasicBlock.h"
9#include "llvm/iTerminators.h"
10#include "llvm/Module.h"
11#include "llvm/Method.h"
12#include "llvm/SymbolTable.h"
13#include "llvm/Type.h"
Chris Lattner615d3cf2001-06-29 05:25:23 +000014#include "llvm/CFG.h"
15#include "llvm/iOther.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
17// Instantiate Templates - This ugliness is the price we have to pay
18// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
19//
20template class ValueHolder<Instruction, BasicBlock>;
21
22BasicBlock::BasicBlock(const string &name, Method *parent)
23 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0) {
24
25 if (parent)
26 parent->getBasicBlocks().push_back(this);
27}
28
29BasicBlock::~BasicBlock() {
30 dropAllReferences();
31 InstList.delete_all();
32}
33
34// Specialize setName to take care of symbol table majik
35void BasicBlock::setName(const string &name) {
36 Method *P;
37 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
38 Value::setName(name);
39 if (P && hasName()) P->getSymbolTable()->insert(this);
40}
41
42void BasicBlock::setParent(Method *parent) {
43 if (getParent() && hasName())
44 getParent()->getSymbolTable()->remove(this);
45
46 InstList.setParent(parent);
47
48 if (getParent() && hasName())
49 getParent()->getSymbolTableSure()->insert(this);
50}
51
52TerminatorInst *BasicBlock::getTerminator() {
53 if (InstList.empty()) return 0;
54 Instruction *T = InstList.back();
55 if (T->isTerminator()) return (TerminatorInst*)T;
56 return 0;
57}
58
59const TerminatorInst *const BasicBlock::getTerminator() const {
60 if (InstList.empty()) return 0;
61 const Instruction *T = InstList.back();
62 if (T->isTerminator()) return (TerminatorInst*)T;
63 return 0;
64}
65
66void BasicBlock::dropAllReferences() {
67 for_each(InstList.begin(), InstList.end(),
68 std::mem_fun(&Instruction::dropAllReferences));
69}
70
71// hasConstantPoolReferences() - This predicate is true if there is a
72// reference to this basic block in the constant pool for this method. For
73// example, if a block is reached through a switch table, that table resides
74// in the constant pool, and the basic block is reference from it.
75//
76bool BasicBlock::hasConstantPoolReferences() const {
77 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner4cee8d82001-06-27 23:41:11 +000078 if ((*I)->isConstant())
Chris Lattner2f7c9632001-06-06 20:29:01 +000079 return true;
80
81 return false;
82}
83
Chris Lattner615d3cf2001-06-29 05:25:23 +000084// removePredecessor - This method is used to notify a BasicBlock that the
85// specified Predecessor of the block is no longer able to reach it. This is
86// actually not used to update the Predecessor list, but is actually used to
87// update the PHI nodes that reside in the block. Note that this should be
88// called while the predecessor still refers to this block.
89//
90void BasicBlock::removePredecessor(BasicBlock *Pred) {
91 using cfg::pred_begin; using cfg::pred_end; using cfg::pred_iterator;
92 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
93 "removePredecessor: BB is not a predecessor!");
94 if (!front()->isPHINode()) return; // Quick exit.
95
96 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
97 unsigned max_idx;
98
99 // Loop over the rest of the predecessors until we run out, or until we find
100 // out that there are more than 2 predecessors.
101 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
102
103 // If there are exactly two predecessors, then we want to nuke the PHI nodes
104 // altogether.
105 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
106 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
107 while (front()->isPHINode()) { // Yup, loop through and nuke the PHI nodes
108 PHINode *PN = (PHINode*)front();
109 PN->removeIncomingValue(Pred); // Remove the predecessor first...
110
111 assert(PN->getNumIncomingValues() == max_idx-1 &&
112 "PHI node shouldn't have this many values!!!");
113
114 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
115 if (max_idx == 2)
116 PN->replaceAllUsesWith(PN->getOperand(0));
117 delete getInstList().remove(begin()); // Remove the PHI node
118 }
119 } else {
120 // Okay, now we know that we need to remove predecessor #pred_idx from all
121 // PHI nodes. Iterate over each PHI node fixing them up
122 iterator II(begin());
123 for (; (*II)->isPHINode(); ++II) {
124 PHINode *PN = (PHINode*)*II;
125 PN->removeIncomingValue(Pred);
126 }
127 }
128}
129
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130
131// splitBasicBlock - This splits a basic block into two at the specified
132// instruction. Note that all instructions BEFORE the specified iterator stay
133// as part of the original basic block, an unconditional branch is added to
134// the new BB, and the rest of the instructions in the BB are moved to the new
135// BB, including the old terminator. This invalidates the iterator.
136//
137// Note that this only works on well formed basic blocks (must have a
138// terminator), and 'I' must not be the end of instruction list (which would
139// cause a degenerate basic block to be formed, having a terminator inside of
140// the basic block).
141//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000142BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
144 assert(I != InstList.end() &&
145 "Trying to get me to create degenerate basic block!");
146
147 BasicBlock *New = new BasicBlock("", getParent());
148
149 // Go from the end of the basic block through to the iterator pointer, moving
150 // to the new basic block...
151 Instruction *Inst = 0;
152 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000153 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000154 Inst = InstList.remove(--EndIt); // Remove from end
155 New->InstList.push_front(Inst); // Add to front
156 } while (Inst != *I); // Loop until we move the specified instruction.
157
158 // Add a branch instruction to the newly formed basic block.
159 InstList.push_back(new BranchInst(New));
160 return New;
161}