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