X86CallFrameOptimization.cpp 21 KB

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