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

Michael Kuperstein michael.m.kuperstein at intel.com
Mon Apr 6 06:22:02 PDT 2015


Author: mkuper
Date: Mon Apr  6 08:22:01 2015
New Revision: 234152

URL: http://llvm.org/viewvc/llvm-project?rev=234152&view=rev
Log:
HasSideEffects() should return false for calls to pure and const functions.

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

Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/test/CodeGen/builtin-assume.c
    cfe/trunk/test/Sema/builtin-assume.c

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=234152&r1=234151&r2=234152&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Apr  6 08:22:01 2015
@@ -2942,11 +2942,19 @@ bool Expr::HasSideEffects(const ASTConte
   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;

Modified: cfe/trunk/test/CodeGen/builtin-assume.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtin-assume.c?rev=234152&r1=234151&r2=234152&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtin-assume.c (original)
+++ cfe/trunk/test/CodeGen/builtin-assume.c Mon Apr  6 08:22:01 2015
@@ -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];

Modified: cfe/trunk/test/Sema/builtin-assume.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtin-assume.c?rev=234152&r1=234151&r2=234152&view=diff
==============================================================================
--- cfe/trunk/test/Sema/builtin-assume.c (original)
+++ cfe/trunk/test/Sema/builtin-assume.c Mon Apr  6 08:22:01 2015
@@ -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];





More information about the cfe-commits mailing list