52 #define DEBUG_TYPE "x86-cf-opt" 56 cl::desc(
"Avoid optimizing x86 call frames for size"),
79 CallContext() : FrameSetup(nullptr), ArgStoreVector(4, nullptr) {}
91 int64_t ExpectedDist = 0;
97 bool NoStackParams =
false;
100 bool UsePush =
false;
117 enum InstClassification { Convert, Skip, Exit };
124 StringRef getPassName()
const override {
return "X86 Optimize Call Frame"; }
131 unsigned Log2SlotSize;
137 "X86 Call Frame Optimization",
false,
false)
149 if (STI->isTargetDarwin() &&
150 (!MF.getLandingPads().empty() ||
151 (MF.getFunction().needsUnwindTableEntry() && !TFL->hasFP(MF))))
156 if (STI->isTargetWin64())
166 unsigned FrameSetupOpcode =
TII->getCallFrameSetupOpcode();
167 unsigned FrameDestroyOpcode =
TII->getCallFrameDestroyOpcode();
169 bool InsideFrameSequence =
false;
171 if (
MI.getOpcode() == FrameSetupOpcode) {
172 if (InsideFrameSequence)
174 InsideFrameSequence =
true;
175 }
else if (
MI.getOpcode() == FrameDestroyOpcode) {
176 if (!InsideFrameSequence)
178 InsideFrameSequence =
false;
182 if (InsideFrameSequence)
192 ContextVector &CallSeqVector) {
197 if (CannotReserveFrame)
200 unsigned StackAlign = TFL->getStackAlignment();
202 int64_t Advantage = 0;
203 for (
auto CC : CallSeqVector) {
207 if (CC.NoStackParams)
223 if (CC.ExpectedDist % StackAlign)
227 Advantage += (CC.ExpectedDist >> Log2SlotSize) * 3;
231 return Advantage >= 0;
234 bool X86CallFrameOptimization::runOnMachineFunction(
MachineFunction &MF) {
236 TII = STI->getInstrInfo();
237 TFL = STI->getFrameLowering();
244 Log2SlotSize =
Log2_32(SlotSize);
246 if (skipFunction(MF.
getFunction()) || !isLegal(MF))
249 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
251 bool Changed =
false;
253 ContextVector CallSeqVector;
257 if (
MI.getOpcode() == FrameSetupOpcode) {
259 collectCallInfo(MF, MBB,
MI, Context);
260 CallSeqVector.push_back(Context);
263 if (!isProfitable(MF, CallSeqVector))
266 for (
auto CC : CallSeqVector) {
268 adjustCallSequence(MF, CC);
276 X86CallFrameOptimization::InstClassification
277 X86CallFrameOptimization::classifyInstruction(
285 switch (MI->getOpcode()) {
288 case X86::AND64mi8: {
290 return ImmOp.
getImm() == 0 ? Convert : Exit;
296 return ImmOp.
getImm() == -1 ? Convert : Exit;
330 if (MI->isCall() || MI->mayStore())
336 unsigned int Reg = MO.getReg();
337 if (!RegInfo.isPhysicalRegister(Reg))
342 for (
unsigned int U : UsedRegs)
343 if (RegInfo.regsOverlap(Reg, U))
361 assert(I->getOpcode() ==
TII->getCallFrameSetupOpcode());
363 Context.FrameSetup = FrameSetup;
367 unsigned int MaxAdjust =
TII->getFrameSize(*FrameSetup) >> Log2SlotSize;
371 Context.NoStackParams =
true;
378 while (I->getOpcode() == X86::LEA32r || I->isDebugInstr())
382 auto StackPtrCopyInst = MBB.
end();
391 for (
auto J = I; !J->isCall(); ++J)
392 if (J->isCopy() && J->getOperand(0).isReg() && J->getOperand(1).isReg() &&
393 J->getOperand(1).getReg() == StackPtr) {
394 StackPtrCopyInst = J;
395 Context.SPCopy = &*J++;
396 StackPtr = Context.SPCopy->getOperand(0).getReg();
405 Context.ArgStoreVector.resize(MaxAdjust,
nullptr);
409 for (InstClassification Classification = Skip; Classification != Exit; ++
I) {
411 if (I == StackPtrCopyInst)
413 Classification = classifyInstruction(MBB, I, RegInfo, UsedRegs);
414 if (Classification != Convert)
436 "Negative stack displacement when passing parameters");
439 if (StackDisp & (SlotSize - 1))
441 StackDisp >>= Log2SlotSize;
443 assert((
size_t)StackDisp < Context.ArgStoreVector.size() &&
444 "Function call has more parameters than the stack is adjusted for.");
447 if (Context.ArgStoreVector[StackDisp] !=
nullptr)
449 Context.ArgStoreVector[StackDisp] = &*
I;
454 unsigned int Reg = MO.getReg();
455 if (RegInfo.isPhysicalRegister(Reg))
464 if (I == MBB.
end() || !I->isCall())
468 if ((++I)->getOpcode() !=
TII->getCallFrameDestroyOpcode())
473 auto MMI = Context.ArgStoreVector.begin(), MME = Context.ArgStoreVector.end();
474 for (; MMI != MME; ++MMI, Context.ExpectedDist += SlotSize)
479 if (MMI == Context.ArgStoreVector.begin())
484 for (; MMI != MME; ++MMI)
488 Context.UsePush =
true;
492 const CallContext &Context) {
498 TII->setFrameAdjustment(*FrameSetup, Context.ExpectedDist);
500 DebugLoc DL = FrameSetup->getDebugLoc();
501 bool Is64Bit = STI->is64Bit();
505 for (
int Idx = (Context.ExpectedDist >> Log2SlotSize) - 1; Idx >= 0; --Idx) {
510 switch (Store->getOpcode()) {
521 PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32;
526 if (PushOp.
isImm()) {
527 int64_t Val = PushOp.
getImm();
529 PushOpcode = Is64Bit ? X86::PUSH64i8 : X86::PUSH32i8;
531 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode)).
add(PushOp);
539 if (Is64Bit && Store->getOpcode() == X86::MOV32mr) {
540 unsigned UndefReg =
MRI->createVirtualRegister(&X86::GR64RegClass);
541 Reg =
MRI->createVirtualRegister(&X86::GR64RegClass);
542 BuildMI(MBB, Context.Call, DL,
TII->get(X86::IMPLICIT_DEF), UndefReg);
543 BuildMI(MBB, Context.Call, DL,
TII->get(X86::INSERT_SUBREG),
Reg)
551 bool SlowPUSHrmm = STI->isAtom() || STI->isSLM();
556 if (!SlowPUSHrmm && (DefMov = canFoldIntoRegPush(FrameSetup, Reg))) {
557 PushOpcode = Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm;
558 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode));
566 PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
567 Push =
BuildMI(MBB, Context.Call, DL,
TII->get(PushOpcode))
580 MBB, std::next(Push), DL,
588 if (Context.SPCopy &&
MRI->use_empty(Context.SPCopy->getOperand(0).getReg()))
589 Context.SPCopy->eraseFromParent();
597 MachineInstr *X86CallFrameOptimization::canFoldIntoRegPush(
613 if (!
MRI->hasOneNonDBGUse(Reg))
620 if ((DefMI.
getOpcode() != X86::MOV32rm &&
622 DefMI.
getParent() != FrameSetup->getParent())
628 if (I->isLoadFoldBarrier())
635 return new X86CallFrameOptimization();
const MachineInstrBuilder & add(const MachineOperand &MO) const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AddrNumOperands - Total number of operands in a memory reference.
Compute iterated dominance frontiers using a linear time algorithm.
Implements a dense probed hash-table based set.
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
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.
void initializeX86CallFrameOptimizationPass(PassRegistry &)
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.
unsigned getStackRegister() const
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.
MachineOperand class - Representation of each machine instruction 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())
AddrSegmentReg - The operand # of the segment in the memory operand.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
const MachineOperand & getOperand(unsigned i) const