diff --git a/llvm/include/llvm/Instruction.h b/llvm/include/llvm/Instruction.h index 7f7bcc881cc0..239e878a6a5a 100644 --- a/llvm/include/llvm/Instruction.h +++ b/llvm/include/llvm/Instruction.h @@ -54,7 +54,7 @@ public: /// mayWriteToMemory - Return true if this instruction may modify memory. /// - virtual bool mayWriteToMemory() const { return false; } + bool mayWriteToMemory() const; /// clone() - Create a copy of 'this' instruction that is identical in all /// ways except the following: diff --git a/llvm/include/llvm/Instructions.h b/llvm/include/llvm/Instructions.h index 9fc76019aa6a..259b5aa16570 100644 --- a/llvm/include/llvm/Instructions.h +++ b/llvm/include/llvm/Instructions.h @@ -189,8 +189,6 @@ public: virtual FreeInst *clone() const; - virtual bool mayWriteToMemory() const { return true; } - // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const FreeInst *) { return true; } static inline bool classof(const Instruction *I) { @@ -245,8 +243,6 @@ public: virtual LoadInst *clone() const; - virtual bool mayWriteToMemory() const { return isVolatile(); } - Value *getPointerOperand() { return getOperand(0); } const Value *getPointerOperand() const { return getOperand(0); } static unsigned getPointerOperandIndex() { return 0U; } @@ -310,8 +306,6 @@ public: virtual StoreInst *clone() const; - virtual bool mayWriteToMemory() const { return true; } - Value *getPointerOperand() { return getOperand(1); } const Value *getPointerOperand() const { return getOperand(1); } static unsigned getPointerOperandIndex() { return 1U; } @@ -722,8 +716,7 @@ public: ~CallInst(); virtual CallInst *clone() const; - bool mayWriteToMemory() const { return true; } - + bool isTailCall() const { return SubclassData & 1; } void setTailCall(bool isTailCall = true) { SubclassData = (SubclassData & ~1) | unsigned(isTailCall); @@ -845,7 +838,6 @@ public: } virtual VAArgInst *clone() const; - bool mayWriteToMemory() const { return true; } // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const VAArgInst *) { return true; } @@ -888,8 +880,6 @@ public: virtual ExtractElementInst *clone() const; - virtual bool mayWriteToMemory() const { return false; } - /// Transparently provide more efficient getOperand methods. Value *getOperand(unsigned i) const { assert(i < 2 && "getOperand() out of range!"); @@ -938,8 +928,6 @@ public: virtual InsertElementInst *clone() const; - virtual bool mayWriteToMemory() const { return false; } - /// getType - Overload to return most specific vector type. /// inline const VectorType *getType() const { @@ -990,8 +978,6 @@ public: virtual ShuffleVectorInst *clone() const; - virtual bool mayWriteToMemory() const { return false; } - /// getType - Overload to return most specific vector type. /// inline const VectorType *getType() const { @@ -1499,8 +1485,6 @@ public: virtual InvokeInst *clone() const; - bool mayWriteToMemory() const { return true; } - /// getCallingConv/setCallingConv - Get or set the calling convention of this /// function call. unsigned getCallingConv() const { return SubclassData; } diff --git a/llvm/lib/VMCore/Instruction.cpp b/llvm/lib/VMCore/Instruction.cpp index 39a895510ae2..e02af07937a1 100644 --- a/llvm/lib/VMCore/Instruction.cpp +++ b/llvm/lib/VMCore/Instruction.cpp @@ -225,6 +225,25 @@ bool Instruction::isSameOperationAs(Instruction *I) const { return true; } +/// mayWriteToMemory - Return true if this instruction may modify memory. +/// +bool Instruction::mayWriteToMemory() const { + switch (getOpcode()) { + default: return false; + case Instruction::Free: + case Instruction::Store: + case Instruction::Invoke: + case Instruction::VAArg: + return true; + case Instruction::Call: + if (const IntrinsicInst *II = dyn_cast(this)) { + // If the intrinsic doesn't write memory, it is safe. + } + return true; + case Instruction::Load: + return cast(this)->isVolatile(); + } +} /// isAssociative - Return true if the instruction is associative: ///