Skip to content

[CodeGen][RISCV] Add helper class for emitting CFI instructions into MIR #135845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions llvm/include/llvm/CodeGen/CFIInstBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_CFIINSTBUILDER_H
#define LLVM_CODEGEN_CFIINSTBUILDER_H

#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/MC/MCDwarf.h"

namespace llvm {

/// Helper class for creating CFI instructions and inserting them into MIR.
class CFIInstBuilder {
MachineFunction &MF;
MachineBasicBlock &MBB;
MachineBasicBlock::iterator InsertPt;

/// MIFlag to set on a MachineInstr. Typically, FrameSetup or FrameDestroy.
MachineInstr::MIFlag MIFlag;

/// Selects DWARF register numbering: debug or exception handling. Should be
/// consistent with the choice of the ELF section (.debug_frame or .eh_frame)
/// where CFI will be encoded.
bool IsEH;

// Cache frequently used variables.
const TargetRegisterInfo &TRI;
const MCInstrDesc &CFIID;
const MIMetadata MIMD; // Default-initialized, no debug location desired.

public:
CFIInstBuilder(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertPt,
MachineInstr::MIFlag MIFlag, bool IsEH = true)
: MF(*MBB.getParent()), MBB(MBB), MIFlag(MIFlag), IsEH(IsEH),
TRI(*MF.getSubtarget().getRegisterInfo()),
CFIID(MF.getSubtarget().getInstrInfo()->get(
TargetOpcode::CFI_INSTRUCTION)) {
setInsertPoint(InsertPt);
}

void setInsertPoint(MachineBasicBlock::iterator IP) { InsertPt = IP; }

void insertCFIInst(const MCCFIInstruction &CFIInst) const {
BuildMI(MBB, InsertPt, MIMD, CFIID)
.addCFIIndex(MF.addFrameInst(CFIInst))
.setMIFlag(MIFlag);
}

void buildDefCFA(MCRegister Reg, int64_t Offset) const {
insertCFIInst(MCCFIInstruction::cfiDefCfa(
nullptr, TRI.getDwarfRegNum(Reg, IsEH), Offset));
}

void buildDefCFARegister(MCRegister Reg) const {
insertCFIInst(MCCFIInstruction::createDefCfaRegister(
nullptr, TRI.getDwarfRegNum(Reg, IsEH)));
}

void buildDefCFAOffset(int64_t Offset) const {
insertCFIInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
}

void buildOffset(MCRegister Reg, int64_t Offset) const {
insertCFIInst(MCCFIInstruction::createOffset(
nullptr, TRI.getDwarfRegNum(Reg, IsEH), Offset));
}

void buildRestore(MCRegister Reg) const {
insertCFIInst(MCCFIInstruction::createRestore(
nullptr, TRI.getDwarfRegNum(Reg, IsEH)));
}

void buildEscape(StringRef Bytes, StringRef Comment = "") const {
insertCFIInst(
MCCFIInstruction::createEscape(nullptr, Bytes, SMLoc(), Comment));
}
};

} // namespace llvm

#endif // LLVM_CODEGEN_CFIINSTBUILDER_H
Loading