LLVM 23.0.0git
X86InsertVZeroUpper.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the pass which inserts x86 AVX vzeroupper instructions
11/// before calls to SSE encoded functions. This avoids transition latency
12/// penalty when transferring control between AVX encoded instructions and old
13/// SSE encoding mode.
14///
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
18#include "X86InstrInfo.h"
19#include "X86Subtarget.h"
21#include "llvm/ADT/Statistic.h"
32#include "llvm/IR/Analysis.h"
33#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/DebugLoc.h"
35#include "llvm/IR/Function.h"
36#include "llvm/Support/Debug.h"
39#include <cassert>
40
41using namespace llvm;
42
43#define DEBUG_TYPE "x86-insert-vzeroupper"
44
45static cl::opt<bool>
46 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
47 cl::desc("Minimize AVX to SSE transition penalty"),
48 cl::init(true));
49
50STATISTIC(NumVZU, "Number of vzeroupper instructions inserted");
51
52namespace {
53class X86InsertVZeroUpperLegacy : public MachineFunctionPass {
54public:
55 static char ID;
56
57 X86InsertVZeroUpperLegacy() : MachineFunctionPass(ID) {}
58
59 StringRef getPassName() const override { return "X86 vzeroupper inserter"; }
60
61 bool runOnMachineFunction(MachineFunction &MF) override;
62
63 MachineFunctionProperties getRequiredProperties() const override {
64 return MachineFunctionProperties().setNoVRegs();
65 }
66};
67
68enum BlockExitState { PASS_THROUGH, EXITS_CLEAN, EXITS_DIRTY };
69
70// Core algorithm state:
71// BlockState - Each block is either:
72// - PASS_THROUGH: There are neither YMM/ZMM dirtying instructions nor
73// vzeroupper instructions in this block.
74// - EXITS_CLEAN: There is (or will be) a vzeroupper instruction in this
75// block that will ensure that YMM/ZMM is clean on exit.
76// - EXITS_DIRTY: An instruction in the block dirties YMM/ZMM and no
77// subsequent vzeroupper in the block clears it.
78//
79// AddedToDirtySuccessors - This flag is raised when a block is added to the
80// DirtySuccessors list to ensure that it's not
81// added multiple times.
82//
83// FirstUnguardedCall - Records the location of the first unguarded call in
84// each basic block that may need to be guarded by a
85// vzeroupper. We won't know whether it actually needs
86// to be guarded until we discover a predecessor that
87// is DIRTY_OUT.
88struct BlockState {
89 BlockExitState ExitState = PASS_THROUGH;
90 bool AddedToDirtySuccessors = false;
91 MachineBasicBlock::iterator FirstUnguardedCall;
92
93 BlockState() = default;
94};
95
96using BlockStateMap = SmallVector<BlockState, 8>;
97using DirtySuccessorsWorkList = SmallVector<MachineBasicBlock *, 8>;
98} // end anonymous namespace
99
100char X86InsertVZeroUpperLegacy::ID = 0;
101
103 return new X86InsertVZeroUpperLegacy();
104}
105
106#ifndef NDEBUG
107static const char *getBlockExitStateName(BlockExitState ST) {
108 switch (ST) {
109 case PASS_THROUGH:
110 return "Pass-through";
111 case EXITS_DIRTY:
112 return "Exits-dirty";
113 case EXITS_CLEAN:
114 return "Exits-clean";
115 }
116 llvm_unreachable("Invalid block exit state.");
117}
118#endif
119
120/// VZEROUPPER cleans state that is related to Y/ZMM0-15 only.
121/// Thus, there is no need to check for Y/ZMM16 and above.
123 return (Reg >= X86::YMM0 && Reg <= X86::YMM15) ||
124 (Reg >= X86::ZMM0 && Reg <= X86::ZMM15);
125}
126
128 for (std::pair<MCRegister, Register> LI : MRI.liveins())
129 if (isYmmOrZmmReg(LI.first))
130 return true;
131
132 return false;
133}
134
136 for (unsigned reg = X86::YMM0; reg <= X86::YMM15; ++reg) {
137 if (!MO.clobbersPhysReg(reg))
138 return false;
139 }
140 for (unsigned reg = X86::ZMM0; reg <= X86::ZMM15; ++reg) {
141 if (!MO.clobbersPhysReg(reg))
142 return false;
143 }
144 return true;
145}
146
148 for (const MachineOperand &MO : MI.operands()) {
149 if (MI.isCall() && MO.isRegMask() && !clobbersAllYmmAndZmmRegs(MO))
150 return true;
151 if (!MO.isReg())
152 continue;
153 if (MO.isDebug())
154 continue;
155 if (isYmmOrZmmReg(MO.getReg().asMCReg()))
156 return true;
157 }
158 return false;
159}
160
161/// Check if given call instruction has a RegMask operand.
163 assert(MI.isCall() && "Can only be called on call instructions.");
164 for (const MachineOperand &MO : MI.operands()) {
165 if (MO.isRegMask())
166 return true;
167 }
168 return false;
169}
170
171/// Insert a vzeroupper instruction before I.
174 const TargetInstrInfo *TII) {
175 BuildMI(MBB, I, I->getDebugLoc(), TII->get(X86::VZEROUPPER));
176 ++NumVZU;
177 return true;
178}
179
180/// Add MBB to the DirtySuccessors list if it hasn't already been added.
182 BlockStateMap &BlockStates,
183 DirtySuccessorsWorkList &DirtySuccessors) {
184 if (!BlockStates[MBB.getNumber()].AddedToDirtySuccessors) {
185 DirtySuccessors.push_back(&MBB);
186 BlockStates[MBB.getNumber()].AddedToDirtySuccessors = true;
187 }
188}
189
190/// Loop over all of the instructions in the basic block, inserting vzeroupper
191/// instructions before function calls.
193 BlockStateMap &BlockStates,
194 DirtySuccessorsWorkList &DirtySuccessors,
195 bool IsX86INTR, const TargetInstrInfo *TII) {
196 // Start by assuming that the block is PASS_THROUGH which implies no unguarded
197 // calls.
198 BlockExitState CurState = PASS_THROUGH;
199 BlockStates[MBB.getNumber()].FirstUnguardedCall = MBB.end();
200 bool MadeChange = false;
201
202 for (MachineInstr &MI : MBB) {
203 bool IsCall = MI.isCall();
204 bool IsReturn = MI.isReturn();
205 bool IsControlFlow = IsCall || IsReturn;
206
207 // No need for vzeroupper before iret in interrupt handler function,
208 // epilogue will restore YMM/ZMM registers if needed.
209 if (IsX86INTR && IsReturn)
210 continue;
211
212 // An existing VZERO* instruction resets the state.
213 if (MI.getOpcode() == X86::VZEROALL || MI.getOpcode() == X86::VZEROUPPER) {
214 CurState = EXITS_CLEAN;
215 continue;
216 }
217
218 // Shortcut: don't need to check regular instructions in dirty state.
219 if (!IsControlFlow && CurState == EXITS_DIRTY)
220 continue;
221
222 if (hasYmmOrZmmReg(MI)) {
223 // We found a ymm/zmm-using instruction; this could be an AVX/AVX512
224 // instruction, or it could be control flow.
225 CurState = EXITS_DIRTY;
226 continue;
227 }
228
229 // Check for control-flow out of the current function (which might
230 // indirectly execute SSE instructions).
231 if (!IsControlFlow)
232 continue;
233
234 // If the call has no RegMask, skip it as well. It usually happens on
235 // helper function calls (such as '_chkstk', '_ftol2') where standard
236 // calling convention is not used (RegMask is not used to mark register
237 // clobbered and register usage (def/implicit-def/use) is well-defined and
238 // explicitly specified.
239 if (IsCall && !callHasRegMask(MI))
240 continue;
241
242 // The VZEROUPPER instruction resets the upper 128 bits of YMM0-YMM15
243 // registers. In addition, the processor changes back to Clean state, after
244 // which execution of SSE instructions or AVX instructions has no transition
245 // penalty. Add the VZEROUPPER instruction before any function call/return
246 // that might execute SSE code.
247 // FIXME: In some cases, we may want to move the VZEROUPPER into a
248 // predecessor block.
249 if (CurState == EXITS_DIRTY) {
250 // After the inserted VZEROUPPER the state becomes clean again, but
251 // other YMM/ZMM may appear before other subsequent calls or even before
252 // the end of the BB.
253 MadeChange |= insertVZeroUpper(MI, MBB, TII);
254 CurState = EXITS_CLEAN;
255 } else if (CurState == PASS_THROUGH) {
256 // If this block is currently in pass-through state and we encounter a
257 // call then whether we need a vzeroupper or not depends on whether this
258 // block has successors that exit dirty. Record the location of the call,
259 // and set the state to EXITS_CLEAN, but do not insert the vzeroupper yet.
260 // It will be inserted later if necessary.
261 BlockStates[MBB.getNumber()].FirstUnguardedCall = MI;
262 CurState = EXITS_CLEAN;
263 }
264 }
265
266 LLVM_DEBUG(dbgs() << "MBB #" << MBB.getNumber() << " exit state: "
267 << getBlockExitStateName(CurState) << '\n');
268
269 if (CurState == EXITS_DIRTY)
270 for (MachineBasicBlock *Succ : MBB.successors())
271 addDirtySuccessor(*Succ, BlockStates, DirtySuccessors);
272
273 BlockStates[MBB.getNumber()].ExitState = CurState;
274 return MadeChange;
275}
276
277/// Loop over all of the basic blocks, inserting vzeroupper instructions before
278/// function calls.
280 if (!UseVZeroUpper)
281 return false;
282
283 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
284 if (!ST.hasAVX() || !ST.insertVZEROUPPER())
285 return false;
286
288
289 bool FnHasLiveInYmmOrZmm = checkFnHasLiveInYmmOrZmm(MRI);
290
291 // Fast check: if the function doesn't use any ymm/zmm registers, we don't
292 // need to insert any VZEROUPPER instructions. This is constant-time, so it
293 // is cheap in the common case of no ymm/zmm use.
294 bool YmmOrZmmUsed = FnHasLiveInYmmOrZmm;
295 for (const auto *RC : {&X86::VR256RegClass, &X86::VR512_0_15RegClass}) {
296 if (!YmmOrZmmUsed) {
297 for (MCPhysReg R : *RC) {
298 if (!MRI.reg_nodbg_empty(R)) {
299 YmmOrZmmUsed = true;
300 break;
301 }
302 }
303 }
304 }
305 if (!YmmOrZmmUsed)
306 return false;
307
308 const TargetInstrInfo *TII = ST.getInstrInfo();
309 bool IsX86INTR = MF.getFunction().getCallingConv() == CallingConv::X86_INTR;
310 bool EverMadeChange = false;
311 BlockStateMap BlockStates(MF.getNumBlockIDs());
312 DirtySuccessorsWorkList DirtySuccessors;
313
314 assert(BlockStates.size() == MF.getNumBlockIDs() && DirtySuccessors.empty() &&
315 "X86VZeroUpper state should be clear");
316
317 // Process all blocks. This will compute block exit states, record the first
318 // unguarded call in each block, and add successors of dirty blocks to the
319 // DirtySuccessors list.
320 for (MachineBasicBlock &MBB : MF)
321 EverMadeChange |=
322 processBasicBlock(MBB, BlockStates, DirtySuccessors, IsX86INTR, TII);
323
324 // If any YMM/ZMM regs are live-in to this function, add the entry block to
325 // the DirtySuccessors list
326 if (FnHasLiveInYmmOrZmm)
327 addDirtySuccessor(MF.front(), BlockStates, DirtySuccessors);
328
329 // Re-visit all blocks that are successors of EXITS_DIRTY blocks. Add
330 // vzeroupper instructions to unguarded calls, and propagate EXITS_DIRTY
331 // through PASS_THROUGH blocks.
332 while (!DirtySuccessors.empty()) {
333 MachineBasicBlock &MBB = *DirtySuccessors.back();
334 DirtySuccessors.pop_back();
335 BlockState &BBState = BlockStates[MBB.getNumber()];
336
337 // MBB is a successor of a dirty block, so its first call needs to be
338 // guarded.
339 if (BBState.FirstUnguardedCall != MBB.end())
340 EverMadeChange |= insertVZeroUpper(BBState.FirstUnguardedCall, MBB, TII);
341
342 // If this successor was a pass-through block, then it is now dirty. Its
343 // successors need to be added to the worklist (if they haven't been
344 // already).
345 if (BBState.ExitState == PASS_THROUGH) {
346 LLVM_DEBUG(dbgs() << "MBB #" << MBB.getNumber()
347 << " was Pass-through, is now Dirty-out.\n");
348 for (MachineBasicBlock *Succ : MBB.successors())
349 addDirtySuccessor(*Succ, BlockStates, DirtySuccessors);
350 }
351 }
352
353 return EverMadeChange;
354}
355
356bool X86InsertVZeroUpperLegacy::runOnMachineFunction(MachineFunction &MF) {
357 return insertVZeroUpper(MF);
358}
359
360PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
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)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
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 cl::opt< bool > UseVZeroUpper("x86-use-vzeroupper", cl::Hidden, cl::desc("Minimize AVX to SSE transition penalty"), cl::init(true))
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.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
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.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ X86_INTR
x86 hardware interrupt context.
initializer< Ty > init(const Ty &Val)
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.
Definition Debug.cpp:207
FunctionPass * createX86InsertVZeroUpperLegacyPass()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21