blob: bf7191c659328fa5435731abd9cf756c277f0e4a [file] [log] [blame]
Chris Lattner184b2982002-09-08 18:59:35 +00001//===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
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 Lattner184b2982002-09-08 18:59:35 +000014#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000015#include "SymbolTableListTraitsImpl.h"
16#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000017
Chris Lattner113f4f42002-06-25 16:13:24 +000018// DummyInst - An instance of this class is used to mark the end of the
19// instruction list. This is not a real instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +000020//
Chris Lattner113f4f42002-06-25 16:13:24 +000021struct DummyInst : public Instruction {
Chris Lattner184b2982002-09-08 18:59:35 +000022 DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {
23 // This should not be garbage monitored.
24 LeakDetector::removeGarbageObject(this);
25 }
Chris Lattner113f4f42002-06-25 16:13:24 +000026
Chris Lattnerc678c172002-07-25 15:39:05 +000027 virtual Instruction *clone() const {
28 assert(0 && "Cannot clone EOL");abort();
29 return 0;
30 }
Chris Lattner113f4f42002-06-25 16:13:24 +000031 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
32
33 // Methods for support type inquiry through isa, cast, and dyn_cast...
34 static inline bool classof(const DummyInst *) { return true; }
35 static inline bool classof(const Instruction *I) {
36 return I->getOpcode() == NumOtherOps;
37 }
38 static inline bool classof(const Value *V) {
39 return isa<Instruction>(V) && classof(cast<Instruction>(V));
40 }
41};
42
43Instruction *ilist_traits<Instruction>::createNode() {
44 return new DummyInst();
45}
46iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
47 return BB->getInstList();
48}
49
50// Explicit instantiation of SymbolTableListTraits since some of the methods
51// are not in the public header file...
52template SymbolTableListTraits<Instruction, BasicBlock, Function>;
53
Chris Lattner2f7c9632001-06-06 20:29:01 +000054
Chris Lattner9ed7aef2002-09-06 21:33:15 +000055// BasicBlock ctor - If the function parameter is specified, the basic block is
56// automatically inserted at the end of the function.
57//
Chris Lattnerf739fa82002-04-08 22:03:57 +000058BasicBlock::BasicBlock(const std::string &name, Function *Parent)
Vikram S. Adve6e792fc2002-07-08 22:31:11 +000059 : Value(Type::LabelTy, Value::BasicBlockVal, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000060 // Initialize the instlist...
61 InstList.setItemParent(this);
62
Chris Lattner184b2982002-09-08 18:59:35 +000063 // Make sure that we get added to a function
64 LeakDetector::addGarbageObject(this);
65
Chris Lattnerf2a738c2001-07-14 06:13:19 +000066 if (Parent)
Chris Lattner113f4f42002-06-25 16:13:24 +000067 Parent->getBasicBlockList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000068}
69
70BasicBlock::~BasicBlock() {
71 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000072 InstList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000073}
74
Chris Lattner9ed7aef2002-09-06 21:33:15 +000075void BasicBlock::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000076 if (getParent())
77 LeakDetector::addGarbageObject(this);
78
Chris Lattner9ed7aef2002-09-06 21:33:15 +000079 InstList.setParent(parent);
Chris Lattner184b2982002-09-08 18:59:35 +000080
81 if (getParent())
82 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000083}
84
Chris Lattner2f7c9632001-06-06 20:29:01 +000085// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000086void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +000087 Function *P;
Chris Lattner2d189a52001-09-07 16:44:17 +000088 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
89 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000090 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
91 Value::setName(name);
92 if (P && hasName()) P->getSymbolTable()->insert(this);
93}
94
Chris Lattner2f7c9632001-06-06 20:29:01 +000095TerminatorInst *BasicBlock::getTerminator() {
96 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +000097 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +000098}
99
100const TerminatorInst *const BasicBlock::getTerminator() const {
101 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103}
104
105void BasicBlock::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 for(iterator I = begin(), E = end(); I != E; ++I)
107 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108}
109
Chris Lattner3462ae32001-12-03 22:26:30 +0000110// hasConstantReferences() - This predicate is true if there is a
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111// reference to this basic block in the constant pool for this method. For
112// example, if a block is reached through a switch table, that table resides
113// in the constant pool, and the basic block is reference from it.
114//
Chris Lattner3462ae32001-12-03 22:26:30 +0000115bool BasicBlock::hasConstantReferences() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattnerca142372002-04-28 19:55:58 +0000117 if (::isa<Constant>((Value*)*I))
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118 return true;
119
120 return false;
121}
122
Chris Lattner615d3cf2001-06-29 05:25:23 +0000123// removePredecessor - This method is used to notify a BasicBlock that the
124// specified Predecessor of the block is no longer able to reach it. This is
125// actually not used to update the Predecessor list, but is actually used to
126// update the PHI nodes that reside in the block. Note that this should be
127// called while the predecessor still refers to this block.
128//
129void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +0000130 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +0000131 "removePredecessor: BB is not a predecessor!");
Chris Lattnerda558102001-10-02 03:41:24 +0000132 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000133
Chris Lattner83d485b2002-02-12 22:39:50 +0000134 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
Chris Lattner615d3cf2001-06-29 05:25:23 +0000135 unsigned max_idx;
136
137 // Loop over the rest of the predecessors until we run out, or until we find
138 // out that there are more than 2 predecessors.
139 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
140
141 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerf7373e42002-05-21 19:52:49 +0000142 // altogether. We cannot do this, however if this in this case however:
143 //
144 // Loop:
145 // %x = phi [X, Loop]
146 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
147 // br Loop ;; %x2 does not dominate all uses
148 //
149 // This is because the PHI node input is actually taken from the predecessor
150 // basic block. The only case this can happen is with a self loop, so we
151 // check for this case explicitly now.
152 //
Chris Lattner615d3cf2001-06-29 05:25:23 +0000153 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000154 if (max_idx == 2) {
155 PI = pred_begin(this);
156 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
157
158 // Disable PHI elimination!
159 if (this == Other) max_idx = 3;
160 }
161
Chris Lattner615d3cf2001-06-29 05:25:23 +0000162 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000163 // Yup, loop through and nuke the PHI nodes
Chris Lattner113f4f42002-06-25 16:13:24 +0000164 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000165 PN->removeIncomingValue(Pred); // Remove the predecessor first...
166
167 assert(PN->getNumIncomingValues() == max_idx-1 &&
168 "PHI node shouldn't have this many values!!!");
169
170 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
171 if (max_idx == 2)
172 PN->replaceAllUsesWith(PN->getOperand(0));
Chris Lattner5cd012d2002-05-23 16:52:34 +0000173 else // Otherwise there are no incoming values/edges, replace with dummy
174 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner113f4f42002-06-25 16:13:24 +0000175 getInstList().pop_front(); // Remove the PHI node
Chris Lattner615d3cf2001-06-29 05:25:23 +0000176 }
177 } else {
178 // Okay, now we know that we need to remove predecessor #pred_idx from all
179 // PHI nodes. Iterate over each PHI node fixing them up
Chris Lattner113f4f42002-06-25 16:13:24 +0000180 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(&*II); ++II)
Chris Lattnerf7373e42002-05-21 19:52:49 +0000181 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000182 }
183}
184
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185
186// splitBasicBlock - This splits a basic block into two at the specified
187// instruction. Note that all instructions BEFORE the specified iterator stay
188// as part of the original basic block, an unconditional branch is added to
189// the new BB, and the rest of the instructions in the BB are moved to the new
190// BB, including the old terminator. This invalidates the iterator.
191//
192// Note that this only works on well formed basic blocks (must have a
193// terminator), and 'I' must not be the end of instruction list (which would
194// cause a degenerate basic block to be formed, having a terminator inside of
195// the basic block).
196//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000197BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
199 assert(I != InstList.end() &&
200 "Trying to get me to create degenerate basic block!");
201
202 BasicBlock *New = new BasicBlock("", getParent());
203
204 // Go from the end of the basic block through to the iterator pointer, moving
205 // to the new basic block...
206 Instruction *Inst = 0;
207 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000208 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 Inst = InstList.remove(--EndIt); // Remove from end
210 New->InstList.push_front(Inst); // Add to front
Chris Lattner113f4f42002-06-25 16:13:24 +0000211 } while (Inst != &*I); // Loop until we move the specified instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212
213 // Add a branch instruction to the newly formed basic block.
214 InstList.push_back(new BranchInst(New));
Chris Lattner4eed0b82002-02-25 00:35:07 +0000215
216 // Now we must loop through all of the successors of the New block (which
217 // _were_ the successors of the 'this' block), and update any PHI nodes in
218 // successors. If there were PHI nodes in the successors, then they need to
219 // know that incoming branches will be from New, not from Old.
220 //
221 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
222 I != E; ++I) {
223 // Loop over any phi nodes in the basic block, updating the BB field of
224 // incoming values...
225 BasicBlock *Successor = *I;
226 for (BasicBlock::iterator II = Successor->begin();
Chris Lattner113f4f42002-06-25 16:13:24 +0000227 PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000228 int IDX = PN->getBasicBlockIndex(this);
229 while (IDX != -1) {
230 PN->setIncomingBlock((unsigned)IDX, New);
231 IDX = PN->getBasicBlockIndex(this);
232 }
233 }
234 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 return New;
236}