Skip to content

Commit 3cd3847

Browse files
committed
[CHERI] Update CallSiteInfo handling in RISCVExpandPseudoInsts for improved assertions in upstream
1 parent 094f9b1 commit 3cd3847

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,23 @@ class RISCVExpandPseudo : public MachineFunctionPass {
111111
* function is / may be exported from this compartment but, at this call site,
112112
* should be treated as a library call.
113113
*/
114-
MachineBasicBlock *insertLoadOfImportTable(MachineBasicBlock &MBB,
115-
MachineBasicBlock::iterator MBBI,
116-
const Function *Fn,
117-
Register DestReg,
118-
bool TreatAsLibrary = false,
119-
bool CallImportTarget = false);
114+
MachineBasicBlock *
115+
insertLoadOfImportTable(MachineBasicBlock &MBB,
116+
MachineBasicBlock::iterator MBBI, const Function *Fn,
117+
Register DestReg, bool TreatAsLibrary = false,
118+
bool CallImportTarget = false,
119+
const MachineInstr *OriginalCall = nullptr);
120120
/**
121121
* Helper that inserts a load from the import table identified by an import
122122
* and export table entry symbol.
123123
*
124124
* Calls the result if `CallImportTarget` is true.
125125
*/
126-
MachineBasicBlock *
127-
insertLoadOfImportTable(MachineBasicBlock &MBB,
128-
MachineBasicBlock::iterator MBBI,
129-
MCSymbol *ImportSymbol, MCSymbol *ExportSymbol,
130-
const StringRef ImportName, Register DestReg,
131-
bool IsLibrary, bool IsPublic, bool CallImportTarget);
126+
MachineBasicBlock *insertLoadOfImportTable(
127+
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
128+
MCSymbol *ImportSymbol, MCSymbol *ExportSymbol,
129+
const StringRef ImportName, Register DestReg, bool IsLibrary,
130+
bool IsPublic, bool CallImportTarget, const MachineInstr *OriginalCall);
132131

133132
#ifndef NDEBUG
134133
unsigned getInstSizeInBytes(const MachineFunction &MF) const {
@@ -268,7 +267,7 @@ bool RISCVExpandPseudo::expandMI(MachineBasicBlock &MBB,
268267
MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
269268
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
270269
const Function *Fn, Register DestReg, bool TreatAsLibrary,
271-
bool CallImportTarget) {
270+
bool CallImportTarget, const MachineInstr *OriginalCall) {
272271
auto *MF = MBB.getParent();
273272
const StringRef ImportName = Fn->getName();
274273
// We can hit this code path if we need to do a library-style import
@@ -296,7 +295,8 @@ MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
296295
MCSymbol *ExportSymbol = MF->getContext().getOrCreateSymbol(ExportEntryName);
297296
return insertLoadOfImportTable(
298297
MBB, MBBI, ImportSymbol, ExportSymbol, ImportName, DestReg,
299-
IsLibrary || TreatAsLibrary, Fn->hasExternalLinkage(), CallImportTarget);
298+
IsLibrary || TreatAsLibrary, Fn->hasExternalLinkage(), CallImportTarget,
299+
OriginalCall);
300300
}
301301

302302
static const GlobalValue *resolveGlobalAlias(const GlobalValue *GV) {
@@ -309,7 +309,8 @@ static const GlobalValue *resolveGlobalAlias(const GlobalValue *GV) {
309309
MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
310310
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
311311
MCSymbol *ImportSymbol, MCSymbol *ExportSymbol, const StringRef ImportName,
312-
Register DestReg, bool IsLibrary, bool IsPublic, bool CallImportTarget) {
312+
Register DestReg, bool IsLibrary, bool IsPublic, bool CallImportTarget,
313+
const MachineInstr *OriginalCall) {
313314
auto *MF = MBB.getParent();
314315
const DebugLoc DL = MBBI->getDebugLoc();
315316
MachineBasicBlock *NewMBB =
@@ -328,9 +329,12 @@ MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
328329
.addReg(DestReg, RegState::Kill)
329330
.addMBB(NewMBB, RISCVII::MO_CHERIOT_COMPARTMENT_LO_I);
330331

331-
if (CallImportTarget)
332-
BuildMI(NewMBB, DL, TII->get(RISCV::C_CJALR))
333-
.addReg(DestReg, RegState::Kill);
332+
if (CallImportTarget) {
333+
auto NewCallMI = BuildMI(NewMBB, DL, TII->get(RISCV::C_CJALR))
334+
.addReg(DestReg, RegState::Kill);
335+
if (OriginalCall && OriginalCall->shouldUpdateAdditionalCallInfo())
336+
MF->moveAdditionalCallInfo(OriginalCall, NewCallMI);
337+
}
334338

335339
NewMBB->splice(NewMBB->end(), &MBB, std::next(MBBI), MBB.end());
336340
// Update machine-CFG edges.
@@ -408,8 +412,10 @@ bool RISCVExpandPseudo::expandCompartmentCall(MachineBasicBlock &MBB,
408412
BuildMI(NewMBB, DL, TII->get(RISCV::CLC_64), RISCV::X7_Y)
409413
.addReg(RISCV::X7_Y, RegState::Kill)
410414
.addMBB(NewMBB, RISCVII::MO_CHERIOT_COMPARTMENT_LO_I);
411-
BuildMI(NewMBB, DL, TII->get(RISCV::C_CJALR))
412-
.addReg(RISCV::X7_Y, RegState::Kill);
415+
auto NewCallMI = BuildMI(NewMBB, DL, TII->get(RISCV::C_CJALR))
416+
.addReg(RISCV::X7_Y, RegState::Kill);
417+
if (MI.shouldUpdateAdditionalCallInfo())
418+
MF->moveAdditionalCallInfo(&MI, NewCallMI);
413419

414420
// Move all the rest of the instructions to NewMBB.
415421
NewMBB->splice(NewMBB->end(), &MBB, std::next(MBBI), MBB.end());
@@ -455,7 +461,7 @@ bool RISCVExpandPseudo::expandLibraryCall(
455461
MI.setDesc(TII->get(RISCV::PseudoCCALL));
456462
return true;
457463
}
458-
insertLoadOfImportTable(MBB, MBBI, Fn, RISCV::X7_Y, true, true);
464+
insertLoadOfImportTable(MBB, MBBI, Fn, RISCV::X7_Y, true, true, &MI);
459465

460466
NextMBBI = MBB.end();
461467
} else if (Callee.isSymbol()) {
@@ -486,14 +492,18 @@ bool RISCVExpandPseudo::expandLibraryCall(
486492
MF->getContext().getOrCreateSymbol(ExportEntryName);
487493
insertLoadOfImportTable(MBB, MBBI, ImportSymbol, ExportSymbol,
488494
Callee.getSymbolName(), RISCV::X7_Y, true, true,
489-
true);
495+
true, &MI);
490496

491497
NextMBBI = MBB.end();
492498
} else {
493499
assert(Callee.isReg() && "Expected register operand");
494500
// Indirect library calls are just cjalr instructions.
495-
BuildMI(&MBB, MI.getDebugLoc(), TII->get(RISCV::C_CJALR)).add(Callee);
501+
auto NewCallMI =
502+
BuildMI(&MBB, MI.getDebugLoc(), TII->get(RISCV::C_CJALR)).add(Callee);
503+
if (MI.shouldUpdateAdditionalCallInfo())
504+
MF->moveAdditionalCallInfo(NewCallMI, &MI);
496505
}
506+
497507
MI.eraseFromParent();
498508
return true;
499509
}

0 commit comments

Comments
 (0)