LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
X86VZeroUpper.cpp
Go to the documentation of this file.
1 //===-- X86VZeroUpper.cpp - AVX vzeroupper instruction inserter -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 tranfering control between AVX encoded instructions and old
13 // SSE encoding mode.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #define DEBUG_TYPE "x86-vzeroupper"
18 #include "X86.h"
19 #include "X86InstrInfo.h"
20 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Support/Debug.h"
28 using namespace llvm;
29 
30 STATISTIC(NumVZU, "Number of vzeroupper instructions inserted");
31 
32 namespace {
33  struct VZeroUpperInserter : public MachineFunctionPass {
34  static char ID;
35  VZeroUpperInserter() : MachineFunctionPass(ID) {}
36 
37  virtual bool runOnMachineFunction(MachineFunction &MF);
38 
39  bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
40 
41  virtual const char *getPassName() const { return "X86 vzeroupper inserter";}
42 
43  private:
44  const TargetInstrInfo *TII; // Machine instruction info.
45 
46  // Any YMM register live-in to this function?
47  bool FnHasLiveInYmm;
48 
49  // BBState - Contains the state of each MBB: unknown, clean, dirty
51 
52  // BBSolved - Keep track of all MBB which had been already analyzed
53  // and there is no further processing required.
54  BitVector BBSolved;
55 
56  // Machine Basic Blocks are classified according this pass:
57  //
58  // ST_UNKNOWN - The MBB state is unknown, meaning from the entry state
59  // until the MBB exit there isn't a instruction using YMM to change
60  // the state to dirty, or one of the incoming predecessors is unknown
61  // and there's not a dirty predecessor between them.
62  //
63  // ST_CLEAN - No YMM usage in the end of the MBB. A MBB could have
64  // instructions using YMM and be marked ST_CLEAN, as long as the state
65  // is cleaned by a vzeroupper before any call.
66  //
67  // ST_DIRTY - Any MBB ending with a YMM usage not cleaned up by a
68  // vzeroupper instruction.
69  //
70  // ST_INIT - Placeholder for an empty state set
71  //
72  enum {
73  ST_UNKNOWN = 0,
74  ST_CLEAN = 1,
75  ST_DIRTY = 2,
76  ST_INIT = 3
77  };
78 
79  // computeState - Given two states, compute the resulting state, in
80  // the following way
81  //
82  // 1) One dirty state yields another dirty state
83  // 2) All states must be clean for the result to be clean
84  // 3) If none above and one unknown, the result state is also unknown
85  //
86  static unsigned computeState(unsigned PrevState, unsigned CurState) {
87  if (PrevState == ST_INIT)
88  return CurState;
89 
90  if (PrevState == ST_DIRTY || CurState == ST_DIRTY)
91  return ST_DIRTY;
92 
93  if (PrevState == ST_CLEAN && CurState == ST_CLEAN)
94  return ST_CLEAN;
95 
96  return ST_UNKNOWN;
97  }
98 
99  };
100  char VZeroUpperInserter::ID = 0;
101 }
102 
104  return new VZeroUpperInserter();
105 }
106 
107 static bool isYmmReg(unsigned Reg) {
108  return (Reg >= X86::YMM0 && Reg <= X86::YMM31);
109 }
110 
111 static bool isZmmReg(unsigned Reg) {
112  return (Reg >= X86::ZMM0 && Reg <= X86::ZMM31);
113 }
114 
117  E = MRI.livein_end(); I != E; ++I)
118  if (isYmmReg(I->first) || isZmmReg(I->first))
119  return true;
120 
121  return false;
122 }
123 
124 static bool clobbersAllYmmRegs(const MachineOperand &MO) {
125  for (unsigned reg = X86::YMM0; reg < X86::YMM31; ++reg) {
126  if (!MO.clobbersPhysReg(reg))
127  return false;
128  }
129  for (unsigned reg = X86::ZMM0; reg < X86::ZMM31; ++reg) {
130  if (!MO.clobbersPhysReg(reg))
131  return false;
132  }
133  return true;
134 }
135 
136 static bool hasYmmReg(MachineInstr *MI) {
137  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
138  const MachineOperand &MO = MI->getOperand(i);
139  if (MI->isCall() && MO.isRegMask() && !clobbersAllYmmRegs(MO))
140  return true;
141  if (!MO.isReg())
142  continue;
143  if (MO.isDebug())
144  continue;
145  if (isYmmReg(MO.getReg()))
146  return true;
147  }
148  return false;
149 }
150 
151 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
152 /// vzero upper instructions before function calls.
153 bool VZeroUpperInserter::runOnMachineFunction(MachineFunction &MF) {
154  TII = MF.getTarget().getInstrInfo();
156  bool EverMadeChange = false;
157 
158  // Fast check: if the function doesn't use any ymm registers, we don't need
159  // to insert any VZEROUPPER instructions. This is constant-time, so it is
160  // cheap in the common case of no ymm use.
161  bool YMMUsed = false;
162  const TargetRegisterClass *RC = &X86::VR256RegClass;
163  for (TargetRegisterClass::iterator i = RC->begin(), e = RC->end();
164  i != e; i++) {
165  if (!MRI.reg_nodbg_empty(*i)) {
166  YMMUsed = true;
167  break;
168  }
169  }
170  if (!YMMUsed)
171  return EverMadeChange;
172 
173  // Pre-compute the existence of any live-in YMM registers to this function
174  FnHasLiveInYmm = checkFnHasLiveInYmm(MRI);
175 
176  assert(BBState.empty());
177  BBState.resize(MF.getNumBlockIDs(), 0);
178  BBSolved.resize(MF.getNumBlockIDs(), 0);
179 
180  // Each BB state depends on all predecessors, loop over until everything
181  // converges. (Once we converge, we can implicitly mark everything that is
182  // still ST_UNKNOWN as ST_CLEAN.)
183  while (1) {
184  bool MadeChange = false;
185 
186  // Process all basic blocks.
187  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
188  MadeChange |= processBasicBlock(MF, *I);
189 
190  // If this iteration over the code changed anything, keep iterating.
191  if (!MadeChange) break;
192  EverMadeChange = true;
193  }
194 
195  BBState.clear();
196  BBSolved.clear();
197  return EverMadeChange;
198 }
199 
200 /// processBasicBlock - Loop over all of the instructions in the basic block,
201 /// inserting vzero upper instructions before function calls.
202 bool VZeroUpperInserter::processBasicBlock(MachineFunction &MF,
203  MachineBasicBlock &BB) {
204  bool Changed = false;
205  unsigned BBNum = BB.getNumber();
206 
207  // Don't process already solved BBs
208  if (BBSolved[BBNum])
209  return false; // No changes
210 
211  // Check the state of all predecessors
212  unsigned EntryState = ST_INIT;
214  PE = BB.pred_end(); PI != PE; ++PI) {
215  EntryState = computeState(EntryState, BBState[(*PI)->getNumber()]);
216  if (EntryState == ST_DIRTY)
217  break;
218  }
219 
220 
221  // The entry MBB for the function may set the initial state to dirty if
222  // the function receives any YMM incoming arguments
223  if (&BB == MF.begin()) {
224  EntryState = ST_CLEAN;
225  if (FnHasLiveInYmm)
226  EntryState = ST_DIRTY;
227  }
228 
229  // The current state is initialized according to the predecessors
230  unsigned CurState = EntryState;
231  bool BBHasCall = false;
232 
233  for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
234  DebugLoc dl = I->getDebugLoc();
235  MachineInstr *MI = I;
236 
237  // Don't need to check instructions added in prolog.
238  // In prolog, special function calls may be added for specific targets
239  // (e.g. on Windows, a prolog helper '_chkstk' is called when the local
240  // variables exceed 4K bytes on stack.) These helpers won't use/def YMM/XMM
241  // registers.
243  continue;
244 
245  bool isControlFlow = MI->isCall() || MI->isReturn();
246 
247  // Shortcut: don't need to check regular instructions in dirty state.
248  if (!isControlFlow && CurState == ST_DIRTY)
249  continue;
250 
251  if (hasYmmReg(MI)) {
252  // We found a ymm-using instruction; this could be an AVX instruction,
253  // or it could be control flow.
254  CurState = ST_DIRTY;
255  continue;
256  }
257 
258  // Check for control-flow out of the current function (which might
259  // indirectly execute SSE instructions).
260  if (!isControlFlow)
261  continue;
262 
263  BBHasCall = true;
264 
265  // The VZEROUPPER instruction resets the upper 128 bits of all Intel AVX
266  // registers. This instruction has zero latency. In addition, the processor
267  // changes back to Clean state, after which execution of Intel SSE
268  // instructions or Intel AVX instructions has no transition penalty. Add
269  // the VZEROUPPER instruction before any function call/return that might
270  // execute SSE code.
271  // FIXME: In some cases, we may want to move the VZEROUPPER into a
272  // predecessor block.
273  if (CurState == ST_DIRTY) {
274  // Only insert the VZEROUPPER in case the entry state isn't unknown.
275  // When unknown, only compute the information within the block to have
276  // it available in the exit if possible, but don't change the block.
277  if (EntryState != ST_UNKNOWN) {
278  BuildMI(BB, I, dl, TII->get(X86::VZEROUPPER));
279  ++NumVZU;
280  }
281 
282  // After the inserted VZEROUPPER the state becomes clean again, but
283  // other YMM may appear before other subsequent calls or even before
284  // the end of the BB.
285  CurState = ST_CLEAN;
286  }
287  }
288 
289  DEBUG(dbgs() << "MBB #" << BBNum
290  << ", current state: " << CurState << '\n');
291 
292  // A BB can only be considered solved when we both have done all the
293  // necessary transformations, and have computed the exit state. This happens
294  // in two cases:
295  // 1) We know the entry state: this immediately implies the exit state and
296  // all the necessary transformations.
297  // 2) There are no calls, and and a non-call instruction marks this block:
298  // no transformations are necessary, and we know the exit state.
299  if (EntryState != ST_UNKNOWN || (!BBHasCall && CurState != ST_UNKNOWN))
300  BBSolved[BBNum] = true;
301 
302  if (CurState != BBState[BBNum])
303  Changed = true;
304 
305  BBState[BBNum] = CurState;
306  return Changed;
307 }
livein_iterator livein_end() const
FunctionPass * createX86IssueVZeroUpperPass()
unsigned getNumBlockIDs() const
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static bool isZmmReg(unsigned Reg)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
unsigned getNumOperands() const
Definition: MachineInstr.h:265
static bool hasYmmReg(MachineInstr *MI)
bundle_iterator< MachineInstr, instr_iterator > iterator
static bool checkFnHasLiveInYmm(MachineRegisterInfo &MRI)
bool isReturn(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:345
STATISTIC(NumVZU,"Number of vzeroupper instructions inserted")
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
bool getFlag(MIFlag Flag) const
getFlag - Return whether an MI flag is set.
Definition: MachineInstr.h:154
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
virtual const TargetInstrInfo * getInstrInfo() const
livein_iterator livein_begin() const
static bool isYmmReg(unsigned Reg)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
MachineRegisterInfo & getRegInfo()
#define I(x, y, z)
Definition: MD5.cpp:54
bool isCall(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:349
const TargetMachine & getTarget() const
std::vector< std::pair< unsigned, unsigned > >::const_iterator livein_iterator
unsigned getReg() const
getReg - Returns the register number.
static bool clobbersAllYmmRegs(const MachineOperand &MO)
bool isDebug() const
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:97
const MCRegisterInfo & MRI
bool reg_nodbg_empty(unsigned RegNo) const