Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 1 | //===----- X86CallFrameOptimization.cpp - Optimize x86 call sequences -----===// |
| 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 a pass that optimizes call sequences on x86. |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 11 | // Currently, it converts movs of function parameters onto the stack into |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 12 | // pushes. This is beneficial for two main reasons: |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 13 | // 1) The push instruction encoding is much smaller than a stack-ptr-based mov. |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 14 | // 2) It is possible to push memory arguments directly. So, if the |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 15 | // the transformation is performed pre-reg-alloc, it can help relieve |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 16 | // register pressure. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 20 | #include "MCTargetDesc/X86BaseInfo.h" |
| 21 | #include "X86FrameLowering.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 22 | #include "X86InstrInfo.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 23 | #include "X86MachineFunctionInfo.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 24 | #include "X86RegisterInfo.h" |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 25 | #include "X86Subtarget.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseSet.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
| 29 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 31 | #include "llvm/CodeGen/MachineFunction.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstr.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineOperand.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 37 | #include "llvm/IR/DebugLoc.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Function.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCDwarf.h" |
| 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/MathExtras.h" |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetInstrInfo.h" |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetRegisterInfo.h" |
| 45 | #include <cassert> |
| 46 | #include <cstddef> |
| 47 | #include <cstdint> |
| 48 | #include <iterator> |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace llvm; |
| 51 | |
| 52 | #define DEBUG_TYPE "x86-cf-opt" |
| 53 | |
Benjamin Kramer | 970eac4 | 2015-02-06 17:51:54 +0000 | [diff] [blame] | 54 | static cl::opt<bool> |
| 55 | NoX86CFOpt("no-x86-call-frame-opt", |
| 56 | cl::desc("Avoid optimizing x86 call frames for size"), |
| 57 | cl::init(false), cl::Hidden); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 58 | |
| 59 | namespace { |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 60 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 61 | class X86CallFrameOptimization : public MachineFunctionPass { |
| 62 | public: |
| 63 | X86CallFrameOptimization() : MachineFunctionPass(ID) {} |
| 64 | |
| 65 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 66 | |
| 67 | private: |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 68 | // Information we know about a particular call site |
| 69 | struct CallContext { |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 70 | CallContext() : FrameSetup(nullptr), MovVector(4, nullptr) {} |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 71 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 72 | // Iterator referring to the frame setup instruction |
| 73 | MachineBasicBlock::iterator FrameSetup; |
| 74 | |
| 75 | // Actual call instruction |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 76 | MachineInstr *Call = nullptr; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 77 | |
| 78 | // A copy of the stack pointer |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 79 | MachineInstr *SPCopy = nullptr; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 80 | |
| 81 | // The total displacement of all passed parameters |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 82 | int64_t ExpectedDist = 0; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 83 | |
| 84 | // The sequence of movs used to pass the parameters |
| 85 | SmallVector<MachineInstr *, 4> MovVector; |
| 86 | |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 87 | // True if this call site has no stack parameters |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 88 | bool NoStackParams = false; |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 89 | |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 90 | // True if this call site can use push instructions |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 91 | bool UsePush = false; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 94 | typedef SmallVector<CallContext, 8> ContextVector; |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 95 | |
| 96 | bool isLegal(MachineFunction &MF); |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 97 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 98 | bool isProfitable(MachineFunction &MF, ContextVector &CallSeqMap); |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 99 | |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 100 | void collectCallInfo(MachineFunction &MF, MachineBasicBlock &MBB, |
| 101 | MachineBasicBlock::iterator I, CallContext &Context); |
| 102 | |
Hans Wennborg | 501e739 | 2016-05-05 16:39:31 +0000 | [diff] [blame] | 103 | void adjustCallSequence(MachineFunction &MF, const CallContext &Context); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 104 | |
| 105 | MachineInstr *canFoldIntoRegPush(MachineBasicBlock::iterator FrameSetup, |
| 106 | unsigned Reg); |
| 107 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 108 | enum InstClassification { Convert, Skip, Exit }; |
| 109 | |
| 110 | InstClassification classifyInstruction(MachineBasicBlock &MBB, |
| 111 | MachineBasicBlock::iterator MI, |
| 112 | const X86RegisterInfo &RegInfo, |
| 113 | DenseSet<unsigned int> &UsedRegs); |
| 114 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 115 | StringRef getPassName() const override { return "X86 Optimize Call Frame"; } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 116 | |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 117 | const X86InstrInfo *TII; |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 118 | const X86FrameLowering *TFL; |
| 119 | const X86Subtarget *STI; |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 120 | MachineRegisterInfo *MRI; |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 121 | unsigned SlotSize; |
| 122 | unsigned Log2SlotSize; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 123 | static char ID; |
| 124 | }; |
| 125 | |
| 126 | char X86CallFrameOptimization::ID = 0; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 127 | |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 128 | } // end anonymous namespace |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 129 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 130 | // This checks whether the transformation is legal. |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 131 | // Also returns false in cases where it's potentially legal, but |
| 132 | // we don't even want to try. |
| 133 | bool X86CallFrameOptimization::isLegal(MachineFunction &MF) { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 134 | if (NoX86CFOpt.getValue()) |
| 135 | return false; |
| 136 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 137 | // We can't encode multiple DW_CFA_GNU_args_size or DW_CFA_def_cfa_offset |
| 138 | // in the compact unwind encoding that Darwin uses. So, bail if there |
| 139 | // is a danger of that being generated. |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 140 | if (STI->isTargetDarwin() && |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 141 | (!MF.getLandingPads().empty() || |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 142 | (MF.getFunction()->needsUnwindTableEntry() && !TFL->hasFP(MF)))) |
Frederic Riss | 263b772 | 2015-10-08 15:45:08 +0000 | [diff] [blame] | 143 | return false; |
| 144 | |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 145 | // It is not valid to change the stack pointer outside the prolog/epilog |
| 146 | // on 64-bit Windows. |
| 147 | if (STI->isTargetWin64()) |
| 148 | return false; |
| 149 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 150 | // You would expect straight-line code between call-frame setup and |
| 151 | // call-frame destroy. You would be wrong. There are circumstances (e.g. |
| 152 | // CMOV_GR8 expansion of a select that feeds a function call!) where we can |
| 153 | // end up with the setup and the destroy in different basic blocks. |
| 154 | // This is bad, and breaks SP adjustment. |
| 155 | // So, check that all of the frames in the function are closed inside |
| 156 | // the same block, and, for good measure, that there are no nested frames. |
Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 157 | unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode(); |
| 158 | unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 159 | for (MachineBasicBlock &BB : MF) { |
| 160 | bool InsideFrameSequence = false; |
| 161 | for (MachineInstr &MI : BB) { |
| 162 | if (MI.getOpcode() == FrameSetupOpcode) { |
| 163 | if (InsideFrameSequence) |
| 164 | return false; |
| 165 | InsideFrameSequence = true; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 166 | } else if (MI.getOpcode() == FrameDestroyOpcode) { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 167 | if (!InsideFrameSequence) |
| 168 | return false; |
| 169 | InsideFrameSequence = false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (InsideFrameSequence) |
| 174 | return false; |
| 175 | } |
| 176 | |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 177 | return true; |
| 178 | } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 179 | |
Joerg Sonnenberger | 772bb5b | 2016-03-22 22:24:52 +0000 | [diff] [blame] | 180 | // Check whether this transformation is profitable for a particular |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 181 | // function - in terms of code size. |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 182 | bool X86CallFrameOptimization::isProfitable(MachineFunction &MF, |
| 183 | ContextVector &CallSeqVector) { |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 184 | // This transformation is always a win when we do not expect to have |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 185 | // a reserved call frame. Under other circumstances, it may be either |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 186 | // a win or a loss, and requires a heuristic. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 187 | bool CannotReserveFrame = MF.getFrameInfo().hasVarSizedObjects(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 188 | if (CannotReserveFrame) |
| 189 | return true; |
| 190 | |
Reid Kleckner | 938bd6f | 2015-07-16 01:30:00 +0000 | [diff] [blame] | 191 | unsigned StackAlign = TFL->getStackAlignment(); |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 192 | |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 193 | int64_t Advantage = 0; |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 194 | for (auto CC : CallSeqVector) { |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 195 | // Call sites where no parameters are passed on the stack |
| 196 | // do not affect the cost, since there needs to be no |
| 197 | // stack adjustment. |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 198 | if (CC.NoStackParams) |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 199 | continue; |
| 200 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 201 | if (!CC.UsePush) { |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 202 | // If we don't use pushes for a particular call site, |
| 203 | // we pay for not having a reserved call frame with an |
| 204 | // additional sub/add esp pair. The cost is ~3 bytes per instruction, |
| 205 | // depending on the size of the constant. |
| 206 | // TODO: Callee-pop functions should have a smaller penalty, because |
| 207 | // an add is needed even with a reserved call frame. |
| 208 | Advantage -= 6; |
| 209 | } else { |
| 210 | // We can use pushes. First, account for the fixed costs. |
| 211 | // We'll need a add after the call. |
| 212 | Advantage -= 3; |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 213 | // If we have to realign the stack, we'll also need a sub before |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 214 | if (CC.ExpectedDist % StackAlign) |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 215 | Advantage -= 3; |
| 216 | // Now, for each push, we save ~3 bytes. For small constants, we actually, |
| 217 | // save more (up to 5 bytes), but 3 should be a good approximation. |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 218 | Advantage += (CC.ExpectedDist >> Log2SlotSize) * 3; |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 222 | return Advantage >= 0; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool X86CallFrameOptimization::runOnMachineFunction(MachineFunction &MF) { |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 226 | STI = &MF.getSubtarget<X86Subtarget>(); |
| 227 | TII = STI->getInstrInfo(); |
| 228 | TFL = STI->getFrameLowering(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 229 | MRI = &MF.getRegInfo(); |
| 230 | |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 231 | const X86RegisterInfo &RegInfo = |
| 232 | *static_cast<const X86RegisterInfo *>(STI->getRegisterInfo()); |
| 233 | SlotSize = RegInfo.getSlotSize(); |
| 234 | assert(isPowerOf2_32(SlotSize) && "Expect power of 2 stack slot size"); |
| 235 | Log2SlotSize = Log2_32(SlotSize); |
| 236 | |
Andrew Kaylor | 81901d6 | 2016-08-18 22:49:51 +0000 | [diff] [blame] | 237 | if (skipFunction(*MF.getFunction()) || !isLegal(MF)) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 238 | return false; |
| 239 | |
Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 240 | unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 241 | |
| 242 | bool Changed = false; |
| 243 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 244 | ContextVector CallSeqVector; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 245 | |
Michael Kuperstein | 048cc3b | 2016-03-20 00:16:13 +0000 | [diff] [blame] | 246 | for (auto &MBB : MF) |
| 247 | for (auto &MI : MBB) |
| 248 | if (MI.getOpcode() == FrameSetupOpcode) { |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 249 | CallContext Context; |
Michael Kuperstein | 048cc3b | 2016-03-20 00:16:13 +0000 | [diff] [blame] | 250 | collectCallInfo(MF, MBB, MI, Context); |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 251 | CallSeqVector.push_back(Context); |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 254 | if (!isProfitable(MF, CallSeqVector)) |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 255 | return false; |
| 256 | |
Hans Wennborg | 501e739 | 2016-05-05 16:39:31 +0000 | [diff] [blame] | 257 | for (auto CC : CallSeqVector) { |
| 258 | if (CC.UsePush) { |
| 259 | adjustCallSequence(MF, CC); |
| 260 | Changed = true; |
| 261 | } |
| 262 | } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 263 | |
| 264 | return Changed; |
| 265 | } |
| 266 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 267 | X86CallFrameOptimization::InstClassification |
| 268 | X86CallFrameOptimization::classifyInstruction( |
| 269 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 270 | const X86RegisterInfo &RegInfo, DenseSet<unsigned int> &UsedRegs) { |
| 271 | if (MI == MBB.end()) |
| 272 | return Exit; |
| 273 | |
| 274 | // The instructions we actually care about are movs onto the stack |
| 275 | int Opcode = MI->getOpcode(); |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 276 | if (Opcode == X86::MOV32mi || Opcode == X86::MOV32mr || |
| 277 | Opcode == X86::MOV64mi32 || Opcode == X86::MOV64mr) |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 278 | return Convert; |
| 279 | |
| 280 | // Not all calling conventions have only stack MOVs between the stack |
| 281 | // adjust and the call. |
| 282 | |
| 283 | // We want to tolerate other instructions, to cover more cases. |
| 284 | // In particular: |
| 285 | // a) PCrel calls, where we expect an additional COPY of the basereg. |
| 286 | // b) Passing frame-index addresses. |
| 287 | // c) Calling conventions that have inreg parameters. These generate |
| 288 | // both copies and movs into registers. |
| 289 | // To avoid creating lots of special cases, allow any instruction |
| 290 | // that does not write into memory, does not def or use the stack |
| 291 | // pointer, and does not def any register that was used by a preceding |
| 292 | // push. |
| 293 | // (Reading from memory is allowed, even if referenced through a |
| 294 | // frame index, since these will get adjusted properly in PEI) |
| 295 | |
| 296 | // The reason for the last condition is that the pushes can't replace |
| 297 | // the movs in place, because the order must be reversed. |
| 298 | // So if we have a MOV32mr that uses EDX, then an instruction that defs |
| 299 | // EDX, and then the call, after the transformation the push will use |
| 300 | // the modified version of EDX, and not the original one. |
| 301 | // Since we are still in SSA form at this point, we only need to |
| 302 | // make sure we don't clobber any *physical* registers that were |
| 303 | // used by an earlier mov that will become a push. |
| 304 | |
| 305 | if (MI->isCall() || MI->mayStore()) |
| 306 | return Exit; |
| 307 | |
| 308 | for (const MachineOperand &MO : MI->operands()) { |
| 309 | if (!MO.isReg()) |
| 310 | continue; |
| 311 | unsigned int Reg = MO.getReg(); |
| 312 | if (!RegInfo.isPhysicalRegister(Reg)) |
| 313 | continue; |
| 314 | if (RegInfo.regsOverlap(Reg, RegInfo.getStackRegister())) |
| 315 | return Exit; |
| 316 | if (MO.isDef()) { |
| 317 | for (unsigned int U : UsedRegs) |
| 318 | if (RegInfo.regsOverlap(Reg, U)) |
| 319 | return Exit; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return Skip; |
| 324 | } |
| 325 | |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 326 | void X86CallFrameOptimization::collectCallInfo(MachineFunction &MF, |
| 327 | MachineBasicBlock &MBB, |
| 328 | MachineBasicBlock::iterator I, |
| 329 | CallContext &Context) { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 330 | // Check that this particular call sequence is amenable to the |
| 331 | // transformation. |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 332 | const X86RegisterInfo &RegInfo = |
| 333 | *static_cast<const X86RegisterInfo *>(STI->getRegisterInfo()); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 334 | |
| 335 | // We expect to enter this at the beginning of a call sequence |
| 336 | assert(I->getOpcode() == TII->getCallFrameSetupOpcode()); |
| 337 | MachineBasicBlock::iterator FrameSetup = I++; |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 338 | Context.FrameSetup = FrameSetup; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 339 | |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 340 | // How much do we adjust the stack? This puts an upper bound on |
| 341 | // the number of parameters actually passed on it. |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 342 | unsigned int MaxAdjust = TII->getFrameSize(*FrameSetup) >> Log2SlotSize; |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 343 | |
Michael Kuperstein | db95d04 | 2015-02-12 08:36:35 +0000 | [diff] [blame] | 344 | // A zero adjustment means no stack parameters |
| 345 | if (!MaxAdjust) { |
| 346 | Context.NoStackParams = true; |
| 347 | return; |
| 348 | } |
| 349 | |
Michael Kuperstein | 5842b20 | 2016-12-07 19:31:08 +0000 | [diff] [blame] | 350 | // Skip over DEBUG_VALUE. |
| 351 | // For globals in PIC mode, we can have some LEAs here. Skip them as well. |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 352 | // TODO: Extend this to something that covers more cases. |
Michael Kuperstein | 5842b20 | 2016-12-07 19:31:08 +0000 | [diff] [blame] | 353 | while (I->getOpcode() == X86::LEA32r || I->isDebugValue()) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 354 | ++I; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 355 | |
Nico Weber | eb9488b | 2016-07-13 21:38:27 +0000 | [diff] [blame] | 356 | unsigned StackPtr = RegInfo.getStackRegister(); |
| 357 | // SelectionDAG (but not FastISel) inserts a copy of ESP into a virtual |
| 358 | // register here. If it's there, use that virtual register as stack pointer |
| 359 | // instead. |
| 360 | if (I->isCopy() && I->getOperand(0).isReg() && I->getOperand(1).isReg() && |
| 361 | I->getOperand(1).getReg() == StackPtr) { |
| 362 | Context.SPCopy = &*I++; |
| 363 | StackPtr = Context.SPCopy->getOperand(0).getReg(); |
| 364 | } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 365 | |
| 366 | // Scan the call setup sequence for the pattern we're looking for. |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 367 | // We only handle a simple case - a sequence of store instructions that |
| 368 | // push a sequence of stack-slot-aligned values onto the stack, with |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 369 | // no gaps between them. |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 370 | if (MaxAdjust > 4) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 371 | Context.MovVector.resize(MaxAdjust, nullptr); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 372 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 373 | InstClassification Classification; |
| 374 | DenseSet<unsigned int> UsedRegs; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 375 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 376 | while ((Classification = classifyInstruction(MBB, I, RegInfo, UsedRegs)) != |
| 377 | Exit) { |
| 378 | if (Classification == Skip) { |
| 379 | ++I; |
| 380 | continue; |
| 381 | } |
| 382 | |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 383 | // We know the instruction has a supported store opcode. |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 384 | // We only want movs of the form: |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 385 | // mov imm/reg, k(%StackPtr) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 386 | // If we run into something else, bail. |
| 387 | // Note that AddrBaseReg may, counter to its name, not be a register, |
| 388 | // but rather a frame index. |
| 389 | // TODO: Support the fi case. This should probably work now that we |
| 390 | // have the infrastructure to track the stack pointer within a call |
| 391 | // sequence. |
| 392 | if (!I->getOperand(X86::AddrBaseReg).isReg() || |
| 393 | (I->getOperand(X86::AddrBaseReg).getReg() != StackPtr) || |
| 394 | !I->getOperand(X86::AddrScaleAmt).isImm() || |
| 395 | (I->getOperand(X86::AddrScaleAmt).getImm() != 1) || |
| 396 | (I->getOperand(X86::AddrIndexReg).getReg() != X86::NoRegister) || |
| 397 | (I->getOperand(X86::AddrSegmentReg).getReg() != X86::NoRegister) || |
| 398 | !I->getOperand(X86::AddrDisp).isImm()) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 399 | return; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 400 | |
| 401 | int64_t StackDisp = I->getOperand(X86::AddrDisp).getImm(); |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 402 | assert(StackDisp >= 0 && |
| 403 | "Negative stack displacement when passing parameters"); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 404 | |
| 405 | // We really don't want to consider the unaligned case. |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 406 | if (StackDisp & (SlotSize - 1)) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 407 | return; |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 408 | StackDisp >>= Log2SlotSize; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 409 | |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 410 | assert((size_t)StackDisp < Context.MovVector.size() && |
| 411 | "Function call has more parameters than the stack is adjusted for."); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 412 | |
| 413 | // If the same stack slot is being filled twice, something's fishy. |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 414 | if (Context.MovVector[StackDisp] != nullptr) |
| 415 | return; |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 416 | Context.MovVector[StackDisp] = &*I; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 417 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 418 | for (const MachineOperand &MO : I->uses()) { |
| 419 | if (!MO.isReg()) |
| 420 | continue; |
| 421 | unsigned int Reg = MO.getReg(); |
| 422 | if (RegInfo.isPhysicalRegister(Reg)) |
| 423 | UsedRegs.insert(Reg); |
| 424 | } |
| 425 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 426 | ++I; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Michael Kuperstein | dadb847 | 2015-07-16 13:54:14 +0000 | [diff] [blame] | 429 | // We now expect the end of the sequence. If we stopped early, |
| 430 | // or reached the end of the block without finding a call, bail. |
| 431 | if (I == MBB.end() || !I->isCall()) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 432 | return; |
| 433 | |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 434 | Context.Call = &*I; |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 435 | if ((++I)->getOpcode() != TII->getCallFrameDestroyOpcode()) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 436 | return; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 437 | |
| 438 | // Now, go through the vector, and see that we don't have any gaps, |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 439 | // but only a series of MOVs. |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 440 | auto MMI = Context.MovVector.begin(), MME = Context.MovVector.end(); |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 441 | for (; MMI != MME; ++MMI, Context.ExpectedDist += SlotSize) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 442 | if (*MMI == nullptr) |
| 443 | break; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 444 | |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 445 | // If the call had no parameters, do nothing |
| 446 | if (MMI == Context.MovVector.begin()) |
| 447 | return; |
| 448 | |
| 449 | // We are either at the last parameter, or a gap. |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 450 | // Make sure it's not a gap |
| 451 | for (; MMI != MME; ++MMI) |
| 452 | if (*MMI != nullptr) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 453 | return; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 454 | |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 455 | Context.UsePush = true; |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Hans Wennborg | 501e739 | 2016-05-05 16:39:31 +0000 | [diff] [blame] | 458 | void X86CallFrameOptimization::adjustCallSequence(MachineFunction &MF, |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 459 | const CallContext &Context) { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 460 | // Ok, we can in fact do the transformation for this call. |
| 461 | // Do not remove the FrameSetup instruction, but adjust the parameters. |
| 462 | // PEI will end up finalizing the handling of this. |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 463 | MachineBasicBlock::iterator FrameSetup = Context.FrameSetup; |
| 464 | MachineBasicBlock &MBB = *(FrameSetup->getParent()); |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 465 | TII->setFrameAdjustment(*FrameSetup, Context.ExpectedDist); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 466 | |
Andrew Kaylor | e2ea93c | 2015-09-08 18:18:46 +0000 | [diff] [blame] | 467 | DebugLoc DL = FrameSetup->getDebugLoc(); |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 468 | bool Is64Bit = STI->is64Bit(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 469 | // Now, iterate through the vector in reverse order, and replace the movs |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 470 | // with pushes. MOVmi/MOVmr doesn't have any defs, so no need to |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 471 | // replace uses. |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 472 | for (int Idx = (Context.ExpectedDist >> Log2SlotSize) - 1; Idx >= 0; --Idx) { |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 473 | MachineBasicBlock::iterator MOV = *Context.MovVector[Idx]; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 474 | MachineOperand PushOp = MOV->getOperand(X86::AddrNumOperands); |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 475 | MachineBasicBlock::iterator Push = nullptr; |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 476 | unsigned PushOpcode; |
| 477 | switch (MOV->getOpcode()) { |
| 478 | default: |
| 479 | llvm_unreachable("Unexpected Opcode!"); |
| 480 | case X86::MOV32mi: |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 481 | case X86::MOV64mi32: |
| 482 | PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 483 | // If the operand is a small (8-bit) immediate, we can use a |
| 484 | // PUSH instruction with a shorter encoding. |
| 485 | // Note that isImm() may fail even though this is a MOVmi, because |
| 486 | // the operand can also be a symbol. |
| 487 | if (PushOp.isImm()) { |
| 488 | int64_t Val = PushOp.getImm(); |
| 489 | if (isInt<8>(Val)) |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 490 | PushOpcode = Is64Bit ? X86::PUSH64i8 : X86::PUSH32i8; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 491 | } |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 492 | Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode)).add(PushOp); |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 493 | break; |
| 494 | case X86::MOV32mr: |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 495 | case X86::MOV64mr: { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 496 | unsigned int Reg = PushOp.getReg(); |
| 497 | |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 498 | // If storing a 32-bit vreg on 64-bit targets, extend to a 64-bit vreg |
| 499 | // in preparation for the PUSH64. The upper 32 bits can be undef. |
| 500 | if (Is64Bit && MOV->getOpcode() == X86::MOV32mr) { |
| 501 | unsigned UndefReg = MRI->createVirtualRegister(&X86::GR64RegClass); |
| 502 | Reg = MRI->createVirtualRegister(&X86::GR64RegClass); |
| 503 | BuildMI(MBB, Context.Call, DL, TII->get(X86::IMPLICIT_DEF), UndefReg); |
| 504 | BuildMI(MBB, Context.Call, DL, TII->get(X86::INSERT_SUBREG), Reg) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 505 | .addReg(UndefReg) |
| 506 | .add(PushOp) |
| 507 | .addImm(X86::sub_32bit); |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 510 | // If PUSHrmm is not slow on this target, try to fold the source of the |
| 511 | // push into the instruction. |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 512 | bool SlowPUSHrmm = STI->isAtom() || STI->isSLM(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 513 | |
| 514 | // Check that this is legal to fold. Right now, we're extremely |
| 515 | // conservative about that. |
| 516 | MachineInstr *DefMov = nullptr; |
| 517 | if (!SlowPUSHrmm && (DefMov = canFoldIntoRegPush(FrameSetup, Reg))) { |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 518 | PushOpcode = Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm; |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 519 | Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode)); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 520 | |
| 521 | unsigned NumOps = DefMov->getDesc().getNumOperands(); |
| 522 | for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i) |
| 523 | Push->addOperand(DefMov->getOperand(i)); |
| 524 | |
| 525 | DefMov->eraseFromParent(); |
| 526 | } else { |
David L Kreitzer | 0fe4632 | 2016-05-02 13:45:25 +0000 | [diff] [blame] | 527 | PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r; |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 528 | Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode)) |
David L Kreitzer | 99775c1 | 2016-04-12 21:45:09 +0000 | [diff] [blame] | 529 | .addReg(Reg) |
| 530 | .getInstr(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 531 | } |
David L Kreitzer | d5cb341 | 2016-04-19 17:43:44 +0000 | [diff] [blame] | 532 | break; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 533 | } |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 534 | } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 535 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 536 | // For debugging, when using SP-based CFA, we need to adjust the CFA |
| 537 | // offset after each push. |
Michael Kuperstein | 77ce9d3 | 2015-12-06 13:06:20 +0000 | [diff] [blame] | 538 | // TODO: This is needed only if we require precise CFA. |
Daniel Jasper | 559aa75 | 2017-06-29 13:58:24 +0000 | [diff] [blame] | 539 | if (!TFL->hasFP(MF)) |
| 540 | TFL->BuildCFI( |
| 541 | MBB, std::next(Push), DL, |
| 542 | MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize)); |
| 543 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 544 | MBB.erase(MOV); |
| 545 | } |
| 546 | |
| 547 | // The stack-pointer copy is no longer used in the call sequences. |
| 548 | // There should not be any other users, but we can't commit to that, so: |
Nico Weber | eb9488b | 2016-07-13 21:38:27 +0000 | [diff] [blame] | 549 | if (Context.SPCopy && MRI->use_empty(Context.SPCopy->getOperand(0).getReg())) |
Michael Kuperstein | 1921d3d | 2015-02-11 08:53:55 +0000 | [diff] [blame] | 550 | Context.SPCopy->eraseFromParent(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 551 | |
| 552 | // Once we've done this, we need to make sure PEI doesn't assume a reserved |
| 553 | // frame. |
| 554 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 555 | FuncInfo->setHasPushSequences(true); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | MachineInstr *X86CallFrameOptimization::canFoldIntoRegPush( |
| 559 | MachineBasicBlock::iterator FrameSetup, unsigned Reg) { |
| 560 | // Do an extremely restricted form of load folding. |
| 561 | // ISel will often create patterns like: |
| 562 | // movl 4(%edi), %eax |
| 563 | // movl 8(%edi), %ecx |
| 564 | // movl 12(%edi), %edx |
| 565 | // movl %edx, 8(%esp) |
| 566 | // movl %ecx, 4(%esp) |
| 567 | // movl %eax, (%esp) |
| 568 | // call |
| 569 | // Get rid of those with prejudice. |
| 570 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 571 | return nullptr; |
| 572 | |
| 573 | // Make sure this is the only use of Reg. |
| 574 | if (!MRI->hasOneNonDBGUse(Reg)) |
| 575 | return nullptr; |
| 576 | |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 577 | MachineInstr &DefMI = *MRI->getVRegDef(Reg); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 578 | |
| 579 | // Make sure the def is a MOV from memory. |
David L Kreitzer | 29711c0 | 2016-06-30 21:43:11 +0000 | [diff] [blame] | 580 | // If the def is in another block, give up. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 581 | if ((DefMI.getOpcode() != X86::MOV32rm && |
| 582 | DefMI.getOpcode() != X86::MOV64rm) || |
| 583 | DefMI.getParent() != FrameSetup->getParent()) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 584 | return nullptr; |
| 585 | |
Michael Kuperstein | bc7f99a | 2015-08-12 10:14:58 +0000 | [diff] [blame] | 586 | // Make sure we don't have any instructions between DefMI and the |
| 587 | // push that make folding the load illegal. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 588 | for (MachineBasicBlock::iterator I = DefMI; I != FrameSetup; ++I) |
Michael Kuperstein | bc7f99a | 2015-08-12 10:14:58 +0000 | [diff] [blame] | 589 | if (I->isLoadFoldBarrier()) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 590 | return nullptr; |
| 591 | |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 592 | return &DefMI; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 593 | } |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 594 | |
| 595 | FunctionPass *llvm::createX86CallFrameOptimization() { |
| 596 | return new X86CallFrameOptimization(); |
| 597 | } |