X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FInstruction.cpp;h=66379a0493128641f91055a06f4d02483b320c69;hb=e853d2e2508e21b5c3156c7d8b6e6902a7d2604a;hp=05bed4c64316fa77e406ec96153c6ccd0c700bd0;hpb=4f1be4abba762f8a7b77d7622abaf1ed1a87b48b;p=oota-llvm.git diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index 05bed4c6431..66379a04931 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -20,7 +20,7 @@ #include "llvm/Support/LeakDetector.h" using namespace llvm; -Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, +Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction *InsertBefore) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { // Make sure that we get added to a basicblock @@ -34,7 +34,7 @@ Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, } } -Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, +Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { // Make sure that we get added to a basicblock @@ -101,7 +101,7 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case Switch: return "switch"; case IndirectBr: return "indirectbr"; case Invoke: return "invoke"; - case Unwind: return "unwind"; + case Resume: return "resume"; case Unreachable: return "unreachable"; // Standard binary operators... @@ -127,6 +127,9 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case Alloca: return "alloca"; case Load: return "load"; case Store: return "store"; + case AtomicCmpXchg: return "cmpxchg"; + case AtomicRMW: return "atomicrmw"; + case Fence: return "fence"; case GetElementPtr: return "getelementptr"; // Convert instructions... @@ -158,11 +161,10 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case ShuffleVector: return "shufflevector"; case ExtractValue: return "extractvalue"; case InsertValue: return "insertvalue"; + case LandingPad: return "landingpad"; default: return " "; } - - return 0; } /// isIdenticalTo - Return true if the specified instruction is exactly @@ -191,90 +193,113 @@ bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { // Check special state that is a part of some instructions. if (const LoadInst *LI = dyn_cast(this)) return LI->isVolatile() == cast(I)->isVolatile() && - LI->getAlignment() == cast(I)->getAlignment(); + LI->getAlignment() == cast(I)->getAlignment() && + LI->getOrdering() == cast(I)->getOrdering() && + LI->getSynchScope() == cast(I)->getSynchScope(); if (const StoreInst *SI = dyn_cast(this)) return SI->isVolatile() == cast(I)->isVolatile() && - SI->getAlignment() == cast(I)->getAlignment(); + SI->getAlignment() == cast(I)->getAlignment() && + SI->getOrdering() == cast(I)->getOrdering() && + SI->getSynchScope() == cast(I)->getSynchScope(); if (const CmpInst *CI = dyn_cast(this)) return CI->getPredicate() == cast(I)->getPredicate(); if (const CallInst *CI = dyn_cast(this)) return CI->isTailCall() == cast(I)->isTailCall() && CI->getCallingConv() == cast(I)->getCallingConv() && - CI->getAttributes().getRawPointer() == - cast(I)->getAttributes().getRawPointer(); + CI->getAttributes() == cast(I)->getAttributes(); if (const InvokeInst *CI = dyn_cast(this)) return CI->getCallingConv() == cast(I)->getCallingConv() && - CI->getAttributes().getRawPointer() == - cast(I)->getAttributes().getRawPointer(); - if (const InsertValueInst *IVI = dyn_cast(this)) { - if (IVI->getNumIndices() != cast(I)->getNumIndices()) - return false; - for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i) - if (IVI->idx_begin()[i] != cast(I)->idx_begin()[i]) - return false; - return true; - } - if (const ExtractValueInst *EVI = dyn_cast(this)) { - if (EVI->getNumIndices() != cast(I)->getNumIndices()) - return false; - for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i) - if (EVI->idx_begin()[i] != cast(I)->idx_begin()[i]) + CI->getAttributes() == cast(I)->getAttributes(); + if (const InsertValueInst *IVI = dyn_cast(this)) + return IVI->getIndices() == cast(I)->getIndices(); + if (const ExtractValueInst *EVI = dyn_cast(this)) + return EVI->getIndices() == cast(I)->getIndices(); + if (const FenceInst *FI = dyn_cast(this)) + return FI->getOrdering() == cast(FI)->getOrdering() && + FI->getSynchScope() == cast(FI)->getSynchScope(); + if (const AtomicCmpXchgInst *CXI = dyn_cast(this)) + return CXI->isVolatile() == cast(I)->isVolatile() && + CXI->getOrdering() == cast(I)->getOrdering() && + CXI->getSynchScope() == cast(I)->getSynchScope(); + if (const AtomicRMWInst *RMWI = dyn_cast(this)) + return RMWI->getOperation() == cast(I)->getOperation() && + RMWI->isVolatile() == cast(I)->isVolatile() && + RMWI->getOrdering() == cast(I)->getOrdering() && + RMWI->getSynchScope() == cast(I)->getSynchScope(); + if (const PHINode *thisPHI = dyn_cast(this)) { + const PHINode *otherPHI = cast(I); + for (unsigned i = 0, e = thisPHI->getNumOperands(); i != e; ++i) { + if (thisPHI->getIncomingBlock(i) != otherPHI->getIncomingBlock(i)) return false; + } return true; } - return true; } // isSameOperationAs // This should be kept in sync with isEquivalentOperation in // lib/Transforms/IPO/MergeFunctions.cpp. -bool Instruction::isSameOperationAs(const Instruction *I) const { +bool Instruction::isSameOperationAs(const Instruction *I, + unsigned flags) const { + bool IgnoreAlignment = flags & CompareIgnoringAlignment; + bool UseScalarTypes = flags & CompareUsingScalarTypes; + if (getOpcode() != I->getOpcode() || getNumOperands() != I->getNumOperands() || - getType() != I->getType()) + (UseScalarTypes ? + getType()->getScalarType() != I->getType()->getScalarType() : + getType() != I->getType())) return false; // We have two instructions of identical opcode and #operands. Check to see // if all operands are the same type for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i)->getType() != I->getOperand(i)->getType()) + if (UseScalarTypes ? + getOperand(i)->getType()->getScalarType() != + I->getOperand(i)->getType()->getScalarType() : + getOperand(i)->getType() != I->getOperand(i)->getType()) return false; // Check special state that is a part of some instructions. if (const LoadInst *LI = dyn_cast(this)) return LI->isVolatile() == cast(I)->isVolatile() && - LI->getAlignment() == cast(I)->getAlignment(); + (LI->getAlignment() == cast(I)->getAlignment() || + IgnoreAlignment) && + LI->getOrdering() == cast(I)->getOrdering() && + LI->getSynchScope() == cast(I)->getSynchScope(); if (const StoreInst *SI = dyn_cast(this)) return SI->isVolatile() == cast(I)->isVolatile() && - SI->getAlignment() == cast(I)->getAlignment(); + (SI->getAlignment() == cast(I)->getAlignment() || + IgnoreAlignment) && + SI->getOrdering() == cast(I)->getOrdering() && + SI->getSynchScope() == cast(I)->getSynchScope(); if (const CmpInst *CI = dyn_cast(this)) return CI->getPredicate() == cast(I)->getPredicate(); if (const CallInst *CI = dyn_cast(this)) return CI->isTailCall() == cast(I)->isTailCall() && CI->getCallingConv() == cast(I)->getCallingConv() && - CI->getAttributes().getRawPointer() == - cast(I)->getAttributes().getRawPointer(); + CI->getAttributes() == cast(I)->getAttributes(); if (const InvokeInst *CI = dyn_cast(this)) return CI->getCallingConv() == cast(I)->getCallingConv() && - CI->getAttributes().getRawPointer() == - cast(I)->getAttributes().getRawPointer(); - if (const InsertValueInst *IVI = dyn_cast(this)) { - if (IVI->getNumIndices() != cast(I)->getNumIndices()) - return false; - for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i) - if (IVI->idx_begin()[i] != cast(I)->idx_begin()[i]) - return false; - return true; - } - if (const ExtractValueInst *EVI = dyn_cast(this)) { - if (EVI->getNumIndices() != cast(I)->getNumIndices()) - return false; - for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i) - if (EVI->idx_begin()[i] != cast(I)->idx_begin()[i]) - return false; - return true; - } + CI->getAttributes() == + cast(I)->getAttributes(); + if (const InsertValueInst *IVI = dyn_cast(this)) + return IVI->getIndices() == cast(I)->getIndices(); + if (const ExtractValueInst *EVI = dyn_cast(this)) + return EVI->getIndices() == cast(I)->getIndices(); + if (const FenceInst *FI = dyn_cast(this)) + return FI->getOrdering() == cast(I)->getOrdering() && + FI->getSynchScope() == cast(I)->getSynchScope(); + if (const AtomicCmpXchgInst *CXI = dyn_cast(this)) + return CXI->isVolatile() == cast(I)->isVolatile() && + CXI->getOrdering() == cast(I)->getOrdering() && + CXI->getSynchScope() == cast(I)->getSynchScope(); + if (const AtomicRMWInst *RMWI = dyn_cast(this)) + return RMWI->getOperation() == cast(I)->getOperation() && + RMWI->isVolatile() == cast(I)->isVolatile() && + RMWI->getOrdering() == cast(I)->getOrdering() && + RMWI->getSynchScope() == cast(I)->getSynchScope(); return true; } @@ -307,13 +332,16 @@ bool Instruction::mayReadFromMemory() const { default: return false; case Instruction::VAArg: case Instruction::Load: + case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory + case Instruction::AtomicCmpXchg: + case Instruction::AtomicRMW: return true; case Instruction::Call: return !cast(this)->doesNotAccessMemory(); case Instruction::Invoke: return !cast(this)->doesNotAccessMemory(); case Instruction::Store: - return cast(this)->isVolatile(); + return !cast(this)->isUnordered(); } } @@ -322,15 +350,18 @@ bool Instruction::mayReadFromMemory() const { bool Instruction::mayWriteToMemory() const { switch (getOpcode()) { default: return false; + case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory case Instruction::Store: case Instruction::VAArg: + case Instruction::AtomicCmpXchg: + case Instruction::AtomicRMW: return true; case Instruction::Call: return !cast(this)->onlyReadsMemory(); case Instruction::Invoke: return !cast(this)->onlyReadsMemory(); case Instruction::Load: - return cast(this)->isVolatile(); + return !cast(this)->isUnordered(); } } @@ -339,7 +370,7 @@ bool Instruction::mayWriteToMemory() const { bool Instruction::mayThrow() const { if (const CallInst *CI = dyn_cast(this)) return !CI->doesNotThrow(); - return false; + return isa(this); } /// isAssociative - Return true if the instruction is associative: @@ -348,7 +379,7 @@ bool Instruction::mayThrow() const { /// /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. /// -bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) { +bool Instruction::isAssociative(unsigned Opcode) { return Opcode == And || Opcode == Or || Opcode == Xor || Opcode == Add || Opcode == Mul; } @@ -375,67 +406,27 @@ bool Instruction::isCommutative(unsigned op) { } } -bool Instruction::isSafeToSpeculativelyExecute() const { - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (Constant *C = dyn_cast(getOperand(i))) - if (C->canTrap()) - return false; +/// isIdempotent - Return true if the instruction is idempotent: +/// +/// Idempotent operators satisfy: x op x === x +/// +/// In LLVM, the And and Or operators are idempotent. +/// +bool Instruction::isIdempotent(unsigned Opcode) { + return Opcode == And || Opcode == Or; +} - switch (getOpcode()) { - default: - return true; - case UDiv: - case URem: { - // x / y is undefined if y == 0, but calcuations like x / 3 are safe. - ConstantInt *Op = dyn_cast(getOperand(1)); - return Op && !Op->isNullValue(); - } - case SDiv: - case SRem: { - // x / y is undefined if y == 0, and might be undefined if y == -1, - // but calcuations like x / 3 are safe. - ConstantInt *Op = dyn_cast(getOperand(1)); - return Op && !Op->isNullValue() && !Op->isAllOnesValue(); - } - case Load: { - if (cast(this)->isVolatile()) - return false; - // Note that it is not safe to speculate into a malloc'd region because - // malloc may return null. - // It's also not safe to follow a bitcast, for example: - // bitcast i8* (alloca i8) to i32* - // would result in a 4-byte load from a 1-byte alloca. - Value *Op0 = getOperand(0); - if (GEPOperator *GEP = dyn_cast(Op0)) { - // TODO: it's safe to do this for any GEP with constant indices that - // compute inside the allocated type, but not for any inbounds gep. - if (GEP->hasAllZeroIndices()) - Op0 = GEP->getPointerOperand(); - } - if (isa(Op0)) - return true; - if (GlobalVariable *GV = dyn_cast(getOperand(0))) - return !GV->hasExternalWeakLinkage(); - return false; - } - case Call: - return false; // The called function could have undefined behavior or - // side-effects. - // FIXME: We should special-case some intrinsics (bswap, - // overflow-checking arithmetic, etc.) - case VAArg: - case Alloca: - case Invoke: - case PHI: - case Store: - case Ret: - case Br: - case IndirectBr: - case Switch: - case Unwind: - case Unreachable: - return false; // Misc instructions which have effects - } +/// isNilpotent - Return true if the instruction is nilpotent: +/// +/// Nilpotent operators satisfy: x op x === Id, +/// +/// where Id is the identity for the operator, i.e. a constant such that +/// x op Id === x and Id op x === x for all x. +/// +/// In LLVM, the Xor operator is nilpotent. +/// +bool Instruction::isNilpotent(unsigned Opcode) { + return Opcode == Xor; } Instruction *Instruction::clone() const { @@ -447,8 +438,10 @@ Instruction *Instruction::clone() const { // Otherwise, enumerate and copy over metadata from the old instruction to the // new one. SmallVector, 4> TheMDs; - getAllMetadata(TheMDs); + getAllMetadataOtherThanDebugLoc(TheMDs); for (unsigned i = 0, e = TheMDs.size(); i != e; ++i) New->setMetadata(TheMDs[i].first, TheMDs[i].second); + + New->setDebugLoc(getDebugLoc()); return New; }