This is an archive of the discontinued LLVM Phabricator instance.

[LoongArch] Optimize conditional selection of integer
ClosedPublic

Authored by hev on Jun 17 2023, 3:15 AM.

Details

Summary

This patch optimizes code generation by leveraging the zeroing behavior of the maskeqz/masknez instructions.

int sel(int a, int b)
{
    return (a < b) ? a : 0;
}
slt	$a1,$a0,$a1
masknez	$a2,$r0,$a1
maskeqz	$a0,$a0,$a1
or	$a0,$a0,$a2

>

slt	$a1,$a0,$a1
maskeqz	$a0,$a0,$a1

Diff Detail

Event Timeline

hev created this revision.Jun 17 2023, 3:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2023, 3:15 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
hev requested review of this revision.Jun 17 2023, 3:15 AM
xry111 added inline comments.Jun 17 2023, 7:46 AM
llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
1202

Can we just write

def : Pat<(select GPR:$cond, GPR:$t, 0), (MASKEQZ GPR:$t, GPR:$cond)>;

here? I see RISC-V uses an untyped 0 in similar select patterns.

hev updated this revision to Diff 532402.Jun 17 2023, 8:03 AM
  1. Update to use untyped immediate operand in select patterns.
  2. Add test cases.
hev marked an inline comment as done.Jun 17 2023, 8:04 AM
hev added inline comments.
llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
1202

Yeap, thanks.

SixWeining accepted this revision.Jun 17 2023, 8:14 AM

Thanks for the improvement.

This revision is now accepted and ready to land.Jun 17 2023, 8:14 AM
This revision was automatically updated to reflect the committed changes.

Sorry, I forgot to amend the author name and email when merging this patch. I will revert and re-commit later.