]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add overflow API for plus minus mult on range
authorJiufu Guo <guojiufu@linux.ibm.com>
Wed, 30 Aug 2023 08:59:16 +0000 (16:59 +0800)
committerguojiufu <guojiufu@linux.ibm.com>
Thu, 31 Aug 2023 01:56:44 +0000 (09:56 +0800)
In previous reviews, adding overflow APIs to range-op would be useful.
Those APIs could help to check if overflow happens when operating
between two 'range's, like: plus, minus, and mult.

Previous discussions are here:
https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624067.html
https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624701.html

gcc/ChangeLog:

* range-op-mixed.h (operator_plus::overflow_free_p): New declare.
(operator_minus::overflow_free_p): New declare.
(operator_mult::overflow_free_p): New declare.
* range-op.cc (range_op_handler::overflow_free_p): New function.
(range_operator::overflow_free_p): New default function.
(operator_plus::overflow_free_p): New function.
(operator_minus::overflow_free_p): New function.
(operator_mult::overflow_free_p): New function.
* range-op.h (range_op_handler::overflow_free_p): New declare.
(range_operator::overflow_free_p): New declare.
* value-range.cc (irange::nonnegative_p): New function.
(irange::nonpositive_p): New function.
* value-range.h (irange::nonnegative_p): New declare.
(irange::nonpositive_p): New declare.

gcc/range-op-mixed.h
gcc/range-op.cc
gcc/range-op.h
gcc/value-range.cc
gcc/value-range.h

index 825f934cf603488b99146e20d3e5111972cf73d0..ef562326c1f770707f4184f7fae2434d5fdf07f1 100644 (file)
@@ -403,6 +403,10 @@ public:
                                  relation_kind rel) const final override;
   void update_bitmask (irange &r, const irange &lh,
                       const irange &rh) const final override;
+
+  virtual bool overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio = TRIO_VARYING) const;
+
 private:
   void wi_fold (irange &r, tree type, const wide_int &lh_lb,
                const wide_int &lh_ub, const wide_int &rh_lb,
@@ -468,6 +472,10 @@ public:
                                relation_kind rel) const final override;
   void update_bitmask (irange &r, const irange &lh,
                       const irange &rh) const final override;
+
+  virtual bool overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio = TRIO_VARYING) const;
+
 private:
   void wi_fold (irange &r, tree type, const wide_int &lh_lb,
                const wide_int &lh_ub, const wide_int &rh_lb,
@@ -547,6 +555,9 @@ public:
                const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub,
                const REAL_VALUE_TYPE &rh_lb, const REAL_VALUE_TYPE &rh_ub,
                relation_kind kind) const final override;
+  virtual bool overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio = TRIO_VARYING) const;
+
 };
 
 class operator_addr_expr : public range_operator
index 268f6b6f02508456c7eba0236cf3a4cc0c14dece..619979f2f61da98fde27ea3fdf0ec753b33cb247 100644 (file)
@@ -377,6 +377,22 @@ range_op_handler::op1_op2_relation (const vrange &lhs,
     }
 }
 
+bool
+range_op_handler::overflow_free_p (const vrange &lh,
+                                  const vrange &rh,
+                                  relation_trio rel) const
+{
+  gcc_checking_assert (m_operator);
+  switch (dispatch_kind (lh, lh, rh))
+    {
+      case RO_III:
+       return m_operator->overflow_free_p(as_a <irange> (lh),
+                                          as_a <irange> (rh),
+                                          rel);
+      default:
+       return false;
+    }
+}
 
 // Update the known bitmasks in R when applying the operation CODE to
 // LH and RH.
@@ -706,6 +722,13 @@ range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
   return false;
 }
 
+bool
+range_operator::overflow_free_p (const irange &, const irange &,
+                                relation_trio) const
+{
+  return false;
+}
+
 // Apply any known bitmask updates based on this operator.
 
 void
@@ -4364,6 +4387,107 @@ range_op_table::initialize_integral_ops ()
 
 }
 
