Use this fact in some foldings of comparisons. See testcase.
Unified · Split
| @@ -4979,7 +4979,9 @@ static void expr_cond(void) · 4978 unmodified lines | ||
| 4979 | 4979 | expr_lor(); |
| 4980 | 4980 | if (tok == '?') { |
| 4981 | 4981 | next(); |
| 4982 | − if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { | |
| 4982 | + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && | |
| 4983 | + (!(vtop->r & VT_SYM) || | |
| 4984 | + !(vtop->sym->type.t & VT_WEAK))) { | |
| 4983 | 4985 | int saved_nocode_wanted = nocode_wanted; |
| 4984 | 4986 | CType boolean; |
| 4985 | 4987 | int c; |
| @@ -1470,6 +1470,10 @@ typedef int arrtype2[3]; · 1469 unmodified lines | ||
| 1470 | 1470 | arrtype2 sinit21 = {4}; |
| 1471 | 1471 | arrtype2 sinit22 = {5,6,7}; |
| 1472 | 1472 | |
| 1473 | +/* Address comparisons of non-weak symbols with zero can be const-folded */ | |
| 1474 | +int sinit23[2] = { "astring" ? sizeof("astring") : -1, | |
| 1475 | + &sinit23 ? 42 : -1 }; | |
| 1476 | + | |
| 1473 | 1477 | void init_test(void) |
| 1474 | 1478 | { |
| 1475 | 1479 | int linit1 = 2; |
| @@ -1484,6 +1488,9 @@ void init_test(void) · 8 unmodified lines | ||
| 1484 | 1488 | int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, }; |
| 1485 | 1489 | struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 }; |
| 1486 | 1490 | int linit17 = sizeof(linit17); |
| 1491 | + int zero = 0; | |
| 1492 | + /* Addresses on non-weak symbols are non-zero, but not the access itself */ | |
| 1493 | + int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 }; | |
| 1487 | 1494 | |
| 1488 | 1495 | printf("init_test:\n"); |
| 1489 | 1496 | |
| @@ -1577,6 +1584,9 @@ void init_test(void) · 87 unmodified lines | ||
| 1577 | 1584 | printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]); |
| 1578 | 1585 | printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22)); |
| 1579 | 1586 | printf("arrtype6: %d\n", sizeof(arrtype2)); |
| 1587 | + | |
| 1588 | + printf("sinit23= %d %d\n", sinit23[0], sinit23[1]); | |
| 1589 | + printf("linit18= %d %d\n", linit18[0], linit18[1]); | |
| 1580 | 1590 | } |
| 1581 | 1591 | |
| 1582 | 1592 | |