Skip to content
Merged
Changes from all 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
25 changes: 18 additions & 7 deletions llvm/lib/CodeGen/CFIInstrInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,20 @@ class CFIInstrInserter : public MachineFunctionPass {
/// contains the location where CSR register is saved.
struct CSRSavedLocation {
CSRSavedLocation(std::optional<unsigned> R, std::optional<int> O)
: Reg(R), Offset(O) {}
: Reg(R), Offset(O) {
assert((Reg.has_value() ^ Offset.has_value()) &&
"Register and offset can not both be valid");
}
std::optional<unsigned> Reg;
std::optional<int> Offset;

bool operator==(const CSRSavedLocation &RHS) const {
return Reg == RHS.Reg && Offset == RHS.Offset;
}

bool operator!=(const CSRSavedLocation &RHS) const {
return !(*this == RHS);
}
};

/// Contains cfa offset and register values valid at entry and exit of basic
Expand Down Expand Up @@ -267,11 +278,11 @@ void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
break;
}
if (CSRReg || CSROffset) {
CSRSavedLocation Loc(CSRReg, CSROffset);
auto It = CSRLocMap.find(CFI.getRegister());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try the insert instead of find here. If the insert fails, do the check to make sure the value we tried to insert matches what was already there.

This can be a follow up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #170760

if (It == CSRLocMap.end()) {
CSRLocMap.insert(
{CFI.getRegister(), CSRSavedLocation(CSRReg, CSROffset)});
} else if (It->second.Reg != CSRReg || It->second.Offset != CSROffset) {
CSRLocMap.insert({CFI.getRegister(), Loc});
} else if (It->second != Loc) {
reportFatalInternalError(
"Different saved locations for the same CSR");
}
Expand Down Expand Up @@ -397,11 +408,11 @@ bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
if (!RO.Reg && RO.Offset) {
CFIIndex = MF.addFrameInst(
MCCFIInstruction::createOffset(nullptr, Reg, *RO.Offset));
} else if (RO.Reg && !RO.Offset) {
} else {
assert((RO.Reg && !RO.Offset) &&
"Reg and Offset cannot both be valid/invalid");
CFIIndex = MF.addFrameInst(
MCCFIInstruction::createRegister(nullptr, Reg, *RO.Reg));
} else {
llvm_unreachable("RO.Reg and RO.Offset cannot both be valid/invalid");
}
BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(CFIIndex);
Expand Down
Loading