LSR should use expressions computed inside the loop to materialize values computed outside the loop
| 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 ] ;
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 ] ;
bb12: ; preds = %cond_true
*** %tmp.2 = mul uint %t_addr.0.0, 2 ;
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
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 }
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.
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
nice, thank you for closing this out Bill!