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 | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 15 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/IR/Operator.h" |
| 20 | #include "llvm/IR/Type.h" |
Chris Lattner | 0e03ab6 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 23 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 24 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 25 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Chris Lattner | 3c78744 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 26 | |
| 27 | // If requested, insert this instruction into a basic block... |
| 28 | if (InsertBefore) { |
Yaron Keren | 43b4d38 | 2015-06-15 17:03:35 +0000 | [diff] [blame] | 29 | BasicBlock *BB = InsertBefore->getParent(); |
| 30 | 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] | 31 | BB->getInstList().insert(InsertBefore->getIterator(), this); |
Chris Lattner | 3c78744 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 32 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 35 | Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 36 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 37 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { |
Alkis Evlogimenos | 9f0fdf7 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 38 | |
| 39 | // append this instruction into the basic block |
| 40 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 41 | InsertAtEnd->getInstList().push_back(this); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 45 | // Out of line virtual method, so the vtable, etc has a home. |
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 | |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 61 | Module *Instruction::getModule() { |
| 62 | return getParent()->getModule(); |
| 63 | } |
| 64 | |
Sanjoy Das | 411fdcd | 2015-12-08 00:13:12 +0000 | [diff] [blame] | 65 | Function *Instruction::getFunction() { return getParent()->getParent(); } |
| 66 | |
| 67 | const Function *Instruction::getFunction() const { |
| 68 | return getParent()->getParent(); |
| 69 | } |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 71 | void Instruction::removeFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 72 | getParent()->getInstList().remove(getIterator()); |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Berlin | a5af720 | 2015-04-02 00:03:07 +0000 | [diff] [blame] | 75 | iplist<Instruction>::iterator Instruction::eraseFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 76 | return getParent()->getInstList().erase(getIterator()); |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 77 | } |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 78 | |
Owen Anderson | 2505e0c | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 79 | /// insertBefore - Insert an unlinked instructions into a basic block |
| 80 | /// immediately before the specified instruction. |
| 81 | void Instruction::insertBefore(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 82 | InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this); |
Owen Anderson | 2505e0c | 2008-06-17 18:29:27 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 6e66d0a | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 85 | /// insertAfter - Insert an unlinked instructions into a basic block |
| 86 | /// immediately after the specified instruction. |
| 87 | void Instruction::insertAfter(Instruction *InsertPos) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 88 | InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(), |
| 89 | this); |
Chris Lattner | 6e66d0a | 2009-01-13 07:43:51 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 92 | /// moveBefore - Unlink this instruction from its current basic block and |
| 93 | /// insert it into the basic block that MovePos lives in, right before |
| 94 | /// MovePos. |
| 95 | void Instruction::moveBefore(Instruction *MovePos) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 96 | MovePos->getParent()->getInstList().splice( |
| 97 | MovePos->getIterator(), getParent()->getInstList(), getIterator()); |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 100 | /// Set or clear the unsafe-algebra flag on this instruction, which must be an |
| 101 | /// operator which supports this flag. See LangRef.html for the meaning of this |
| 102 | /// flag. |
| 103 | void Instruction::setHasUnsafeAlgebra(bool B) { |
| 104 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 105 | cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B); |
| 106 | } |
| 107 | |
| 108 | /// Set or clear the NoNaNs flag on this instruction, which must be an operator |
| 109 | /// which supports this flag. See LangRef.html for the meaning of this flag. |
| 110 | void Instruction::setHasNoNaNs(bool B) { |
| 111 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 112 | cast<FPMathOperator>(this)->setHasNoNaNs(B); |
| 113 | } |
| 114 | |
| 115 | /// Set or clear the no-infs flag on this instruction, which must be an operator |
| 116 | /// which supports this flag. See LangRef.html for the meaning of this flag. |
| 117 | void Instruction::setHasNoInfs(bool B) { |
| 118 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 119 | cast<FPMathOperator>(this)->setHasNoInfs(B); |
| 120 | } |
| 121 | |
| 122 | /// Set or clear the no-signed-zeros flag on this instruction, which must be an |
| 123 | /// operator which supports this flag. See LangRef.html for the meaning of this |
| 124 | /// flag. |
| 125 | void Instruction::setHasNoSignedZeros(bool B) { |
| 126 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 127 | cast<FPMathOperator>(this)->setHasNoSignedZeros(B); |
| 128 | } |
| 129 | |
| 130 | /// Set or clear the allow-reciprocal flag on this instruction, which must be an |
| 131 | /// operator which supports this flag. See LangRef.html for the meaning of this |
| 132 | /// flag. |
| 133 | void Instruction::setHasAllowReciprocal(bool B) { |
| 134 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 135 | cast<FPMathOperator>(this)->setHasAllowReciprocal(B); |
| 136 | } |
| 137 | |
| 138 | /// Convenience function for setting all the fast-math flags on this |
| 139 | /// instruction, which must be an operator which supports these flags. See |
| 140 | /// LangRef.html for the meaning of these flats. |
| 141 | void Instruction::setFastMathFlags(FastMathFlags FMF) { |
| 142 | assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); |
| 143 | cast<FPMathOperator>(this)->setFastMathFlags(FMF); |
| 144 | } |
| 145 | |
Sanjay Patel | b2325b9 | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 146 | void Instruction::copyFastMathFlags(FastMathFlags FMF) { |
| 147 | assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); |
| 148 | cast<FPMathOperator>(this)->copyFastMathFlags(FMF); |
| 149 | } |
| 150 | |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 151 | /// Determine whether the unsafe-algebra flag is set. |
| 152 | bool Instruction::hasUnsafeAlgebra() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 153 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 154 | return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); |
| 155 | } |
| 156 | |
| 157 | /// Determine whether the no-NaNs flag is set. |
| 158 | bool Instruction::hasNoNaNs() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 159 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 160 | return cast<FPMathOperator>(this)->hasNoNaNs(); |
| 161 | } |
| 162 | |
| 163 | /// Determine whether the no-infs flag is set. |
| 164 | bool Instruction::hasNoInfs() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 165 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 166 | return cast<FPMathOperator>(this)->hasNoInfs(); |
| 167 | } |
| 168 | |
| 169 | /// Determine whether the no-signed-zeros flag is set. |
| 170 | bool Instruction::hasNoSignedZeros() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 171 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 172 | return cast<FPMathOperator>(this)->hasNoSignedZeros(); |
| 173 | } |
| 174 | |
| 175 | /// Determine whether the allow-reciprocal flag is set. |
| 176 | bool Instruction::hasAllowReciprocal() const { |
Chad Rosier | 829cc2e | 2014-06-11 18:26:29 +0000 | [diff] [blame] | 177 | assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 178 | return cast<FPMathOperator>(this)->hasAllowReciprocal(); |
| 179 | } |
| 180 | |
| 181 | /// Convenience function for getting all the fast-math flags, which must be an |
| 182 | /// operator which supports these flags. See LangRef.html for the meaning of |
Sanjay Patel | 1893a25 | 2014-09-10 16:58:40 +0000 | [diff] [blame] | 183 | /// these flags. |
Michael Ilseman | 149209e | 2012-11-27 00:41:22 +0000 | [diff] [blame] | 184 | FastMathFlags Instruction::getFastMathFlags() 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)->getFastMathFlags(); |
| 187 | } |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 188 | |
Michael Ilseman | 05d3bf77a | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 189 | /// Copy I's fast-math flags |
| 190 | void Instruction::copyFastMathFlags(const Instruction *I) { |
Sanjay Patel | b2325b9 | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 191 | copyFastMathFlags(I->getFastMathFlags()); |
Michael Ilseman | 05d3bf77a | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 195 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 196 | switch (OpCode) { |
| 197 | // Terminators |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 198 | case Ret: return "ret"; |
| 199 | case Br: return "br"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 200 | case Switch: return "switch"; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 201 | case IndirectBr: return "indirectbr"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 202 | case Invoke: return "invoke"; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 203 | case Resume: return "resume"; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 204 | case Unreachable: return "unreachable"; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 205 | case CleanupRet: return "cleanupret"; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 206 | case CatchRet: return "catchret"; |
| 207 | case CatchPad: return "catchpad"; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 208 | case CatchSwitch: return "catchswitch"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 209 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 210 | // Standard binary operators... |
| 211 | case Add: return "add"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 212 | case FAdd: return "fadd"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 213 | case Sub: return "sub"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 214 | case FSub: return "fsub"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 215 | case Mul: return "mul"; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 216 | case FMul: return "fmul"; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 217 | case UDiv: return "udiv"; |
| 218 | case SDiv: return "sdiv"; |
| 219 | case FDiv: return "fdiv"; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 220 | case URem: return "urem"; |
| 221 | case SRem: return "srem"; |
| 222 | case FRem: return "frem"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 223 | |
| 224 | // Logical operators... |
| 225 | case And: return "and"; |
| 226 | case Or : return "or"; |
| 227 | case Xor: return "xor"; |
| 228 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 229 | // Memory instructions... |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 230 | case Alloca: return "alloca"; |
| 231 | case Load: return "load"; |
| 232 | case Store: return "store"; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 233 | case AtomicCmpXchg: return "cmpxchg"; |
| 234 | case AtomicRMW: return "atomicrmw"; |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 235 | case Fence: return "fence"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 236 | case GetElementPtr: return "getelementptr"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 237 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 238 | // Convert instructions... |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 239 | case Trunc: return "trunc"; |
| 240 | case ZExt: return "zext"; |
| 241 | case SExt: return "sext"; |
| 242 | case FPTrunc: return "fptrunc"; |
| 243 | case FPExt: return "fpext"; |
| 244 | case FPToUI: return "fptoui"; |
| 245 | case FPToSI: return "fptosi"; |
| 246 | case UIToFP: return "uitofp"; |
| 247 | case SIToFP: return "sitofp"; |
| 248 | case IntToPtr: return "inttoptr"; |
| 249 | case PtrToInt: return "ptrtoint"; |
| 250 | case BitCast: return "bitcast"; |
| 251 | case AddrSpaceCast: return "addrspacecast"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 252 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 253 | // Other instructions... |
Reid Spencer | 45e5239 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 254 | case ICmp: return "icmp"; |
| 255 | case FCmp: return "fcmp"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 256 | case PHI: return "phi"; |
| 257 | case Select: return "select"; |
| 258 | case Call: return "call"; |
| 259 | case Shl: return "shl"; |
| 260 | case LShr: return "lshr"; |
| 261 | case AShr: return "ashr"; |
| 262 | case VAArg: return "va_arg"; |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 263 | case ExtractElement: return "extractelement"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 264 | case InsertElement: return "insertelement"; |
| 265 | case ShuffleVector: return "shufflevector"; |
Matthijs Kooijman | bf8d6ce | 2008-05-30 10:31:54 +0000 | [diff] [blame] | 266 | case ExtractValue: return "extractvalue"; |
| 267 | case InsertValue: return "insertvalue"; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 268 | case LandingPad: return "landingpad"; |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 269 | case CleanupPad: return "cleanuppad"; |
Chris Lattner | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 270 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 271 | default: return "<Invalid operator> "; |
| 272 | } |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 274 | |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 275 | /// Return true if both instructions have the same special state |
| 276 | /// This must be kept in sync with lib/Transforms/IPO/MergeFunctions.cpp. |
| 277 | static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, |
| 278 | bool IgnoreAlignment = false) { |
| 279 | assert(I1->getOpcode() == I2->getOpcode() && |
| 280 | "Can not compare special state of different instructions"); |
| 281 | |
| 282 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 283 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
| 284 | (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() || |
| 285 | IgnoreAlignment) && |
| 286 | LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && |
| 287 | LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope(); |
| 288 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 289 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
| 290 | (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() || |
| 291 | IgnoreAlignment) && |
| 292 | SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && |
| 293 | SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope(); |
| 294 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 295 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 296 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
| 297 | return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && |
| 298 | CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
Sanjoy Das | 2de4d0a | 2015-12-14 19:11:35 +0000 | [diff] [blame^] | 299 | CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && |
| 300 | CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 301 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 302 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
Sanjoy Das | 2de4d0a | 2015-12-14 19:11:35 +0000 | [diff] [blame^] | 303 | CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && |
| 304 | CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 305 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) |
| 306 | return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); |
| 307 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) |
| 308 | return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); |
| 309 | if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) |
| 310 | return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && |
| 311 | FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope(); |
| 312 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) |
| 313 | return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 314 | CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 315 | CXI->getSuccessOrdering() == |
| 316 | cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && |
| 317 | CXI->getFailureOrdering() == |
| 318 | cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && |
| 319 | CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope(); |
| 320 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) |
| 321 | return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && |
| 322 | RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && |
| 323 | RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && |
| 324 | RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope(); |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 329 | /// isIdenticalTo - Return true if the specified instruction is exactly |
| 330 | /// identical to the current one. This means that all operands match and any |
| 331 | /// extra information (e.g. load is volatile) agree. |
Chris Lattner | 378b041 | 2008-11-27 08:39:18 +0000 | [diff] [blame] | 332 | bool Instruction::isIdenticalTo(const Instruction *I) const { |
Dan Gohman | 23d2c76 | 2009-08-25 22:24:20 +0000 | [diff] [blame] | 333 | return isIdenticalToWhenDefined(I) && |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 334 | SubclassOptionalData == I->SubclassOptionalData; |
| 335 | } |
| 336 | |
Dan Gohman | 23d2c76 | 2009-08-25 22:24:20 +0000 | [diff] [blame] | 337 | /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 338 | /// ignores the SubclassOptionalData flags, which specify conditions |
| 339 | /// under which the instruction's result is undefined. |
| 340 | bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 341 | if (getOpcode() != I->getOpcode() || |
| 342 | getNumOperands() != I->getNumOperands() || |
| 343 | getType() != I->getType()) |
| 344 | return false; |
| 345 | |
NAKAMURA Takumi | aaffd70 | 2014-06-02 01:35:34 +0000 | [diff] [blame] | 346 | // If both instructions have no operands, they are identical. |
| 347 | if (getNumOperands() == 0 && I->getNumOperands() == 0) |
| 348 | return haveSameSpecialState(this, I); |
| 349 | |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 350 | // We have two instructions of identical opcode and #operands. Check to see |
| 351 | // if all operands are the same. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 352 | if (!std::equal(op_begin(), op_end(), I->op_begin())) |
| 353 | return false; |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 354 | |
Joel Jones | 3d90a9a | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 355 | if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { |
| 356 | const PHINode *otherPHI = cast<PHINode>(I); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 357 | return std::equal(thisPHI->block_begin(), thisPHI->block_end(), |
| 358 | otherPHI->block_begin()); |
Joel Jones | 3d90a9a | 2012-05-10 15:59:41 +0000 | [diff] [blame] | 359 | } |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 360 | |
| 361 | return haveSameSpecialState(this, I); |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 364 | // isSameOperationAs |
Dan Gohman | 17fb0d2 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 365 | // This should be kept in sync with isEquivalentOperation in |
| 366 | // lib/Transforms/IPO/MergeFunctions.cpp. |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 367 | bool Instruction::isSameOperationAs(const Instruction *I, |
| 368 | unsigned flags) const { |
| 369 | bool IgnoreAlignment = flags & CompareIgnoringAlignment; |
| 370 | bool UseScalarTypes = flags & CompareUsingScalarTypes; |
| 371 | |
Dan Gohman | 17fb0d2 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 372 | if (getOpcode() != I->getOpcode() || |
| 373 | getNumOperands() != I->getNumOperands() || |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 374 | (UseScalarTypes ? |
| 375 | getType()->getScalarType() != I->getType()->getScalarType() : |
| 376 | getType() != I->getType())) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 377 | return false; |
| 378 | |
| 379 | // We have two instructions of identical opcode and #operands. Check to see |
| 380 | // if all operands are the same type |
| 381 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Hal Finkel | 74e5225 | 2012-06-28 05:42:26 +0000 | [diff] [blame] | 382 | if (UseScalarTypes ? |
| 383 | getOperand(i)->getType()->getScalarType() != |
| 384 | I->getOperand(i)->getType()->getScalarType() : |
| 385 | getOperand(i)->getType() != I->getOperand(i)->getType()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 386 | return false; |
| 387 | |
Arnaud A. de Grandmaison | 6a90dc4 | 2014-05-27 21:35:46 +0000 | [diff] [blame] | 388 | return haveSameSpecialState(this, I, IgnoreAlignment); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 391 | /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the |
| 392 | /// specified block. Note that PHI nodes are considered to evaluate their |
| 393 | /// operands in the corresponding predecessor block. |
| 394 | bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 395 | for (const Use &U : uses()) { |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 396 | // PHI nodes uses values in the corresponding predecessor block. For other |
| 397 | // 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] | 398 | const Instruction *I = cast<Instruction>(U.getUser()); |
| 399 | const PHINode *PN = dyn_cast<PHINode>(I); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 400 | if (!PN) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 401 | if (I->getParent() != BB) |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 402 | return true; |
| 403 | continue; |
| 404 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 405 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 406 | if (PN->getIncomingBlock(U) != BB) |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 407 | return true; |
| 408 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 409 | return false; |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 412 | /// mayReadFromMemory - Return true if this instruction may read memory. |
| 413 | /// |
| 414 | bool Instruction::mayReadFromMemory() const { |
| 415 | switch (getOpcode()) { |
| 416 | default: return false; |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 417 | case Instruction::VAArg: |
Chris Lattner | 954907a | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 418 | case Instruction::Load: |
Eli Friedman | 89b694b | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 419 | case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 420 | case Instruction::AtomicCmpXchg: |
| 421 | case Instruction::AtomicRMW: |
David Majnemer | 880c2cb | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 422 | case Instruction::CatchPad: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 423 | case Instruction::CatchRet: |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 424 | return true; |
| 425 | case Instruction::Call: |
| 426 | return !cast<CallInst>(this)->doesNotAccessMemory(); |
| 427 | case Instruction::Invoke: |
| 428 | return !cast<InvokeInst>(this)->doesNotAccessMemory(); |
Chris Lattner | 954907a | 2008-05-08 21:58:49 +0000 | [diff] [blame] | 429 | case Instruction::Store: |
Eli Friedman | b9d5a63 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 430 | return !cast<StoreInst>(this)->isUnordered(); |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
Chris Lattner | 8ec6e8a | 2008-04-20 22:11:30 +0000 | [diff] [blame] | 433 | |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 434 | /// mayWriteToMemory - Return true if this instruction may modify memory. |
| 435 | /// |
| 436 | bool Instruction::mayWriteToMemory() const { |
| 437 | switch (getOpcode()) { |
| 438 | default: return false; |
Eli Friedman | 89b694b | 2011-07-27 01:08:30 +0000 | [diff] [blame] | 439 | case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 440 | case Instruction::Store: |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 441 | case Instruction::VAArg: |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 442 | case Instruction::AtomicCmpXchg: |
| 443 | case Instruction::AtomicRMW: |
David Majnemer | 880c2cb | 2015-09-10 18:50:09 +0000 | [diff] [blame] | 444 | case Instruction::CatchPad: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 445 | case Instruction::CatchRet: |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 446 | return true; |
| 447 | case Instruction::Call: |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 448 | return !cast<CallInst>(this)->onlyReadsMemory(); |
Chris Lattner | 8462711 | 2008-05-08 17:16:51 +0000 | [diff] [blame] | 449 | case Instruction::Invoke: |
| 450 | return !cast<InvokeInst>(this)->onlyReadsMemory(); |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 451 | case Instruction::Load: |
Eli Friedman | b9d5a63 | 2011-08-15 21:00:18 +0000 | [diff] [blame] | 452 | return !cast<LoadInst>(this)->isUnordered(); |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 455 | |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 456 | bool Instruction::isAtomic() const { |
| 457 | switch (getOpcode()) { |
| 458 | default: |
| 459 | return false; |
| 460 | case Instruction::AtomicCmpXchg: |
| 461 | case Instruction::AtomicRMW: |
| 462 | case Instruction::Fence: |
| 463 | return true; |
| 464 | case Instruction::Load: |
| 465 | return cast<LoadInst>(this)->getOrdering() != NotAtomic; |
| 466 | case Instruction::Store: |
| 467 | return cast<StoreInst>(this)->getOrdering() != NotAtomic; |
| 468 | } |
| 469 | } |
| 470 | |
Duncan Sands | 1efabaa | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 471 | bool Instruction::mayThrow() const { |
| 472 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 473 | return !CI->doesNotThrow(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 474 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(this)) |
| 475 | return CRI->unwindsToCaller(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 476 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this)) |
| 477 | return CatchSwitch->unwindsToCaller(); |
Bill Wendling | d7c6c91 | 2011-08-16 21:15:50 +0000 | [diff] [blame] | 478 | return isa<ResumeInst>(this); |
Duncan Sands | 1efabaa | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Nadav Rotem | 0186347 | 2013-02-19 20:02:09 +0000 | [diff] [blame] | 481 | bool Instruction::mayReturn() const { |
| 482 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 483 | return !CI->doesNotReturn(); |
| 484 | return true; |
| 485 | } |
| 486 | |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 487 | /// isAssociative - Return true if the instruction is associative: |
| 488 | /// |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 489 | /// Associative operators satisfy: x op (y op z) === (x op y) op z |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 490 | /// |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 491 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 492 | /// |
Duncan Sands | 70db5e7 | 2010-12-20 13:10:23 +0000 | [diff] [blame] | 493 | bool Instruction::isAssociative(unsigned Opcode) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 494 | return Opcode == And || Opcode == Or || Opcode == Xor || |
| 495 | Opcode == Add || Opcode == Mul; |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Shuxin Yang | 01ab5d7 | 2012-11-29 01:47:31 +0000 | [diff] [blame] | 498 | bool Instruction::isAssociative() const { |
| 499 | unsigned Opcode = getOpcode(); |
| 500 | if (isAssociative(Opcode)) |
| 501 | return true; |
| 502 | |
| 503 | switch (Opcode) { |
| 504 | case FMul: |
| 505 | case FAdd: |
| 506 | return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); |
| 507 | default: |
| 508 | return false; |
| 509 | } |
| 510 | } |
| 511 | |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 512 | /// isCommutative - Return true if the instruction is commutative: |
| 513 | /// |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 514 | /// Commutative operators satisfy: (x op y) === (y op x) |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 515 | /// |
| 516 | /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when |
| 517 | /// applied to any type. |
| 518 | /// |
| 519 | bool Instruction::isCommutative(unsigned op) { |
| 520 | switch (op) { |
| 521 | case Add: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 522 | case FAdd: |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 523 | case Mul: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 524 | case FMul: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 525 | case And: |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 526 | case Or: |
| 527 | case Xor: |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 528 | return true; |
| 529 | default: |
| 530 | return false; |
| 531 | } |
| 532 | } |
Tanya Lattner | a93c7ae | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 533 | |
Duncan Sands | d7aeefe | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 534 | /// isIdempotent - Return true if the instruction is idempotent: |
| 535 | /// |
| 536 | /// Idempotent operators satisfy: x op x === x |
| 537 | /// |
| 538 | /// In LLVM, the And and Or operators are idempotent. |
| 539 | /// |
| 540 | bool Instruction::isIdempotent(unsigned Opcode) { |
| 541 | return Opcode == And || Opcode == Or; |
| 542 | } |
| 543 | |
| 544 | /// isNilpotent - Return true if the instruction is nilpotent: |
| 545 | /// |
| 546 | /// Nilpotent operators satisfy: x op x === Id, |
| 547 | /// |
| 548 | /// where Id is the identity for the operator, i.e. a constant such that |
| 549 | /// x op Id === x and Id op x === x for all x. |
| 550 | /// |
| 551 | /// In LLVM, the Xor operator is nilpotent. |
| 552 | /// |
| 553 | bool Instruction::isNilpotent(unsigned Opcode) { |
| 554 | return Opcode == Xor; |
| 555 | } |
| 556 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 557 | Instruction *Instruction::cloneImpl() const { |
| 558 | llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); |
| 559 | } |
| 560 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 561 | Instruction *Instruction::clone() const { |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 562 | Instruction *New = nullptr; |
| 563 | switch (getOpcode()) { |
| 564 | default: |
| 565 | llvm_unreachable("Unhandled Opcode."); |
| 566 | #define HANDLE_INST(num, opc, clas) \ |
| 567 | case Instruction::opc: \ |
| 568 | New = cast<clas>(this)->cloneImpl(); \ |
| 569 | break; |
| 570 | #include "llvm/IR/Instruction.def" |
| 571 | #undef HANDLE_INST |
| 572 | } |
| 573 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 574 | New->SubclassOptionalData = SubclassOptionalData; |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 575 | if (!hasMetadata()) |
| 576 | return New; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 578 | // Otherwise, enumerate and copy over metadata from the old instruction to the |
| 579 | // new one. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 580 | SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; |
Chris Lattner | 8f29491 | 2011-07-14 18:57:51 +0000 | [diff] [blame] | 581 | getAllMetadataOtherThanDebugLoc(TheMDs); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 582 | for (const auto &MD : TheMDs) |
| 583 | New->setMetadata(MD.first, MD.second); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 8f29491 | 2011-07-14 18:57:51 +0000 | [diff] [blame] | 585 | New->setDebugLoc(getDebugLoc()); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 586 | return New; |
| 587 | } |