#include "X86.h"
#include "X86InstrInfo.h"
#include "X86Subtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Pass.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
#define DEBUG_TYPE "x86-seses"
STATISTIC(NumLFENCEsInserted, "Number of lfence instructions inserted");
static cl::opt<bool> EnableSpeculativeExecutionSideEffectSuppression(
"x86-seses-enable-without-lvi-cfi",
cl::desc("Force enable speculative execution side effect suppression. "
"(Note: User must pass -mlvi-cfi in order to mitigate indirect "
"branches and returns.)"),
cl::init(false), cl::Hidden);
static cl::opt<bool> OneLFENCEPerBasicBlock(
"x86-seses-one-lfence-per-bb",
cl::desc(
"Omit all lfences other than the first to be placed in a basic block."),
cl::init(false), cl::Hidden);
static cl::opt<bool> OnlyLFENCENonConst(
"x86-seses-only-lfence-non-const",
cl::desc("Only lfence before groups of terminators where at least one "
"branch instruction has an input to the addressing mode that is a "
"register other than %rip."),
cl::init(false), cl::Hidden);
static cl::opt<bool>
OmitBranchLFENCEs("x86-seses-omit-branch-lfences",
cl::desc("Omit all lfences before branch instructions."),
cl::init(false), cl::Hidden);
namespace {
class X86SpeculativeExecutionSideEffectSuppression
: public MachineFunctionPass {
public:
X86SpeculativeExecutionSideEffectSuppression() : MachineFunctionPass(ID) {}
static char ID;
StringRef getPassName() const override {
return "X86 Speculative Execution Side Effect Suppression";
}
bool runOnMachineFunction(MachineFunction &MF) override;
};
}
char X86SpeculativeExecutionSideEffectSuppression::ID = 0;
static bool hasConstantAddressingMode(const MachineInstr &MI) {
for (const MachineOperand &MO : MI.uses())
if (MO.isReg() && X86::RIP != MO.getReg())
return false;
return true;
}
bool X86SpeculativeExecutionSideEffectSuppression::runOnMachineFunction(
MachineFunction &MF) {
const auto &OptLevel = MF.getTarget().getOptLevel();
const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
if (!EnableSpeculativeExecutionSideEffectSuppression &&
!(Subtarget.useLVILoadHardening() && OptLevel == CodeGenOpt::None) &&
!Subtarget.useSpeculativeExecutionSideEffectSuppression())
return false;
LLVM_DEBUG(dbgs() << "********** " << getPassName() << " : " << MF.getName()
<< " **********\n");
bool Modified = false;
const X86InstrInfo *TII = Subtarget.getInstrInfo();
for (MachineBasicBlock &MBB : MF) {
MachineInstr *FirstTerminator = nullptr;
bool PrevInstIsLFENCE = false;
for (auto &MI : MBB) {
if (MI.getOpcode() == X86::LFENCE) {
PrevInstIsLFENCE = true;
continue;
}
if (MI.mayLoadOrStore() && !MI.isTerminator()) {
if (!PrevInstIsLFENCE) {
BuildMI(MBB, MI, DebugLoc(), TII->get(X86::LFENCE));
NumLFENCEsInserted++;
Modified = true;
}
if (OneLFENCEPerBasicBlock)
break;
}
if (MI.isTerminator() && FirstTerminator == nullptr)
FirstTerminator = &MI;
if (!MI.isBranch() || OmitBranchLFENCEs) {
PrevInstIsLFENCE = false;
continue;
}
if (OnlyLFENCENonConst && hasConstantAddressingMode(MI)) {
PrevInstIsLFENCE = false;
continue;
}
if (!PrevInstIsLFENCE) {
assert(FirstTerminator && "Unknown terminator instruction");
BuildMI(MBB, FirstTerminator, DebugLoc(), TII->get(X86::LFENCE));
NumLFENCEsInserted++;
Modified = true;
}
break;
}
}
return Modified;
}
FunctionPass *llvm::createX86SpeculativeExecutionSideEffectSuppression() {
return new X86SpeculativeExecutionSideEffectSuppression();
}
INITIALIZE_PASS(X86SpeculativeExecutionSideEffectSuppression, "x86-seses",
"X86 Speculative Execution Side Effect Suppression", false,
false)