Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | #include "llvm/Method.h" |
| 11 | #include "llvm/SymbolTable.h" |
| 12 | #include "llvm/Type.h" |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 13 | #include "llvm/CFG.h" |
| 14 | #include "llvm/iOther.h" |
Vikram S. Adve | 32b5d84 | 2001-07-30 18:47:24 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 20 | template class ValueHolder<Instruction, BasicBlock, Method>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 22 | BasicBlock::BasicBlock(const string &name, Method *Parent) |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame^] | 23 | : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0), |
| 24 | machineInstrVec(new MachineCodeForBasicBlock) { |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 25 | if (Parent) |
| 26 | Parent->getBasicBlocks().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | BasicBlock::~BasicBlock() { |
| 30 | dropAllReferences(); |
| 31 | InstList.delete_all(); |
Vikram S. Adve | 32b5d84 | 2001-07-30 18:47:24 +0000 | [diff] [blame] | 32 | delete machineInstrVec; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame^] | 36 | void BasicBlock::setName(const string &name, SymbolTable *ST) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | Method *P; |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame^] | 38 | assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) && |
| 39 | "Invalid symtab argument!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this); |
| 41 | Value::setName(name); |
| 42 | if (P && hasName()) P->getSymbolTable()->insert(this); |
| 43 | } |
| 44 | |
| 45 | void BasicBlock::setParent(Method *parent) { |
| 46 | if (getParent() && hasName()) |
| 47 | getParent()->getSymbolTable()->remove(this); |
| 48 | |
| 49 | InstList.setParent(parent); |
| 50 | |
| 51 | if (getParent() && hasName()) |
| 52 | getParent()->getSymbolTableSure()->insert(this); |
| 53 | } |
| 54 | |
| 55 | TerminatorInst *BasicBlock::getTerminator() { |
| 56 | if (InstList.empty()) return 0; |
| 57 | Instruction *T = InstList.back(); |
| 58 | if (T->isTerminator()) return (TerminatorInst*)T; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | const TerminatorInst *const BasicBlock::getTerminator() const { |
| 63 | if (InstList.empty()) return 0; |
| 64 | const Instruction *T = InstList.back(); |
| 65 | if (T->isTerminator()) return (TerminatorInst*)T; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void BasicBlock::dropAllReferences() { |
| 70 | for_each(InstList.begin(), InstList.end(), |
| 71 | std::mem_fun(&Instruction::dropAllReferences)); |
| 72 | } |
| 73 | |
| 74 | // hasConstantPoolReferences() - This predicate is true if there is a |
| 75 | // reference to this basic block in the constant pool for this method. For |
| 76 | // example, if a block is reached through a switch table, that table resides |
| 77 | // in the constant pool, and the basic block is reference from it. |
| 78 | // |
| 79 | bool BasicBlock::hasConstantPoolReferences() const { |
| 80 | for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 81 | if ((*I)->isConstant()) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 82 | return true; |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 87 | // removePredecessor - This method is used to notify a BasicBlock that the |
| 88 | // specified Predecessor of the block is no longer able to reach it. This is |
| 89 | // actually not used to update the Predecessor list, but is actually used to |
| 90 | // update the PHI nodes that reside in the block. Note that this should be |
| 91 | // called while the predecessor still refers to this block. |
| 92 | // |
| 93 | void BasicBlock::removePredecessor(BasicBlock *Pred) { |
| 94 | using cfg::pred_begin; using cfg::pred_end; using cfg::pred_iterator; |
| 95 | assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) && |
| 96 | "removePredecessor: BB is not a predecessor!"); |
| 97 | if (!front()->isPHINode()) return; // Quick exit. |
| 98 | |
| 99 | pred_iterator PI(pred_begin(this)), EI(pred_end(this)); |
| 100 | unsigned max_idx; |
| 101 | |
| 102 | // Loop over the rest of the predecessors until we run out, or until we find |
| 103 | // out that there are more than 2 predecessors. |
| 104 | for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/; |
| 105 | |
| 106 | // If there are exactly two predecessors, then we want to nuke the PHI nodes |
| 107 | // altogether. |
| 108 | assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); |
| 109 | if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one? |
| 110 | while (front()->isPHINode()) { // Yup, loop through and nuke the PHI nodes |
| 111 | PHINode *PN = (PHINode*)front(); |
| 112 | PN->removeIncomingValue(Pred); // Remove the predecessor first... |
| 113 | |
| 114 | assert(PN->getNumIncomingValues() == max_idx-1 && |
| 115 | "PHI node shouldn't have this many values!!!"); |
| 116 | |
| 117 | // If the PHI _HAD_ two uses, replace PHI node with its now *single* value |
| 118 | if (max_idx == 2) |
| 119 | PN->replaceAllUsesWith(PN->getOperand(0)); |
| 120 | delete getInstList().remove(begin()); // Remove the PHI node |
| 121 | } |
| 122 | } else { |
| 123 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 124 | // PHI nodes. Iterate over each PHI node fixing them up |
| 125 | iterator II(begin()); |
| 126 | for (; (*II)->isPHINode(); ++II) { |
| 127 | PHINode *PN = (PHINode*)*II; |
| 128 | PN->removeIncomingValue(Pred); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 133 | |
| 134 | // splitBasicBlock - This splits a basic block into two at the specified |
| 135 | // instruction. Note that all instructions BEFORE the specified iterator stay |
| 136 | // as part of the original basic block, an unconditional branch is added to |
| 137 | // the new BB, and the rest of the instructions in the BB are moved to the new |
| 138 | // BB, including the old terminator. This invalidates the iterator. |
| 139 | // |
| 140 | // Note that this only works on well formed basic blocks (must have a |
| 141 | // terminator), and 'I' must not be the end of instruction list (which would |
| 142 | // cause a degenerate basic block to be formed, having a terminator inside of |
| 143 | // the basic block). |
| 144 | // |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 145 | BasicBlock *BasicBlock::splitBasicBlock(iterator I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 146 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); |
| 147 | assert(I != InstList.end() && |
| 148 | "Trying to get me to create degenerate basic block!"); |
| 149 | |
| 150 | BasicBlock *New = new BasicBlock("", getParent()); |
| 151 | |
| 152 | // Go from the end of the basic block through to the iterator pointer, moving |
| 153 | // to the new basic block... |
| 154 | Instruction *Inst = 0; |
| 155 | do { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 156 | iterator EndIt = end(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | Inst = InstList.remove(--EndIt); // Remove from end |
| 158 | New->InstList.push_front(Inst); // Add to front |
| 159 | } while (Inst != *I); // Loop until we move the specified instruction. |
| 160 | |
| 161 | // Add a branch instruction to the newly formed basic block. |
| 162 | InstList.push_back(new BranchInst(New)); |
| 163 | return New; |
| 164 | } |