* [PATCH] Backport PR56917 to 4.8
@ 2015-06-18 13:15 Richard Biener
0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2015-06-18 13:15 UTC (permalink / raw)
To: gcc-patches
I am currently bootstrapping and regtesting the following for backport
to the 4.8 branch and the testcase for 4.9 to trunk.
Richard.
2015-06-18 Richard Biener <rguenther@suse.de>
Backport from mainline
2014-12-04 Marek Polacek <polacek@redhat.com>
PR middle-end/56917
* fold-const.c (fold_unary_loc): Perform the negation in A's type
when transforming ~ (A - 1) or ~ (A + -1) to -A.
* g++.dg/other/const4.C: New testcase.
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c (revision 221345)
+++ gcc/fold-const.c (revision 221346)
@@ -8324,9 +8324,14 @@ fold_unary_loc (location_t loc, enum tre
&& integer_onep (TREE_OPERAND (arg0, 1)))
|| (TREE_CODE (arg0) == PLUS_EXPR
&& integer_all_onesp (TREE_OPERAND (arg0, 1)))))
- return fold_build1_loc (loc, NEGATE_EXPR, type,
- fold_convert_loc (loc, type,
- TREE_OPERAND (arg0, 0)));
+ {
+ /* Perform the negation in ARG0's type and only then convert
+ to TYPE as to avoid introducing undefined behavior. */
+ tree t = fold_build1_loc (loc, NEGATE_EXPR,
+ TREE_TYPE (TREE_OPERAND (arg0, 0)),
+ TREE_OPERAND (arg0, 0));
+ return fold_convert_loc (loc, type, t);
+ }
/* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
else if (TREE_CODE (arg0) == BIT_XOR_EXPR
&& (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
Index: gcc/testsuite/g++.dg/other/const4.C
===================================================================
*** gcc/testsuite/g++.dg/other/const4.C (revision 0)
--- gcc/testsuite/g++.dg/other/const4.C (working copy)
***************
*** 0 ****
--- 1,10 ----
+ // { dg-do compile }
+
+ int lValue;
+ int main()
+ {
+ switch (lValue)
+ {
+ case -(int)((2U << (8 * sizeof(int) - 2)) - 1) - 1:;
+ }
+ }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2015-06-18 13:09 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-18 13:15 [PATCH] Backport PR56917 to 4.8 Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).