1 //===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
3 // This file implements the BasicBlock class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/BasicBlock.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/SymbolTable.h"
14 #include "SymbolTableListTraitsImpl.h"
17 // DummyInst - An instance of this class is used to mark the end of the
18 // instruction list. This is not a real instruction.
20 struct DummyInst : public Instruction {
21 DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {}
23 virtual Instruction *clone() const { assert(0 && "Cannot clone EOL");abort();}
24 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
26 // Methods for support type inquiry through isa, cast, and dyn_cast...
27 static inline bool classof(const DummyInst *) { return true; }
28 static inline bool classof(const Instruction *I) {
29 return I->getOpcode() == NumOtherOps;
31 static inline bool classof(const Value *V) {
32 return isa<Instruction>(V) && classof(cast<Instruction>(V));
36 Instruction *ilist_traits<Instruction>::createNode() {
37 return new DummyInst();
39 iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
40 return BB->getInstList();
43 // Explicit instantiation of SymbolTableListTraits since some of the methods
44 // are not in the public header file...
45 template SymbolTableListTraits<Instruction, BasicBlock, Function>;
48 BasicBlock::BasicBlock(const std::string &name, Function *Parent)
49 : Value(Type::LabelTy, Value::BasicBlockVal, name) {
50 // Initialize the instlist...
51 InstList.setItemParent(this);
54 Parent->getBasicBlockList().push_back(this);
57 BasicBlock::~BasicBlock() {
62 // Specialize setName to take care of symbol table majik
63 void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
65 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
66 "Invalid symtab argument!");
67 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
69 if (P && hasName()) P->getSymbolTable()->insert(this);
72 TerminatorInst *BasicBlock::getTerminator() {
73 if (InstList.empty()) return 0;
74 return dyn_cast<TerminatorInst>(&InstList.back());
77 const TerminatorInst *const BasicBlock::getTerminator() const {
78 if (InstList.empty()) return 0;
79 return dyn_cast<TerminatorInst>(&InstList.back());
82 void BasicBlock::dropAllReferences() {
83 for(iterator I = begin(), E = end(); I != E; ++I)
84 I->dropAllReferences();
87 // hasConstantReferences() - This predicate is true if there is a
88 // reference to this basic block in the constant pool for this method. For
89 // example, if a block is reached through a switch table, that table resides
90 // in the constant pool, and the basic block is reference from it.
92 bool BasicBlock::hasConstantReferences() const {
93 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
94 if (::isa<Constant>((Value*)*I))
100 // removePredecessor - This method is used to notify a BasicBlock that the
101 // specified Predecessor of the block is no longer able to reach it. This is
102 // actually not used to update the Predecessor list, but is actually used to
103 // update the PHI nodes that reside in the block. Note that this should be
104 // called while the predecessor still refers to this block.
106 void BasicBlock::removePredecessor(BasicBlock *Pred) {
107 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
108 "removePredecessor: BB is not a predecessor!");
109 if (!isa<PHINode>(front())) return; // Quick exit.
111 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
114 // Loop over the rest of the predecessors until we run out, or until we find
115 // out that there are more than 2 predecessors.
116 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
118 // If there are exactly two predecessors, then we want to nuke the PHI nodes
119 // altogether. We cannot do this, however if this in this case however:
122 // %x = phi [X, Loop]
123 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
124 // br Loop ;; %x2 does not dominate all uses
126 // This is because the PHI node input is actually taken from the predecessor
127 // basic block. The only case this can happen is with a self loop, so we
128 // check for this case explicitly now.
130 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
132 PI = pred_begin(this);
133 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
135 // Disable PHI elimination!
136 if (this == Other) max_idx = 3;
139 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
140 // Yup, loop through and nuke the PHI nodes
141 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
142 PN->removeIncomingValue(Pred); // Remove the predecessor first...
144 assert(PN->getNumIncomingValues() == max_idx-1 &&
145 "PHI node shouldn't have this many values!!!");
147 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
149 PN->replaceAllUsesWith(PN->getOperand(0));
150 else // Otherwise there are no incoming values/edges, replace with dummy
151 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
152 getInstList().pop_front(); // Remove the PHI node
155 // Okay, now we know that we need to remove predecessor #pred_idx from all
156 // PHI nodes. Iterate over each PHI node fixing them up
157 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(&*II); ++II)
158 PN->removeIncomingValue(Pred);
163 // splitBasicBlock - This splits a basic block into two at the specified
164 // instruction. Note that all instructions BEFORE the specified iterator stay
165 // as part of the original basic block, an unconditional branch is added to
166 // the new BB, and the rest of the instructions in the BB are moved to the new
167 // BB, including the old terminator. This invalidates the iterator.
169 // Note that this only works on well formed basic blocks (must have a
170 // terminator), and 'I' must not be the end of instruction list (which would
171 // cause a degenerate basic block to be formed, having a terminator inside of
174 BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
175 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
176 assert(I != InstList.end() &&
177 "Trying to get me to create degenerate basic block!");
179 BasicBlock *New = new BasicBlock("", getParent());
181 // Go from the end of the basic block through to the iterator pointer, moving
182 // to the new basic block...
183 Instruction *Inst = 0;
185 iterator EndIt = end();
186 Inst = InstList.remove(--EndIt); // Remove from end
187 New->InstList.push_front(Inst); // Add to front
188 } while (Inst != &*I); // Loop until we move the specified instruction.
190 // Add a branch instruction to the newly formed basic block.
191 InstList.push_back(new BranchInst(New));
193 // Now we must loop through all of the successors of the New block (which
194 // _were_ the successors of the 'this' block), and update any PHI nodes in
195 // successors. If there were PHI nodes in the successors, then they need to
196 // know that incoming branches will be from New, not from Old.
198 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
200 // Loop over any phi nodes in the basic block, updating the BB field of
201 // incoming values...
202 BasicBlock *Successor = *I;
203 for (BasicBlock::iterator II = Successor->begin();
204 PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
205 int IDX = PN->getBasicBlockIndex(this);
207 PN->setIncomingBlock((unsigned)IDX, New);
208 IDX = PN->getBasicBlockIndex(this);