Skip to content

Commit d44dc13

Browse files
committed
ranger: Fix up REALPART_EXPR/IMAGPART_EXPR handling [PR104604]
The following testcase is miscompiled since r12-3328. That change assumed that if rhs1 of a GIMPLE_ASSIGN is COMPLEX_CST, then that is the value of the lhs of the stmt, but that is not the case always, only if it is a GIMPLE_SINGLE_RHS stmt. If it is e.g. GIMPLE_UNARY_RHS or GIMPLE_BINARY_RHS (the latter happens in the testcase), then it can be e.g. __complex__ (3, 0) / var and the REALPART_EXPR of that isn't 3, but the realpart of the division. I assume once the ranger can do complex numbers adjust_*part_expr will just fetch one or the other range from a underlying complex range, but until then, we should limit this to what r12-3328 meant to do. 2022-02-22 Jakub Jelinek <[email protected]> PR tree-optimization/104604 * gimple-range-fold.cc (adjust_imagpart_expr, adjust_realpart_expr): Only check if gimple_assign_rhs1 is COMPLEX_CST if gimple_assign_rhs_code is COMPLEX_CST. * gcc.c-torture/execute/pr104604.c: New test.
1 parent 7e69118 commit d44dc13

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

gcc/gimple-range-fold.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ adjust_imagpart_expr (irange &res, const gimple *stmt)
397397
}
398398
return;
399399
}
400-
if (is_gimple_assign (def_stmt))
400+
if (is_gimple_assign (def_stmt)
401+
&& gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
401402
{
402403
tree cst = gimple_assign_rhs1 (def_stmt);
403404
if (TREE_CODE (cst) == COMPLEX_CST)
@@ -422,7 +423,8 @@ adjust_realpart_expr (irange &res, const gimple *stmt)
422423
if (!SSA_NAME_DEF_STMT (name))
423424
return;
424425

425-
if (is_gimple_assign (def_stmt))
426+
if (is_gimple_assign (def_stmt)
427+
&& gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
426428
{
427429
tree cst = gimple_assign_rhs1 (def_stmt);
428430
if (TREE_CODE (cst) == COMPLEX_CST)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* PR tree-optimization/104604 */
2+
3+
unsigned char g;
4+
5+
__attribute__((noipa))
6+
unsigned char
7+
foo (_Complex unsigned c)
8+
{
9+
unsigned char v = g;
10+
_Complex unsigned t = 3;
11+
t /= c;
12+
return v + t;
13+
}
14+
15+
__attribute__((noipa))
16+
unsigned char
17+
bar (_Complex unsigned c)
18+
{
19+
unsigned char v = g;
20+
_Complex unsigned t = 42;
21+
t /= c;
22+
return v + t;
23+
}
24+
25+
int
26+
main ()
27+
{
28+
unsigned char x = foo (7);
29+
if (x)
30+
__builtin_abort ();
31+
if (bar (7) != 6)
32+
__builtin_abort ();
33+
return 0;
34+
}

0 commit comments

Comments
 (0)