Skip to content

Commit

Permalink
* fold-const.c (fold_binary_loc): Don't fold if the result
Browse files Browse the repository at this point in the history
	is undefined.
	* match.pd (A + (-B) -> A - B, A - (-B) -> A + B,
	-(-A) -> A): Likewise.

	* c-c++-common/ubsan/overflow-sub-4.c: New test.
	* c-c++-common/ubsan/overflow-sub-2.c: Adjust dg-output.
	* c-c++-common/ubsan/overflow-int128.c: Likewise.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@217427 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
mpolacek committed Nov 12, 2014
1 parent 1d2d4c4 commit 497d1b7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
7 changes: 7 additions & 0 deletions gcc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2014-11-12 Marek Polacek <[email protected]>

* fold-const.c (fold_binary_loc): Don't fold if the result
is undefined.
* match.pd (A + (-B) -> A - B, A - (-B) -> A + B,
-(-A) -> A): Likewise.

2014-11-12 Richard Biener <[email protected]>

Merge from match-and-simplify branch
Expand Down
3 changes: 3 additions & 0 deletions gcc/fold-const.c
Original file line number Diff line number Diff line change
Expand Up @@ -10538,6 +10538,9 @@ fold_binary_loc (location_t loc,

/* A - B -> A + (-B) if B is easily negatable. */
if (negate_expr_p (arg1)
&& (!INTEGRAL_TYPE_P (type)
|| TYPE_OVERFLOW_WRAPS (type)
|| (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
&& ((FLOAT_TYPE_P (type)
/* Avoid this transformation if B is a positive REAL_CST. */
&& (TREE_CODE (arg1) != REAL_CST
Expand Down
12 changes: 9 additions & 3 deletions gcc/match.pd
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,25 @@ along with GCC; see the file COPYING3. If not see
/* Apply STRIP_NOPS on @0 and the negate. */
(if (tree_nop_conversion_p (type, TREE_TYPE (@0))
&& tree_nop_conversion_p (type, TREE_TYPE (@1))
&& (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
&& (!INTEGRAL_TYPE_P (type)
|| TYPE_OVERFLOW_WRAPS (type)
|| (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
(minus (convert @0) (convert @1))))
/* A - (-B) -> A + B */
(simplify
(minus (convert1? @0) (convert2? (negate @1)))
(if (tree_nop_conversion_p (type, TREE_TYPE (@0))
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))
&& tree_nop_conversion_p (type, TREE_TYPE (@1))
&& (!INTEGRAL_TYPE_P (type)
|| TYPE_OVERFLOW_WRAPS (type)
|| (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
(plus (convert @0) (convert @1))))
/* -(-A) -> A */
(simplify
(negate (convert? (negate @1)))
(if (tree_nop_conversion_p (type, TREE_TYPE (@1))
&& (TYPE_OVERFLOW_WRAPS (type)
&& (!INTEGRAL_TYPE_P (type)
|| TYPE_OVERFLOW_WRAPS (type)
|| (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
(convert @1)))

Expand Down
6 changes: 6 additions & 0 deletions gcc/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2014-11-12 Marek Polacek <[email protected]>

* c-c++-common/ubsan/overflow-sub-4.c: New test.
* c-c++-common/ubsan/overflow-sub-2.c: Adjust dg-output.
* c-c++-common/ubsan/overflow-int128.c: Likewise.

2014-11-12 Evgeny Stupachenko <[email protected]>

* gcc.target/i386/pr52252-atom-1.c: Extend assembler scan on AVX2 case.
Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/c-c++-common/ubsan/overflow-int128.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ main (void)
/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffff9b \\+ 1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ 0x80000000000000000000000000000000 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 - 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000064 \\+ -1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\* 2 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*negation of 0x80000000000000000000000000000000 cannot be represented in type '__int128'; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
16 changes: 8 additions & 8 deletions gcc/testsuite/c-c++-common/ubsan/overflow-sub-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ main (void)
}

/* { dg-output "signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 \\+ -1024 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147482648 \\+ -1048576 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 - 1024 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -2147482648 - 1048576 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1024 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1048576 cannot be represented in type 'long int'\[^\n\r]*" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1024 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1048576 cannot be represented in type 'long int'\[^\n\r]*" } */
19 changes: 19 additions & 0 deletions gcc/testsuite/c-c++-common/ubsan/overflow-sub-4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=signed-integer-overflow" } */

#define INT_MIN (-__INT_MAX__ - 1)

int
main ()
{
int x = INT_MIN;
int y = 0;
int z;
asm ("" : "+g" (y));
asm ("" : "+g" (x));
z = y - (-x);
asm ("" : "+g" (z));
}

/* { dg-output "negation of -2147483648 cannot be represented in type 'int'\[^\n\r]*; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*signed integer overflow: 0 - -2147483648 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */

0 comments on commit 497d1b7

Please sign in to comment.