Skip to content

Commit

Permalink
[LoongArch] Implement isZextFree
Browse files Browse the repository at this point in the history
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@loongson.cn>

Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D154819
  • Loading branch information
heiher authored and SixWeining committed Jul 24, 2023
1 parent 90e08c2 commit 9c21f95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
15 changes: 15 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,21 @@ bool LoongArchTargetLowering::isLegalAddImmediate(int64_t Imm) const {
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())
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/LoongArch/LoongArchISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class LoongArchTargetLowering : public TargetLowering {

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;

Expand Down
12 changes: 4 additions & 8 deletions llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
define zeroext i8 @test_zext_i8(ptr %p) nounwind {
; LA32-LABEL: test_zext_i8:
; LA32: # %bb.0:
; LA32-NEXT: ld.b $a0, $a0, 0
; LA32-NEXT: andi $a0, $a0, 255
; LA32-NEXT: ld.bu $a0, $a0, 0
; LA32-NEXT: ret
;
; LA64-LABEL: test_zext_i8:
; LA64: # %bb.0:
; LA64-NEXT: ld.b $a0, $a0, 0
; LA64-NEXT: andi $a0, $a0, 255
; LA64-NEXT: ld.bu $a0, $a0, 0
; LA64-NEXT: ret
%a = load i8, ptr %p, align 1
br label %exit
Expand All @@ -26,16 +24,14 @@ define zeroext i16 @test_zext_i16(ptr %p) nounwind {
; LA32-LABEL: test_zext_i16:
; LA32: # %bb.0:
; LA32-NEXT: ld.bu $a1, $a0, 0
; LA32-NEXT: ld.b $a0, $a0, 1
; LA32-NEXT: ld.bu $a0, $a0, 1
; LA32-NEXT: slli.w $a0, $a0, 8
; LA32-NEXT: or $a0, $a0, $a1
; LA32-NEXT: bstrpick.w $a0, $a0, 15, 0
; LA32-NEXT: ret
;
; LA64-LABEL: test_zext_i16:
; LA64: # %bb.0:
; LA64-NEXT: ld.h $a0, $a0, 0
; LA64-NEXT: bstrpick.d $a0, $a0, 15, 0
; LA64-NEXT: ld.hu $a0, $a0, 0
; LA64-NEXT: ret
%a = load i16, ptr %p, align 1
br label %exit
Expand Down

0 comments on commit 9c21f95

Please sign in to comment.