Skip to content

Commit

Permalink
[RISCV][GISel] Select trap and debugtrap. (#73171)
Browse files Browse the repository at this point in the history
  • Loading branch information
topperc committed Nov 27, 2023
1 parent be1e8a6 commit b4cf014
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
27 changes: 27 additions & 0 deletions llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class RISCVInstructionSelector : public InstructionSelector {
MachineRegisterInfo &MRI) const;
bool selectFPCompare(MachineInstr &MI, MachineIRBuilder &MIB,
MachineRegisterInfo &MRI) const;
bool selectIntrinsicWithSideEffects(MachineInstr &MI, MachineIRBuilder &MIB,
MachineRegisterInfo &MRI) const;

ComplexRendererFns selectShiftMask(MachineOperand &Root) const;
ComplexRendererFns selectAddrRegImm(MachineOperand &Root) const;
Expand Down Expand Up @@ -608,6 +610,8 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
return selectSelect(MI, MIB, MRI);
case TargetOpcode::G_FCMP:
return selectFPCompare(MI, MIB, MRI);
case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
return selectIntrinsicWithSideEffects(MI, MIB, MRI);
default:
return false;
}
Expand Down Expand Up @@ -1060,6 +1064,29 @@ bool RISCVInstructionSelector::selectFPCompare(MachineInstr &MI,
return true;
}

bool RISCVInstructionSelector::selectIntrinsicWithSideEffects(
MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI) const {
assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS &&
"Unexpected opcode");
// Find the intrinsic ID.
unsigned IntrinID = cast<GIntrinsic>(MI).getIntrinsicID();

// Select the instruction.
switch (IntrinID) {
default:
return false;
case Intrinsic::trap:
MIB.buildInstr(RISCV::UNIMP, {}, {});
break;
case Intrinsic::debugtrap:
MIB.buildInstr(RISCV::EBREAK, {}, {});
break;
}

MI.eraseFromParent();
return true;
}

namespace llvm {
InstructionSelector *
createRISCVInstructionSelector(const RISCVTargetMachine &TM,
Expand Down
34 changes: 34 additions & 0 deletions llvm/test/CodeGen/RISCV/GlobalISel/instruction-select/trap.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple=riscv32 -run-pass=instruction-select -simplify-mir \
# RUN: -verify-machineinstrs %s -o - | FileCheck %s
# RUN: llc -mtriple=riscv64 -run-pass=instruction-select -simplify-mir \
# RUN: -verify-machineinstrs %s -o - | FileCheck %s

---
name: test_trap
legalized: true
regBankSelected: true
tracksRegLiveness: true
body: |
bb.1:
; CHECK-LABEL: name: test_trap
; CHECK: UNIMP
; CHECK-NEXT: PseudoRET
G_INTRINSIC_W_SIDE_EFFECTS intrinsic(@llvm.trap)
PseudoRET
...
---
name: test_debugtrap
legalized: true
regBankSelected: true
tracksRegLiveness: true
body: |
bb.1:
; CHECK-LABEL: name: test_debugtrap
; CHECK: EBREAK
; CHECK-NEXT: PseudoRET
G_INTRINSIC_W_SIDE_EFFECTS intrinsic(@llvm.debugtrap)
PseudoRET
...

0 comments on commit b4cf014

Please sign in to comment.