From ec80adf4173f3626e6c7931a9c1ba3f760cb2364 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Thu, 22 Sep 2022 17:55:56 -0400
Subject: [PATCH 4/6] Track value_relations in GORI.

This allows GORI to recognize and pass relations along the calculation chain.
This will allow relations between the LHS and the operand being calculated
to be utilized in op1_range and op2_range.

	* gimple-range-gori.cc (ori_compute::compute_operand_range):
	Create a relation record and pass it along when possible.
	(gori_compute::compute_operand1_range): Pass relation along.
	(gori_compute::compute_operand2_range): Ditto.
	(gori_compute::compute_operand1_and_operand2_range): Ditto.
	* gimple-range-gori.h (class gori_compute): Adjust prototypes.
	* gimple-range-op.cc (gimple_range_op_handler::calc_op1): Pass
	relation to op1_range call.
	(gimple_range_op_handler::calc_op2): Pass relation to op2_range call.
	* gimple-range-op.h (class gimple_range_op_handler): Adjust
	prototypes.
---
 gcc/gimple-range-gori.cc | 42 +++++++++++++++++++++++++++-------------
 gcc/gimple-range-gori.h  | 14 ++++++++++----
 gcc/gimple-range-op.cc   | 12 ++++++------
 gcc/gimple-range-op.h    |  6 ++++--
 4 files changed, 49 insertions(+), 25 deletions(-)

diff --git a/gcc/gimple-range-gori.cc b/gcc/gimple-range-gori.cc
index 40b2f2f6ae9..57a7e820749 100644
--- a/gcc/gimple-range-gori.cc
+++ b/gcc/gimple-range-gori.cc
@@ -603,8 +603,10 @@ gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
 bool
 gori_compute::compute_operand_range (vrange &r, gimple *stmt,
 				     const vrange &lhs, tree name,
-				     fur_source &src)
+				     fur_source &src, value_relation *rel)
 {
+  value_relation vrel;
+  value_relation *vrel_ptr = rel;
   // If the lhs doesn't tell us anything, neither will unwinding further.
   if (lhs.varying_p ())
     return false;
@@ -625,11 +627,23 @@ gori_compute::compute_operand_range (vrange &r, gimple *stmt,
   tree op1 = gimple_range_ssa_p (handler.operand1 ());
   tree op2 = gimple_range_ssa_p (handler.operand2 ());
 
+  // If there is a relation, use it instead of any passed in.  This will allow
+  // multiple relations to be processed in compound logicals.
+  if (op1 && op2)
+    {
+      relation_kind k = handler.op1_op2_relation (lhs);
+      if (k != VREL_VARYING)
+       {
+	 vrel.set_relation (k, op1, op2);
+	 vrel_ptr = &vrel;
+       }
+    }
+
   // Handle end of lookup first.
   if (op1 == name)
-    return compute_operand1_range (r, handler, lhs, name, src);
+    return compute_operand1_range (r, handler, lhs, name, src, vrel_ptr);
   if (op2 == name)
-    return compute_operand2_range (r, handler, lhs, name, src);
+    return compute_operand2_range (r, handler, lhs, name, src, vrel_ptr);
 
   // NAME is not in this stmt, but one of the names in it ought to be
   // derived from it.
@@ -672,11 +686,12 @@ gori_compute::compute_operand_range (vrange &r, gimple *stmt,
     }
   // Follow the appropriate operands now.
   else if (op1_in_chain && op2_in_chain)
-    res = compute_operand1_and_operand2_range (r, handler, lhs, name, src);
+    res = compute_operand1_and_operand2_range (r, handler, lhs, name, src,
+					       vrel_ptr);
   else if (op1_in_chain)
-    res = compute_operand1_range (r, handler, lhs, name, src);
+    res = compute_operand1_range (r, handler, lhs, name, src, vrel_ptr);
   else if (op2_in_chain)
-    res = compute_operand2_range (r, handler, lhs, name, src);
+    res = compute_operand2_range (r, handler, lhs, name, src, vrel_ptr);
   else
     gcc_unreachable ();
 
@@ -927,7 +942,7 @@ bool
 gori_compute::compute_operand1_range (vrange &r,
 				      gimple_range_op_handler &handler,
 				      const vrange &lhs, tree name,
-				      fur_source &src)
+				      fur_source &src, value_relation *rel)
 {
   gimple *stmt = handler.stmt ();
   tree op1 = handler.operand1 ();
@@ -998,7 +1013,7 @@ gori_compute::compute_operand1_range (vrange &r,
   gcc_checking_assert (src_stmt);
 
   // Then feed this range back as the LHS of the defining statement.
-  return compute_operand_range (r, src_stmt, op1_range, name, src);
+  return compute_operand_range (r, src_stmt, op1_range, name, src, rel);
 }
 
 
@@ -1010,7 +1025,7 @@ bool
 gori_compute::compute_operand2_range (vrange &r,
 				      gimple_range_op_handler &handler,
 				      const vrange &lhs, tree name,
-				      fur_source &src)
+				      fur_source &src, value_relation *rel)
 {
   gimple *stmt = handler.stmt ();
   tree op1 = handler.operand1 ();
@@ -1070,7 +1085,7 @@ gori_compute::compute_operand2_range (vrange &r,
 //  gcc_checking_assert (!is_import_p (op2, find.bb));
 
   // Then feed this range back as the LHS of the defining statement.
-  return compute_operand_range (r, src_stmt, op2_range, name, src);
+  return compute_operand_range (r, src_stmt, op2_range, name, src, rel);
 }
 
 // Calculate a range for NAME from both operand positions of S
@@ -1083,17 +1098,18 @@ gori_compute::compute_operand1_and_operand2_range (vrange &r,
 								     &handler,
 						   const vrange &lhs,
 						   tree name,
-						   fur_source &src)
+						   fur_source &src,
+						   value_relation *rel)
 {
   Value_Range op_range (TREE_TYPE (name));
 
   // Calculate a good a range for op2.  Since op1 == op2, this will
   // have already included whatever the actual range of name is.
-  if (!compute_operand2_range (op_range, handler, lhs, name, src))
+  if (!compute_operand2_range (op_range, handler, lhs, name, src, rel))
     return false;
 
   // Now get the range thru op1.
-  if (!compute_operand1_range (r, handler, lhs, name, src))
+  if (!compute_operand1_range (r, handler, lhs, name, src, rel))
     return false;
 
   // Both operands have to be simultaneously true, so perform an intersection.
diff --git a/gcc/gimple-range-gori.h b/gcc/gimple-range-gori.h
index 0c776ef853f..1fff3e6255a 100644
--- a/gcc/gimple-range-gori.h
+++ b/gcc/gimple-range-gori.h
@@ -153,6 +153,8 @@ private:
 //
 // The remaining routines are internal use only.
 
+class value_relation;
+
 class gori_compute : public gori_map
 {
 public:
@@ -167,17 +169,21 @@ private:
   bool may_recompute_p (tree name, edge e);
   bool may_recompute_p (tree name, basic_block bb = NULL);
   bool compute_operand_range (vrange &r, gimple *stmt, const vrange &lhs,
-			      tree name, class fur_source &src);
+			      tree name, class fur_source &src,
+			      value_relation *rel = NULL);
   bool compute_operand_range_switch (vrange &r, gswitch *s, const vrange &lhs,
 				     tree name, fur_source &src);
   bool compute_operand1_range (vrange &r, gimple_range_op_handler &handler,
-			       const vrange &lhs, tree name, fur_source &src);
+			       const vrange &lhs, tree name, fur_source &src,
+			       value_relation *rel = NULL);
   bool compute_operand2_range (vrange &r, gimple_range_op_handler &handler,
-			       const vrange &lhs, tree name, fur_source &src);
+			       const vrange &lhs, tree name, fur_source &src,
+			       value_relation *rel = NULL);
   bool compute_operand1_and_operand2_range (vrange &r,
 					    gimple_range_op_handler &handler,
 					    const vrange &lhs, tree name,
-					    fur_source &src);
+					    fur_source &src,
+					    value_relation *rel = NULL);
   void compute_logical_operands (vrange &true_range, vrange &false_range,
 				 gimple_range_op_handler &handler,
 				 const irange &lhs, tree name, fur_source &src,
diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index d7c6dfa933d..f6d2053f156 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -202,7 +202,7 @@ gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range)
 
 bool
 gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range,
