Skip to content

Commit 37b10af

Browse files
committed
[RISCV][GISel] Use correct shift width for GIShiftMask32 ComplexOperandMatcher.
We should use 32 instead of XLen. This allows us to remove 'and X, 31' from the shift amount.
1 parent 794afe0 commit 37b10af

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ class RISCVInstructionSelector : public InstructionSelector {
8383
bool selectMergeValues(MachineInstr &MI, MachineIRBuilder &MIB) const;
8484
bool selectUnmergeValues(MachineInstr &MI, MachineIRBuilder &MIB) const;
8585

86-
ComplexRendererFns selectShiftMask(MachineOperand &Root) const;
86+
ComplexRendererFns selectShiftMask(MachineOperand &Root,
87+
unsigned ShiftWidth) const;
88+
ComplexRendererFns selectShiftMaskXLen(MachineOperand &Root) const {
89+
return selectShiftMask(Root, STI.getXLen());
90+
}
91+
ComplexRendererFns selectShiftMask32(MachineOperand &Root) const {
92+
return selectShiftMask(Root, 32);
93+
}
8794
ComplexRendererFns selectAddrRegImm(MachineOperand &Root) const;
8895

8996
ComplexRendererFns selectSExtBits(MachineOperand &Root, unsigned Bits) const;
@@ -172,22 +179,18 @@ RISCVInstructionSelector::RISCVInstructionSelector(
172179
}
173180

174181
InstructionSelector::ComplexRendererFns
175-
RISCVInstructionSelector::selectShiftMask(MachineOperand &Root) const {
182+
RISCVInstructionSelector::selectShiftMask(MachineOperand &Root,
183+
unsigned ShiftWidth) const {
176184
if (!Root.isReg())
177185
return std::nullopt;
178186

179187
using namespace llvm::MIPatternMatch;
180188

181-
Register RootReg = Root.getReg();
182-
Register ShAmtReg = RootReg;
183-
const LLT ShiftLLT = MRI->getType(RootReg);
184-
unsigned ShiftWidth = ShiftLLT.getSizeInBits();
185-
assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!");
189+
Register ShAmtReg = Root.getReg();
186190
// Peek through zext.
187191
Register ZExtSrcReg;
188-
if (mi_match(ShAmtReg, *MRI, m_GZExt(m_Reg(ZExtSrcReg)))) {
192+
if (mi_match(ShAmtReg, *MRI, m_GZExt(m_Reg(ZExtSrcReg))))
189193
ShAmtReg = ZExtSrcReg;
190-
}
191194

192195
APInt AndMask;
193196
Register AndSrcReg;

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def gi_trailing_zero : GICustomOperandRenderer<"renderTrailingZeros">,
7676
// should add a LowLevelTypeByHwMode, and use that to define our XLenLLT instead
7777
// here.
7878
def GIShiftMaskXLen :
79-
GIComplexOperandMatcher<s32, "selectShiftMask">,
79+
GIComplexOperandMatcher<s32, "selectShiftMaskXLen">,
8080
GIComplexPatternEquiv<shiftMaskXLen>;
8181
def GIShiftMask32 :
82-
GIComplexOperandMatcher<s32, "selectShiftMask">,
82+
GIComplexOperandMatcher<s64, "selectShiftMask32">,
8383
GIComplexPatternEquiv<shiftMask32>;
8484

8585
def gi_sh1add_op : GIComplexOperandMatcher<s32, "selectSHXADDOp<1>">,

llvm/test/CodeGen/RISCV/GlobalISel/rv64zbb-zbkb.ll

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ define signext i32 @rol_i32(i32 signext %a, i32 signext %b) nounwind {
108108
; RV64I-LABEL: rol_i32:
109109
; RV64I: # %bb.0:
110110
; RV64I-NEXT: neg a2, a1
111-
; RV64I-NEXT: andi a1, a1, 31
112111
; RV64I-NEXT: sllw a1, a0, a1
113-
; RV64I-NEXT: andi a2, a2, 31
114112
; RV64I-NEXT: srlw a0, a0, a2
115113
; RV64I-NEXT: or a0, a1, a0
116114
; RV64I-NEXT: ret
@@ -128,9 +126,7 @@ define void @rol_i32_nosext(i32 signext %a, i32 signext %b, ptr %x) nounwind {
128126
; RV64I-LABEL: rol_i32_nosext:
129127
; RV64I: # %bb.0:
130128
; RV64I-NEXT: neg a3, a1
131-
; RV64I-NEXT: andi a1, a1, 31
132129
; RV64I-NEXT: sllw a1, a0, a1
133-
; RV64I-NEXT: andi a3, a3, 31
134130
; RV64I-NEXT: srlw a0, a0, a3
135131
; RV64I-NEXT: or a0, a1, a0
136132
; RV64I-NEXT: sw a0, 0(a2)
@@ -151,9 +147,7 @@ define signext i32 @rol_i32_neg_constant_rhs(i32 signext %a) nounwind {
151147
; RV64I: # %bb.0:
152148
; RV64I-NEXT: li a1, -2
153149
; RV64I-NEXT: neg a2, a0
154-
; RV64I-NEXT: andi a0, a0, 31
155150
; RV64I-NEXT: sllw a0, a1, a0
156-
; RV64I-NEXT: andi a2, a2, 31
157151
; RV64I-NEXT: srlw a1, a1, a2
158152
; RV64I-NEXT: or a0, a0, a1
159153
; RV64I-NEXT: ret
@@ -192,9 +186,7 @@ define signext i32 @ror_i32(i32 signext %a, i32 signext %b) nounwind {
192186
; RV64I-LABEL: ror_i32:
193187
; RV64I: # %bb.0:
194188
; RV64I-NEXT: neg a2, a1
195-
; RV64I-NEXT: andi a1, a1, 31
196189
; RV64I-NEXT: srlw a1, a0, a1
197-
; RV64I-NEXT: andi a2, a2, 31
198190
; RV64I-NEXT: sllw a0, a0, a2
199191
; RV64I-NEXT: or a0, a1, a0
200192
; RV64I-NEXT: ret
@@ -212,9 +204,7 @@ define void @ror_i32_nosext(i32 signext %a, i32 signext %b, ptr %x) nounwind {
212204
; RV64I-LABEL: ror_i32_nosext:
213205
; RV64I: # %bb.0:
214206
; RV64I-NEXT: neg a3, a1
215-
; RV64I-NEXT: andi a1, a1, 31
216207
; RV64I-NEXT: srlw a1, a0, a1
217-
; RV64I-NEXT: andi a3, a3, 31
218208
; RV64I-NEXT: sllw a0, a0, a3
219209
; RV64I-NEXT: or a0, a1, a0
220210
; RV64I-NEXT: sw a0, 0(a2)
@@ -235,9 +225,7 @@ define signext i32 @ror_i32_neg_constant_rhs(i32 signext %a) nounwind {
235225
; RV64I: # %bb.0:
236226
; RV64I-NEXT: li a1, -2
237227
; RV64I-NEXT: neg a2, a0
238-
; RV64I-NEXT: andi a0, a0, 31
239228
; RV64I-NEXT: srlw a0, a1, a0
240-
; RV64I-NEXT: andi a2, a2, 31
241229
; RV64I-NEXT: sllw a1, a1, a2
242230
; RV64I-NEXT: or a0, a0, a1
243231
; RV64I-NEXT: ret

0 commit comments

Comments
 (0)