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 | 03e36d7 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/IntrinsicInst.h" |
| 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/Type.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Hans Wennborg | 4d651e4 | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 24 | |
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()) |
Mehdi Amini | d1e3c5a | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 29 | return F->getValueSymbolTable(); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 30 | return nullptr; |
Chris Lattner | 17fcdd5 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 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... |
Duncan P. N. Exon Smith | 2a8bcfa | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 39 | template class llvm::SymbolTableListTraits<Instruction>; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 41 | BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 42 | BasicBlock *InsertBefore) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 43 | : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) { |
Chris Lattner | 0a1a874 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 44 | |
Duncan P. N. Exon Smith | 865919d | 2014-08-01 21:22:04 +0000 | [diff] [blame] | 45 | if (NewParent) |
| 46 | insertInto(NewParent, InsertBefore); |
| 47 | else |
| 48 | assert(!InsertBefore && |
Chris Lattner | 4f05611 | 2004-02-04 03:57:50 +0000 | [diff] [blame] | 49 | "Cannot insert block before another block with no function!"); |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 50 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 51 | setName(Name); |
Chris Lattner | 0a1a874 | 2002-09-26 05:03:22 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Duncan P. N. Exon Smith | 865919d | 2014-08-01 21:22:04 +0000 | [diff] [blame] | 54 | void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) { |
| 55 | assert(NewParent && "Expected a parent"); |
| 56 | assert(!Parent && "Already has a parent"); |
| 57 | |
| 58 | if (InsertBefore) |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 59 | NewParent->getBasicBlockList().insert(InsertBefore->getIterator(), this); |
Duncan P. N. Exon Smith | 865919d | 2014-08-01 21:22:04 +0000 | [diff] [blame] | 60 | else |
| 61 | NewParent->getBasicBlockList().push_back(this); |
| 62 | } |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 63 | |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 64 | BasicBlock::~BasicBlock() { |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 65 | // If the address of the block is taken and it is being deleted (e.g. because |
| 66 | // it is dead), this means that there is either a dangling constant expr |
| 67 | // hanging off the block, or an undefined use of the block (source code |
| 68 | // expecting the address of a label to keep the block alive even though there |
| 69 | // is no indirect branch). Handle these cases by zapping the BlockAddress |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 70 | // nodes. There are no other possible uses at this point. |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 71 | if (hasAddressTaken()) { |
| 72 | assert(!use_empty() && "There should be at least one blockaddress!"); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 73 | Constant *Replacement = |
| 74 | ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1); |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 75 | while (!use_empty()) { |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 76 | BlockAddress *BA = cast<BlockAddress>(user_back()); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 77 | BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, |
| 78 | BA->getType())); |
Chris Lattner | dac8bde | 2009-10-30 22:39:36 +0000 | [diff] [blame] | 79 | BA->destroyConstant(); |
| 80 | } |
| 81 | } |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 82 | |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 83 | assert(getParent() == nullptr && "BasicBlock still linked into the program!"); |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 84 | dropAllReferences(); |
| 85 | InstList.clear(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 88 | void BasicBlock::setParent(Function *parent) { |
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 | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Florian Hahn | 848303c | 2018-04-19 09:48:07 +0000 | [diff] [blame] | 93 | iterator_range<filter_iterator<BasicBlock::const_iterator, |
| 94 | std::function<bool(const Instruction &)>>> |
| 95 | BasicBlock::instructionsWithoutDebug() const { |
| 96 | std::function<bool(const Instruction &)> Fn = [](const Instruction &I) { |
| 97 | return !isa<DbgInfoIntrinsic>(I); |
| 98 | }; |
| 99 | return make_filter_range(*this, Fn); |
| 100 | } |
| 101 | |
| 102 | iterator_range<filter_iterator<BasicBlock::iterator, |
| 103 | std::function<bool(Instruction &)>>> |
| 104 | BasicBlock::instructionsWithoutDebug() { |
| 105 | std::function<bool(Instruction &)> Fn = [](Instruction &I) { |
| 106 | return !isa<DbgInfoIntrinsic>(I); |
| 107 | }; |
| 108 | return make_filter_range(*this, Fn); |
| 109 | } |
| 110 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 111 | void BasicBlock::removeFromParent() { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 112 | getParent()->getBasicBlockList().remove(getIterator()); |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Daniel Berlin | cc6ce08 | 2015-04-03 01:20:33 +0000 | [diff] [blame] | 115 | iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 116 | return getParent()->getBasicBlockList().erase(getIterator()); |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 119 | /// Unlink this basic block from its current function and |
Chris Lattner | a71965b | 2006-09-23 04:03:45 +0000 | [diff] [blame] | 120 | /// insert it into the function that MovePos lives in, right before MovePos. |
Chris Lattner | 6a13aed | 2005-08-12 22:14:06 +0000 | [diff] [blame] | 121 | void BasicBlock::moveBefore(BasicBlock *MovePos) { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 122 | MovePos->getParent()->getBasicBlockList().splice( |
| 123 | MovePos->getIterator(), getParent()->getBasicBlockList(), getIterator()); |
Chris Lattner | 6a13aed | 2005-08-12 22:14:06 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 126 | /// Unlink this basic block from its current function and |
Chris Lattner | a71965b | 2006-09-23 04:03:45 +0000 | [diff] [blame] | 127 | /// insert it into the function that MovePos lives in, right after MovePos. |
| 128 | void BasicBlock::moveAfter(BasicBlock *MovePos) { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 129 | MovePos->getParent()->getBasicBlockList().splice( |
| 130 | ++MovePos->getIterator(), getParent()->getBasicBlockList(), |
| 131 | getIterator()); |
Chris Lattner | a71965b | 2006-09-23 04:03:45 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 134 | const Module *BasicBlock::getModule() const { |
Philip Reames | d59f970 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 135 | return getParent()->getParent(); |
| 136 | } |
| 137 | |
Chandler Carruth | d8d8371 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 138 | const Instruction *BasicBlock::getTerminator() const { |
| 139 | if (InstList.empty() || !InstList.back().isTerminator()) |
| 140 | return nullptr; |
| 141 | return &InstList.back(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 144 | const CallInst *BasicBlock::getTerminatingMustTailCall() const { |
Reid Kleckner | 2970677 | 2014-08-12 00:05:15 +0000 | [diff] [blame] | 145 | if (InstList.empty()) |
| 146 | return nullptr; |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 147 | const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back()); |
Reid Kleckner | 2970677 | 2014-08-12 00:05:15 +0000 | [diff] [blame] | 148 | if (!RI || RI == &InstList.front()) |
| 149 | return nullptr; |
| 150 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 151 | const Instruction *Prev = RI->getPrevNode(); |
Reid Kleckner | 2970677 | 2014-08-12 00:05:15 +0000 | [diff] [blame] | 152 | if (!Prev) |
| 153 | return nullptr; |
| 154 | |
| 155 | if (Value *RV = RI->getReturnValue()) { |
| 156 | if (RV != Prev) |
| 157 | return nullptr; |
| 158 | |
| 159 | // Look through the optional bitcast. |
| 160 | if (auto *BI = dyn_cast<BitCastInst>(Prev)) { |
| 161 | RV = BI->getOperand(0); |
| 162 | Prev = BI->getPrevNode(); |
| 163 | if (!Prev || RV != Prev) |
| 164 | return nullptr; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (auto *CI = dyn_cast<CallInst>(Prev)) { |
| 169 | if (CI->isMustTailCall()) |
| 170 | return CI; |
| 171 | } |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 175 | const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const { |
Sanjoy Das | f9e7219 | 2016-03-11 19:08:34 +0000 | [diff] [blame] | 176 | if (InstList.empty()) |
| 177 | return nullptr; |
| 178 | auto *RI = dyn_cast<ReturnInst>(&InstList.back()); |
| 179 | if (!RI || RI == &InstList.front()) |
| 180 | return nullptr; |
| 181 | |
| 182 | if (auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode())) |
| 183 | if (Function *F = CI->getCalledFunction()) |
| 184 | if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize) |
| 185 | return CI; |
| 186 | |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 190 | const Instruction* BasicBlock::getFirstNonPHI() const { |
| 191 | for (const Instruction &I : *this) |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 192 | if (!isa<PHINode>(I)) |
| 193 | return &I; |
| 194 | return nullptr; |
Vladimir Prus | dd49dbf | 2006-06-08 15:46:18 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 197 | const Instruction* BasicBlock::getFirstNonPHIOrDbg() const { |
| 198 | for (const Instruction &I : *this) |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 199 | if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I)) |
| 200 | return &I; |
| 201 | return nullptr; |
Dale Johannesen | 7249ef0 | 2010-04-02 21:49:27 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 204 | const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const { |
| 205 | for (const Instruction &I : *this) { |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 206 | if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I)) |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 207 | continue; |
| 208 | |
Vedant Kumar | eeb1971 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 209 | if (I.isLifetimeStartOrEnd()) |
| 210 | continue; |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 211 | |
| 212 | return &I; |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 213 | } |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 214 | return nullptr; |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 217 | BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const { |
| 218 | const Instruction *FirstNonPHI = getFirstNonPHI(); |
David Majnemer | 0148a4c | 2015-07-07 18:49:41 +0000 | [diff] [blame] | 219 | if (!FirstNonPHI) |
| 220 | return end(); |
| 221 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 222 | const_iterator InsertPt = FirstNonPHI->getIterator(); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 223 | if (InsertPt->isEHPad()) ++InsertPt; |
Bill Wendling | d2e103d | 2011-08-16 20:42:52 +0000 | [diff] [blame] | 224 | return InsertPt; |
| 225 | } |
| 226 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 227 | void BasicBlock::dropAllReferences() { |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 228 | for (Instruction &I : *this) |
| 229 | I.dropAllReferences(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 232 | /// If this basic block has a single predecessor block, |
Chris Lattner | ad993cb | 2005-02-24 02:37:26 +0000 | [diff] [blame] | 233 | /// return the block, otherwise return a null pointer. |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 234 | const BasicBlock *BasicBlock::getSinglePredecessor() const { |
| 235 | const_pred_iterator PI = pred_begin(this), E = pred_end(this); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 236 | if (PI == E) return nullptr; // No preds. |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 237 | const BasicBlock *ThePred = *PI; |
Chris Lattner | ad993cb | 2005-02-24 02:37:26 +0000 | [diff] [blame] | 238 | ++PI; |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 239 | return (PI == E) ? ThePred : nullptr /*multiple preds*/; |
Chris Lattner | ad993cb | 2005-02-24 02:37:26 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 242 | /// If this basic block has a unique predecessor block, |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 243 | /// return the block, otherwise return a null pointer. |
NAKAMURA Takumi | 8fc9b3d | 2011-08-09 23:12:56 +0000 | [diff] [blame] | 244 | /// Note that unique predecessor doesn't mean single edge, there can be |
| 245 | /// multiple edges from the unique predecessor to this block (for example |
Torok Edwin | 9053a73 | 2008-12-11 11:44:49 +0000 | [diff] [blame] | 246 | /// a switch statement with multiple cases having the same destination). |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 247 | const BasicBlock *BasicBlock::getUniquePredecessor() const { |
| 248 | const_pred_iterator PI = pred_begin(this), E = pred_end(this); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 249 | if (PI == E) return nullptr; // No preds. |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 250 | const BasicBlock *PredBB = *PI; |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 251 | ++PI; |
| 252 | for (;PI != E; ++PI) { |
| 253 | if (*PI != PredBB) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 254 | return nullptr; |
Torok Edwin | 9053a73 | 2008-12-11 11:44:49 +0000 | [diff] [blame] | 255 | // The same predecessor appears multiple times in the predecessor list. |
| 256 | // This is OK. |
Torok Edwin | 87f1e77 | 2008-12-11 10:36:07 +0000 | [diff] [blame] | 257 | } |
| 258 | return PredBB; |
| 259 | } |
| 260 | |
Vedant Kumar | 94a229e | 2018-11-19 19:54:27 +0000 | [diff] [blame] | 261 | bool BasicBlock::hasNPredecessors(unsigned N) const { |
| 262 | return hasNItems(pred_begin(this), pred_end(this), N); |
| 263 | } |
| 264 | |
| 265 | bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const { |
| 266 | return hasNItemsOrMore(pred_begin(this), pred_end(this), N); |
| 267 | } |
| 268 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 269 | const BasicBlock *BasicBlock::getSingleSuccessor() const { |
| 270 | succ_const_iterator SI = succ_begin(this), E = succ_end(this); |
Jingyue Wu | 85e632d | 2015-05-15 17:54:48 +0000 | [diff] [blame] | 271 | if (SI == E) return nullptr; // no successors |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 272 | const BasicBlock *TheSucc = *SI; |
Jingyue Wu | 85e632d | 2015-05-15 17:54:48 +0000 | [diff] [blame] | 273 | ++SI; |
| 274 | return (SI == E) ? TheSucc : nullptr /* multiple successors */; |
| 275 | } |
| 276 | |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 277 | const BasicBlock *BasicBlock::getUniqueSuccessor() const { |
| 278 | succ_const_iterator SI = succ_begin(this), E = succ_end(this); |
Hans Wennborg | 4d651e4 | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 279 | if (SI == E) return nullptr; // No successors |
Craig Topper | 170b761 | 2017-03-27 02:38:17 +0000 | [diff] [blame] | 280 | const BasicBlock *SuccBB = *SI; |
Philip Reames | 2e38beb | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 281 | ++SI; |
| 282 | for (;SI != E; ++SI) { |
| 283 | if (*SI != SuccBB) |
Hans Wennborg | 4d651e4 | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 284 | return nullptr; |
Philip Reames | 2e38beb | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 285 | // The same successor appears multiple times in the successor list. |
| 286 | // This is OK. |
| 287 | } |
| 288 | return SuccBB; |
| 289 | } |
| 290 | |
Chandler Carruth | a1a0cf0 | 2017-05-26 03:10:00 +0000 | [diff] [blame] | 291 | iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() { |
Matt Arsenault | 0598347 | 2017-12-29 19:25:53 +0000 | [diff] [blame] | 292 | PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin()); |
| 293 | return make_range<phi_iterator>(P, nullptr); |
Chandler Carruth | a1a0cf0 | 2017-05-26 03:10:00 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 296 | /// This method is used to notify a BasicBlock that the |
Chris Lattner | 566f600 | 2005-04-21 16:06:03 +0000 | [diff] [blame] | 297 | /// 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] | 298 | /// 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] | 299 | /// update the PHI nodes that reside in the block. Note that this should be |
| 300 | /// called while the predecessor still refers to this block. |
| 301 | /// |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 302 | void BasicBlock::removePredecessor(BasicBlock *Pred, |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 303 | bool DontDeleteUselessPHIs) { |
Chris Lattner | 1f21ef1 | 2005-02-23 16:53:04 +0000 | [diff] [blame] | 304 | assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. |
Chris Lattner | 35f0aec | 2005-02-23 07:09:08 +0000 | [diff] [blame] | 305 | find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 306 | "removePredecessor: BB is not a predecessor!"); |
Chris Lattner | 35f0aec | 2005-02-23 07:09:08 +0000 | [diff] [blame] | 307 | |
Chris Lattner | f23586c | 2004-12-11 22:10:29 +0000 | [diff] [blame] | 308 | if (InstList.empty()) return; |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 309 | PHINode *APN = dyn_cast<PHINode>(&front()); |
| 310 | if (!APN) return; // Quick exit. |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 311 | |
| 312 | // 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] | 313 | // altogether. However, we cannot do this, if this in this case: |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 314 | // |
| 315 | // Loop: |
| 316 | // %x = phi [X, Loop] |
| 317 | // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 |
| 318 | // br Loop ;; %x2 does not dominate all uses |
| 319 | // |
| 320 | // 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] | 321 | // 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] | 322 | // check for this case explicitly now. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 323 | // |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 324 | unsigned max_idx = APN->getNumIncomingValues(); |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 325 | assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 326 | if (max_idx == 2) { |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 327 | BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); |
Chris Lattner | 87a09b1 | 2002-05-21 19:52:49 +0000 | [diff] [blame] | 328 | |
| 329 | // Disable PHI elimination! |
| 330 | if (this == Other) max_idx = 3; |
| 331 | } |
| 332 | |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 333 | // <= Two predecessors BEFORE I remove one? |
| 334 | if (max_idx <= 2 && !DontDeleteUselessPHIs) { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 335 | // Yup, loop through and nuke the PHI nodes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 336 | while (PHINode *PN = dyn_cast<PHINode>(&front())) { |
Chris Lattner | 3464547 | 2005-04-12 18:52:14 +0000 | [diff] [blame] | 337 | // Remove the predecessor first. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 338 | PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 339 | |
| 340 | // 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] | 341 | if (max_idx == 2) { |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 342 | if (PN->getIncomingValue(0) != PN) |
| 343 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
Chris Lattner | 02a78cf | 2003-04-25 23:14:19 +0000 | [diff] [blame] | 344 | else |
| 345 | // 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] | 346 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | dee430d | 2002-10-08 21:36:34 +0000 | [diff] [blame] | 347 | getInstList().pop_front(); // Remove the PHI node |
| 348 | } |
| 349 | |
| 350 | // If the PHI node already only had one entry, it got deleted by |
| 351 | // removeIncomingValue. |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 352 | } |
| 353 | } else { |
| 354 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 355 | // PHI nodes. Iterate over each PHI node fixing them up |
Chris Lattner | f878218 | 2004-06-05 00:11:27 +0000 | [diff] [blame] | 356 | PHINode *PN; |
Chris Lattner | 80f4d88 | 2005-08-05 15:34:10 +0000 | [diff] [blame] | 357 | for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { |
| 358 | ++II; |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 359 | PN->removeIncomingValue(Pred, false); |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 360 | // If all incoming values to the Phi are the same, we can replace the Phi |
| 361 | // with that value. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 362 | Value* PNV = nullptr; |
Duncan Sands | 23a1957 | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 363 | if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue())) |
| 364 | if (PNV != PN) { |
| 365 | PN->replaceAllUsesWith(PNV); |
| 366 | PN->eraseFromParent(); |
| 367 | } |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 368 | } |
Chris Lattner | b47af25 | 2001-06-29 05:25:23 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 372 | bool BasicBlock::canSplitPredecessors() const { |
| 373 | const Instruction *FirstNonPHI = getFirstNonPHI(); |
| 374 | if (isa<LandingPadInst>(FirstNonPHI)) |
| 375 | return true; |
| 376 | // This is perhaps a little conservative because constructs like |
| 377 | // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors |
| 378 | // cannot handle such things just yet. |
| 379 | if (FirstNonPHI->isEHPad()) |
| 380 | return false; |
| 381 | return true; |
| 382 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 383 | |
Andrew Kaylor | c539eea | 2017-06-22 23:27:16 +0000 | [diff] [blame] | 384 | bool BasicBlock::isLegalToHoistInto() const { |
| 385 | auto *Term = getTerminator(); |
| 386 | // No terminator means the block is under construction. |
| 387 | if (!Term) |
| 388 | return true; |
| 389 | |
| 390 | // If the block has no successors, there can be no instructions to hoist. |
| 391 | assert(Term->getNumSuccessors() > 0); |
| 392 | |
| 393 | // Instructions should not be hoisted across exception handling boundaries. |
Chandler Carruth | acc248f | 2018-08-26 08:56:42 +0000 | [diff] [blame] | 394 | return !Term->isExceptionalTerminator(); |
Andrew Kaylor | c539eea | 2017-06-22 23:27:16 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 397 | /// This splits a basic block into two at the specified |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 398 | /// instruction. Note that all instructions BEFORE the specified iterator stay |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 399 | /// 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] | 400 | /// the new BB, and the rest of the instructions in the BB are moved to the new |
| 401 | /// BB, including the old terminator. This invalidates the iterator. |
| 402 | /// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 403 | /// 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] | 404 | /// terminator), and 'I' must not be the end of instruction list (which would |
| 405 | /// 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] | 406 | /// the basic block). |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 407 | /// |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 408 | BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 409 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 410 | assert(I != InstList.end() && |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 411 | "Trying to get me to create degenerate basic block!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 412 | |
Duncan P. N. Exon Smith | 14147ed | 2016-02-21 19:52:15 +0000 | [diff] [blame] | 413 | BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), |
| 414 | this->getNextNode()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 415 | |
Alexey Samsonov | bc48228 | 2015-06-11 18:25:54 +0000 | [diff] [blame] | 416 | // Save DebugLoc of split point before invalidating iterator. |
| 417 | DebugLoc Loc = I->getDebugLoc(); |
Chris Lattner | f2c3106 | 2004-02-03 23:11:21 +0000 | [diff] [blame] | 418 | // Move all of the specified instructions from the original basic block into |
| 419 | // the new basic block. |
| 420 | New->getInstList().splice(New->end(), this->getInstList(), I, end()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 421 | |
| 422 | // Add a branch instruction to the newly formed basic block. |
Alexey Samsonov | bc48228 | 2015-06-11 18:25:54 +0000 | [diff] [blame] | 423 | BranchInst *BI = BranchInst::Create(New, this); |
| 424 | BI->setDebugLoc(Loc); |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 425 | |
| 426 | // Now we must loop through all of the successors of the New block (which |
| 427 | // _were_ the successors of the 'this' block), and update any PHI nodes in |
| 428 | // successors. If there were PHI nodes in the successors, then they need to |
| 429 | // know that incoming branches will be from New, not from Old. |
| 430 | // |
Duncan P. N. Exon Smith | facdfc6 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 431 | 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] | 432 | // Loop over any phi nodes in the basic block, updating the BB field of |
| 433 | // incoming values... |
Duncan P. N. Exon Smith | facdfc6 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 434 | BasicBlock *Successor = *I; |
Chandler Carruth | a1a0cf0 | 2017-05-26 03:10:00 +0000 | [diff] [blame] | 435 | for (auto &PN : Successor->phis()) { |
| 436 | int Idx = PN.getBasicBlockIndex(this); |
| 437 | while (Idx != -1) { |
| 438 | PN.setIncomingBlock((unsigned)Idx, New); |
| 439 | Idx = PN.getBasicBlockIndex(this); |
Chris Lattner | e8e320d | 2002-02-25 00:35:07 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 443 | return New; |
| 444 | } |
Dan Gohman | eeb8ef1 | 2009-10-29 00:09:08 +0000 | [diff] [blame] | 445 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 446 | void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { |
Chandler Carruth | 2aaf722 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 447 | Instruction *TI = getTerminator(); |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 448 | if (!TI) |
| 449 | // Cope with being called on a BasicBlock that doesn't have a terminator |
| 450 | // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. |
| 451 | return; |
Chandler Carruth | 5eb83c5 | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 452 | for (BasicBlock *Succ : successors(TI)) { |
NAKAMURA Takumi | 77c7140 | 2011-08-09 23:13:05 +0000 | [diff] [blame] | 453 | // N.B. Succ might not be a complete BasicBlock, so don't assume |
| 454 | // that it ends with a non-phi instruction. |
| 455 | for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { |
| 456 | PHINode *PN = dyn_cast<PHINode>(II); |
| 457 | if (!PN) |
| 458 | break; |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 459 | int i; |
| 460 | while ((i = PN->getBasicBlockIndex(this)) >= 0) |
| 461 | PN->setIncomingBlock(i, New); |
| 462 | } |
| 463 | } |
| 464 | } |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 465 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 466 | /// Return true if this basic block is a landing pad. I.e., it's |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 467 | /// the destination of the 'unwind' edge of an invoke instruction. |
| 468 | bool BasicBlock::isLandingPad() const { |
| 469 | return isa<LandingPadInst>(getFirstNonPHI()); |
| 470 | } |
| 471 | |
Sanjay Patel | 4ed780a | 2015-02-27 18:07:41 +0000 | [diff] [blame] | 472 | /// Return the landingpad instruction associated with the landing pad. |
Bill Wendling | 7750c3f | 2012-01-31 00:26:24 +0000 | [diff] [blame] | 473 | const LandingPadInst *BasicBlock::getLandingPadInst() const { |
| 474 | return dyn_cast<LandingPadInst>(getFirstNonPHI()); |
| 475 | } |
Hiroshi Yamauchi | dd33e17 | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 476 | |
| 477 | Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const { |
Chandler Carruth | 2aaf722 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 478 | const Instruction *TI = getTerminator(); |
Hiroshi Yamauchi | dd33e17 | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 479 | if (MDNode *MDIrrLoopHeader = |
| 480 | TI->getMetadata(LLVMContext::MD_irr_loop)) { |
| 481 | MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0)); |
| 482 | if (MDName->getString().equals("loop_header_weight")) { |
| 483 | auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1)); |
| 484 | return Optional<uint64_t>(CI->getValue().getZExtValue()); |
| 485 | } |
| 486 | } |
| 487 | return Optional<uint64_t>(); |
| 488 | } |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 489 | |
Vedant Kumar | ab0a33b | 2018-06-26 21:16:59 +0000 | [diff] [blame] | 490 | BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) { |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 491 | while (isa<DbgInfoIntrinsic>(It)) |
| 492 | ++It; |
| 493 | return It; |
| 494 | } |