Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 1 | //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the BasicBlock class for the IR library. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "SymbolTableListTraitsImpl.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Instructions.h" |
| 19 | #include "llvm/IR/IntrinsicInst.h" |
| 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/Type.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | |
Gabor Greif | 0dd2a6a | 2009-03-07 12:33:24 +0000 | [diff] [blame] | 27 | ValueSymbolTable *BasicBlock::getValueSymbolTable() { |
| 28 | if (Function *F = getParent()) |
| 29 | return &F->getValueSymbolTable(); |
Chris Lattner | 17fcdd5 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 33 | LLVMContext &BasicBlock::getContext() const { |
| 34 | return getType()->getContext(); |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 37 | // Explicit instantiation of SymbolTableListTraits since some of the methods |
| 38 | // are not in the public header file... |
John McCall | c63ca0a | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 39 | template class llvm::SymbolTableListTraits<Instruction, BasicBlock>; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 41 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 42 | BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 43 | BasicBlock *InsertBefore) |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 44 | : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(0) { |
Chris Lattner | 0a1a874 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 45 | |
| 46 | // Make sure that we get added to a function |
| 47 | LeakDetector::addGarbageObject(this); |
| 48 | |
| 49 | if (InsertBefore) { |
Chris Lattner | 17fcdd5 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 50 | assert(NewParent && |
Chris Lattner | 4f05611 | 2004-02-04 03:57:50 +0000 | [diff] [blame] | 51 | "Cannot insert block before another block with no function!"); |
Chris Lattner | 17fcdd5 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 52 | NewParent->getBasicBlockList().insert(InsertBefore, this); |
| 53 | } else if (NewParent) { |
| 54 | NewParent->getBasicBlockList().push_back(this); |
Chris Lattner | 0a1a874 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 55 | } |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 56 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 57 | setName(Name); |
Chris Lattner | 0a1a874 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 60 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 61 | BasicBlock::~BasicBlock() { |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 62 | // If the address of the block is taken and it is being deleted (e.g. because |
| 63 | // it is dead), this means that there is either a dangling constant expr |
| 64 | // hanging off the block, or an undefined use of the block (source code |
| 65 | // expecting the address of a label to keep the block alive even though there |
| 66 | // is no indirect branch). Handle these cases by zapping the BlockAddress |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 67 | // nodes. There are no other possible uses at this point. |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 68 | if (hasAddressTaken()) { |
| 69 | assert(!use_empty() && "There should be at least one blockaddress!"); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 70 | Constant *Replacement = |
| 71 | ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1); |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 72 | while (!use_empty()) { |
| 73 | BlockAddress *BA = cast<BlockAddress>(use_back()); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 74 | BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, |
| 75 | BA->getType())); |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 76 | BA->destroyConstant(); |
| 77 | } |
| 78 | } |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 79 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 80 | assert(getParent() == 0 && "BasicBlock still linked into the program!"); |
| 81 | dropAllReferences(); |
| 82 | InstList.clear(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 85 | void BasicBlock::setParent(Function *parent) { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 86 | if (getParent()) |
| 87 | LeakDetector::addGarbageObject(this); |
| 88 | |
Chris Lattner | 17fcdd5 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 89 | // Set Parent=parent, updating instruction symtab entries as appropriate. |
| 90 | InstList.setSymTabObject(&Parent, parent); |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 91 | |
| 92 | if (getParent()) |
| 93 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 96 | void BasicBlock::removeFromParent() { |
| 97 | getParent()->getBasicBlockList().remove(this); |
| 98 | } |
| 99 | |
| 100 | void BasicBlock::eraseFromParent() { |
| 101 | getParent()->getBasicBlockList().erase(this); |
| 102 | } |
| 103 | |
Chris Lattner | a71965b | 2006-09-23 04:03:45 +0000 | [diff] [blame] | 104 | /// moveBefore - Unlink this basic block from its current function and |
| 105 | /// insert it into the function that MovePos lives in, right before MovePos. |
Chris Lattner | 6a13aed | 2005-08-12 22:14:06 +0000 | [diff] [blame] | 106 | void BasicBlock::moveBefore(BasicBlock *MovePos) { |
| 107 | MovePos->getParent()->getBasicBlockList().splice(MovePos, |
| 108 | getParent()->getBasicBlockList(), this); |
| 109 | } |
| 110 | |
Chris Lattner | a71965b | 2006-09-23 04:03:45 +0000 | [diff] [blame] | 111 | /// moveAfter - Unlink this basic block from its current function and |
| 112 | /// insert it into the function that MovePos lives in, right after MovePos. |
| 113 | void BasicBlock::moveAfter(BasicBlock *MovePos) { |
| 114 | Function::iterator I = MovePos; |
| 115 | MovePos->getParent()->getBasicBlockList().splice(++I, |
| 116 | getParent()->getBasicBlockList(), this); |
| 117 | } |
| 118 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 120 | TerminatorInst *BasicBlock::getTerminator() { |
| 121 | if (InstList.empty()) return 0; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 122 | return dyn_cast<TerminatorInst>(&InstList.back()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Dan Gohman | 50cdabc | 2007-11-19 20:46:23 +0000 | [diff] [blame] | 125 | const TerminatorInst *BasicBlock::getTerminator() const { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 126 | if (InstList.empty()) return 0; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 127 | return dyn_cast<TerminatorInst>(&InstList.back()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 130 | Instruction* BasicBlock::getFirstNonPHI() { |
| 131 | BasicBlock::iterator i = begin(); |
| 132 | // All valid basic blocks should have a terminator, |
| 133 | // which is not a PHINode. If we have an invalid basic |
| 134 | // block we'll get an assertion failure when dereferencing |
| 135 | // a past-the-end iterator. |
| 136 | while (isa<PHINode>(i)) ++i; |
| 137 | return &*i; |
Vladimir Prus | dd49dbf | 2006-06-08 15:46:18 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Dale Johannesen | 7249ef0 | 2010-04-02 21:49:27 +0000 | [diff] [blame] | 140 | Instruction* BasicBlock::getFirstNonPHIOrDbg() { |
| 141 | BasicBlock::iterator i = begin(); |
| 142 | // All valid basic blocks should have a terminator, |
| 143 | // which is not a PHINode. If we have an invalid basic |
| 144 | // block we'll get an assertion failure when dereferencing |
| 145 | // a past-the-end iterator. |
| 146 | while (isa<PHINode>(i) || isa<DbgInfoIntrinsic>(i)) ++i; |
| 147 | return &*i; |
| 148 | } |
| 149 | |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 150 | Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() { |
| 151 | // All valid basic blocks should have a terminator, |
| 152 | // which is not a PHINode. If we have an invalid basic |
| 153 | // block we'll get an assertion failure when dereferencing |
| 154 | // a past-the-end iterator. |
| 155 | BasicBlock::iterator i = begin(); |
| 156 | for (;; ++i) { |
| 157 | if (isa<PHINode>(i) || isa<DbgInfoIntrinsic>(i)) |
| 158 | continue; |
| 159 | |
| 160 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(i); |
| 161 | if (!II) |
| 162 | break; |
| 163 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 164 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 165 | break; |
| 166 | } |
| 167 | return &*i; |
| 168 | } |
| 169 | |
Bill Wendling | d2e103d | 2011-08-16 20:42:52 +0000 | [diff] [blame] | 170 | BasicBlock::iterator BasicBlock::getFirstInsertionPt() { |
| 171 | iterator InsertPt = getFirstNonPHI(); |
| 172 | if (isa<LandingPadInst>(InsertPt)) ++InsertPt; |
| 173 | return InsertPt; |
| 174 | } |
| 175 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | void BasicBlock::dropAllReferences() { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 177 | for(iterator I = begin(), E = end(); I != E; ++I) |
| 178 | I->dropAllReferences(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | ad993cb | 2005-02-24 02:37:26 +0000 | [diff] [blame] | 181 | /// getSinglePredecessor - If this basic block has a single predecessor block, |
| 182 | /// return the block, otherwise return a null pointer. |
| 183 | BasicBlock *BasicBlock::getSinglePredecessor() { |
| 184 | pred_iterator PI = pred_begin(this), E = pred_end(this); |
| 185 | if (PI == E) return 0; // No preds. |
| 186 | BasicBlock *ThePred = *PI; |
| 187 | ++PI; |
| 188 | return (PI == E) ? ThePred : 0 /*multiple preds*/; |
| 189 | } |
| 190 | |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 191 | /// getUniquePredecessor - If this basic block has a unique predecessor block, |
| 192 | /// return the block, otherwise return a null pointer. |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 193 | /// Note that unique predecessor doesn't mean single edge, there can be |
| 194 | /// multiple edges from the unique predecessor to this block (for example |
Torok Edwin | 9053a73 | 2008-12-11 11:44:49 +0000 | [diff] [blame] | 195 | /// a switch statement with multiple cases having the same destination). |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 196 | BasicBlock *BasicBlock::getUniquePredecessor() { |
| 197 | pred_iterator PI = pred_begin(this), E = pred_end(this); |
| 198 | if (PI == E) return 0; // No preds. |
| 199 | BasicBlock *PredBB = *PI; |
| 200 | ++PI; |
| 201 | for (;PI != E; ++PI) { |
| 202 | if (*PI != PredBB) |
| 203 | return 0; |
Torok Edwin | 9053a73 | 2008-12-11 11:44:49 +0000 | [diff] [blame] | 204 | // The same predecessor appears multiple times in the predecessor list. |
| 205 | // This is OK. |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 206 | } |
| 207 | return PredBB; |
| 208 | } |
| 209 | |
Chris Lattner | 566f600 | 2005-04-21 16:06:03 +0000 | [diff] [blame] | 210 | /// removePredecessor - This method is used to notify a BasicBlock that the |
| 211 | /// specified Predecessor of the block is no longer able to reach it. This is |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 212 | /// actually not used to update the Predecessor list, but is actually used to |
Chris Lattner | 566f600 | 2005-04-21 16:06:03 +0000 | [diff] [blame] | 213 | /// update the PHI nodes that reside in the block. Note that this should be |
| 214 | /// called while the predecessor still refers to this block. |
| 215 | /// |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 216 | void BasicBlock::removePredecessor(BasicBlock *Pred, |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 217 | bool DontDeleteUselessPHIs) { |
Chris Lattner | 1f21ef1 | 2005-02-23 16:53:04 +0000 | [diff] [blame] | 218 | assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. |
Chris Lattner | 35f0aec | 2005-02-23 07:09:08 +0000 | [diff] [blame] | 219 | find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 220 | "removePredecessor: BB is not a predecessor!"); |
Chris Lattner | 35f0aec | 2005-02-23 07:09:08 +0000 | [diff] [blame] | 221 | |
Chris Lattner | f23586c | 2004-12-11 22:10:29 +0000 | [diff] [blame] | 222 | if (InstList.empty()) return; |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 223 | PHINode *APN = dyn_cast<PHINode>(&front()); |
| 224 | if (!APN) return; // Quick exit. |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 225 | |
| 226 | // If there are exactly two predecessors, then we want to nuke the PHI nodes |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 227 | // altogether. However, we cannot do this, if this in this case: |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 228 | // |
| 229 | // Loop: |
| 230 | // %x = phi [X, Loop] |
| 231 | // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 |
| 232 | // br Loop ;; %x2 does not dominate all uses |
| 233 | // |
| 234 | // This is because the PHI node input is actually taken from the predecessor |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 235 | // basic block. The only case this can happen is with a self loop, so we |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 236 | // check for this case explicitly now. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 237 | // |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 238 | unsigned max_idx = APN->getNumIncomingValues(); |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 239 | assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 240 | if (max_idx == 2) { |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 241 | BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 242 | |
| 243 | // Disable PHI elimination! |
| 244 | if (this == Other) max_idx = 3; |
| 245 | } |
| 246 | |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 247 | // <= Two predecessors BEFORE I remove one? |
| 248 | if (max_idx <= 2 && !DontDeleteUselessPHIs) { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 249 | // Yup, loop through and nuke the PHI nodes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 250 | while (PHINode *PN = dyn_cast<PHINode>(&front())) { |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 251 | // Remove the predecessor first. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 252 | PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 253 | |
| 254 | // If the PHI _HAD_ two uses, replace PHI node with its now *single* value |
Chris Lattner | dee430d | 2002-10-08 21:36:34 +0000 | [diff] [blame] | 255 | if (max_idx == 2) { |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 256 | if (PN->getIncomingValue(0) != PN) |
| 257 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
Chris Lattner | 02a78cf | 2003-04-25 23:14:19 +0000 | [diff] [blame] | 258 | else |
| 259 | // We are left with an infinite loop with no entries: kill the PHI. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 260 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | dee430d | 2002-10-08 21:36:34 +0000 | [diff] [blame] | 261 | getInstList().pop_front(); // Remove the PHI node |
| 262 | } |
| 263 | |
| 264 | // If the PHI node already only had one entry, it got deleted by |
| 265 | // removeIncomingValue. |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 266 | } |
| 267 | } else { |
| 268 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 269 | // PHI nodes. Iterate over each PHI node fixing them up |
Chris Lattner | f878218 | 2004-06-05 00:11:27 +0000 | [diff] [blame] | 270 | PHINode *PN; |
Chris Lattner | 80f4d88 | 2005-08-05 15:34:10 +0000 | [diff] [blame] | 271 | for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { |
| 272 | ++II; |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 273 | PN->removeIncomingValue(Pred, false); |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 274 | // If all incoming values to the Phi are the same, we can replace the Phi |
| 275 | // with that value. |
Owen Anderson | 90245d4 | 2006-06-14 04:43:14 +0000 | [diff] [blame] | 276 | Value* PNV = 0; |
Duncan Sands | 23a1957 | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 277 | if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue())) |
| 278 | if (PNV != PN) { |
| 279 | PN->replaceAllUsesWith(PNV); |
| 280 | PN->eraseFromParent(); |
| 281 | } |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 287 | /// splitBasicBlock - This splits a basic block into two at the specified |
| 288 | /// instruction. Note that all instructions BEFORE the specified iterator stay |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 289 | /// as part of the original basic block, an unconditional branch is added to |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 290 | /// the new BB, and the rest of the instructions in the BB are moved to the new |
| 291 | /// BB, including the old terminator. This invalidates the iterator. |
| 292 | /// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 293 | /// Note that this only works on well formed basic blocks (must have a |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 294 | /// terminator), and 'I' must not be the end of instruction list (which would |
| 295 | /// cause a degenerate basic block to be formed, having a terminator inside of |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 296 | /// the basic block). |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 297 | /// |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 298 | BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 299 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 300 | assert(I != InstList.end() && |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 301 | "Trying to get me to create degenerate basic block!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 303 | BasicBlock *InsertBefore = llvm::next(Function::iterator(this)) |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 304 | .getNodePtrUnchecked(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 305 | BasicBlock *New = BasicBlock::Create(getContext(), BBName, |
| 306 | getParent(), InsertBefore); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 307 | |
Chris Lattner | f2c3106 | 2004-02-03 23:11:21 +0000 | [diff] [blame] | 308 | // Move all of the specified instructions from the original basic block into |
| 309 | // the new basic block. |
| 310 | New->getInstList().splice(New->end(), this->getInstList(), I, end()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | |
| 312 | // Add a branch instruction to the newly formed basic block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 313 | BranchInst::Create(New, this); |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 314 | |
| 315 | // Now we must loop through all of the successors of the New block (which |
| 316 | // _were_ the successors of the 'this' block), and update any PHI nodes in |
| 317 | // successors. If there were PHI nodes in the successors, then they need to |
| 318 | // know that incoming branches will be from New, not from Old. |
| 319 | // |
Chris Lattner | 1c9ab51 | 2003-09-24 22:03:22 +0000 | [diff] [blame] | 320 | for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) { |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 321 | // Loop over any phi nodes in the basic block, updating the BB field of |
| 322 | // incoming values... |
| 323 | BasicBlock *Successor = *I; |
Chris Lattner | f878218 | 2004-06-05 00:11:27 +0000 | [diff] [blame] | 324 | PHINode *PN; |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 325 | for (BasicBlock::iterator II = Successor->begin(); |
Chris Lattner | 9bf2a92 | 2004-06-05 17:43:52 +0000 | [diff] [blame] | 326 | (PN = dyn_cast<PHINode>(II)); ++II) { |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 327 | int IDX = PN->getBasicBlockIndex(this); |
| 328 | while (IDX != -1) { |
| 329 | PN->setIncomingBlock((unsigned)IDX, New); |
| 330 | IDX = PN->getBasicBlockIndex(this); |
| 331 | } |
| 332 | } |
| 333 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 334 | return New; |
| 335 | } |
Dan Gohman | eeb8ef1 | 2009-10-29 00:09:08 +0000 | [diff] [blame] | 336 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 337 | void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { |
| 338 | TerminatorInst *TI = getTerminator(); |
| 339 | if (!TI) |
| 340 | // Cope with being called on a BasicBlock that doesn't have a terminator |
| 341 | // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. |
| 342 | return; |
| 343 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 344 | BasicBlock *Succ = TI->getSuccessor(i); |
NAKAMURA Takumi | 77c7140 | 2011-08-09 23:13:05 +0000 | [diff] [blame] | 345 | // N.B. Succ might not be a complete BasicBlock, so don't assume |
| 346 | // that it ends with a non-phi instruction. |
| 347 | for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { |
| 348 | PHINode *PN = dyn_cast<PHINode>(II); |
| 349 | if (!PN) |
| 350 | break; |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 351 | int i; |
| 352 | while ((i = PN->getBasicBlockIndex(this)) >= 0) |
| 353 | PN->setIncomingBlock(i, New); |
| 354 | } |
| 355 | } |
| 356 | } |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 357 | |
| 358 | /// isLandingPad - Return true if this basic block is a landing pad. I.e., it's |
| 359 | /// the destination of the 'unwind' edge of an invoke instruction. |
| 360 | bool BasicBlock::isLandingPad() const { |
| 361 | return isa<LandingPadInst>(getFirstNonPHI()); |
| 362 | } |
| 363 | |
| 364 | /// getLandingPadInst() - Return the landingpad instruction associated with |
| 365 | /// the landing pad. |
| 366 | LandingPadInst *BasicBlock::getLandingPadInst() { |
| 367 | return dyn_cast<LandingPadInst>(getFirstNonPHI()); |
| 368 | } |
Bill Wendling | 7750c3f | 2012-01-31 00:26:24 +0000 | [diff] [blame] | 369 | const LandingPadInst *BasicBlock::getLandingPadInst() const { |
| 370 | return dyn_cast<LandingPadInst>(getFirstNonPHI()); |
| 371 | } |