From 3bff15c1c9fb3eb0bb042717e072476ec2d6d88c Mon Sep 17 00:00:00 2001 From: Aldy Hernandez Date: Mon, 7 Nov 2022 08:40:12 +0100 Subject: [range-op] Restrict division by power of 2 optimization to positive numbers. The problem here is that we are transforming a division by a power of 2 into a right shift, and using this to shift the maybe nonzero bits. This gives the wrong result when the number being divided is negative. In the testcase we are dividing this by 8: [irange] int [-256, -255] NONZERO 0xffffff01 and coming up with: [irange] int [-32, -31] NONZERO 0xffffffe0 The maybe nonzero bits are wrong as -31 has the LSB set (0xffffffe1) whereas the bitmask says the lower 4 bits are off. PR tree-optimization/107541 gcc/ChangeLog: * range-op.cc (operator_div::fold_range): Restrict power of 2 optimization to positive numbers. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr107541.c: New test. --- gcc/range-op.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'gcc/range-op.cc') diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 5e94c3d2282..2b5db0cac85 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -1953,7 +1953,9 @@ operator_div::fold_range (irange &r, tree type, return true; tree t; - if (rh.singleton_p (&t)) + if (code == TRUNC_DIV_EXPR + && rh.singleton_p (&t) + && !wi::neg_p (lh.lower_bound ())) { wide_int wi = wi::to_wide (t); int shift = wi::exact_log2 (wi); -- cgit v1.2.1 From a239a63f868e29e9276088e7c0fb00804c2903ba Mon Sep 17 00:00:00 2001 From: Aldy Hernandez Date: Fri, 4 Nov 2022 22:24:42 +0100 Subject: Improve multiplication by powers of 2 in range-ops. For unsigned numbers, multiplication by X, where X is a power of 2 is [0,0][X,+INF]. This patch causes a regression to g++.dg/pr71488.C where -Wstringop-overflow gets the same IL as before, but better ranges cause it to issue a bogus warning. I will create a PR with some notes. No discernible changes in performance. Tested on x86-64 Linux. PR tree-optimization/55157 gcc/ChangeLog: * range-op.cc (operator_mult::wi_fold): Optimize multiplications by powers of 2. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr55157.c: New test. --- gcc/range-op.cc | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'gcc/range-op.cc') diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 2b5db0cac85..a13e88840a6 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -1911,8 +1911,20 @@ operator_mult::wi_fold (irange &r, tree type, // diff = max - min prod2 = prod3 - prod0; if (wi::geu_p (prod2, sizem1)) - // The range covers all values. - r.set_varying (type); + { + // Multiplying by X, where X is a power of 2 is [0,0][X,+INF]. + if (TYPE_UNSIGNED (type) && rh_lb == rh_ub + && wi::exact_log2 (rh_lb) != -1 && prec > 1) + { + r.set (type, rh_lb, wi::max_value (prec, sign)); + int_range<2> zero; + zero.set_zero (type); + r.union_ (zero); + } + else + // The range covers all values. + r.set_varying (type); + } else { wide_int new_lb = wide_int::from (prod0, prec, sign); -- cgit v1.2.1