This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Combine ADD to ADDZE
ClosedPublic

Authored by HLJ2009 on Aug 29 2018, 12:30 AM.

Details

Summary

On the ppc64le platform, I found that if ir has the following form,

define i64 @addze1(i64 %x, i64 %z) local_unnamed_addr #0 {
entry:
  %cmp = icmp ne i64 %z, CONSTANT      (-32767 <= CONSTANT <= 32768)
  %conv1 = zext i1 %cmp to i64
  %add = add nsw i64 %conv1, %x
  ret i64 %add
}

we can optimize it to the form below.

                                when C == 0
                            --> addze X, (addic Z, -1))
                           /
add X, (zext(setne Z, C))--
                           \    when -32768 <= -C <= 32767 && C != 0
                            --> addze X, (addic (addi Z, -C), -1)

Besides , if ir has the following form,

define i64 @addze2(i64 %x, i64 %z) local_unnamed_addr #0 {
entry:
  %cmp = icmp eq i64 %z, CONSTANT      (-32767 <= CONSTANT <= 32768)
  %conv1 = zext i1 %cmp to i64
  %add = add nsw i64 %conv1, %x
  ret i64 %add
}

we can optimize it to the form below.

                                when C == 0
                            --> addze X, (subfic Z, 0)
                           /
add X, (zext(setne Z, C))--
                           \    when -32768 <= -C <= 32767 && C != 0
                            --> addze X, (subfic (addi Z, -C), 0)

Diff Detail

Repository
rL LLVM

Event Timeline

HLJ2009 created this revision.Aug 29 2018, 12:30 AM
HLJ2009 edited the summary of this revision. (Show Details)Aug 29 2018, 12:31 AM

Does this code pattern frequently happen?

Does this code pattern frequently happen?

This code does not necessarily be executed frequently, but I think this is also a point that can be optimized.

nemanjai accepted this revision.Aug 31 2018, 5:53 AM

LGTM aside from a minor nit which you can fix on the commit.

llvm/lib/Target/PowerPC/PPCISelLowering.cpp
14177 ↗(On Diff #163016)

I think it might be clearer if you show that it is only the carry from the addic that is being used in the addze (since addze only takes one explicit operand). Perhaps something like:
addze X, (addic Z, -1).1 or addze X, (addic Z, -1).carry

This revision is now accepted and ready to land.Aug 31 2018, 5:53 AM
HLJ2009 updated this revision to Diff 163652.Sep 2 2018, 8:16 PM

Modify comments using nemanjai's suggestion

HLJ2009 updated this revision to Diff 164358.Sep 6 2018, 11:58 PM

Limit optimization platforms to ppc64 and ppc64le

Hi Steven @steven.zhang. I don't have the commit access. Could you help to commit the change for me? Thanks.

This revision was automatically updated to reflect the committed changes.