52 #define DEBUG_TYPE "x86-cf-opt"
56 cl::desc(
"Avoid optimizing x86 call frames for size"),
70 CallContext() : FrameSetup(nullptr), MovVector(4, nullptr) {}
82 int64_t ExpectedDist = 0;
88 bool NoStackParams =
false;
108 enum InstClassification { Convert, Skip, Exit };
115 StringRef getPassName()
const override {
return "X86 Optimize Call Frame"; }
122 unsigned Log2SlotSize;
140 if (STI->isTargetDarwin() &&
147 if (STI->isTargetWin64())
157 unsigned FrameSetupOpcode =
TII->getCallFrameSetupOpcode();
158 unsigned FrameDestroyOpcode =
TII->getCallFrameDestroyOpcode();
160 bool InsideFrameSequence =
false;
162 if (
MI.getOpcode() == FrameSetupOpcode) {
163 if (InsideFrameSequence)
165 InsideFrameSequence =
true;
166 }
else if (
MI.getOpcode() == FrameDestroyOpcode) {
167 if (!InsideFrameSequence)
169 InsideFrameSequence =
false;
173 if (InsideFrameSequence)
183 ContextVector &CallSeqVector) {
188 if (CannotReserveFrame)
191 unsigned StackAlign = TFL->getStackAlignment();
193 int64_t Advantage = 0;
194 for (
auto CC : CallSeqVector) {
198 if (CC.NoStackParams)
214 if (CC.ExpectedDist % StackAlign)
218 Advantage += (CC.ExpectedDist >> Log2SlotSize) * 3;
222 return Advantage >= 0;
225 bool X86CallFrameOptimization::runOnMachineFunction(
MachineFunction &MF) {
227 TII = STI->getInstrInfo();
228 TFL = STI->getFrameLowering();
235 Log2SlotSize =
Log2_32(SlotSize);
237 if (skipFunction(*MF.
getFunction()) || !isLegal(MF))
240 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
242 bool Changed =
false;
244 ContextVector CallSeqVector;
248 if (
MI.getOpcode() == FrameSetupOpcode) {
250 collectCallInfo(MF, MBB,
MI, Context);
251 CallSeqVector.push_back(Context);
254 if (!isProfitable(MF, CallSeqVector))
257 for (
auto CC : CallSeqVector) {
259 adjustCallSequence(MF, CC);
267 X86CallFrameOptimization::InstClassification
268 X86CallFrameOptimization::classifyInstruction(
275 int Opcode = MI->getOpcode();
276 if (Opcode == X86::MOV32mi || Opcode == X86::MOV32mr ||
277 Opcode == X86::MOV64mi32 || Opcode == X86::MOV64mr)
305 if (MI->isCall() || MI->mayStore())
311 unsigned int Reg = MO.getReg();
312 if (!RegInfo.isPhysicalRegister(Reg))
317 for (
unsigned int U : UsedRegs)
318 if (RegInfo.regsOverlap(Reg, U))
329 CallContext &Context) {
336 assert(I->getOpcode() == TII->getCallFrameSetupOpcode());
338 Context.FrameSetup = FrameSetup;
342 unsigned int MaxAdjust = TII->getFrameSize(*FrameSetup) >> Log2SlotSize;
346 Context.NoStackParams =
true;
353 while (I->getOpcode() == X86::LEA32r || I->isDebugValue())
360 if (I->isCopy() && I->getOperand(0).isReg() && I->getOperand(1).isReg() &&
361 I->getOperand(1).getReg() == StackPtr) {
362 Context.SPCopy = &*I++;
363 StackPtr = Context.SPCopy->getOperand(0).getReg();
371 Context.MovVector.resize(MaxAdjust,
nullptr);
373 InstClassification Classification;
376 while ((Classification = classifyInstruction(MBB, I, RegInfo, UsedRegs)) !=
378 if (Classification == Skip) {
403 "Negative stack displacement when passing parameters");
406 if (StackDisp & (SlotSize - 1))
408 StackDisp >>= Log2SlotSize;
410 assert((
size_t)StackDisp < Context.MovVector.size() &&
411 "Function call has more parameters than the stack is adjusted for.");
414 if (Context.MovVector[StackDisp] !=
nullptr)
416 Context.MovVector[StackDisp] = &*
I;
421 unsigned int Reg = MO.getReg();
422 if (RegInfo.isPhysicalRegister(Reg))
431 if (I == MBB.
end() || !I->isCall())
435 if ((++I)->getOpcode() != TII->getCallFrameDestroyOpcode())
440 auto MMI = Context.MovVector.begin(), MME = Context.MovVector.end();
441 for (; MMI != MME; ++MMI, Context.ExpectedDist += SlotSize)
446 if (MMI == Context.MovVector.begin())
451 for (; MMI != MME; ++MMI)
455 Context.UsePush =
true;
459 const CallContext &Context) {
465 TII->setFrameAdjustment(*FrameSetup, Context.ExpectedDist);
468 bool Is64Bit = STI->is64Bit();
472 for (
int Idx = (Context.ExpectedDist >> Log2SlotSize) - 1; Idx >= 0; --Idx) {
477 switch (MOV->getOpcode()) {
482 PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32;
487 if (PushOp.
isImm()) {
488 int64_t Val = PushOp.
getImm();
490 PushOpcode = Is64Bit ? X86::PUSH64i8 : X86::PUSH32i8;
492 Push =
BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode)).
add(PushOp);
496 unsigned int Reg = PushOp.
getReg();
500 if (Is64Bit && MOV->getOpcode() == X86::MOV32mr) {
501 unsigned UndefReg =
MRI->createVirtualRegister(&X86::GR64RegClass);
502 Reg =
MRI->createVirtualRegister(&X86::GR64RegClass);
503 BuildMI(MBB, Context.Call, DL, TII->get(X86::IMPLICIT_DEF), UndefReg);
504 BuildMI(MBB, Context.Call, DL, TII->get(X86::INSERT_SUBREG),
Reg)
512 bool SlowPUSHrmm = STI->isAtom() || STI->isSLM();
517 if (!SlowPUSHrmm && (DefMov = canFoldIntoRegPush(FrameSetup, Reg))) {
518 PushOpcode = Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm;
519 Push =
BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode));
527 PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
528 Push =
BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode))
541 MBB, std::next(Push), DL,
549 if (Context.SPCopy &&
MRI->use_empty(Context.SPCopy->getOperand(0).getReg()))
550 Context.SPCopy->eraseFromParent();
558 MachineInstr *X86CallFrameOptimization::canFoldIntoRegPush(
574 if (!
MRI->hasOneNonDBGUse(Reg))
581 if ((DefMI.
getOpcode() != X86::MOV32rm &&
583 DefMI.
getParent() != FrameSetup->getParent())
589 if (I->isLoadFoldBarrier())
596 return new X86CallFrameOptimization();
Implements a dense probed hash-table based set.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
constexpr bool isInt< 8 >(int64_t x)
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
FunctionPass * createX86CallFrameOptimization()
Return a pass that optimizes the code-size of x86 call sequences.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
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...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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...
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void setHasPushSequences(bool HasPush)
const MachineBasicBlock * getParent() const
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.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MachineOperand & getOperand(unsigned i) const
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...
AddrNumOperands - Total number of operands in a memory reference.
FunctionPass class - This class is used to implement most global optimizations.
unsigned getStackRegister() const
AddrSegmentReg - The operand # of the segment in the memory operand.
MachineOperand class - Representation of each machine instruction operand.
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
static cl::opt< bool > NoX86CFOpt("no-x86-call-frame-opt", cl::desc("Avoid optimizing x86 call frames for size"), cl::init(false), cl::Hidden)
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
llvm_unreachable("Unknown RegBank!")
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned getSlotSize() const
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
StringRef - Represent a constant reference to a string, i.e.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...