diff mbox series

[v3,2/8] tree-object-size: Abstract object_sizes array

Message ID 20211126052851.2176408-3-siddhesh@gotplt.org
State New
Headers show
Series __builtin_dynamic_object_size | expand

Commit Message

Siddhesh Poyarekar Nov. 26, 2021, 5:28 a.m. UTC
Put all accesses to object_sizes behind functions so that we can add
dynamic capability more easily.

gcc/ChangeLog:

	* tree-object-size.c (object_sizes_grow, object_sizes_release,
	object_sizes_unknown_p, object_sizes_get, object_size_set_force,
	object_sizes_set): New functions.
	(addr_object_size, compute_builtin_object_size,
	expr_object_size, call_object_size, unknown_object_size,
	merge_object_sizes, plus_stmt_object_size,
	cond_expr_object_size, collect_object_sizes_for,
	check_for_plus_in_loops_1, init_object_sizes,
	fini_object_sizes): Adjust.

Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
---
Changes from v2:

- Incorporated review suggestions.

 gcc/tree-object-size.c | 177 +++++++++++++++++++++++------------------
 1 file changed, 98 insertions(+), 79 deletions(-)

Comments

Jakub Jelinek Nov. 26, 2021, 4:47 p.m. UTC | #1
On Fri, Nov 26, 2021 at 10:58:45AM +0530, Siddhesh Poyarekar wrote:
> Put all accesses to object_sizes behind functions so that we can add
> dynamic capability more easily.
> 
> gcc/ChangeLog:
> 
> 	* tree-object-size.c (object_sizes_grow, object_sizes_release,
> 	object_sizes_unknown_p, object_sizes_get, object_size_set_force,
> 	object_sizes_set): New functions.
> 	(addr_object_size, compute_builtin_object_size,
> 	expr_object_size, call_object_size, unknown_object_size,
> 	merge_object_sizes, plus_stmt_object_size,
> 	cond_expr_object_size, collect_object_sizes_for,
> 	check_for_plus_in_loops_1, init_object_sizes,
> 	fini_object_sizes): Adjust.

Ok, thanks.

	Jakub
diff mbox series

Patch

diff --git a/gcc/tree-object-size.c b/gcc/tree-object-size.c
index 5e93bb74f92..3780437ff91 100644
--- a/gcc/tree-object-size.c
+++ b/gcc/tree-object-size.c
@@ -88,6 +88,71 @@  unknown (int object_size_type)
   return ((unsigned HOST_WIDE_INT) -((object_size_type >> 1) ^ 1));
 }
 
+/* Grow object_sizes[OBJECT_SIZE_TYPE] to num_ssa_names.  */
+
+static inline void
+object_sizes_grow (int object_size_type)
+{
+  if (num_ssa_names > object_sizes[object_size_type].length ())
+    object_sizes[object_size_type].safe_grow (num_ssa_names, true);
+}
+
+/* Release object_sizes[OBJECT_SIZE_TYPE].  */
+
+static inline void
+object_sizes_release (int object_size_type)
+{
+  object_sizes[object_size_type].release ();
+}
+
+/* Return true if object_sizes[OBJECT_SIZE_TYPE][VARNO] is unknown.  */
+
+static inline bool
+object_sizes_unknown_p (int object_size_type, unsigned varno)
+{
+  return (object_sizes[object_size_type][varno]
+	  == unknown (object_size_type));
+}
+
+/* Return size for VARNO corresponding to OSI.  */
+
+static inline unsigned HOST_WIDE_INT
+object_sizes_get (struct object_size_info *osi, unsigned varno)
+{
+  return object_sizes[osi->object_size_type][varno];
+}
+
+/* Set size for VARNO corresponding to OSI to VAL.  */
+
+static inline bool
+object_sizes_set_force (struct object_size_info *osi, unsigned varno,
+			unsigned HOST_WIDE_INT val)
+{
+  object_sizes[osi->object_size_type][varno] = val;
+  return true;
+}
+
+/* Set size for VARNO corresponding to OSI to VAL if it is the new minimum or
+   maximum.  */
+
+static inline bool
+object_sizes_set (struct object_size_info *osi, unsigned varno,
+		  unsigned HOST_WIDE_INT val)
+{
+  int object_size_type = osi->object_size_type;
+  if ((object_size_type & OST_MINIMUM) == 0)
+    {
+      if (object_sizes[object_size_type][varno] < val)
+	return object_sizes_set_force (osi, varno, val);
+    }
+  else
+    {
+      if (object_sizes[object_size_type][varno] > val)
+	return object_sizes_set_force (osi, varno, val);
+    }
+  return false;
+}
+
 /* Initialize OFFSET_LIMIT variable.  */
 static void
 init_offset_limit (void)
