Skip to content

Commit 538b90c

Browse files
committed
[RISCV][GlobalISel] Select ALU GPR instructions
Some instruction selection patterns required for ALU GPR instructions have already been automatically imported from existing TableGen descriptions - this patch simply adds testing for them. Logic for selecting constants and copies has been added, along with the first of the GIComplexPatternEquiv definitions required to select the shiftMaskXLen ComplexPattern. New patterns have been added to directly select RV64 W instructions from gMIR rather than using custom gMIR operations earlier in the pipeline. In future this could also support the checks present in the DAGToDAGISel for finding ops whose users only require the lower 32 bits. Differential Revision: https://reviews.llvm.org/D76445
1 parent 2102ed0 commit 538b90c

File tree

8 files changed

+2006
-2
lines changed

8 files changed

+2006
-2
lines changed

llvm/lib/Target/RISCV/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ tablegen(LLVM RISCVGenAsmWriter.inc -gen-asm-writer)
77
tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
88
tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
99
tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
10-
tablegen(LLVM RISCVGenGlobalISel.inc -gen-global-isel)
1110
tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
1211
tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
1312
tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
@@ -16,6 +15,9 @@ tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
1615
tablegen(LLVM RISCVGenSearchableTables.inc -gen-searchable-tables)
1716
tablegen(LLVM RISCVGenSubtargetInfo.inc -gen-subtarget)
1817

18+
set(LLVM_TARGET_DEFINITIONS RISCVGISel.td)
19+
tablegen(LLVM RISCVGenGlobalISel.inc -gen-global-isel)
20+
1921
add_public_tablegen_target(RISCVCommonTableGen)
2022

2123
add_llvm_target(RISCVCodeGen

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class RISCVInstructionSelector : public InstructionSelector {
5050
bool selectConstant(MachineInstr &MI, MachineIRBuilder &MIB,
5151
MachineRegisterInfo &MRI) const;
5252

53+
ComplexRendererFns selectS32ShiftMask(MachineOperand &Root) const;
54+
5355
const RISCVSubtarget &STI;
5456
const RISCVInstrInfo &TII;
5557
const RISCVRegisterInfo &TRI;
@@ -89,6 +91,16 @@ RISCVInstructionSelector::RISCVInstructionSelector(
8991
{
9092
}
9193

94+
InstructionSelector::ComplexRendererFns
95+
RISCVInstructionSelector::selectS32ShiftMask(MachineOperand &Root) const {
96+
// TODO: Also check if we are seeing the result of an AND operation which
97+
// could be bypassed since we only check the lower log2(xlen) bits.
98+
return {{
99+
[=](MachineInstrBuilder &MIB) { MIB.addReg(Root.getReg()); },
100+
[=](MachineInstrBuilder &MIB) { MIB.addImm(0); } // src_mods
101+
}};
102+
}
103+
92104
bool RISCVInstructionSelector::select(MachineInstr &MI) {
93105
unsigned Opc = MI.getOpcode();
94106
MachineBasicBlock &MBB = *MI.getParent();
@@ -111,6 +123,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
111123

112124
switch (Opc) {
113125
case TargetOpcode::G_ANYEXT:
126+
case TargetOpcode::G_TRUNC:
114127
MI.setDesc(TII.get(TargetOpcode::COPY));
115128
return true;
116129
case TargetOpcode::G_CONSTANT:
@@ -129,7 +142,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
129142
const TargetRegisterClass *RISCVInstructionSelector::getRegClassForTypeOnBank(
130143
LLT Ty, const RegisterBank &RB, bool GetAllRegSet) const {
131144
if (RB.getID() == RISCV::GPRRegBankID) {
132-
if (Ty.getSizeInBits() == (STI.is64Bit() ? 64 : 32))
145+
if (Ty.getSizeInBits() <= 32 || (STI.is64Bit() && Ty.getSizeInBits() == 64))
133146
return &RISCV::GPRRegClass;
134147
}
135148

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- RISCVGIsel.td - RISCV GlobalISel Patterns ----------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// This file contains patterns that are relevant to GlobalISel, including
11+
/// GIComplexOperandMatcher definitions for equivalent SelectionDAG
12+
/// ComplexPatterns.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
include "RISCV.td"
17+
18+
// FIXME: This is labelled as handling 's32', however the ComplexPattern it
19+
// refers to handles both i32 and i64 based on the HwMode. Currently this LLT
20+
// parameter appears to be ignored so this pattern works for both, however we
21+
// should add a LowLevelTypeByHwMode, and use that to define our XLenLLT instead
22+
// here.
23+
def s32ShiftMaskGI :
24+
GIComplexOperandMatcher<s32, "selectS32ShiftMask">,
25+
GIComplexPatternEquiv<shiftMaskXLen>;
26+
27+
let Predicates = [IsRV64] in {
28+
def : Pat<(i32 (add GPR:$rs1, GPR:$rs2)), (ADDW GPR:$rs1, GPR:$rs2)>;
29+
def : Pat<(i32 (sub GPR:$rs1, GPR:$rs2)), (SUBW GPR:$rs1, GPR:$rs2)>;
30+
31+
def : Pat<(i32 (shl GPR:$rs1, (i32 GPR:$rs2))), (SLLW GPR:$rs1, GPR:$rs2)>;
32+
def : Pat<(i32 (sra GPR:$rs1, (i32 GPR:$rs2))), (SRAW GPR:$rs1, GPR:$rs2)>;
33+
def : Pat<(i32 (srl GPR:$rs1, (i32 GPR:$rs2))), (SRLW GPR:$rs1, GPR:$rs2)>;
34+
}
35+
36+
let Predicates = [HasStdExtMOrZmmul, IsRV64] in {
37+
def : Pat<(i32 (mul GPR:$rs1, GPR:$rs2)), (MULW GPR:$rs1, GPR:$rs2)>;
38+
}
39+
40+
let Predicates = [HasStdExtM, IsRV64] in {
41+
def : Pat<(i32 (sdiv GPR:$rs1, GPR:$rs2)), (DIVW GPR:$rs1, GPR:$rs2)>;
42+
def : Pat<(i32 (srem GPR:$rs1, GPR:$rs2)), (REMW GPR:$rs1, GPR:$rs2)>;
43+
def : Pat<(i32 (udiv GPR:$rs1, GPR:$rs2)), (DIVUW GPR:$rs1, GPR:$rs2)>;
44+
def : Pat<(i32 (urem GPR:$rs1, GPR:$rs2)), (REMUW GPR:$rs1, GPR:$rs2)>;
45+
}

0 commit comments

Comments
 (0)