Skip to content

Commit 7f9a50f

Browse files
authored
[RISCV][GISel] Select G_SELECT (#67614)
This patch adds minimal support for selecting G_SELECT. In the future we may want to fix this patch to select the other opcodes or handle that in the combiner.
1 parent f3c477a commit 7f9a50f

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ class RISCVInstructionSelector : public InstructionSelector {
4444
const TargetRegisterClass *
4545
getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB) const;
4646

47+
// tblgen-erated 'select' implementation, used as the initial selector for
48+
// the patterns that don't require complex C++.
4749
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
50+
51+
// Custom selection methods
4852
bool selectCopy(MachineInstr &MI, MachineRegisterInfo &MRI) const;
4953
bool selectConstant(MachineInstr &MI, MachineIRBuilder &MIB,
5054
MachineRegisterInfo &MRI) const;
5155
bool selectSExtInreg(MachineInstr &MI, MachineIRBuilder &MIB) const;
56+
bool selectSelect(MachineInstr &MI, MachineIRBuilder &MIB,
57+
MachineRegisterInfo &MRI) const;
5258

5359
bool earlySelectShift(unsigned Opc, MachineInstr &I, MachineIRBuilder &MIB,
5460
const MachineRegisterInfo &MRI);
@@ -239,6 +245,8 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
239245
}
240246
case TargetOpcode::G_SEXT_INREG:
241247
return selectSExtInreg(MI, MIB);
248+
case TargetOpcode::G_SELECT:
249+
return selectSelect(MI, MIB, MRI);
242250
default:
243251
return false;
244252
}
@@ -376,6 +384,27 @@ bool RISCVInstructionSelector::selectSExtInreg(MachineInstr &MI,
376384
return true;
377385
}
378386

