gcc trunk, rev250367, x86_64. > cat f.cpp #include <stdio.h> unsigned short a = 41461; unsigned short b = 3419; int c = 0; void foo() { if (a + b * ~(0 != 5)) c = -~(b * ~(0 != 5)) + 2147483647; } int main() { foo(); printf("%d\n", c); return 0; } > g++ f.cpp -O0; ./a.out 2147476810 > g++ f.cpp -O2; ./a.out -2147483648
Looks like SLSR does an overflow-unsafe transformation, then VRP2 takes advantage of it. Maybe.
Seems to work with GCC 7.
Started with r249447: gcc/ * match.pd (nop_convert): New predicate. ((A +- CST1) +- CST2): Allow some NOP conversions. gcc/testsuite/ * gcc.dg/tree-ssa/addadd.c: Un-XFAIL. * gcc.dg/tree-ssa/addadd-2.c: New file.
if (a + b * -2) c = (b-1073741824)*-2; might let you find an earlier culprit.
Mine, but my queue is getting pretty deep, so it will be a bit before I look at it.
How can I switch off optimization phases to workaround the bug? I have another instances of wrong code bugs, so I'd like to make sure that I don't create duplicate reports for the same problem.
Try -fno-slsr.
Patch under test that fixes this case: Index: gcc/gimple-ssa-strength-reduction.c =================================================================== --- gcc/gimple-ssa-strength-reduction.c (revision 250791) +++ gcc/gimple-ssa-strength-reduction.c (working copy) @@ -2082,6 +2082,11 @@ replace_mult_candidate (slsr_cand_t c, tree basis_ types but allows for safe negation without twisted logic. */ if (wi::fits_shwi_p (bump) && bump.to_shwi () != HOST_WIDE_INT_MIN + /* It is more likely that the bump doesn't fit in the target + type, so check whether constraining it to that type changes + the value. */ + && wi::eq_p (TREE_INT_CST_LOW (wide_int_to_tree (target_type, bump)), + bump) /* It is not useful to replace casts, copies, negates, or adds of an SSA name and a constant. */ && cand_code != SSA_NAME
This is overkill, it has some test case fallout. Will have to look a bit deeper.
The TREE_INT_CST_LOW part looks suspicious. Also, wide-int.h should provide enough infrastructure so that you should be able to do everything on wide-int, not have to create trees.
(In reply to Jakub Jelinek from comment #10) > The TREE_INT_CST_LOW part looks suspicious. Also, wide-int.h should provide > enough infrastructure so that you should be able to do everything on > wide-int, not have to create trees. I just saw this comment now. I think that the TREE_INT_CST_LOW is ok given that we've already constrained bump to fit in a HOST_WIDE_INT, right? But anyway, your second point is well taken -- it looks like I can make use of wi::to_uhwi and wi::to_shwi with a specified precision from the target type, so use of trees should indeed be unnecessary. I have a working patch that uses the trees, but I will rework that in favor of a pure wide-int solution. Thanks for the suggestion! Bill
I had in mind something like wi::eq_p (wi::ext (w, TYPE_PRECISION (type), TYPE_SIGN (type)), w) or so.
(In reply to Jakub Jelinek from comment #12) > I had in mind something like > wi::eq_p (wi::ext (w, TYPE_PRECISION (type), TYPE_SIGN (type)), w) > or so. Ah, good, thank you.
Created attachment 41899 [details] Patch under test This is the patch I'm currently looking at. I am unhappy at having to use a tree to get maxval, but I cannot find a way to convert a wide_int into a widest_int so that I can add them together (see the desired code that is commented out). Do you have a suggestion how I can do this within the wide_int framework?
Proposed patch awaiting approval: https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00347.html
Author: wschmidt Date: Tue Aug 29 14:41:53 2017 New Revision: 251414 URL: https://gcc.gnu.org/viewcvs?rev=251414&root=gcc&view=rev Log: [gcc] 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr81503.c Modified: trunk/gcc/ChangeLog trunk/gcc/gimple-ssa-strength-reduction.c trunk/gcc/testsuite/ChangeLog
Fixed in trunk so far. Although this test case succeeds on GCC 7, the bug is latent there, so I'll keep this open and backport the fix to other releases in a week or so.
Author: wschmidt Date: Tue Sep 5 21:49:01 2017 New Revision: 251743 URL: https://gcc.gnu.org/viewcvs?rev=251743&root=gcc&view=rev Log: [gcc] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Added: branches/gcc-7-branch/gcc/testsuite/gcc.c-torture/execute/pr81503.c Modified: branches/gcc-7-branch/gcc/ChangeLog branches/gcc-7-branch/gcc/gimple-ssa-strength-reduction.c branches/gcc-7-branch/gcc/testsuite/ChangeLog
Author: wschmidt Date: Tue Sep 5 21:50:38 2017 New Revision: 251744 URL: https://gcc.gnu.org/viewcvs?rev=251744&root=gcc&view=rev Log: [gcc] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Added: branches/gcc-6-branch/gcc/testsuite/gcc.c-torture/execute/pr81503.c Modified: branches/gcc-6-branch/gcc/ChangeLog branches/gcc-6-branch/gcc/gimple-ssa-strength-reduction.c branches/gcc-6-branch/gcc/testsuite/ChangeLog
Author: wschmidt Date: Tue Sep 5 21:52:01 2017 New Revision: 251745 URL: https://gcc.gnu.org/viewcvs?rev=251745&root=gcc&view=rev Log: [gcc] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-09-05 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Backport from mainline 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Added: branches/gcc-5-branch/gcc/testsuite/gcc.c-torture/execute/pr81503.c Modified: branches/gcc-5-branch/gcc/ChangeLog branches/gcc-5-branch/gcc/gimple-ssa-strength-reduction.c branches/gcc-5-branch/gcc/testsuite/ChangeLog
Fixed everywhere now.
Author: aldyh Date: Wed Sep 13 17:36:05 2017 New Revision: 252605 URL: https://gcc.gnu.org/viewcvs?rev=252605&root=gcc&view=rev Log: [gcc] 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> Jakub Jelinek <jakub@redhat.com> Richard Biener <rguenther@suse.de> PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-08-29 Bill Schmidt <wschmidt@linux.vnet.ibm.com> PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Added: branches/range-gen2/gcc/testsuite/gcc.c-torture/execute/pr81503.c Modified: branches/range-gen2/gcc/ChangeLog branches/range-gen2/gcc/gimple-ssa-strength-reduction.c branches/range-gen2/gcc/testsuite/ChangeLog