1 //===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
3 // This file implements the Method class for the VMCore library.
5 //===----------------------------------------------------------------------===//
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"
15 // Instantiate Templates - This ugliness is the price we have to pay
16 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
18 template class ValueHolder<Instruction, BasicBlock>;
20 BasicBlock::BasicBlock(const string &name, Method *parent)
21 : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0) {
24 parent->getBasicBlocks().push_back(this);
27 BasicBlock::~BasicBlock() {
29 InstList.delete_all();
32 // Specialize setName to take care of symbol table majik
33 void BasicBlock::setName(const string &name) {
35 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
37 if (P && hasName()) P->getSymbolTable()->insert(this);
40 void BasicBlock::setParent(Method *parent) {
41 if (getParent() && hasName())
42 getParent()->getSymbolTable()->remove(this);
44 InstList.setParent(parent);
46 if (getParent() && hasName())
47 getParent()->getSymbolTableSure()->insert(this);
50 TerminatorInst *BasicBlock::getTerminator() {
51 if (InstList.empty()) return 0;
52 Instruction *T = InstList.back();
53 if (T->isTerminator()) return (TerminatorInst*)T;
57 const TerminatorInst *const BasicBlock::getTerminator() const {
58 if (InstList.empty()) return 0;
59 const Instruction *T = InstList.back();
60 if (T->isTerminator()) return (TerminatorInst*)T;
64 void BasicBlock::dropAllReferences() {
65 for_each(InstList.begin(), InstList.end(),
66 std::mem_fun(&Instruction::dropAllReferences));
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.
74 bool BasicBlock::hasConstantPoolReferences() const {
75 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
76 if ((*I)->getValueType() == ConstantVal)
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.
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
94 BasicBlock *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!");
99 BasicBlock *New = new BasicBlock("", getParent());
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;
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.
110 // Add a branch instruction to the newly formed basic block.
111 InstList.push_back(new BranchInst(New));