blob: 446af17be1847fba4a313e34094bac5849f4d271 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
2//
Chris Lattnerf739fa82002-04-08 22:03:57 +00003// This file implements the BasicBlock class for the VMCore library.
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattner113f4f42002-06-25 16:13:24 +00007#include "llvm/BasicBlock.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/iTerminators.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/Type.h"
Chris Lattner83d485b2002-02-12 22:39:50 +000010#include "llvm/Support/CFG.h"
Chris Lattner5cd012d2002-05-23 16:52:34 +000011#include "llvm/Constant.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000012#include "llvm/iPHINode.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000013#include "llvm/SymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000014#include "SymbolTableListTraitsImpl.h"
15#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
Chris Lattner113f4f42002-06-25 16:13:24 +000017// DummyInst - An instance of this class is used to mark the end of the
18// instruction list. This is not a real instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//
Chris Lattner113f4f42002-06-25 16:13:24 +000020struct DummyInst : public Instruction {
21 DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {}
22
23 virtual Instruction *clone() const { assert(0 && "Cannot clone EOL");abort();}
24 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
25
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;
30 }
31 static inline bool classof(const Value *V) {
32 return isa<Instruction>(V) && classof(cast<Instruction>(V));
33 }
34};
35
36Instruction *ilist_traits<Instruction>::createNode() {
37 return new DummyInst();
38}
39iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
40 return BB->getInstList();
41}
42
43// Explicit instantiation of SymbolTableListTraits since some of the methods
44// are not in the public header file...
45template SymbolTableListTraits<Instruction, BasicBlock, Function>;
46
Chris Lattner2f7c9632001-06-06 20:29:01 +000047
Chris Lattnerf739fa82002-04-08 22:03:57 +000048BasicBlock::BasicBlock(const std::string &name, Function *Parent)
Vikram S. Adve6e792fc2002-07-08 22:31:11 +000049 : Value(Type::LabelTy, Value::BasicBlockVal, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000050 // Initialize the instlist...
51 InstList.setItemParent(this);
52
Chris Lattnerf2a738c2001-07-14 06:13:19 +000053 if (Parent)
Chris Lattner113f4f42002-06-25 16:13:24 +000054 Parent->getBasicBlockList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000055}
56
57BasicBlock::~BasicBlock() {
58 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000059 InstList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000060}
61
62// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000063void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +000064 Function *P;
Chris Lattner2d189a52001-09-07 16:44:17 +000065 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
66 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000067 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
68 Value::setName(name);
69 if (P && hasName()) P->getSymbolTable()->insert(this);
70}
71
Chris Lattner2f7c9632001-06-06 20:29:01 +000072TerminatorInst *BasicBlock::getTerminator() {
73 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +000074 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +000075}
76
77const TerminatorInst *const BasicBlock::getTerminator() const {
78 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +000079 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +000080}
81
82void BasicBlock::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +000083 for(iterator I = begin(), E = end(); I != E; ++I)
84 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +000085}
86
Chris Lattner3462ae32001-12-03 22:26:30 +000087// hasConstantReferences() - This predicate is true if there is a
Chris Lattner2f7c9632001-06-06 20:29:01 +000088// 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.
91//
Chris Lattner3462ae32001-12-03 22:26:30 +000092bool BasicBlock::hasConstantReferences() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000093 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattnerca142372002-04-28 19:55:58 +000094 if (::isa<Constant>((Value*)*I))
Chris Lattner2f7c9632001-06-06 20:29:01 +000095 return true;
96
97 return false;
98}
99
Chris Lattner615d3cf2001-06-29 05:25:23 +0000100// 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.
105//
106void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +0000107 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +0000108 "removePredecessor: BB is not a predecessor!");
Chris Lattnerda558102001-10-02 03:41:24 +0000109 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000110
Chris Lattner83d485b2002-02-12 22:39:50 +0000111 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
Chris Lattner615d3cf2001-06-29 05:25:23 +0000112 unsigned max_idx;
113
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*/;
117
118 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerf7373e42002-05-21 19:52:49 +0000119 // altogether. We cannot do this, however if this in this case however:
120 //
121 // Loop:
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
125 //
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.
129 //
Chris Lattner615d3cf2001-06-29 05:25:23 +0000130 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000131 if (max_idx == 2) {
132 PI = pred_begin(this);
133 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
134
135 // Disable PHI elimination!
136 if (this == Other) max_idx = 3;
137 }
138
Chris Lattner615d3cf2001-06-29 05:25:23 +0000139 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000140 // Yup, loop through and nuke the PHI nodes
Chris Lattner113f4f42002-06-25 16:13:24 +0000141 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000142 PN->removeIncomingValue(Pred); // Remove the predecessor first...
143
144 assert(PN->getNumIncomingValues() == max_idx-1 &&
145 "PHI node shouldn't have this many values!!!");
146
147 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
148 if (max_idx == 2)
149 PN->replaceAllUsesWith(PN->getOperand(0));
Chris Lattner5cd012d2002-05-23 16:52:34 +0000150 else // Otherwise there are no incoming values/edges, replace with dummy
151 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner113f4f42002-06-25 16:13:24 +0000152 getInstList().pop_front(); // Remove the PHI node
Chris Lattner615d3cf2001-06-29 05:25:23 +0000153 }
154 } else {
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
Chris Lattner113f4f42002-06-25 16:13:24 +0000157 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(&*II); ++II)
Chris Lattnerf7373e42002-05-21 19:52:49 +0000158 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000159 }
160}
161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162
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.
168//
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
172// the basic block).
173//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000174BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000175 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
176 assert(I != InstList.end() &&
177 "Trying to get me to create degenerate basic block!");
178
179 BasicBlock *New = new BasicBlock("", getParent());
180
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;
184 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000185 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186 Inst = InstList.remove(--EndIt); // Remove from end
187 New->InstList.push_front(Inst); // Add to front
Chris Lattner113f4f42002-06-25 16:13:24 +0000188 } while (Inst != &*I); // Loop until we move the specified instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189
190 // Add a branch instruction to the newly formed basic block.
191 InstList.push_back(new BranchInst(New));
Chris Lattner4eed0b82002-02-25 00:35:07 +0000192
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.
197 //
198 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
199 I != E; ++I) {
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();
Chris Lattner113f4f42002-06-25 16:13:24 +0000204 PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000205 int IDX = PN->getBasicBlockIndex(this);
206 while (IDX != -1) {
207 PN->setIncomingBlock((unsigned)IDX, New);
208 IDX = PN->getBasicBlockIndex(this);
209 }
210 }
211 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212 return New;
213}