Update range query cache when a statement is updated.
authorAndrew MacLeod <amacleod@redhat.com>
Thu, 3 Nov 2022 01:37:49 +0000 (21:37 -0400)
committerAndrew MacLeod <amacleod@redhat.com>
Thu, 3 Nov 2022 16:46:03 +0000 (12:46 -0400)
Add an update_stmt interface to range query, and hook into it with the
ssa statement update call.

gcc/
* gimple-range.cc (gimple_ranger::update_stmt): New.
* gimple-range.h (gimple_ranger::update_stmt): New prototype.
* tree-ssa-operands.cc (update_stmt_operands): Notify range
query that stmt has changed.
* value-query.h (range_query::update_stmt): New.

gcc/testsuite/
* gcc.dg/tree-ssa/vrp-update.c: New.

gcc/gimple-range.cc
gcc/gimple-range.h
gcc/testsuite/gcc.dg/tree-ssa/vrp-update.c [new file with mode: 0644]
gcc/tree-ssa-operands.cc
gcc/value-query.h

index 110cf57..8063869 100644 (file)
@@ -482,6 +482,40 @@ gimple_ranger::register_inferred_ranges (gimple *s)
   m_cache.apply_inferred_ranges (s);
 }
 
+// When a statement S has changed since the result was cached, re-evaluate
+// and update the global cache.
+
+void
+gimple_ranger::update_stmt (gimple *s)
+{
+  tree lhs = gimple_get_lhs (s);
+  if (!lhs || !gimple_range_ssa_p (lhs))
+    return;
+  Value_Range r (TREE_TYPE (lhs));
+  // Only update if it already had a value.
+  if (m_cache.get_global_range (r, lhs))
+    {
+      // Re-calculate a new value using just cache values.
+      Value_Range tmp (TREE_TYPE (lhs));
+      fold_using_range f;
+      fur_depend src (s, &(gori ()), &m_cache);
+      f.fold_stmt (tmp, s, src, lhs);
+
+      // Combine the new value with the old value to check for a change.
+      if (r.intersect (tmp))
+       {
+         if (dump_file && (dump_flags & TDF_DETAILS))
+           {
+             print_generic_expr (dump_file, lhs, TDF_SLIM);
+             fprintf (dump_file, " : global value re-evaluated to ");
+             r.dump (dump_file);
+             fputc ('\n', dump_file);
+           }
+         m_cache.set_global_range (lhs, r);
+       }
+    }
+}
+
 // This routine will export whatever global ranges are known to GCC
 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
 
index 4800bfb..22e05f6 100644 (file)
@@ -51,6 +51,7 @@ public:
   virtual bool range_of_stmt (vrange &r, gimple *, tree name = NULL) override;
   virtual bool range_of_expr (vrange &r, tree name, gimple * = NULL) override;
   virtual bool range_on_edge (vrange &r, edge e, tree name) override;
+  virtual void update_stmt (gimple *) override;
   void range_on_entry (vrange &r, basic_block bb, tree name);
   void range_on_exit (vrange &r, basic_block bb, tree name);
   void export_global_ranges ();
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-update.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-update.c
new file mode 100644 (file)
index 0000000..9e5da88
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-options "-O2 -fdump-tree-vrp1 " } */
+
+/* Tests that calls to update_stmt by the folder will also update ranger's
+   cache value and produce the correct result for the builtin_constant_p
+   function.  */
+
+void dead ();
+
+void foo( void *_thrdescr, int _result)
+{
+  const char *lossage = _result ? "constant string" : 0;
+
+  if (__builtin_expect (lossage != ((void *)0) , 0))
+    {
+    unsigned __message_length = __builtin_strlen (lossage);
+    if (! __builtin_constant_p (__message_length))
+      dead ();
+    }
+}
+
+/* { dg-final { scan-tree-dump-not "dead" "vrp1" } } */
index 4915622..9e85998 100644 (file)
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stmt.h"
 #include "print-tree.h"
 #include "dumpfile.h"
+#include "value-query.h"
 
 
 /* This file contains the code required to manage the operands cache of the
@@ -1146,6 +1147,8 @@ update_stmt_operands (struct function *fn, gimple *stmt)
   gcc_assert (gimple_modified_p (stmt));
   operands_scanner (fn, stmt).build_ssa_operands ();
   gimple_set_modified (stmt, false);
+  // Inform the active range query an update has happened.
+  get_range_query (fn)->update_stmt (stmt);
 
   timevar_pop (TV_TREE_OPS);
 }
index fc638eb..b8e6fed 100644 (file)
@@ -93,6 +93,9 @@ public:
   virtual bool range_on_edge (vrange &r, edge, tree expr);
   virtual bool range_of_stmt (vrange &r, gimple *, tree name = NULL);
 
+  // When the IL in a stmt is changed, call this for better results.
+  virtual void update_stmt (gimple *) { }
+
   // Query if there is any relation between SSA1 and SSA2.
   relation_kind query_relation (gimple *s, tree ssa1, tree ssa2,
                                bool get_range = true);