From: Michael Kuperstein Date: Mon, 6 Apr 2015 13:22:01 +0000 (+0000) Subject: HasSideEffects() should return false for calls to pure and const functions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9d0cf0b63f2276210ce32821191c59bdfd7a5260;p=clang HasSideEffects() should return false for calls to pure and const functions. Differential Revision: http://reviews.llvm.org/D8548 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@234152 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index d13a11c4a3..d4ec2714d5 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -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(this)->getCalleeDecl(); + bool IsPure = FD && (FD->hasAttr() || FD->hasAttr()); + 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; diff --git a/test/CodeGen/builtin-assume.c b/test/CodeGen/builtin-assume.c index 8f83f17377..19afec69e7 100644 --- a/test/CodeGen/builtin-assume.c +++ b/test/CodeGen/builtin-assume.c @@ -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]; diff --git a/test/Sema/builtin-assume.c b/test/Sema/builtin-assume.c index 512eeeccdc..43b31375f2 100644 --- a/test/Sema/builtin-assume.c +++ b/test/Sema/builtin-assume.c @@ -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];