The user has overridden the function name "toupper" . Its marked as a builtin function, presumably because it matches the name.  In range folding, we were assuming the LHS and the parameter were compatible types...  but they are not in this case..

I don't know if we should be marking such a thing as a builtin function, but regardless.. we shouldn't be trapping.  If the type of the argument is not compatible with  the LHS, then we'll simply assume nothing about the function.

Bootstraps with no regression on x86_64-pc-linux-gnu.  pushed.

Andrew



commit c86c95edd165d674614516cda0b1fcb6616c1096
Author: Andrew MacLeod <amacl...@redhat.com>
Date:   Mon Aug 9 15:53:42 2021 -0400

    Ensure toupper and tolower follow the expected pattern.
    
    If the parameter is not compatible with the LHS, assume this is not really a
    builtin function to avoid a trap.
    
            gcc/
            PR tree-optimization/101741
            * gimple-range-fold.cc (fold_using_range::range_of_builtin_call): Check
            type of parameter for toupper/tolower.
    
            gcc/testsuite/
            * gcc.dg/pr101741.c: New.

diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc
index 410bc4ddca4..d3e3e14ff64 100644
--- a/gcc/gimple-range-fold.cc
+++ b/gcc/gimple-range-fold.cc
@@ -894,6 +894,9 @@ fold_using_range::range_of_builtin_call (irange &r, gcall *call,
     case CFN_BUILT_IN_TOUPPER:
       {
 	arg = gimple_call_arg (call, 0);
+	// If the argument isn't compatible with the LHS, do nothing.
+	if (!range_compatible_p (type, TREE_TYPE (arg)))
+	  return false;
 	if (!src.get_operand (r, arg))
 	  return false;
 
@@ -913,6 +916,9 @@ fold_using_range::range_of_builtin_call (irange &r, gcall *call,
      case CFN_BUILT_IN_TOLOWER:
       {
 	arg = gimple_call_arg (call, 0);
+	// If the argument isn't compatible with the LHS, do nothing.
+	if (!range_compatible_p (type, TREE_TYPE (arg)))
+	  return false;
 	if (!src.get_operand (r, arg))
 	  return false;
 
diff --git a/gcc/testsuite/gcc.dg/pr101741.c b/gcc/testsuite/gcc.dg/pr101741.c
new file mode 100644
index 00000000000..6587dca77d5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101741.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/101741 */
+/* { dg-do compile } */
+/* { dg-options "-O2 " } */
+
+int
+foo (void);
+
+unsigned int
+toupper (int c)
+{
+  c = foo ();
+  while (c)
+    c = toupper (c);
+
+  return c;
+}

Reply via email to