From 0c56557e8e8d5c24cf3aa7c3abede95ab2f83f66 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 15 Jul 2019 05:25:57 -0400
Subject: [PATCH 086/108] FIXME: cleanups

---
 gcc/analyzer/constraint-manager.cc | 193 ++++++++++++++++++++++++-------------
 gcc/analyzer/constraint-manager.h  |  44 ++-------
 2 files changed, 137 insertions(+), 100 deletions(-)

diff --git a/gcc/analyzer/constraint-manager.cc b/gcc/analyzer/constraint-manager.cc
index 5336f12..236037b 100644
--- a/gcc/analyzer/constraint-manager.cc
+++ b/gcc/analyzer/constraint-manager.cc
@@ -62,6 +62,38 @@ along with GCC; see the file COPYING3.  If not see
 #include "analyzer/constraint-manager.h"
 #include "tree-diagnostic.h"
 
+/* One of the end-points of a range.  */
+
+struct bound
+{
+  bound () : m_constant (NULL_TREE), m_closed (false) {}
+  bound (tree constant, bool closed)
+  : m_constant (constant), m_closed (closed) {}
+
+  void ensure_closed (bool is_upper);
+
+  tree m_constant;
+  bool m_closed;
+};
+
+/* FIXME.  */
+
+struct range
+{
+  range () : m_lower_bound (), m_upper_bound () {}
+  range (const bound &lower, const bound &upper)
+  : m_lower_bound (lower), m_upper_bound (upper) {}
+
+  std::string to_str () const;
+
+  bool constrained_to_single_element (tree *out);
+
+  bound m_lower_bound;
+  bound m_upper_bound;
+};
+
+////////////////////////////////////////////////////////////////////////////
+
 /* A subclass of purge_criteria: purge a specific svalue_id.  */
 
 class purge_one_svalue_id : public purge_criteria
@@ -78,6 +110,83 @@ private:
   svalue_id m_victim;
 };
 
+////////////////////////////////////////////////////////////////////////////
+
+/* struct bound.  */
+
+/* FIXME.  */
+
+void
+bound::ensure_closed (bool is_upper)
+{
+  if (!m_closed)
+    {
+      /* Offset by 1 in the appropriate direction.
+	 For example, convert 3 < x into 4 <= x,
+	 and convert x < 5 into x <= 4.  */
+      gcc_assert (CONSTANT_CLASS_P (m_constant));
+      m_constant = fold_build2 (is_upper ? MINUS_EXPR : PLUS_EXPR,
+				TREE_TYPE (m_constant),
+				m_constant, integer_one_node);
+      gcc_assert (CONSTANT_CLASS_P (m_constant));
+      m_closed = true;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////
+
+/* struct range.  */
+
+/* FIXME.  */
+
+std::string
+range::to_str () const
+{
+  std::string result = expr_to_string (m_lower_bound.m_constant);
+  result += " <";
+  if (m_lower_bound.m_closed)
+    result += "=";
+  result += " x <";
+  if (m_upper_bound.m_closed)
+    result += "=";
+  result += " ";
+  result += expr_to_string (m_upper_bound.m_constant);
+  return result;  // TODO
+}
+
+/* FIXME.  */
+
+bool
+range::constrained_to_single_element (tree *out)
+{
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (m_lower_bound.m_constant)))
+    return false;
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (m_upper_bound.m_constant)))
+    return false;
+  // FIXME: also check the endpoint
+
+  /* Convert any open bounds to closed bounds.  */
+  m_lower_bound.ensure_closed (false);
+  m_upper_bound.ensure_closed (true);
+
+  // Are they equal?
+  tree comparison
+    = fold_build2 (EQ_EXPR, boolean_type_node,
+		   m_lower_bound.m_constant,
+		   m_upper_bound.m_constant);
+  if (comparison == boolean_true_node)
+    {
+      *out = m_lower_bound.m_constant;
+      return true;
+    }
+  else
+    return false;
+}
+
+////////////////////////////////////////////////////////////////////////////
+
+/* class equiv_class.  */
+
 /* FIXME.  */
 
 equiv_class::equiv_class ()
@@ -177,6 +286,7 @@ equiv_class::operator== (const equiv_class &other)
 void
 equiv_class::add (svalue_id sid, const constraint_manager &cm)
 {
+  gcc_assert (!sid.null_p ());
   if (tree cst = cm.maybe_get_constant (sid))
     {
       gcc_assert (m_constant == NULL_TREE);
@@ -200,6 +310,7 @@ equiv_class::add (svalue_id sid, const constraint_manager &cm)
 bool
 equiv_class::del (svalue_id sid)
 {
+  gcc_assert (!sid.null_p ());
   gcc_assert (sid != m_cst_sid);
 
   int i;
@@ -246,6 +357,8 @@ equiv_class::remap_svalue_ids (const svalue_id_map &map)
   map.update (&m_cst_sid);
 }
 
+////////////////////////////////////////////////////////////////////////////
+
 /* FIXME.  */
 
 const char *
@@ -294,6 +407,10 @@ eval_constraint_op_for_op (enum constraint_op c_op, enum tree_code t_op)
   return tristate (tristate::TS_UNKNOWN);
 }
 
+////////////////////////////////////////////////////////////////////////////
+
+/* class constraint.  */
+
 /* FIXME.  */
 
 std::string
@@ -334,6 +451,10 @@ constraint::operator== (const constraint &other) const
   return true;
 }
 
