BasicBlock.cpp revision 009505452b713ed2e3a8e99c5545a6e721c65495
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
15// Instantiate Templates - This ugliness is the price we have to pay
16// for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
17//
18template class ValueHolder<Instruction, BasicBlock>;
19
20BasicBlock::BasicBlock(const string &name, Method *parent)
21  : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0) {
22
23  if (parent)
24    parent->getBasicBlocks().push_back(this);
25}
26
27BasicBlock::~BasicBlock() {
28  dropAllReferences();
29  InstList.delete_all();
30}
31
32// Specialize setName to take care of symbol table majik
33void BasicBlock::setName(const string &name) {
34  Method *P;
35  if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
36  Value::setName(name);
37  if (P && hasName()) P->getSymbolTable()->insert(this);
38}
39
40void BasicBlock::setParent(Method *parent) {
41  if (getParent() && hasName())
42    getParent()->getSymbolTable()->remove(this);
43
44  InstList.setParent(parent);
45
46  if (getParent() && hasName())
47    getParent()->getSymbolTableSure()->insert(this);
48}
49
50TerminatorInst *BasicBlock::getTerminator() {
51  if (InstList.empty()) return 0;
52  Instruction *T = InstList.back();
53  if (T->isTerminator()) return (TerminatorInst*)T;
54  return 0;
55}
56
57const TerminatorInst *const BasicBlock::getTerminator() const {
58  if (InstList.empty()) return 0;
59  const Instruction *T = InstList.back();
60  if (T->isTerminator()) return (TerminatorInst*)T;
61  return 0;
62}
63
64void BasicBlock::dropAllReferences() {
65  for_each(InstList.begin(), InstList.end(),
66	   std::mem_fun(&Instruction::dropAllReferences));
67}
68
69// hasConstantPoolReferences() - This predicate is true if there is a
70// reference to this basic block in the constant pool for this method.  For
71// example, if a block is reached through a switch table, that table resides
72// in the constant pool, and the basic block is reference from it.
73//
74bool BasicBlock::hasConstantPoolReferences() const {
75  for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
76    if ((*I)->getValueType() == ConstantVal)
77      return true;
78
79  return false;
80}
81
82
83// splitBasicBlock - This splits a basic block into two at the specified
84// instruction.  Note that all instructions BEFORE the specified iterator stay
85// as part of the original basic block, an unconditional branch is added to
86// the new BB, and the rest of the instructions in the BB are moved to the new
87// BB, including the old terminator.  This invalidates the iterator.
88//
89// Note that this only works on well formed basic blocks (must have a
90// terminator), and 'I' must not be the end of instruction list (which would
91// cause a degenerate basic block to be formed, having a terminator inside of
92// the basic block).
93//
94BasicBlock *BasicBlock::splitBasicBlock(InstListType::iterator I) {
95  assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
96  assert(I != InstList.end() &&
97	 "Trying to get me to create degenerate basic block!");
98
99  BasicBlock *New = new BasicBlock("", getParent());
100
101  // Go from the end of the basic block through to the iterator pointer, moving
102  // to the new basic block...
103  Instruction *Inst = 0;
104  do {
105    InstListType::iterator EndIt = InstList.end();
106    Inst = InstList.remove(--EndIt);                  // Remove from end
107    New->InstList.push_front(Inst);                   // Add to front
108  } while (Inst != *I);   // Loop until we move the specified instruction.
109
110  // Add a branch instruction to the newly formed basic block.
111  InstList.push_back(new BranchInst(New));
112  return New;
113}
114