Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=// |
| 2 | // |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 3 | // This file implements the BasicBlock class for the VMCore library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 6e6f5be | 2002-04-08 00:15:29 +0000 | [diff] [blame] | 7 | #include "ValueHolderImpl.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 8 | #include "llvm/iTerminators.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | #include "llvm/Type.h" |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 10 | #include "llvm/Support/CFG.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 11 | #include "llvm/iPHINode.h" |
Vikram S. Adve | 32b5d84 | 2001-07-30 18:47:24 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | |
| 14 | // Instantiate Templates - This ugliness is the price we have to pay |
| 15 | // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :( |
| 16 | // |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 17 | template class ValueHolder<Instruction, BasicBlock, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 19 | BasicBlock::BasicBlock(const std::string &name, Function *Parent) |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame] | 20 | : Value(Type::LabelTy, Value::BasicBlockVal, name), InstList(this, 0), |
| 21 | machineInstrVec(new MachineCodeForBasicBlock) { |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 22 | if (Parent) |
| 23 | Parent->getBasicBlocks().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | BasicBlock::~BasicBlock() { |
| 27 | dropAllReferences(); |
| 28 | InstList.delete_all(); |
Vikram S. Adve | 32b5d84 | 2001-07-30 18:47:24 +0000 | [diff] [blame] | 29 | delete machineInstrVec; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 33 | void BasicBlock::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 34 | Function *P; |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame] | 35 | assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) && |
| 36 | "Invalid symtab argument!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this); |
| 38 | Value::setName(name); |
| 39 | if (P && hasName()) P->getSymbolTable()->insert(this); |
| 40 | } |
| 41 | |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 42 | void BasicBlock::setParent(Function *parent) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | if (getParent() && hasName()) |
| 44 | getParent()->getSymbolTable()->remove(this); |
| 45 | |
| 46 | InstList.setParent(parent); |
| 47 | |
| 48 | if (getParent() && hasName()) |
| 49 | getParent()->getSymbolTableSure()->insert(this); |
| 50 | } |
| 51 | |
| 52 | TerminatorInst *BasicBlock::getTerminator() { |
| 53 | if (InstList.empty()) return 0; |
| 54 | Instruction *T = InstList.back(); |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 55 | if (isa<TerminatorInst>(T)) return cast<TerminatorInst>(T); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | const TerminatorInst *const BasicBlock::getTerminator() const { |
| 60 | if (InstList.empty()) return 0; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 61 | if (const TerminatorInst *TI = dyn_cast<TerminatorInst>(InstList.back())) |
| 62 | return TI; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | void BasicBlock::dropAllReferences() { |
| 67 | for_each(InstList.begin(), InstList.end(), |
| 68 | std::mem_fun(&Instruction::dropAllReferences)); |
| 69 | } |
| 70 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 71 | // hasConstantReferences() - This predicate is true if there is a |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 72 | // reference to this basic block in the constant pool for this method. For |
| 73 | // example, if a block is reached through a switch table, that table resides |
| 74 | // in the constant pool, and the basic block is reference from it. |
| 75 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 76 | bool BasicBlock::hasConstantReferences() const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 78 | if (::isa<Constant>((Value*)*I)) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | return true; |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 84 | // removePredecessor - This method is used to notify a BasicBlock that the |
| 85 | // specified Predecessor of the block is no longer able to reach it. This is |
| 86 | // actually not used to update the Predecessor list, but is actually used to |
| 87 | // update the PHI nodes that reside in the block. Note that this should be |
| 88 | // called while the predecessor still refers to this block. |
| 89 | // |
| 90 | void BasicBlock::removePredecessor(BasicBlock *Pred) { |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 91 | assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) && |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 92 | "removePredecessor: BB is not a predecessor!"); |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 93 | if (!isa<PHINode>(front())) return; // Quick exit. |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 95 | pred_iterator PI(pred_begin(this)), EI(pred_end(this)); |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 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 |
Chris Lattner | f7373e4 | 2002-05-21 19:52:49 +0000 | [diff] [blame^] | 103 | // altogether. We cannot do this, however if this in this case however: |
| 104 | // |
| 105 | // Loop: |
| 106 | // %x = phi [X, Loop] |
| 107 | // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 |
| 108 | // br Loop ;; %x2 does not dominate all uses |
| 109 | // |
| 110 | // This is because the PHI node input is actually taken from the predecessor |
| 111 | // basic block. The only case this can happen is with a self loop, so we |
| 112 | // check for this case explicitly now. |
| 113 | // |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 114 | assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); |
Chris Lattner | f7373e4 | 2002-05-21 19:52:49 +0000 | [diff] [blame^] | 115 | if (max_idx == 2) { |
| 116 | PI = pred_begin(this); |
| 117 | BasicBlock *Other = *PI == Pred ? *++PI : *PI; |
| 118 | |
| 119 | // Disable PHI elimination! |
| 120 | if (this == Other) max_idx = 3; |
| 121 | } |
| 122 | |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 123 | if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one? |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 124 | // Yup, loop through and nuke the PHI nodes |
| 125 | while (PHINode *PN = dyn_cast<PHINode>(front())) { |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 126 | PN->removeIncomingValue(Pred); // Remove the predecessor first... |
| 127 | |
| 128 | assert(PN->getNumIncomingValues() == max_idx-1 && |
| 129 | "PHI node shouldn't have this many values!!!"); |
| 130 | |
| 131 | // If the PHI _HAD_ two uses, replace PHI node with its now *single* value |
| 132 | if (max_idx == 2) |
| 133 | PN->replaceAllUsesWith(PN->getOperand(0)); |
| 134 | delete getInstList().remove(begin()); // Remove the PHI node |
| 135 | } |
| 136 | } else { |
| 137 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 138 | // PHI nodes. Iterate over each PHI node fixing them up |
Chris Lattner | f7373e4 | 2002-05-21 19:52:49 +0000 | [diff] [blame^] | 139 | for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(*II); ++II) |
| 140 | PN->removeIncomingValue(Pred); |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 144 | |
| 145 | // splitBasicBlock - This splits a basic block into two at the specified |
| 146 | // instruction. Note that all instructions BEFORE the specified iterator stay |
| 147 | // as part of the original basic block, an unconditional branch is added to |
| 148 | // the new BB, and the rest of the instructions in the BB are moved to the new |
| 149 | // BB, including the old terminator. This invalidates the iterator. |
| 150 | // |
| 151 | // Note that this only works on well formed basic blocks (must have a |
| 152 | // terminator), and 'I' must not be the end of instruction list (which would |
| 153 | // cause a degenerate basic block to be formed, having a terminator inside of |
| 154 | // the basic block). |
| 155 | // |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 156 | BasicBlock *BasicBlock::splitBasicBlock(iterator I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); |
| 158 | assert(I != InstList.end() && |
| 159 | "Trying to get me to create degenerate basic block!"); |
| 160 | |
| 161 | BasicBlock *New = new BasicBlock("", getParent()); |
| 162 | |
| 163 | // Go from the end of the basic block through to the iterator pointer, moving |
| 164 | // to the new basic block... |
| 165 | Instruction *Inst = 0; |
| 166 | do { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 167 | iterator EndIt = end(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 168 | Inst = InstList.remove(--EndIt); // Remove from end |
| 169 | New->InstList.push_front(Inst); // Add to front |
| 170 | } while (Inst != *I); // Loop until we move the specified instruction. |
| 171 | |
| 172 | // Add a branch instruction to the newly formed basic block. |
| 173 | InstList.push_back(new BranchInst(New)); |
Chris Lattner | 4eed0b8 | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 174 | |
| 175 | // Now we must loop through all of the successors of the New block (which |
| 176 | // _were_ the successors of the 'this' block), and update any PHI nodes in |
| 177 | // successors. If there were PHI nodes in the successors, then they need to |
| 178 | // know that incoming branches will be from New, not from Old. |
| 179 | // |
| 180 | for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New); |
| 181 | I != E; ++I) { |
| 182 | // Loop over any phi nodes in the basic block, updating the BB field of |
| 183 | // incoming values... |
| 184 | BasicBlock *Successor = *I; |
| 185 | for (BasicBlock::iterator II = Successor->begin(); |
| 186 | PHINode *PN = dyn_cast<PHINode>(*II); ++II) { |
| 187 | int IDX = PN->getBasicBlockIndex(this); |
| 188 | while (IDX != -1) { |
| 189 | PN->setIncomingBlock((unsigned)IDX, New); |
| 190 | IDX = PN->getBasicBlockIndex(this); |
| 191 | } |
| 192 | } |
| 193 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 194 | return New; |
| 195 | } |