https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103120

--- Comment #5 from Aldy Hernandez <aldyh at gcc dot gnu.org> ---
This is an ordering issue in the path solver, and it's not even relation
related!  How interesting.

./xgcc -B./ a.c -O2 -fdisable-tree-ethread -fdisable-tree-thread1
-fdisable-tree-thread2 -fdisable-tree-threadfull1 -fno-tree-dominator-opts
-fdump-tree-threadfull2-details-threading -fdbg-cnt=back_threadfull2:1-1

The path is 19->3->??:

  <bb 3> [local count: 916928331]:
  # value_20 = PHI <value_17(19), value_7(D)(17)>
  # n_27 = PHI <n_16(19), 1(17)>
  n_16 = n_27 + 4;
  value_17 = value_20 / 10000;
  if (value_20 > 42949672959999)
    goto <bb 19>; [89.00%]
  else
    goto <bb 4>; [11.00%]

The path solver calculates:

*********** path_range_query ******************

path_range_query: compute_ranges for path: BB 19, BB 3
 Registering killing_def (path_oracle) value_17
range_defined_in_block (BB3) for value_20 is long unsigned int [4294967296,
1844674407370955]
range_defined_in_block (BB3) for value_17 is long unsigned int [429496,
184467440737]
range_defined_in_block (BB3) for value_20 is long unsigned int [429496,
184467440737]

So, it's calculating the range for value_20 as it tries to resolve value_17. 
The first 2 ranges are correct.

Then it tries to calculate the range for value_20 (again) because it's an
import.  However, when looking at the PHI, it ignores that there's already a
cache entry, so it tries the following...

  # value_20 = PHI <value_17(19), value_7(D)(17)>

...using the new value of value_17 which is incorrect.

It blows my mind that we haven't run into this before, especially with such a
simple test case.

I need to think about this, but my gut feeling says the following should do it:

diff --git a/gcc/gimple-range-path.cc b/gcc/gimple-range-path.cc
index 9175651e896..7b5b2e5e55d 100644
--- a/gcc/gimple-range-path.cc
+++ b/gcc/gimple-range-path.cc
@@ -356,7 +356,8 @@ path_range_query::compute_ranges_in_block (basic_block bb)
     {
       tree name = ssa_name (i);

-      if (range_defined_in_block (r, name, bb))
+      if (!bitmap_bit_p (m_has_cache_entry, SSA_NAME_VERSION (name))
+         && range_defined_in_block (r, name, bb))
        set_cache (r, name);
     }

Reply via email to