387+
bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
388+
MachineIRBuilder &MIB,
389+
MachineRegisterInfo &MRI) const {
390+
// TODO: Currently we check that the conditional code passed to G_SELECT is
391+
// not equal to zero; however, in the future, we might want to try and check
392+
// if the conditional code comes from a G_ICMP. If it does, we can directly
393+
// use G_ICMP to get the first three input operands of the
394+
// Select_GPR_Using_CC_GPR. This might be done here, or in the appropriate
395+
// combiner.
396+
assert(MI.getOpcode() == TargetOpcode::G_SELECT);
397+
MachineInstr *Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
398+
.addDef(MI.getOperand(0).getReg())
399+
.addReg(MI.getOperand(1).getReg())
400+
.addReg(RISCV::X0)
401+
.addImm(RISCVCC::COND_NE)
402+
.addReg(MI.getOperand(2).getReg())
403+
.addReg(MI.getOperand(3).getReg());
404+
MI.eraseFromParent();
405+
return constrainSelectedInstRegOperands(*Result, TII, TRI, RBI);
406+
}
407+
379408
namespace llvm {
380409
InstructionSelector *
381410
createRISCVInstructionSelector(const RISCVTargetMachine &TM,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv32 -run-pass=instruction-select --simplify-mir \
3+
# RUN: -verify-machineinstrs %s -o - | FileCheck %s
4+
---
5+
name: select_s32
6+
legalized: true
7+
regBankSelected: true
8+
tracksRegLiveness: true
9+
body: |
10+
bb.0:
11+
liveins: $x10, $x11, $x12
12+
13+
; CHECK-LABEL: name: select_s32
14+
; CHECK: liveins: $x10, $x11, $x12
15+
; CHECK-NEXT: {{ $}}
16+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
17+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
18+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr = COPY $x12
19+
; CHECK-NEXT: [[Select_GPR_Using_CC_GPR:%[0-9]+]]:gpr = Select_GPR_Using_CC_GPR [[COPY]], $x0, 1, [[COPY1]], [[COPY2]]
20+
; CHECK-NEXT: $x10 = COPY [[Select_GPR_Using_CC_GPR]]
21+
; CHECK-NEXT: PseudoRET implicit $x10
22+
%0:gprb(s32) = COPY $x10
23+
%1:gprb(s32) = COPY $x11
24+
%2:gprb(s32) = COPY $x12
25+
%3:gprb(s32) = G_SELECT %0, %1, %2
26+
$x10 = COPY %3(s32)
27+
PseudoRET implicit $x10
28+
29+
...
30+
---
31+
name: select_p0
32+
legalized: true
33+
regBankSelected: true
34+
tracksRegLiveness: true
35+
body: |
36+
bb.0:
37+
liveins: $x10, $x11, $x12
38+
39+
; CHECK-LABEL: name: select_p0
40+
; CHECK: liveins: $x10, $x11, $x12
41+
; CHECK-NEXT: {{ $}}
42+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
43+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
44+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr = COPY $x12
45+
; CHECK-NEXT: [[Select_GPR_Using_CC_GPR:%[0-9]+]]:gpr = Select_GPR_Using_CC_GPR [[COPY]], $x0, 1, [[COPY1]], [[COPY2]]
46+
; CHECK-NEXT: $x10 = COPY [[Select_GPR_Using_CC_GPR]]
47+
; CHECK-NEXT: PseudoRET implicit $x10
48+
%0:gprb(s32) = COPY $x10
49+
%1:gprb(p0) = COPY $x11
50+
%2:gprb(p0) = COPY $x12
51+
%3:gprb(p0) = G_SELECT %0, %1, %2
52+
$x10 = COPY %3(p0)
53+
PseudoRET implicit $x10
54+
55+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv64 -run-pass=instruction-select %s -o - \
3+
# RUN: | FileCheck %s
4+
---
5+
name: select_s64
6+
legalized: true
7+
regBankSelected: true
8+
tracksRegLiveness: true
9+
body: |
10+
bb.0:
11+
liveins: $x10, $x11, $x12
12+
13+
; CHECK-LABEL: name: select_s64
14+
; CHECK: liveins: $x10, $x11, $x12
15+
; CHECK-NEXT: {{ $}}
16+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
17+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
18+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr = COPY $x12
19+
; CHECK-NEXT: [[Select_GPR_Using_CC_GPR:%[0-9]+]]:gpr = Select_GPR_Using_CC_GPR [[COPY]], $x0, 1, [[COPY1]], [[COPY2]]
20+
; CHECK-NEXT: $x10 = COPY [[Select_GPR_Using_CC_GPR]]
21+
; CHECK-NEXT: PseudoRET implicit $x10
22+
%0:gprb(s64) = COPY $x10
23+
%1:gprb(s64) = COPY $x11
24+
%2:gprb(s64) = COPY $x12
25+
%3:gprb(s64) = G_SELECT %0, %1, %2
26+
$x10 = COPY %3(s64)
27+
PseudoRET implicit $x10
28+
29+
...
30+
---
31+
name: select_p0
32+
legalized: true
33+
regBankSelected: true
34+
tracksRegLiveness: true
35+
body: |
36+
bb.0:
37+
liveins: $x10, $x11, $x12
38+
39+
; CHECK-LABEL: name: select_p0
40+
; CHECK: liveins: $x10, $x11, $x12
41+
; CHECK-NEXT: {{ $}}
42+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
43+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $x11
44+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr = COPY $x12
45+
; CHECK-NEXT: [[Select_GPR_Using_CC_GPR:%[0-9]+]]:gpr = Select_GPR_Using_CC_GPR [[COPY]], $x0, 1, [[COPY1]], [[COPY2]]
46+
; CHECK-NEXT: $x10 = COPY [[Select_GPR_Using_CC_GPR]]
47+
; CHECK-NEXT: PseudoRET implicit $x10
48+
%0:gprb(s64) = COPY $x10
49+
%1:gprb(p0) = COPY $x11
50+
%2:gprb(p0) = COPY $x12
51+
%3:gprb(p0) = G_SELECT %0, %1, %2
52+
$x10 = COPY %3(p0)
53+
PseudoRET implicit $x10
54+
55+
...

0 commit comments

Comments
 (0)