51 #define DEBUG_TYPE "x86-cf-opt" 55 cl::desc(
"Avoid optimizing x86 call frames for size"),
71 CallContext() : FrameSetup(nullptr), ArgStoreVector(4, nullptr) {}
83 int64_t ExpectedDist = 0;
89 bool NoStackParams =
false;
109 enum InstClassification { Convert, Skip, Exit };
116 StringRef getPassName()
const override {
return "X86 Optimize Call Frame"; }
122 unsigned SlotSize = 0;
123 unsigned Log2SlotSize = 0;
129 "X86 Call Frame Optimization",
false,
false)
141 if (STI->isTargetDarwin() &&
142 (!MF.getLandingPads().empty() ||
143 (MF.getFunction().needsUnwindTableEntry() && !TFL->hasFP(MF))))
148 if (STI->isTargetWin64())
163 unsigned FrameSetupOpcode =
TII->getCallFrameSetupOpcode();
164 unsigned FrameDestroyOpcode =
TII->getCallFrameDestroyOpcode();
166 !STI->getTargetLowering()->getStackProbeSymbolName(MF).empty();
167 unsigned StackProbeSize = STI->getTargetLowering()->getStackProbeSize(MF);
169 bool InsideFrameSequence =
false;
171 if (
MI.getOpcode() == FrameSetupOpcode) {
172 if (
TII->getFrameSize(
MI) >= StackProbeSize && UseStackProbe)
174 if (InsideFrameSequence)
176 InsideFrameSequence =
true;
177 }
else if (
MI.getOpcode() == FrameDestroyOpcode) {
178 if (!InsideFrameSequence)
180 InsideFrameSequence =
false;
184 if (InsideFrameSequence)
194 ContextVector &CallSeqVector) {
199 if (CannotReserveFrame)
202 unsigned StackAlign = TFL->getStackAlignment();
204 int64_t Advantage = 0;
205 for (
auto CC : CallSeqVector) {
209 if (CC.NoStackParams)
225 if (CC.ExpectedDist % StackAlign)
229 Advantage += (CC.ExpectedDist >> Log2SlotSize) * 3;
233 return Advantage >= 0;
236 bool X86CallFrameOptimization::runOnMachineFunction(
MachineFunction &MF) {
238 TII = STI->getInstrInfo();
239 TFL = STI->getFrameLowering();
246 Log2SlotSize =
Log2_32(SlotSize);
248 if (skipFunction(MF.
getFunction()) || !isLegal(MF))
251 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
253 bool Changed =
false;
255 ContextVector CallSeqVector;
259 if (
MI.getOpcode() == FrameSetupOpcode) {
261 collectCallInfo(MF, MBB,
MI, Context);
262 CallSeqVector.push_back(Context);
265 if (!isProfitable(MF, CallSeqVector))
268 for (
auto CC : CallSeqVector) {
270 adjustCallSequence(MF, CC);
278 X86CallFrameOptimization::InstClassification
279 X86CallFrameOptimization::classifyInstruction(
287 switch (MI->getOpcode()) {
290 case X86::AND64mi8: {
292 return ImmOp.
getImm() == 0 ? Convert : Exit;
298 return ImmOp.
getImm() == -1 ? Convert : Exit;
332 if (MI->isCall() || MI->mayStore())
344 for (
unsigned int U : UsedRegs)
345 if (RegInfo.regsOverlap(Reg, U))
363 assert(I->getOpcode() ==
TII->getCallFrameSetupOpcode());
365 Context.FrameSetup = FrameSetup;
369 unsigned int MaxAdjust =
TII->getFrameSize(*FrameSetup) >> Log2SlotSize;
373 Context.NoStackParams =
true;
380 while (I->getOpcode() == X86::LEA32r || I->isDebugInstr())
384 auto StackPtrCopyInst = MBB.
end();
393 for (
auto J = I; !J->isCall(); ++J)
394 if (J->isCopy() && J->getOperand(0).isReg() && J->getOperand(1).isReg() &&
395 J->getOperand(1).getReg() == StackPtr) {
396 StackPtrCopyInst = J;
397 Context.SPCopy = &*J++;
398 StackPtr = Context.SPCopy->getOperand(0).getReg();
407 Context.ArgStoreVector.resize(MaxAdjust,
nullptr);
411 for (InstClassification Classification = Skip; Classification != Exit; ++
I) {
413 if (I == StackPtrCopyInst)
415 Classification = classifyInstruction(MBB, I, RegInfo, UsedRegs);
416 if (Classification != Convert)
438 "Negative stack displacement when passing parameters");
441 if (StackDisp & (SlotSize - 1))
443 StackDisp >>= Log2SlotSize;
445 assert((
size_t)StackDisp < Context.ArgStoreVector.size() &&
446 "Function call has more parameters than the stack is adjusted for.");
449 if (Context.ArgStoreVector[StackDisp] !=
nullptr)
451 Context.ArgStoreVector[StackDisp] = &*
I;
466 if (I == MBB.
end() || !I->isCall())
470 if ((++I)->getOpcode() !=
TII->getCallFrameDestroyOpcode())
475 auto MMI = Context.ArgStoreVector.begin(), MME = Context.ArgStoreVector.end();
476 for (; MMI != MME; ++MMI, Context.ExpectedDist += SlotSize)
481 if (MMI == Context.ArgStoreVector.begin())
486 for (; MMI != MME; ++MMI)
490 Context.UsePush =
true;
494 const CallContext &Context) {
500 TII->setFrameAdjustment(*FrameSetup, Context.ExpectedDist);
502 DebugLoc DL = FrameSetup->getDebugLoc();
503 bool Is64Bit = STI->is64Bit();
507 for (
int Idx = (Context.ExpectedDist >> Log2SlotSize) - 1; Idx >= 0; --Idx) {
512 switch (Store->getOpcode()) {
523 PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32;
528 if (PushOp.
isImm()) {
529 int64_t Val = PushOp.
getImm();
531 PushOpcode = Is64Bit ? X86::PUSH64i8 : X86::PUSH32i8;
533 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode)).
add(PushOp);
541 if (Is64Bit && Store->getOpcode() == X86::MOV32mr) {
542 Register UndefReg =
MRI->createVirtualRegister(&X86::GR64RegClass);
543 Reg =
MRI->createVirtualRegister(&X86::GR64RegClass);
544 BuildMI(MBB, Context.Call, DL,
TII->get(X86::IMPLICIT_DEF), UndefReg);
545 BuildMI(MBB, Context.Call, DL,
TII->get(X86::INSERT_SUBREG),
Reg)
553 bool SlowPUSHrmm = STI->isAtom() || STI->isSLM();
558 if (!SlowPUSHrmm && (DefMov = canFoldIntoRegPush(FrameSetup, Reg))) {
559 PushOpcode = Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm;
560 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode));
568 PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
569 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode))
582 MBB, std::next(Push), DL,
590 if (Context.SPCopy &&
MRI->use_empty(Context.SPCopy->getOperand(0).getReg()))
591 Context.SPCopy->eraseFromParent();
599 MachineInstr *X86CallFrameOptimization::canFoldIntoRegPush(
615 if (!
MRI->hasOneNonDBGUse(Reg))
622 if ((DefMI.
getOpcode() != X86::MOV32rm &&
624 DefMI.
getParent() != FrameSetup->getParent())
630 if (I->isLoadFoldBarrier())
637 return new X86CallFrameOptimization();
const MachineInstrBuilder & add(const MachineOperand &MO) const
This class represents lattice values for constants.
Implements a dense probed hash-table based set.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
AddrNumOperands - Total number of operands in a memory reference.
INITIALIZE_PASS(X86CallFrameOptimization, DEBUG_TYPE, "X86 Call Frame Optimization", false, false) bool X86CallFrameOptimization
constexpr bool isInt< 8 >(int64_t x)
FunctionPass * createX86CallFrameOptimization()
Return a pass that optimizes the code-size of x86 call sequences.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
unsigned getSlotSize() const
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment)
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
void setHasPushSequences(bool HasPush)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
unsigned const MachineRegisterInfo * MRI
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
std::pair< iterator, bool > insert(const ValueT &V)
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
AddrSegmentReg - The operand # of the segment in the memory operand.
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...
static cl::opt< bool > NoX86CFOpt("no-x86-call-frame-opt", cl::desc("Avoid optimizing x86 call frames for size"), cl::init(false), cl::Hidden)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Register getStackRegister() const
Wrapper class representing virtual and physical registers.