Skip to content

Commit 5dc8d61

Browse files
authored
[RISCV][GISel] Implement zexti32/zexti16 ComplexPatterns. (#115097)
1 parent efe87fb commit 5dc8d61

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class RISCVInstructionSelector : public InstructionSelector {
8787
ComplexRendererFns selectShiftMask(MachineOperand &Root) const;
8888
ComplexRendererFns selectAddrRegImm(MachineOperand &Root) const;
8989

90+
ComplexRendererFns selectZExtBits(MachineOperand &Root, unsigned Bits) const;
91+
template <unsigned Bits>
92+
ComplexRendererFns selectZExtBits(MachineOperand &Root) const {
93+
return selectZExtBits(Root, Bits);
94+
}
95+
9096
ComplexRendererFns selectSHXADDOp(MachineOperand &Root, unsigned ShAmt) const;
9197
template <unsigned ShAmt>
9298
ComplexRendererFns selectSHXADDOp(MachineOperand &Root) const {
@@ -242,6 +248,24 @@ RISCVInstructionSelector::selectShiftMask(MachineOperand &Root) const {
242248
return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(ShAmtReg); }}};
243249
}
244250

251+
InstructionSelector::ComplexRendererFns
252+
RISCVInstructionSelector::selectZExtBits(MachineOperand &Root,
253+
unsigned Bits) const {
254+
if (!Root.isReg())
255+
return std::nullopt;
256+
Register RootReg = Root.getReg();
257+
258+
Register RegX;
259+
uint64_t Mask = maskTrailingOnes<uint64_t>(Bits);
260+
if (mi_match(RootReg, *MRI, m_GAnd(m_Reg(RegX), m_SpecificICst(Mask)))) {
261+
return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(RegX); }}};
262+
}
263+
264+
// TODO: Use computeKnownBits.
265+
266+
return std::nullopt;
267+
}
268+
245269
InstructionSelector::ComplexRendererFns
246270
RISCVInstructionSelector::selectSHXADDOp(MachineOperand &Root,
247271
unsigned ShAmt) const {

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ def gi_sh2add_uw_op : GIComplexOperandMatcher<s32, "selectSHXADD_UWOp<2>">,
9696
def gi_sh3add_uw_op : GIComplexOperandMatcher<s32, "selectSHXADD_UWOp<3>">,
9797
GIComplexPatternEquiv<sh3add_uw_op>;
9898

99+
def gi_zexti32 : GIComplexOperandMatcher<s64, "selectZExtBits<32>">,
100+
GIComplexPatternEquiv<zexti32>;
101+
def gi_zexti16 : GIComplexOperandMatcher<s32, "selectZExtBits<16>">,
102+
GIComplexPatternEquiv<zexti16>;
103+
99104
// Ptr type used in patterns with GlobalISelEmitter
100105
def PtrVT : PtrValueTypeByHwMode<XLenVT, 0>;
101106

llvm/test/CodeGen/RISCV/GlobalISel/rv32zbkb.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ define i32 @pack_i32(i32 %a, i32 %b) nounwind {
1616
;
1717
; RV32ZBKB-LABEL: pack_i32:
1818
; RV32ZBKB: # %bb.0:
19-
; RV32ZBKB-NEXT: zext.h a0, a0
20-
; RV32ZBKB-NEXT: slli a1, a1, 16
21-
; RV32ZBKB-NEXT: or a0, a1, a0
19+
; RV32ZBKB-NEXT: pack a0, a0, a1
2220
; RV32ZBKB-NEXT: ret
2321
%shl = and i32 %a, 65535
2422
%shl1 = shl i32 %b, 16

llvm/test/CodeGen/RISCV/GlobalISel/rv64zbkb.ll

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ define i64 @pack_i64(i64 %a, i64 %b) nounwind {
8585
;
8686
; RV64ZBKB-LABEL: pack_i64:
8787
; RV64ZBKB: # %bb.0:
88-
; RV64ZBKB-NEXT: slli a0, a0, 32
89-
; RV64ZBKB-NEXT: srli a0, a0, 32
90-
; RV64ZBKB-NEXT: slli a1, a1, 32
91-
; RV64ZBKB-NEXT: or a0, a1, a0
88+
; RV64ZBKB-NEXT: pack a0, a0, a1
9289
; RV64ZBKB-NEXT: ret
9390
%shl = and i64 %a, 4294967295
9491
%shl1 = shl i64 %b, 32
@@ -109,12 +106,9 @@ define i64 @pack_i64_2(i32 signext %a, i32 signext %b) nounwind {
109106
;
110107
; RV64ZBKB-LABEL: pack_i64_2:
111108
; RV64ZBKB: # %bb.0:
112-
; RV64ZBKB-NEXT: slli a0, a0, 32
113-
; RV64ZBKB-NEXT: srli a0, a0, 32
114109
; RV64ZBKB-NEXT: slli a1, a1, 32
115110
; RV64ZBKB-NEXT: srli a1, a1, 32
116-
; RV64ZBKB-NEXT: slli a1, a1, 32
117-
; RV64ZBKB-NEXT: or a0, a1, a0
111+
; RV64ZBKB-NEXT: pack a0, a0, a1
118112
; RV64ZBKB-NEXT: ret
119113
%zexta = zext i32 %a to i64
120114
%zextb = zext i32 %b to i64
@@ -343,10 +337,7 @@ define i64 @pack_i64_allWUsers(i32 signext %0, i32 signext %1, i32 signext %2) {
343337
; RV64ZBKB-NEXT: add a0, a1, a0
344338
; RV64ZBKB-NEXT: slli a0, a0, 32
345339
; RV64ZBKB-NEXT: srli a0, a0, 32
346-
; RV64ZBKB-NEXT: slli a0, a0, 32
347-
; RV64ZBKB-NEXT: slli a2, a2, 32
348-
; RV64ZBKB-NEXT: srli a2, a2, 32
349-
; RV64ZBKB-NEXT: or a0, a0, a2
340+
; RV64ZBKB-NEXT: pack a0, a2, a0
350341
; RV64ZBKB-NEXT: ret
351342
%4 = add i32 %1, %0
352343
%5 = zext i32 %4 to i64

0 commit comments

Comments
 (0)