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