Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class -----------------===// |
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 Instruction 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/Instruction.h" |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 15 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.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" |
Dehao Chen | c7322da | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 19 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Operator.h" |
| 21 | #include "llvm/IR/Type.h" |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 24 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 25 | Instruction *InsertBefore) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 26 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 27 | |
| 28 | // If requested, insert this instruction into a basic block... |
| 29 | if (InsertBefore) { |
Yaron Keren | 698a7e5 | 2015-06-15 17:03:35 +0000 | [diff] [blame] | 30 | BasicBlock *BB = InsertBefore->getParent(); |
| 31 | assert(BB && "Instruction to insert before is not in a basic block!"); |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 32 | BB->getInstList().insert(InsertBefore->getIterator(), this); |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 33 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 36 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 37 | BasicBlock *InsertAtEnd) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 38 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 39 | |
| 40 | // append this instruction into the basic block |
| 41 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 42 | InsertAtEnd->getInstList().push_back(this); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 45 | Instruction::~Instruction() { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 46 | assert(!Parent && "Instruction still linked in the program!"); |
Dan Gohman | 4f1be4a | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 47 | if (hasMetadataHashEntry()) |
| 48 | clearMetadataHashEntries(); |
Chris Lattner | 70aa33e | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 52 | void Instruction::setParent(BasicBlock *P) { |
| 53 | Parent = P; |
| 54 | } |
| 55 | |
Mehdi Amini | a716c07 | 2015-03-03 22:01:13 +0000 | [diff] [blame] | 56 | const Module *Instruction::getModule() const { |
| 57 | return getParent()->getModule(); |
| 58 | } |
| 59 | |
Sanjoy Das | c11338c | 2015-12-08 00:13:12 +0000 | [diff] [blame] | 60 | const Function *Instruction::getFunction() const { |
| 61 | return getParent()->getParent(); |
| 62 | } |
Philip Reames | d59f970 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 64 | void Instruction::removeFromParent() { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 65 | getParent()->getInstList().remove(getIterator()); |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Berlin | 525a959 | 2015-04-02 00:03:07 +0000 | [diff] [blame] | 68 | iplist<Instruction>::iterator Instruction::eraseFromParent() { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 69 | return getParent()->getInstList().erase(getIterator()); |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 70 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 71 | |
Sanjay Patel | a66b6e6 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 72 | /// Insert an unlinked instruction into a basic block immediately before the |
| 73 | /// specified instruction. |
Owen Anderson | 26bb50a | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 74 | void Instruction::insertBefore(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 75 | InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this); |
Owen Anderson | 26bb50a | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Sanjay Patel | a66b6e6 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 78 | /// Insert an unlinked instruction into a basic block immediately after the |
| 79 | /// specified instruction. |
Chris Lattner | 3ff704f | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 80 | void Instruction::insertAfter(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | eac3095 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 81 | InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(), |
| 82 | this); |
Chris Lattner | 3ff704f | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Sanjay Patel | a66b6e6 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 85 | /// Unlink this instruction from its current basic block and insert it into the |
| 86 | /// basic block that MovePos lives in, right before MovePos. |
Chris Lattner | 0fe34d8 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 87 | void Instruction::moveBefore(Instruction *MovePos) { |
Duncan P. N. Exon Smith | 3b3dd88 | 2016-08-17 01:54:41 +0000 | [diff] [blame] | 88 | moveBefore(*MovePos->getParent(), MovePos->getIterator()); |
| 89 | } |
| 90 | |
Sanjay Patel | 1bf0915 | 2017-08-29 14:07:48 +0000 | [diff] [blame] | 91 | void Instruction::moveAfter(Instruction *MovePos) { |
| 92 | moveBefore(*MovePos->getParent(), ++MovePos->getIterator()); |
| 93 | } |
| 94 | |
Duncan P. N. Exon Smith | 3b3dd88 | 2016-08-17 01:54:41 +0000 | [diff] [blame] | 95 | void Instruction::moveBefore(BasicBlock &BB, |
| 96 | SymbolTableList<Instruction>::iterator I) { |
| 97 | assert(I == BB.end() || I->getParent() == &BB); |
| 98 | BB.getInstList().splice(I, getParent()->getInstList(), getIterator()); |
Chris Lattner | 0fe34d8 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 101 | void Instruction::setHasNoUnsignedWrap(bool b) { |
| 102 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 103 | } |
| 104 | |
| 105 | void Instruction::setHasNoSignedWrap(bool b) { |
| 106 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 107 | } |
| 108 | |
| 109 | void Instruction::setIsExact(bool b) { |
| 110 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
| 111 | } |
| 112 | |
| 113 | bool Instruction::hasNoUnsignedWrap() const { |
| 114 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 115 | } |
| 116 | |
| 117 | bool Instruction::hasNoSignedWrap() const { |
| 118 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 119 | } |
| 120 | |
Sanjoy Das | 9e36975 | 2017-02-23 22:50:52 +0000 | [diff] [blame] | 121 | void Instruction::dropPoisonGeneratingFlags() { |
| 122 | switch (getOpcode()) { |
| 123 | case Instruction::Add: |
| 124 | case Instruction::Sub: |
| 125 | case Instruction::Mul: |
| 126 | case Instruction::Shl: |
| 127 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false); |
| 128 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false); |
| 129 | break; |
| 130 | |
| 131 | case Instruction::UDiv: |
| 132 | case Instruction::SDiv: |
| 133 | case Instruction::AShr: |
| 134 | case Instruction::LShr: |
| 135 | cast<PossiblyExactOperator>(this)->setIsExact(false); |
| 136 | break; |
| 137 | |
| 138 | case Instruction::GetElementPtr: |
| 139 | cast<GetElementPtrInst>(this)->setIsInBounds(false); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 144 | bool Instruction::isExact() const { |
| 145 | return cast<PossiblyExactOperator>(this)->isExact(); |
| 146 | } |
| 147 | |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 148 | void Instruction::setFast(bool B) { |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 149 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 150 | cast<FPMathOperator>(this)->setFast(B); |
| 151 | } |
| 152 | |
| 153 | void Instruction::setHasAllowReassoc(bool B) { |
| 154 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 155 | cast<FPMathOperator>(this)->setHasAllowReassoc(B); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 158 | void Instruction::setHasNoNaNs(bool B) { |
| 159 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 160 | cast<FPMathOperator>(this)->setHasNoNaNs(B); |
| 161 | } |
| 162 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 163 | void Instruction::setHasNoInfs(bool B) { |
| 164 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 165 | cast<FPMathOperator>(this)->setHasNoInfs(B); |
| 166 | } |
| 167 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 168 | void Instruction::setHasNoSignedZeros(bool B) { |
| 169 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 170 | cast<FPMathOperator>(this)->setHasNoSignedZeros(B); |
| 171 | } |
| 172 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 173 | void Instruction::setHasAllowReciprocal(bool B) { |
| 174 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 175 | cast<FPMathOperator>(this)->setHasAllowReciprocal(B); |
| 176 | } |
| 177 | |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 178 | void Instruction::setHasApproxFunc(bool B) { |
| 179 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 180 | cast<FPMathOperator>(this)->setHasApproxFunc(B); |
| 181 | } |
| 182 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 183 | void Instruction::setFastMathFlags(FastMathFlags FMF) { |
| 184 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 185 | cast<FPMathOperator>(this)->setFastMathFlags(FMF); |
| 186 | } |
| 187 | |
Sanjay Patel | 4211bbc | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 188 | void Instruction::copyFastMathFlags(FastMathFlags FMF) { |
| 189 | assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); |
| 190 | cast<FPMathOperator>(this)->copyFastMathFlags(FMF); |
| 191 | } |
| 192 | |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 193 | bool Instruction::isFast() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 194 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 195 | return cast<FPMathOperator>(this)->isFast(); |
| 196 | } |
| 197 | |
| 198 | bool Instruction::hasAllowReassoc() const { |
| 199 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
| 200 | return cast<FPMathOperator>(this)->hasAllowReassoc(); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 203 | bool Instruction::hasNoNaNs() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 204 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 205 | return cast<FPMathOperator>(this)->hasNoNaNs(); |
| 206 | } |
| 207 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 208 | bool Instruction::hasNoInfs() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 209 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 210 | return cast<FPMathOperator>(this)->hasNoInfs(); |
| 211 | } |
| 212 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 213 | bool Instruction::hasNoSignedZeros() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 214 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 215 | return cast<FPMathOperator>(this)->hasNoSignedZeros(); |
| 216 | } |
| 217 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 218 | bool Instruction::hasAllowReciprocal() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 219 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 220 | return cast<FPMathOperator>(this)->hasAllowReciprocal(); |
| 221 | } |
| 222 | |
Adam Nemet | 5c57c11 | 2017-03-28 20:11:52 +0000 | [diff] [blame] | 223 | bool Instruction::hasAllowContract() const { |
| 224 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
| 225 | return cast<FPMathOperator>(this)->hasAllowContract(); |
| 226 | } |
| 227 | |
Sanjay Patel | 00e900a | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 228 | bool Instruction::hasApproxFunc() const { |
| 229 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
| 230 | return cast<FPMathOperator>(this)->hasApproxFunc(); |
| 231 | } |
| 232 | |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 233 | FastMathFlags Instruction::getFastMathFlags() const { |
Chad Rosier | add80b4 | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 234 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 125fc7f | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 235 | return cast<FPMathOperator>(this)->getFastMathFlags(); |
| 236 | } |
Chris Lattner | 0fe34d8 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 237 | |
Michael Ilseman | 4b896dd | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 238 | void Instruction::copyFastMathFlags(const Instruction *I) { |
Sanjay Patel | 4211bbc | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 239 | copyFastMathFlags(I->getFastMathFlags()); |
Michael Ilseman | 4b896dd | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 240 | } |
| 241 | |
George Burgess IV | 9276050 | 2017-06-09 03:56:15 +0000 | [diff] [blame] | 242 | void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) { |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 243 | // Copy the wrapping flags. |
George Burgess IV | 9276050 | 2017-06-09 03:56:15 +0000 | [diff] [blame] | 244 | if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) { |
| 245 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 246 | setHasNoSignedWrap(OB->hasNoSignedWrap()); |
| 247 | setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); |
| 248 | } |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Copy the exact flag. |
| 252 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 253 | if (isa<PossiblyExactOperator>(this)) |
| 254 | setIsExact(PE->isExact()); |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 255 | |
| 256 | // Copy the fast-math flags. |
| 257 | if (auto *FP = dyn_cast<FPMathOperator>(V)) |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 258 | if (isa<FPMathOperator>(this)) |
| 259 | copyFastMathFlags(FP->getFastMathFlags()); |
David Majnemer | 1bd4284 | 2016-07-15 05:02:31 +0000 | [diff] [blame] | 260 | |
| 261 | if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) |
| 262 | if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) |
| 263 | DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds()); |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void Instruction::andIRFlags(const Value *V) { |
| 267 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 268 | if (isa<OverflowingBinaryOperator>(this)) { |
| 269 | setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); |
| 270 | setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); |
| 271 | } |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 275 | if (isa<PossiblyExactOperator>(this)) |
| 276 | setIsExact(isExact() & PE->isExact()); |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 277 | |
| 278 | if (auto *FP = dyn_cast<FPMathOperator>(V)) { |
David Majnemer | 582e856 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 279 | if (isa<FPMathOperator>(this)) { |
| 280 | FastMathFlags FM = getFastMathFlags(); |
| 281 | FM &= FP->getFastMathFlags(); |
| 282 | copyFastMathFlags(FM); |
| 283 | } |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 284 | } |
David Majnemer | 1bd4284 | 2016-07-15 05:02:31 +0000 | [diff] [blame] | 285 | |
| 286 | if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) |
| 287 | if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) |
| 288 | DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds()); |
David Majnemer | 91e5e85 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 289 | } |
Michael Ilseman | 4b896dd | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 290 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 291 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 292 | switch (OpCode) { |
| 293 | // Terminators |
Chris Lattner | 0513e9f | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 294 | case Ret: return "ret"; |
| 295 | case Br: return "br"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 296 | case Switch: return "switch"; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 297 | case IndirectBr: return "indirectbr"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 298 | case Invoke: return "invoke"; |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 299 | case Resume: return "resume"; |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 300 | case Unreachable: return "unreachable"; |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 301 | case CleanupRet: return "cleanupret"; |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 302 | case CatchRet: return "catchret"; |
| 303 | case CatchPad: return "catchpad"; |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 304 | case CatchSwitch: return "catchswitch"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 305 | |
Cameron McInally | ca8cb68 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 306 | // Standard unary operators... |
| 307 | case FNeg: return "fneg"; |
| 308 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 309 | // Standard binary operators... |
| 310 | case Add: return "add"; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 311 | case FAdd: return "fadd"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 312 | case Sub: return "sub"; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 313 | case FSub: return "fsub"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 314 | case Mul: return "mul"; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 315 | case FMul: return "fmul"; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 316 | case UDiv: return "udiv"; |
| 317 | case SDiv: return "sdiv"; |
| 318 | case FDiv: return "fdiv"; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 319 | case URem: return "urem"; |
| 320 | case SRem: return "srem"; |
| 321 | case FRem: return "frem"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 322 | |
| 323 | // Logical operators... |
| 324 | case And: return "and"; |
| 325 | case Or : return "or"; |
| 326 | case Xor: return "xor"; |
| 327 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 328 | // Memory instructions... |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 329 | case Alloca: return "alloca"; |
| 330 | case Load: return "load"; |
| 331 | case Store: return "store"; |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 332 | case AtomicCmpXchg: return "cmpxchg"; |
| 333 | case AtomicRMW: return "atomicrmw"; |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 334 | case Fence: return "fence"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 335 | case GetElementPtr: return "getelementptr"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 336 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 337 | // Convert instructions... |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 338 | case Trunc: return "trunc"; |
| 339 | case ZExt: return "zext"; |
| 340 | case SExt: return "sext"; |
| 341 | case FPTrunc: return "fptrunc"; |
| 342 | case FPExt: return "fpext"; |
| 343 | case FPToUI: return "fptoui"; |
| 344 | case FPToSI: return "fptosi"; |
| 345 | case UIToFP: return "uitofp"; |
| 346 | case SIToFP: return "sitofp"; |
| 347 | case IntToPtr: return "inttoptr"; |
| 348 | case PtrToInt: return "ptrtoint"; |
| 349 | case BitCast: return "bitcast"; |
| 350 | case AddrSpaceCast: return "addrspacecast"; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 351 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 352 | // Other instructions... |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 353 | case ICmp: return "icmp"; |
| 354 | case FCmp: return "fcmp"; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 355 | case PHI: return "phi"; |
| 356 | case Select: return "select"; |
| 357 | case Call: return "call"; |
| 358 | case Shl: return "shl"; |
| 359 | case LShr: return "lshr"; |
| 360 | case AShr: return "ashr"; |
| 361 | case VAArg: return "va_arg"; |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 362 | case ExtractElement: return "extractelement"; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 363 | case InsertElement: return "insertelement"; |
| 364 | case ShuffleVector: return "shufflevector"; |
Matthijs Kooijman | 74b5e07 | 2008-05-30 10:31:54 +0000 | [diff] [blame] | 365 | case ExtractValue: return "extractvalue"; |
| 366 | case InsertValue: return "insertvalue"; |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 367 | case LandingPad: return "landingpad"; |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 368 | case CleanupPad: return "cleanuppad"; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 369 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 370 | default: return "<Invalid operator> "; |
| 371 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 373 | |
Sanjay Patel | 5e827ba | 2016-10-05 18:51:12 +0000 | [diff] [blame] | 374 | /// Return true if both instructions have the same special state. This must be |
JF Bastien | eb5d561 | 2016-04-11 22:30:37 +0000 | [diff] [blame] | 375 | /// kept in sync with FunctionComparator::cmpOperations in |
| 376 | /// lib/Transforms/IPO/MergeFunctions.cpp. |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 377 | static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, |
| 378 | bool IgnoreAlignment = false) { |
| 379 | assert(I1->getOpcode() == I2->getOpcode() && |
| 380 | "Can not compare special state of different instructions"); |
| 381 | |
JF Bastien | 5e99fa6 | 2016-04-12 18:06:55 +0000 | [diff] [blame] | 382 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1)) |
| 383 | return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() && |
| 384 | (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() || |
| 385 | IgnoreAlignment); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 386 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 387 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
| 388 | (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() || |
| 389 | IgnoreAlignment) && |
| 390 | LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 391 | LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 392 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 393 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
| 394 | (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() || |
| 395 | IgnoreAlignment) && |
| 396 | SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 397 | SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 398 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 399 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 400 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
| 401 | return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && |
| 402 | CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
Sanjoy Das | 97a58d8 | 2015-12-14 19:11:35 +0000 | [diff] [blame] | 403 | CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && |
| 404 | CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 405 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 406 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
Sanjoy Das | 97a58d8 | 2015-12-14 19:11:35 +0000 | [diff] [blame] | 407 | CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && |
| 408 | CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 409 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) |
| 410 | return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); |
| 411 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) |
| 412 | return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); |
| 413 | if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) |
| 414 | return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 415 | FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 416 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) |
| 417 | return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 418 | CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 419 | CXI->getSuccessOrdering() == |
| 420 | cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && |
| 421 | CXI->getFailureOrdering() == |
| 422 | cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 423 | CXI->getSyncScopeID() == |
| 424 | cast<AtomicCmpXchgInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 425 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) |
| 426 | return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && |
| 427 | RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && |
| 428 | RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 429 | RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 430 | |
| 431 | return true; |
| 432 | } |
| 433 | |
Chris Lattner | e3a0884 | 2008-11-27 08:39:18 +0000 | [diff] [blame] | 434 | bool Instruction::isIdenticalTo(const Instruction *I) const { |
Dan Gohman | 75b0eda | 2009-08-25 22:24:20 +0000 | [diff] [blame] | 435 | return isIdenticalToWhenDefined(I) && |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 436 | SubclassOptionalData == I->SubclassOptionalData; |
| 437 | } |
| 438 | |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 439 | bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 440 | if (getOpcode() != I->getOpcode() || |
| 441 | getNumOperands() != I->getNumOperands() || |
| 442 | getType() != I->getType()) |
| 443 | return false; |
| 444 | |
NAKAMURA Takumi | df19139 | 2014-06-02 01:35:34 +0000 | [diff] [blame] | 445 | // If both instructions have no operands, they are identical. |
| 446 | if (getNumOperands() == 0 && I->getNumOperands() == 0) |
| 447 | return haveSameSpecialState(this, I); |
| 448 | |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 449 | // We have two instructions of identical opcode and #operands. Check to see |
| 450 | // if all operands are the same. |
Benjamin Kramer | 6e9eeab | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 451 | if (!std::equal(op_begin(), op_end(), I->op_begin())) |
| 452 | return false; |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 453 | |
Joel Jones | 9df72a9 | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 454 | if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { |
| 455 | const PHINode *otherPHI = cast<PHINode>(I); |
Benjamin Kramer | 6e9eeab | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 456 | return std::equal(thisPHI->block_begin(), thisPHI->block_end(), |
| 457 | otherPHI->block_begin()); |
Joel Jones | 9df72a9 | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 458 | } |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 459 | |
| 460 | return haveSameSpecialState(this, I); |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 461 | } |
| 462 | |
JF Bastien | eb5d561 | 2016-04-11 22:30:37 +0000 | [diff] [blame] | 463 | // Keep this in sync with FunctionComparator::cmpOperations in |
Dan Gohman | 194ae78 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 464 | // lib/Transforms/IPO/MergeFunctions.cpp. |
Hal Finkel | ec4e85e | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 465 | bool Instruction::isSameOperationAs(const Instruction *I, |
| 466 | unsigned flags) const { |
| 467 | bool IgnoreAlignment = flags & CompareIgnoringAlignment; |
| 468 | bool UseScalarTypes = flags & CompareUsingScalarTypes; |
| 469 | |
Dan Gohman | 194ae78 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 470 | if (getOpcode() != I->getOpcode() || |
| 471 | getNumOperands() != I->getNumOperands() || |
Hal Finkel | ec4e85e | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 472 | (UseScalarTypes ? |
| 473 | getType()->getScalarType() != I->getType()->getScalarType() : |
| 474 | getType() != I->getType())) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 475 | return false; |
| 476 | |
| 477 | // We have two instructions of identical opcode and #operands. Check to see |
| 478 | // if all operands are the same type |
| 479 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Hal Finkel | ec4e85e | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 480 | if (UseScalarTypes ? |
| 481 | getOperand(i)->getType()->getScalarType() != |
| 482 | I->getOperand(i)->getType()->getScalarType() : |
| 483 | getOperand(i)->getType() != I->getOperand(i)->getType()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 484 | return false; |
| 485 | |
Arnaud A. de Grandmaison | d4ffb93 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 486 | return haveSameSpecialState(this, I, IgnoreAlignment); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 489 | bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 490 | for (const Use &U : uses()) { |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 491 | // PHI nodes uses values in the corresponding predecessor block. For other |
| 492 | // instructions, just check to see whether the parent of the use matches up. |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 493 | const Instruction *I = cast<Instruction>(U.getUser()); |
| 494 | const PHINode *PN = dyn_cast<PHINode>(I); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 495 | if (!PN) { |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 496 | if (I->getParent() != BB) |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 497 | return true; |
| 498 | continue; |
| 499 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 500 | |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 501 | if (PN->getIncomingBlock(U) != BB) |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 502 | return true; |
| 503 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 504 | return false; |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | d96288a | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 507 | bool Instruction::mayReadFromMemory() const { |
| 508 | switch (getOpcode()) { |
| 509 | default: return false; |
Chris Lattner | d96288a | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 510 | case Instruction::VAArg: |
Chris Lattner | 748118d | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 511 | case Instruction::Load: |
Eli Friedman | 8a552bb | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 512 | case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 513 | case Instruction::AtomicCmpXchg: |
| 514 | case Instruction::AtomicRMW: |
David Majnemer | 1c018c2 | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 515 | case Instruction::CatchPad: |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 516 | case Instruction::CatchRet: |
Chris Lattner | d96288a | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 517 | return true; |
| 518 | case Instruction::Call: |
| 519 | return !cast<CallInst>(this)->doesNotAccessMemory(); |
| 520 | case Instruction::Invoke: |
| 521 | return !cast<InvokeInst>(this)->doesNotAccessMemory(); |
Chris Lattner | 748118d | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 522 | case Instruction::Store: |
Eli Friedman | e5e7712 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 523 | return !cast<StoreInst>(this)->isUnordered(); |
Chris Lattner | d96288a | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
Chris Lattner | 7ae40e7 | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 526 | |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 527 | bool Instruction::mayWriteToMemory() const { |
| 528 | switch (getOpcode()) { |
| 529 | default: return false; |
Eli Friedman | 8a552bb | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 530 | case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 531 | case Instruction::Store: |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 532 | case Instruction::VAArg: |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 533 | case Instruction::AtomicCmpXchg: |
| 534 | case Instruction::AtomicRMW: |
David Majnemer | 1c018c2 | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 535 | case Instruction::CatchPad: |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 536 | case Instruction::CatchRet: |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 537 | return true; |
| 538 | case Instruction::Call: |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 539 | return !cast<CallInst>(this)->onlyReadsMemory(); |
Chris Lattner | d96288a | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 540 | case Instruction::Invoke: |
| 541 | return !cast<InvokeInst>(this)->onlyReadsMemory(); |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 542 | case Instruction::Load: |
Eli Friedman | e5e7712 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 543 | return !cast<LoadInst>(this)->isUnordered(); |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 546 | |
Robin Morisset | 1ad925c | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 547 | bool Instruction::isAtomic() const { |
| 548 | switch (getOpcode()) { |
| 549 | default: |
| 550 | return false; |
| 551 | case Instruction::AtomicCmpXchg: |
| 552 | case Instruction::AtomicRMW: |
| 553 | case Instruction::Fence: |
| 554 | return true; |
| 555 | case Instruction::Load: |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 556 | return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; |
Robin Morisset | 1ad925c | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 557 | case Instruction::Store: |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 558 | return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; |
Robin Morisset | 1ad925c | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Tim Shen | f52671d | 2017-05-09 15:27:17 +0000 | [diff] [blame] | 562 | bool Instruction::hasAtomicLoad() const { |
| 563 | assert(isAtomic()); |
| 564 | switch (getOpcode()) { |
| 565 | default: |
| 566 | return false; |
| 567 | case Instruction::AtomicCmpXchg: |
| 568 | case Instruction::AtomicRMW: |
| 569 | case Instruction::Load: |
| 570 | return true; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | bool Instruction::hasAtomicStore() const { |
| 575 | assert(isAtomic()); |
| 576 | switch (getOpcode()) { |
| 577 | default: |
| 578 | return false; |
| 579 | case Instruction::AtomicCmpXchg: |
| 580 | case Instruction::AtomicRMW: |
| 581 | case Instruction::Store: |
| 582 | return true; |
| 583 | } |
| 584 | } |
| 585 | |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 586 | bool Instruction::mayThrow() const { |
| 587 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 588 | return !CI->doesNotThrow(); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 589 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(this)) |
| 590 | return CRI->unwindsToCaller(); |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 591 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this)) |
| 592 | return CatchSwitch->unwindsToCaller(); |
Bill Wendling | 55fdb4e | 2011-08-16 21:15:50 +0000 | [diff] [blame] | 593 | return isa<ResumeInst>(this); |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Chris Bieneman | 5489d32 | 2018-01-09 21:58:46 +0000 | [diff] [blame] | 596 | bool Instruction::isSafeToRemove() const { |
| 597 | return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) && |
Chandler Carruth | 9179aee | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 598 | !this->isTerminator(); |
Chris Bieneman | 5489d32 | 2018-01-09 21:58:46 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Vedant Kumar | eeb1971 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 601 | bool Instruction::isLifetimeStartOrEnd() const { |
| 602 | auto II = dyn_cast<IntrinsicInst>(this); |
| 603 | if (!II) |
| 604 | return false; |
| 605 | Intrinsic::ID ID = II->getIntrinsicID(); |
| 606 | return ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end; |
| 607 | } |
| 608 | |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 609 | const Instruction *Instruction::getNextNonDebugInstruction() const { |
| 610 | for (const Instruction *I = getNextNode(); I; I = I->getNextNode()) |
| 611 | if (!isa<DbgInfoIntrinsic>(I)) |
| 612 | return I; |
| 613 | return nullptr; |
| 614 | } |
| 615 | |
Carlos Alberto Enciso | 8381d3e | 2018-11-09 09:42:10 +0000 | [diff] [blame] | 616 | const Instruction *Instruction::getPrevNonDebugInstruction() const { |
| 617 | for (const Instruction *I = getPrevNode(); I; I = I->getPrevNode()) |
| 618 | if (!isa<DbgInfoIntrinsic>(I)) |
| 619 | return I; |
| 620 | return nullptr; |
| 621 | } |
| 622 | |
Shuxin Yang | 9b7f6f2 | 2012-11-29 01:47:31 +0000 | [diff] [blame] | 623 | bool Instruction::isAssociative() const { |
| 624 | unsigned Opcode = getOpcode(); |
| 625 | if (isAssociative(Opcode)) |
| 626 | return true; |
| 627 | |
| 628 | switch (Opcode) { |
| 629 | case FMul: |
| 630 | case FAdd: |
Warren Ristow | 0bd45c4 | 2018-05-24 20:16:43 +0000 | [diff] [blame] | 631 | return cast<FPMathOperator>(this)->hasAllowReassoc() && |
| 632 | cast<FPMathOperator>(this)->hasNoSignedZeros(); |
Shuxin Yang | 9b7f6f2 | 2012-11-29 01:47:31 +0000 | [diff] [blame] | 633 | default: |
| 634 | return false; |
| 635 | } |
| 636 | } |
| 637 | |
Chandler Carruth | 5eb83c5 | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 638 | unsigned Instruction::getNumSuccessors() const { |
| 639 | switch (getOpcode()) { |
| 640 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 641 | case Instruction::OPC: \ |
| 642 | return static_cast<const CLASS *>(this)->getNumSuccessors(); |
| 643 | #include "llvm/IR/Instruction.def" |
| 644 | default: |
| 645 | break; |
| 646 | } |
| 647 | llvm_unreachable("not a terminator"); |
| 648 | } |
| 649 | |
| 650 | BasicBlock *Instruction::getSuccessor(unsigned idx) const { |
| 651 | switch (getOpcode()) { |
| 652 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 653 | case Instruction::OPC: \ |
| 654 | return static_cast<const CLASS *>(this)->getSuccessor(idx); |
| 655 | #include "llvm/IR/Instruction.def" |
| 656 | default: |
| 657 | break; |
| 658 | } |
| 659 | llvm_unreachable("not a terminator"); |
| 660 | } |
| 661 | |
| 662 | void Instruction::setSuccessor(unsigned idx, BasicBlock *B) { |
| 663 | switch (getOpcode()) { |
| 664 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 665 | case Instruction::OPC: \ |
| 666 | return static_cast<CLASS *>(this)->setSuccessor(idx, B); |
| 667 | #include "llvm/IR/Instruction.def" |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | llvm_unreachable("not a terminator"); |
| 672 | } |
| 673 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 674 | Instruction *Instruction::cloneImpl() const { |
| 675 | llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); |
| 676 | } |
| 677 | |
Xinliang David Li | ee836b9 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 678 | void Instruction::swapProfMetadata() { |
| 679 | MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 680 | if (!ProfileData || ProfileData->getNumOperands() != 3 || |
| 681 | !isa<MDString>(ProfileData->getOperand(0))) |
| 682 | return; |
| 683 | |
| 684 | MDString *MDName = cast<MDString>(ProfileData->getOperand(0)); |
| 685 | if (MDName->getString() != "branch_weights") |
| 686 | return; |
| 687 | |
| 688 | // The first operand is the name. Fetch them backwards and build a new one. |
| 689 | Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), |
| 690 | ProfileData->getOperand(1)}; |
| 691 | setMetadata(LLVMContext::MD_prof, |
| 692 | MDNode::get(ProfileData->getContext(), Ops)); |
| 693 | } |
| 694 | |
Xinliang David Li | ee836b9 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 695 | void Instruction::copyMetadata(const Instruction &SrcInst, |
| 696 | ArrayRef<unsigned> WL) { |
| 697 | if (!SrcInst.hasMetadata()) |
| 698 | return; |
| 699 | |
| 700 | DenseSet<unsigned> WLS; |
| 701 | for (unsigned M : WL) |
| 702 | WLS.insert(M); |
| 703 | |
| 704 | // Otherwise, enumerate and copy over metadata from the old instruction to the |
| 705 | // new one. |
| 706 | SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; |
| 707 | SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs); |
| 708 | for (const auto &MD : TheMDs) { |
| 709 | if (WL.empty() || WLS.count(MD.first)) |
| 710 | setMetadata(MD.first, MD.second); |
| 711 | } |
| 712 | if (WL.empty() || WLS.count(LLVMContext::MD_dbg)) |
| 713 | setDebugLoc(SrcInst.getDebugLoc()); |
Xinliang David Li | ee836b9 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 716 | Instruction *Instruction::clone() const { |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 717 | Instruction *New = nullptr; |
| 718 | switch (getOpcode()) { |
| 719 | default: |
| 720 | llvm_unreachable("Unhandled Opcode."); |
| 721 | #define HANDLE_INST(num, opc, clas) \ |
| 722 | case Instruction::opc: \ |
| 723 | New = cast<clas>(this)->cloneImpl(); \ |
| 724 | break; |
| 725 | #include "llvm/IR/Instruction.def" |
| 726 | #undef HANDLE_INST |
| 727 | } |
| 728 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 729 | New->SubclassOptionalData = SubclassOptionalData; |
Xinliang David Li | ee836b9 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 730 | New->copyMetadata(*this); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 731 | return New; |
| 732 | } |
Dehao Chen | c7322da | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 733 | |
| 734 | void Instruction::updateProfWeight(uint64_t S, uint64_t T) { |
| 735 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 736 | if (ProfileData == nullptr) |
| 737 | return; |
| 738 | |
| 739 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); |
Dehao Chen | c6a0731 | 2017-05-05 00:47:34 +0000 | [diff] [blame] | 740 | if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && |
| 741 | !ProfDataName->getString().equals("VP"))) |
Dehao Chen | c7322da | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 742 | return; |
| 743 | |
Dehao Chen | c7322da | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 744 | MDBuilder MDB(getContext()); |
Dehao Chen | c6a0731 | 2017-05-05 00:47:34 +0000 | [diff] [blame] | 745 | SmallVector<Metadata *, 3> Vals; |
| 746 | Vals.push_back(ProfileData->getOperand(0)); |
| 747 | APInt APS(128, S), APT(128, T); |
| 748 | if (ProfDataName->getString().equals("branch_weights")) |
| 749 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) { |
| 750 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 751 | APInt Val(128, |
| 752 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i)) |
| 753 | ->getValue() |
| 754 | .getZExtValue()); |
| 755 | Val *= APS; |
| 756 | Vals.push_back(MDB.createConstant( |
| 757 | ConstantInt::get(Type::getInt64Ty(getContext()), |
| 758 | Val.udiv(APT).getLimitedValue()))); |
| 759 | } |
| 760 | else if (ProfDataName->getString().equals("VP")) |
| 761 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { |
| 762 | // The first value is the key of the value profile, which will not change. |
| 763 | Vals.push_back(ProfileData->getOperand(i)); |
| 764 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 765 | APInt Val(128, |
| 766 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) |
| 767 | ->getValue() |
| 768 | .getZExtValue()); |
| 769 | Val *= APS; |
| 770 | Vals.push_back(MDB.createConstant( |
| 771 | ConstantInt::get(Type::getInt64Ty(getContext()), |
| 772 | Val.udiv(APT).getLimitedValue()))); |
| 773 | } |
| 774 | setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); |
Dehao Chen | c7322da | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 775 | } |
Dehao Chen | f62637a | 2017-03-23 23:26:00 +0000 | [diff] [blame] | 776 | |
| 777 | void Instruction::setProfWeight(uint64_t W) { |
| 778 | assert((isa<CallInst>(this) || isa<InvokeInst>(this)) && |
| 779 | "Can only set weights for call and invoke instrucitons"); |
| 780 | SmallVector<uint32_t, 1> Weights; |
| 781 | Weights.push_back(W); |
| 782 | MDBuilder MDB(getContext()); |
| 783 | setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); |
| 784 | } |