blob: b399c552210fef6b20987b7ead7201266a9f24a7 [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"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000015#include "llvm/Constant.h"
16#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Type.h"
Chris Lattner83d485b2002-02-12 22:39:50 +000018#include "llvm/Support/CFG.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000019#include "llvm/SymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
22#include <algorithm>
Chris Lattnera2960002003-11-21 16:52:05 +000023using namespace llvm;
Chris Lattner2f7c9632001-06-06 20:29:01 +000024
Chris Lattnera2960002003-11-21 16:52:05 +000025namespace {
26 /// DummyInst - An instance of this class is used to mark the end of the
27 /// instruction list. This is not a real instruction.
28 struct DummyInst : public Instruction {
29 DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd) {
30 // This should not be garbage monitored.
31 LeakDetector::removeGarbageObject(this);
32 }
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattnera2960002003-11-21 16:52:05 +000034 virtual Instruction *clone() const {
35 assert(0 && "Cannot clone EOL");abort();
36 return 0;
37 }
38 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
Chris Lattner113f4f42002-06-25 16:13:24 +000039
Chris Lattnera2960002003-11-21 16:52:05 +000040 // 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) {
43 return I->getOpcode() == OtherOpsEnd;
44 }
45 static inline bool classof(const Value *V) {
46 return isa<Instruction>(V) && classof(cast<Instruction>(V));
47 }
48 };
49}
Chris Lattner113f4f42002-06-25 16:13:24 +000050
51Instruction *ilist_traits<Instruction>::createNode() {
52 return new DummyInst();
53}
54iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
55 return BB->getInstList();
56}
57
58// Explicit instantiation of SymbolTableListTraits since some of the methods
59// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000060template class SymbolTableListTraits<Instruction, BasicBlock, Function>;
Chris Lattner113f4f42002-06-25 16:13:24 +000061
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerbb5f0db2004-02-04 03:57:50 +000063BasicBlock::BasicBlock(const std::string &Name, Function *Parent,
64 BasicBlock *InsertBefore)
Chris Lattner4dfede82002-09-26 05:03:22 +000065 : Value(Type::LabelTy, Value::BasicBlockVal, Name) {
66 // Initialize the instlist...
67 InstList.setItemParent(this);
68
69 // Make sure that we get added to a function
70 LeakDetector::addGarbageObject(this);
71
72 if (InsertBefore) {
Chris Lattnerbb5f0db2004-02-04 03:57:50 +000073 assert(Parent &&
74 "Cannot insert block before another block with no function!");
75 Parent->getBasicBlockList().insert(InsertBefore, this);
76 } else if (Parent) {
77 Parent->getBasicBlockList().push_back(this);
Chris Lattner4dfede82002-09-26 05:03:22 +000078 }
79}
80
81
Chris Lattner2f7c9632001-06-06 20:29:01 +000082BasicBlock::~BasicBlock() {
Misha Brukmanede10c92004-04-16 17:37:12 +000083 assert(getParent() == 0 && "BasicBlock still linked into the program!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000085 InstList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000086}
87
Chris Lattner9ed7aef2002-09-06 21:33:15 +000088void BasicBlock::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000089 if (getParent())
90 LeakDetector::addGarbageObject(this);
91
Chris Lattner9ed7aef2002-09-06 21:33:15 +000092 InstList.setParent(parent);
Chris Lattner184b2982002-09-08 18:59:35 +000093
94 if (getParent())
95 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000096}
97
Chris Lattner2f7c9632001-06-06 20:29:01 +000098// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000099void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +0000100 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000101 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner2d189a52001-09-07 16:44:17 +0000102 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000103 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000105 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106}
107
Chris Lattner02a71e72004-10-11 22:21:39 +0000108void BasicBlock::removeFromParent() {
109 getParent()->getBasicBlockList().remove(this);
110}
111
112void BasicBlock::eraseFromParent() {
113 getParent()->getBasicBlockList().erase(this);
114}
115
116
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117TerminatorInst *BasicBlock::getTerminator() {
118 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000119 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120}
121
122const TerminatorInst *const BasicBlock::getTerminator() const {
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
127void BasicBlock::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000128 for(iterator I = begin(), E = end(); I != E; ++I)
129 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130}
131
Chris Lattner615d3cf2001-06-29 05:25:23 +0000132// removePredecessor - This method is used to notify a BasicBlock that the
133// specified Predecessor of the block is no longer able to reach it. This is
134// actually not used to update the Predecessor list, but is actually used to
135// update the PHI nodes that reside in the block. Note that this should be
136// called while the predecessor still refers to this block.
137//
138void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +0000139 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +0000140 "removePredecessor: BB is not a predecessor!");
Chris Lattner8d0b1b22004-12-11 22:10:29 +0000141 if (InstList.empty()) return;
Chris Lattnerbea72472004-07-06 17:44:17 +0000142 PHINode *APN = dyn_cast<PHINode>(&front());
143 if (!APN) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000144
145 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerbea72472004-07-06 17:44:17 +0000146 // altogether. However, we cannot do this, if this in this case:
Chris Lattnerf7373e42002-05-21 19:52:49 +0000147 //
148 // Loop:
149 // %x = phi [X, Loop]
150 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
151 // br Loop ;; %x2 does not dominate all uses
152 //
153 // This is because the PHI node input is actually taken from the predecessor
154 // basic block. The only case this can happen is with a self loop, so we
155 // check for this case explicitly now.
156 //
Chris Lattnerbea72472004-07-06 17:44:17 +0000157 unsigned max_idx = APN->getNumIncomingValues();
Chris Lattner615d3cf2001-06-29 05:25:23 +0000158 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000159 if (max_idx == 2) {
Chris Lattnerbea72472004-07-06 17:44:17 +0000160 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred);
Chris Lattnerf7373e42002-05-21 19:52:49 +0000161
162 // Disable PHI elimination!
163 if (this == Other) max_idx = 3;
164 }
165
Chris Lattner615d3cf2001-06-29 05:25:23 +0000166 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000167 // Yup, loop through and nuke the PHI nodes
Chris Lattner113f4f42002-06-25 16:13:24 +0000168 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000169 PN->removeIncomingValue(Pred); // Remove the predecessor first...
Chris Lattner615d3cf2001-06-29 05:25:23 +0000170
171 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
Chris Lattnercaf5b502002-10-08 21:36:34 +0000172 if (max_idx == 2) {
Chris Lattneref8c8332003-04-25 23:14:19 +0000173 if (PN->getOperand(0) != PN)
174 PN->replaceAllUsesWith(PN->getOperand(0));
175 else
176 // We are left with an infinite loop with no entries: kill the PHI.
177 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattnercaf5b502002-10-08 21:36:34 +0000178 getInstList().pop_front(); // Remove the PHI node
179 }
180
181 // If the PHI node already only had one entry, it got deleted by
182 // removeIncomingValue.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000183 }
184 } else {
185 // Okay, now we know that we need to remove predecessor #pred_idx from all
186 // PHI nodes. Iterate over each PHI node fixing them up
Chris Lattner708ee9d2004-06-05 00:11:27 +0000187 PHINode *PN;
Chris Lattner08d1b9d2004-06-05 17:43:52 +0000188 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ++II)
Chris Lattnerf7373e42002-05-21 19:52:49 +0000189 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000190 }
191}
192
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193
194// splitBasicBlock - This splits a basic block into two at the specified
195// instruction. Note that all instructions BEFORE the specified iterator stay
196// as part of the original basic block, an unconditional branch is added to
197// the new BB, and the rest of the instructions in the BB are moved to the new
198// BB, including the old terminator. This invalidates the iterator.
199//
200// Note that this only works on well formed basic blocks (must have a
201// terminator), and 'I' must not be the end of instruction list (which would
202// cause a degenerate basic block to be formed, having a terminator inside of
203// the basic block).
204//
Chris Lattner6d569352003-08-24 03:41:39 +0000205BasicBlock *BasicBlock::splitBasicBlock(iterator I, const std::string &BBName) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
207 assert(I != InstList.end() &&
208 "Trying to get me to create degenerate basic block!");
209
Chris Lattnerbb5f0db2004-02-04 03:57:50 +0000210 BasicBlock *New = new BasicBlock(BBName, getParent(), getNext());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211
Chris Lattnerc4c7ea52004-02-03 23:11:21 +0000212 // Move all of the specified instructions from the original basic block into
213 // the new basic block.
214 New->getInstList().splice(New->end(), this->getInstList(), I, end());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215
216 // Add a branch instruction to the newly formed basic block.
Chris Lattnera2960002003-11-21 16:52:05 +0000217 new BranchInst(New, this);
Chris Lattner4eed0b82002-02-25 00:35:07 +0000218
219 // Now we must loop through all of the successors of the New block (which
220 // _were_ the successors of the 'this' block), and update any PHI nodes in
221 // successors. If there were PHI nodes in the successors, then they need to
222 // know that incoming branches will be from New, not from Old.
223 //
Chris Lattner64d88bc2003-09-24 22:03:22 +0000224 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000225 // Loop over any phi nodes in the basic block, updating the BB field of
226 // incoming values...
227 BasicBlock *Successor = *I;
Chris Lattner708ee9d2004-06-05 00:11:27 +0000228 PHINode *PN;
Chris Lattner4eed0b82002-02-25 00:35:07 +0000229 for (BasicBlock::iterator II = Successor->begin();
Chris Lattner08d1b9d2004-06-05 17:43:52 +0000230 (PN = dyn_cast<PHINode>(II)); ++II) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000231 int IDX = PN->getBasicBlockIndex(this);
232 while (IDX != -1) {
233 PN->setIncomingBlock((unsigned)IDX, New);
234 IDX = PN->getBasicBlockIndex(this);
235 }
236 }
237 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238 return New;
239}