* Pull BasicBlock::pred_* and BasicBlock::succ_* out of BasicBlock.h and into
[oota-llvm.git] / lib / VMCore / BasicBlock.cpp
1 //===-- 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/iTerminators.h"
9 #include "llvm/SymbolTable.h"
10 #include "llvm/Type.h"
11 #include "llvm/Support/CFG.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14
15 // Instantiate Templates - This ugliness is the price we have to pay
16 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
17 //
18 template class ValueHolder<Instruction, BasicBlock, Method>;
19
20 BasicBlock::BasicBlock(const std::string &name, Method *Parent)
21   : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0),
22     machineInstrVec(new MachineCodeForBasicBlock) {
23   if (Parent)
24     Parent->getBasicBlocks().push_back(this);
25 }
26
27 BasicBlock::~BasicBlock() {
28   dropAllReferences();
29   InstList.delete_all();
30   delete machineInstrVec;
31 }
32
33 // Specialize setName to take care of symbol table majik
34 void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
35   Method *P;
36   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37          "Invalid symtab argument!");
38   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
39   Value::setName(name);
40   if (P && hasName()) P->getSymbolTable()->insert(this);
41 }
42
43 void BasicBlock::setParent(Method *parent) { 
44   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
53 TerminatorInst *BasicBlock::getTerminator() {
54   if (InstList.empty()) return 0;
55   Instruction *T = InstList.back();
56   if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T);
57   return 0;
58 }
59
60 const TerminatorInst *const BasicBlock::getTerminator() const {
61   if (InstList.empty()) return 0;
62   if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back()))
63     return TI;
64   return 0;
65 }
66
67 void BasicBlock::dropAllReferences() {
68   for_each(InstList.begin(), InstList.end(), 
69            std::mem_fun(&Instruction::dropAllReferences));
70 }
71
72 // hasConstantReferences() - This predicate is true if there is a 
73 // 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 //
77 bool BasicBlock::hasConstantReferences() const {
78   for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
79     if (::isa<Constant>(*I))
80       return true;
81
82   return false;
83 }
84
85 // 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 //
91 void BasicBlock::removePredecessor(BasicBlock *Pred) {
92   assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
93          "removePredecessor: BB is not a predecessor!");
94   if (!isa<PHINode>(front())) return;   // Quick exit.
95
96   pred_iterator PI(pred_begin(this)), EI(pred_end(this));
97   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?
107     // Yup, loop through and nuke the PHI nodes
108     while (PHINode *PN = dyn_cast<PHINode>(front())) {
109       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());
123     for (; isa<PHINode>(*II); ++II)
124       cast<PHINode>(*II)->removeIncomingValue(Pred);
125   }
126 }
127
128
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 //
140 BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
141   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 {
151     iterator EndIt = end();
152     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));
158   return New;
159 }