blob: 861aea43c25a87b32d075d7a8f31f618bfccd0fe [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
2//
3// This file implements the Method class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/BasicBlock.h"
9#include "llvm/iTerminators.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/Method.h"
11#include "llvm/SymbolTable.h"
12#include "llvm/Type.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000013#include "llvm/iPHINode.h"
Vikram S. Adve32b5d842001-07-30 18:47:24 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015
16// Instantiate Templates - This ugliness is the price we have to pay
17// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
18//
Chris Lattnerf2a738c2001-07-14 06:13:19 +000019template class ValueHolder<Instruction, BasicBlock, Method>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000020
Chris Lattnerf2a738c2001-07-14 06:13:19 +000021BasicBlock::BasicBlock(const string &name, Method *Parent)
Chris Lattner2d189a52001-09-07 16:44:17 +000022 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
23 machineInstrVec(new MachineCodeForBasicBlock) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +000024 if (Parent)
25 Parent->getBasicBlocks().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000026}
27
28BasicBlock::~BasicBlock() {
29 dropAllReferences();
30 InstList.delete_all();
Vikram S. Adve32b5d842001-07-30 18:47:24 +000031 delete machineInstrVec;
Chris Lattner2f7c9632001-06-06 20:29:01 +000032}
33
34// Specialize setName to take care of symbol table majik
Chris Lattner2d189a52001-09-07 16:44:17 +000035void BasicBlock::setName(const string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000036 Method *P;
Chris Lattner2d189a52001-09-07 16:44:17 +000037 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
38 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
40 Value::setName(name);
41 if (P && hasName()) P->getSymbolTable()->insert(this);
42}
43
44void BasicBlock::setParent(Method *parent) {
45 if (getParent() && hasName())
46 getParent()->getSymbolTable()->remove(this);
47
48 InstList.setParent(parent);
49
50 if (getParent() && hasName())
51 getParent()->getSymbolTableSure()->insert(this);
52}
53
54TerminatorInst *BasicBlock::getTerminator() {
55 if (InstList.empty()) return 0;
56 Instruction *T = InstList.back();
Chris Lattnerda558102001-10-02 03:41:24 +000057 if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T);
Chris Lattner2f7c9632001-06-06 20:29:01 +000058 return 0;
59}
60
61const TerminatorInst *const BasicBlock::getTerminator() const {
62 if (InstList.empty()) return 0;
Chris Lattnerda558102001-10-02 03:41:24 +000063 if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back()))
64 return TI;
Chris Lattner2f7c9632001-06-06 20:29:01 +000065 return 0;
66}
67
68void BasicBlock::dropAllReferences() {
69 for_each(InstList.begin(), InstList.end(),
70 std::mem_fun(&Instruction::dropAllReferences));
71}
72
Chris Lattner3462ae32001-12-03 22:26:30 +000073// hasConstantReferences() - This predicate is true if there is a
Chris Lattner2f7c9632001-06-06 20:29:01 +000074// reference to this basic block in the constant pool for this method. For
75// example, if a block is reached through a switch table, that table resides
76// in the constant pool, and the basic block is reference from it.
77//
Chris Lattner3462ae32001-12-03 22:26:30 +000078bool BasicBlock::hasConstantReferences() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000079 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner3462ae32001-12-03 22:26:30 +000080 if (::isa<Constant>(*I))
Chris Lattner2f7c9632001-06-06 20:29:01 +000081 return true;
82
83 return false;
84}
85
Chris Lattner615d3cf2001-06-29 05:25:23 +000086// removePredecessor - This method is used to notify a BasicBlock that the
87// specified Predecessor of the block is no longer able to reach it. This is
88// actually not used to update the Predecessor list, but is actually used to
89// update the PHI nodes that reside in the block. Note that this should be
90// called while the predecessor still refers to this block.
91//
92void BasicBlock::removePredecessor(BasicBlock *Pred) {
Chris Lattnerba1c1f22001-10-01 13:19:53 +000093 assert(find(pred_begin(), pred_end(), Pred) != pred_end() &&
Chris Lattner615d3cf2001-06-29 05:25:23 +000094 "removePredecessor: BB is not a predecessor!");
Chris Lattnerda558102001-10-02 03:41:24 +000095 if (!isa<PHINode>(front())) return; // Quick exit.
Chris Lattner615d3cf2001-06-29 05:25:23 +000096
Chris Lattnerba1c1f22001-10-01 13:19:53 +000097 pred_iterator PI(pred_begin()), EI(pred_end());
Chris Lattner615d3cf2001-06-29 05:25:23 +000098 unsigned max_idx;
99
100 // Loop over the rest of the predecessors until we run out, or until we find
101 // out that there are more than 2 predecessors.
102 for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
103
104 // If there are exactly two predecessors, then we want to nuke the PHI nodes
105 // altogether.
106 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
107 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
Chris Lattnerda558102001-10-02 03:41:24 +0000108 // Yup, loop through and nuke the PHI nodes
109 while (PHINode *PN = dyn_cast<PHINode>(front())) {
Chris Lattner615d3cf2001-06-29 05:25:23 +0000110 PN->removeIncomingValue(Pred); // Remove the predecessor first...
111
112 assert(PN->getNumIncomingValues() == max_idx-1 &&
113 "PHI node shouldn't have this many values!!!");
114
115 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
116 if (max_idx == 2)
117 PN->replaceAllUsesWith(PN->getOperand(0));
118 delete getInstList().remove(begin()); // Remove the PHI node
119 }
120 } else {
121 // Okay, now we know that we need to remove predecessor #pred_idx from all
122 // PHI nodes. Iterate over each PHI node fixing them up
123 iterator II(begin());
Chris Lattnerda558102001-10-02 03:41:24 +0000124 for (; isa<PHINode>(*II); ++II)
125 cast<PHINode>(*II)->removeIncomingValue(Pred);
Chris Lattner615d3cf2001-06-29 05:25:23 +0000126 }
127}
128
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129
130// splitBasicBlock - This splits a basic block into two at the specified
131// instruction. Note that all instructions BEFORE the specified iterator stay
132// as part of the original basic block, an unconditional branch is added to
133// the new BB, and the rest of the instructions in the BB are moved to the new
134// BB, including the old terminator. This invalidates the iterator.
135//
136// Note that this only works on well formed basic blocks (must have a
137// terminator), and 'I' must not be the end of instruction list (which would
138// cause a degenerate basic block to be formed, having a terminator inside of
139// the basic block).
140//
Chris Lattner4cee8d82001-06-27 23:41:11 +0000141BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
143 assert(I != InstList.end() &&
144 "Trying to get me to create degenerate basic block!");
145
146 BasicBlock *New = new BasicBlock("", getParent());
147
148 // Go from the end of the basic block through to the iterator pointer, moving
149 // to the new basic block...
150 Instruction *Inst = 0;
151 do {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000152 iterator EndIt = end();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153 Inst = InstList.remove(--EndIt); // Remove from end
154 New->InstList.push_front(Inst); // Add to front
155 } while (Inst != *I); // Loop until we move the specified instruction.
156
157 // Add a branch instruction to the newly formed basic block.
158 InstList.push_back(new BranchInst(New));
159 return New;
160}