+////////////////////////////////////////////////////////////////////////////
+
+/* class equiv_class_id.  */
+
 /* FIXME.  */
 
 const equiv_class &
@@ -367,6 +488,10 @@ equiv_class_id::to_str () const
   return pp_formatted_text (&pp);
 }
 
+////////////////////////////////////////////////////////////////////////////
+
+/* class constraint_manager.  */
+
 /* FIXME.  */
 
 constraint_manager::constraint_manager (const constraint_manager &other)
@@ -640,71 +765,7 @@ constraint_manager::add_constraint (equiv_class_id lhs_ec_id,
   return true;
 }
 
-/* FIXME.  */
-
-void
-bound::ensure_closed (bool is_upper)
-{
-  if (!m_closed)
-    {
-      /* Offset by 1 in the appropriate direction.
-	 For example, convert 3 < x into 4 <= x,
-	 and convert x < 5 into x <= 4.  */
-      gcc_assert (CONSTANT_CLASS_P (m_constant));
-      m_constant = fold_build2 (is_upper ? MINUS_EXPR : PLUS_EXPR,
-				TREE_TYPE (m_constant),
-				m_constant, integer_one_node);
-      gcc_assert (CONSTANT_CLASS_P (m_constant));
-      m_closed = true;
-    }
-}
-
-/* FIXME.  */
-
-std::string
-range::to_str () const
-{
-  std::string result = expr_to_string (m_lower_bound.m_constant);
-  result += " <";
-  if (m_lower_bound.m_closed)
-    result += "=";
-  result += " x <";
-  if (m_upper_bound.m_closed)
-    result += "=";
-  result += " ";
-  result += expr_to_string (m_upper_bound.m_constant);
-  return result;  // TODO
-}
-
-/* FIXME.  */
-
-bool
-range::constrained_to_single_element (tree *out)
-{
-  if (!INTEGRAL_TYPE_P (TREE_TYPE (m_lower_bound.m_constant)))
-    return false;
-  if (!INTEGRAL_TYPE_P (TREE_TYPE (m_upper_bound.m_constant)))
-    return false;
-  // FIXME: also check the endpoint
-
-  /* Convert any open bounds to closed bounds.  */
-  m_lower_bound.ensure_closed (false);
-  m_upper_bound.ensure_closed (true);
-
-  // Are they equal?
-  tree comparison
-    = fold_build2 (EQ_EXPR, boolean_type_node,
-		   m_lower_bound.m_constant,
-		   m_upper_bound.m_constant);
-  if (comparison == boolean_true_node)
-    {
-      *out = m_lower_bound.m_constant;
-      return true;
-    }
-  else
-    return false;
-}
-
+////////////////////////////////////////////////////////////////////////////
 
 /* FIXME.  */
 
@@ -1389,6 +1450,8 @@ debug (const constraint_manager &cm)
   fprintf (stderr, "%s\n", cm.to_str ().c_str ());
 }
 
+////////////////////////////////////////////////////////////////////////////
+
 #if CHECKING_P
 
 namespace selftest {
diff --git a/gcc/analyzer/constraint-manager.h b/gcc/analyzer/constraint-manager.h
index deaf471..e84161c 100644
--- a/gcc/analyzer/constraint-manager.h
+++ b/gcc/analyzer/constraint-manager.h
@@ -29,36 +29,6 @@ class return_superedge;
 
 class constraint_manager;
 
-/* One of the end-points of a range.  */
-
-struct bound
-{
-  bound () : m_constant (NULL_TREE), m_closed (false) {}
-  bound (tree constant, bool closed)
-  : m_constant (constant), m_closed (closed) {}
-
-  void ensure_closed (bool is_upper);
-
-  tree m_constant;
-  bool m_closed;
-};
-
-/* FIXME.  */
-
-struct range
-{
-  range () : m_lower_bound (), m_upper_bound () {}
-  range (const bound &lower, const bound &upper)
-  : m_lower_bound (lower), m_upper_bound (upper) {}
-
-  std::string to_str () const;
-
-  bool constrained_to_single_element (tree *out);
-
-  bound m_lower_bound;
-  bound m_upper_bound;
-};
-
 /* Abstract base class for specifying how state should be purged.  */
 
 class purge_criteria
@@ -68,7 +38,9 @@ public:
   virtual bool should_purge_p (svalue_id sid) const = 0;
 };
 
-/* FIXME.  */
+/* An equivalence class within a constraint manager: a set of
+   svalue_ids that are known to all be equal to each other,
+   together with an optional tree constant that they are equal to.  */
 
 class equiv_class
 {
@@ -112,7 +84,8 @@ enum constraint_op
 
 const char *constraint_op_code (enum constraint_op c_op);
 
-/* FIXME.  */
+/* An ID for an equiv_class within a constraint_manager.  Internally, this
+   is an index into a vector of equiv_class * within the constraint_manager.  */
 
 class equiv_class_id
 {
@@ -149,9 +122,10 @@ public:
   int m_idx;
 };
 
-/* FIXME.  */
+/* A relationship between two equivalence classes in a constraint_manager.  */
 
-class constraint {
+class constraint
+{
  public:
   constraint (equiv_class_id lhs, enum constraint_op c_op, equiv_class_id rhs)
   : m_lhs (lhs), m_op (c_op), m_rhs (rhs)
@@ -175,7 +149,7 @@ class constraint {
   equiv_class_id m_rhs;
 };
 
-/* FIXME.  */
+/* A collection of equivalence classes and constraints on them.  */
 
 class constraint_manager
 {
-- 
1.8.5.3

