builtins.c (fold_builtin_isascii, [...]): New.
authorKaveh R. Ghazi <ghazi@caip.rutgers.edu>
Thu, 8 Apr 2004 14:46:05 +0000 (14:46 +0000)
committerKaveh Ghazi <ghazi@gcc.gnu.org>
Thu, 8 Apr 2004 14:46:05 +0000 (14:46 +0000)
* builtins.c (fold_builtin_isascii, fold_builtin_toascii): New.
(fold_builtin): Handle BUILT_IN_ISASCII and BUILT_IN_TOASCII.

testsuite:
* gcc.dg/torture/builtin-ctype-2.c: New test.

From-SVN: r80508

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c [new file with mode: 0644]

index 434bbaa7e5176ee07a3995a2d81ef22b5153b7ea..e2c2f7b71e4367f2924a31b8e12ed108aaf7c6eb 100644 (file)
@@ -1,3 +1,8 @@
+2004-04-08  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * builtins.c (fold_builtin_isascii, fold_builtin_toascii): New.
+       (fold_builtin): Handle BUILT_IN_ISASCII and BUILT_IN_TOASCII.
+
 2004-04-07  H.J. Lu  <hongjiu.lu@intel.com>
 
        * config/ia64/ia64.c (ia64_encode_section_info): Don't prod
index b0d0d48f2fdd2a9fe78931c45a4ee398d66e7cab..655dd2503952fdf9ff93e051c9cdbf3f243b6fb4 100644 (file)
@@ -6742,6 +6742,43 @@ fold_builtin_signbit (tree exp)
   return NULL_TREE;
 }
 
+/* Fold a call to builtin isascii.  */
+
+static tree
+fold_builtin_isascii (tree arglist)
+{
+  if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
+    return 0;
+  else
+    {
+      /* Transform isascii(c) -> ((c & ~0x7f) == 0).  */
+      tree arg = TREE_VALUE (arglist);
+      
+      return fold (build (EQ_EXPR, integer_type_node,
+                         build (BIT_AND_EXPR, integer_type_node, arg,
+                                build_int_2 (~ (unsigned HOST_WIDE_INT) 0x7f,
+                                             ~ (HOST_WIDE_INT) 0)),
+                         integer_zero_node));
+    }
+}
+
+/* Fold a call to builtin toascii.  */
+
+static tree
+fold_builtin_toascii (tree arglist)
+{
+  if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
+    return 0;
+  else
+    {
+      /* Transform toascii(c) -> (c & 0x7f).  */
+      tree arg = TREE_VALUE (arglist);
+      
+      return fold (build (BIT_AND_EXPR, integer_type_node, arg,
+                         build_int_2 (0x7f, 0)));
+    }
+}
+
 
 /* Used by constant folding to eliminate some builtin calls early.  EXP is
    the CALL_EXPR of a call to a builtin function.  */
@@ -7236,6 +7273,12 @@ fold_builtin (tree exp)
     case BUILT_IN_SIGNBITL:
       return fold_builtin_signbit (exp);
 
+    case BUILT_IN_ISASCII:
+      return fold_builtin_isascii (arglist);
+
+    case BUILT_IN_TOASCII:
+      return fold_builtin_toascii (arglist);
+
     default:
       break;
     }
index 11b90ec46eb2b98b62d4671be299aa43db175a27..d0aa28f22b9202776558fcd01ccb16f035b5d4a3 100644 (file)
@@ -1,3 +1,7 @@
+2004-04-08  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * gcc.dg/torture/builtin-ctype-2.c: New test.
+
 2004-04-07  Ian Lance Taylor  <ian@wasabisystems.com>
 
        * gcc.dg/pch/pch.exp: Add largefile test.
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c b/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c
new file mode 100644 (file)
index 0000000..a306bcc
--- /dev/null
@@ -0,0 +1,84 @@
+/* Copyright (C) 2004  Free Software Foundation.
+
+   Verify that built-in ctype transformations are done correctly by
+   the compiler.
+
+   Written by Kaveh Ghazi, 2004-04-05.  */
+
+/* { dg-do link } */
+
+extern void link_failure_var(void);
+
+void test(int i)
+{
+  /* All of these ctype calls should compile-time evaluate to true.  */
+#define TEST_CTYPE_CST_TRUE(FN, VALUE) \
+  extern void link_failure_##FN##_cst_true(void); \
+  extern int FN(int); \
+  if (! FN(VALUE)) \
+    link_failure_##FN##_cst_true()
+
+  /* All of these ctype calls should compile-time evaluate to false.  */
+#define TEST_CTYPE_CST_FALSE(FN, VALUE) \
+  extern void link_failure_##FN##_cst_false(void); \
+  extern int FN(int); \
+  if (FN(VALUE)) \
+    link_failure_##FN##_cst_false()
+  
+  /* All of these ctype calls should compile-time evaluate to true.  */
+#define TEST_TOCTYPE_CST_TRUE(FN, VALUE) \
+  extern void link_failure_##FN##_cst_true(void); \
+  extern int FN(int); \
+  if (FN(VALUE) != (VALUE)) \
+    link_failure_##FN##_cst_true()
+
+  /* All of these ctype calls should compile-time evaluate to false.  */
+#define TEST_TOCTYPE_CST_FALSE(FN, VALUE) \
+  extern void link_failure_##FN##_cst_false(void); \
+  extern int FN(int); \
+  if (FN(VALUE) == (VALUE)) \
+    link_failure_##FN##_cst_false()
+  
+#ifdef __OPTIMIZE__
+  TEST_CTYPE_CST_TRUE (isascii, 0);
+  TEST_CTYPE_CST_TRUE (isascii, 1);
+  TEST_CTYPE_CST_TRUE (isascii, 126);
+  TEST_CTYPE_CST_TRUE (isascii, 127);
+
+  TEST_CTYPE_CST_FALSE (isascii, -1);
+  TEST_CTYPE_CST_FALSE (isascii, 128);
+  TEST_CTYPE_CST_FALSE (isascii, 129);
+  TEST_CTYPE_CST_FALSE (isascii, 255);
+  TEST_CTYPE_CST_FALSE (isascii, 256);
+  TEST_CTYPE_CST_FALSE (isascii, 257);
+  TEST_CTYPE_CST_FALSE (isascii, 10000);
+  TEST_CTYPE_CST_FALSE (isascii, __INT_MAX__);
+  
+  /* This ctype call should transform into another expression.  */
+  if (isascii(i) != ((i & ~0x7f) == 0))
+    link_failure_var();
+
+  TEST_TOCTYPE_CST_TRUE (toascii, 0);
+  TEST_TOCTYPE_CST_TRUE (toascii, 1);
+  TEST_TOCTYPE_CST_TRUE (toascii, 126);
+  TEST_TOCTYPE_CST_TRUE (toascii, 127);
+
+  TEST_TOCTYPE_CST_FALSE (toascii, -1);
+  TEST_TOCTYPE_CST_FALSE (toascii, 128);
+  TEST_TOCTYPE_CST_FALSE (toascii, 129);
+  TEST_TOCTYPE_CST_FALSE (toascii, 255);
+  TEST_TOCTYPE_CST_FALSE (toascii, 256);
+  TEST_TOCTYPE_CST_FALSE (toascii, 10000);
+  TEST_TOCTYPE_CST_FALSE (toascii, __INT_MAX__);
+
+  /* This ctype call should transform into another expression.  */
+  if (toascii(i) != (i & 0x7f))
+    link_failure_var();
+
+#endif /* __OPTIMIZE__ */
+}
+
+int main (void)
+{
+  return 0;
+}