LLVM  mainline
Instruction.cpp
Go to the documentation of this file.
00001 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Instruction class for the IR library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/Instruction.h"
00015 #include "llvm/IR/CallSite.h"
00016 #include "llvm/IR/Constants.h"
00017 #include "llvm/IR/Instructions.h"
00018 #include "llvm/IR/Module.h"
00019 #include "llvm/IR/Operator.h"
00020 #include "llvm/IR/Type.h"
00021 using namespace llvm;
00022 
00023 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
00024                          Instruction *InsertBefore)
00025   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
00026 
00027   // If requested, insert this instruction into a basic block...
00028   if (InsertBefore) {
00029     BasicBlock *BB = InsertBefore->getParent();
00030     assert(BB && "Instruction to insert before is not in a basic block!");
00031     BB->getInstList().insert(InsertBefore->getIterator(), this);
00032   }
00033 }
00034 
00035 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
00036                          BasicBlock *InsertAtEnd)
00037   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
00038 
00039   // append this instruction into the basic block
00040   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
00041   InsertAtEnd->getInstList().push_back(this);
00042 }
00043 
00044 
00045 // Out of line virtual method, so the vtable, etc has a home.
00046 Instruction::~Instruction() {
00047   assert(!Parent && "Instruction still linked in the program!");
00048   if (hasMetadataHashEntry())
00049     clearMetadataHashEntries();
00050 }
00051 
00052 
00053 void Instruction::setParent(BasicBlock *P) {
00054   Parent = P;
00055 }
00056 
00057 const Module *Instruction::getModule() const {
00058   return getParent()->getModule();
00059 }
00060 
00061 Module *Instruction::getModule() {
00062   return getParent()->getModule();
00063 }
00064 
00065 Function *Instruction::getFunction() { return getParent()->getParent(); }
00066 
00067 const Function *Instruction::getFunction() const {
00068   return getParent()->getParent();
00069 }
00070 
00071 void Instruction::removeFromParent() {
00072   getParent()->getInstList().remove(getIterator());
00073 }
00074 
00075 iplist<Instruction>::iterator Instruction::eraseFromParent() {
00076   return getParent()->getInstList().erase(getIterator());
00077 }
00078 
00079 /// Insert an unlinked instruction into a basic block immediately before the
00080 /// specified instruction.
00081 void Instruction::insertBefore(Instruction *InsertPos) {
00082   InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
00083 }
00084 
00085 /// Insert an unlinked instruction into a basic block immediately after the
00086 /// specified instruction.
00087 void Instruction::insertAfter(Instruction *InsertPos) {
00088   InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
00089                                                     this);
00090 }
00091 
00092 /// Unlink this instruction from its current basic block and insert it into the
00093 /// basic block that MovePos lives in, right before MovePos.
00094 void Instruction::moveBefore(Instruction *MovePos) {
00095   MovePos->getParent()->getInstList().splice(
00096       MovePos->getIterator(), getParent()->getInstList(), getIterator());
00097 }
00098 
00099 /// Set or clear the unsafe-algebra flag on this instruction, which must be an
00100 /// operator which supports this flag. See LangRef.html for the meaning of this
00101 /// flag.
00102 void Instruction::setHasUnsafeAlgebra(bool B) {
00103   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00104   cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
00105 }
00106 
00107 /// Set or clear the NoNaNs flag on this instruction, which must be an operator
00108 /// which supports this flag. See LangRef.html for the meaning of this flag.
00109 void Instruction::setHasNoNaNs(bool B) {
00110   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00111   cast<FPMathOperator>(this)->setHasNoNaNs(B);
00112 }
00113 
00114 /// Set or clear the no-infs flag on this instruction, which must be an operator
00115 /// which supports this flag. See LangRef.html for the meaning of this flag.
00116 void Instruction::setHasNoInfs(bool B) {
00117   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00118   cast<FPMathOperator>(this)->setHasNoInfs(B);
00119 }
00120 
00121 /// Set or clear the no-signed-zeros flag on this instruction, which must be an
00122 /// operator which supports this flag. See LangRef.html for the meaning of this
00123 /// flag.
00124 void Instruction::setHasNoSignedZeros(bool B) {
00125   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00126   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
00127 }
00128 
00129 /// Set or clear the allow-reciprocal flag on this instruction, which must be an
00130 /// operator which supports this flag. See LangRef.html for the meaning of this
00131 /// flag.
00132 void Instruction::setHasAllowReciprocal(bool B) {
00133   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00134   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
00135 }
00136 
00137 /// Convenience function for setting all the fast-math flags on this
00138 /// instruction, which must be an operator which supports these flags. See
00139 /// LangRef.html for the meaning of these flats.
00140 void Instruction::setFastMathFlags(FastMathFlags FMF) {
00141   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
00142   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
00143 }
00144 
00145 void Instruction::copyFastMathFlags(FastMathFlags FMF) {
00146   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
00147   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
00148 }
00149 
00150 /// Determine whether the unsafe-algebra flag is set.
00151 bool Instruction::hasUnsafeAlgebra() const {
00152   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00153   return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
00154 }
00155 
00156 /// Determine whether the no-NaNs flag is set.
00157 bool Instruction::hasNoNaNs() const {
00158   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00159   return cast<FPMathOperator>(this)->hasNoNaNs();
00160 }
00161 
00162 /// Determine whether the no-infs flag is set.
00163 bool Instruction::hasNoInfs() const {
00164   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00165   return cast<FPMathOperator>(this)->hasNoInfs();
00166 }
00167 
00168 /// Determine whether the no-signed-zeros flag is set.
00169 bool Instruction::hasNoSignedZeros() const {
00170   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00171   return cast<FPMathOperator>(this)->hasNoSignedZeros();
00172 }
00173 
00174 /// Determine whether the allow-reciprocal flag is set.
00175 bool Instruction::hasAllowReciprocal() const {
00176   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00177   return cast<FPMathOperator>(this)->hasAllowReciprocal();
00178 }
00179 
00180 /// Convenience function for getting all the fast-math flags, which must be an
00181 /// operator which supports these flags. See LangRef.html for the meaning of
00182 /// these flags.
00183 FastMathFlags Instruction::getFastMathFlags() const {
00184   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
00185   return cast<FPMathOperator>(this)->getFastMathFlags();
00186 }
00187 
00188 /// Copy I's fast-math flags
00189 void Instruction::copyFastMathFlags(const Instruction *I) {
00190   copyFastMathFlags(I->getFastMathFlags());
00191 }
00192 
00193 
00194 const char *Instruction::getOpcodeName(unsigned OpCode) {
00195   switch (OpCode) {
00196   // Terminators
00197   case Ret:    return "ret";
00198   case Br:     return "br";
00199   case Switch: return "switch";
00200   case IndirectBr: return "indirectbr";
00201   case Invoke: return "invoke";
00202   case Resume: return "resume";
00203   case Unreachable: return "unreachable";
00204   case CleanupRet: return "cleanupret";
00205   case CatchRet: return "catchret";
00206   case CatchPad: return "catchpad";
00207   case CatchSwitch: return "catchswitch";
00208 
00209   // Standard binary operators...
00210   case Add: return "add";
00211   case FAdd: return "fadd";
00212   case Sub: return "sub";
00213   case FSub: return "fsub";
00214   case Mul: return "mul";
00215   case FMul: return "fmul";
00216   case UDiv: return "udiv";
00217   case SDiv: return "sdiv";
00218   case FDiv: return "fdiv";
00219   case URem: return "urem";
00220   case SRem: return "srem";
00221   case FRem: return "frem";
00222 
00223   // Logical operators...
00224   case And: return "and";
00225   case Or : return "or";
00226   case Xor: return "xor";
00227 
00228   // Memory instructions...
00229   case Alloca:        return "alloca";
00230   case Load:          return "load";
00231   case Store:         return "store";
00232   case AtomicCmpXchg: return "cmpxchg";
00233   case AtomicRMW:     return "atomicrmw";
00234   case Fence:         return "fence";
00235   case GetElementPtr: return "getelementptr";
00236 
00237   // Convert instructions...
00238   case Trunc:         return "trunc";
00239   case ZExt:          return "zext";
00240   case SExt:          return "sext";
00241   case FPTrunc:       return "fptrunc";
00242   case FPExt:         return "fpext";
00243   case FPToUI:        return "fptoui";
00244   case FPToSI:        return "fptosi";
00245   case UIToFP:        return "uitofp";
00246   case SIToFP:        return "sitofp";
00247   case IntToPtr:      return "inttoptr";
00248   case PtrToInt:      return "ptrtoint";
00249   case BitCast:       return "bitcast";
00250   case AddrSpaceCast: return "addrspacecast";
00251 
00252   // Other instructions...
00253   case ICmp:           return "icmp";
00254   case FCmp:           return "fcmp";
00255   case PHI:            return "phi";
00256   case Select:         return "select";
00257   case Call:           return "call";
00258   case Shl:            return "shl";
00259   case LShr:           return "lshr";
00260   case AShr:           return "ashr";
00261   case VAArg:          return "va_arg";
00262   case ExtractElement: return "extractelement";
00263   case InsertElement:  return "insertelement";
00264   case ShuffleVector:  return "shufflevector";
00265   case ExtractValue:   return "extractvalue";
00266   case InsertValue:    return "insertvalue";
00267   case LandingPad:     return "landingpad";
00268   case CleanupPad:     return "cleanuppad";
00269 
00270   default: return "<Invalid operator> ";
00271   }
00272 }
00273 
00274 /// Return true if both instructions have the same special state
00275 /// This must be kept in sync with lib/Transforms/IPO/MergeFunctions.cpp.
00276 static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
00277                                  bool IgnoreAlignment = false) {
00278   assert(I1->getOpcode() == I2->getOpcode() &&
00279          "Can not compare special state of different instructions");
00280 
00281   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
00282     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
00283            (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
00284             IgnoreAlignment) &&
00285            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
00286            LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
00287   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
00288     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
00289            (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
00290             IgnoreAlignment) &&
00291            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
00292            SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
00293   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
00294     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
00295   if (const CallInst *CI = dyn_cast<CallInst>(I1))
00296     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
00297            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
00298            CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
00299            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
00300   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
00301     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
00302            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
00303            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
00304   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
00305     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
00306   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
00307     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
00308   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
00309     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
00310            FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
00311   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
00312     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
00313            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
00314            CXI->getSuccessOrdering() ==
00315                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
00316            CXI->getFailureOrdering() ==
00317                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
00318            CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
00319   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
00320     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
00321            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
00322            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
00323            RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
00324 
00325   return true;
00326 }
00327 
00328 /// isIdenticalTo - Return true if the specified instruction is exactly
00329 /// identical to the current one.  This means that all operands match and any
00330 /// extra information (e.g. load is volatile) agree.
00331 bool Instruction::isIdenticalTo(const Instruction *I) const {
00332   return isIdenticalToWhenDefined(I) &&
00333          SubclassOptionalData == I->SubclassOptionalData;
00334 }
00335 
00336 /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
00337 /// ignores the SubclassOptionalData flags, which specify conditions
00338 /// under which the instruction's result is undefined.
00339 bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
00340   if (getOpcode() != I->getOpcode() ||
00341       getNumOperands() != I->getNumOperands() ||
00342       getType() != I->getType())
00343     return false;
00344 
00345   // If both instructions have no operands, they are identical.
00346   if (getNumOperands() == 0 && I->getNumOperands() == 0)
00347     return haveSameSpecialState(this, I);
00348 
00349   // We have two instructions of identical opcode and #operands.  Check to see
00350   // if all operands are the same.
00351   if (!std::equal(op_begin(), op_end(), I->op_begin()))
00352     return false;
00353 
00354   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
00355     const PHINode *otherPHI = cast<PHINode>(I);
00356     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
00357                       otherPHI->block_begin());
00358   }
00359 
00360   return haveSameSpecialState(this, I);
00361 }
00362 
00363 // isSameOperationAs
00364 // This should be kept in sync with isEquivalentOperation in
00365 // lib/Transforms/IPO/MergeFunctions.cpp.
00366 bool Instruction::isSameOperationAs(const Instruction *I,
00367                                     unsigned flags) const {
00368   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
00369   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
00370 
00371   if (getOpcode() != I->getOpcode() ||
00372       getNumOperands() != I->getNumOperands() ||
00373       (UseScalarTypes ?
00374        getType()->getScalarType() != I->getType()->getScalarType() :
00375        getType() != I->getType()))
00376     return false;
00377 
00378   // We have two instructions of identical opcode and #operands.  Check to see
00379   // if all operands are the same type
00380   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00381     if (UseScalarTypes ?
00382         getOperand(i)->getType()->getScalarType() !=
00383           I->getOperand(i)->getType()->getScalarType() :
00384         getOperand(i)->getType() != I->getOperand(i)->getType())
00385       return false;
00386 
00387   return haveSameSpecialState(this, I, IgnoreAlignment);
00388 }
00389 
00390 /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
00391 /// specified block.  Note that PHI nodes are considered to evaluate their
00392 /// operands in the corresponding predecessor block.
00393 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
00394   for (const Use &U : uses()) {
00395     // PHI nodes uses values in the corresponding predecessor block.  For other
00396     // instructions, just check to see whether the parent of the use matches up.
00397     const Instruction *I = cast<Instruction>(U.getUser());
00398     const PHINode *PN = dyn_cast<PHINode>(I);
00399     if (!PN) {
00400       if (I->getParent() != BB)
00401         return true;
00402       continue;
00403     }
00404 
00405     if (PN->getIncomingBlock(U) != BB)
00406       return true;
00407   }
00408   return false;
00409 }
00410 
00411 /// mayReadFromMemory - Return true if this instruction may read memory.
00412 ///
00413 bool Instruction::mayReadFromMemory() const {
00414   switch (getOpcode()) {
00415   default: return false;
00416   case Instruction::VAArg:
00417   case Instruction::Load:
00418   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
00419   case Instruction::AtomicCmpXchg:
00420   case Instruction::AtomicRMW:
00421   case Instruction::CatchPad:
00422   case Instruction::CatchRet:
00423     return true;
00424   case Instruction::Call:
00425     return !cast<CallInst>(this)->doesNotAccessMemory();
00426   case Instruction::Invoke:
00427     return !cast<InvokeInst>(this)->doesNotAccessMemory();
00428   case Instruction::Store:
00429     return !cast<StoreInst>(this)->isUnordered();
00430   }
00431 }
00432 
00433 /// mayWriteToMemory - Return true if this instruction may modify memory.
00434 ///
00435 bool Instruction::mayWriteToMemory() const {
00436   switch (getOpcode()) {
00437   default: return false;
00438   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
00439   case Instruction::Store:
00440   case Instruction::VAArg:
00441   case Instruction::AtomicCmpXchg:
00442   case Instruction::AtomicRMW:
00443   case Instruction::CatchPad:
00444   case Instruction::CatchRet:
00445     return true;
00446   case Instruction::Call:
00447     return !cast<CallInst>(this)->onlyReadsMemory();
00448   case Instruction::Invoke:
00449     return !cast<InvokeInst>(this)->onlyReadsMemory();
00450   case Instruction::Load:
00451     return !cast<LoadInst>(this)->isUnordered();
00452   }
00453 }
00454 
00455 bool Instruction::isAtomic() const {
00456   switch (getOpcode()) {
00457   default:
00458     return false;
00459   case Instruction::AtomicCmpXchg:
00460   case Instruction::AtomicRMW:
00461   case Instruction::Fence:
00462     return true;
00463   case Instruction::Load:
00464     return cast<LoadInst>(this)->getOrdering() != NotAtomic;
00465   case Instruction::Store:
00466     return cast<StoreInst>(this)->getOrdering() != NotAtomic;
00467   }
00468 }
00469 
00470 bool Instruction::mayThrow() const {
00471   if (const CallInst *CI = dyn_cast<CallInst>(this))
00472     return !CI->doesNotThrow();
00473   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
00474     return CRI->unwindsToCaller();
00475   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
00476     return CatchSwitch->unwindsToCaller();
00477   return isa<ResumeInst>(this);
00478 }
00479 
00480 bool Instruction::mayReturn() const {
00481   if (const CallInst *CI = dyn_cast<CallInst>(this))
00482     return !CI->doesNotReturn();
00483   return true;
00484 }
00485 
00486 /// isAssociative - Return true if the instruction is associative:
00487 ///
00488 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
00489 ///
00490 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
00491 ///
00492 bool Instruction::isAssociative(unsigned Opcode) {
00493   return Opcode == And || Opcode == Or || Opcode == Xor ||
00494          Opcode == Add || Opcode == Mul;
00495 }
00496 
00497 bool Instruction::isAssociative() const {
00498   unsigned Opcode = getOpcode();
00499   if (isAssociative(Opcode))
00500     return true;
00501 
00502   switch (Opcode) {
00503   case FMul:
00504   case FAdd:
00505     return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
00506   default:
00507     return false;
00508   }
00509 }
00510 
00511 /// isCommutative - Return true if the instruction is commutative:
00512 ///
00513 ///   Commutative operators satisfy: (x op y) === (y op x)
00514 ///
00515 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00516 /// applied to any type.
00517 ///
00518 bool Instruction::isCommutative(unsigned op) {
00519   switch (op) {
00520   case Add:
00521   case FAdd:
00522   case Mul:
00523   case FMul:
00524   case And:
00525   case Or:
00526   case Xor:
00527     return true;
00528   default:
00529     return false;
00530   }
00531 }
00532 
00533 /// isIdempotent - Return true if the instruction is idempotent:
00534 ///
00535 ///   Idempotent operators satisfy:  x op x === x
00536 ///
00537 /// In LLVM, the And and Or operators are idempotent.
00538 ///
00539 bool Instruction::isIdempotent(unsigned Opcode) {
00540   return Opcode == And || Opcode == Or;
00541 }
00542 
00543 /// isNilpotent - Return true if the instruction is nilpotent:
00544 ///
00545 ///   Nilpotent operators satisfy:  x op x === Id,
00546 ///
00547 ///   where Id is the identity for the operator, i.e. a constant such that
00548 ///     x op Id === x and Id op x === x for all x.
00549 ///
00550 /// In LLVM, the Xor operator is nilpotent.
00551 ///
00552 bool Instruction::isNilpotent(unsigned Opcode) {
00553   return Opcode == Xor;
00554 }
00555 
00556 Instruction *Instruction::cloneImpl() const {
00557   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
00558 }
00559 
00560 Instruction *Instruction::clone() const {
00561   Instruction *New = nullptr;
00562   switch (getOpcode()) {
00563   default:
00564     llvm_unreachable("Unhandled Opcode.");
00565 #define HANDLE_INST(num, opc, clas)                                            \
00566   case Instruction::opc:                                                       \
00567     New = cast<clas>(this)->cloneImpl();                                       \
00568     break;
00569 #include "llvm/IR/Instruction.def"
00570 #undef HANDLE_INST
00571   }
00572 
00573   New->SubclassOptionalData = SubclassOptionalData;
00574   if (!hasMetadata())
00575     return New;
00576 
00577   // Otherwise, enumerate and copy over metadata from the old instruction to the
00578   // new one.
00579   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
00580   getAllMetadataOtherThanDebugLoc(TheMDs);
00581   for (const auto &MD : TheMDs)
00582     New->setMetadata(MD.first, MD.second);
00583 
00584   New->setDebugLoc(getDebugLoc());
00585   return New;
00586 }