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