@@ -247,7 +312,7 @@  addr_object_size (struct object_size_info *osi, const_tree ptr,
 	    collect_object_sizes_for (osi, var);
 	  if (bitmap_bit_p (computed[object_size_type],
 			    SSA_NAME_VERSION (var)))
-	    sz = object_sizes[object_size_type][SSA_NAME_VERSION (var)];
+	    sz = object_sizes_get (osi, SSA_NAME_VERSION (var));
 	  else
 	    sz = unknown (object_size_type);
 	}
@@ -582,14 +647,14 @@  compute_builtin_object_size (tree ptr, int object_size_type,
       return false;
     }
 
+  struct object_size_info osi;
+  osi.object_size_type = object_size_type;
   if (!bitmap_bit_p (computed[object_size_type], SSA_NAME_VERSION (ptr)))
     {
-      struct object_size_info osi;
       bitmap_iterator bi;
       unsigned int i;
 
-      if (num_ssa_names > object_sizes[object_size_type].length ())
-	object_sizes[object_size_type].safe_grow (num_ssa_names, true);
+      object_sizes_grow (object_size_type);
       if (dump_file)
 	{
 	  fprintf (dump_file, "Computing %s %sobject size for ",
@@ -601,7 +666,6 @@  compute_builtin_object_size (tree ptr, int object_size_type,
 
       osi.visited = BITMAP_ALLOC (NULL);
       osi.reexamine = BITMAP_ALLOC (NULL);
-      osi.object_size_type = object_size_type;
       osi.depths = NULL;
       osi.stack = NULL;
       osi.tos = NULL;
@@ -678,8 +742,7 @@  compute_builtin_object_size (tree ptr, int object_size_type,
       if (dump_file)
 	{
 	  EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
-	    if (object_sizes[object_size_type][i]
-		!= unknown (object_size_type))
+	    if (!object_sizes_unknown_p (object_size_type, i))
 	      {
 		print_generic_expr (dump_file, ssa_name (i),
 				    dump_flags);
@@ -689,7 +752,7 @@  compute_builtin_object_size (tree ptr, int object_size_type,
 			 ((object_size_type & OST_MINIMUM) ? "minimum"
 			  : "maximum"),
 			 (object_size_type & OST_SUBOBJECT) ? "sub" : "",
-			 object_sizes[object_size_type][i]);
+			 object_sizes_get (&osi, i));
 	      }
 	}
 
@@ -697,7 +760,7 @@  compute_builtin_object_size (tree ptr, int object_size_type,
       BITMAP_FREE (osi.visited);
     }
 
-  *psize = object_sizes[object_size_type][SSA_NAME_VERSION (ptr)];
+  *psize = object_sizes_get (&osi, SSA_NAME_VERSION (ptr));
   return *psize != unknown (object_size_type);
 }
 
@@ -710,8 +773,7 @@  expr_object_size (struct object_size_info *osi, tree ptr, tree value)
   unsigned int varno = SSA_NAME_VERSION (ptr);
   unsigned HOST_WIDE_INT bytes;
 
-  gcc_assert (object_sizes[object_size_type][varno]
-	      != unknown (object_size_type));
+  gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
   gcc_assert (osi->pass == 0);
 
   if (TREE_CODE (value) == WITH_SIZE_EXPR)
@@ -726,16 +788,7 @@  expr_object_size (struct object_size_info *osi, tree ptr, tree value)
   else
     bytes = unknown (object_size_type);
 
-  if ((object_size_type & OST_MINIMUM) == 0)
-    {
-      if (object_sizes[object_size_type][varno] < bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
-  else
-    {
-      if (object_sizes[object_size_type][varno] > bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
+  object_sizes_set (osi, varno, bytes);
 }
 
 
@@ -750,22 +803,12 @@  call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
 
   gcc_assert (is_gimple_call (call));
 
-  gcc_assert (object_sizes[object_size_type][varno]
-	      != unknown (object_size_type));
+  gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
   gcc_assert (osi->pass == 0);
 
   bytes = alloc_object_size (call, object_size_type);
 
-  if ((object_size_type & OST_MINIMUM) == 0)
-    {
-      if (object_sizes[object_size_type][varno] < bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
-  else
-    {
-      if (object_sizes[object_size_type][varno] > bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
+  object_sizes_set (osi, varno, bytes);
 }
 
 
@@ -776,12 +819,11 @@  unknown_object_size (struct object_size_info *osi, tree ptr)
 {
   int object_size_type = osi->object_size_type;
   unsigned int varno = SSA_NAME_VERSION (ptr);
-  unsigned HOST_WIDE_INT bytes = unknown (object_size_type);
 
-  gcc_checking_assert (object_sizes[object_size_type][varno] != bytes);
+  gcc_checking_assert (!object_sizes_unknown_p (object_size_type, varno));
   gcc_checking_assert (osi->pass == 0);
 
-  object_sizes[object_size_type][varno] = bytes;
+  object_sizes_set (osi, varno, unknown (object_size_type));
 }
 
 
@@ -796,38 +838,24 @@  merge_object_sizes (struct object_size_info *osi, tree dest, tree orig,
   unsigned int varno = SSA_NAME_VERSION (dest);
   unsigned HOST_WIDE_INT orig_bytes;
 
-  if (object_sizes[object_size_type][varno] == unknown (object_size_type))
+  if (object_sizes_unknown_p (object_size_type, varno))
     return false;
   if (offset >= offset_limit)
     {
-      object_sizes[object_size_type][varno] = unknown (object_size_type);
+      object_sizes_set (osi, varno, unknown (object_size_type));
       return false;
     }
 
   if (osi->pass == 0)
     collect_object_sizes_for (osi, orig);
 
-  orig_bytes = object_sizes[object_size_type][SSA_NAME_VERSION (orig)];
+  orig_bytes = object_sizes_get (osi, SSA_NAME_VERSION (orig));
   if (orig_bytes != unknown (object_size_type))
     orig_bytes = (offset > orig_bytes)
 		 ? HOST_WIDE_INT_0U : orig_bytes - offset;
 
-  if ((object_size_type & OST_MINIMUM) == 0)
-    {
-      if (object_sizes[object_size_type][varno] < orig_bytes)
-	{
-	  object_sizes[object_size_type][varno] = orig_bytes;
-	  osi->changed = true;
-	}
-    }
-  else
-    {
-      if (object_sizes[object_size_type][varno] > orig_bytes)
-	{
-	  object_sizes[object_size_type][varno] = orig_bytes;
-	  osi->changed = true;
-	}
-    }
+  osi->changed = object_sizes_set (osi, varno, orig_bytes);
+
   return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
 }
 
@@ -859,7 +887,7 @@  plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
   else
     gcc_unreachable ();
 
-  if (object_sizes[object_size_type][varno] == unknown (object_size_type))
+  if (object_sizes_unknown_p (object_size_type, varno))
     return false;
 
   /* Handle PTR + OFFSET here.  */
@@ -890,16 +918,7 @@  plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
   else
     bytes = unknown (object_size_type);
 
-  if ((object_size_type & OST_MINIMUM) == 0)
-    {
-      if (object_sizes[object_size_type][varno] < bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
-  else
-    {
-      if (object_sizes[object_size_type][varno] > bytes)
-	object_sizes[object_size_type][varno] = bytes;
-    }
+  object_sizes_set (osi, varno, bytes);
   return false;
 }
 
@@ -918,7 +937,7 @@  cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
 
   gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
 
-  if (object_sizes[object_size_type][varno] == unknown (object_size_type))
+  if (object_sizes_unknown_p (object_size_type, varno))
     return false;
 
   then_ = gimple_assign_rhs2 (stmt);
@@ -929,7 +948,7 @@  cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
   else
     expr_object_size (osi, var, then_);
 
-  if (object_sizes[object_size_type][varno] == unknown (object_size_type))
+  if (object_sizes_unknown_p (object_size_type, varno))
     return reexamine;
 
   if (TREE_CODE (else_) == SSA_NAME)
@@ -975,8 +994,10 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
     {
       if (bitmap_set_bit (osi->visited, varno))
 	{
-	  object_sizes[object_size_type][varno]
-	    = (object_size_type & OST_MINIMUM) ? -1 : 0;
+	  /* Initialize to 0 for maximum size and M1U for minimum size so that
+	     it gets immediately overridden.  */
+	  object_sizes_set_force (osi, varno,
+				  unknown (object_size_type ^ OST_MINIMUM));
 	}
       else
 	{
@@ -1047,7 +1068,7 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
 
     case GIMPLE_ASM:
       /* Pointers defined by __asm__ statements can point anywhere.  */
-      object_sizes[object_size_type][varno] = unknown (object_size_type);
+      object_sizes_set (osi, varno, unknown (object_size_type));
       break;
 
     case GIMPLE_NOP:
@@ -1056,7 +1077,7 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
 	expr_object_size (osi, var, SSA_NAME_VAR (var));
       else
 	/* Uninitialized SSA names point nowhere.  */
-	object_sizes[object_size_type][varno] = unknown (object_size_type);
+	object_sizes_set (osi, varno, unknown (object_size_type));
       break;
 
     case GIMPLE_PHI:
@@ -1067,8 +1088,7 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
 	  {
 	    tree rhs = gimple_phi_arg (stmt, i)->def;
 
-	    if (object_sizes[object_size_type][varno]
-		== unknown (object_size_type))
+	    if (object_sizes_unknown_p (object_size_type, varno))
 	      break;
 
 	    if (TREE_CODE (rhs) == SSA_NAME)
@@ -1083,8 +1103,7 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
       gcc_unreachable ();
     }
 
-  if (! reexamine
-      || object_sizes[object_size_type][varno] == unknown (object_size_type))
+  if (! reexamine || object_sizes_unknown_p (object_size_type, varno))
     {
       bitmap_set_bit (computed[object_size_type], varno);
       bitmap_clear_bit (osi->reexamine, varno);
@@ -1124,7 +1143,7 @@  check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
 	      --sp;
 	      bitmap_clear_bit (osi->reexamine, *sp);
 	      bitmap_set_bit (computed[osi->object_size_type], *sp);
-	      object_sizes[osi->object_size_type][*sp] = 0;
+	      object_sizes_set_force (osi, *sp, 0);
 	      if (*sp == varno)
 		break;
 	    }
@@ -1248,7 +1267,7 @@  init_object_sizes (void)
 
   for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
     {
-      object_sizes[object_size_type].safe_grow (num_ssa_names, true);
+      object_sizes_grow (object_size_type);
       computed[object_size_type] = BITMAP_ALLOC (NULL);
     }
 
@@ -1265,7 +1284,7 @@  fini_object_sizes (void)
 
   for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
     {
-      object_sizes[object_size_type].release ();
+      object_sizes_release (object_size_type);
       BITMAP_FREE (computed[object_size_type]);
     }
 }