aboutsummaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2015-10-29 14:10:31 +0000
committerRichard Biener <rguenth@gcc.gnu.org>2015-10-29 14:10:31 +0000
commit55db37536fcff75d42cb852e3c822f9f3fb5f04f (patch)
treeaf44e27e3907d99f2751ff4e4aa9f9a2f0ece228 /gcc/fold-const.c
parent957060b5c5d272296ccd00ebdb5179ef25123fb5 (diff)
downloadgcc-55db37536fcff75d42cb852e3c822f9f3fb5f04f.zip
gcc-55db37536fcff75d42cb852e3c822f9f3fb5f04f.tar.gz
gcc-55db37536fcff75d42cb852e3c822f9f3fb5f04f.tar.bz2
re PR middle-end/68142 (unsafe association of multiplication)
2015-10-29 Richard Biener <rguenther@suse.de> PR middle-end/68142 * fold-const.c (extract_muldiv_1): Avoid introducing undefined overflow. * c-c++-common/ubsan/pr68142.c: New testcase. From-SVN: r229528
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 298c520..26fd735 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -6008,8 +6008,17 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
or (for divide and modulus) if it is a multiple of our constant. */
if (code == MULT_EXPR
|| wi::multiple_of_p (t, c, TYPE_SIGN (type)))
- return const_binop (code, fold_convert (ctype, t),
- fold_convert (ctype, c));
+ {
+ tree tem = const_binop (code, fold_convert (ctype, t),
+ fold_convert (ctype, c));
+ /* If the multiplication overflowed to INT_MIN then we lost sign
+ information on it and a subsequent multiplication might
+ spuriously overflow. See PR68142. */
+ if (TREE_OVERFLOW (tem)
+ && wi::eq_p (tem, wi::min_value (TYPE_PRECISION (ctype), SIGNED)))
+ return NULL_TREE;
+ return tem;
+ }
break;
CASE_CONVERT: case NON_LVALUE_EXPR: