From 03544ec2a43fab162d25cf44627d1d08430bcccd Mon Sep 17 00:00:00 2001 From: Nadav Rotem Date: Tue, 19 Feb 2013 20:02:09 +0000 Subject: [PATCH] Fix a bug in mayHaveSideEffects. Functions that do not return are now considered as instructions with side effects. rdar://13227456 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175553 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Instruction.h | 8 +++++++- lib/IR/Instruction.cpp | 8 ++++++-- test/Transforms/FunctionAttrs/noreturn.ll | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/Transforms/FunctionAttrs/noreturn.ll diff --git a/include/llvm/IR/Instruction.h b/include/llvm/IR/Instruction.h index e5e5f96d561..5721d8f2f3f 100644 --- a/include/llvm/IR/Instruction.h +++ b/include/llvm/IR/Instruction.h @@ -309,6 +309,12 @@ public: /// bool mayThrow() const; + /// mayReturn - Return true if this is a function that may return. + /// this is true for all normal instructions. The only exception + /// is functions that are marked with the 'noreturn' attribute. + /// + bool mayReturn() const; + /// mayHaveSideEffects - Return true if the instruction may have side effects. /// /// Note that this does not consider malloc and alloca to have side @@ -316,7 +322,7 @@ public: /// instructions which don't used the returned value. For cases where this /// matters, isSafeToSpeculativelyExecute may be more appropriate. bool mayHaveSideEffects() const { - return mayWriteToMemory() || mayThrow(); + return mayWriteToMemory() || mayThrow() || !mayReturn(); } /// clone() - Create a copy of 'this' instruction that is identical in all diff --git a/lib/IR/Instruction.cpp b/lib/IR/Instruction.cpp index 42df5d71182..2b5a0b39c31 100644 --- a/lib/IR/Instruction.cpp +++ b/lib/IR/Instruction.cpp @@ -455,14 +455,18 @@ 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 isa(this); } +bool Instruction::mayReturn() const { + if (const CallInst *CI = dyn_cast(this)) + return !CI->doesNotReturn(); + return true; +} + /// 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/FunctionAttrs/noreturn.ll b/test/Transforms/FunctionAttrs/noreturn.ll new file mode 100644 index 00000000000..470ebcb1d3c --- /dev/null +++ b/test/Transforms/FunctionAttrs/noreturn.ll @@ -0,0 +1,18 @@ +; RUN: opt < %s -functionattrs -instcombine -S | FileCheck %s + +define void @endless_loop() noreturn nounwind readnone ssp uwtable { +entry: + br label %while.body + +while.body: + br label %while.body +} +;CHECK: @main +;CHECK: endless_loop +;CHECK: ret +define i32 @main() noreturn nounwind ssp uwtable { +entry: + tail call void @endless_loop() + unreachable +} +