blob: d3df1d1d71dbd1265a6d2ca420132d5126d50ab4 [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 Lattnera2960002003-11-21 16:52:05 +000024using namespace llvm;
Chris Lattner2f7c9632001-06-06 20:29:01 +000025
Chris Lattnera2960002003-11-21 16:52:05 +000026namespace {
27 /// DummyInst - An instance of this class is used to mark the end of the
28 /// instruction list. This is not a real instruction.
29 struct DummyInst : public Instruction {
30 DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd) {
31 // This should not be garbage monitored.
32 LeakDetector::removeGarbageObject(this);
33 }
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattnera2960002003-11-21 16:52:05 +000035 virtual Instruction *clone() const {
36 assert(0 && "Cannot clone EOL");abort();
37 return 0;
38 }
39 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
Chris Lattner113f4f42002-06-25 16:13:24 +000040
Chris Lattnera2960002003-11-21 16:52:05 +000041 // Methods for support type inquiry through isa, cast, and dyn_cast...
42 static inline bool classof(const DummyInst *) { return true; }
43 static inline bool classof(const Instruction *I) {
44 return I->getOpcode() == OtherOpsEnd;
45 }
46 static inline bool classof(const Value *V) {
47 return isa<Instruction>(V) && classof(cast<Instruction>(V));
48 }
49 };
50}
Chris Lattner113f4f42002-06-25 16:13:24 +000051
52Instruction *ilist_traits<Instruction>::createNode() {
53 return new DummyInst();
54}
55iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
56 return BB->getInstList();
57}
58
59// Explicit instantiation of SymbolTableListTraits since some of the methods
60// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000061template class SymbolTableListTraits<Instruction, BasicBlock, Function>;
Chris Lattner113f4f42002-06-25 16:13:24 +000062
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerbb5f0db2004-02-04 03:57:50 +000064BasicBlock::BasicBlock(const std::string &Name, Function *Parent,
65 BasicBlock *InsertBefore)
Chris Lattner4dfede82002-09-26 05:03:22 +000066 : Value(Type::LabelTy, Value::BasicBlockVal, Name) {
67 // Initialize the instlist...
68 InstList.setItemParent(this);
69
70 // Make sure that we get added to a function
71 LeakDetector::addGarbageObject(this);
72
73 if (InsertBefore) {
Chris Lattnerbb5f0db2004-02-04 03:57:50 +000074 assert(Parent &&
75 "Cannot insert block before another block with no function!");
76 Parent->getBasicBlockList().insert(InsertBefore, this);
77 } else if (Parent) {
78 Parent->getBasicBlockList().push_back(this);
Chris Lattner4dfede82002-09-26 05:03:22 +000079 }
80}
81
82
Chris Lattner2f7c9632001-06-06 20:29:01 +000083BasicBlock::~BasicBlock() {
Misha Brukmanede10c92004-04-16 17:37:12 +000084 assert(getParent() == 0 && "BasicBlock still linked into the program!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000086 InstList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000087}
88
Chris Lattner9ed7aef2002-09-06 21:33:15 +000089void BasicBlock::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000090 if (getParent())
91 LeakDetector::addGarbageObject(this);
92
Chris Lattner9ed7aef2002-09-06 21:33:15 +000093 InstList.setParent(parent);
Chris Lattner184b2982002-09-08 18:59:35 +000094
95 if (getParent())
96 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000097}
98
Chris Lattner2f7c9632001-06-06 20:29:01 +000099// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000100void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +0000101 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000102 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner2d189a52001-09-07 16:44:17 +0000103 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000104 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000106 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107}
108
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109TerminatorInst *BasicBlock::getTerminator() {
110 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000111 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112}
113
114const TerminatorInst *const BasicBlock::getTerminator() const {
115 if (InstList.empty()) return 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 return dyn_cast<TerminatorInst>(&InstList.back());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117}
118
119void BasicBlock::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000120 for(iterator I = begin(), E = end(); I != E; ++I)
121 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122}
123
Chris Lattner615d3cf2001-06-29 05:25:23 +0000124// removePredecessor - This method is used to notify a BasicBlock that the
125// specified Predecessor of the block is no longer able to reach it. This is
126// actually not used to update the Predecessor list, but is actually used to
127// update the PHI nodes that reside in the block. Note that this should be
128// called while the predecessor still refers to this block.
129//
130void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +0000131 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +0000132 "removePredecessor: BB is not a predecessor!");
Chris Lattnerbea72472004-07-06 17:44:17 +0000133 PHINode *APN = dyn_cast<PHINode>(&front());
134 if (!APN) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000135
136 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerbea72472004-07-06 17:44:17 +0000137 // altogether. However, we cannot do this, if this in this case:
Chris Lattnerf7373e42002-05-21 19:52:49 +0000138 //
139 // Loop:
140 // %x = phi [X, Loop]
141 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
142 // br Loop ;; %x2 does not dominate all uses
143 //
144 // This is because the PHI node input is actually taken from the predecessor
145 // basic block. The only case this can happen is with a self loop, so we
146 // check for this case explicitly now.
147 //
Chris Lattnerbea72472004-07-06 17:44:17 +0000148 unsigned max_idx = APN->getNumIncomingValues();
Chris Lattner615d3cf2001-06-29 05:25:23 +0000149 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000150 if (max_idx == 2) {
Chris Lattnerbea72472004-07-06 17:44:17 +0000151 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred);
Chris Lattnerf7373e42002-05-21 19:52:49 +0000152
153 // Disable PHI elimination!
154 if (this == Other) max_idx = 3;
155 }
156
Chris Lattner615d3cf2001-06-29 05:25:23 +0000157 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000158 // Yup, loop through and nuke the PHI nodes
Chris Lattner113f4f42002-06-25 16:13:24 +0000159 while (PHINode *PN = dyn_cast<PHINode>(&front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000160 PN->removeIncomingValue(Pred); // Remove the predecessor first...
Chris Lattner615d3cf2001-06-29 05:25:23 +0000161
162 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
Chris Lattnercaf5b502002-10-08 21:36:34 +0000163 if (max_idx == 2) {
Chris Lattneref8c8332003-04-25 23:14:19 +0000164 if (PN->getOperand(0) != PN)
165 PN->replaceAllUsesWith(PN->getOperand(0));
166 else
167 // We are left with an infinite loop with no entries: kill the PHI.
168 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattnercaf5b502002-10-08 21:36:34 +0000169 getInstList().pop_front(); // Remove the PHI node
170 }
171
172 // If the PHI node already only had one entry, it got deleted by
173 // removeIncomingValue.
Chris Lattner615d3cf2001-06-29 05:25:23 +0000174 }
175 } else {
176 // Okay, now we know that we need to remove predecessor #pred_idx from all
177 // PHI nodes. Iterate over each PHI node fixing them up
Chris Lattner708ee9d2004-06-05 00:11:27 +0000178 PHINode *PN;
Chris Lattner08d1b9d2004-06-05 17:43:52 +0000179 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ++II)
Chris Lattnerf7373e42002-05-21 19:52:49 +0000180 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000181 }
182}
183
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184
185// splitBasicBlock - This splits a basic block into two at the specified
186// instruction. Note that all instructions BEFORE the specified iterator stay
187// as part of the original basic block, an unconditional branch is added to
188// the new BB, and the rest of the instructions in the BB are moved to the new
189// BB, including the old terminator. This invalidates the iterator.
190//
191// Note that this only works on well formed basic blocks (must have a
192// terminator), and 'I' must not be the end of instruction list (which would
193// cause a degenerate basic block to be formed, having a terminator inside of
194// the basic block).
195//
Chris Lattner6d569352003-08-24 03:41:39 +0000196BasicBlock *BasicBlock::splitBasicBlock(iterator I, const std::string &BBName) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
198 assert(I != InstList.end() &&
199 "Trying to get me to create degenerate basic block!");
200
Chris Lattnerbb5f0db2004-02-04 03:57:50 +0000201 BasicBlock *New = new BasicBlock(BBName, getParent(), getNext());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202
Chris Lattnerc4c7ea52004-02-03 23:11:21 +0000203 // Move all of the specified instructions from the original basic block into
204 // the new basic block.
205 New->getInstList().splice(New->end(), this->getInstList(), I, end());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206
207 // Add a branch instruction to the newly formed basic block.
Chris Lattnera2960002003-11-21 16:52:05 +0000208 new BranchInst(New, this);
Chris Lattner4eed0b82002-02-25 00:35:07 +0000209
210 // Now we must loop through all of the successors of the New block (which
211 // _were_ the successors of the 'this' block), and update any PHI nodes in
212 // successors. If there were PHI nodes in the successors, then they need to
213 // know that incoming branches will be from New, not from Old.
214 //
Chris Lattner64d88bc2003-09-24 22:03:22 +0000215 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000216 // Loop over any phi nodes in the basic block, updating the BB field of
217 // incoming values...
218 BasicBlock *Successor = *I;
Chris Lattner708ee9d2004-06-05 00:11:27 +0000219 PHINode *PN;
Chris Lattner4eed0b82002-02-25 00:35:07 +0000220 for (BasicBlock::iterator II = Successor->begin();
Chris Lattner08d1b9d2004-06-05 17:43:52 +0000221 (PN = dyn_cast<PHINode>(II)); ++II) {
Chris Lattner4eed0b82002-02-25 00:35:07 +0000222 int IDX = PN->getBasicBlockIndex(this);
223 while (IDX != -1) {
224 PN->setIncomingBlock((unsigned)IDX, New);
225 IDX = PN->getBasicBlockIndex(this);
226 }
227 }
228 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000229 return New;
230}