llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[GVN] GVN/Fix rle-no-phi-translate.ll

Open madhur13490 opened this issue 1 year ago • 3 comments

; FIXME: This should be promotable, but memdep/gvn don't track values
; path/edge sensitively enough.

define i32 @g(ptr %b, ptr %c) nounwind {
entry:
        store i32 1, ptr %b
        store i32 2, ptr %c
        
	%t1 = icmp eq ptr %b, null		
	br i1 %t1, label %bb, label %bb2

bb:		
	br label %bb2

bb2:		
	%c_addr.0 = phi ptr [ %b, %entry ], [ %c, %bb ]		
	%cv = load i32, ptr %c_addr.0, align 4		
	ret i32 %cv
}

godbolt: https://godbolt.org/z/6PE1MqaYM

This test is marked as XFAIL. It expects no load at the end and return 2.

madhur13490 avatar Jul 22 '25 11:07 madhur13490

Hi @nikic I came across this test and curious to understand what would it take to fix this in GVN?

madhur13490 avatar Jul 22 '25 11:07 madhur13490

You cannot forward from either store, since %b may alias with %c (https://alive2.llvm.org/ce/z/uAJxyf). To remove the load we would need to use a select (https://alive2.llvm.org/ce/z/deWKdT), but I don’t think we expect GVN to do this sort of transform.

ManuelJBrito avatar Sep 14 '25 09:09 ManuelJBrito

It's a bit hard to understand what this test is actually supposing to test. The case where the branch condition folds away to true/false seems uninteresting. I guess there is a minor deficiency here in that GVN fails to handle this by itself, but normally that branch would be optimized away earlier.

If we use a generic branch condition, then to do full load GVN we need noalias: https://godbolt.org/z/no4d9qTEW

But maybe this is about load PRE. For example, if we swap the arguments of the phi, we actually do get load PRE: https://godbolt.org/z/Tq9MhYEzc This forwards the store to c and inserts a PRE load for b.

Peculiarly, this only works with this specific order of phi arguments. I thought at first that maybe GVN doesn't want to make this transform if it requires inserting a critical edge, but that's not it, because it's not applied even if we make the edge explicit: https://godbolt.org/z/aqPsb15r3

This does seem unexpected. Possibly it's some kind of MDA caching artifact.

nikic avatar Sep 14 '25 10:09 nikic