Skip to content

Commit 15e894b

Browse files
committed
[RISCV] Implement isZextFree
This returns true for 8-bit and 16-bit loads, allowing LBU/LHU to be selected and avoiding unnecessary masks. llvm-svn: 330943
1 parent e74f519 commit 15e894b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
211211
return (SrcBits == 64 && DestBits == 32);
212212
}
213213

214+
bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
215+
// Zexts are free if they can be combined with a load.
216+
if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
217+
EVT MemVT = LD->getMemoryVT();
218+
if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
219+
(Subtarget.is64Bit() && MemVT == MVT::i32)) &&
220+
(LD->getExtensionType() == ISD::NON_EXTLOAD ||
221+
LD->getExtensionType() == ISD::ZEXTLOAD))
222+
return true;
223+
}
224+
225+
return TargetLowering::isZExtFree(Val, VT2);
226+
}
227+
214228
// Changes the condition code and swaps operands if necessary, so the SetCC
215229
// operation matches one of the comparisons supported directly in the RISC-V
216230
// ISA.

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class RISCVTargetLowering : public TargetLowering {
4646
bool isLegalAddImmediate(int64_t Imm) const override;
4747
bool isTruncateFree(Type *SrcTy, Type *DstTy) const override;
4848
bool isTruncateFree(EVT SrcVT, EVT DstVT) const override;
49+
bool isZExtFree(SDValue Val, EVT VT2) const override;
4950

5051
// Provide custom lowering hooks for some operations.
5152
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ define i32 @test_zext_i8() {
1515
; RV32I-NEXT: bne a0, a1, .LBB0_3
1616
; RV32I-NEXT: # %bb.1: # %entry
1717
; RV32I-NEXT: lui a0, %hi(bytes+1)
18-
; RV32I-NEXT: lb a0, %lo(bytes+1)(a0)
19-
; RV32I-NEXT: andi a0, a0, 255
18+
; RV32I-NEXT: lbu a0, %lo(bytes+1)(a0)
2019
; RV32I-NEXT: addi a1, zero, 7
2120
; RV32I-NEXT: bne a0, a1, .LBB0_3
2221
; RV32I-NEXT: # %bb.2: # %if.end
@@ -46,15 +45,13 @@ define i32 @test_zext_i16() {
4645
; RV32I-LABEL: test_zext_i16:
4746
; RV32I: # %bb.0: # %entry
4847
; RV32I-NEXT: lui a0, 16
49-
; RV32I-NEXT: addi a1, a0, -120
50-
; RV32I-NEXT: lui a2, %hi(shorts)
51-
; RV32I-NEXT: lhu a2, %lo(shorts)(a2)
52-
; RV32I-NEXT: bne a2, a1, .LBB1_3
48+
; RV32I-NEXT: addi a0, a0, -120
49+
; RV32I-NEXT: lui a1, %hi(shorts)
50+
; RV32I-NEXT: lhu a1, %lo(shorts)(a1)
51+
; RV32I-NEXT: bne a1, a0, .LBB1_3
5352
; RV32I-NEXT: # %bb.1: # %entry
54-
; RV32I-NEXT: lui a1, %hi(shorts+2)
55-
; RV32I-NEXT: lh a1, %lo(shorts+2)(a1)
56-
; RV32I-NEXT: addi a0, a0, -1
57-
; RV32I-NEXT: and a0, a1, a0
53+
; RV32I-NEXT: lui a0, %hi(shorts+2)
54+
; RV32I-NEXT: lhu a0, %lo(shorts+2)(a0)
5855
; RV32I-NEXT: addi a1, zero, 7
5956
; RV32I-NEXT: bne a0, a1, .LBB1_3
6057
; RV32I-NEXT: # %bb.2: # %if.end

0 commit comments

Comments
 (0)