//
//===----------------------------------------------------------------------===//
+#include "llvm/Instruction.h"
#include "llvm/Type.h"
#include "llvm/Instructions.h"
-#include "llvm/Function.h"
#include "llvm/Constants.h"
-#include "llvm/GlobalVariable.h"
+#include "llvm/Module.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/LeakDetector.h"
using namespace llvm;
// Out of line virtual method, so the vtable, etc has a home.
Instruction::~Instruction() {
assert(Parent == 0 && "Instruction still linked in the program!");
+ if (hasMetadataHashEntry())
+ clearMetadataHashEntries();
}
case Ret: return "ret";
case Br: return "br";
case Switch: return "switch";
+ case IndirectBr: return "indirectbr";
case Invoke: return "invoke";
case Unwind: return "unwind";
case Unreachable: return "unreachable";
case Xor: return "xor";
// Memory instructions...
- case Malloc: return "malloc";
- case Free: return "free";
case Alloca: return "alloca";
case Load: return "load";
case Store: return "store";
/// identical to the current one. This means that all operands match and any
/// extra information (e.g. load is volatile) agree.
bool Instruction::isIdenticalTo(const Instruction *I) const {
+ return isIdenticalToWhenDefined(I) &&
+ SubclassOptionalData == I->SubclassOptionalData;
+}
+
+/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
+/// ignores the SubclassOptionalData flags, which specify conditions
+/// under which the instruction's result is undefined.
+bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
if (getOpcode() != I->getOpcode() ||
getNumOperands() != I->getNumOperands() ||
getType() != I->getType())
if (const CallInst *CI = dyn_cast<CallInst>(this))
return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
- CI->getAttributes().getRawPointer() ==
- cast<CallInst>(I)->getAttributes().getRawPointer();
+ CI->getAttributes() == cast<CallInst>(I)->getAttributes();
if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
- CI->getAttributes().getRawPointer() ==
- cast<InvokeInst>(I)->getAttributes().getRawPointer();
+ CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
return false;
if (const CallInst *CI = dyn_cast<CallInst>(this))
return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
- CI->getAttributes().getRawPointer() ==
- cast<CallInst>(I)->getAttributes().getRawPointer();
+ CI->getAttributes() == cast<CallInst>(I)->getAttributes();
if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
- CI->getAttributes().getRawPointer() ==
- cast<InvokeInst>(I)->getAttributes().getRawPointer();
+ CI->getAttributes() ==
+ cast<InvokeInst>(I)->getAttributes();
if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
return false;
/// specified block. Note that PHI nodes are considered to evaluate their
/// operands in the corresponding predecessor block.
bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
- for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
+ for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
// PHI nodes uses values in the corresponding predecessor block. For other
// instructions, just check to see whether the parent of the use matches up.
- const PHINode *PN = dyn_cast<PHINode>(*UI);
+ const User *U = *UI;
+ const PHINode *PN = dyn_cast<PHINode>(U);
if (PN == 0) {
- if (cast<Instruction>(*UI)->getParent() != BB)
+ if (cast<Instruction>(U)->getParent() != BB)
return true;
continue;
}
-
+
if (PN->getIncomingBlock(UI) != BB)
return true;
}
- return false;
+ return false;
}
/// mayReadFromMemory - Return true if this instruction may read memory.
bool Instruction::mayReadFromMemory() const {
switch (getOpcode()) {
default: return false;
- case Instruction::Free:
case Instruction::VAArg:
case Instruction::Load:
return true;
bool Instruction::mayWriteToMemory() const {
switch (getOpcode()) {
default: return false;
- case Instruction::Free:
case Instruction::Store:
case Instruction::VAArg:
return true;
///
/// 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;
}
return Op && !Op->isNullValue() && !Op->isAllOnesValue();
}
case Load: {
- if (cast<LoadInst>(this)->isVolatile())
+ const LoadInst *LI = cast<LoadInst>(this);
+ if (LI->isVolatile())
return false;
- if (isa<AllocationInst>(getOperand(0)))
- return true;
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(getOperand(0)))
- return !GV->hasExternalWeakLinkage();
- // FIXME: Handle cases involving GEPs. We have to be careful because
- // a load of a out-of-bounds GEP has undefined behavior.
- return false;
+ return LI->getPointerOperand()->isDereferenceablePointer();
}
case Call:
return false; // The called function could have undefined behavior or
// overflow-checking arithmetic, etc.)
case VAArg:
case Alloca:
- case Malloc:
case Invoke:
case PHI:
case Store:
- case Free:
case Ret:
case Br:
+ case IndirectBr:
case Switch:
case Unwind:
case Unreachable:
return false; // Misc instructions which have effects
}
}
+
+Instruction *Instruction::clone() const {
+ Instruction *New = clone_impl();
+ New->SubclassOptionalData = SubclassOptionalData;
+ if (!hasMetadata())
+ return New;
+
+ // Otherwise, enumerate and copy over metadata from the old instruction to the
+ // new one.
+ SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
+ getAllMetadata(TheMDs);
+ for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
+ New->setMetadata(TheMDs[i].first, TheMDs[i].second);
+ return New;
+}