2015-02-02 00:56:04 +08:00
|
|
|
//===----- X86CallFrameOptimization.cpp - Optimize x86 call sequences -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-02-02 00:56:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a pass that optimizes call sequences on x86.
|
2015-02-11 16:53:55 +08:00
|
|
|
// Currently, it converts movs of function parameters onto the stack into
|
2015-02-02 00:56:04 +08:00
|
|
|
// pushes. This is beneficial for two main reasons:
|
2016-04-20 01:43:44 +08:00
|
|
|
// 1) The push instruction encoding is much smaller than a stack-ptr-based mov.
|
2015-02-02 00:56:04 +08:00
|
|
|
// 2) It is possible to push memory arguments directly. So, if the
|
2016-04-13 05:45:09 +08:00
|
|
|
// the transformation is performed pre-reg-alloc, it can help relieve
|
2015-02-02 00:56:04 +08:00
|
|
|
// register pressure.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "MCTargetDesc/X86BaseInfo.h"
|
|
|
|
#include "X86FrameLowering.h"
|
2015-02-02 00:56:04 +08:00
|
|
|
#include "X86InstrInfo.h"
|
|
|
|
#include "X86MachineFunctionInfo.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "X86RegisterInfo.h"
|
2016-04-13 05:45:09 +08:00
|
|
|
#include "X86Subtarget.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2015-02-02 00:56:04 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2015-02-02 00:56:04 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2015-02-02 00:56:04 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2015-02-02 00:56:04 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2017-02-03 06:55:55 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "x86-cf-opt"
|
|
|
|
|
2015-02-07 01:51:54 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoX86CFOpt("no-x86-call-frame-opt",
|
|
|
|
cl::desc("Avoid optimizing x86 call frames for size"),
|
|
|
|
cl::init(false), cl::Hidden);
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-02-03 06:55:55 +08:00
|
|
|
|
2015-02-02 00:56:04 +08:00
|
|
|
class X86CallFrameOptimization : public MachineFunctionPass {
|
|
|
|
public:
|
2019-06-13 10:09:32 +08:00
|
|
|
X86CallFrameOptimization() : MachineFunctionPass(ID) { }
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
2017-10-24 13:47:07 +08:00
|
|
|
static char ID;
|
|
|
|
|
2015-02-02 00:56:04 +08:00
|
|
|
private:
|
2015-02-11 16:53:55 +08:00
|
|
|
// Information we know about a particular call site
|
|
|
|
struct CallContext {
|
2017-10-24 21:24:26 +08:00
|
|
|
CallContext() : FrameSetup(nullptr), ArgStoreVector(4, nullptr) {}
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
// Iterator referring to the frame setup instruction
|
|
|
|
MachineBasicBlock::iterator FrameSetup;
|
|
|
|
|
|
|
|
// Actual call instruction
|
2017-02-03 06:55:55 +08:00
|
|
|
MachineInstr *Call = nullptr;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
|
|
|
// A copy of the stack pointer
|
2017-02-03 06:55:55 +08:00
|
|
|
MachineInstr *SPCopy = nullptr;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
|
|
|
// The total displacement of all passed parameters
|
2017-02-03 06:55:55 +08:00
|
|
|
int64_t ExpectedDist = 0;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2017-10-24 21:24:26 +08:00
|
|
|
// The sequence of storing instructions used to pass the parameters
|
|
|
|
SmallVector<MachineInstr *, 4> ArgStoreVector;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2015-02-12 16:36:35 +08:00
|
|
|
// True if this call site has no stack parameters
|
2017-02-03 06:55:55 +08:00
|
|
|
bool NoStackParams = false;
|
2015-02-12 16:36:35 +08:00
|
|
|
|
2016-04-13 05:45:09 +08:00
|
|
|
// True if this call site can use push instructions
|
2017-02-03 06:55:55 +08:00
|
|
|
bool UsePush = false;
|
2015-02-11 16:53:55 +08:00
|
|
|
};
|
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
typedef SmallVector<CallContext, 8> ContextVector;
|
2015-02-12 16:36:35 +08:00
|
|
|
|
|
|
|
bool isLegal(MachineFunction &MF);
|
2015-07-16 21:54:14 +08:00
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
bool isProfitable(MachineFunction &MF, ContextVector &CallSeqMap);
|
2015-02-12 16:36:35 +08:00
|
|
|
|
2015-02-11 16:53:55 +08:00
|
|
|
void collectCallInfo(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I, CallContext &Context);
|
|
|
|
|
2016-05-06 00:39:31 +08:00
|
|
|
void adjustCallSequence(MachineFunction &MF, const CallContext &Context);
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
MachineInstr *canFoldIntoRegPush(MachineBasicBlock::iterator FrameSetup,
|
|
|
|
unsigned Reg);
|
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
enum InstClassification { Convert, Skip, Exit };
|
|
|
|
|
|
|
|
InstClassification classifyInstruction(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const X86RegisterInfo &RegInfo,
|
|
|
|
DenseSet<unsigned int> &UsedRegs);
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "X86 Optimize Call Frame"; }
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2017-04-13 22:10:52 +08:00
|
|
|
const X86InstrInfo *TII;
|
2015-11-03 16:17:25 +08:00
|
|
|
const X86FrameLowering *TFL;
|
|
|
|
const X86Subtarget *STI;
|
2016-05-02 21:45:25 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2016-04-20 01:43:44 +08:00
|
|
|
unsigned SlotSize;
|
|
|
|
unsigned Log2SlotSize;
|
2015-02-02 00:56:04 +08:00
|
|
|
};
|
|
|
|
|
2017-02-03 06:55:55 +08:00
|
|
|
} // end anonymous namespace
|
2017-10-24 13:47:07 +08:00
|
|
|
char X86CallFrameOptimization::ID = 0;
|
|
|
|
INITIALIZE_PASS(X86CallFrameOptimization, DEBUG_TYPE,
|
|
|
|
"X86 Call Frame Optimization", false, false)
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
// This checks whether the transformation is legal.
|
2015-02-12 16:36:35 +08:00
|
|
|
// Also returns false in cases where it's potentially legal, but
|
|
|
|
// we don't even want to try.
|
|
|
|
bool X86CallFrameOptimization::isLegal(MachineFunction &MF) {
|
2015-02-02 00:56:04 +08:00
|
|
|
if (NoX86CFOpt.getValue())
|
|
|
|
return false;
|
|
|
|
|
2015-11-03 16:17:25 +08:00
|
|
|
// We can't encode multiple DW_CFA_GNU_args_size or DW_CFA_def_cfa_offset
|
|
|
|
// in the compact unwind encoding that Darwin uses. So, bail if there
|
|
|
|
// is a danger of that being generated.
|
2016-04-13 05:45:09 +08:00
|
|
|
if (STI->isTargetDarwin() &&
|
2016-12-02 03:32:15 +08:00
|
|
|
(!MF.getLandingPads().empty() ||
|
2017-12-16 06:22:58 +08:00
|
|
|
(MF.getFunction().needsUnwindTableEntry() && !TFL->hasFP(MF))))
|
2015-10-08 23:45:08 +08:00
|
|
|
return false;
|
|
|
|
|
2016-05-02 21:45:25 +08:00
|
|
|
// It is not valid to change the stack pointer outside the prolog/epilog
|
|
|
|
// on 64-bit Windows.
|
|
|
|
if (STI->isTargetWin64())
|
|
|
|
return false;
|
|
|
|
|
2015-02-02 00:56:04 +08:00
|
|
|
// You would expect straight-line code between call-frame setup and
|
|
|
|
// call-frame destroy. You would be wrong. There are circumstances (e.g.
|
|
|
|
// CMOV_GR8 expansion of a select that feeds a function call!) where we can
|
|
|
|
// end up with the setup and the destroy in different basic blocks.
|
|
|
|
// This is bad, and breaks SP adjustment.
|
|
|
|
// So, check that all of the frames in the function are closed inside
|
|
|
|
// the same block, and, for good measure, that there are no nested frames.
|
2015-05-19 04:27:55 +08:00
|
|
|
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
|
|
|
|
unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
|
2015-02-02 00:56:04 +08:00
|
|
|
for (MachineBasicBlock &BB : MF) {
|
|
|
|
bool InsideFrameSequence = false;
|
|
|
|
for (MachineInstr &MI : BB) {
|
|
|
|
if (MI.getOpcode() == FrameSetupOpcode) {
|
|
|
|
if (InsideFrameSequence)
|
|
|
|
return false;
|
|
|
|
InsideFrameSequence = true;
|
2015-02-11 16:53:55 +08:00
|
|
|
} else if (MI.getOpcode() == FrameDestroyOpcode) {
|
2015-02-02 00:56:04 +08:00
|
|
|
if (!InsideFrameSequence)
|
|
|
|
return false;
|
|
|
|
InsideFrameSequence = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InsideFrameSequence)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:36:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2016-03-23 06:24:52 +08:00
|
|
|
// Check whether this transformation is profitable for a particular
|
2015-02-12 16:36:35 +08:00
|
|
|
// function - in terms of code size.
|
2016-04-13 05:45:09 +08:00
|
|
|
bool X86CallFrameOptimization::isProfitable(MachineFunction &MF,
|
|
|
|
ContextVector &CallSeqVector) {
|
2015-02-12 16:36:35 +08:00
|
|
|
// This transformation is always a win when we do not expect to have
|
2015-02-11 16:53:55 +08:00
|
|
|
// a reserved call frame. Under other circumstances, it may be either
|
2015-02-02 00:56:04 +08:00
|
|
|
// a win or a loss, and requires a heuristic.
|
2016-07-29 02:40:00 +08:00
|
|
|
bool CannotReserveFrame = MF.getFrameInfo().hasVarSizedObjects();
|
2015-02-02 00:56:04 +08:00
|
|
|
if (CannotReserveFrame)
|
|
|
|
return true;
|
|
|
|
|
2015-07-16 09:30:00 +08:00
|
|
|
unsigned StackAlign = TFL->getStackAlignment();
|
2015-07-16 21:54:14 +08:00
|
|
|
|
2015-02-12 16:36:35 +08:00
|
|
|
int64_t Advantage = 0;
|
2015-09-09 02:18:46 +08:00
|
|
|
for (auto CC : CallSeqVector) {
|
2015-02-12 16:36:35 +08:00
|
|
|
// Call sites where no parameters are passed on the stack
|
|
|
|
// do not affect the cost, since there needs to be no
|
|
|
|
// stack adjustment.
|
2015-09-09 02:18:46 +08:00
|
|
|
if (CC.NoStackParams)
|
2015-02-12 16:36:35 +08:00
|
|
|
continue;
|
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
if (!CC.UsePush) {
|
2015-02-12 16:36:35 +08:00
|
|
|
// If we don't use pushes for a particular call site,
|
|
|
|
// we pay for not having a reserved call frame with an
|
|
|
|
// additional sub/add esp pair. The cost is ~3 bytes per instruction,
|
|
|
|
// depending on the size of the constant.
|
|
|
|
// TODO: Callee-pop functions should have a smaller penalty, because
|
|
|
|
// an add is needed even with a reserved call frame.
|
|
|
|
Advantage -= 6;
|
|
|
|
} else {
|
|
|
|
// We can use pushes. First, account for the fixed costs.
|
|
|
|
// We'll need a add after the call.
|
|
|
|
Advantage -= 3;
|
2016-05-02 21:45:25 +08:00
|
|
|
// If we have to realign the stack, we'll also need a sub before
|
2015-09-09 02:18:46 +08:00
|
|
|
if (CC.ExpectedDist % StackAlign)
|
2015-02-12 16:36:35 +08:00
|
|
|
Advantage -= 3;
|
|
|
|
// Now, for each push, we save ~3 bytes. For small constants, we actually,
|
|
|
|
// save more (up to 5 bytes), but 3 should be a good approximation.
|
2016-04-20 01:43:44 +08:00
|
|
|
Advantage += (CC.ExpectedDist >> Log2SlotSize) * 3;
|
2015-02-12 16:36:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:45:09 +08:00
|
|
|
return Advantage >= 0;
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool X86CallFrameOptimization::runOnMachineFunction(MachineFunction &MF) {
|
2015-11-03 16:17:25 +08:00
|
|
|
STI = &MF.getSubtarget<X86Subtarget>();
|
|
|
|
TII = STI->getInstrInfo();
|
|
|
|
TFL = STI->getFrameLowering();
|
2015-02-02 00:56:04 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
|
2016-04-20 01:43:44 +08:00
|
|
|
const X86RegisterInfo &RegInfo =
|
|
|
|
*static_cast<const X86RegisterInfo *>(STI->getRegisterInfo());
|
|
|
|
SlotSize = RegInfo.getSlotSize();
|
|
|
|
assert(isPowerOf2_32(SlotSize) && "Expect power of 2 stack slot size");
|
|
|
|
Log2SlotSize = Log2_32(SlotSize);
|
|
|
|
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()) || !isLegal(MF))
|
2015-02-02 00:56:04 +08:00
|
|
|
return false;
|
|
|
|
|
2015-05-19 04:27:55 +08:00
|
|
|
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
ContextVector CallSeqVector;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2016-03-20 08:16:13 +08:00
|
|
|
for (auto &MBB : MF)
|
|
|
|
for (auto &MI : MBB)
|
|
|
|
if (MI.getOpcode() == FrameSetupOpcode) {
|
2015-09-09 02:18:46 +08:00
|
|
|
CallContext Context;
|
2016-03-20 08:16:13 +08:00
|
|
|
collectCallInfo(MF, MBB, MI, Context);
|
2015-09-09 02:18:46 +08:00
|
|
|
CallSeqVector.push_back(Context);
|
2015-02-11 16:53:55 +08:00
|
|
|
}
|
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
if (!isProfitable(MF, CallSeqVector))
|
2015-02-12 16:36:35 +08:00
|
|
|
return false;
|
|
|
|
|
2016-05-06 00:39:31 +08:00
|
|
|
for (auto CC : CallSeqVector) {
|
|
|
|
if (CC.UsePush) {
|
|
|
|
adjustCallSequence(MF, CC);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
X86CallFrameOptimization::InstClassification
|
|
|
|
X86CallFrameOptimization::classifyInstruction(
|
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
|
|
|
const X86RegisterInfo &RegInfo, DenseSet<unsigned int> &UsedRegs) {
|
|
|
|
if (MI == MBB.end())
|
|
|
|
return Exit;
|
|
|
|
|
2017-10-24 20:13:05 +08:00
|
|
|
// The instructions we actually care about are movs onto the stack or special
|
|
|
|
// cases of constant-stores to stack
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case X86::AND16mi8:
|
|
|
|
case X86::AND32mi8:
|
|
|
|
case X86::AND64mi8: {
|
|
|
|
MachineOperand ImmOp = MI->getOperand(X86::AddrNumOperands);
|
|
|
|
return ImmOp.getImm() == 0 ? Convert : Exit;
|
|
|
|
}
|
|
|
|
case X86::OR16mi8:
|
|
|
|
case X86::OR32mi8:
|
|
|
|
case X86::OR64mi8: {
|
|
|
|
MachineOperand ImmOp = MI->getOperand(X86::AddrNumOperands);
|
|
|
|
return ImmOp.getImm() == -1 ? Convert : Exit;
|
|
|
|
}
|
|
|
|
case X86::MOV32mi:
|
|
|
|
case X86::MOV32mr:
|
|
|
|
case X86::MOV64mi32:
|
|
|
|
case X86::MOV64mr:
|
|
|
|
return Convert;
|
|
|
|
}
|
2015-07-16 21:54:14 +08:00
|
|
|
|
|
|
|
// Not all calling conventions have only stack MOVs between the stack
|
|
|
|
// adjust and the call.
|
|
|
|
|
|
|
|
// We want to tolerate other instructions, to cover more cases.
|
|
|
|
// In particular:
|
|
|
|
// a) PCrel calls, where we expect an additional COPY of the basereg.
|
|
|
|
// b) Passing frame-index addresses.
|
|
|
|
// c) Calling conventions that have inreg parameters. These generate
|
|
|
|
// both copies and movs into registers.
|
|
|
|
// To avoid creating lots of special cases, allow any instruction
|
|
|
|
// that does not write into memory, does not def or use the stack
|
|
|
|
// pointer, and does not def any register that was used by a preceding
|
|
|
|
// push.
|
|
|
|
// (Reading from memory is allowed, even if referenced through a
|
|
|
|
// frame index, since these will get adjusted properly in PEI)
|
|
|
|
|
|
|
|
// The reason for the last condition is that the pushes can't replace
|
|
|
|
// the movs in place, because the order must be reversed.
|
|
|
|
// So if we have a MOV32mr that uses EDX, then an instruction that defs
|
|
|
|
// EDX, and then the call, after the transformation the push will use
|
|
|
|
// the modified version of EDX, and not the original one.
|
|
|
|
// Since we are still in SSA form at this point, we only need to
|
|
|
|
// make sure we don't clobber any *physical* registers that were
|
|
|
|
// used by an earlier mov that will become a push.
|
|
|
|
|
|
|
|
if (MI->isCall() || MI->mayStore())
|
|
|
|
return Exit;
|
|
|
|
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned int Reg = MO.getReg();
|
|
|
|
if (!RegInfo.isPhysicalRegister(Reg))
|
|
|
|
continue;
|
|
|
|
if (RegInfo.regsOverlap(Reg, RegInfo.getStackRegister()))
|
|
|
|
return Exit;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
for (unsigned int U : UsedRegs)
|
|
|
|
if (RegInfo.regsOverlap(Reg, U))
|
|
|
|
return Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Skip;
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:53:55 +08:00
|
|
|
void X86CallFrameOptimization::collectCallInfo(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
CallContext &Context) {
|
2015-02-02 00:56:04 +08:00
|
|
|
// Check that this particular call sequence is amenable to the
|
|
|
|
// transformation.
|
2016-04-13 05:45:09 +08:00
|
|
|
const X86RegisterInfo &RegInfo =
|
|
|
|
*static_cast<const X86RegisterInfo *>(STI->getRegisterInfo());
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// We expect to enter this at the beginning of a call sequence
|
|
|
|
assert(I->getOpcode() == TII->getCallFrameSetupOpcode());
|
|
|
|
MachineBasicBlock::iterator FrameSetup = I++;
|
2015-09-09 02:18:46 +08:00
|
|
|
Context.FrameSetup = FrameSetup;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-02-12 16:36:35 +08:00
|
|
|
// How much do we adjust the stack? This puts an upper bound on
|
|
|
|
// the number of parameters actually passed on it.
|
2017-04-13 22:10:52 +08:00
|
|
|
unsigned int MaxAdjust = TII->getFrameSize(*FrameSetup) >> Log2SlotSize;
|
2015-07-16 21:54:14 +08:00
|
|
|
|
2015-02-12 16:36:35 +08:00
|
|
|
// A zero adjustment means no stack parameters
|
|
|
|
if (!MaxAdjust) {
|
|
|
|
Context.NoStackParams = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-08 03:31:08 +08:00
|
|
|
// Skip over DEBUG_VALUE.
|
|
|
|
// For globals in PIC mode, we can have some LEAs here. Skip them as well.
|
2015-02-02 00:56:04 +08:00
|
|
|
// TODO: Extend this to something that covers more cases.
|
2018-05-09 10:42:00 +08:00
|
|
|
while (I->getOpcode() == X86::LEA32r || I->isDebugInstr())
|
2015-02-02 00:56:04 +08:00
|
|
|
++I;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2016-07-14 05:38:27 +08:00
|
|
|
unsigned StackPtr = RegInfo.getStackRegister();
|
2017-10-24 15:38:29 +08:00
|
|
|
auto StackPtrCopyInst = MBB.end();
|
2016-07-14 05:38:27 +08:00
|
|
|
// SelectionDAG (but not FastISel) inserts a copy of ESP into a virtual
|
2017-10-24 15:38:29 +08:00
|
|
|
// register. If it's there, use that virtual register as stack pointer
|
|
|
|
// instead. Also, we need to locate this instruction so that we can later
|
|
|
|
// safely ignore it while doing the conservative processing of the call chain.
|
|
|
|
// The COPY can be located anywhere between the call-frame setup
|
|
|
|
// instruction and its first use. We use the call instruction as a boundary
|
|
|
|
// because it is usually cheaper to check if an instruction is a call than
|
|
|
|
// checking if an instruction uses a register.
|
|
|
|
for (auto J = I; !J->isCall(); ++J)
|
|
|
|
if (J->isCopy() && J->getOperand(0).isReg() && J->getOperand(1).isReg() &&
|
|
|
|
J->getOperand(1).getReg() == StackPtr) {
|
|
|
|
StackPtrCopyInst = J;
|
|
|
|
Context.SPCopy = &*J++;
|
|
|
|
StackPtr = Context.SPCopy->getOperand(0).getReg();
|
|
|
|
break;
|
|
|
|
}
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// Scan the call setup sequence for the pattern we're looking for.
|
2016-04-20 01:43:44 +08:00
|
|
|
// We only handle a simple case - a sequence of store instructions that
|
|
|
|
// push a sequence of stack-slot-aligned values onto the stack, with
|
2015-02-02 00:56:04 +08:00
|
|
|
// no gaps between them.
|
|
|
|
if (MaxAdjust > 4)
|
2017-10-24 21:24:26 +08:00
|
|
|
Context.ArgStoreVector.resize(MaxAdjust, nullptr);
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
DenseSet<unsigned int> UsedRegs;
|
2015-06-22 16:31:22 +08:00
|
|
|
|
2017-10-24 15:38:29 +08:00
|
|
|
for (InstClassification Classification = Skip; Classification != Exit; ++I) {
|
|
|
|
// If this is the COPY of the stack pointer, it's ok to ignore.
|
|
|
|
if (I == StackPtrCopyInst)
|
|
|
|
continue;
|
|
|
|
Classification = classifyInstruction(MBB, I, RegInfo, UsedRegs);
|
|
|
|
if (Classification != Convert)
|
2015-07-16 21:54:14 +08:00
|
|
|
continue;
|
2016-04-20 01:43:44 +08:00
|
|
|
// We know the instruction has a supported store opcode.
|
2015-02-02 00:56:04 +08:00
|
|
|
// We only want movs of the form:
|
2016-04-20 01:43:44 +08:00
|
|
|
// mov imm/reg, k(%StackPtr)
|
2015-02-02 00:56:04 +08:00
|
|
|
// If we run into something else, bail.
|
|
|
|
// Note that AddrBaseReg may, counter to its name, not be a register,
|
|
|
|
// but rather a frame index.
|
|
|
|
// TODO: Support the fi case. This should probably work now that we
|
|
|
|
// have the infrastructure to track the stack pointer within a call
|
|
|
|
// sequence.
|
|
|
|
if (!I->getOperand(X86::AddrBaseReg).isReg() ||
|
|
|
|
(I->getOperand(X86::AddrBaseReg).getReg() != StackPtr) ||
|
|
|
|
!I->getOperand(X86::AddrScaleAmt).isImm() ||
|
|
|
|
(I->getOperand(X86::AddrScaleAmt).getImm() != 1) ||
|
|
|
|
(I->getOperand(X86::AddrIndexReg).getReg() != X86::NoRegister) ||
|
|
|
|
(I->getOperand(X86::AddrSegmentReg).getReg() != X86::NoRegister) ||
|
|
|
|
!I->getOperand(X86::AddrDisp).isImm())
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
int64_t StackDisp = I->getOperand(X86::AddrDisp).getImm();
|
2015-02-11 16:53:55 +08:00
|
|
|
assert(StackDisp >= 0 &&
|
|
|
|
"Negative stack displacement when passing parameters");
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// We really don't want to consider the unaligned case.
|
2016-04-20 01:43:44 +08:00
|
|
|
if (StackDisp & (SlotSize - 1))
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2016-04-20 01:43:44 +08:00
|
|
|
StackDisp >>= Log2SlotSize;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2017-10-24 21:24:26 +08:00
|
|
|
assert((size_t)StackDisp < Context.ArgStoreVector.size() &&
|
2015-02-11 16:53:55 +08:00
|
|
|
"Function call has more parameters than the stack is adjusted for.");
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// If the same stack slot is being filled twice, something's fishy.
|
2017-10-24 21:24:26 +08:00
|
|
|
if (Context.ArgStoreVector[StackDisp] != nullptr)
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2017-10-24 21:24:26 +08:00
|
|
|
Context.ArgStoreVector[StackDisp] = &*I;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
for (const MachineOperand &MO : I->uses()) {
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned int Reg = MO.getReg();
|
|
|
|
if (RegInfo.isPhysicalRegister(Reg))
|
|
|
|
UsedRegs.insert(Reg);
|
|
|
|
}
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 15:38:29 +08:00
|
|
|
--I;
|
|
|
|
|
2015-07-16 21:54:14 +08:00
|
|
|
// We now expect the end of the sequence. If we stopped early,
|
|
|
|
// or reached the end of the block without finding a call, bail.
|
|
|
|
if (I == MBB.end() || !I->isCall())
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
|
|
|
|
2016-07-12 11:18:50 +08:00
|
|
|
Context.Call = &*I;
|
2017-04-13 22:10:52 +08:00
|
|
|
if ((++I)->getOpcode() != TII->getCallFrameDestroyOpcode())
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// Now, go through the vector, and see that we don't have any gaps,
|
2017-10-24 21:24:26 +08:00
|
|
|
// but only a series of storing instructions.
|
|
|
|
auto MMI = Context.ArgStoreVector.begin(), MME = Context.ArgStoreVector.end();
|
2016-04-20 01:43:44 +08:00
|
|
|
for (; MMI != MME; ++MMI, Context.ExpectedDist += SlotSize)
|
2015-02-02 00:56:04 +08:00
|
|
|
if (*MMI == nullptr)
|
|
|
|
break;
|
2015-02-11 16:53:55 +08:00
|
|
|
|
2015-02-02 00:56:04 +08:00
|
|
|
// If the call had no parameters, do nothing
|
2017-10-24 21:24:26 +08:00
|
|
|
if (MMI == Context.ArgStoreVector.begin())
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-02-11 16:53:55 +08:00
|
|
|
// We are either at the last parameter, or a gap.
|
2015-02-02 00:56:04 +08:00
|
|
|
// Make sure it's not a gap
|
|
|
|
for (; MMI != MME; ++MMI)
|
|
|
|
if (*MMI != nullptr)
|
2015-02-11 16:53:55 +08:00
|
|
|
return;
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-02-11 16:53:55 +08:00
|
|
|
Context.UsePush = true;
|
|
|
|
}
|
|
|
|
|
2016-05-06 00:39:31 +08:00
|
|
|
void X86CallFrameOptimization::adjustCallSequence(MachineFunction &MF,
|
2015-02-11 16:53:55 +08:00
|
|
|
const CallContext &Context) {
|
2015-02-02 00:56:04 +08:00
|
|
|
// Ok, we can in fact do the transformation for this call.
|
|
|
|
// Do not remove the FrameSetup instruction, but adjust the parameters.
|
|
|
|
// PEI will end up finalizing the handling of this.
|
2015-09-09 02:18:46 +08:00
|
|
|
MachineBasicBlock::iterator FrameSetup = Context.FrameSetup;
|
|
|
|
MachineBasicBlock &MBB = *(FrameSetup->getParent());
|
2017-04-13 22:10:52 +08:00
|
|
|
TII->setFrameAdjustment(*FrameSetup, Context.ExpectedDist);
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-09-09 02:18:46 +08:00
|
|
|
DebugLoc DL = FrameSetup->getDebugLoc();
|
2016-05-02 21:45:25 +08:00
|
|
|
bool Is64Bit = STI->is64Bit();
|
2017-10-24 20:13:05 +08:00
|
|
|
// Now, iterate through the vector in reverse order, and replace the store to
|
|
|
|
// stack with pushes. MOVmi/MOVmr doesn't have any defs, so no need to
|
2015-02-02 00:56:04 +08:00
|
|
|
// replace uses.
|
2016-04-20 01:43:44 +08:00
|
|
|
for (int Idx = (Context.ExpectedDist >> Log2SlotSize) - 1; Idx >= 0; --Idx) {
|
2017-10-24 21:24:26 +08:00
|
|
|
MachineBasicBlock::iterator Store = *Context.ArgStoreVector[Idx];
|
|
|
|
MachineOperand PushOp = Store->getOperand(X86::AddrNumOperands);
|
2015-11-03 16:17:25 +08:00
|
|
|
MachineBasicBlock::iterator Push = nullptr;
|
2016-04-20 01:43:44 +08:00
|
|
|
unsigned PushOpcode;
|
2017-10-24 21:24:26 +08:00
|
|
|
switch (Store->getOpcode()) {
|
2016-04-20 01:43:44 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected Opcode!");
|
2017-10-24 20:13:05 +08:00
|
|
|
case X86::AND16mi8:
|
|
|
|
case X86::AND32mi8:
|
|
|
|
case X86::AND64mi8:
|
|
|
|
case X86::OR16mi8:
|
|
|
|
case X86::OR32mi8:
|
|
|
|
case X86::OR64mi8:
|
2016-04-20 01:43:44 +08:00
|
|
|
case X86::MOV32mi:
|
2016-05-02 21:45:25 +08:00
|
|
|
case X86::MOV64mi32:
|
|
|
|
PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32;
|
2015-02-02 00:56:04 +08:00
|
|
|
// If the operand is a small (8-bit) immediate, we can use a
|
|
|
|
// PUSH instruction with a shorter encoding.
|
|
|
|
// Note that isImm() may fail even though this is a MOVmi, because
|
|
|
|
// the operand can also be a symbol.
|
|
|
|
if (PushOp.isImm()) {
|
|
|
|
int64_t Val = PushOp.getImm();
|
|
|
|
if (isInt<8>(Val))
|
2016-05-02 21:45:25 +08:00
|
|
|
PushOpcode = Is64Bit ? X86::PUSH64i8 : X86::PUSH32i8;
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
2017-01-13 17:58:52 +08:00
|
|
|
Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode)).add(PushOp);
|
2016-04-20 01:43:44 +08:00
|
|
|
break;
|
|
|
|
case X86::MOV32mr:
|
2017-02-03 06:55:55 +08:00
|
|
|
case X86::MOV64mr: {
|
2015-02-02 00:56:04 +08:00
|
|
|
unsigned int Reg = PushOp.getReg();
|
|
|
|
|
2016-05-02 21:45:25 +08:00
|
|
|
// If storing a 32-bit vreg on 64-bit targets, extend to a 64-bit vreg
|
|
|
|
// in preparation for the PUSH64. The upper 32 bits can be undef.
|
2017-10-24 21:24:26 +08:00
|
|
|
if (Is64Bit && Store->getOpcode() == X86::MOV32mr) {
|
2016-05-02 21:45:25 +08:00
|
|
|
unsigned UndefReg = MRI->createVirtualRegister(&X86::GR64RegClass);
|
|
|
|
Reg = MRI->createVirtualRegister(&X86::GR64RegClass);
|
|
|
|
BuildMI(MBB, Context.Call, DL, TII->get(X86::IMPLICIT_DEF), UndefReg);
|
|
|
|
BuildMI(MBB, Context.Call, DL, TII->get(X86::INSERT_SUBREG), Reg)
|
2017-01-13 17:58:52 +08:00
|
|
|
.addReg(UndefReg)
|
|
|
|
.add(PushOp)
|
|
|
|
.addImm(X86::sub_32bit);
|
2016-05-02 21:45:25 +08:00
|
|
|
}
|
|
|
|
|
2015-02-02 00:56:04 +08:00
|
|
|
// If PUSHrmm is not slow on this target, try to fold the source of the
|
|
|
|
// push into the instruction.
|
2015-11-03 16:17:25 +08:00
|
|
|
bool SlowPUSHrmm = STI->isAtom() || STI->isSLM();
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// Check that this is legal to fold. Right now, we're extremely
|
|
|
|
// conservative about that.
|
|
|
|
MachineInstr *DefMov = nullptr;
|
|
|
|
if (!SlowPUSHrmm && (DefMov = canFoldIntoRegPush(FrameSetup, Reg))) {
|
2016-05-02 21:45:25 +08:00
|
|
|
PushOpcode = Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm;
|
2016-04-20 01:43:44 +08:00
|
|
|
Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode));
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
unsigned NumOps = DefMov->getDesc().getNumOperands();
|
|
|
|
for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i)
|
|
|
|
Push->addOperand(DefMov->getOperand(i));
|
|
|
|
|
|
|
|
DefMov->eraseFromParent();
|
|
|
|
} else {
|
2016-05-02 21:45:25 +08:00
|
|
|
PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
|
2016-04-20 01:43:44 +08:00
|
|
|
Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode))
|
2016-04-13 05:45:09 +08:00
|
|
|
.addReg(Reg)
|
|
|
|
.getInstr();
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
2016-04-20 01:43:44 +08:00
|
|
|
break;
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
2017-02-03 06:55:55 +08:00
|
|
|
}
|
2015-02-02 00:56:04 +08:00
|
|
|
|
2015-11-03 16:17:25 +08:00
|
|
|
// For debugging, when using SP-based CFA, we need to adjust the CFA
|
|
|
|
// offset after each push.
|
2015-12-06 21:06:20 +08:00
|
|
|
// TODO: This is needed only if we require precise CFA.
|
2017-06-29 21:58:24 +08:00
|
|
|
if (!TFL->hasFP(MF))
|
|
|
|
TFL->BuildCFI(
|
|
|
|
MBB, std::next(Push), DL,
|
|
|
|
MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize));
|
|
|
|
|
2017-10-24 21:24:26 +08:00
|
|
|
MBB.erase(Store);
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The stack-pointer copy is no longer used in the call sequences.
|
|
|
|
// There should not be any other users, but we can't commit to that, so:
|
2016-07-14 05:38:27 +08:00
|
|
|
if (Context.SPCopy && MRI->use_empty(Context.SPCopy->getOperand(0).getReg()))
|
2015-02-11 16:53:55 +08:00
|
|
|
Context.SPCopy->eraseFromParent();
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// Once we've done this, we need to make sure PEI doesn't assume a reserved
|
|
|
|
// frame.
|
|
|
|
X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
|
|
|
|
FuncInfo->setHasPushSequences(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *X86CallFrameOptimization::canFoldIntoRegPush(
|
|
|
|
MachineBasicBlock::iterator FrameSetup, unsigned Reg) {
|
|
|
|
// Do an extremely restricted form of load folding.
|
|
|
|
// ISel will often create patterns like:
|
|
|
|
// movl 4(%edi), %eax
|
|
|
|
// movl 8(%edi), %ecx
|
|
|
|
// movl 12(%edi), %edx
|
|
|
|
// movl %edx, 8(%esp)
|
|
|
|
// movl %ecx, 4(%esp)
|
|
|
|
// movl %eax, (%esp)
|
|
|
|
// call
|
|
|
|
// Get rid of those with prejudice.
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Make sure this is the only use of Reg.
|
|
|
|
if (!MRI->hasOneNonDBGUse(Reg))
|
|
|
|
return nullptr;
|
|
|
|
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineInstr &DefMI = *MRI->getVRegDef(Reg);
|
2015-02-02 00:56:04 +08:00
|
|
|
|
|
|
|
// Make sure the def is a MOV from memory.
|
2016-07-01 05:43:11 +08:00
|
|
|
// If the def is in another block, give up.
|
2016-07-12 11:18:50 +08:00
|
|
|
if ((DefMI.getOpcode() != X86::MOV32rm &&
|
|
|
|
DefMI.getOpcode() != X86::MOV64rm) ||
|
|
|
|
DefMI.getParent() != FrameSetup->getParent())
|
2015-02-02 00:56:04 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2015-08-12 18:14:58 +08:00
|
|
|
// Make sure we don't have any instructions between DefMI and the
|
|
|
|
// push that make folding the load illegal.
|
2016-07-12 11:18:50 +08:00
|
|
|
for (MachineBasicBlock::iterator I = DefMI; I != FrameSetup; ++I)
|
2015-08-12 18:14:58 +08:00
|
|
|
if (I->isLoadFoldBarrier())
|
2015-02-02 00:56:04 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2016-07-12 11:18:50 +08:00
|
|
|
return &DefMI;
|
2015-02-02 00:56:04 +08:00
|
|
|
}
|
2017-02-03 06:55:55 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createX86CallFrameOptimization() {
|
|
|
|
return new X86CallFrameOptimization();
|
|
|
|
}
|