HasSideEffects() should return false for calls to pure and const functions.

Differential Revision: http://reviews.llvm.org/D8548

llvm-svn: 234152
This commit is contained in:
Michael Kuperstein 2015-04-06 13:22:01 +00:00
parent 1931b62632
commit aed5ccdeed
3 changed files with 43 additions and 4 deletions

View File

@ -2942,11 +2942,19 @@ bool Expr::HasSideEffects(const ASTContext &Ctx,
case CXXOperatorCallExprClass:
case CXXMemberCallExprClass:
case CUDAKernelCallExprClass:
case UserDefinedLiteralClass: {
// We don't know a call definitely has side effects, except for calls
// to pure/const functions that definitely don't.
// If the call itself is considered side-effect free, check the operands.
const Decl *FD = cast<CallExpr>(this)->getCalleeDecl();
bool IsPure = FD && (FD->hasAttr<ConstAttr>() || FD->hasAttr<PureAttr>());
if (IsPure || !IncludePossibleEffects)
break;
return true;
}
case BlockExprClass:
case CXXBindTemporaryExprClass:
case UserDefinedLiteralClass:
// We don't know a call definitely has side effects, but we can check the
// call's operands.
if (!IncludePossibleEffects)
break;
return true;

View File

@ -1,25 +1,44 @@
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple i386-mingw32 -fms-extensions -emit-llvm -o - %s | FileCheck %s
int nonconst(void);
int isconst(void) __attribute__((const));
int ispure(void) __attribute__((pure));
// CHECK-LABEL: @test1
int test1(int *a, int i) {
// CHECK: store i32* %a, i32** [[A_ADDR:%.+]], align
// CHECK: [[A:%.+]] = load i32*, i32** [[A_ADDR]]
// CHECK: [[CMP:%.+]] = icmp ne i32* [[A]], null
// CHECK: call void @llvm.assume(i1 [[CMP]])
// CHECK: [[CALL:%.+]] = call i32 @isconst()
// CHECK: [[BOOL:%.+]] = icmp ne i32 [[CALL]], 0
// CHECK: call void @llvm.assume(i1 [[BOOL]])
// CHECK: [[CALLPURE:%.+]] = call i32 @ispure()
// CHECK: [[BOOLPURE:%.+]] = icmp ne i32 [[CALLPURE]], 0
// CHECK: call void @llvm.assume(i1 [[BOOLPURE]])
#ifdef _MSC_VER
__assume(a != 0)
__assume(isconst());
__assume(ispure());
#else
__builtin_assume(a != 0);
__builtin_assume(isconst());
__builtin_assume(ispure());
#endif
// Nothing is generated for an assume with side effects...
// CHECK-NOT: load i32*, i32** %i.addr
// CHECK-NOT: call void @llvm.assume
// CHECK-NOT: call i32 @nonconst()
#ifdef _MSC_VER
__assume(++i != 0)
__assume(nonconst());
#else
__builtin_assume(++i != 0);
__builtin_assume(nonconst());
#endif
return a[0];

View File

@ -1,16 +1,28 @@
// RUN: %clang_cc1 -triple i386-mingw32 -fms-extensions -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify %s
int nonconst(void);
int isconst(void) __attribute__((const));
int ispure(int) __attribute__((pure));
int foo(int *a, int i) {
#ifdef _MSC_VER
__assume(i != 4);
__assume(++i > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
__assume(nonconst() > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
__assume(isconst() > 2);
__assume(ispure(i) > 2);
__assume(ispure(++i) > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
int test = sizeof(struct{char qq[(__assume(i != 5), 7)];});
#else
__builtin_assume(i != 4);
__builtin_assume(++i > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
__builtin_assume(nonconst() > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
__builtin_assume(isconst() > 2);
__builtin_assume(ispure(i) > 2);
__builtin_assume(ispure(++i) > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
int test = sizeof(struct{char qq[(__builtin_assume(i != 5), 7)];});
#endif
return a[i];