Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Instruction class for the IR library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 16 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Instructions.h" |
Dehao Chen | e593049 | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 19 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Operator.h" |
| 22 | #include "llvm/IR/Type.h" |
Chris Lattner | 0e03ab6 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 25 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 26 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 27 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Chris Lattner | 3c78744 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 28 | |
| 29 | // If requested, insert this instruction into a basic block... |
| 30 | if (InsertBefore) { |
Yaron Keren | 43b4d38 | 2015-06-15 17:03:35 +0000 | [diff] [blame] | 31 | BasicBlock *BB = InsertBefore->getParent(); |
| 32 | assert(BB && "Instruction to insert before is not in a basic block!"); |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 33 | BB->getInstList().insert(InsertBefore->getIterator(), this); |
Chris Lattner | 3c78744 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 34 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 37 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 38 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 39 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Alkis Evlogimenos | 9f0fdf7 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 40 | |
| 41 | // append this instruction into the basic block |
| 42 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 43 | InsertAtEnd->getInstList().push_back(this); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 46 | Instruction::~Instruction() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 47 | assert(!Parent && "Instruction still linked in the program!"); |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 48 | if (hasMetadataHashEntry()) |
| 49 | clearMetadataHashEntries(); |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 53 | void Instruction::setParent(BasicBlock *P) { |
| 54 | Parent = P; |
| 55 | } |
| 56 | |
Mehdi Amini | 9a9738f | 2015-03-03 22:01:13 +0000 | [diff] [blame] | 57 | const Module *Instruction::getModule() const { |
| 58 | return getParent()->getModule(); |
| 59 | } |
| 60 | |
Sanjoy Das | 411fdcd | 2015-12-08 00:13:12 +0000 | [diff] [blame] | 61 | const Function *Instruction::getFunction() const { |
| 62 | return getParent()->getParent(); |
| 63 | } |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 65 | void Instruction::removeFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 66 | getParent()->getInstList().remove(getIterator()); |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Daniel Berlin | a5af720 | 2015-04-02 00:03:07 +0000 | [diff] [blame] | 69 | iplist<Instruction>::iterator Instruction::eraseFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 70 | return getParent()->getInstList().erase(getIterator()); |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 71 | } |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 72 | |
Sanjay Patel | f5c2d12 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 73 | /// Insert an unlinked instruction into a basic block immediately before the |
| 74 | /// specified instruction. |
Owen Anderson | 2505e0c | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 75 | void Instruction::insertBefore(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 76 | InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this); |
Owen Anderson | 2505e0c | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Sanjay Patel | f5c2d12 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 79 | /// Insert an unlinked instruction into a basic block immediately after the |
| 80 | /// specified instruction. |
Chris Lattner | 6e66d0a | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 81 | void Instruction::insertAfter(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 82 | InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(), |
| 83 | this); |
Chris Lattner | 6e66d0a | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Sanjay Patel | f5c2d12 | 2016-01-06 00:18:29 +0000 | [diff] [blame] | 86 | /// Unlink this instruction from its current basic block and insert it into the |
| 87 | /// basic block that MovePos lives in, right before MovePos. |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 88 | void Instruction::moveBefore(Instruction *MovePos) { |
Duncan P. N. Exon Smith | 362d1204 | 2016-08-17 01:54:41 +0000 | [diff] [blame] | 89 | moveBefore(*MovePos->getParent(), MovePos->getIterator()); |
| 90 | } |
| 91 | |
Sanjay Patel | 674d2c2 | 2017-08-29 14:07:48 +0000 | [diff] [blame] | 92 | void Instruction::moveAfter(Instruction *MovePos) { |
| 93 | moveBefore(*MovePos->getParent(), ++MovePos->getIterator()); |
| 94 | } |
| 95 | |
Duncan P. N. Exon Smith | 362d1204 | 2016-08-17 01:54:41 +0000 | [diff] [blame] | 96 | void Instruction::moveBefore(BasicBlock &BB, |
| 97 | SymbolTableList<Instruction>::iterator I) { |
| 98 | assert(I == BB.end() || I->getParent() == &BB); |
| 99 | BB.getInstList().splice(I, getParent()->getInstList(), getIterator()); |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 102 | void Instruction::setHasNoUnsignedWrap(bool b) { |
| 103 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 104 | } |
| 105 | |
| 106 | void Instruction::setHasNoSignedWrap(bool b) { |
| 107 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 108 | } |
| 109 | |
| 110 | void Instruction::setIsExact(bool b) { |
| 111 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
| 112 | } |
| 113 | |
| 114 | bool Instruction::hasNoUnsignedWrap() const { |
| 115 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 116 | } |
| 117 | |
| 118 | bool Instruction::hasNoSignedWrap() const { |
| 119 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 120 | } |
| 121 | |
Sanjoy Das | aa722ae | 2017-02-23 22:50:52 +0000 | [diff] [blame] | 122 | void Instruction::dropPoisonGeneratingFlags() { |
| 123 | switch (getOpcode()) { |
| 124 | case Instruction::Add: |
| 125 | case Instruction::Sub: |
| 126 | case Instruction::Mul: |
| 127 | case Instruction::Shl: |
| 128 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false); |
| 129 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false); |
| 130 | break; |
| 131 | |
| 132 | case Instruction::UDiv: |
| 133 | case Instruction::SDiv: |
| 134 | case Instruction::AShr: |
| 135 | case Instruction::LShr: |
| 136 | cast<PossiblyExactOperator>(this)->setIsExact(false); |
| 137 | break; |
| 138 | |
| 139 | case Instruction::GetElementPtr: |
| 140 | cast<GetElementPtrInst>(this)->setIsInBounds(false); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 145 | bool Instruction::isExact() const { |
| 146 | return cast<PossiblyExactOperator>(this)->isExact(); |
| 147 | } |
| 148 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 149 | void Instruction::setHasUnsafeAlgebra(bool B) { |
| 150 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 151 | cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B); |
| 152 | } |
| 153 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 154 | void Instruction::setHasNoNaNs(bool B) { |
| 155 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 156 | cast<FPMathOperator>(this)->setHasNoNaNs(B); |
| 157 | } |
| 158 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 159 | void Instruction::setHasNoInfs(bool B) { |
| 160 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 161 | cast<FPMathOperator>(this)->setHasNoInfs(B); |
| 162 | } |
| 163 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 164 | void Instruction::setHasNoSignedZeros(bool B) { |
| 165 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 166 | cast<FPMathOperator>(this)->setHasNoSignedZeros(B); |
| 167 | } |
| 168 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 169 | void Instruction::setHasAllowReciprocal(bool B) { |
| 170 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 171 | cast<FPMathOperator>(this)->setHasAllowReciprocal(B); |
| 172 | } |
| 173 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 174 | void Instruction::setFastMathFlags(FastMathFlags FMF) { |
| 175 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 176 | cast<FPMathOperator>(this)->setFastMathFlags(FMF); |
| 177 | } |
| 178 | |
Sanjay Patel | b2325b9 | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 179 | void Instruction::copyFastMathFlags(FastMathFlags FMF) { |
| 180 | assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); |
| 181 | cast<FPMathOperator>(this)->copyFastMathFlags(FMF); |
| 182 | } |
| 183 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 184 | bool Instruction::hasUnsafeAlgebra() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 185 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 186 | return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); |
| 187 | } |
| 188 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 189 | bool Instruction::hasNoNaNs() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 190 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 191 | return cast<FPMathOperator>(this)->hasNoNaNs(); |
| 192 | } |
| 193 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 194 | bool Instruction::hasNoInfs() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 195 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 196 | return cast<FPMathOperator>(this)->hasNoInfs(); |
| 197 | } |
| 198 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 199 | bool Instruction::hasNoSignedZeros() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 200 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 201 | return cast<FPMathOperator>(this)->hasNoSignedZeros(); |
| 202 | } |
| 203 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 204 | bool Instruction::hasAllowReciprocal() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 205 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 206 | return cast<FPMathOperator>(this)->hasAllowReciprocal(); |
| 207 | } |
| 208 | |
Adam Nemet | cd847a8 | 2017-03-28 20:11:52 +0000 | [diff] [blame] | 209 | bool Instruction::hasAllowContract() const { |
| 210 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
| 211 | return cast<FPMathOperator>(this)->hasAllowContract(); |
| 212 | } |
| 213 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 214 | FastMathFlags Instruction::getFastMathFlags() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 215 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 216 | return cast<FPMathOperator>(this)->getFastMathFlags(); |
| 217 | } |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 218 | |
Michael Ilseman | 05d3bf77a | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 219 | void Instruction::copyFastMathFlags(const Instruction *I) { |
Sanjay Patel | b2325b9 | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 220 | copyFastMathFlags(I->getFastMathFlags()); |
Michael Ilseman | 05d3bf77a | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 221 | } |
| 222 | |
George Burgess IV | a20352e | 2017-06-09 03:56:15 +0000 | [diff] [blame] | 223 | void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) { |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 224 | // Copy the wrapping flags. |
George Burgess IV | a20352e | 2017-06-09 03:56:15 +0000 | [diff] [blame] | 225 | if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) { |
| 226 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 227 | setHasNoSignedWrap(OB->hasNoSignedWrap()); |
| 228 | setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); |
| 229 | } |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Copy the exact flag. |
| 233 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 234 | if (isa<PossiblyExactOperator>(this)) |
| 235 | setIsExact(PE->isExact()); |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 236 | |
| 237 | // Copy the fast-math flags. |
| 238 | if (auto *FP = dyn_cast<FPMathOperator>(V)) |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 239 | if (isa<FPMathOperator>(this)) |
| 240 | copyFastMathFlags(FP->getFastMathFlags()); |
David Majnemer | 92f84cc | 2016-07-15 05:02:31 +0000 | [diff] [blame] | 241 | |
| 242 | if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) |
| 243 | if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) |
| 244 | DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds()); |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void Instruction::andIRFlags(const Value *V) { |
| 248 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 249 | if (isa<OverflowingBinaryOperator>(this)) { |
| 250 | setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); |
| 251 | setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); |
| 252 | } |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 256 | if (isa<PossiblyExactOperator>(this)) |
| 257 | setIsExact(isExact() & PE->isExact()); |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 258 | |
| 259 | if (auto *FP = dyn_cast<FPMathOperator>(V)) { |
David Majnemer | d0ce8f1 | 2016-04-22 06:37:51 +0000 | [diff] [blame] | 260 | if (isa<FPMathOperator>(this)) { |
| 261 | FastMathFlags FM = getFastMathFlags(); |
| 262 | FM &= FP->getFastMathFlags(); |
| 263 | copyFastMathFlags(FM); |
| 264 | } |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 265 | } |
David Majnemer | 92f84cc | 2016-07-15 05:02:31 +0000 | [diff] [blame] | 266 | |
| 267 | if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) |
| 268 | if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) |
| 269 | DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds()); |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 270 | } |
Michael Ilseman | 05d3bf77a | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 271 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 272 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 273 | switch (OpCode) { |
| 274 | // Terminators |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 275 | case Ret: return "ret"; |
| 276 | case Br: return "br"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 277 | case Switch: return "switch"; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 278 | case IndirectBr: return "indirectbr"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 279 | case Invoke: return "invoke"; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 280 | case Resume: return "resume"; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 281 | case Unreachable: return "unreachable"; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 282 | case CleanupRet: return "cleanupret"; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 283 | case CatchRet: return "catchret"; |
| 284 | case CatchPad: return "catchpad"; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 285 | case CatchSwitch: return "catchswitch"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 286 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 287 | // Standard binary operators... |
| 288 | case Add: return "add"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 289 | case FAdd: return "fadd"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 290 | case Sub: return "sub"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 291 | case FSub: return "fsub"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 292 | case Mul: return "mul"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 293 | case FMul: return "fmul"; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 294 | case UDiv: return "udiv"; |
| 295 | case SDiv: return "sdiv"; |
| 296 | case FDiv: return "fdiv"; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 297 | case URem: return "urem"; |
| 298 | case SRem: return "srem"; |
| 299 | case FRem: return "frem"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 300 | |
| 301 | // Logical operators... |
| 302 | case And: return "and"; |
| 303 | case Or : return "or"; |
| 304 | case Xor: return "xor"; |
| 305 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 306 | // Memory instructions... |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 307 | case Alloca: return "alloca"; |
| 308 | case Load: return "load"; |
| 309 | case Store: return "store"; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 310 | case AtomicCmpXchg: return "cmpxchg"; |
| 311 | case AtomicRMW: return "atomicrmw"; |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 312 | case Fence: return "fence"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 313 | case GetElementPtr: return "getelementptr"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 314 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 315 | // Convert instructions... |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 316 | case Trunc: return "trunc"; |
| 317 | case ZExt: return "zext"; |
| 318 | case SExt: return "sext"; |
| 319 | case FPTrunc: return "fptrunc"; |
| 320 | case FPExt: return "fpext"; |
| 321 | case FPToUI: return "fptoui"; |
| 322 | case FPToSI: return "fptosi"; |
| 323 | case UIToFP: return "uitofp"; |
| 324 | case SIToFP: return "sitofp"; |
| 325 | case IntToPtr: return "inttoptr"; |
| 326 | case PtrToInt: return "ptrtoint"; |
| 327 | case BitCast: return "bitcast"; |
| 328 | case AddrSpaceCast: return "addrspacecast"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 329 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 330 | // Other instructions... |
Reid Spencer | 45e5239 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 331 | case ICmp: return "icmp"; |
| 332 | case FCmp: return "fcmp"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 333 | case PHI: return "phi"; |
| 334 | case Select: return "select"; |
| 335 | case Call: return "call"; |
| 336 | case Shl: return "shl"; |
| 337 | case LShr: return "lshr"; |
| 338 | case AShr: return "ashr"; |
| 339 | case VAArg: return "va_arg"; |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 340 | case ExtractElement: return "extractelement"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 341 | case InsertElement: return "insertelement"; |
| 342 | case ShuffleVector: return "shufflevector"; |
Matthijs Kooijman | bf8d6ce | 2008-05-30 10:31:54 +0000 | [diff] [blame] | 343 | case ExtractValue: return "extractvalue"; |
| 344 | case InsertValue: return "insertvalue"; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 345 | case LandingPad: return "landingpad"; |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 346 | case CleanupPad: return "cleanuppad"; |
Chris Lattner | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 347 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 348 | default: return "<Invalid operator> "; |
| 349 | } |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 351 | |
Sanjay Patel | a40c479 | 2016-10-05 18:51:12 +0000 | [diff] [blame] | 352 | /// Return true if both instructions have the same special state. This must be |
JF Bastien | 3b6eaac | 2016-04-11 22:30:37 +0000 | [diff] [blame] | 353 | /// kept in sync with FunctionComparator::cmpOperations in |
| 354 | /// lib/Transforms/IPO/MergeFunctions.cpp. |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 355 | static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, |
| 356 | bool IgnoreAlignment = false) { |
| 357 | assert(I1->getOpcode() == I2->getOpcode() && |
| 358 | "Can not compare special state of different instructions"); |
| 359 | |
JF Bastien | 5502e91 | 2016-04-12 18:06:55 +0000 | [diff] [blame] | 360 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1)) |
| 361 | return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() && |
| 362 | (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() || |
| 363 | IgnoreAlignment); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 364 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 365 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
| 366 | (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() || |
| 367 | IgnoreAlignment) && |
| 368 | LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 369 | LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 370 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 371 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
| 372 | (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() || |
| 373 | IgnoreAlignment) && |
| 374 | SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 375 | SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 376 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 377 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 378 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
| 379 | return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && |
| 380 | CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
Sanjoy Das | 2de4d0a | 2015-12-14 19:11:35 +0000 | [diff] [blame] | 381 | CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && |
| 382 | CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 383 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 384 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
Sanjoy Das | 2de4d0a | 2015-12-14 19:11:35 +0000 | [diff] [blame] | 385 | CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && |
| 386 | CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 387 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) |
| 388 | return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); |
| 389 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) |
| 390 | return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); |
| 391 | if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) |
| 392 | return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 393 | FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 394 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) |
| 395 | return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 396 | CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 397 | CXI->getSuccessOrdering() == |
| 398 | cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && |
| 399 | CXI->getFailureOrdering() == |
| 400 | cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 401 | CXI->getSyncScopeID() == |
| 402 | cast<AtomicCmpXchgInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 403 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) |
| 404 | return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && |
| 405 | RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && |
| 406 | RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 407 | RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID(); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 408 | |
| 409 | return true; |
| 410 | } |
| 411 | |
Chris Lattner | 378b041 | 2008-11-27 08:39:18 +0000 | [diff] [blame] | 412 | bool Instruction::isIdenticalTo(const Instruction *I) const { |
Dan Gohman | 23d2c76 | 2009-08-25 22:24:20 +0000 | [diff] [blame] | 413 | return isIdenticalToWhenDefined(I) && |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 414 | SubclassOptionalData == I->SubclassOptionalData; |
| 415 | } |
| 416 | |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 417 | bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 418 | if (getOpcode() != I->getOpcode() || |
| 419 | getNumOperands() != I->getNumOperands() || |
| 420 | getType() != I->getType()) |
| 421 | return false; |
| 422 | |
NAKAMURA Takumi | aaffd70 | 2014-06-02 01:35:34 +0000 | [diff] [blame] | 423 | // If both instructions have no operands, they are identical. |
| 424 | if (getNumOperands() == 0 && I->getNumOperands() == 0) |
| 425 | return haveSameSpecialState(this, I); |
| 426 | |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 427 | // We have two instructions of identical opcode and #operands. Check to see |
| 428 | // if all operands are the same. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 429 | if (!std::equal(op_begin(), op_end(), I->op_begin())) |
| 430 | return false; |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 431 | |
Joel Jones | 3d90a9a | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 432 | if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { |
| 433 | const PHINode *otherPHI = cast<PHINode>(I); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 434 | return std::equal(thisPHI->block_begin(), thisPHI->block_end(), |
| 435 | otherPHI->block_begin()); |
Joel Jones | 3d90a9a | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 436 | } |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 437 | |
| 438 | return haveSameSpecialState(this, I); |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 439 | } |
| 440 | |
JF Bastien | 3b6eaac | 2016-04-11 22:30:37 +0000 | [diff] [blame] | 441 | // Keep this in sync with FunctionComparator::cmpOperations in |
Dan Gohman | 17fb0d2 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 442 | // lib/Transforms/IPO/MergeFunctions.cpp. |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 443 | bool Instruction::isSameOperationAs(const Instruction *I, |
| 444 | unsigned flags) const { |
| 445 | bool IgnoreAlignment = flags & CompareIgnoringAlignment; |
| 446 | bool UseScalarTypes = flags & CompareUsingScalarTypes; |
| 447 | |
Dan Gohman | 17fb0d2 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 448 | if (getOpcode() != I->getOpcode() || |
| 449 | getNumOperands() != I->getNumOperands() || |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 450 | (UseScalarTypes ? |
| 451 | getType()->getScalarType() != I->getType()->getScalarType() : |
| 452 | getType() != I->getType())) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 453 | return false; |
| 454 | |
| 455 | // We have two instructions of identical opcode and #operands. Check to see |
| 456 | // if all operands are the same type |
| 457 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 458 | if (UseScalarTypes ? |
| 459 | getOperand(i)->getType()->getScalarType() != |
| 460 | I->getOperand(i)->getType()->getScalarType() : |
| 461 | getOperand(i)->getType() != I->getOperand(i)->getType()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 462 | return false; |
| 463 | |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 464 | return haveSameSpecialState(this, I, IgnoreAlignment); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 467 | bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 468 | for (const Use &U : uses()) { |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 469 | // PHI nodes uses values in the corresponding predecessor block. For other |
| 470 | // instructions, just check to see whether the parent of the use matches up. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 471 | const Instruction *I = cast<Instruction>(U.getUser()); |
| 472 | const PHINode *PN = dyn_cast<PHINode>(I); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 473 | if (!PN) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 474 | if (I->getParent() != BB) |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 475 | return true; |
| 476 | continue; |
| 477 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 478 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 479 | if (PN->getIncomingBlock(U) != BB) |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 480 | return true; |
| 481 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 482 | return false; |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 485 | bool Instruction::mayReadFromMemory() const { |
| 486 | switch (getOpcode()) { |
| 487 | default: return false; |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 488 | case Instruction::VAArg: |
Chris Lattner | 954907a | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 489 | case Instruction::Load: |
Eli Friedman | 89b694b | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 490 | case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 491 | case Instruction::AtomicCmpXchg: |
| 492 | case Instruction::AtomicRMW: |
David Majnemer | 880c2cb | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 493 | case Instruction::CatchPad: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 494 | case Instruction::CatchRet: |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 495 | return true; |
| 496 | case Instruction::Call: |
| 497 | return !cast<CallInst>(this)->doesNotAccessMemory(); |
| 498 | case Instruction::Invoke: |
| 499 | return !cast<InvokeInst>(this)->doesNotAccessMemory(); |
Chris Lattner | 954907a | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 500 | case Instruction::Store: |
Eli Friedman | b9d5a63 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 501 | return !cast<StoreInst>(this)->isUnordered(); |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 504 | |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 505 | bool Instruction::mayWriteToMemory() const { |
| 506 | switch (getOpcode()) { |
| 507 | default: return false; |
Eli Friedman | 89b694b | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 508 | case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 509 | case Instruction::Store: |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 510 | case Instruction::VAArg: |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 511 | case Instruction::AtomicCmpXchg: |
| 512 | case Instruction::AtomicRMW: |
David Majnemer | 880c2cb | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 513 | case Instruction::CatchPad: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 514 | case Instruction::CatchRet: |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 515 | return true; |
| 516 | case Instruction::Call: |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 517 | return !cast<CallInst>(this)->onlyReadsMemory(); |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 518 | case Instruction::Invoke: |
| 519 | return !cast<InvokeInst>(this)->onlyReadsMemory(); |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 520 | case Instruction::Load: |
Eli Friedman | b9d5a63 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 521 | return !cast<LoadInst>(this)->isUnordered(); |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 524 | |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 525 | bool Instruction::isAtomic() const { |
| 526 | switch (getOpcode()) { |
| 527 | default: |
| 528 | return false; |
| 529 | case Instruction::AtomicCmpXchg: |
| 530 | case Instruction::AtomicRMW: |
| 531 | case Instruction::Fence: |
| 532 | return true; |
| 533 | case Instruction::Load: |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 534 | return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 535 | case Instruction::Store: |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 536 | return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Tim Shen | 04de70d | 2017-05-09 15:27:17 +0000 | [diff] [blame] | 540 | bool Instruction::hasAtomicLoad() const { |
| 541 | assert(isAtomic()); |
| 542 | switch (getOpcode()) { |
| 543 | default: |
| 544 | return false; |
| 545 | case Instruction::AtomicCmpXchg: |
| 546 | case Instruction::AtomicRMW: |
| 547 | case Instruction::Load: |
| 548 | return true; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | bool Instruction::hasAtomicStore() const { |
| 553 | assert(isAtomic()); |
| 554 | switch (getOpcode()) { |
| 555 | default: |
| 556 | return false; |
| 557 | case Instruction::AtomicCmpXchg: |
| 558 | case Instruction::AtomicRMW: |
| 559 | case Instruction::Store: |
| 560 | return true; |
| 561 | } |
| 562 | } |
| 563 | |
Duncan Sands | 1efabaa | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 564 | bool Instruction::mayThrow() const { |
| 565 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 566 | return !CI->doesNotThrow(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 567 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(this)) |
| 568 | return CRI->unwindsToCaller(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 569 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this)) |
| 570 | return CatchSwitch->unwindsToCaller(); |
Bill Wendling | d7c6c91 | 2011-08-16 21:15:50 +0000 | [diff] [blame] | 571 | return isa<ResumeInst>(this); |
Duncan Sands | 1efabaa | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Shuxin Yang | 01ab5d7 | 2012-11-29 01:47:31 +0000 | [diff] [blame] | 574 | bool Instruction::isAssociative() const { |
| 575 | unsigned Opcode = getOpcode(); |
| 576 | if (isAssociative(Opcode)) |
| 577 | return true; |
| 578 | |
| 579 | switch (Opcode) { |
| 580 | case FMul: |
| 581 | case FAdd: |
| 582 | return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); |
| 583 | default: |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 588 | Instruction *Instruction::cloneImpl() const { |
| 589 | llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); |
| 590 | } |
| 591 | |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 592 | void Instruction::swapProfMetadata() { |
| 593 | MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 594 | if (!ProfileData || ProfileData->getNumOperands() != 3 || |
| 595 | !isa<MDString>(ProfileData->getOperand(0))) |
| 596 | return; |
| 597 | |
| 598 | MDString *MDName = cast<MDString>(ProfileData->getOperand(0)); |
| 599 | if (MDName->getString() != "branch_weights") |
| 600 | return; |
| 601 | |
| 602 | // The first operand is the name. Fetch them backwards and build a new one. |
| 603 | Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), |
| 604 | ProfileData->getOperand(1)}; |
| 605 | setMetadata(LLVMContext::MD_prof, |
| 606 | MDNode::get(ProfileData->getContext(), Ops)); |
| 607 | } |
| 608 | |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 609 | void Instruction::copyMetadata(const Instruction &SrcInst, |
| 610 | ArrayRef<unsigned> WL) { |
| 611 | if (!SrcInst.hasMetadata()) |
| 612 | return; |
| 613 | |
| 614 | DenseSet<unsigned> WLS; |
| 615 | for (unsigned M : WL) |
| 616 | WLS.insert(M); |
| 617 | |
| 618 | // Otherwise, enumerate and copy over metadata from the old instruction to the |
| 619 | // new one. |
| 620 | SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; |
| 621 | SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs); |
| 622 | for (const auto &MD : TheMDs) { |
| 623 | if (WL.empty() || WLS.count(MD.first)) |
| 624 | setMetadata(MD.first, MD.second); |
| 625 | } |
| 626 | if (WL.empty() || WLS.count(LLVMContext::MD_dbg)) |
| 627 | setDebugLoc(SrcInst.getDebugLoc()); |
| 628 | return; |
| 629 | } |
| 630 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 631 | Instruction *Instruction::clone() const { |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 632 | Instruction *New = nullptr; |
| 633 | switch (getOpcode()) { |
| 634 | default: |
| 635 | llvm_unreachable("Unhandled Opcode."); |
| 636 | #define HANDLE_INST(num, opc, clas) \ |
| 637 | case Instruction::opc: \ |
| 638 | New = cast<clas>(this)->cloneImpl(); \ |
| 639 | break; |
| 640 | #include "llvm/IR/Instruction.def" |
| 641 | #undef HANDLE_INST |
| 642 | } |
| 643 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 644 | New->SubclassOptionalData = SubclassOptionalData; |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 645 | New->copyMetadata(*this); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 646 | return New; |
| 647 | } |
Dehao Chen | e593049 | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 648 | |
| 649 | void Instruction::updateProfWeight(uint64_t S, uint64_t T) { |
| 650 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 651 | if (ProfileData == nullptr) |
| 652 | return; |
| 653 | |
| 654 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); |
Dehao Chen | a75d0da | 2017-05-05 00:47:34 +0000 | [diff] [blame] | 655 | if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && |
| 656 | !ProfDataName->getString().equals("VP"))) |
Dehao Chen | e593049 | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 657 | return; |
| 658 | |
Dehao Chen | e593049 | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 659 | MDBuilder MDB(getContext()); |
Dehao Chen | a75d0da | 2017-05-05 00:47:34 +0000 | [diff] [blame] | 660 | SmallVector<Metadata *, 3> Vals; |
| 661 | Vals.push_back(ProfileData->getOperand(0)); |
| 662 | APInt APS(128, S), APT(128, T); |
| 663 | if (ProfDataName->getString().equals("branch_weights")) |
| 664 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) { |
| 665 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 666 | APInt Val(128, |
| 667 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i)) |
| 668 | ->getValue() |
| 669 | .getZExtValue()); |
| 670 | Val *= APS; |
| 671 | Vals.push_back(MDB.createConstant( |
| 672 | ConstantInt::get(Type::getInt64Ty(getContext()), |
| 673 | Val.udiv(APT).getLimitedValue()))); |
| 674 | } |
| 675 | else if (ProfDataName->getString().equals("VP")) |
| 676 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { |
| 677 | // The first value is the key of the value profile, which will not change. |
| 678 | Vals.push_back(ProfileData->getOperand(i)); |
| 679 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 680 | APInt Val(128, |
| 681 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) |
| 682 | ->getValue() |
| 683 | .getZExtValue()); |
| 684 | Val *= APS; |
| 685 | Vals.push_back(MDB.createConstant( |
| 686 | ConstantInt::get(Type::getInt64Ty(getContext()), |
| 687 | Val.udiv(APT).getLimitedValue()))); |
| 688 | } |
| 689 | setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); |
Dehao Chen | e593049 | 2017-03-20 16:40:44 +0000 | [diff] [blame] | 690 | } |
Dehao Chen | 722e940 | 2017-03-23 23:26:00 +0000 | [diff] [blame] | 691 | |
| 692 | void Instruction::setProfWeight(uint64_t W) { |
| 693 | assert((isa<CallInst>(this) || isa<InvokeInst>(this)) && |
| 694 | "Can only set weights for call and invoke instrucitons"); |
| 695 | SmallVector<uint32_t, 1> Weights; |
| 696 | Weights.push_back(W); |
| 697 | MDBuilder MDB(getContext()); |
| 698 | setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); |
| 699 | } |