40 #define DEBUG_TYPE "x86-vzeroupper" 42 STATISTIC(NumVZU,
"Number of vzeroupper instructions inserted");
57 StringRef getPassName()
const override {
return "X86 vzeroupper inserter"; }
65 using BlockExitState =
enum { PASS_THROUGH, EXITS_CLEAN, EXITS_DIRTY };
67 static const char* getBlockExitStateName(BlockExitState
ST);
88 BlockExitState ExitState = PASS_THROUGH;
89 bool AddedToDirtySuccessors =
false;
92 BlockState() =
default;
98 BlockStateMap BlockStates;
99 DirtySuccessorsWorkList DirtySuccessors;
112 return new VZeroUpperInserter();
116 const char* VZeroUpperInserter::getBlockExitStateName(BlockExitState
ST) {
118 case PASS_THROUGH:
return "Pass-through";
119 case EXITS_DIRTY:
return "Exits-dirty";
120 case EXITS_CLEAN:
return "Exits-clean";
129 return (Reg >= X86::YMM0 && Reg <= X86::YMM15) ||
130 (Reg >= X86::ZMM0 && Reg <= X86::ZMM15);
134 for (std::pair<unsigned, unsigned> LI : MRI.
liveins())
142 for (
unsigned reg = X86::YMM0; reg <= X86::YMM15; ++reg) {
146 for (
unsigned reg = X86::ZMM0; reg <= X86::ZMM15; ++reg) {
169 assert(MI.
isCall() &&
"Can only be called on call instructions.");
181 BuildMI(MBB, I, dl,
TII->get(X86::VZEROUPPER));
183 EverMadeChange =
true;
188 if (!BlockStates[MBB.
getNumber()].AddedToDirtySuccessors) {
189 DirtySuccessors.push_back(&MBB);
190 BlockStates[MBB.
getNumber()].AddedToDirtySuccessors =
true;
199 BlockExitState CurState = PASS_THROUGH;
200 BlockStates[MBB.
getNumber()].FirstUnguardedCall = MBB.
end();
203 bool IsCall =
MI.isCall();
204 bool IsReturn =
MI.isReturn();
205 bool IsControlFlow = IsCall || IsReturn;
209 if (IsX86INTR && IsReturn)
213 if (
MI.getOpcode() == X86::VZEROALL ||
MI.getOpcode() == X86::VZEROUPPER) {
214 CurState = EXITS_CLEAN;
219 if (!IsControlFlow && CurState == EXITS_DIRTY)
225 CurState = EXITS_DIRTY;
249 if (CurState == EXITS_DIRTY) {
253 insertVZeroUpper(
MI, MBB);
254 CurState = EXITS_CLEAN;
255 }
else if (CurState == PASS_THROUGH) {
261 BlockStates[MBB.getNumber()].FirstUnguardedCall =
MI;
262 CurState = EXITS_CLEAN;
266 LLVM_DEBUG(
dbgs() <<
"MBB #" << MBB.getNumber() <<
" exit state: " 267 << getBlockExitStateName(CurState) <<
'\n');
269 if (CurState == EXITS_DIRTY)
273 addDirtySuccessor(**
SI);
275 BlockStates[MBB.getNumber()].ExitState = CurState;
286 EverMadeChange =
false;
294 bool YmmOrZmmUsed = FnHasLiveInYmmOrZmm;
295 for (
auto *RC : {&X86::VR256RegClass, &X86::VR512_0_15RegClass}) {
309 assert(BlockStates.empty() && DirtySuccessors.empty() &&
310 "X86VZeroUpper state should be clear");
317 processBasicBlock(MBB);
321 if (FnHasLiveInYmmOrZmm)
322 addDirtySuccessor(MF.front());
327 while (!DirtySuccessors.empty()) {
329 DirtySuccessors.pop_back();
330 BlockState &BBState = BlockStates[MBB.
getNumber()];
334 if (BBState.FirstUnguardedCall != MBB.
end())
335 insertVZeroUpper(BBState.FirstUnguardedCall, MBB);
340 if (BBState.ExitState == PASS_THROUGH) {
342 <<
" was Pass-through, is now Dirty-out.\n");
344 addDirtySuccessor(*Succ);
349 return EverMadeChange;
bool isCall(QueryType Type=AnyInBundle) const
This class represents lattice values for constants.
static bool hasYmmOrZmmReg(MachineInstr &MI)
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
static bool isYmmOrZmmReg(unsigned Reg)
VZEROUPPER cleans state that is related to Y/ZMM0-15 only.
const X86InstrInfo * getInstrInfo() const override
STATISTIC(NumFunctions, "Total number of functions")
FunctionPass * createX86IssueVZeroUpperPass()
This pass inserts AVX vzeroupper instructions before each call to avoid transition penalty between fu...
iterator_range< mop_iterator > operands()
iterator_range< succ_iterator > successors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
TargetInstrInfo - Interface to description of machine instruction set.
X86_INTR - x86 hardware interrupt context.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool insertVZEROUPPER() const
static bool callHasRegMask(MachineInstr &MI)
Check if given call instruction has a RegMask operand.
bool reg_nodbg_empty(Register RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions...
FunctionPass class - This class is used to implement most global optimizations.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
ArrayRef< std::pair< unsigned, unsigned > > liveins() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static bool checkFnHasLiveInYmmOrZmm(MachineRegisterInfo &MRI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool clobbersAllYmmAndZmmRegs(const MachineOperand &MO)
StringRef - Represent a constant reference to a string, i.e.
std::vector< MachineBasicBlock * >::iterator succ_iterator
Properties which a MachineFunction may have at a given point in time.