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