[PATCH] D154819: [LoongArch] Implement isZextFree

hev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 01:08:10 PDT 2023


hev created this revision.
hev added reviewers: SixWeining, xen0n.
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 returns true for 8-bit and 16-bit loads, allowing ld.bu/ld.hu to be selected and avoiding unnecessary masks.

Signed-off-by: WANG Rui <wangrui at loongson.cn>

Depends on D154818 <https://reviews.llvm.org/D154818>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154819

Files:
  llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
  llvm/lib/Target/LoongArch/LoongArchISelLowering.h
  llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll


Index: llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll
===================================================================
--- llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll
+++ llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll
@@ -16,8 +16,7 @@
 ; LA32-NEXT:    ori $a2, $zero, 136
 ; LA32-NEXT:    bne $a1, $a2, .LBB0_3
 ; LA32-NEXT:  # %bb.1: # %entry
-; LA32-NEXT:    ld.b $a0, $a0, 1
-; LA32-NEXT:    andi $a0, $a0, 255
+; LA32-NEXT:    ld.bu $a0, $a0, 1
 ; LA32-NEXT:    ori $a1, $zero, 7
 ; LA32-NEXT:    bne $a0, $a1, .LBB0_3
 ; LA32-NEXT:  # %bb.2: # %if.end
@@ -35,8 +34,7 @@
 ; LA64-NEXT:    ori $a2, $zero, 136
 ; LA64-NEXT:    bne $a1, $a2, .LBB0_3
 ; LA64-NEXT:  # %bb.1: # %entry
-; LA64-NEXT:    ld.b $a0, $a0, 1
-; LA64-NEXT:    andi $a0, $a0, 255
+; LA64-NEXT:    ld.bu $a0, $a0, 1
 ; LA64-NEXT:    ori $a1, $zero, 7
 ; LA64-NEXT:    bne $a0, $a1, .LBB0_3
 ; LA64-NEXT:  # %bb.2: # %if.end
@@ -72,8 +70,7 @@
 ; LA32-NEXT:    ld.hu $a2, $a0, 0
 ; LA32-NEXT:    bne $a2, $a1, .LBB1_3
 ; LA32-NEXT:  # %bb.1: # %entry
-; LA32-NEXT:    ld.h $a0, $a0, 2
-; LA32-NEXT:    bstrpick.w $a0, $a0, 15, 0
+; LA32-NEXT:    ld.hu $a0, $a0, 2
 ; LA32-NEXT:    ori $a1, $zero, 7
 ; LA32-NEXT:    bne $a0, $a1, .LBB1_3
 ; LA32-NEXT:  # %bb.2: # %if.end
@@ -92,8 +89,7 @@
 ; LA64-NEXT:    ld.hu $a2, $a0, 0
 ; LA64-NEXT:    bne $a2, $a1, .LBB1_3
 ; LA64-NEXT:  # %bb.1: # %entry
-; LA64-NEXT:    ld.h $a0, $a0, 2
-; LA64-NEXT:    bstrpick.d $a0, $a0, 15, 0
+; LA64-NEXT:    ld.hu $a0, $a0, 2
 ; LA64-NEXT:    ori $a1, $zero, 7
 ; LA64-NEXT:    bne $a0, $a1, .LBB1_3
 ; LA64-NEXT:  # %bb.2: # %if.end
Index: llvm/lib/Target/LoongArch/LoongArchISelLowering.h
===================================================================
--- llvm/lib/Target/LoongArch/LoongArchISelLowering.h
+++ llvm/lib/Target/LoongArch/LoongArchISelLowering.h
@@ -204,6 +204,7 @@
 
   bool isLegalICmpImmediate(int64_t Imm) const override;
   bool isLegalAddImmediate(int64_t Imm) const override;
+  bool isZExtFree(SDValue Val, EVT VT2) const override;
 
   bool hasAndNotCompare(SDValue Y) const override;
 
Index: llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
===================================================================
--- llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -3251,6 +3251,21 @@
   return isInt<12>(Imm);
 }
 
+bool LoongArchTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
+  // Zexts are free if they can be combined with a load.
+  // Don't advertise i32->i64 zextload as being free for LA64. It interacts
+  // poorly with type legalization of compares preferring sext.
+  if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
+    EVT MemVT = LD->getMemoryVT();
+    if ((MemVT == MVT::i8 || MemVT == MVT::i16) &&
+        (LD->getExtensionType() == ISD::NON_EXTLOAD ||
+         LD->getExtensionType() == ISD::ZEXTLOAD))
+      return true;
+  }
+
+  return TargetLowering::isZExtFree(Val, VT2);
+}
+
 bool LoongArchTargetLowering::hasAndNotCompare(SDValue Y) const {
   // TODO: Support vectors.
   if (Y.getValueType().isVector())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154819.538537.patch
Type: text/x-patch
Size: 3120 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230710/75c3ab3a/attachment-0001.bin>


More information about the llvm-commits mailing list