[PATCH] Have HasSideEffects() return false for __attribute__((const)) functions

Michael Kuperstein michael.m.kuperstein at intel.com
Mon Mar 23 08:31:31 PDT 2015


Hi hfinkel, rsmith, aaron.ballman,

Currently, Expr::HasSideEffects() returns true for function-call expressions even if the function has the const attribute.
It seems like returning false in this case ought to be safe, if none of the sub-expressions have side effects.

http://reviews.llvm.org/D8548

Files:
  lib/AST/Expr.cpp
  test/CodeGen/builtin-assume.c
  test/Sema/builtin-assume.c

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
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: 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<CallExpr>(this)->getCalleeDecl();
+    bool IsConst = FD && FD->hasAttr<ConstAttr>();
+    if (IsConst || !IncludePossibleEffects)
+      break;
+    return true;
+  }
+
+  case BlockExprClass:
+  case CXXBindTemporaryExprClass:
     if (!IncludePossibleEffects)
       break;
     return true;

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8548.22472.patch
Type: text/x-patch
Size: 3331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150323/97e19464/attachment.bin>


More information about the cfe-commits mailing list