Skip to content

Commit 9c21f95

Browse files
heiherSixWeining
authored andcommitted
[LoongArch] Implement isZextFree
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 <[email protected]> Reviewed By: SixWeining, xen0n Differential Revision: https://reviews.llvm.org/D154819
1 parent 90e08c2 commit 9c21f95

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,21 @@ bool LoongArchTargetLowering::isLegalAddImmediate(int64_t Imm) const {
32553255
return isInt<12>(Imm);
32563256
}
32573257

3258+
bool LoongArchTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
3259+
// Zexts are free if they can be combined with a load.
3260+
// Don't advertise i32->i64 zextload as being free for LA64. It interacts
3261+
// poorly with type legalization of compares preferring sext.
3262+
if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
3263+
EVT MemVT = LD->getMemoryVT();
3264+
if ((MemVT == MVT::i8 || MemVT == MVT::i16) &&
3265+
(LD->getExtensionType() == ISD::NON_EXTLOAD ||
3266+
LD->getExtensionType() == ISD::ZEXTLOAD))
3267+
return true;
3268+
}
3269+
3270+
return TargetLowering::isZExtFree(Val, VT2);
3271+
}
3272+
32583273
bool LoongArchTargetLowering::hasAndNotCompare(SDValue Y) const {
32593274
// TODO: Support vectors.
32603275
if (Y.getValueType().isVector())

llvm/lib/Target/LoongArch/LoongArchISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class LoongArchTargetLowering : public TargetLowering {
204204

205205
bool isLegalICmpImmediate(int64_t Imm) const override;
206206
bool isLegalAddImmediate(int64_t Imm) const override;
207+
bool isZExtFree(SDValue Val, EVT VT2) const override;
207208

208209
bool hasAndNotCompare(SDValue Y) const override;
209210

llvm/test/CodeGen/LoongArch/zext-with-load-is-free.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
define zeroext i8 @test_zext_i8(ptr %p) nounwind {
88
; LA32-LABEL: test_zext_i8:
99
; LA32: # %bb.0:
10-
; LA32-NEXT: ld.b $a0, $a0, 0
11-
; LA32-NEXT: andi $a0, $a0, 255
10+
; LA32-NEXT: ld.bu $a0, $a0, 0
1211
; LA32-NEXT: ret
1312
;
1413
; LA64-LABEL: test_zext_i8:
1514
; LA64: # %bb.0:
16-
; LA64-NEXT: ld.b $a0, $a0, 0
17-
; LA64-NEXT: andi $a0, $a0, 255
15+
; LA64-NEXT: ld.bu $a0, $a0, 0
1816
; LA64-NEXT: ret
1917
%a = load i8, ptr %p, align 1
2018
br label %exit
@@ -26,16 +24,14 @@ define zeroext i16 @test_zext_i16(ptr %p) nounwind {
2624
; LA32-LABEL: test_zext_i16:
2725
; LA32: # %bb.0:
2826
; LA32-NEXT: ld.bu $a1, $a0, 0
29-
; LA32-NEXT: ld.b $a0, $a0, 1
27+
; LA32-NEXT: ld.bu $a0, $a0, 1
3028
; LA32-NEXT: slli.w $a0, $a0, 8
3129
; LA32-NEXT: or $a0, $a0, $a1
32-
; LA32-NEXT: bstrpick.w $a0, $a0, 15, 0
3330
; LA32-NEXT: ret
3431
;
3532
; LA64-LABEL: test_zext_i16:
3633
; LA64: # %bb.0:
37-
; LA64-NEXT: ld.h $a0, $a0, 0
38-
; LA64-NEXT: bstrpick.d $a0, $a0, 15, 0
34+
; LA64-NEXT: ld.hu $a0, $a0, 0
3935
; LA64-NEXT: ret
4036
%a = load i16, ptr %p, align 1
4137
br label %exit

0 commit comments

Comments
 (0)