Skip to content

Commit ed3a60c

Browse files
authored
[RISCV][GlobalISel] Fix selectShiftMask when shift mask is created from G_AND (#89602)
This patch fixes cases where G_AND creating the shift mask is eliminated if one of its source operands is a constant, resulting from an incorrect predicate.
1 parent d98e3d4 commit ed3a60c

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,28 @@ RISCVInstructionSelector::selectShiftMask(MachineOperand &Root) const {
177177

178178
APInt AndMask;
179179
Register AndSrcReg;
180+
// Try to combine the following pattern (applicable to other shift
181+
// instructions as well as 32-bit ones):
182+
//
183+
// %4:gprb(s64) = G_AND %3, %2
184+
// %5:gprb(s64) = G_LSHR %1, %4(s64)
185+
//
186+
// According to RISC-V's ISA manual, SLL, SRL, and SRA ignore other bits than
187+
// the lowest log2(XLEN) bits of register rs2. As for the above pattern, if
188+
// the lowest log2(XLEN) bits of register rd and rs2 of G_AND are the same,
189+
// then it can be eliminated. Given register rs1 or rs2 holding a constant
190+
// (the and mask), there are two cases G_AND can be erased:
191+
//
192+
// 1. the lowest log2(XLEN) bits of the and mask are all set
193+
// 2. the bits of the register being masked are already unset (zero set)
180194
if (mi_match(ShAmtReg, MRI, m_GAnd(m_Reg(AndSrcReg), m_ICst(AndMask)))) {
181195
APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1);
182196
if (ShMask.isSubsetOf(AndMask)) {
183197
ShAmtReg = AndSrcReg;
184198
} else {
185199
// SimplifyDemandedBits may have optimized the mask so try restoring any
186200
// bits that are known zero.
187-
KnownBits Known = KB->getKnownBits(ShAmtReg);
201+
KnownBits Known = KB->getKnownBits(AndSrcReg);
188202
if (ShMask.isSubsetOf(AndMask | Known.Zero))
189203
ShAmtReg = AndSrcReg;
190204
}

llvm/test/CodeGen/RISCV/GlobalISel/instruction-select/shift-rv32.mir

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,59 @@ body: |
188188
$x10 = COPY %4(s32)
189189
PseudoRET implicit $x10
190190
...
191+
192+
---
193+
name: srl_and_needed
194+
legalized: true
195+
regBankSelected: true
196+
tracksRegLiveness: true
197+
body: |
198+
bb.1.entry:
199+
liveins: $x10, $x11
200+
201+
; CHECK-LABEL: name: srl_and_needed
202+
; CHECK: liveins: $x10, $x11
203+
; CHECK-NEXT: {{ $}}
204+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
205+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
206+
; CHECK-NEXT: [[ANDI:%[0-9]+]]:gpr = ANDI [[COPY]], 15
207+
; CHECK-NEXT: [[SRL:%[0-9]+]]:gpr = SRL [[COPY1]], [[ANDI]]
208+
; CHECK-NEXT: $x10 = COPY [[SRL]]
209+
; CHECK-NEXT: PseudoRET implicit $x10
210+
%0:gprb(s32) = COPY $x10
211+
%1:gprb(s32) = COPY $x11
212+
%2:gprb(s32) = G_CONSTANT i32 15
213+
%3:gprb(s32) = G_AND %0, %2
214+
%4:gprb(s32) = G_LSHR %1, %3(s32)
215+
$x10 = COPY %4(s32)
216+
PseudoRET implicit $x10
217+
...
218+
219+
---
220+
name: srl_and_eliminated
221+
legalized: true
222+
regBankSelected: true
223+
tracksRegLiveness: true
224+
body: |
225+
bb.1.entry:
226+
liveins: $x10, $x11
227+
228+
; CHECK-LABEL: name: srl_and_eliminated
229+
; CHECK: liveins: $x10, $x11
230+
; CHECK-NEXT: {{ $}}
231+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
232+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
233+
; CHECK-NEXT: [[ANDI:%[0-9]+]]:gpr = ANDI [[COPY]], 47
234+
; CHECK-NEXT: [[SRL:%[0-9]+]]:gpr = SRL [[COPY1]], [[ANDI]]
235+
; CHECK-NEXT: $x10 = COPY [[SRL]]
236+
; CHECK-NEXT: PseudoRET implicit $x10
237+
%0:gprb(s32) = COPY $x10
238+
%1:gprb(s32) = COPY $x11
239+
%2:gprb(s32) = G_CONSTANT i32 15
240+
%3:gprb(s32) = G_CONSTANT i32 47
241+
%4:gprb(s32) = G_AND %0, %3
242+
%5:gprb(s32) = G_AND %4, %2
243+
%6:gprb(s32) = G_LSHR %1, %5(s32)
244+
$x10 = COPY %6(s32)
245+
PseudoRET implicit $x10
246+
...

llvm/test/CodeGen/RISCV/GlobalISel/instruction-select/shift-rv64.mir

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,63 @@ body: |
241241
$x10 = COPY %6(s64)
242242
PseudoRET implicit $x10
243243
...
244+
245+
---
246+
name: srl_and_needed
247+
legalized: true
248+
regBankSelected: true
249+
tracksRegLiveness: true
250+
body: |
251+
bb.1.entry:
252+
liveins: $x10, $x11
253+
254+
; CHECK-LABEL: name: srl_and_needed
255+
; CHECK: liveins: $x10, $x11
256+
; CHECK-NEXT: {{ $}}
257+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
258+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
259+
; CHECK-NEXT: [[ANDI:%[0-9]+]]:gpr = ANDI [[COPY]], 15
260+
; CHECK-NEXT: [[SRL:%[0-9]+]]:gpr = SRL [[COPY1]], [[ANDI]]
261+
; CHECK-NEXT: $x10 = COPY [[SRL]]
262+
; CHECK-NEXT: PseudoRET implicit $x10
263+
%0:gprb(s64) = COPY $x10
264+
%1:gprb(s64) = COPY $x11
265+
%2:gprb(s32) = G_CONSTANT i32 15
266+
%3:gprb(s32) = G_TRUNC %0(s64)
267+
%4:gprb(s32) = G_AND %3, %2
268+
%5:gprb(s64) = nneg G_ZEXT %4(s32)
269+
%6:gprb(s64) = G_LSHR %1, %5(s64)
270+
$x10 = COPY %6(s64)
271+
PseudoRET implicit $x10
272+
...
273+
274+
---
275+
name: srl_and_eliminated
276+
legalized: true
277+
regBankSelected: true
278+
tracksRegLiveness: true
279+
body: |
280+
bb.1.entry:
281+
liveins: $x10, $x11
282+
283+
; CHECK-LABEL: name: srl_and_eliminated
284+
; CHECK: liveins: $x10, $x11
285+
; CHECK-NEXT: {{ $}}
286+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
287+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
288+
; CHECK-NEXT: [[ANDI:%[0-9]+]]:gpr = ANDI [[COPY]], 79
289+
; CHECK-NEXT: [[SRL:%[0-9]+]]:gpr = SRL [[COPY1]], [[ANDI]]
290+
; CHECK-NEXT: $x10 = COPY [[SRL]]
291+
; CHECK-NEXT: PseudoRET implicit $x10
292+
%0:gprb(s64) = COPY $x10
293+
%1:gprb(s64) = COPY $x11
294+
%2:gprb(s32) = G_CONSTANT i32 15
295+
%3:gprb(s32) = G_TRUNC %0(s64)
296+
%7:gprb(s32) = G_CONSTANT i32 79
297+
%8:gprb(s32) = G_AND %3, %7
298+
%4:gprb(s32) = G_AND %8, %2
299+
%5:gprb(s64) = nneg G_ZEXT %4(s32)
300+
%6:gprb(s64) = G_LSHR %1, %5(s64)
301+
$x10 = COPY %6(s64)
302+
PseudoRET implicit $x10
303+
...

0 commit comments

Comments
 (0)