Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 1 | //===- X86VZeroUpper.cpp - AVX vzeroupper instruction inserter ------------===// |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 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 |
Andrea Di Biagio | 4f8bdcb | 2015-02-07 13:56:20 +0000 | [diff] [blame] | 12 | // penalty when transferring control between AVX encoded instructions and old |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 13 | // SSE encoding mode. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 17 | #include "X86.h" |
| 18 | #include "X86InstrInfo.h" |
Elena Demikhovsky | 52e4a0e | 2014-01-05 10:46:09 +0000 | [diff] [blame] | 19 | #include "X86Subtarget.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineOperand.h" |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CallingConv.h" |
| 32 | #include "llvm/IR/DebugLoc.h" |
| 33 | #include "llvm/IR/Function.h" |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 37 | #include <cassert> |
| 38 | |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "x86-vzeroupper" |
| 42 | |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 43 | STATISTIC(NumVZU, "Number of vzeroupper instructions inserted"); |
| 44 | |
| 45 | namespace { |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 46 | |
| 47 | class VZeroUpperInserter : public MachineFunctionPass { |
| 48 | public: |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 49 | VZeroUpperInserter() : MachineFunctionPass(ID) {} |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 50 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 51 | bool runOnMachineFunction(MachineFunction &MF) override; |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 52 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 53 | MachineFunctionProperties getRequiredProperties() const override { |
| 54 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 55 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 56 | } |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 57 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 58 | StringRef getPassName() const override { return "X86 vzeroupper inserter"; } |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 61 | void processBasicBlock(MachineBasicBlock &MBB); |
| 62 | void insertVZeroUpper(MachineBasicBlock::iterator I, |
| 63 | MachineBasicBlock &MBB); |
| 64 | void addDirtySuccessor(MachineBasicBlock &MBB); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 65 | |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 66 | using BlockExitState = enum { PASS_THROUGH, EXITS_CLEAN, EXITS_DIRTY }; |
| 67 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 68 | static const char* getBlockExitStateName(BlockExitState ST); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 69 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 70 | // Core algorithm state: |
| 71 | // BlockState - Each block is either: |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 72 | // - PASS_THROUGH: There are neither YMM/ZMM dirtying instructions nor |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 73 | // vzeroupper instructions in this block. |
| 74 | // - EXITS_CLEAN: There is (or will be) a vzeroupper instruction in this |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 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 |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 77 | // subsequent vzeroupper in the block clears it. |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 78 | // |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 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. |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 82 | // |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 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. |
| 88 | struct BlockState { |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 89 | BlockExitState ExitState = PASS_THROUGH; |
| 90 | bool AddedToDirtySuccessors = false; |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 91 | MachineBasicBlock::iterator FirstUnguardedCall; |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 92 | |
| 93 | BlockState() = default; |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 94 | }; |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 95 | |
| 96 | using BlockStateMap = SmallVector<BlockState, 8>; |
| 97 | using DirtySuccessorsWorkList = SmallVector<MachineBasicBlock *, 8>; |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 98 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 99 | BlockStateMap BlockStates; |
| 100 | DirtySuccessorsWorkList DirtySuccessors; |
| 101 | bool EverMadeChange; |
Amjad Aboud | 719325fe1 | 2016-03-01 11:32:03 +0000 | [diff] [blame] | 102 | bool IsX86INTR; |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 103 | const TargetInstrInfo *TII; |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 104 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 105 | static char ID; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 106 | }; |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 107 | |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 108 | } // end anonymous namespace |
| 109 | |
| 110 | char VZeroUpperInserter::ID = 0; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 111 | |
| 112 | FunctionPass *llvm::createX86IssueVZeroUpperPass() { |
| 113 | return new VZeroUpperInserter(); |
| 114 | } |
| 115 | |
Craig Topper | 3eb6ff9 | 2017-03-22 06:07:58 +0000 | [diff] [blame] | 116 | #ifndef NDEBUG |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 117 | const char* VZeroUpperInserter::getBlockExitStateName(BlockExitState ST) { |
| 118 | switch (ST) { |
| 119 | case PASS_THROUGH: return "Pass-through"; |
| 120 | case EXITS_DIRTY: return "Exits-dirty"; |
| 121 | case EXITS_CLEAN: return "Exits-clean"; |
| 122 | } |
| 123 | llvm_unreachable("Invalid block exit state."); |
| 124 | } |
Craig Topper | 3eb6ff9 | 2017-03-22 06:07:58 +0000 | [diff] [blame] | 125 | #endif |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 126 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 127 | /// VZEROUPPER cleans state that is related to Y/ZMM0-15 only. |
| 128 | /// Thus, there is no need to check for Y/ZMM16 and above. |
| 129 | static bool isYmmOrZmmReg(unsigned Reg) { |
| 130 | return (Reg >= X86::YMM0 && Reg <= X86::YMM15) || |
| 131 | (Reg >= X86::ZMM0 && Reg <= X86::ZMM15); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 134 | static bool checkFnHasLiveInYmmOrZmm(MachineRegisterInfo &MRI) { |
Krzysztof Parzyszek | 72518ea | 2017-10-16 19:08:41 +0000 | [diff] [blame] | 135 | for (std::pair<unsigned, unsigned> LI : MRI.liveins()) |
| 136 | if (isYmmOrZmmReg(LI.first)) |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 137 | return true; |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 142 | static bool clobbersAllYmmAndZmmRegs(const MachineOperand &MO) { |
Elena Demikhovsky | 52e4a0e | 2014-01-05 10:46:09 +0000 | [diff] [blame] | 143 | for (unsigned reg = X86::YMM0; reg <= X86::YMM15; ++reg) { |
Elena Demikhovsky | 9e0df7c | 2013-02-13 08:02:04 +0000 | [diff] [blame] | 144 | if (!MO.clobbersPhysReg(reg)) |
| 145 | return false; |
| 146 | } |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 147 | for (unsigned reg = X86::ZMM0; reg <= X86::ZMM15; ++reg) { |
| 148 | if (!MO.clobbersPhysReg(reg)) |
| 149 | return false; |
| 150 | } |
Elena Demikhovsky | 9e0df7c | 2013-02-13 08:02:04 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 154 | static bool hasYmmOrZmmReg(MachineInstr &MI) { |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 155 | for (const MachineOperand &MO : MI.operands()) { |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 156 | if (MI.isCall() && MO.isRegMask() && !clobbersAllYmmAndZmmRegs(MO)) |
Elena Demikhovsky | 9e0df7c | 2013-02-13 08:02:04 +0000 | [diff] [blame] | 157 | return true; |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 158 | if (!MO.isReg()) |
| 159 | continue; |
| 160 | if (MO.isDebug()) |
| 161 | continue; |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 162 | if (isYmmOrZmmReg(MO.getReg())) |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 168 | /// Check if given call instruction has a RegMask operand. |
| 169 | static bool callHasRegMask(MachineInstr &MI) { |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 170 | assert(MI.isCall() && "Can only be called on call instructions."); |
| 171 | for (const MachineOperand &MO : MI.operands()) { |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 172 | if (MO.isRegMask()) |
| 173 | return true; |
Michael Liao | 14b0284 | 2013-12-03 09:17:32 +0000 | [diff] [blame] | 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Sanjay Patel | 8bc63b2 | 2016-05-20 16:46:01 +0000 | [diff] [blame] | 178 | /// Insert a vzeroupper instruction before I. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 179 | void VZeroUpperInserter::insertVZeroUpper(MachineBasicBlock::iterator I, |
Sanjay Patel | 8bc63b2 | 2016-05-20 16:46:01 +0000 | [diff] [blame] | 180 | MachineBasicBlock &MBB) { |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 181 | DebugLoc dl = I->getDebugLoc(); |
| 182 | BuildMI(MBB, I, dl, TII->get(X86::VZEROUPPER)); |
| 183 | ++NumVZU; |
| 184 | EverMadeChange = true; |
| 185 | } |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 186 | |
Sanjay Patel | 8bc63b2 | 2016-05-20 16:46:01 +0000 | [diff] [blame] | 187 | /// Add MBB to the DirtySuccessors list if it hasn't already been added. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 188 | void VZeroUpperInserter::addDirtySuccessor(MachineBasicBlock &MBB) { |
| 189 | if (!BlockStates[MBB.getNumber()].AddedToDirtySuccessors) { |
| 190 | DirtySuccessors.push_back(&MBB); |
| 191 | BlockStates[MBB.getNumber()].AddedToDirtySuccessors = true; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 192 | } |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Sanjay Patel | 8bc63b2 | 2016-05-20 16:46:01 +0000 | [diff] [blame] | 195 | /// Loop over all of the instructions in the basic block, inserting vzeroupper |
| 196 | /// instructions before function calls. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 197 | void VZeroUpperInserter::processBasicBlock(MachineBasicBlock &MBB) { |
Sanjay Patel | 5496a23 | 2016-05-20 17:07:19 +0000 | [diff] [blame] | 198 | // Start by assuming that the block is PASS_THROUGH which implies no unguarded |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 199 | // calls. |
| 200 | BlockExitState CurState = PASS_THROUGH; |
| 201 | BlockStates[MBB.getNumber()].FirstUnguardedCall = MBB.end(); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 202 | |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 203 | for (MachineInstr &MI : MBB) { |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 204 | bool IsCall = MI.isCall(); |
| 205 | bool IsReturn = MI.isReturn(); |
| 206 | bool IsControlFlow = IsCall || IsReturn; |
| 207 | |
Amjad Aboud | 719325fe1 | 2016-03-01 11:32:03 +0000 | [diff] [blame] | 208 | // No need for vzeroupper before iret in interrupt handler function, |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 209 | // epilogue will restore YMM/ZMM registers if needed. |
| 210 | if (IsX86INTR && IsReturn) |
| 211 | continue; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 212 | |
Sanjay Patel | 3955360 | 2016-05-25 16:39:47 +0000 | [diff] [blame] | 213 | // An existing VZERO* instruction resets the state. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 214 | if (MI.getOpcode() == X86::VZEROALL || MI.getOpcode() == X86::VZEROUPPER) { |
Sanjay Patel | 3955360 | 2016-05-25 16:39:47 +0000 | [diff] [blame] | 215 | CurState = EXITS_CLEAN; |
| 216 | continue; |
| 217 | } |
| 218 | |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 219 | // Shortcut: don't need to check regular instructions in dirty state. |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 220 | if (!IsControlFlow && CurState == EXITS_DIRTY) |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 221 | continue; |
| 222 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 223 | if (hasYmmOrZmmReg(MI)) { |
| 224 | // We found a ymm/zmm-using instruction; this could be an AVX/AVX512 |
| 225 | // instruction, or it could be control flow. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 226 | CurState = EXITS_DIRTY; |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | |
| 230 | // Check for control-flow out of the current function (which might |
| 231 | // indirectly execute SSE instructions). |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 232 | if (!IsControlFlow) |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 233 | continue; |
| 234 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 235 | // If the call has no RegMask, skip it as well. It usually happens on |
| 236 | // helper function calls (such as '_chkstk', '_ftol2') where standard |
| 237 | // calling convention is not used (RegMask is not used to mark register |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 238 | // clobbered and register usage (def/implicit-def/use) is well-defined and |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 239 | // explicitly specified. |
| 240 | if (IsCall && !callHasRegMask(MI)) |
Michael Liao | 14b0284 | 2013-12-03 09:17:32 +0000 | [diff] [blame] | 241 | continue; |
| 242 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 243 | // The VZEROUPPER instruction resets the upper 128 bits of YMM0-YMM15 |
Sanjay Patel | 5496a23 | 2016-05-20 17:07:19 +0000 | [diff] [blame] | 244 | // registers. In addition, the processor changes back to Clean state, after |
| 245 | // which execution of SSE instructions or AVX instructions has no transition |
| 246 | // penalty. Add the VZEROUPPER instruction before any function call/return |
| 247 | // that might execute SSE code. |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 248 | // FIXME: In some cases, we may want to move the VZEROUPPER into a |
| 249 | // predecessor block. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 250 | if (CurState == EXITS_DIRTY) { |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 251 | // After the inserted VZEROUPPER the state becomes clean again, but |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 252 | // other YMM/ZMM may appear before other subsequent calls or even before |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 253 | // the end of the BB. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 254 | insertVZeroUpper(MI, MBB); |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 255 | CurState = EXITS_CLEAN; |
| 256 | } else if (CurState == PASS_THROUGH) { |
| 257 | // If this block is currently in pass-through state and we encounter a |
| 258 | // call then whether we need a vzeroupper or not depends on whether this |
| 259 | // block has successors that exit dirty. Record the location of the call, |
| 260 | // and set the state to EXITS_CLEAN, but do not insert the vzeroupper yet. |
| 261 | // It will be inserted later if necessary. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 262 | BlockStates[MBB.getNumber()].FirstUnguardedCall = MI; |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 263 | CurState = EXITS_CLEAN; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 267 | DEBUG(dbgs() << "MBB #" << MBB.getNumber() << " exit state: " |
| 268 | << getBlockExitStateName(CurState) << '\n'); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 269 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 270 | if (CurState == EXITS_DIRTY) |
| 271 | for (MachineBasicBlock::succ_iterator SI = MBB.succ_begin(), |
| 272 | SE = MBB.succ_end(); |
| 273 | SI != SE; ++SI) |
| 274 | addDirtySuccessor(**SI); |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 275 | |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 276 | BlockStates[MBB.getNumber()].ExitState = CurState; |
| 277 | } |
Eli Friedman | 8f24960 | 2011-11-04 23:46:11 +0000 | [diff] [blame] | 278 | |
Sanjay Patel | 8bc63b2 | 2016-05-20 16:46:01 +0000 | [diff] [blame] | 279 | /// Loop over all of the basic blocks, inserting vzeroupper instructions before |
| 280 | /// function calls. |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 281 | bool VZeroUpperInserter::runOnMachineFunction(MachineFunction &MF) { |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 282 | const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>(); |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 283 | if (!ST.hasAVX() || ST.hasFastPartialYMMorZMMWrite()) |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 284 | return false; |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 285 | TII = ST.getInstrInfo(); |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 286 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 287 | EverMadeChange = false; |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 288 | IsX86INTR = MF.getFunction().getCallingConv() == CallingConv::X86_INTR; |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 289 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 290 | bool FnHasLiveInYmmOrZmm = checkFnHasLiveInYmmOrZmm(MRI); |
Matthias Braun | ada0adf | 2015-01-08 00:33:48 +0000 | [diff] [blame] | 291 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 292 | // Fast check: if the function doesn't use any ymm/zmm registers, we don't |
| 293 | // need to insert any VZEROUPPER instructions. This is constant-time, so it |
| 294 | // is cheap in the common case of no ymm/zmm use. |
| 295 | bool YmmOrZmmUsed = FnHasLiveInYmmOrZmm; |
| 296 | const TargetRegisterClass *RCs[2] = {&X86::VR256RegClass, &X86::VR512RegClass}; |
| 297 | for (auto *RC : RCs) { |
| 298 | if (!YmmOrZmmUsed) { |
| 299 | for (TargetRegisterClass::iterator i = RC->begin(), e = RC->end(); i != e; |
| 300 | i++) { |
| 301 | if (!MRI.reg_nodbg_empty(*i)) { |
| 302 | YmmOrZmmUsed = true; |
| 303 | break; |
| 304 | } |
Matthias Braun | ada0adf | 2015-01-08 00:33:48 +0000 | [diff] [blame] | 305 | } |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 308 | if (!YmmOrZmmUsed) { |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
| 312 | assert(BlockStates.empty() && DirtySuccessors.empty() && |
| 313 | "X86VZeroUpper state should be clear"); |
| 314 | BlockStates.resize(MF.getNumBlockIDs()); |
| 315 | |
| 316 | // Process all blocks. This will compute block exit states, record the first |
| 317 | // unguarded call in each block, and add successors of dirty blocks to the |
| 318 | // DirtySuccessors list. |
Sanjay Patel | fbca70d | 2015-05-05 21:20:52 +0000 | [diff] [blame] | 319 | for (MachineBasicBlock &MBB : MF) |
| 320 | processBasicBlock(MBB); |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 321 | |
Amjad Aboud | 4f97751 | 2017-03-03 09:03:24 +0000 | [diff] [blame] | 322 | // If any YMM/ZMM regs are live-in to this function, add the entry block to |
| 323 | // the DirtySuccessors list |
| 324 | if (FnHasLiveInYmmOrZmm) |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 325 | addDirtySuccessor(MF.front()); |
| 326 | |
Sanjay Patel | 8099fb7e | 2016-05-23 18:01:20 +0000 | [diff] [blame] | 327 | // Re-visit all blocks that are successors of EXITS_DIRTY blocks. Add |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 328 | // vzeroupper instructions to unguarded calls, and propagate EXITS_DIRTY |
| 329 | // through PASS_THROUGH blocks. |
| 330 | while (!DirtySuccessors.empty()) { |
| 331 | MachineBasicBlock &MBB = *DirtySuccessors.back(); |
| 332 | DirtySuccessors.pop_back(); |
| 333 | BlockState &BBState = BlockStates[MBB.getNumber()]; |
| 334 | |
| 335 | // MBB is a successor of a dirty block, so its first call needs to be |
| 336 | // guarded. |
| 337 | if (BBState.FirstUnguardedCall != MBB.end()) |
| 338 | insertVZeroUpper(BBState.FirstUnguardedCall, MBB); |
| 339 | |
Sanjay Patel | 5496a23 | 2016-05-20 17:07:19 +0000 | [diff] [blame] | 340 | // If this successor was a pass-through block, then it is now dirty. Its |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 341 | // successors need to be added to the worklist (if they haven't been |
| 342 | // already). |
| 343 | if (BBState.ExitState == PASS_THROUGH) { |
| 344 | DEBUG(dbgs() << "MBB #" << MBB.getNumber() |
| 345 | << " was Pass-through, is now Dirty-out.\n"); |
Sanjay Patel | 13a0d49 | 2016-05-23 18:00:50 +0000 | [diff] [blame] | 346 | for (MachineBasicBlock *Succ : MBB.successors()) |
| 347 | addDirtySuccessor(*Succ); |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | BlockStates.clear(); |
| 352 | return EverMadeChange; |
Bruno Cardoso Lopes | 2a3ffb5 | 2011-08-23 01:14:17 +0000 | [diff] [blame] | 353 | } |