-				   const vrange &op2_range)
+				   const vrange &op2_range, relation_kind k)
 {
   // Give up on empty ranges.
   if (lhs_range.undefined_p ())
@@ -225,9 +225,9 @@ gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range,
 	op2_type = TREE_TYPE (operand1 ());
       Value_Range trange (op2_type);
       trange.set_varying (op2_type);
-      return op1_range (r, type, lhs_range, trange);
+      return op1_range (r, type, lhs_range, trange, k);
     }
-  return op1_range (r, type, lhs_range, op2_range);
+  return op1_range (r, type, lhs_range, op2_range, k);
 }
 
 // Calculate what we can determine of the range of this statement's
@@ -237,7 +237,7 @@ gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range,
 
 bool
 gimple_range_op_handler::calc_op2 (vrange &r, const vrange &lhs_range,
-				   const vrange &op1_range)
+				   const vrange &op1_range, relation_kind k)
 {
   // Give up on empty ranges.
   if (lhs_range.undefined_p ())
@@ -250,9 +250,9 @@ gimple_range_op_handler::calc_op2 (vrange &r, const vrange &lhs_range,
       tree op1_type = TREE_TYPE (operand1 ());
       Value_Range trange (op1_type);
       trange.set_varying (op1_type);
-      return op2_range (r, type, lhs_range, trange);
+      return op2_range (r, type, lhs_range, trange, k);
     }
-  return op2_range (r, type, lhs_range, op1_range);
+  return op2_range (r, type, lhs_range, op1_range, k);
 }
 
 // --------------------------------------------------------------------
diff --git a/gcc/gimple-range-op.h b/gcc/gimple-range-op.h
index 68764198bc0..3a555f29a65 100644
--- a/gcc/gimple-range-op.h
+++ b/gcc/gimple-range-op.h
@@ -35,8 +35,10 @@ public:
   tree operand1 () const { gcc_checking_assert (m_valid); return m_op1; }
   tree operand2 () const { gcc_checking_assert (m_valid); return m_op2; }
   bool calc_op1 (vrange &r, const vrange &lhs_range);
-  bool calc_op1 (vrange &r, const vrange &lhs_range, const vrange &op2_range);
-  bool calc_op2 (vrange &r, const vrange &lhs_range, const vrange &op1_range);
+  bool calc_op1 (vrange &r, const vrange &lhs_range, const vrange &op2_range,
+		 relation_kind k = VREL_VARYING);
+  bool calc_op2 (vrange &r, const vrange &lhs_range, const vrange &op1_range,
+		 relation_kind k = VREL_VARYING);
 private:
   void maybe_builtin_call ();
   gimple *m_stmt;
-- 
2.37.3

