diff mbox series

[11/17] Convert CFN_CLZ builtins to range-ops.

Message ID e2ad2282-85ff-da6b-970a-66e63c925957@redhat.com
State New
Headers show
Series Move builtin functions to range-ops. | expand

Commit Message

Andrew MacLeod Sept. 22, 2022, 7:05 p.m. UTC
Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed.

Andrew
diff mbox series

Patch

From ae1669a98656cca594fcd2fef6bd2cd7308a361f Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Tue, 20 Sep 2022 18:12:25 -0400
Subject: [PATCH 11/17] Convert CFN_CLZ builtins to range-ops.

	* gimple-range-fold.cc (range_of_builtin_int_call): Remove case
	for CFN_CLZ.
	* gimple-range-op.cc (class cfn_clz): New.
	(gimple_range_op_handler::maybe_builtin_call): Set arguments.
---
 gcc/gimple-range-fold.cc | 61 -----------------------------
 gcc/gimple-range-op.cc   | 84 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 61 deletions(-)

diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc
index ca531037e13..63eaa90be96 100644
--- a/gcc/gimple-range-fold.cc
+++ b/gcc/gimple-range-fold.cc
@@ -926,67 +926,6 @@  fold_using_range::range_of_builtin_int_call (irange &r, gcall *call,
       r.set (build_zero_cst (type), build_one_cst (type));
       return true;
 
-    CASE_CFN_CLZ:
-      // __builtin_c[lt]z* return [0, prec-1], except when the
-      // argument is 0, but that is undefined behavior.
-      //
-      // For __builtin_c[lt]z* consider argument of 0 always undefined
-      // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
-      arg = gimple_call_arg (call, 0);
-      prec = TYPE_PRECISION (TREE_TYPE (arg));
-      mini = 0;
-      maxi = prec - 1;
-      mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
-      if (gimple_call_internal_p (call))
-	{
-	  if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
-	      && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
-	    {
-	      // Only handle the single common value.
-	      if (zerov == prec)
-		maxi = prec;
-	      else
-		// Magic value to give up, unless we can prove arg is non-zero.
-		mini = -2;
-	    }
-	}
-
-      src.get_operand (r, arg);
-      // From clz of minimum we can compute result maximum.
-      if (!r.undefined_p ())
-	{
-	  // From clz of minimum we can compute result maximum.
-	  if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
-	    {
-	      maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
-	      if (mini == -2)
-		mini = 0;
-	    }
-	  else if (!range_includes_zero_p (&r))
-	    {
-	      mini = 0;
-	      maxi = prec - 1;
-	    }
-	  if (mini == -2)
-	    break;
-	  // From clz of maximum we can compute result minimum.
-	  wide_int max = r.upper_bound ();
-	  int newmini = prec - 1 - wi::floor_log2 (max);
-	  if (max == 0)
-	    {
-	      // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
-	      // return [prec, prec], otherwise ignore the range.
-	      if (maxi == prec)
-		mini = prec;
-	    }
-	  else
-	    mini = newmini;
-	}
-      if (mini == -2)
-	break;
-      r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
-      return true;
-
     CASE_CFN_CTZ:
       // __builtin_ctz* return [0, prec-1], except for when the
       // argument is 0, but that is undefined behavior.
diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index 84837f8ee43..caba49309f9 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -414,6 +414,81 @@  public:
   }
 } op_cfn_popcount;
 
+// Implement range operator for CFN_BUILT_IN_CLZ
+class cfn_clz : public range_operator
+{
+public:
+  cfn_clz (bool internal) { m_gimple_call_internal_p = internal; }
+  using range_operator::fold_range;
+  virtual bool fold_range (irange &r, tree type, const irange &lh,
+			   const irange &, relation_kind) const;
+private:
+  bool m_gimple_call_internal_p;
+} op_cfn_clz (false), op_cfn_clz_internal (true);
+
+bool
+cfn_clz::fold_range (irange &r, tree type, const irange &lh,
+		     const irange &, relation_kind) const
+{
+  // __builtin_c[lt]z* return [0, prec-1], except when the
+  // argument is 0, but that is undefined behavior.
+  //
+  // For __builtin_c[lt]z* consider argument of 0 always undefined
+  // behavior, for internal fns depending on C?Z_DEFINED_ALUE_AT_ZERO.
+  if (lh.undefined_p ())
+    return false;
+  int prec = TYPE_PRECISION (lh.type ());
+  int mini = 0;
+  int maxi = prec - 1;
+  int zerov = 0;
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (lh.type ());
+  if (m_gimple_call_internal_p)
+    {
+      if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
+	  && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
+	{
+	  // Only handle the single common value.
+	  if (zerov == prec)
+	    maxi = prec;
+	  else
+	    // Magic value to give up, unless we can prove arg is non-zero.
+	    mini = -2;
+	}
+    }
+
+  // From clz of minimum we can compute result maximum.
+  if (wi::gt_p (lh.lower_bound (), 0, TYPE_SIGN (lh.type ())))
+    {
+      maxi = prec - 1 - wi::floor_log2 (lh.lower_bound ());
+      if (mini == -2)
+	mini = 0;
+    }
+  else if (!range_includes_zero_p (&lh))
+    {
+      mini = 0;
+      maxi = prec - 1;
+    }
+  if (mini == -2)
+    return false;
+  // From clz of maximum we can compute result minimum.
+  wide_int max = lh.upper_bound ();
+  int newmini = prec - 1 - wi::floor_log2 (max);
+  if (max == 0)
+    {
+      // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
+      // return [prec, prec], otherwise ignore the range.
+      if (maxi == prec)
+	mini = prec;
+    }
+  else
+    mini = newmini;
+
+  if (mini == -2)
+    return false;
+  r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
+  return true;
+}
+
 // Set up a gimple_range_op_handler for any built in function which can be
 // supported via range-ops.
 
@@ -469,6 +544,15 @@  gimple_range_op_handler::maybe_builtin_call ()
       m_valid = true;
       break;
 
+    CASE_CFN_CLZ:
+      m_op1 = gimple_call_arg (call, 0);
+      m_valid = true;
+      if (gimple_call_internal_p (call))
+	m_int = &op_cfn_clz_internal;
+      else
+	m_int = &op_cfn_clz;
+      break;
+
     default:
       break;
     }
-- 
2.37.3