1 //===---- X86ArgumentStackSlotRebase.cpp - rebase argument stack slot -----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass replace the frame register with a GPR virtual register and set
10 // the stack offset for each instruction which reference argument from stack.
12 //===----------------------------------------------------------------------===//
15 #include "X86MachineFunctionInfo.h"
16 #include "X86RegisterInfo.h"
17 #include "X86Subtarget.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/TargetRegisterInfo.h"
26 #include "llvm/CodeGen/TargetSubtargetInfo.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/InitializePasses.h"
30 #include "llvm/Pass.h"
34 #define DEBUG_TYPE "x86argumentstackrebase"
38 class X86ArgumentStackSlotPass
: public MachineFunctionPass
{
41 static char ID
; // Pass identification, replacement for typeid
43 explicit X86ArgumentStackSlotPass() : MachineFunctionPass(ID
) {}
45 bool runOnMachineFunction(MachineFunction
&MF
) override
;
47 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
49 MachineFunctionPass::getAnalysisUsage(AU
);
53 } // end anonymous namespace
55 char X86ArgumentStackSlotPass::ID
= 0;
57 INITIALIZE_PASS(X86ArgumentStackSlotPass
, DEBUG_TYPE
, "Argument Stack Rebase",
60 FunctionPass
*llvm::createX86ArgumentStackSlotPass() {
61 return new X86ArgumentStackSlotPass();
64 static Register
getArgBaseReg(MachineFunction
&MF
) {
65 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
66 const X86Subtarget
&STI
= MF
.getSubtarget
<X86Subtarget
>();
67 const Function
&F
= MF
.getFunction();
68 CallingConv::ID CC
= F
.getCallingConv();
70 const TargetRegisterClass
*RC
= nullptr;
72 // We need a virtual register in case there is inline assembly
73 // clobber argument base register.
75 RC
= STI
.is64Bit() ? &X86::GR64_ArgRefRegClass
: &X86::GR32_ArgRefRegClass
;
77 case CallingConv::X86_RegCall
:
78 // FIXME: For regcall there is no scratch register on 32-bit target.
79 // We may use a callee saved register as argument base register and
80 // save it before being changed as base pointer. We need DW_CFA to
81 // indicate where the callee saved register is saved, so that it can
82 // be correctly unwind.
89 RC
= STI
.is64Bit() ? &X86::GR64_ArgRefRegClass
: nullptr;
91 // TODO: Refine register class for each calling convention.
96 return MRI
.createVirtualRegister(RC
);
101 bool X86ArgumentStackSlotPass::runOnMachineFunction(MachineFunction
&MF
) {
102 const Function
&F
= MF
.getFunction();
103 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
104 const X86Subtarget
&STI
= MF
.getSubtarget
<X86Subtarget
>();
105 const X86RegisterInfo
*TRI
= STI
.getRegisterInfo();
106 const X86InstrInfo
*TII
= STI
.getInstrInfo();
107 X86MachineFunctionInfo
*X86FI
= MF
.getInfo
<X86MachineFunctionInfo
>();
108 bool Changed
= false;
110 if (F
.hasFnAttribute(Attribute::Naked
))
112 // Only support Linux and ELF.
113 if (!STI
.isTargetLinux() && !STI
.isTargetELF())
115 if (!TRI
->hasBasePointer(MF
))
118 if (STI
.isTarget64BitILP32())
121 Register BasePtr
= TRI
->getBaseRegister();
122 auto IsBaseRegisterClobbered
= [&]() {
123 for (MachineBasicBlock
&MBB
: MF
) {
124 for (MachineInstr
&MI
: MBB
) {
125 if (!MI
.isInlineAsm())
127 for (MachineOperand
&MO
: MI
.operands()) {
130 Register Reg
= MO
.getReg();
131 if (!Reg
.isPhysical())
133 if (TRI
->isSuperOrSubRegisterEq(BasePtr
, Reg
))
140 if (!IsBaseRegisterClobbered())
143 Register ArgBaseReg
= getArgBaseReg(MF
);
144 if (!ArgBaseReg
.isValid())
146 // leal 4(%esp), %reg
147 MachineBasicBlock
&MBB
= MF
.front();
148 MachineBasicBlock::iterator MBBI
= MBB
.begin();
150 // Emit instruction to copy get stack pointer to a virtual register
151 // and save the instruction to x86 machine functon info. We can get
152 // physical register of ArgBaseReg after register allocation. The
153 // stack slot is used to save/restore argument base pointer. We can
154 // get the index from the instruction.
155 unsigned SlotSize
= TRI
->getSlotSize();
156 int FI
= MFI
.CreateSpillStackObject(SlotSize
, Align(SlotSize
));
157 // Use pseudo LEA to prevent the instruction from being eliminated.
158 // TODO: if it is duplicated we can expand it to lea.
160 BuildMI(MBB
, MBBI
, DL
,
161 TII
->get(STI
.is64Bit() ? X86::PLEA64r
: X86::PLEA32r
), ArgBaseReg
)
164 .addUse(X86::NoRegister
)
166 .addUse(X86::NoRegister
)
167 .setMIFlag(MachineInstr::FrameSetup
);
168 X86FI
->setStackPtrSaveMI(LEA
);
170 for (MachineBasicBlock
&MBB
: MF
) {
171 for (MachineInstr
&MI
: MBB
) {
173 for (MachineOperand
&MO
: MI
.operands()) {
175 int Idx
= MO
.getIndex();
176 if (!MFI
.isFixedObjectIndex(Idx
))
178 int64_t Offset
= MFI
.getObjectOffset(Idx
);
181 // TODO replace register for debug instruction
182 if (MI
.isDebugInstr())
184 // Replace frame register with argument base pointer and its offset.
185 TRI
->eliminateFrameIndex(MI
.getIterator(), I
, ArgBaseReg
, Offset
);