[PATCH] D153193: [LoongArch] Optimize conditional selection of integer

hev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 17 03:15:45 PDT 2023


hev created this revision.
hev added reviewers: SixWeining, wangleiat, xen0n, xry111.
hev added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
hev requested review of this revision.
Herald added a subscriber: llvm-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153193

Files:
  llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
  llvm/test/CodeGen/LoongArch/atomicrmw-uinc-udec-wrap.ll


Index: llvm/test/CodeGen/LoongArch/atomicrmw-uinc-udec-wrap.ll
===================================================================
--- llvm/test/CodeGen/LoongArch/atomicrmw-uinc-udec-wrap.ll
+++ llvm/test/CodeGen/LoongArch/atomicrmw-uinc-udec-wrap.ll
@@ -22,8 +22,6 @@
 ; LA64-NEXT:    addi.d $a5, $a5, 1
 ; LA64-NEXT:    xori $a6, $a6, 1
 ; LA64-NEXT:    masknez $a5, $a5, $a6
-; LA64-NEXT:    maskeqz $a6, $zero, $a6
-; LA64-NEXT:    or $a5, $a6, $a5
 ; LA64-NEXT:    andi $a5, $a5, 255
 ; LA64-NEXT:    sll.w $a5, $a5, $a0
 ; LA64-NEXT:    and $a6, $a3, $a4
@@ -77,8 +75,6 @@
 ; LA64-NEXT:    addi.d $a5, $a5, 1
 ; LA64-NEXT:    xori $a6, $a6, 1
 ; LA64-NEXT:    masknez $a5, $a5, $a6
-; LA64-NEXT:    maskeqz $a6, $zero, $a6
-; LA64-NEXT:    or $a5, $a6, $a5
 ; LA64-NEXT:    bstrpick.d $a5, $a5, 15, 0
 ; LA64-NEXT:    sll.w $a5, $a5, $a0
 ; LA64-NEXT:    and $a6, $a3, $a4
@@ -123,8 +119,6 @@
 ; LA64-NEXT:    xori $a1, $a1, 1
 ; LA64-NEXT:    addi.d $a4, $a3, 1
 ; LA64-NEXT:    masknez $a4, $a4, $a1
-; LA64-NEXT:    maskeqz $a1, $zero, $a1
-; LA64-NEXT:    or $a4, $a1, $a4
 ; LA64-NEXT:  .LBB2_3: # %atomicrmw.start
 ; LA64-NEXT:    # Parent Loop BB2_1 Depth=1
 ; LA64-NEXT:    # => This Inner Loop Header: Depth=2
@@ -164,8 +158,6 @@
 ; LA64-NEXT:    xori $a2, $a2, 1
 ; LA64-NEXT:    addi.d $a4, $a3, 1
 ; LA64-NEXT:    masknez $a4, $a4, $a2
-; LA64-NEXT:    maskeqz $a2, $zero, $a2
-; LA64-NEXT:    or $a4, $a2, $a4
 ; LA64-NEXT:  .LBB3_3: # %atomicrmw.start
 ; LA64-NEXT:    # Parent Loop BB3_1 Depth=1
 ; LA64-NEXT:    # => This Inner Loop Header: Depth=2
Index: llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
===================================================================
--- llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
+++ llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
@@ -1199,6 +1199,10 @@
 
 /// Select
 
+def : Pat<(select GPR:$cond, GPR:$t, (i32 0)), (MASKEQZ GPR:$t, GPR:$cond)>;
+def : Pat<(select GPR:$cond, (i32 0), GPR:$f), (MASKNEZ GPR:$f, GPR:$cond)>;
+def : Pat<(select GPR:$cond, GPR:$t, (i64 0)), (MASKEQZ GPR:$t, GPR:$cond)>;
+def : Pat<(select GPR:$cond, (i64 0), GPR:$f), (MASKNEZ GPR:$f, GPR:$cond)>;
 def : Pat<(select GPR:$cond, GPR:$t, GPR:$f),
           (OR (MASKEQZ GPR:$t, GPR:$cond), (MASKNEZ GPR:$f, GPR:$cond))>;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153193.532377.patch
Type: text/x-patch
Size: 2273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230617/8a5dcc1a/attachment-0001.bin>


More information about the llvm-commits mailing list