LLVM API Documentation

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