1 //===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
3 // This file implements the BasicBlock class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "ValueHolderImpl.h"
8 #include "llvm/iTerminators.h"
9 #include "llvm/SymbolTable.h"
10 #include "llvm/Type.h"
11 #include "llvm/Support/CFG.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/CodeGen/MachineInstr.h"
15 // Instantiate Templates - This ugliness is the price we have to pay
16 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
18 template class ValueHolder<Instruction, BasicBlock, Function>;
20 BasicBlock::BasicBlock(const std::string &name, Function *Parent)
21 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
22 machineInstrVec(new MachineCodeForBasicBlock) {
24 Parent->getBasicBlocks().push_back(this);
27 BasicBlock::~BasicBlock() {
29 InstList.delete_all();
30 delete machineInstrVec;
33 // Specialize setName to take care of symbol table majik
34 void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
36 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37 "Invalid symtab argument!");
38 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
40 if (P && hasName()) P->getSymbolTable()->insert(this);
43 void BasicBlock::setParent(Function *parent) {
44 if (getParent() && hasName())
45 getParent()->getSymbolTable()->remove(this);
47 InstList.setParent(parent);
49 if (getParent() && hasName())
50 getParent()->getSymbolTableSure()->insert(this);
53 TerminatorInst *BasicBlock::getTerminator() {
54 if (InstList.empty()) return 0;
55 Instruction *T = InstList.back();
56 if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T);
60 const TerminatorInst *const BasicBlock::getTerminator() const {
61 if (InstList.empty()) return 0;
62 if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back()))
67 void BasicBlock::dropAllReferences() {
68 for_each(InstList.begin(), InstList.end(),
69 std::mem_fun(&Instruction::dropAllReferences));
72 // hasConstantReferences() - This predicate is true if there is a
73 // reference to this basic block in the constant pool for this method. For
74 // example, if a block is reached through a switch table, that table resides
75 // in the constant pool, and the basic block is reference from it.
77 bool BasicBlock::hasConstantReferences() const {
78 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
79 if (::isa<Constant>(*I))
85 // removePredecessor - This method is used to notify a BasicBlock that the
86 // specified Predecessor of the block is no longer able to reach it. This is
87 // actually not used to update the Predecessor list, but is actually used to
88 // update the PHI nodes that reside in the block. Note that this should be
89 // called while the predecessor still refers to this block.
91 void BasicBlock::removePredecessor(BasicBlock *Pred) {
92 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
93 "removePredecessor: BB is not a predecessor!");
94 if (!isa<PHINode>(front())) return; // Quick exit.
96 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
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*/;
103 // If there are exactly two predecessors, then we want to nuke the PHI nodes
105 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
106 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
107 // Yup, loop through and nuke the PHI nodes
108 while (PHINode *PN = dyn_cast<PHINode>(front())) {
109 PN->removeIncomingValue(Pred); // Remove the predecessor first...
111 assert(PN->getNumIncomingValues() == max_idx-1 &&
112 "PHI node shouldn't have this many values!!!");
114 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
116 PN->replaceAllUsesWith(PN->getOperand(0));
117 delete getInstList().remove(begin()); // Remove the PHI node
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 (; isa<PHINode>(*II); ++II)
124 cast<PHINode>(*II)->removeIncomingValue(Pred);
129 // splitBasicBlock - This splits a basic block into two at the specified
130 // instruction. Note that all instructions BEFORE the specified iterator stay
131 // as part of the original basic block, an unconditional branch is added to
132 // the new BB, and the rest of the instructions in the BB are moved to the new
133 // BB, including the old terminator. This invalidates the iterator.
135 // Note that this only works on well formed basic blocks (must have a
136 // terminator), and 'I' must not be the end of instruction list (which would
137 // cause a degenerate basic block to be formed, having a terminator inside of
140 BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
141 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
142 assert(I != InstList.end() &&
143 "Trying to get me to create degenerate basic block!");
145 BasicBlock *New = new BasicBlock("", getParent());
147 // Go from the end of the basic block through to the iterator pointer, moving
148 // to the new basic block...
149 Instruction *Inst = 0;
151 iterator EndIt = end();
152 Inst = InstList.remove(--EndIt); // Remove from end
153 New->InstList.push_front(Inst); // Add to front
154 } while (Inst != *I); // Loop until we move the specified instruction.
156 // Add a branch instruction to the newly formed basic block.
157 InstList.push_back(new BranchInst(New));
159 // Now we must loop through all of the successors of the New block (which
160 // _were_ the successors of the 'this' block), and update any PHI nodes in
161 // successors. If there were PHI nodes in the successors, then they need to
162 // know that incoming branches will be from New, not from Old.
164 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
166 // Loop over any phi nodes in the basic block, updating the BB field of
167 // incoming values...
168 BasicBlock *Successor = *I;
169 for (BasicBlock::iterator II = Successor->begin();
170 PHINode *PN = dyn_cast<PHINode>(*II); ++II) {
171 int IDX = PN->getBasicBlockIndex(this);
173 PN->setIncomingBlock((unsigned)IDX, New);
174 IDX = PN->getBasicBlockIndex(this);