43#define DEBUG_TYPE "x86-insert-vzeroupper"
45STATISTIC(NumVZU,
"Number of vzeroupper instructions inserted");
54 StringRef getPassName()
const override {
return "X86 vzeroupper inserter"; }
58 MachineFunctionProperties getRequiredProperties()
const override {
59 return MachineFunctionProperties().setNoVRegs();
63enum BlockExitState { PASS_THROUGH, EXITS_CLEAN, EXITS_DIRTY };
84 BlockExitState ExitState = PASS_THROUGH;
85 bool AddedToDirtySuccessors =
false;
88 BlockState() =
default;
95char X86InsertVZeroUpperLegacy::ID = 0;
98 return new X86InsertVZeroUpperLegacy();
105 return "Pass-through";
107 return "Exits-dirty";
109 return "Exits-clean";
118 return (
Reg >= X86::YMM0 &&
Reg <= X86::YMM15) ||
119 (
Reg >= X86::ZMM0 &&
Reg <= X86::ZMM15);
123 for (std::pair<MCRegister, Register> LI : MRI.
liveins())
131 for (
unsigned reg = X86::YMM0; reg <= X86::YMM15; ++reg) {
135 for (
unsigned reg = X86::ZMM0; reg <= X86::ZMM15; ++reg) {
158 assert(
MI.isCall() &&
"Can only be called on call instructions.");
177 BlockStateMap &BlockStates,
178 DirtySuccessorsWorkList &DirtySuccessors) {
179 if (!BlockStates[
MBB.getNumber()].AddedToDirtySuccessors) {
180 DirtySuccessors.push_back(&
MBB);
181 BlockStates[
MBB.getNumber()].AddedToDirtySuccessors =
true;
188 BlockStateMap &BlockStates,
189 DirtySuccessorsWorkList &DirtySuccessors,
193 BlockExitState CurState = PASS_THROUGH;
194 BlockStates[
MBB.getNumber()].FirstUnguardedCall =
MBB.end();
195 bool MadeChange =
false;
198 bool IsCall =
MI.isCall();
199 bool IsReturn =
MI.isReturn();
200 bool IsControlFlow = IsCall || IsReturn;
204 if (IsX86INTR && IsReturn)
208 if (
MI.getOpcode() == X86::VZEROALL ||
MI.getOpcode() == X86::VZEROUPPER) {
209 CurState = EXITS_CLEAN;
214 if (!IsControlFlow && CurState == EXITS_DIRTY)
220 CurState = EXITS_DIRTY;
244 if (CurState == EXITS_DIRTY) {
249 CurState = EXITS_CLEAN;
250 }
else if (CurState == PASS_THROUGH) {
256 BlockStates[
MBB.getNumber()].FirstUnguardedCall =
MI;
257 CurState = EXITS_CLEAN;
264 if (CurState == EXITS_DIRTY)
268 BlockStates[
MBB.getNumber()].ExitState = CurState;
276 if (!ST.getCLOpts().use_vzeroupper || !ST.hasAVX() || !ST.insertVZEROUPPER())
286 bool YmmOrZmmUsed = FnHasLiveInYmmOrZmm;
287 for (
const auto *RC : {&X86::VR256RegClass, &X86::VR512_0_15RegClass}) {
302 bool EverMadeChange =
false;
304 DirtySuccessorsWorkList DirtySuccessors;
307 "X86VZeroUpper state should be clear");
318 if (FnHasLiveInYmmOrZmm)
324 while (!DirtySuccessors.empty()) {
326 DirtySuccessors.pop_back();
327 BlockState &BBState = BlockStates[
MBB.getNumber()];
331 if (BBState.FirstUnguardedCall !=
MBB.end())
337 if (BBState.ExitState == PASS_THROUGH) {
339 <<
" was Pass-through, is now Dirty-out.\n");
345 return EverMadeChange;
348bool X86InsertVZeroUpperLegacy::runOnMachineFunction(
MachineFunction &MF) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static bool callHasRegMask(MachineInstr &MI)
Check if given call instruction has a RegMask operand.
static bool checkFnHasLiveInYmmOrZmm(MachineRegisterInfo &MRI)
static bool hasYmmOrZmmReg(MachineInstr &MI)
static bool isYmmOrZmmReg(MCRegister Reg)
VZEROUPPER cleans state that is related to Y/ZMM0-15 only.
static bool clobbersAllYmmAndZmmRegs(const MachineOperand &MO)
static bool processBasicBlock(MachineBasicBlock &MBB, BlockStateMap &BlockStates, DirtySuccessorsWorkList &DirtySuccessors, bool IsX86INTR, const TargetInstrInfo *TII)
Loop over all of the instructions in the basic block, inserting vzeroupper instructions before functi...
static const char * getBlockExitStateName(BlockExitState ST)
static bool insertVZeroUpper(MachineBasicBlock::iterator I, MachineBasicBlock &MBB, const TargetInstrInfo *TII)
Insert a vzeroupper instruction before I.
static void addDirtySuccessor(MachineBasicBlock &MBB, BlockStateMap &BlockStates, DirtySuccessorsWorkList &DirtySuccessors)
Add MBB to the DirtySuccessors list if it hasn't already been added.
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Wrapper class representing physical registers. Should be passed by value.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock & front() const
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool reg_nodbg_empty(Register RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions.
ArrayRef< std::pair< MCRegister, Register > > liveins() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ X86_INTR
x86 hardware interrupt context.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createX86InsertVZeroUpperLegacyPass()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...