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"
10 #include "llvm/Support/CFG.h"
11 #include "llvm/Constant.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>((Value*)*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
104 // altogether. We cannot do this, however if this in this case however:
107 // %x = phi [X, Loop]
108 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
109 // br Loop ;; %x2 does not dominate all uses
111 // This is because the PHI node input is actually taken from the predecessor
112 // basic block. The only case this can happen is with a self loop, so we
113 // check for this case explicitly now.
115 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
117 PI = pred_begin(this);
118 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
120 // Disable PHI elimination!
121 if (this == Other) max_idx = 3;
124 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
125 // Yup, loop through and nuke the PHI nodes
126 while (PHINode *PN = dyn_cast<PHINode>(front())) {
127 PN->removeIncomingValue(Pred); // Remove the predecessor first...
129 assert(PN->getNumIncomingValues() == max_idx-1 &&
130 "PHI node shouldn't have this many values!!!");
132 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
134 PN->replaceAllUsesWith(PN->getOperand(0));
135 else // Otherwise there are no incoming values/edges, replace with dummy
136 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
137 delete getInstList().remove(begin()); // Remove the PHI node
140 // Okay, now we know that we need to remove predecessor #pred_idx from all
141 // PHI nodes. Iterate over each PHI node fixing them up
142 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(*II); ++II)
143 PN->removeIncomingValue(Pred);
148 // splitBasicBlock - This splits a basic block into two at the specified
149 // instruction. Note that all instructions BEFORE the specified iterator stay
150 // as part of the original basic block, an unconditional branch is added to
151 // the new BB, and the rest of the instructions in the BB are moved to the new
152 // BB, including the old terminator. This invalidates the iterator.
154 // Note that this only works on well formed basic blocks (must have a
155 // terminator), and 'I' must not be the end of instruction list (which would
156 // cause a degenerate basic block to be formed, having a terminator inside of
159 BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
160 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
161 assert(I != InstList.end() &&
162 "Trying to get me to create degenerate basic block!");
164 BasicBlock *New = new BasicBlock("", getParent());
166 // Go from the end of the basic block through to the iterator pointer, moving
167 // to the new basic block...
168 Instruction *Inst = 0;
170 iterator EndIt = end();
171 Inst = InstList.remove(--EndIt); // Remove from end
172 New->InstList.push_front(Inst); // Add to front
173 } while (Inst != *I); // Loop until we move the specified instruction.
175 // Add a branch instruction to the newly formed basic block.
176 InstList.push_back(new BranchInst(New));
178 // Now we must loop through all of the successors of the New block (which
179 // _were_ the successors of the 'this' block), and update any PHI nodes in
180 // successors. If there were PHI nodes in the successors, then they need to
181 // know that incoming branches will be from New, not from Old.
183 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
185 // Loop over any phi nodes in the basic block, updating the BB field of
186 // incoming values...
187 BasicBlock *Successor = *I;
188 for (BasicBlock::iterator II = Successor->begin();
189 PHINode *PN = dyn_cast<PHINode>(*II); ++II) {
190 int IDX = PN->getBasicBlockIndex(this);
192 PN->setIncomingBlock((unsigned)IDX, New);
193 IDX = PN->getBasicBlockIndex(this);