+bool
+operator_plus::overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio) const
+{
+  if (lh.undefined_p () || rh.undefined_p ())
+    return false;
+
+  tree type = lh.type ();
+  if (TYPE_OVERFLOW_UNDEFINED (type))
+    return true;
+
+  wi::overflow_type ovf;
+  signop sgn = TYPE_SIGN (type);
+  wide_int wmax0 = lh.upper_bound ();
+  wide_int wmax1 = rh.upper_bound ();
+  wi::add (wmax0, wmax1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  if (TYPE_UNSIGNED (type))
+    return true;
+
+  wide_int wmin0 = lh.lower_bound ();
+  wide_int wmin1 = rh.lower_bound ();
+  wi::add (wmin0, wmin1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  return true;
+}
+
+bool
+operator_minus::overflow_free_p (const irange &lh, const irange &rh,
+                                relation_trio) const
+{
+  if (lh.undefined_p () || rh.undefined_p ())
+    return false;
+
+  tree type = lh.type ();
+  if (TYPE_OVERFLOW_UNDEFINED (type))
+    return true;
+
+  wi::overflow_type ovf;
+  signop sgn = TYPE_SIGN (type);
+  wide_int wmin0 = lh.lower_bound ();
+  wide_int wmax1 = rh.upper_bound ();
+  wi::sub (wmin0, wmax1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  if (TYPE_UNSIGNED (type))
+    return true;
+
+  wide_int wmax0 = lh.upper_bound ();
+  wide_int wmin1 = rh.lower_bound ();
+  wi::sub (wmax0, wmin1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  return true;
+}
+
+bool
+operator_mult::overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio) const
+{
+  if (lh.undefined_p () || rh.undefined_p ())
+    return false;
+
+  tree type = lh.type ();
+  if (TYPE_OVERFLOW_UNDEFINED (type))
+    return true;
+
+  wi::overflow_type ovf;
+  signop sgn = TYPE_SIGN (type);
+  wide_int wmax0 = lh.upper_bound ();
+  wide_int wmax1 = rh.upper_bound ();
+  wi::mul (wmax0, wmax1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  if (TYPE_UNSIGNED (type))
+    return true;
+
+  wide_int wmin0 = lh.lower_bound ();
+  wide_int wmin1 = rh.lower_bound ();
+  wi::mul (wmin0, wmin1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  wi::mul (wmin0, wmax1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  wi::mul (wmax0, wmin1, sgn, &ovf);
+  if (ovf != wi::OVF_NONE)
+    return false;
+
+  return true;
+}
+
 #if CHECKING_P
 #include "selftest.h"
 
index 51121410233f73ad715ae38296f186d672c77eee..21d401cc83d93acba33dfe18782412d971a2e5c4 100644 (file)
@@ -154,6 +154,9 @@ public:
   virtual relation_kind op1_op2_relation (const frange &lhs,
                                          const frange &op1,
                                          const frange &op2) const;
+
+  virtual bool overflow_free_p (const irange &lh, const irange &rh,
+                               relation_trio = TRIO_VARYING) const;
 protected:
   // Perform an integral operation between 2 sub-ranges and return it.
   virtual void wi_fold (irange &r, tree type,
@@ -223,6 +226,8 @@ public:
   relation_kind op1_op2_relation (const vrange &lhs,
                                  const vrange &op1,
                                  const vrange &op2) const;
+  bool overflow_free_p (const vrange &lh, const vrange &rh,
+                       relation_trio = TRIO_VARYING) const;
 protected:
   unsigned dispatch_kind (const vrange &lhs, const vrange &op1,
                          const vrange& op2) const;
index 60180c80e55b19c322c31e92f2ad5660cc27633c..391cef64cd13512d34fc2a53ffac7bba8bf9dfee 100644 (file)
@@ -313,6 +313,18 @@ add_vrange (const vrange &v, inchash::hash &hstate,
 
 } //namespace inchash
 
+bool
+irange::nonnegative_p () const
+{
+  return wi::ge_p (lower_bound (), 0, TYPE_SIGN (type ()));
+}
+
+bool
+irange::nonpositive_p () const
+{
+  return wi::le_p (upper_bound (), 0, TYPE_SIGN (type ()));
+}
+
 bool
 irange::supports_type_p (const_tree type) const
 {
index 622b68863d26b00b000b47f74be1699b34f8fbed..6c5be36d74d2167c02b143076f7cc002c6f48699 100644 (file)
@@ -288,6 +288,8 @@ public:
   virtual bool singleton_p (tree *result = NULL) const override;
   bool singleton_p (wide_int &) const;
   bool contains_p (const wide_int &) const;
+  bool nonnegative_p () const;
+  bool nonpositive_p () const;
 
   // In-place operators.
   virtual bool union_ (const vrange &) override;