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