Skip to content

Commit 72ba591

Browse files
committed
[RISCV][GISel] Select G_SELECT
1 parent 56f7c7e commit 72ba591

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);
@@ -240,6 +246,8 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
240246
}
241247
case TargetOpcode::G_SEXT_INREG:
242248
return selectSExtInreg(MI, MIB);
249+
case TargetOpcode::G_SELECT:
250+
return selectSelect(MI, MIB, MRI);
243251
default:
244252
return false;
245253
}
@@ -389,6 +397,27 @@ bool RISCVInstructionSelector::selectSExtInreg(MachineInstr &MI,
389397
return true;
390398
}
391399

400+
bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
401+
MachineIRBuilder &MIB,
402+
MachineRegisterInfo &MRI) const {
403+
// TODO: Currently we check that the conditional code passed to G_SELECT is
404+
// not equal to zero; however, in the future, we might want to try and check
405+
// if the conditional code comes from a G_ICMP. If it does, we can directly
406+
// use G_ICMP to get the first three input operands of the
407+
// Select_GPR_Using_CC_GPR. This might be done here, or in the appropriate
408+
// combiner.
409+
assert(MI.getOpcode() == TargetOpcode::G_SELECT);
410+
MachineInstr *Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
411+
.addDef(MI.getOperand(0).getReg())
412+
.addReg(MI.getOperand(1).getReg())
413+
.addReg(RISCV::X0)
414+
.addImm(RISCVCC::COND_NE)
415+
.addReg(MI.getOperand(2).getReg())
416+
.addReg(MI.getOperand(3).getReg());
417+
MI.eraseFromParent();
418+
return constrainSelectedInstRegOperands(*Result, TII, TRI, RBI);
419+
}
420+
392421
namespace llvm {
393422
InstructionSelector *
394423
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)