BasicBlock.cpp revision a3d3c2b64545c00f70453642e5d84b028dfce671
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/BasicBlock.h"
9#include "llvm/iTerminators.h"
10#include "llvm/Module.h"
11#include "llvm/Method.h"
12#include "llvm/SymbolTable.h"
13#include "llvm/Type.h"
14#include "llvm/CFG.h"
15#include "llvm/iOther.h"
16
17// Instantiate Templates - This ugliness is the price we have to pay
18// for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
19//
20template class ValueHolder<Instruction, BasicBlock, Method>;
21
22BasicBlock::BasicBlock(const string &name, Method *Parent)
23  : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0) {
24  if (Parent)
25    Parent->getBasicBlocks().push_back(this);
26}
27
28BasicBlock::~BasicBlock() {
29  dropAllReferences();
30  InstList.delete_all();
31}
32
33// Specialize setName to take care of symbol table majik
34void BasicBlock::setName(const string &name) {
35  Method *P;
36  if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
37  Value::setName(name);
38  if (P && hasName()) P->getSymbolTable()->insert(this);
39}
40
41void BasicBlock::setParent(Method *parent) {
42  if (getParent() && hasName())
43    getParent()->getSymbolTable()->remove(this);
44
45  InstList.setParent(parent);
46
47  if (getParent() && hasName())
48    getParent()->getSymbolTableSure()->insert(this);
49}
50
51TerminatorInst *BasicBlock::getTerminator() {
52  if (InstList.empty()) return 0;
53  Instruction *T = InstList.back();
54  if (T->isTerminator()) return (TerminatorInst*)T;
55  return 0;
56}
57
58const TerminatorInst *const BasicBlock::getTerminator() const {
59  if (InstList.empty()) return 0;
60  const Instruction *T = InstList.back();
61  if (T->isTerminator()) return (TerminatorInst*)T;
62  return 0;
63}
64
65void BasicBlock::dropAllReferences() {
66  for_each(InstList.begin(), InstList.end(),
67	   std::mem_fun(&Instruction::dropAllReferences));
68}
69
70// hasConstantPoolReferences() - This predicate is true if there is a
71// reference to this basic block in the constant pool for this method.  For
72// example, if a block is reached through a switch table, that table resides
73// in the constant pool, and the basic block is reference from it.
74//
75bool BasicBlock::hasConstantPoolReferences() const {
76  for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
77    if ((*I)->isConstant())
78      return true;
79
80  return false;
81}
82
83// removePredecessor - This method is used to notify a BasicBlock that the
84// specified Predecessor of the block is no longer able to reach it.  This is
85// actually not used to update the Predecessor list, but is actually used to
86// update the PHI nodes that reside in the block.  Note that this should be
87// called while the predecessor still refers to this block.
88//
89void BasicBlock::removePredecessor(BasicBlock *Pred) {
90  using cfg::pred_begin; using cfg::pred_end; using cfg::pred_iterator;
91  assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
92	 "removePredecessor: BB is not a predecessor!");
93  if (!front()->isPHINode()) return;   // Quick exit.
94
95  pred_iterator PI(pred_begin(this)), EI(pred_end(this));
96  unsigned max_idx;
97
98  // Loop over the rest of the predecessors until we run out, or until we find
99  // out that there are more than 2 predecessors.
100  for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
101
102  // If there are exactly two predecessors, then we want to nuke the PHI nodes
103  // altogether.
104  assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
105  if (max_idx <= 2) {                // <= Two predecessors BEFORE I remove one?
106    while (front()->isPHINode()) {   // Yup, loop through and nuke the PHI nodes
107      PHINode *PN = (PHINode*)front();
108      PN->removeIncomingValue(Pred); // Remove the predecessor first...
109
110      assert(PN->getNumIncomingValues() == max_idx-1 &&
111	     "PHI node shouldn't have this many values!!!");
112
113      // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
114      if (max_idx == 2)
115	PN->replaceAllUsesWith(PN->getOperand(0));
116      delete getInstList().remove(begin());  // Remove the PHI node
117    }
118  } else {
119    // Okay, now we know that we need to remove predecessor #pred_idx from all
120    // PHI nodes.  Iterate over each PHI node fixing them up
121    iterator II(begin());
122    for (; (*II)->isPHINode(); ++II) {
123      PHINode *PN = (PHINode*)*II;
124      PN->removeIncomingValue(Pred);
125    }
126  }
127}
128
129
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//
141BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
142  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 {
152    iterator EndIt = end();
153    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}
161