aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-17 03:16:26 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-17 03:16:26 +0800
commitf5c592905e6e1ef4b78337d76c8215e543607b8d (patch)
tree193400315338b57af33699ad7f952fb3731b3a65 /backend/lib/Target/RISCV
parent9720e989ccdce77a2c45a8379d36ff4428e6c2bf (diff)
downloadrcc-project-f5c592905e6e1ef4b78337d76c8215e543607b8d.tar.gz
rcc-project-f5c592905e6e1ef4b78337d76c8215e543607b8d.tar.xz
[RISCV][GlobalISel] Generate conditional branch selection
Diffstat (limited to 'backend/lib/Target/RISCV')
-rw-r--r--backend/lib/Target/RISCV/CMakeLists.txt1
-rw-r--r--backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp13
-rw-r--r--backend/lib/Target/RISCV/RISCVGISelControlFlow.rccdesc252
-rw-r--r--backend/lib/Target/RISCV/RISCVGISelInteger.rccdesc10
4 files changed, 253 insertions, 23 deletions
diff --git a/backend/lib/Target/RISCV/CMakeLists.txt b/backend/lib/Target/RISCV/CMakeLists.txt
index f9eb79f..48f3263 100644
--- a/backend/lib/Target/RISCV/CMakeLists.txt
+++ b/backend/lib/Target/RISCV/CMakeLists.txt
@@ -15,6 +15,7 @@ set(
"${CMAKE_CURRENT_SOURCE_DIR}/RISCVInstrInfoZb.rccdesc"
"${CMAKE_CURRENT_SOURCE_DIR}/RISCVInstrInfoPseudo.rccdesc"
"${CMAKE_CURRENT_SOURCE_DIR}/RISCVGISelInteger.rccdesc"
+ "${CMAKE_CURRENT_SOURCE_DIR}/RISCVGISelControlFlow.rccdesc"
"${CMAKE_CURRENT_SOURCE_DIR}/RISCVGISelShift.rccdesc"
"${CMAKE_CURRENT_SOURCE_DIR}/RISCVGISelFloating.rccdesc"
)
diff --git a/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp b/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
index 89b6ecd..1e2eb92 100644
--- a/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
+++ b/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
@@ -364,7 +364,6 @@ public:
case GenericOpcode::G_FPEXTLOAD:
case GenericOpcode::G_FPTRUNCSTORE:
case GenericOpcode::G_STORE:
- case GenericOpcode::G_BRCOND:
return {};
case GenericOpcode::G_FCONSTANT: {
const MachineOperand *Immediate = Instruction.operandAt(1);
@@ -440,8 +439,6 @@ public:
return selectStore(Instruction, Function);
case GenericOpcode::G_FPTRUNCSTORE:
return selectFloatTruncStore(Instruction, Function, Target);
- case GenericOpcode::G_BRCOND:
- return selectConditionalBranch(Instruction);
case GenericOpcode::G_CONSTANT:
return selectIntegerConstant(Instruction, Function, Target, Type);
case GenericOpcode::G_FCONSTANT:
@@ -1422,16 +1419,6 @@ private:
return makeUnexpected(ErrorCode::Unsupported, "RISC-V cannot select this generic store type");
}
- [[nodiscard]] Expected<void> selectConditionalBranch(MachineInstr &Instruction) const {
- auto Zero = physicalRegister(PhysicalRegisterID::X0);
- if (!Zero)
- return std::unexpected(Zero.error());
- auto Inserted = insertPhysicalRegister(Instruction, 1, *Zero, RegisterOperandFlag::Use);
- if (!Inserted)
- return std::unexpected(Inserted.error());
- return Instruction.setOpcode(instructionOpcode(InstructionOpcode::BNE));
- }
-
[[nodiscard]] Expected<void> selectIntegerConstant(MachineInstr &Instruction,
MachineFunction &Function,
const TargetDescription &Target,
diff --git a/backend/lib/Target/RISCV/RISCVGISelControlFlow.rccdesc b/backend/lib/Target/RISCV/RISCVGISelControlFlow.rccdesc
new file mode 100644
index 0000000..c58135f
--- /dev/null
+++ b/backend/lib/Target/RISCV/RISCVGISelControlFlow.rccdesc
@@ -0,0 +1,252 @@
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+selection_rules:
+ -
+ id: select_g_br
+ priority: 100
+ match:
+ opcode: G_BR
+ apply:
+ instructions:
+ - opcode: PseudoBR
+ root: true
+ operands: [{ operand: 0 }]
+ -
+ id: select_g_brcond_sgt_minus_one
+ priority: 600
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: sgt }
+ captures:
+ - { id: lhs, operand: 2 }
+ operand_def_constants:
+ - index: 3
+ ranges: [{ min: -1, max: -1 }]
+ apply:
+ instructions:
+ - opcode: BGE
+ root: true
+ operands:
+ - { captured_operand: lhs }
+ - { physical_register: X0, flags: [use] }
+ - { operand: 1 }
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_slt_one
+ priority: 600
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: slt }
+ captures:
+ - { id: lhs, operand: 2 }
+ operand_def_constants:
+ - index: 3
+ ranges: [{ min: 1, max: 1 }]
+ apply:
+ instructions:
+ - opcode: BGE
+ root: true
+ operands:
+ - { physical_register: X0, flags: [use] }
+ - { captured_operand: lhs }
+ - { operand: 1 }
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_eq
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: eq }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BEQ
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_ne
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: ne }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BNE
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_slt
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: slt }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BLT
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_sge
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: sge }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BGE
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_ult
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: ult }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BLTU
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_uge
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: uge }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BGEU
+ root: true
+ operands: [{ captured_operand: lhs }, { captured_operand: rhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_sgt
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: sgt }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BLT
+ root: true
+ operands: [{ captured_operand: rhs }, { captured_operand: lhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_sle
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: sle }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BGE
+ root: true
+ operands: [{ captured_operand: rhs }, { captured_operand: lhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_ugt
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: ugt }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BLTU
+ root: true
+ operands: [{ captured_operand: rhs }, { captured_operand: lhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond_ule
+ priority: 400
+ match:
+ opcode: G_BRCOND
+ def_use:
+ - operand: 0
+ opcode: G_ICMP
+ integer_predicate: { index: 1, value: ule }
+ captures:
+ - { id: lhs, operand: 2 }
+ - { id: rhs, operand: 3 }
+ apply:
+ instructions:
+ - opcode: BGEU
+ root: true
+ operands: [{ captured_operand: rhs }, { captured_operand: lhs }, { operand: 1 }]
+ erase_operand_definitions: [0]
+ -
+ id: select_g_brcond
+ priority: 100
+ match:
+ opcode: G_BRCOND
+ apply:
+ instructions:
+ - opcode: BNE
+ root: true
+ operands:
+ - { operand: 0 }
+ - { physical_register: X0, flags: [use] }
+ - { operand: 1 }
diff --git a/backend/lib/Target/RISCV/RISCVGISelInteger.rccdesc b/backend/lib/Target/RISCV/RISCVGISelInteger.rccdesc
index 6ba88ef..f40e5d4 100644
--- a/backend/lib/Target/RISCV/RISCVGISelInteger.rccdesc
+++ b/backend/lib/Target/RISCV/RISCVGISelInteger.rccdesc
@@ -1,15 +1,5 @@
selection_rules:
-
- id: select_g_br
- priority: 100
- match:
- opcode: G_BR
- apply:
- instructions:
- - opcode: PseudoBR
- root: true
- operands: [{ operand: 0 }]
- -
id: select_g_implicit_def
priority: 100
match: