This is an archive of the discontinued LLVM Phabricator instance.

[CSKY] Optimize multiplication with immediates
ClosedPublic

Authored by benshi001 on Jun 15 2023, 11:57 PM.

Details

Summary

Try to break a multiplication with a specific immediate to
an/a addition/subtraction of left shifts.

Diff Detail

Event Timeline

benshi001 created this revision.Jun 15 2023, 11:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 11:57 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
benshi001 requested review of this revision.Jun 15 2023, 11:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 11:57 PM
benshi001 updated this revision to Diff 532045.Jun 16 2023, 1:37 AM

It's better to add extra test case file to test every condition of // Break MULT into LSLI + ADDU/SUBU.

llvm/lib/Target/CSKY/CSKYISelLowering.cpp
119–121

No related code change.

1386

As this line code shows, it try to avoid data type larger than machine register size. And, CSKY is always 32-bit machine.

1394

Does (-1 - Imm).isPowerOf2() also make sense?

benshi001 marked 2 inline comments as done.Jun 19 2023, 1:47 AM

Thanks. I will create a new test file for all the cases.

llvm/lib/Target/CSKY/CSKYISelLowering.cpp
119–121

This is modified by clang-format.

1394

(-1 - Imm).isPowerOf2() will generate more instructions, I am not sure this is a win, though the multiplication is eliminated. So I left (-1 - Imm) unchanged. Maybe we can decide this case later.

benshi001 marked 2 inline comments as done.Jun 19 2023, 1:53 AM
benshi001 added inline comments.Jun 19 2023, 2:24 AM
llvm/lib/Target/CSKY/CSKYISelLowering.cpp
1394

Especially, for example,

a * -4097 will be generated to

lsli16, a1, a0, 12
addu16 a1, a0
movi16 a0, 0
subu16 a0, a1

An extra 'movi16` is needed.

benshi001 updated this revision to Diff 532579.Jun 19 2023, 2:35 AM
benshi001 edited the summary of this revision. (Show Details)
benshi001 set the repository for this revision to rG LLVM Github Monorepo.
benshi001 marked an inline comment as done.
benshi001 added inline comments.
llvm/lib/Target/CSKY/CSKYISelLowering.cpp
1386

Thanks. I have fixed this issue in my new uploaded patch.

benshi001 marked an inline comment as done.Jun 19 2023, 2:36 AM
benshi001 added inline comments.
llvm/test/CodeGen/CSKY/mul-imm.ll
270

This is the negative case, which can not be optimized due to data size exceeds 32-bit.

zixuan-wu accepted this revision.Jun 19 2023, 7:22 PM

LGTM. With minor change that the title of this revision can be more concrete.

llvm/lib/Target/CSKY/CSKYISelLowering.cpp
1386

Can the 32 hard code be const variable which can be defined in Subtarget consistently?

This revision is now accepted and ready to land.Jun 19 2023, 7:22 PM
benshi001 updated this revision to Diff 532778.Jun 19 2023, 7:47 PM
benshi001 marked an inline comment as done.Jun 19 2023, 7:50 PM
benshi001 added inline comments.
llvm/lib/Target/CSKY/CSKYISelLowering.cpp
1386

Done. I have created a CSKYSubtarget::getDataSizeInBits() for that.

benshi001 marked an inline comment as done.Jun 19 2023, 7:50 PM

This patch relies on https://reviews.llvm.org/D153105, which also needs your approval.

zixuan-wu added inline comments.Jun 19 2023, 7:55 PM
llvm/lib/Target/CSKY/CSKYSubtarget.h
210

I think the name getDataSizeInBits is not precise about target machine.

static const unsigned XLEN = 32;

may be better?

benshi001 updated this revision to Diff 532779.Jun 19 2023, 8:12 PM
benshi001 marked an inline comment as done.
benshi001 added inline comments.
llvm/lib/Target/CSKY/CSKYSubtarget.h
210

I prefer const unsigned XLen = 32;.

benshi001 marked an inline comment as done.Jun 19 2023, 8:12 PM
zixuan-wu added inline comments.Jun 19 2023, 8:38 PM
llvm/lib/Target/CSKY/CSKYSubtarget.h
210

LGTM

This revision was automatically updated to reflect the committed changes.