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

LSR should use expressions computed inside the loop to materialize values computed outside the loop

Open lattner opened this issue 20 years ago • 4 comments

Bugzilla Link 866
Version 1.0
OS All
CC @efriedma-quic,@ekatz,@nlewycky

Extended Description

This is a special case of Bug 863. Consider this:

int %foo(int* %a, int %t, int %C) { entry: br label %cond_true

cond_true: ; preds = %cond_true, %entry %x.0.0 = phi int [ 0, %entry ], [ %tmp9, %cond_true ] ; [#uses=3] %t_addr.0.0 = phi int [ %t, %entry ], [ %tmp7, %cond_true ] ; [#uses=1] %tmp2 = shl int %x.0.0, ubyte 2 ; <int*> [#uses=1] %tmp3 = cast int %tmp2 to int ; [#uses=1] %tmp5 = add int %t_addr.0.0, %t_addr.0.0 ; [#uses=1] %tmp7 = add int %tmp5, %tmp3 ; [#uses=2] %tmp9 = add int %x.0.0, 1 ; [#uses=2] %tmp = setgt int %C, 39 ; [#uses=1] br bool %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true ret int %tmp7 }

There are two uses of tmp7: the ret and the PHI. Because of the phi, the value of tmp7 has to be computed inside the loop for the next iteration. However, LSR doesn't keep track of this and ends up recomputing the value outside the loop, producing this code:

int %foo(int* %a, int %t, int %C) { entry: br label %cond_true

cond_true: ; preds = %cond_true, %entry %iv. = phi uint [ 0, %entry ], [ %iv..inc, %cond_true ] ; [#uses=2] %t_addr.0.0 = phi int [ %t, %entry ], [ %tmp.1, %cond_true ] ; [#uses=1] %t_addr.0.0 = cast int %t_addr.0.0 to uint ; [#uses=2] %tmp = setgt int %C, 39 ; [#uses=1] %iv..inc = add uint %iv., 4 ; [#uses=2] %tmp. = mul uint %t_addr.0.0, 2 ; [#uses=1] %tmp.1 = add uint %iv., %tmp. ; [#uses=1] %tmp.1 = cast uint %tmp.1 to int ; [#uses=1] br bool %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true *** %tmp.2 = mul uint %t_addr.0.0, 2 ; [#uses=1] *** %tmp.3 = add uint %iv..inc, %tmp.2 ; [#uses=1] *** %tmp.4 = add uint %tmp.3, 4294967292 ; [#uses=1] *** %tmp.4 = cast uint %tmp.4 to int ; [#uses=1] ret int %tmp.4 }

Note that LSR is tricky: because it is going to reinsert the entire expression it uses the post- incremented ivar value to avoid coallescing issues. It would have been much better for it to just realize that it could "ret int %tmp %tmp.1" instead of all the ***'d code. Because it uses the the preinc value inside the loop and the postinc value outside the loop, there is no way normal CSE will catch it, and we don't. From this, we generate this nasty code:

_foo: li r2, 0 LBB1_1: ;cond_true mr r3, r4 slwi r4, r3, 1 cmpwi cr0, r5, 40 add r4, r2, r4 addi r2, r2, 4 blt cr0, LBB1_1 ;cond_true LBB1_2: ;bb12 *** slwi r3, r3, 1 *** add r2, r2, r3 *** addi r3, r2, -4 blr

doh. mr r3, r2 would have been much faster, and would have probably eliminated a copy in the loop.

-Chris

lattner avatar Aug 03 '06 08:08 lattner

Doesn't seem to be reproducible on latest build (and actually, at least, since llvm 3.2), using the following llvm-ir:

define i32 @​foo(i32* %a, i32 %t, i32 %C) { entry: br label %cond_true

cond_true: %x.0.0 = phi i32 [ 0, %entry ], [ %tmp9, %cond_true ] %t_addr.0.0 = phi i32 [ %t, %entry ], [ %tmp7, %cond_true ] %tmp2 = shl i32 %x.0.0, 2 %tmp3 = bitcast i32 %tmp2 to i32 %tmp5 = add i32 %t_addr.0.0, %t_addr.0.0 %tmp7 = add i32 %tmp5, %tmp3 %tmp9 = add i32 %x.0.0, 1 %tmp = icmp sgt i32 %C, 39 br i1 %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true ret i32 %tmp7 }

ekatz avatar Nov 26 '19 17:11 ekatz

Updated version of asm (updated for syntax and sanity): declare i32 @​bar()

define i32 @​foo(i32* %a, i32 %t) { entry: br label %cond_true

cond_true: %x.0.0 = phi i32 [ 0, %entry ], [ %tmp9, %cond_true ] %t_addr.0.0 = phi i32 [ %t, %entry ], [ %tmp7, %cond_true ] %tmp2 = shl i32 %x.0.0, 2 %tmp3 = bitcast i32 %tmp2 to i32 %tmp5 = add i32 %t_addr.0.0, %t_addr.0.0 %tmp7 = add i32 %tmp5, %tmp3 %tmp9 = add i32 %x.0.0, 1 %C = call i32 @​bar() %tmp = icmp sgt i32 %C, 39 br i1 %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true ret i32 %tmp7 }

It looks like the bug is still there.

efriedma-quic avatar Jun 18 '08 10:06 efriedma-quic

This looks like it's been addressed. At the very least, we'll need a new example of where LSR is failing. Chris's example compiles down to this:

foo:                                    # @foo
	.p2align	3
	.quad	.Lfunc_begin0
	.quad	.TOC.@tocbase
	.quad	0
	.text
.Lfunc_begin0:
# %bb.0:                                # %entry
	li 3, -4
	cmpwi	5, 39
.LBB0_1:                                # %cond_true
                                        # =>This Inner Loop Header: Depth=1
	slwi 5, 4, 1
	add 4, 3, 5
	addi 4, 4, 4
	addi 3, 3, 4
	bc 4, 1, .LBB0_1
# %bb.2:                                # %bb12
	add 3, 5, 3
	blr

bwendling avatar Mar 05 '22 09:03 bwendling

nice, thank you for closing this out Bill!

lattner avatar Mar 05 '22 17:03 lattner