]> granicus.if.org Git - clang/commitdiff
fix rdar://9289603 - clang should fold trivial ?: for enums as well as integer consta...
authorChris Lattner <sabre@nondot.org>
Sat, 16 Apr 2011 23:15:35 +0000 (23:15 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 16 Apr 2011 23:15:35 +0000 (23:15 +0000)
by making the isCheapEnoughToEvaluateUnconditionally predicate handle anything that folds to a constant.  In particular, we now fold enums.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129649 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprScalar.cpp
test/CodeGen/conditional.c

index 556dbf54ec9c82ebbdff306e205dd58b3d8b9968..d4463c17a3bfba0ee9fdb2e9122031eaa6f58c49 100644 (file)
@@ -2401,9 +2401,8 @@ static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
                                                    CodeGenFunction &CGF) {
   E = E->IgnoreParens();
 
-  // TODO: Allow anything we can constant fold to an integer or fp constant.
-  if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
-      isa<FloatingLiteral>(E))
+  // Anything that is an integer or floating point constant is fine.
+  if (E->isConstantInitializer(CGF.getContext(), false))
     return true;
 
   // Non-volatile automatic variables too, to get "cond ? X : Y" where
index d079aafd787ade3ae6fb633e2ef99a5cc5db48d1..15e15f11e35fd9a04494eec5cc58671bca7833ef 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm %s -o %t
+// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
 
 float test1(int cond, float a, float b) {
   return cond ? a : b;
@@ -47,3 +47,22 @@ void test9(struct test9 *p) {
   p ? p : test9spare();
 }
 
+// CHECK: @test10
+// CHECK: select i1 {{.*}}, i32 4, i32 5
+int test10(int c) {
+  return c ? 4 : 5;
+}
+enum { Gronk = 5 };
+
+// rdar://9289603
+// CHECK: @test11
+// CHECK: select i1 {{.*}}, i32 4, i32 5
+int test11(int c) {
+  return c ? 4 : Gronk;
+}
+
+// CHECK: @test12
+// CHECK: select i1 {{.*}}, double 4.0{{.*}}, double 2.0
+double test12(int c) {
+  return c ? 4.0 : 2.0;
+}