middle-end/101858 - avoid shift of pointer in folding

This makes sure to not generate a shift of pointer types in
simplification of X < (cast) (1 << Y).

2021-08-11  Richard Biener  <rguenther@suse.de>

	PR middle-end/101858
	* fold-const.c (fold_binary_loc): Guard simplification
	of  X < (cast) (1 << Y) to integer types.

	* gcc.dg/pr101858.c: New testcase.
This commit is contained in:
Richard Biener 2021-08-11 10:26:18 +02:00
parent f19c70afa4
commit d7e91f4894
2 changed files with 11 additions and 0 deletions

View File

@ -12497,6 +12497,8 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type,
we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
31 might be 0xffffffff80000000. */
if ((code == LT_EXPR || code == GE_EXPR)
&& (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
|| VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
&& TYPE_UNSIGNED (TREE_TYPE (arg0))
&& CONVERT_EXPR_P (arg1)
&& TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR

View File

@ -0,0 +1,9 @@
/* { dg-do compile } */
/* { dg-options "-w" } */
int foo(int a)
{
if (a < (int*)((__INTPTR_TYPE__)1 << a))
a = 0;
return a;
}