30 #define DEBUG_TYPE "x86-vzeroupper"
32 STATISTIC(NumVZU,
"Number of vzeroupper instructions inserted");
45 StringRef getPassName()
const override {
return "X86 vzeroupper inserter"; }
54 typedef enum { PASS_THROUGH, EXITS_CLEAN, EXITS_DIRTY } BlockExitState;
55 static const char* getBlockExitStateName(BlockExitState
ST);
76 BlockState() : ExitState(PASS_THROUGH), AddedToDirtySuccessors(
false) {}
77 BlockExitState ExitState;
78 bool AddedToDirtySuccessors;
84 BlockStateMap BlockStates;
85 DirtySuccessorsWorkList DirtySuccessors;
97 return new VZeroUpperInserter();
101 const char* VZeroUpperInserter::getBlockExitStateName(BlockExitState
ST) {
103 case PASS_THROUGH:
return "Pass-through";
104 case EXITS_DIRTY:
return "Exits-dirty";
105 case EXITS_CLEAN:
return "Exits-clean";
114 return (Reg >= X86::YMM0 && Reg <= X86::YMM15) ||
115 (Reg >= X86::ZMM0 && Reg <= X86::ZMM15);
128 for (
unsigned reg = X86::YMM0; reg <= X86::YMM15; ++reg) {
132 for (
unsigned reg = X86::ZMM0; reg <= X86::ZMM15; ++reg) {
155 assert(MI.
isCall() &&
"Can only be called on call instructions.");
167 BuildMI(MBB, I, dl,
TII->get(X86::VZEROUPPER));
169 EverMadeChange =
true;
174 if (!BlockStates[MBB.
getNumber()].AddedToDirtySuccessors) {
175 DirtySuccessors.push_back(&MBB);
176 BlockStates[MBB.
getNumber()].AddedToDirtySuccessors =
true;
185 BlockExitState CurState = PASS_THROUGH;
186 BlockStates[MBB.
getNumber()].FirstUnguardedCall = MBB.
end();
189 bool IsCall =
MI.isCall();
190 bool IsReturn =
MI.isReturn();
191 bool IsControlFlow = IsCall || IsReturn;
195 if (IsX86INTR && IsReturn)
199 if (
MI.getOpcode() == X86::VZEROALL ||
MI.getOpcode() == X86::VZEROUPPER) {
200 CurState = EXITS_CLEAN;
205 if (!IsControlFlow && CurState == EXITS_DIRTY)
211 CurState = EXITS_DIRTY;
235 if (CurState == EXITS_DIRTY) {
239 insertVZeroUpper(
MI, MBB);
240 CurState = EXITS_CLEAN;
241 }
else if (CurState == PASS_THROUGH) {
247 BlockStates[MBB.getNumber()].FirstUnguardedCall =
MI;
248 CurState = EXITS_CLEAN;
252 DEBUG(
dbgs() <<
"MBB #" << MBB.getNumber() <<
" exit state: "
253 << getBlockExitStateName(CurState) <<
'\n');
255 if (CurState == EXITS_DIRTY)
259 addDirtySuccessor(**SI);
261 BlockStates[MBB.getNumber()].ExitState = CurState;
272 EverMadeChange =
false;
280 bool YmmOrZmmUsed = FnHasLiveInYmmOrZmm;
282 for (
auto *RC : RCs) {
297 assert(BlockStates.empty() && DirtySuccessors.empty() &&
298 "X86VZeroUpper state should be clear");
305 processBasicBlock(MBB);
309 if (FnHasLiveInYmmOrZmm)
310 addDirtySuccessor(MF.front());
315 while (!DirtySuccessors.empty()) {
317 DirtySuccessors.pop_back();
318 BlockState &BBState = BlockStates[MBB.
getNumber()];
322 if (BBState.FirstUnguardedCall != MBB.
end())
323 insertVZeroUpper(BBState.FirstUnguardedCall, MBB);
328 if (BBState.ExitState == PASS_THROUGH) {
330 <<
" was Pass-through, is now Dirty-out.\n");
332 addDirtySuccessor(*Succ);
337 return EverMadeChange;
STATISTIC(NumFunctions,"Total number of functions")
livein_iterator livein_end() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
static bool hasYmmOrZmmReg(MachineInstr &MI)
static bool isYmmOrZmmReg(unsigned Reg)
VZEROUPPER cleans state that is related to Y/ZMM0-15 only.
const X86InstrInfo * getInstrInfo() const override
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
FunctionPass * createX86IssueVZeroUpperPass()
This pass inserts AVX vzeroupper instructions before each call to avoid transition penalty between fu...
iterator_range< mop_iterator > operands()
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
iterator_range< succ_iterator > successors()
X86_INTR - x86 hardware interrupt context.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Reg
All possible values of the reg field in the ModR/M byte.
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Function Alias Analysis false
unsigned const MachineRegisterInfo * MRI
bool hasFastPartialYMMorZMMWrite() const
static bool callHasRegMask(MachineInstr &MI)
Check if given call instruction has a RegMask operand.
std::vector< std::pair< unsigned, unsigned >>::const_iterator livein_iterator
FunctionPass class - This class is used to implement most global optimizations.
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...
livein_iterator livein_begin() const
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.
const MCPhysReg * iterator
llvm_unreachable("Unknown RegBank!")
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.
bool isCall(QueryType Type=AnyInBundle) const
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.
bool reg_nodbg_empty(unsigned RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions...
std::vector< MachineBasicBlock * >::iterator succ_iterator
Properties which a MachineFunction may have at a given point in time.