Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 1 | //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7 | #include "llvm/BasicBlock.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 | 5cd012d | 2002-05-23 16:52:34 +0000 | [diff] [blame] | 11 | #include "llvm/Constant.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 12 | #include "llvm/iPHINode.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 13 | #include "llvm/SymbolTable.h" |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 14 | #include "Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 15 | #include "SymbolTableListTraitsImpl.h" |
| 16 | #include <algorithm> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 18 | // DummyInst - An instance of this class is used to mark the end of the |
| 19 | // instruction list. This is not a real instruction. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 21 | struct DummyInst : public Instruction { |
Chris Lattner | 69ce867 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 22 | DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 23 | // This should not be garbage monitored. |
| 24 | LeakDetector::removeGarbageObject(this); |
| 25 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 26 | |
Chris Lattner | c678c17 | 2002-07-25 15:39:05 +0000 | [diff] [blame] | 27 | virtual Instruction *clone() const { |
| 28 | assert(0 && "Cannot clone EOL");abort(); |
| 29 | return 0; |
| 30 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 31 | virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; } |
| 32 | |
| 33 | // Methods for support type inquiry through isa, cast, and dyn_cast... |
| 34 | static inline bool classof(const DummyInst *) { return true; } |
| 35 | static inline bool classof(const Instruction *I) { |
Chris Lattner | 69ce867 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 36 | return I->getOpcode() == OtherOpsEnd; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 37 | } |
| 38 | static inline bool classof(const Value *V) { |
| 39 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | Instruction *ilist_traits<Instruction>::createNode() { |
| 44 | return new DummyInst(); |
| 45 | } |
| 46 | iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) { |
| 47 | return BB->getInstList(); |
| 48 | } |
| 49 | |
| 50 | // Explicit instantiation of SymbolTableListTraits since some of the methods |
| 51 | // are not in the public header file... |
| 52 | template SymbolTableListTraits<Instruction, BasicBlock, Function>; |
| 53 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 55 | // BasicBlock ctor - If the function parameter is specified, the basic block is |
| 56 | // automatically inserted at the end of the function. |
| 57 | // |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 58 | BasicBlock::BasicBlock(const std::string &name, Function *Parent) |
Vikram S. Adve | 6e792fc | 2002-07-08 22:31:11 +0000 | [diff] [blame] | 59 | : Value(Type::LabelTy, Value::BasicBlockVal, name) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 60 | // Initialize the instlist... |
| 61 | InstList.setItemParent(this); |
| 62 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 63 | // Make sure that we get added to a function |
| 64 | LeakDetector::addGarbageObject(this); |
| 65 | |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 66 | if (Parent) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | Parent->getBasicBlockList().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 4dfede8 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 70 | /// BasicBlock ctor - If the InsertBefore parameter is specified, the basic |
| 71 | /// block is automatically inserted right before the specified block. |
| 72 | /// |
| 73 | BasicBlock::BasicBlock(const std::string &Name, BasicBlock *InsertBefore) |
| 74 | : Value(Type::LabelTy, Value::BasicBlockVal, Name) { |
| 75 | // Initialize the instlist... |
| 76 | InstList.setItemParent(this); |
| 77 | |
| 78 | // Make sure that we get added to a function |
| 79 | LeakDetector::addGarbageObject(this); |
| 80 | |
| 81 | if (InsertBefore) { |
| 82 | assert(InsertBefore->getParent() && |
| 83 | "Cannot insert block before another block that is not embedded into" |
| 84 | " a function yet!"); |
| 85 | InsertBefore->getParent()->getBasicBlockList().insert(InsertBefore, this); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 90 | BasicBlock::~BasicBlock() { |
| 91 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 92 | InstList.clear(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 95 | void BasicBlock::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 96 | if (getParent()) |
| 97 | LeakDetector::addGarbageObject(this); |
| 98 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 99 | InstList.setParent(parent); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 100 | |
| 101 | if (getParent()) |
| 102 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 106 | void BasicBlock::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 107 | Function *P; |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 108 | assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) && |
Chris Lattner | 2d189a5 | 2001-09-07 16:44:17 +0000 | [diff] [blame] | 109 | "Invalid symtab argument!"); |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 110 | if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | Value::setName(name); |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 112 | if (P && hasName()) P->getSymbolTable().insert(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 115 | TerminatorInst *BasicBlock::getTerminator() { |
| 116 | if (InstList.empty()) return 0; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 117 | return dyn_cast<TerminatorInst>(&InstList.back()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | const TerminatorInst *const BasicBlock::getTerminator() const { |
| 121 | if (InstList.empty()) return 0; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 122 | return dyn_cast<TerminatorInst>(&InstList.back()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void BasicBlock::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 126 | for(iterator I = begin(), E = end(); I != E; ++I) |
| 127 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 130 | // hasConstantReferences() - This predicate is true if there is a |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 131 | // reference to this basic block in the constant pool for this method. For |
| 132 | // example, if a block is reached through a switch table, that table resides |
| 133 | // in the constant pool, and the basic block is reference from it. |
| 134 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 135 | bool BasicBlock::hasConstantReferences() const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | 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] | 137 | if (::isa<Constant>((Value*)*I)) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 138 | return true; |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 143 | // removePredecessor - This method is used to notify a BasicBlock that the |
| 144 | // specified Predecessor of the block is no longer able to reach it. This is |
| 145 | // actually not used to update the Predecessor list, but is actually used to |
| 146 | // update the PHI nodes that reside in the block. Note that this should be |
| 147 | // called while the predecessor still refers to this block. |
| 148 | // |
| 149 | void BasicBlock::removePredecessor(BasicBlock *Pred) { |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 150 | assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) && |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 151 | "removePredecessor: BB is not a predecessor!"); |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 152 | if (!isa<PHINode>(front())) return; // Quick exit. |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 154 | pred_iterator PI(pred_begin(this)), EI(pred_end(this)); |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 155 | unsigned max_idx; |
| 156 | |
| 157 | // Loop over the rest of the predecessors until we run out, or until we find |
| 158 | // out that there are more than 2 predecessors. |
| 159 | for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/; |
| 160 | |
| 161 | // 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] | 162 | // altogether. We cannot do this, however if this in this case however: |
| 163 | // |
| 164 | // Loop: |
| 165 | // %x = phi [X, Loop] |
| 166 | // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 |
| 167 | // br Loop ;; %x2 does not dominate all uses |
| 168 | // |
| 169 | // This is because the PHI node input is actually taken from the predecessor |
| 170 | // basic block. The only case this can happen is with a self loop, so we |
| 171 | // check for this case explicitly now. |
| 172 | // |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 173 | assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); |
Chris Lattner | f7373e4 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 174 | if (max_idx == 2) { |
| 175 | PI = pred_begin(this); |
| 176 | BasicBlock *Other = *PI == Pred ? *++PI : *PI; |
| 177 | |
| 178 | // Disable PHI elimination! |
| 179 | if (this == Other) max_idx = 3; |
| 180 | } |
| 181 | |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 182 | if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one? |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 183 | // Yup, loop through and nuke the PHI nodes |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 184 | while (PHINode *PN = dyn_cast<PHINode>(&front())) { |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 185 | PN->removeIncomingValue(Pred); // Remove the predecessor first... |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 186 | |
| 187 | // If the PHI _HAD_ two uses, replace PHI node with its now *single* value |
Chris Lattner | caf5b50 | 2002-10-08 21:36:34 +0000 | [diff] [blame] | 188 | if (max_idx == 2) { |
Chris Lattner | ef8c833 | 2003-04-25 23:14:19 +0000 | [diff] [blame^] | 189 | if (PN->getOperand(0) != PN) |
| 190 | PN->replaceAllUsesWith(PN->getOperand(0)); |
| 191 | else |
| 192 | // We are left with an infinite loop with no entries: kill the PHI. |
| 193 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
Chris Lattner | caf5b50 | 2002-10-08 21:36:34 +0000 | [diff] [blame] | 194 | getInstList().pop_front(); // Remove the PHI node |
| 195 | } |
| 196 | |
| 197 | // If the PHI node already only had one entry, it got deleted by |
| 198 | // removeIncomingValue. |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 199 | } |
| 200 | } else { |
| 201 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 202 | // PHI nodes. Iterate over each PHI node fixing them up |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 203 | for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(II); ++II) |
Chris Lattner | f7373e4 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 204 | PN->removeIncomingValue(Pred); |
Chris Lattner | 615d3cf | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | |
| 209 | // splitBasicBlock - This splits a basic block into two at the specified |
| 210 | // instruction. Note that all instructions BEFORE the specified iterator stay |
| 211 | // as part of the original basic block, an unconditional branch is added to |
| 212 | // the new BB, and the rest of the instructions in the BB are moved to the new |
| 213 | // BB, including the old terminator. This invalidates the iterator. |
| 214 | // |
| 215 | // Note that this only works on well formed basic blocks (must have a |
| 216 | // terminator), and 'I' must not be the end of instruction list (which would |
| 217 | // cause a degenerate basic block to be formed, having a terminator inside of |
| 218 | // the basic block). |
| 219 | // |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 220 | BasicBlock *BasicBlock::splitBasicBlock(iterator I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); |
| 222 | assert(I != InstList.end() && |
| 223 | "Trying to get me to create degenerate basic block!"); |
| 224 | |
| 225 | BasicBlock *New = new BasicBlock("", getParent()); |
| 226 | |
| 227 | // Go from the end of the basic block through to the iterator pointer, moving |
| 228 | // to the new basic block... |
| 229 | Instruction *Inst = 0; |
| 230 | do { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 231 | iterator EndIt = end(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 232 | Inst = InstList.remove(--EndIt); // Remove from end |
| 233 | New->InstList.push_front(Inst); // Add to front |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 234 | } while (Inst != &*I); // Loop until we move the specified instruction. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | |
| 236 | // Add a branch instruction to the newly formed basic block. |
| 237 | InstList.push_back(new BranchInst(New)); |
Chris Lattner | 4eed0b8 | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 238 | |
| 239 | // Now we must loop through all of the successors of the New block (which |
| 240 | // _were_ the successors of the 'this' block), and update any PHI nodes in |
| 241 | // successors. If there were PHI nodes in the successors, then they need to |
| 242 | // know that incoming branches will be from New, not from Old. |
| 243 | // |
| 244 | for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New); |
| 245 | I != E; ++I) { |
| 246 | // Loop over any phi nodes in the basic block, updating the BB field of |
| 247 | // incoming values... |
| 248 | BasicBlock *Successor = *I; |
| 249 | for (BasicBlock::iterator II = Successor->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 250 | PHINode *PN = dyn_cast<PHINode>(II); ++II) { |
Chris Lattner | 4eed0b8 | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 251 | int IDX = PN->getBasicBlockIndex(this); |
| 252 | while (IDX != -1) { |
| 253 | PN->setIncomingBlock((unsigned)IDX, New); |
| 254 | IDX = PN->getBasicBlockIndex(this); |
| 255 | } |
| 256 | } |
| 257 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | return New; |
| 259 | } |