This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][CodeGen] Peephole optimization of Cmp+Bcc sequences with unconditional branch
Needs ReviewPublic

Authored by eastig on Jul 11 2016, 10:23 AM.

Details

Reviewers
t.p.northover
Summary

This is a peephole optimization of Cmp+Bcc sequences with an unconditional branch. Such sequences can appear after the patch: http://reviews.llvm.org/D18838

When we compare with zero a result can be known. We can figure out which path is taken by Bcc and substitute Bcc by an unconditional branch to that path.

Rules:

SUBS reg, 0; B.LO              => B <false path>
SUBS reg, 0; B.HS <label> => B <label>

Diff Detail

Event Timeline

eastig updated this revision to Diff 63530.Jul 11 2016, 10:23 AM
eastig retitled this revision from to [AArch64][CodeGen] Peephole optimization of Cmp+Bcc sequences with unconditional branch.
eastig updated this object.
eastig added a reviewer: t.p.northover.
eastig added a subscriber: llvm-commits.

Hi Evgeny,

I'm afraid I don't understand the transform from the description. This looks like a simple compare-with-zero pattern - how do we know which branch will be taken?

James

Hi Evgeny,

I'm afraid I don't understand the transform from the description. This looks like a simple compare-with-zero pattern - how do we know which branch will be taken?

James

'SUBS WZR, Wn, #0' always sets C to 1 because nothing is borrowed. So we can say where B.LO and B.HS will go: B.LO will fall through (C is not clear); B.HS will take its target (C is set).
IR, which corresponds this, looks like:

%2 = icmp ult/uge i64 %1, 0
 br i1 %2, label %ret1, label %ret0

BTW it is not optimized in the middle-end. In the back-end the optimized sequence 'SUBS; Bcc' is a result of some other optimizations. In a test, we have seen this case, there is a sequence of pointer expressions and a comparison.