1//===-- Instruction.cpp - Implement the Instruction class -----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the Instruction class for the IR library. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/IR/Instruction.h" 15#include "llvm/IR/CallSite.h" 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" 21using namespace llvm; 22 23Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, 24 Instruction *InsertBefore) 25 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { 26 27 // If requested, insert this instruction into a basic block... 28 if (InsertBefore) { 29 BasicBlock *BB = InsertBefore->getParent(); 30 assert(BB && "Instruction to insert before is not in a basic block!"); 31 BB->getInstList().insert(InsertBefore->getIterator(), this); 32 } 33} 34 35Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, 36 BasicBlock *InsertAtEnd) 37 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { 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); 42} 43 44 45// Out of line virtual method, so the vtable, etc has a home. 46Instruction::~Instruction() { 47 assert(!Parent && "Instruction still linked in the program!"); 48 if (hasMetadataHashEntry()) 49 clearMetadataHashEntries(); 50} 51 52 53void Instruction::setParent(BasicBlock *P) { 54 Parent = P; 55} 56 57const Module *Instruction::getModule() const { 58 return getParent()->getModule(); 59} 60 61Module *Instruction::getModule() { 62 return getParent()->getModule(); 63} 64 65Function *Instruction::getFunction() { return getParent()->getParent(); } 66 67const Function *Instruction::getFunction() const { 68 return getParent()->getParent(); 69} 70 71void Instruction::removeFromParent() { 72 getParent()->getInstList().remove(getIterator()); 73} 74 75iplist<Instruction>::iterator Instruction::eraseFromParent() { 76 return getParent()->getInstList().erase(getIterator()); 77} 78 79/// Insert an unlinked instruction into a basic block immediately before the 80/// specified instruction. 81void Instruction::insertBefore(Instruction *InsertPos) { 82 InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this); 83} 84 85/// Insert an unlinked instruction into a basic block immediately after the 86/// specified instruction. 87void Instruction::insertAfter(Instruction *InsertPos) { 88 InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(), 89 this); 90} 91 92/// Unlink this instruction from its current basic block and insert it into the 93/// basic block that MovePos lives in, right before MovePos. 94void Instruction::moveBefore(Instruction *MovePos) { 95 MovePos->getParent()->getInstList().splice( 96 MovePos->getIterator(), getParent()->getInstList(), getIterator()); 97} 98 99void Instruction::setHasNoUnsignedWrap(bool b) { 100 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); 101} 102 103void Instruction::setHasNoSignedWrap(bool b) { 104 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); 105} 106 107void Instruction::setIsExact(bool b) { 108 cast<PossiblyExactOperator>(this)->setIsExact(b); 109} 110 111bool Instruction::hasNoUnsignedWrap() const { 112 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); 113} 114 115bool Instruction::hasNoSignedWrap() const { 116 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); 117} 118 119bool Instruction::isExact() const { 120 return cast<PossiblyExactOperator>(this)->isExact(); 121} 122 123/// Set or clear the unsafe-algebra flag on this instruction, which must be an 124/// operator which supports this flag. See LangRef.html for the meaning of this 125/// flag. 126void Instruction::setHasUnsafeAlgebra(bool B) { 127 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 128 cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B); 129} 130 131/// Set or clear the NoNaNs flag on this instruction, which must be an operator 132/// which supports this flag. See LangRef.html for the meaning of this flag. 133void Instruction::setHasNoNaNs(bool B) { 134 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 135 cast<FPMathOperator>(this)->setHasNoNaNs(B); 136} 137 138/// Set or clear the no-infs flag on this instruction, which must be an operator 139/// which supports this flag. See LangRef.html for the meaning of this flag. 140void Instruction::setHasNoInfs(bool B) { 141 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 142 cast<FPMathOperator>(this)->setHasNoInfs(B); 143} 144 145/// Set or clear the no-signed-zeros flag on this instruction, which must be an 146/// operator which supports this flag. See LangRef.html for the meaning of this 147/// flag. 148void Instruction::setHasNoSignedZeros(bool B) { 149 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 150 cast<FPMathOperator>(this)->setHasNoSignedZeros(B); 151} 152 153/// Set or clear the allow-reciprocal flag on this instruction, which must be an 154/// operator which supports this flag. See LangRef.html for the meaning of this 155/// flag. 156void Instruction::setHasAllowReciprocal(bool B) { 157 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 158 cast<FPMathOperator>(this)->setHasAllowReciprocal(B); 159} 160 161/// Convenience function for setting all the fast-math flags on this 162/// instruction, which must be an operator which supports these flags. See 163/// LangRef.html for the meaning of these flats. 164void Instruction::setFastMathFlags(FastMathFlags FMF) { 165 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 166 cast<FPMathOperator>(this)->setFastMathFlags(FMF); 167} 168 169void Instruction::copyFastMathFlags(FastMathFlags FMF) { 170 assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); 171 cast<FPMathOperator>(this)->copyFastMathFlags(FMF); 172} 173 174/// Determine whether the unsafe-algebra flag is set. 175bool Instruction::hasUnsafeAlgebra() const { 176 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 177 return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); 178} 179 180/// Determine whether the no-NaNs flag is set. 181bool Instruction::hasNoNaNs() const { 182 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 183 return cast<FPMathOperator>(this)->hasNoNaNs(); 184} 185 186/// Determine whether the no-infs flag is set. 187bool Instruction::hasNoInfs() const { 188 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 189 return cast<FPMathOperator>(this)->hasNoInfs(); 190} 191 192/// Determine whether the no-signed-zeros flag is set. 193bool Instruction::hasNoSignedZeros() const { 194 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 195 return cast<FPMathOperator>(this)->hasNoSignedZeros(); 196} 197 198/// Determine whether the allow-reciprocal flag is set. 199bool Instruction::hasAllowReciprocal() const { 200 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 201 return cast<FPMathOperator>(this)->hasAllowReciprocal(); 202} 203 204/// Convenience function for getting all the fast-math flags, which must be an 205/// operator which supports these flags. See LangRef.html for the meaning of 206/// these flags. 207FastMathFlags Instruction::getFastMathFlags() const { 208 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 209 return cast<FPMathOperator>(this)->getFastMathFlags(); 210} 211 212/// Copy I's fast-math flags 213void Instruction::copyFastMathFlags(const Instruction *I) { 214 copyFastMathFlags(I->getFastMathFlags()); 215} 216 217void Instruction::copyIRFlags(const Value *V) { 218 // Copy the wrapping flags. 219 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 220 if (isa<OverflowingBinaryOperator>(this)) { 221 setHasNoSignedWrap(OB->hasNoSignedWrap()); 222 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); 223 } 224 } 225 226 // Copy the exact flag. 227 if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 228 if (isa<PossiblyExactOperator>(this)) 229 setIsExact(PE->isExact()); 230 231 // Copy the fast-math flags. 232 if (auto *FP = dyn_cast<FPMathOperator>(V)) 233 if (isa<FPMathOperator>(this)) 234 copyFastMathFlags(FP->getFastMathFlags()); 235} 236 237void Instruction::andIRFlags(const Value *V) { 238 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 239 if (isa<OverflowingBinaryOperator>(this)) { 240 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); 241 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); 242 } 243 } 244 245 if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 246 if (isa<PossiblyExactOperator>(this)) 247 setIsExact(isExact() & PE->isExact()); 248 249 if (auto *FP = dyn_cast<FPMathOperator>(V)) { 250 if (isa<FPMathOperator>(this)) { 251 FastMathFlags FM = getFastMathFlags(); 252 FM &= FP->getFastMathFlags(); 253 copyFastMathFlags(FM); 254 } 255 } 256} 257 258const char *Instruction::getOpcodeName(unsigned OpCode) { 259 switch (OpCode) { 260 // Terminators 261 case Ret: return "ret"; 262 case Br: return "br"; 263 case Switch: return "switch"; 264 case IndirectBr: return "indirectbr"; 265 case Invoke: return "invoke"; 266 case Resume: return "resume"; 267 case Unreachable: return "unreachable"; 268 case CleanupRet: return "cleanupret"; 269 case CatchRet: return "catchret"; 270 case CatchPad: return "catchpad"; 271 case CatchSwitch: return "catchswitch"; 272 273 // Standard binary operators... 274 case Add: return "add"; 275 case FAdd: return "fadd"; 276 case Sub: return "sub"; 277 case FSub: return "fsub"; 278 case Mul: return "mul"; 279 case FMul: return "fmul"; 280 case UDiv: return "udiv"; 281 case SDiv: return "sdiv"; 282 case FDiv: return "fdiv"; 283 case URem: return "urem"; 284 case SRem: return "srem"; 285 case FRem: return "frem"; 286 287 // Logical operators... 288 case And: return "and"; 289 case Or : return "or"; 290 case Xor: return "xor"; 291 292 // Memory instructions... 293 case Alloca: return "alloca"; 294 case Load: return "load"; 295 case Store: return "store"; 296 case AtomicCmpXchg: return "cmpxchg"; 297 case AtomicRMW: return "atomicrmw"; 298 case Fence: return "fence"; 299 case GetElementPtr: return "getelementptr"; 300 301 // Convert instructions... 302 case Trunc: return "trunc"; 303 case ZExt: return "zext"; 304 case SExt: return "sext"; 305 case FPTrunc: return "fptrunc"; 306 case FPExt: return "fpext"; 307 case FPToUI: return "fptoui"; 308 case FPToSI: return "fptosi"; 309 case UIToFP: return "uitofp"; 310 case SIToFP: return "sitofp"; 311 case IntToPtr: return "inttoptr"; 312 case PtrToInt: return "ptrtoint"; 313 case BitCast: return "bitcast"; 314 case AddrSpaceCast: return "addrspacecast"; 315 316 // Other instructions... 317 case ICmp: return "icmp"; 318 case FCmp: return "fcmp"; 319 case PHI: return "phi"; 320 case Select: return "select"; 321 case Call: return "call"; 322 case Shl: return "shl"; 323 case LShr: return "lshr"; 324 case AShr: return "ashr"; 325 case VAArg: return "va_arg"; 326 case ExtractElement: return "extractelement"; 327 case InsertElement: return "insertelement"; 328 case ShuffleVector: return "shufflevector"; 329 case ExtractValue: return "extractvalue"; 330 case InsertValue: return "insertvalue"; 331 case LandingPad: return "landingpad"; 332 case CleanupPad: return "cleanuppad"; 333 334 default: return "<Invalid operator> "; 335 } 336} 337 338/// Return true if both instructions have the same special state This must be 339/// kept in sync with FunctionComparator::cmpOperations in 340/// lib/Transforms/IPO/MergeFunctions.cpp. 341static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, 342 bool IgnoreAlignment = false) { 343 assert(I1->getOpcode() == I2->getOpcode() && 344 "Can not compare special state of different instructions"); 345 346 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1)) 347 return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() && 348 (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() || 349 IgnoreAlignment); 350 if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) 351 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && 352 (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() || 353 IgnoreAlignment) && 354 LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && 355 LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope(); 356 if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) 357 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && 358 (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() || 359 IgnoreAlignment) && 360 SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && 361 SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope(); 362 if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) 363 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); 364 if (const CallInst *CI = dyn_cast<CallInst>(I1)) 365 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && 366 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && 367 CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && 368 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); 369 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) 370 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && 371 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && 372 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); 373 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) 374 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); 375 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) 376 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); 377 if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) 378 return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && 379 FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope(); 380 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) 381 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && 382 CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && 383 CXI->getSuccessOrdering() == 384 cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && 385 CXI->getFailureOrdering() == 386 cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && 387 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope(); 388 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) 389 return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && 390 RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && 391 RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && 392 RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope(); 393 394 return true; 395} 396 397/// isIdenticalTo - Return true if the specified instruction is exactly 398/// identical to the current one. This means that all operands match and any 399/// extra information (e.g. load is volatile) agree. 400bool Instruction::isIdenticalTo(const Instruction *I) const { 401 return isIdenticalToWhenDefined(I) && 402 SubclassOptionalData == I->SubclassOptionalData; 403} 404 405/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it 406/// ignores the SubclassOptionalData flags, which specify conditions 407/// under which the instruction's result is undefined. 408bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { 409 if (getOpcode() != I->getOpcode() || 410 getNumOperands() != I->getNumOperands() || 411 getType() != I->getType()) 412 return false; 413 414 // If both instructions have no operands, they are identical. 415 if (getNumOperands() == 0 && I->getNumOperands() == 0) 416 return haveSameSpecialState(this, I); 417 418 // We have two instructions of identical opcode and #operands. Check to see 419 // if all operands are the same. 420 if (!std::equal(op_begin(), op_end(), I->op_begin())) 421 return false; 422 423 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { 424 const PHINode *otherPHI = cast<PHINode>(I); 425 return std::equal(thisPHI->block_begin(), thisPHI->block_end(), 426 otherPHI->block_begin()); 427 } 428 429 return haveSameSpecialState(this, I); 430} 431 432// Keep this in sync with FunctionComparator::cmpOperations in 433// lib/Transforms/IPO/MergeFunctions.cpp. 434bool Instruction::isSameOperationAs(const Instruction *I, 435 unsigned flags) const { 436 bool IgnoreAlignment = flags & CompareIgnoringAlignment; 437 bool UseScalarTypes = flags & CompareUsingScalarTypes; 438 439 if (getOpcode() != I->getOpcode() || 440 getNumOperands() != I->getNumOperands() || 441 (UseScalarTypes ? 442 getType()->getScalarType() != I->getType()->getScalarType() : 443 getType() != I->getType())) 444 return false; 445 446 // We have two instructions of identical opcode and #operands. Check to see 447 // if all operands are the same type 448 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 449 if (UseScalarTypes ? 450 getOperand(i)->getType()->getScalarType() != 451 I->getOperand(i)->getType()->getScalarType() : 452 getOperand(i)->getType() != I->getOperand(i)->getType()) 453 return false; 454 455 return haveSameSpecialState(this, I, IgnoreAlignment); 456} 457 458/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the 459/// specified block. Note that PHI nodes are considered to evaluate their 460/// operands in the corresponding predecessor block. 461bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { 462 for (const Use &U : uses()) { 463 // PHI nodes uses values in the corresponding predecessor block. For other 464 // instructions, just check to see whether the parent of the use matches up. 465 const Instruction *I = cast<Instruction>(U.getUser()); 466 const PHINode *PN = dyn_cast<PHINode>(I); 467 if (!PN) { 468 if (I->getParent() != BB) 469 return true; 470 continue; 471 } 472 473 if (PN->getIncomingBlock(U) != BB) 474 return true; 475 } 476 return false; 477} 478 479/// mayReadFromMemory - Return true if this instruction may read memory. 480/// 481bool Instruction::mayReadFromMemory() const { 482 switch (getOpcode()) { 483 default: return false; 484 case Instruction::VAArg: 485 case Instruction::Load: 486 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory 487 case Instruction::AtomicCmpXchg: 488 case Instruction::AtomicRMW: 489 case Instruction::CatchPad: 490 case Instruction::CatchRet: 491 return true; 492 case Instruction::Call: 493 return !cast<CallInst>(this)->doesNotAccessMemory(); 494 case Instruction::Invoke: 495 return !cast<InvokeInst>(this)->doesNotAccessMemory(); 496 case Instruction::Store: 497 return !cast<StoreInst>(this)->isUnordered(); 498 } 499} 500 501/// mayWriteToMemory - Return true if this instruction may modify memory. 502/// 503bool Instruction::mayWriteToMemory() const { 504 switch (getOpcode()) { 505 default: return false; 506 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory 507 case Instruction::Store: 508 case Instruction::VAArg: 509 case Instruction::AtomicCmpXchg: 510 case Instruction::AtomicRMW: 511 case Instruction::CatchPad: 512 case Instruction::CatchRet: 513 return true; 514 case Instruction::Call: 515 return !cast<CallInst>(this)->onlyReadsMemory(); 516 case Instruction::Invoke: 517 return !cast<InvokeInst>(this)->onlyReadsMemory(); 518 case Instruction::Load: 519 return !cast<LoadInst>(this)->isUnordered(); 520 } 521} 522 523bool Instruction::isAtomic() const { 524 switch (getOpcode()) { 525 default: 526 return false; 527 case Instruction::AtomicCmpXchg: 528 case Instruction::AtomicRMW: 529 case Instruction::Fence: 530 return true; 531 case Instruction::Load: 532 return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 533 case Instruction::Store: 534 return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 535 } 536} 537 538bool Instruction::mayThrow() const { 539 if (const CallInst *CI = dyn_cast<CallInst>(this)) 540 return !CI->doesNotThrow(); 541 if (const auto *CRI = dyn_cast<CleanupReturnInst>(this)) 542 return CRI->unwindsToCaller(); 543 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this)) 544 return CatchSwitch->unwindsToCaller(); 545 return isa<ResumeInst>(this); 546} 547 548/// isAssociative - Return true if the instruction is associative: 549/// 550/// Associative operators satisfy: x op (y op z) === (x op y) op z 551/// 552/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. 553/// 554bool Instruction::isAssociative(unsigned Opcode) { 555 return Opcode == And || Opcode == Or || Opcode == Xor || 556 Opcode == Add || Opcode == Mul; 557} 558 559bool Instruction::isAssociative() const { 560 unsigned Opcode = getOpcode(); 561 if (isAssociative(Opcode)) 562 return true; 563 564 switch (Opcode) { 565 case FMul: 566 case FAdd: 567 return cast<FPMathOperator>(this)->hasUnsafeAlgebra(); 568 default: 569 return false; 570 } 571} 572 573/// isCommutative - Return true if the instruction is commutative: 574/// 575/// Commutative operators satisfy: (x op y) === (y op x) 576/// 577/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 578/// applied to any type. 579/// 580bool Instruction::isCommutative(unsigned op) { 581 switch (op) { 582 case Add: 583 case FAdd: 584 case Mul: 585 case FMul: 586 case And: 587 case Or: 588 case Xor: 589 return true; 590 default: 591 return false; 592 } 593} 594 595/// isIdempotent - Return true if the instruction is idempotent: 596/// 597/// Idempotent operators satisfy: x op x === x 598/// 599/// In LLVM, the And and Or operators are idempotent. 600/// 601bool Instruction::isIdempotent(unsigned Opcode) { 602 return Opcode == And || Opcode == Or; 603} 604 605/// isNilpotent - Return true if the instruction is nilpotent: 606/// 607/// Nilpotent operators satisfy: x op x === Id, 608/// 609/// where Id is the identity for the operator, i.e. a constant such that 610/// x op Id === x and Id op x === x for all x. 611/// 612/// In LLVM, the Xor operator is nilpotent. 613/// 614bool Instruction::isNilpotent(unsigned Opcode) { 615 return Opcode == Xor; 616} 617 618Instruction *Instruction::cloneImpl() const { 619 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); 620} 621 622Instruction *Instruction::clone() const { 623 Instruction *New = nullptr; 624 switch (getOpcode()) { 625 default: 626 llvm_unreachable("Unhandled Opcode."); 627#define HANDLE_INST(num, opc, clas) \ 628 case Instruction::opc: \ 629 New = cast<clas>(this)->cloneImpl(); \ 630 break; 631#include "llvm/IR/Instruction.def" 632#undef HANDLE_INST 633 } 634 635 New->SubclassOptionalData = SubclassOptionalData; 636 if (!hasMetadata()) 637 return New; 638 639 // Otherwise, enumerate and copy over metadata from the old instruction to the 640 // new one. 641 SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; 642 getAllMetadataOtherThanDebugLoc(TheMDs); 643 for (const auto &MD : TheMDs) 644 New->setMetadata(MD.first, MD.second); 645 646 New->setDebugLoc(getDebugLoc()); 647 return New; 648} 649