diff --git a/docs/LangRef.html b/docs/LangRef.html index f07fc31083e..db59e0d0738 100644 --- a/docs/LangRef.html +++ b/docs/LangRef.html @@ -1061,23 +1061,24 @@ unwind or exceptional control flow. If the function does unwind, its runtime behavior is undefined.
readnone
-
This attribute indicates that the function computes its result (or the -exception it throws) based strictly on its arguments, without dereferencing any +
This attribute indicates that the function computes its result (or decides to +unwind an exception) based strictly on its arguments, without dereferencing any pointer arguments or otherwise accessing any mutable state (e.g. memory, control registers, etc) visible to caller functions. It does not write through any pointer arguments (including byval arguments) and -never changes any state visible to callers. readnone functions may not throw -an exception that escapes into the caller.
+never changes any state visible to callers. This means that it cannot unwind +exceptions by calling the C++ exception throwing methods, but could +use the unwind instruction.
readonly
This attribute indicates that the function does not write through any pointer arguments (including byval arguments) or otherwise modify any state (e.g. memory, control registers, etc) visible to caller functions. It may dereference pointer arguments and read state that may -be set in the caller. A readonly function always returns the same value when -called with the same set of arguments and global -state. readonly functions may not throw an exception that escapes into the -caller.
+be set in the caller. A readonly function always returns the same value (or +unwinds an exception identically) when called with the same set of arguments +and global state. It cannot unwind an exception by calling the C++ +exception throwing methods, but may use the unwind instruction.
ssp
This attribute indicates that the function should emit a stack smashing diff --git a/include/llvm/Instruction.h b/include/llvm/Instruction.h index 0a39b08461d..7d946e85a6d 100644 --- a/include/llvm/Instruction.h +++ b/include/llvm/Instruction.h @@ -40,14 +40,6 @@ public: // Out of line virtual method, so the vtable, etc has a home. ~Instruction(); - /// mayWriteToMemory - Return true if this instruction may modify memory. - /// - bool mayWriteToMemory() const; - - /// mayReadFromMemory - Return true if this instruction may read memory. - /// - bool mayReadFromMemory() const; - /// clone() - Create a copy of 'this' instruction that is identical in all /// ways except the following: /// * The instruction has no parent @@ -181,6 +173,24 @@ public: } static bool isTrapping(unsigned op); + /// mayWriteToMemory - Return true if this instruction may modify memory. + /// + bool mayWriteToMemory() const; + + /// mayReadFromMemory - Return true if this instruction may read memory. + /// + bool mayReadFromMemory() const; + + /// mayThrow - Return true if this instruction may throw an exception. + /// + bool mayThrow() const; + + /// mayHaveSideEffects - Return true if the instruction may have side effects. + /// + bool mayHaveSideEffects() const { + return mayWriteToMemory() || mayThrow(); + } + /// Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const Instruction *) { return true; } static inline bool classof(const Value *V) { diff --git a/lib/Transforms/Scalar/ADCE.cpp b/lib/Transforms/Scalar/ADCE.cpp index bfea2b27841..9c55f664ebb 100644 --- a/lib/Transforms/Scalar/ADCE.cpp +++ b/lib/Transforms/Scalar/ADCE.cpp @@ -57,7 +57,7 @@ bool ADCE::runOnFunction(Function& F) { for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) if (isa(I.getInstructionIterator()) || isa(I.getInstructionIterator()) || - I->mayWriteToMemory()) { + I->mayHaveSideEffects()) { alive.insert(I.getInstructionIterator()); worklist.push_back(I.getInstructionIterator()); } diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index d605ffb6602..260bbedddc9 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -1479,13 +1479,13 @@ bool GVN::performPRE(Function& F) { for (BasicBlock::iterator BI = CurrentBlock->begin(), BE = CurrentBlock->end(); BI != BE; ) { Instruction *CurInst = BI++; - + if (isa(CurInst) || isa(CurInst) || isa(CurInst) || (CurInst->getType() == Type::VoidTy) || - CurInst->mayReadFromMemory() || CurInst->mayWriteToMemory() || + CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() || isa(CurInst)) continue; - + uint32_t valno = VN.lookup(CurInst); // Look for the predecessors for PRE opportunities. We're diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index eebac00a10c..9feb442df97 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -12579,7 +12579,7 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { assert(I->hasOneUse() && "Invariants didn't hold!"); // Cannot move control-flow-involving, volatile loads, vaarg, etc. - if (isa(I) || I->mayWriteToMemory() || isa(I)) + if (isa(I) || I->mayHaveSideEffects() || isa(I)) return false; // Do not sink alloca instructions out of the entry block. diff --git a/lib/Transforms/Scalar/LoopDeletion.cpp b/lib/Transforms/Scalar/LoopDeletion.cpp index 96b7a5288a7..65126728c7f 100644 --- a/lib/Transforms/Scalar/LoopDeletion.cpp +++ b/lib/Transforms/Scalar/LoopDeletion.cpp @@ -136,11 +136,8 @@ bool LoopDeletion::IsLoopDead(Loop* L, LI != LE; ++LI) { for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); BI != BE; ++BI) { - if (BI->mayWriteToMemory()) + if (BI->mayHaveSideEffects()) return false; - else if (LoadInst* L = dyn_cast(BI)) - if (L->isVolatile()) - return false; } } diff --git a/lib/Transforms/Scalar/LoopIndexSplit.cpp b/lib/Transforms/Scalar/LoopIndexSplit.cpp index ffa1d902b77..40d2e4a9d8b 100644 --- a/lib/Transforms/Scalar/LoopIndexSplit.cpp +++ b/lib/Transforms/Scalar/LoopIndexSplit.cpp @@ -1148,7 +1148,7 @@ bool LoopIndexSplit::cleanBlock(BasicBlock *BB) { || isa(I)) continue; - if (I->mayWriteToMemory()) + if (I->mayHaveSideEffects()) return false; // I is used only inside this block then it is OK. diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp index 2afb3c8ed7a..e3e881f0812 100644 --- a/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -300,7 +300,7 @@ static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, // Okay, everything after this looks good, check to make sure that this block // doesn't include any side effects. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) - if (I->mayWriteToMemory()) + if (I->mayHaveSideEffects()) return false; return true; @@ -383,7 +383,7 @@ bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val, // part of the loop that the code *would* execute. We already checked the // tail, check the header now. for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I) - if (I->mayWriteToMemory()) + if (I->mayHaveSideEffects()) return false; return true; } diff --git a/lib/Transforms/Scalar/TailDuplication.cpp b/lib/Transforms/Scalar/TailDuplication.cpp index 78690699bd5..99a7dee3988 100644 --- a/lib/Transforms/Scalar/TailDuplication.cpp +++ b/lib/Transforms/Scalar/TailDuplication.cpp @@ -258,7 +258,7 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) { while (!isa(BBI)) { Instruction *I = BBI++; - bool CanHoist = !I->isTrapping() && !I->mayWriteToMemory(); + bool CanHoist = !I->isTrapping() && !I->mayHaveSideEffects(); if (CanHoist) { for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) if (Instruction *OpI = dyn_cast(I->getOperand(op))) diff --git a/lib/Transforms/Scalar/TailRecursionElimination.cpp b/lib/Transforms/Scalar/TailRecursionElimination.cpp index e1234efb578..682d069923e 100644 --- a/lib/Transforms/Scalar/TailRecursionElimination.cpp +++ b/lib/Transforms/Scalar/TailRecursionElimination.cpp @@ -201,7 +201,7 @@ bool TailCallElim::runOnFunction(Function &F) { bool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) { // FIXME: We can move load/store/call/free instructions above the call if the // call does not mod/ref the memory location being processed. - if (I->mayWriteToMemory() || isa(I)) + if (I->mayHaveSideEffects() || isa(I)) return false; // Otherwise, if this is a side-effect free instruction, check to make sure diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index fea739cb46b..4f2bb1e3de7 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -164,17 +164,15 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) { // We don't want debug info removed by anything this general. if (isa(I)) return false; - - if (!I->mayWriteToMemory()) - return true; - // Special case intrinsics that "may write to memory" but can be deleted when - // dead. + if (!I->mayHaveSideEffects()) return true; + + // Special case intrinsics that "may have side effects" but can be deleted + // when dead. if (IntrinsicInst *II = dyn_cast(I)) // Safe to delete llvm.stacksave if dead. if (II->getIntrinsicID() == Intrinsic::stacksave) return true; - return false; } @@ -230,7 +228,7 @@ llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) { SmallPtrSet PHIs; PHIs.insert(PN); for (Instruction *J = cast(*PN->use_begin()); - J->hasOneUse() && !J->mayWriteToMemory(); + J->hasOneUse() && !J->mayHaveSideEffects(); J = cast(*J->use_begin())) // If we find a PHI more than once, we're on a cycle that // won't prove fruitful. diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index f33c1a23f23..9e030b78e9e 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -320,6 +320,14 @@ bool Instruction::mayWriteToMemory() const { } } +/// mayThrow - Return true if this instruction may throw an exception. +/// +bool Instruction::mayThrow() const { + if (const CallInst *CI = dyn_cast(this)) + return !CI->doesNotThrow(); + return false; +} + /// isAssociative - Return true if the instruction is associative: /// /// Associative operators satisfy: x op (y op z) === (x op y) op z) diff --git a/test/Transforms/ADCE/dce_pure_call.ll b/test/Transforms/ADCE/dce_pure_call.ll index a7414e027ea..3935bf72b9c 100644 --- a/test/Transforms/ADCE/dce_pure_call.ll +++ b/test/Transforms/ADCE/dce_pure_call.ll @@ -1,8 +1,8 @@ ; RUN: llvm-as < %s | opt -adce | llvm-dis | not grep call -declare i32 @strlen(i8*) readonly +declare i32 @strlen(i8*) readonly nounwind define void @test() { - call i32 @strlen( i8* null ) readonly ; :1 [#uses=0] + call i32 @strlen( i8* null ) ; :1 [#uses=0] ret void }