[PATCH] D140798: [InstCombine] Fold zero check followed by decrement to usub.sat
Nikita Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 11 01:56:35 PST 2023
nikic added a comment.
Before this patch, CVP converts
define i32 @test() {
entry:
br label %loop
loop:
%iv = phi i32 [ 1000, %entry ], [ %iv.next, %loop ]
%count = phi i32 [ 0, %entry ], [ %count.next, %loop ]
%cmp = icmp eq i32 %iv, 0
%iv.dec = add i32 %iv, -1
%iv.next = select i1 %cmp, i32 0, i32 %iv.dec
%count.next = add i32 %count, 1
br i1 %cmp, label %exit, label %loop
exit:
ret i32 %count
}
into
define i32 @test() {
entry:
br label %loop
loop: ; preds = %loop, %entry
%iv = phi i32 [ 1000, %entry ], [ %iv.dec, %loop ]
%count = phi i32 [ 0, %entry ], [ %count.next, %loop ]
%cmp = icmp eq i32 %iv, 0
%iv.dec = add i32 %iv, -1
%iv.next = select i1 %cmp, i32 0, i32 %iv.dec
%count.next = add i32 %count, 1
br i1 %cmp, label %exit, label %loop
exit: ; preds = %loop
ret i32 %count
}
The difference is that `%iv.next` in the phi node is replaced with `%iv.dec`, because the select and branch have the same condition.
After this patch, we instead have:
define i32 @test() {
entry:
br label %loop
loop: ; preds = %loop, %entry
%iv = phi i32 [ 1000, %entry ], [ %iv.next, %loop ]
%count = phi i32 [ 0, %entry ], [ %count.next, %loop ]
%cmp = icmp eq i32 %iv, 0
%iv.next = call i32 @llvm.usub.sat.i32(i32 %iv, i32 1)
%count.next = add i32 %count, 1
br i1 %cmp, label %exit, label %loop
exit: ; preds = %loop
ret i32 %count
}
The same optimization is still possible in principle, in that we could convert the usub.sat into a sub in this case.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D140798/new/
https://reviews.llvm.org/D140798
More information about the llvm-commits
mailing list