blob: 4c480db631c415649168dd9a71741724647abf56 [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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/Method.h"
11#include "llvm/SymbolTable.h"
12#include "llvm/Type.h"
Chris Lattner615d3cf2001-06-29 05:25:23 +000013#include "llvm/CFG.h"
14#include "llvm/iOther.h"
Vikram S. Adve32b5d842001-07-30 18:47:24 +000015#include "llvm/CodeGen/MachineInstr.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//
Chris Lattnerf2a738c2001-07-14 06:13:19 +000020template class ValueHolder<Instruction, BasicBlock, Method>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000021
Chris Lattnerf2a738c2001-07-14 06:13:19 +000022BasicBlock::BasicBlock(const string &name, Method *Parent)
Chris Lattner2d189a52001-09-07 16:44:17 +000023 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
24 machineInstrVec(new MachineCodeForBasicBlock) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +000025 if (Parent)
26 Parent->getBasicBlocks().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000027}
28
29BasicBlock::~BasicBlock() {
30 dropAllReferences();
31 InstList.delete_all();
Vikram S. Adve32b5d842001-07-30 18:47:24 +000032 delete machineInstrVec;
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
35// Specialize setName to take care of symbol table majik
Chris Lattner2d189a52001-09-07 16:44:17 +000036void BasicBlock::setName(const string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000037 Method *P;
Chris Lattner2d189a52001-09-07 16:44:17 +000038 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
39 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
41 Value::setName(name);
42 if (P && hasName()) P->getSymbolTable()->insert(this);
43}
44
45void BasicBlock::setParent(Method *parent) {
46 if (getParent() && hasName())
47 getParent()->getSymbolTable()->remove(this);
48
49 InstList.setParent(parent);
50
51 if (getParent() && hasName())
52 getParent()->getSymbolTableSure()->insert(this);
53}
54
55TerminatorInst *BasicBlock::getTerminator() {
56 if (InstList.empty()) return 0;
57 Instruction *T = InstList.back();
58 if (T->isTerminator()) return (TerminatorInst*)T;
59 return 0;
60}
61
62const TerminatorInst *const BasicBlock::getTerminator() const {
63 if (InstList.empty()) return 0;
64 const Instruction *T = InstList.back();
65 if (T->isTerminator()) return (TerminatorInst*)T;
66 return 0;
67}
68
69void BasicBlock::dropAllReferences() {
70 for_each(InstList.begin(), InstList.end(),
71 std::mem_fun(&Instruction::dropAllReferences));
72}
73
74// hasConstantPoolReferences() - This predicate is true if there is a
75// reference to this basic block in the constant pool for this method. For
76// example, if a block is reached through a switch table, that table resides
77// in the constant pool, and the basic block is reference from it.
78//
79bool BasicBlock::hasConstantPoolReferences() const {
80 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner4cee8d82001-06-27 23:41:11 +000081 if ((*I)->isConstant())
Chris Lattner2f7c9632001-06-06 20:29:01 +000082 return true;
83
84 return false;
85}
86
Chris Lattner615d3cf2001-06-29 05:25:23 +000087// removePredecessor - This method is used to notify a BasicBlock that the
88// specified Predecessor of the block is no longer able to reach it. This is
89// actually not used to update the Predecessor list, but is actually used to
90// update the PHI nodes that reside in the block. Note that this should be
91// called while the predecessor still refers to this block.
92//
93void BasicBlock::removePredecessor(BasicBlock *Pred) {
94 using cfg::pred_begin; using cfg::pred_end; using cfg::pred_iterator;
95 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
96 "removePredecessor: BB is not a predecessor!");
97 if (!front()->isPHINode()) return; // Quick exit.
98
99 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
100 unsigned max_idx;
101
102 // Loop over the rest of the predecessors until we run out, or until we find
103 // out that there are more than 2 predecessors.
104 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
105
106 // If there are exactly two predecessors, then we want to nuke the PHI nodes
107 // altogether.
108 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
109 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
110 while (front()->isPHINode()) { // Yup, loop through and nuke the PHI nodes
111 PHINode *PN = (PHINode*)front();
112 PN->removeIncomingValue(Pred); // Remove the predecessor first...
113
114 assert(PN->getNumIncomingValues() == max_idx-1 &&
115 "PHI node shouldn't have this many values!!!");
116
117 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
118 if (max_idx == 2)
119 PN->replaceAllUsesWith(PN->getOperand(0));
120 delete getInstList().remove(begin()); // Remove the PHI node
121 }
122 } else {
123 // Okay, now we know that we need to remove predecessor #pred_idx from all
124 // PHI nodes. Iterate over each PHI node fixing them up
125 iterator II(begin());
126 for (; (*II)->isPHINode(); ++II) {
127 PHINode *PN = (PHINode*)*II;
128 PN->removeIncomingValue(Pred);
129 }
130 }
131}
132
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133
134// splitBasicBlock - This splits a basic block into two at the specified
135// instruction. Note that all instructions BEFORE the specified iterator stay
136// as part of the original basic block, an unconditional branch is added to
137// the new BB, and the rest of the instructions in the BB are moved to the new
138// BB, including the old terminator. This invalidates the iterator.
139//
140// Note that this only works on well formed basic blocks (must have a
141// terminator), and 'I' must not be the end of instruction list (which would
142// cause a degenerate basic block to be formed, having a terminator inside of
143// the basic block).
144//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000145BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
147 assert(I != InstList.end() &&
148 "Trying to get me to create degenerate basic block!");
149
150 BasicBlock *New = new BasicBlock("", getParent());
151
152 // Go from the end of the basic block through to the iterator pointer, moving
153 // to the new basic block...
154 Instruction *Inst = 0;
155 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000156 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157 Inst = InstList.remove(--EndIt); // Remove from end
158 New->InstList.push_front(Inst); // Add to front
159 } while (Inst != *I); // Loop until we move the specified instruction.
160
161 // Add a branch instruction to the newly formed basic block.
162 InstList.push_back(new BranchInst(New));
163 return New;
164}