blob: 512ddb59df91d25aa3892ba3ad2d6deadd68c1e5 [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 Lattner6e6f5be2002-04-08 00:15:29 +00007#include "ValueHolderImpl.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"
Vikram S. Adve32b5d842001-07-30 18:47:24 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014
15// Instantiate Templates - This ugliness is the price we have to pay
16// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
17//
Chris Lattnerf739fa82002-04-08 22:03:57 +000018template class ValueHolder<Instruction, BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000019
Chris Lattnerf739fa82002-04-08 22:03:57 +000020BasicBlock::BasicBlock(const std::string &name, Function *Parent)
Chris Lattner2d189a52001-09-07 16:44:17 +000021 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
22 machineInstrVec(new MachineCodeForBasicBlock) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +000023 if (Parent)
24 Parent->getBasicBlocks().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000025}
26
27BasicBlock::~BasicBlock() {
28 dropAllReferences();
29 InstList.delete_all();
Vikram S. Adve32b5d842001-07-30 18:47:24 +000030 delete machineInstrVec;
Chris Lattner2f7c9632001-06-06 20:29:01 +000031}
32
33// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000034void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerf739fa82002-04-08 22:03:57 +000035 Function *P;
Chris Lattner2d189a52001-09-07 16:44:17 +000036 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000038 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
39 Value::setName(name);
40 if (P && hasName()) P->getSymbolTable()->insert(this);
41}
42
Chris Lattnerf739fa82002-04-08 22:03:57 +000043void BasicBlock::setParent(Function *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000044 if (getParent() && hasName())
45 getParent()->getSymbolTable()->remove(this);
46
47 InstList.setParent(parent);
48
49 if (getParent() && hasName())
50 getParent()->getSymbolTableSure()->insert(this);
51}
52
53TerminatorInst *BasicBlock::getTerminator() {
54 if (InstList.empty()) return 0;
55 Instruction *T = InstList.back();
Chris Lattnerda558102001-10-02 03:41:24 +000056 if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T);
Chris Lattner2f7c9632001-06-06 20:29:01 +000057 return 0;
58}
59
60const TerminatorInst *const BasicBlock::getTerminator() const {
61 if (InstList.empty()) return 0;
Chris Lattnerda558102001-10-02 03:41:24 +000062 if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back()))
63 return TI;
Chris Lattner2f7c9632001-06-06 20:29:01 +000064 return 0;
65}
66
67void BasicBlock::dropAllReferences() {
68 for_each(InstList.begin(), InstList.end(),
69 std::mem_fun(&Instruction::dropAllReferences));
70}
71
Chris Lattner3462ae32001-12-03 22:26:30 +000072// hasConstantReferences() - This predicate is true if there is a
Chris Lattner2f7c9632001-06-06 20:29:01 +000073// reference to this basic block in the constant pool for this method. For
74// example, if a block is reached through a switch table, that table resides
75// in the constant pool, and the basic block is reference from it.
76//
Chris Lattner3462ae32001-12-03 22:26:30 +000077bool BasicBlock::hasConstantReferences() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000078 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattnerca142372002-04-28 19:55:58 +000079 if (::isa<Constant>((Value*)*I))
Chris Lattner2f7c9632001-06-06 20:29:01 +000080 return true;
81
82 return false;
83}
84
Chris Lattner615d3cf2001-06-29 05:25:23 +000085// removePredecessor - This method is used to notify a BasicBlock that the
86// specified Predecessor of the block is no longer able to reach it. This is
87// actually not used to update the Predecessor list, but is actually used to
88// update the PHI nodes that reside in the block. Note that this should be
89// called while the predecessor still refers to this block.
90//
91void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattner83d485b2002-02-12 22:39:50 +000092 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattner615d3cf2001-06-29 05:25:23 +000093 "removePredecessor: BB is not a predecessor!");
Chris Lattnerda558102001-10-02 03:41:24 +000094 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +000095
Chris Lattner83d485b2002-02-12 22:39:50 +000096 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
Chris Lattner615d3cf2001-06-29 05:25:23 +000097 unsigned max_idx;
98
99 // Loop over the rest of the predecessors until we run out, or until we find
100 // out that there are more than 2 predecessors.
101 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
102
103 // If there are exactly two predecessors, then we want to nuke the PHI nodes
Chris Lattnerf7373e42002-05-21 19:52:49 +0000104 // altogether. We cannot do this, however if this in this case however:
105 //
106 // Loop:
107 // %x = phi [X, Loop]
108 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
109 // br Loop ;; %x2 does not dominate all uses
110 //
111 // This is because the PHI node input is actually taken from the predecessor
112 // basic block. The only case this can happen is with a self loop, so we
113 // check for this case explicitly now.
114 //
Chris Lattner615d3cf2001-06-29 05:25:23 +0000115 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
Chris Lattnerf7373e42002-05-21 19:52:49 +0000116 if (max_idx == 2) {
117 PI = pred_begin(this);
118 BasicBlock *Other = *PI == Pred ? *++PI : *PI;
119
120 // Disable PHI elimination!
121 if (this == Other) max_idx = 3;
122 }
123
Chris Lattner615d3cf2001-06-29 05:25:23 +0000124 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000125 // Yup, loop through and nuke the PHI nodes
126 while (PHINode *PN = dyn_cast<PHINode>(front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000127 PN->removeIncomingValue(Pred); // Remove the predecessor first...
128
129 assert(PN->getNumIncomingValues() == max_idx-1 &&
130 "PHI node shouldn't have this many values!!!");
131
132 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
133 if (max_idx == 2)
134 PN->replaceAllUsesWith(PN->getOperand(0));
Chris Lattner5cd012d2002-05-23 16:52:34 +0000135 else // Otherwise there are no incoming values/edges, replace with dummy
136 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
137 delete getInstList().remove(begin()); // Remove the PHI node
Chris Lattner615d3cf2001-06-29 05:25:23 +0000138 }
139 } else {
140 // Okay, now we know that we need to remove predecessor #pred_idx from all
141 // PHI nodes. Iterate over each PHI node fixing them up
Chris Lattnerf7373e42002-05-21 19:52:49 +0000142 for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(*II); ++II)
143 PN->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000144 }
145}
146
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147
148// splitBasicBlock - This splits a basic block into two at the specified
149// instruction. Note that all instructions BEFORE the specified iterator stay
150// as part of the original basic block, an unconditional branch is added to
151// the new BB, and the rest of the instructions in the BB are moved to the new
152// BB, including the old terminator. This invalidates the iterator.
153//
154// Note that this only works on well formed basic blocks (must have a
155// terminator), and 'I' must not be the end of instruction list (which would
156// cause a degenerate basic block to be formed, having a terminator inside of
157// the basic block).
158//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000159BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
161 assert(I != InstList.end() &&
162 "Trying to get me to create degenerate basic block!");
163
164 BasicBlock *New = new BasicBlock("", getParent());
165
166 // Go from the end of the basic block through to the iterator pointer, moving
167 // to the new basic block...
168 Instruction *Inst = 0;
169 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000170 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 Inst = InstList.remove(--EndIt); // Remove from end
172 New->InstList.push_front(Inst); // Add to front
173 } while (Inst != *I); // Loop until we move the specified instruction.
174
175 // Add a branch instruction to the newly formed basic block.
176 InstList.push_back(new BranchInst(New));
Chris Lattner4eed0b82002-02-25 00:35:07 +0000177
178 // Now we must loop through all of the successors of the New block (which
179 // _were_ the successors of the 'this' block), and update any PHI nodes in
180 // successors. If there were PHI nodes in the successors, then they need to
181 // know that incoming branches will be from New, not from Old.
182 //
183 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
184 I != E; ++I) {
185 // Loop over any phi nodes in the basic block, updating the BB field of
186 // incoming values...
187 BasicBlock *Successor = *I;
188 for (BasicBlock::iterator II = Successor->begin();
189 PHINode *PN = dyn_cast<PHINode>(*II); ++II) {
190 int IDX = PN->getBasicBlockIndex(this);
191 while (IDX != -1) {
192 PN->setIncomingBlock((unsigned)IDX, New);
193 IDX = PN->getBasicBlockIndex(this);
194 }
195 }
196 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197 return New;
198}