blob: e126d702b40525b44a407c27c508ab9387925ebf [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
2//
Chris Lattnerb7653df2002-04-08 22:03:57 +00003// This file implements the BasicBlock class for the VMCore library.
Chris Lattner00950542001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattner1907aba2002-04-08 00:15:29 +00007#include "ValueHolderImpl.h"
Chris Lattner00950542001-06-06 20:29:01 +00008#include "llvm/iTerminators.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Type.h"
Chris Lattner455889a2002-02-12 22:39:50 +000011#include "llvm/Support/CFG.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000012#include "llvm/iPHINode.h"
Vikram S. Adveb6393392001-07-30 18:47:24 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner00950542001-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 Lattnerb7653df2002-04-08 22:03:57 +000018template class ValueHolder<Instruction, BasicBlock, Function>;
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattnerb7653df2002-04-08 22:03:57 +000020BasicBlock::BasicBlock(const std::string &name, Function *Parent)
Chris Lattner6892b122001-09-07 16:44:17 +000021 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
22 machineInstrVec(new MachineCodeForBasicBlock) {
Chris Lattnera3d3c2b2001-07-14 06:13:19 +000023 if (Parent)
24 Parent->getBasicBlocks().push_back(this);
Chris Lattner00950542001-06-06 20:29:01 +000025}
26
27BasicBlock::~BasicBlock() {
28 dropAllReferences();
29 InstList.delete_all();
Vikram S. Adveb6393392001-07-30 18:47:24 +000030 delete machineInstrVec;
Chris Lattner00950542001-06-06 20:29:01 +000031}
32
33// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000034void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerb7653df2002-04-08 22:03:57 +000035 Function *P;
Chris Lattner6892b122001-09-07 16:44:17 +000036 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37 "Invalid symtab argument!");
Chris Lattner00950542001-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 Lattnerb7653df2002-04-08 22:03:57 +000043void BasicBlock::setParent(Function *parent) {
Chris Lattner00950542001-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 Lattnerb00c5822001-10-02 03:41:24 +000056 if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T);
Chris Lattner00950542001-06-06 20:29:01 +000057 return 0;
58}
59
60const TerminatorInst *const BasicBlock::getTerminator() const {
61 if (InstList.empty()) return 0;
Chris Lattnerb00c5822001-10-02 03:41:24 +000062 if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back()))
63 return TI;
Chris Lattner00950542001-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 Lattnere9bb2df2001-12-03 22:26:30 +000072// hasConstantReferences() - This predicate is true if there is a
Chris Lattner00950542001-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 Lattnere9bb2df2001-12-03 22:26:30 +000077bool BasicBlock::hasConstantReferences() const {
Chris Lattner00950542001-06-06 20:29:01 +000078 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000079 if (::isa<Constant>(*I))
Chris Lattner00950542001-06-06 20:29:01 +000080 return true;
81
82 return false;
83}
84
Chris Lattnerb47af252001-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 Lattner455889a2002-02-12 22:39:50 +000092 assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
Chris Lattnerb47af252001-06-29 05:25:23 +000093 "removePredecessor: BB is not a predecessor!");
Chris Lattnerb00c5822001-10-02 03:41:24 +000094 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattnerb47af252001-06-29 05:25:23 +000095
Chris Lattner455889a2002-02-12 22:39:50 +000096 pred_iterator PI(pred_begin(this)), EI(pred_end(this));
Chris Lattnerb47af252001-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
104 // altogether.
105 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
106 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000107 // Yup, loop through and nuke the PHI nodes
108 while (PHINode *PN = dyn_cast<PHINode>(front())) {
Chris Lattnerb47af252001-06-29 05:25:23 +0000109 PN->removeIncomingValue(Pred); // Remove the predecessor first...
110
111 assert(PN->getNumIncomingValues() == max_idx-1 &&
112 "PHI node shouldn't have this many values!!!");
113
114 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
115 if (max_idx == 2)
116 PN->replaceAllUsesWith(PN->getOperand(0));
117 delete getInstList().remove(begin()); // Remove the PHI node
118 }
119 } else {
120 // Okay, now we know that we need to remove predecessor #pred_idx from all
121 // PHI nodes. Iterate over each PHI node fixing them up
122 iterator II(begin());
Chris Lattnerb00c5822001-10-02 03:41:24 +0000123 for (; isa<PHINode>(*II); ++II)
124 cast<PHINode>(*II)->removeIncomingValue(Pred);
Chris Lattnerb47af252001-06-29 05:25:23 +0000125 }
126}
127
Chris Lattner00950542001-06-06 20:29:01 +0000128
129// splitBasicBlock - This splits a basic block into two at the specified
130// instruction. Note that all instructions BEFORE the specified iterator stay
131// as part of the original basic block, an unconditional branch is added to
132// the new BB, and the rest of the instructions in the BB are moved to the new
133// BB, including the old terminator. This invalidates the iterator.
134//
135// Note that this only works on well formed basic blocks (must have a
136// terminator), and 'I' must not be the end of instruction list (which would
137// cause a degenerate basic block to be formed, having a terminator inside of
138// the basic block).
139//
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000140BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner00950542001-06-06 20:29:01 +0000141 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
142 assert(I != InstList.end() &&
143 "Trying to get me to create degenerate basic block!");
144
145 BasicBlock *New = new BasicBlock("", getParent());
146
147 // Go from the end of the basic block through to the iterator pointer, moving
148 // to the new basic block...
149 Instruction *Inst = 0;
150 do {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000151 iterator EndIt = end();
Chris Lattner00950542001-06-06 20:29:01 +0000152 Inst = InstList.remove(--EndIt); // Remove from end
153 New->InstList.push_front(Inst); // Add to front
154 } while (Inst != *I); // Loop until we move the specified instruction.
155
156 // Add a branch instruction to the newly formed basic block.
157 InstList.push_back(new BranchInst(New));
Chris Lattnere8e320d2002-02-25 00:35:07 +0000158
159 // Now we must loop through all of the successors of the New block (which
160 // _were_ the successors of the 'this' block), and update any PHI nodes in
161 // successors. If there were PHI nodes in the successors, then they need to
162 // know that incoming branches will be from New, not from Old.
163 //
164 for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
165 I != E; ++I) {
166 // Loop over any phi nodes in the basic block, updating the BB field of
167 // incoming values...
168 BasicBlock *Successor = *I;
169 for (BasicBlock::iterator II = Successor->begin();
170 PHINode *PN = dyn_cast<PHINode>(*II); ++II) {
171 int IDX = PN->getBasicBlockIndex(this);
172 while (IDX != -1) {
173 PN->setIncomingBlock((unsigned)IDX, New);
174 IDX = PN->getBasicBlockIndex(this);
175 }
176 }
177 }
Chris Lattner00950542001-06-06 20:29:01 +0000178 return New;
179}