blob: 3110b4dd8269ebd337d86ad22e321918b3cbf2ed [file] [log] [blame]
Chris Lattner184b2982002-09-08 18:59:35 +00001//===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattnerf739fa82002-04-08 22:03:57 +000010// This file implements the BasicBlock class for the VMCore library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner113f4f42002-06-25 16:13:24 +000014#include "llvm/BasicBlock.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/iTerminators.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/Type.h"
Chris Lattner83d485b2002-02-12 22:39:50 +000017#include "llvm/Support/CFG.h"
Chris Lattner5cd012d2002-05-23 16:52:34 +000018#include "llvm/Constant.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "llvm/SymbolTable.h"
Chris Lattner184b2982002-09-08 18:59:35 +000021#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000022#include "SymbolTableListTraitsImpl.h"
23#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000024
Chris Lattner113f4f42002-06-25 16:13:24 +000025// DummyInst - An instance of this class is used to mark the end of the
26// instruction list. This is not a real instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +000027//
Chris Lattner113f4f42002-06-25 16:13:24 +000028struct DummyInst : public Instruction {
Chris Lattner69ce8672002-10-13 19:39:16 +000029 DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd) {
Chris Lattner184b2982002-09-08 18:59:35 +000030 // This should not be garbage monitored.
31 LeakDetector::removeGarbageObject(this);
32 }
Chris Lattner113f4f42002-06-25 16:13:24 +000033
Chris Lattnerc678c172002-07-25 15:39:05 +000034 virtual Instruction *clone() const {
35 assert(0 && "Cannot clone EOL");abort();
36 return 0;
37 }
Chris Lattner113f4f42002-06-25 16:13:24 +000038 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
39
40 // Methods for support type inquiry through isa, cast, and dyn_cast...
41 static inline bool classof(const DummyInst *) { return true; }
42 static inline bool classof(const Instruction *I) {
Chris Lattner69ce8672002-10-13 19:39:16 +000043 return I->getOpcode() == OtherOpsEnd;
Chris Lattner113f4f42002-06-25 16:13:24 +000044 }
45 static inline bool classof(const Value *V) {
46 return isa<Instruction>(V) && classof(cast<Instruction>(V));
47 }
48};
49
50Instruction *ilist_traits<Instruction>::createNode() {
51 return new DummyInst();
52}
53iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
54 return BB->getInstList();
55}
56
57// Explicit instantiation of SymbolTableListTraits since some of the methods
58// are not in the public header file...
59template SymbolTableListTraits<Instruction, BasicBlock, Function>;
60
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattner9ed7aef2002-09-06 21:33:15 +000062// BasicBlock ctor - If the function parameter is specified, the basic block is
63// automatically inserted at the end of the function.
64//
Chris Lattnerf739fa82002-04-08 22:03:57 +000065BasicBlock::BasicBlock(const std::string &name, Function *Parent)
Vikram S. Adve6e792fc2002-07-08 22:31:11 +000066 : Value(Type::LabelTy, Value::BasicBlockVal, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000067 // Initialize the instlist...
68 InstList.setItemParent(this);
69
Chris Lattner184b2982002-09-08 18:59:35 +000070 // Make sure that we get added to a function
71 LeakDetector::addGarbageObject(this);
72
Chris Lattnerf2a738c2001-07-14 06:13:19 +000073 if (Parent)
Chris Lattner113f4f42002-06-25 16:13:24 +000074 Parent->getBasicBlockList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000075}
76
Chris Lattner4dfede82002-09-26 05:03:22 +000077/// BasicBlock ctor - If the InsertBefore parameter is specified, the basic
78/// block is automatically inserted right before the specified block.
79///
80BasicBlock::BasicBlock(const std::string &Name, BasicBlock *InsertBefore)
81 : Value(Type::LabelTy, Value::BasicBlockVal, Name) {
82 // Initialize the instlist...
83 InstList.setItemParent(this);
84
85 // Make sure that we get added to a function
86 LeakDetector::addGarbageObject(this);
87
88 if (InsertBefore) {
89 assert(InsertBefore->getParent() &&
90 "Cannot insert block before another block that is not embedded into"
91 " a function yet!");
92 InsertBefore->getParent()->getBasicBlockList().insert(InsertBefore, this);
93 }
94}
95
96
Chris Lattner2f7c9632001-06-06 20:29:01 +000097BasicBlock::~BasicBlock() {
98 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000099 InstList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100}
101
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000102void BasicBlock::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000103 if (getParent())
104 LeakDetector::addGarbageObject(this);
105
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000106 InstList.setParent(parent);
Chris Lattner184b2982002-09-08 18:59:35 +0000107
108 if (getParent())
109 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000110}
111
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000113void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +0000114 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000115 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner2d189a52001-09-07 16:44:17 +0000116 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000117 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000119 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120}
121
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122TerminatorInst *BasicBlock::getTerminator() {
123 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000124 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125}
126
127const TerminatorInst *const BasicBlock::getTerminator() const {
128 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000129 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130}
131
132void BasicBlock::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000133 for(iterator I = begin(), E = end(); I != E; ++I)
134 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135}
136
Chris Lattner3462ae32001-12-03 22:26:30 +0000137// hasConstantReferences() - This predicate is true if there is a
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138// reference to this basic block in the constant pool for this method. For
139// example, if a block is reached through a switch table, that table resides
140// in the constant pool, and the basic block is reference from it.
141//
Chris Lattner3462ae32001-12-03 22:26:30 +0000142bool BasicBlock::hasConstantReferences() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattnerca142372002-04-28 19:55:58 +0000144 if (::isa<Constant>((Value*)*I))
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145 return true;
146
147 return false;
148}
149
Chris Lattner615d3cf2001-06-29 05:25:23 +0000150// removePredecessor - This method is used to notify a BasicBlock that the
151// specified Predecessor of the block is no longer able to reach it. This is
152// actually not used to update the Predecessor list, but is actually used to
153// update the PHI nodes that reside in the block. Note that this should be
154// called while the predecessor still refers to this block.
155//
156void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +0000157 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +0000158 "removePredecessor: BB is not a predecessor!");
Chris Lattnerda558102001-10-02 03:41:24 +0000159 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000160
Chris Lattner83d485b2002-02-12 22:39:50 +0000161 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
Chris Lattner615d3cf2001-06-29 05:25:23 +0000162 unsigned max_idx;
163
164 // Loop over the rest of the predecessors until we run out, or until we find
165 // out that there are more than 2 predecessors.
166 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
167
168 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerf7373e42002-05-21 19:52:49 +0000169 // altogether. We cannot do this, however if this in this case however:
170 //
171 // Loop:
172 // %x = phi [X, Loop]
173 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
174 // br Loop ;; %x2 does not dominate all uses
175 //
176 // This is because the PHI node input is actually taken from the predecessor
177 // basic block. The only case this can happen is with a self loop, so we
178 // check for this case explicitly now.
179 //
Chris Lattner615d3cf2001-06-29 05:25:23 +0000180 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000181 if (max_idx == 2) {
182 PI = pred_begin(this);
183 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
184
185 // Disable PHI elimination!
186 if (this == Other) max_idx = 3;
187 }
188
Chris Lattner615d3cf2001-06-29 05:25:23 +0000189 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000190 // Yup, loop through and nuke the PHI nodes
Chris Lattner113f4f42002-06-25 16:13:24 +0000191 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000192 PN->removeIncomingValue(Pred); // Remove the predecessor first...
Chris Lattner615d3cf2001-06-29 05:25:23 +0000193
194 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
Chris Lattnercaf5b502002-10-08 21:36:34 +0000195 if (max_idx == 2) {
Chris Lattneref8c8332003-04-25 23:14:19 +0000196 if (PN->getOperand(0) != PN)
197 PN->replaceAllUsesWith(PN->getOperand(0));
198 else
199 // We are left with an infinite loop with no entries: kill the PHI.
200 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattnercaf5b502002-10-08 21:36:34 +0000201 getInstList().pop_front(); // Remove the PHI node
202 }
203
204 // If the PHI node already only had one entry, it got deleted by
205 // removeIncomingValue.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000206 }
207 } else {
208 // Okay, now we know that we need to remove predecessor #pred_idx from all
209 // PHI nodes. Iterate over each PHI node fixing them up
Chris Lattner889f6202003-04-23 16:37:45 +0000210 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(II); ++II)
Chris Lattnerf7373e42002-05-21 19:52:49 +0000211 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000212 }
213}
214
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215
216// splitBasicBlock - This splits a basic block into two at the specified
217// instruction. Note that all instructions BEFORE the specified iterator stay
218// as part of the original basic block, an unconditional branch is added to
219// the new BB, and the rest of the instructions in the BB are moved to the new
220// BB, including the old terminator. This invalidates the iterator.
221//
222// Note that this only works on well formed basic blocks (must have a
223// terminator), and 'I' must not be the end of instruction list (which would
224// cause a degenerate basic block to be formed, having a terminator inside of
225// the basic block).
226//
Chris Lattner6d569352003-08-24 03:41:39 +0000227BasicBlock *BasicBlock::splitBasicBlock(iterator I, const std::string &BBName) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
229 assert(I != InstList.end() &&
230 "Trying to get me to create degenerate basic block!");
231
Chris Lattner6d569352003-08-24 03:41:39 +0000232 BasicBlock *New = new BasicBlock(BBName, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233
234 // Go from the end of the basic block through to the iterator pointer, moving
235 // to the new basic block...
236 Instruction *Inst = 0;
237 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000238 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239 Inst = InstList.remove(--EndIt); // Remove from end
240 New->InstList.push_front(Inst); // Add to front
Chris Lattner113f4f42002-06-25 16:13:24 +0000241 } while (Inst != &*I); // Loop until we move the specified instruction.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242
243 // Add a branch instruction to the newly formed basic block.
244 InstList.push_back(new BranchInst(New));
Chris Lattner4eed0b82002-02-25 00:35:07 +0000245
246 // Now we must loop through all of the successors of the New block (which
247 // _were_ the successors of the 'this' block), and update any PHI nodes in
248 // successors. If there were PHI nodes in the successors, then they need to
249 // know that incoming branches will be from New, not from Old.
250 //
Chris Lattner64d88bc2003-09-24 22:03:22 +0000251 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000252 // Loop over any phi nodes in the basic block, updating the BB field of
253 // incoming values...
254 BasicBlock *Successor = *I;
255 for (BasicBlock::iterator II = Successor->begin();
Chris Lattner889f6202003-04-23 16:37:45 +0000256 PHINode *PN = dyn_cast<PHINode>(II); ++II) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000257 int IDX = PN->getBasicBlockIndex(this);
258 while (IDX != -1) {
259 PN->setIncomingBlock((unsigned)IDX, New);
260 IDX = PN->getBasicBlockIndex(this);
261 }
262 }
263 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 return New;
265}