Skip to content

Commit 3620b99

Browse files
committed
[x86/SLH] Extract another small helper function, add better comments and
use better terminology. NFC. llvm-svn: 337157
1 parent f4e7025 commit 3620b99

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

llvm/lib/Target/X86/X86SpeculativeLoadHardening.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,37 @@ static void canonicalizePHIOperands(MachineFunction &MF) {
330330
}
331331
}
332332

333+
/// Helper to scan a function for loads vulnerable to misspeculation that we
334+
/// want to harden.
335+
///
336+
/// We use this to avoid making changes to functions where there is nothing we
337+
/// need to do to harden against misspeculation.
338+
static bool hasVulnerableLoad(MachineFunction &MF) {
339+
for (MachineBasicBlock &MBB : MF) {
340+
for (MachineInstr &MI : MBB) {
341+
// Loads within this basic block after an LFENCE are not at risk of
342+
// speculatively executing with invalid predicates from prior control
343+
// flow. So break out of this block but continue scanning the function.
344+
if (MI.getOpcode() == X86::LFENCE)
345+
break;
346+
347+
// Looking for loads only.
348+
if (!MI.mayLoad())
349+
continue;
350+
351+
// An MFENCE is modeled as a load but isn't vulnerable to misspeculation.
352+
if (MI.getOpcode() == X86::MFENCE)
353+
continue;
354+
355+
// We found a load.
356+
return true;
357+
}
358+
}
359+
360+
// No loads found.
361+
return false;
362+
}
363+
333364
bool X86SpeculativeLoadHardeningPass::runOnMachineFunction(
334365
MachineFunction &MF) {
335366
LLVM_DEBUG(dbgs() << "********** " << getPassName() << " : " << MF.getName()
@@ -359,34 +390,14 @@ bool X86SpeculativeLoadHardeningPass::runOnMachineFunction(
359390
auto EntryInsertPt = Entry.SkipPHIsLabelsAndDebug(Entry.begin());
360391

361392
// Do a quick scan to see if we have any checkable loads.
362-
bool HasCheckableLoad = false;
363-
for (MachineBasicBlock &MBB : MF) {
364-
for (MachineInstr &MI : MBB) {
365-
// Stop searching blocks at an LFENCE.
366-
if (MI.getOpcode() == X86::LFENCE)
367-
break;
368-
369-
// Looking for loads only.
370-
if (!MI.mayLoad())
371-
continue;
372-
373-
// An MFENCE is modeled as a load but doesn't require hardening.
374-
if (MI.getOpcode() == X86::MFENCE)
375-
continue;
376-
377-
HasCheckableLoad = true;
378-
break;
379-
}
380-
if (HasCheckableLoad)
381-
break;
382-
}
393+
bool HasVulnerableLoad = hasVulnerableLoad(MF);
383394

384395
// See if we have any conditional branching blocks that we will need to trace
385396
// predicate state through.
386397
SmallVector<BlockCondInfo, 16> Infos = collectBlockCondInfo(MF);
387398

388399
// If we have no interesting conditions or loads, nothing to do here.
389-
if (!HasCheckableLoad && Infos.empty())
400+
if (!HasVulnerableLoad && Infos.empty())
390401
return true;
391402

392403
unsigned PredStateReg;
@@ -402,7 +413,7 @@ bool X86SpeculativeLoadHardeningPass::runOnMachineFunction(
402413

403414
// If we have loads being hardened and we've asked for call and ret edges to
404415
// get a full fence-based mitigation, inject that fence.
405-
if (HasCheckableLoad && FenceCallAndRet) {
416+
if (HasVulnerableLoad && FenceCallAndRet) {
406417
// We need to insert an LFENCE at the start of the function to suspend any
407418
// incoming misspeculation from the caller. This helps two-fold: the caller
408419
// may not have been protected as this code has been, and this code gets to

0 commit comments

Comments
 (0)