Bug 113422 - Missed optimizations in the presence of pointer chains
Summary: Missed optimizations in the presence of pointer chains
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: ipa (show other bugs)
Version: 14.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2024-01-16 13:21 UTC by Arnet Colin
Modified: 2024-01-25 13:40 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-01-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Arnet Colin 2024-01-16 13:21:51 UTC
https://godbolt.org/z/hdWKn4bjc

All three functions write to the same variable (b). Clang is able to optimize this. GCC -O3 cannot always do this.
Assembly for foo is writing to **d instead to directly write 1 to b. There is similar behavior for bar and baz. Clang optimizes the code to directly write to b.
Baz is fully optimized only if the foo and bar are removed.

int b = 0;
static int *c = &b;
static int **d = &c;
static int ***e = &d;

void foo() {***e = 1;}
void bar() {**d = 1;}
void baz() {*c = 1;}

Assembly code:
foo:
	movq	d(%rip), %rax
	movq	(%rax), %rax
	movl	$1, (%rax)
	ret
Comment 1 Andrew Pinski 2024-01-16 14:36:30 UTC
Yes gcc static read only pass does not cycle . So it only able to remove currently one level of indirection.
Comment 2 Jan Hubicka 2024-01-25 12:58:14 UTC
Cycling read-only var discovery would be quite expensive, since you need to interleave it with early opts each round.  I wonder how llvm handles this?

I think there is more hope with IPA-PTA getting scalable version at -O2 and possibly being able to solve this.