Index: lib/AST/Expr.cpp =================================================================== --- lib/AST/Expr.cpp +++ lib/AST/Expr.cpp @@ -2946,11 +2946,18 @@ case CXXOperatorCallExprClass: case CXXMemberCallExprClass: case CUDAKernelCallExprClass: - case BlockExprClass: - case CXXBindTemporaryExprClass: - case UserDefinedLiteralClass: + case UserDefinedLiteralClass: { // We don't know a call definitely has side effects, but we can check the // call's operands. + const Decl *FD = cast(this)->getCalleeDecl(); + bool IsConst = FD && FD->hasAttr(); + if (IsConst || !IncludePossibleEffects) + break; + return true; + } + + case BlockExprClass: + case CXXBindTemporaryExprClass: if (!IncludePossibleEffects) break; return true; Index: test/CodeGen/builtin-assume.c =================================================================== --- test/CodeGen/builtin-assume.c +++ test/CodeGen/builtin-assume.c @@ -1,25 +1,38 @@ // 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)); + // 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]]) + #ifdef _MSC_VER __assume(a != 0) + __assume(isconst()); #else __builtin_assume(a != 0); + __builtin_assume(isconst()); #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]; Index: test/Sema/builtin-assume.c =================================================================== --- test/Sema/builtin-assume.c +++ test/Sema/builtin-assume.c @@ -1,15 +1,22 @@ // 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 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); 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); int test = sizeof(struct{char qq[(__builtin_assume(i != 5), 7)];}); #endif