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