From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id 82F773858401; Mon, 8 Nov 2021 15:08:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82F773858401 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-5000] path solver: Avoid recalculating ranges already in the cache. X-Act-Checkin: gcc X-Git-Author: Aldy Hernandez X-Git-Refname: refs/heads/master X-Git-Oldrev: a354b4255b679de45bd3d4d8874a26c89f6c74fc X-Git-Newrev: 18546941ae4c56cd9240d2dc2ca2806e01eb6fb7 Message-Id: <20211108150828.82F773858401@sourceware.org> Date: Mon, 8 Nov 2021 15:08:28 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Nov 2021 15:08:28 -0000 https://gcc.gnu.org/g:18546941ae4c56cd9240d2dc2ca2806e01eb6fb7 commit r12-5000-g18546941ae4c56cd9240d2dc2ca2806e01eb6fb7 Author: Aldy Hernandez Date: Mon Nov 8 12:13:33 2021 +0100 path solver: Avoid recalculating ranges already in the cache. The problem here is an ordering issue with a path that starts with 19->3: [local count: 916928331]: # value_20 = PHI # n_27 = PHI n_16 = n_27 + 4; value_17 = value_20 / 10000; if (value_20 > 42949672959999) goto ; [89.00%] else goto ; [11.00%] The problem here is that both value_17 and value_20 are in the set of imports we must pre-calculate. The value_17 name occurs first in the bitmap, so we try to resolve it first, which causes us to recursively solve the value_20 range. We do so correctly and put them both in the cache. However, when we try to solve value_20 from the bitmap, we ignore that it already has a cached entry and try to resolve the PHI with the wrong value of value_17: # value_20 = PHI The right thing to do is to avoid recalculating definitions already solved. Regstrapped and checked for # threads before and after on x86-64 Linux. gcc/ChangeLog: PR tree-optimization/103120 * gimple-range-path.cc (path_range_query::range_defined_in_block): Bail if there's a cache entry. gcc/testsuite/ChangeLog: * gcc.dg/pr103120.c: New test. Diff: --- gcc/gimple-range-path.cc | 3 +++ gcc/testsuite/gcc.dg/pr103120.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/gcc/gimple-range-path.cc b/gcc/gimple-range-path.cc index 9175651e896..9d3fe89185e 100644 --- a/gcc/gimple-range-path.cc +++ b/gcc/gimple-range-path.cc @@ -300,6 +300,9 @@ path_range_query::range_defined_in_block (irange &r, tree name, basic_block bb) if (def_bb != bb) return false; + if (get_cache (r, name)) + return true; + if (gimple_code (def_stmt) == GIMPLE_PHI) ssa_range_in_phi (r, as_a (def_stmt)); else diff --git a/gcc/testsuite/gcc.dg/pr103120.c b/gcc/testsuite/gcc.dg/pr103120.c new file mode 100644 index 00000000000..b680a6c0fb0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr103120.c @@ -0,0 +1,33 @@ +// { dg-do run } +// { dg-options "-O2" } + +#define radix 10 +__INT32_TYPE__ numDigits(__UINT64_TYPE__ value) +{ + __INT32_TYPE__ n = 1; + while (value > __UINT32_MAX__) + { + n += 4; + value /= radix * radix * radix * radix; + } + __UINT32_TYPE__ v = (__UINT32_TYPE__)value; + while (1) + { + if (v < radix) + return n; + if (v < radix * radix) + return n + 1; + if (v < radix * radix * radix) + return n + 2; + if (v < radix * radix * radix * radix) + return n + 3; + n += 4; + v /= radix * radix * radix * radix; + } +} + +int main() +{ + if (numDigits(__UINT64_MAX__) != 20) + __builtin_abort(); +}