1 //===- llvm/lib/Target/X86/X86CallLowering.cpp - Call lowering ------------===//
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 //===----------------------------------------------------------------------===//
10 /// This file implements the lowering of LLVM calls to machine code calls for
13 //===----------------------------------------------------------------------===//
15 #include "X86CallLowering.h"
16 #include "X86CallingConv.h"
17 #include "X86ISelLowering.h"
18 #include "X86InstrInfo.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/CodeGen/Analysis.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
26 #include "llvm/CodeGen/GlobalISel/Utils.h"
27 #include "llvm/CodeGen/LowLevelType.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/MachineOperand.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/TargetInstrInfo.h"
36 #include "llvm/CodeGen/TargetSubtargetInfo.h"
37 #include "llvm/CodeGen/ValueTypes.h"
38 #include "llvm/IR/Attributes.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/Function.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/MC/MCRegisterInfo.h"
43 #include "llvm/Support/LowLevelTypeImpl.h"
44 #include "llvm/Support/MachineValueType.h"
50 X86CallLowering::X86CallLowering(const X86TargetLowering
&TLI
)
51 : CallLowering(&TLI
) {}
53 bool X86CallLowering::splitToValueTypes(const ArgInfo
&OrigArg
,
54 SmallVectorImpl
<ArgInfo
> &SplitArgs
,
56 MachineRegisterInfo
&MRI
,
57 SplitArgTy PerformArgSplit
) const {
58 const X86TargetLowering
&TLI
= *getTLI
<X86TargetLowering
>();
59 LLVMContext
&Context
= OrigArg
.Ty
->getContext();
61 SmallVector
<EVT
, 4> SplitVTs
;
62 SmallVector
<uint64_t, 4> Offsets
;
63 ComputeValueVTs(TLI
, DL
, OrigArg
.Ty
, SplitVTs
, &Offsets
, 0);
65 if (OrigArg
.Ty
->isVoidTy())
69 unsigned NumParts
= TLI
.getNumRegisters(Context
, VT
);
72 // replace the original type ( pointer -> GPR ).
73 SplitArgs
.emplace_back(OrigArg
.Reg
, VT
.getTypeForEVT(Context
),
74 OrigArg
.Flags
, OrigArg
.IsFixed
);
78 SmallVector
<unsigned, 8> SplitRegs
;
80 EVT PartVT
= TLI
.getRegisterType(Context
, VT
);
81 Type
*PartTy
= PartVT
.getTypeForEVT(Context
);
83 for (unsigned i
= 0; i
< NumParts
; ++i
) {
85 ArgInfo
{MRI
.createGenericVirtualRegister(getLLTForType(*PartTy
, DL
)),
86 PartTy
, OrigArg
.Flags
};
87 SplitArgs
.push_back(Info
);
88 SplitRegs
.push_back(Info
.Reg
);
91 PerformArgSplit(SplitRegs
);
97 struct OutgoingValueHandler
: public CallLowering::ValueHandler
{
98 OutgoingValueHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
99 MachineInstrBuilder
&MIB
, CCAssignFn
*AssignFn
)
100 : ValueHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
),
101 DL(MIRBuilder
.getMF().getDataLayout()),
102 STI(MIRBuilder
.getMF().getSubtarget
<X86Subtarget
>()) {}
104 unsigned getStackAddress(uint64_t Size
, int64_t Offset
,
105 MachinePointerInfo
&MPO
) override
{
106 LLT p0
= LLT::pointer(0, DL
.getPointerSizeInBits(0));
107 LLT SType
= LLT::scalar(DL
.getPointerSizeInBits(0));
108 unsigned SPReg
= MRI
.createGenericVirtualRegister(p0
);
109 MIRBuilder
.buildCopy(SPReg
, STI
.getRegisterInfo()->getStackRegister());
111 unsigned OffsetReg
= MRI
.createGenericVirtualRegister(SType
);
112 MIRBuilder
.buildConstant(OffsetReg
, Offset
);
114 unsigned AddrReg
= MRI
.createGenericVirtualRegister(p0
);
115 MIRBuilder
.buildGEP(AddrReg
, SPReg
, OffsetReg
);
117 MPO
= MachinePointerInfo::getStack(MIRBuilder
.getMF(), Offset
);
121 void assignValueToReg(unsigned ValVReg
, unsigned PhysReg
,
122 CCValAssign
&VA
) override
{
123 MIB
.addUse(PhysReg
, RegState::Implicit
);
126 // If we are copying the value to a physical register with the
127 // size larger than the size of the value itself - build AnyExt
128 // to the size of the register first and only then do the copy.
129 // The example of that would be copying from s32 to xmm0, for which
130 // case ValVT == LocVT == MVT::f32. If LocSize and ValSize are not equal
131 // we expect normal extendRegister mechanism to work.
132 unsigned PhysRegSize
=
133 MRI
.getTargetRegisterInfo()->getRegSizeInBits(PhysReg
, MRI
);
134 unsigned ValSize
= VA
.getValVT().getSizeInBits();
135 unsigned LocSize
= VA
.getLocVT().getSizeInBits();
136 if (PhysRegSize
> ValSize
&& LocSize
== ValSize
) {
137 assert((PhysRegSize
== 128 || PhysRegSize
== 80) && "We expect that to be 128 bit");
138 auto MIB
= MIRBuilder
.buildAnyExt(LLT::scalar(PhysRegSize
), ValVReg
);
139 ExtReg
= MIB
->getOperand(0).getReg();
141 ExtReg
= extendRegister(ValVReg
, VA
);
143 MIRBuilder
.buildCopy(PhysReg
, ExtReg
);
146 void assignValueToAddress(unsigned ValVReg
, unsigned Addr
, uint64_t Size
,
147 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
148 unsigned ExtReg
= extendRegister(ValVReg
, VA
);
149 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
150 MPO
, MachineMemOperand::MOStore
, VA
.getLocVT().getStoreSize(),
152 MIRBuilder
.buildStore(ExtReg
, Addr
, *MMO
);
155 bool assignArg(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
156 CCValAssign::LocInfo LocInfo
,
157 const CallLowering::ArgInfo
&Info
, CCState
&State
) override
{
158 bool Res
= AssignFn(ValNo
, ValVT
, LocVT
, LocInfo
, Info
.Flags
, State
);
159 StackSize
= State
.getNextStackOffset();
161 static const MCPhysReg XMMArgRegs
[] = {X86::XMM0
, X86::XMM1
, X86::XMM2
,
162 X86::XMM3
, X86::XMM4
, X86::XMM5
,
163 X86::XMM6
, X86::XMM7
};
165 NumXMMRegs
= State
.getFirstUnallocated(XMMArgRegs
);
170 uint64_t getStackSize() { return StackSize
; }
171 uint64_t getNumXmmRegs() { return NumXMMRegs
; }
174 MachineInstrBuilder
&MIB
;
175 uint64_t StackSize
= 0;
176 const DataLayout
&DL
;
177 const X86Subtarget
&STI
;
178 unsigned NumXMMRegs
= 0;
181 } // end anonymous namespace
183 bool X86CallLowering::lowerReturn(
184 MachineIRBuilder
&MIRBuilder
, const Value
*Val
,
185 ArrayRef
<unsigned> VRegs
) const {
186 assert(((Val
&& !VRegs
.empty()) || (!Val
&& VRegs
.empty())) &&
187 "Return value without a vreg");
188 auto MIB
= MIRBuilder
.buildInstrNoInsert(X86::RET
).addImm(0);
190 if (!VRegs
.empty()) {
191 MachineFunction
&MF
= MIRBuilder
.getMF();
192 const Function
&F
= MF
.getFunction();
193 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
194 auto &DL
= MF
.getDataLayout();
195 LLVMContext
&Ctx
= Val
->getType()->getContext();
196 const X86TargetLowering
&TLI
= *getTLI
<X86TargetLowering
>();
198 SmallVector
<EVT
, 4> SplitEVTs
;
199 ComputeValueVTs(TLI
, DL
, Val
->getType(), SplitEVTs
);
200 assert(VRegs
.size() == SplitEVTs
.size() &&
201 "For each split Type there should be exactly one VReg.");
203 SmallVector
<ArgInfo
, 8> SplitArgs
;
204 for (unsigned i
= 0; i
< SplitEVTs
.size(); ++i
) {
205 ArgInfo CurArgInfo
= ArgInfo
{VRegs
[i
], SplitEVTs
[i
].getTypeForEVT(Ctx
)};
206 setArgFlags(CurArgInfo
, AttributeList::ReturnIndex
, DL
, F
);
207 if (!splitToValueTypes(CurArgInfo
, SplitArgs
, DL
, MRI
,
208 [&](ArrayRef
<unsigned> Regs
) {
209 MIRBuilder
.buildUnmerge(Regs
, VRegs
[i
]);
214 OutgoingValueHandler
Handler(MIRBuilder
, MRI
, MIB
, RetCC_X86
);
215 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
219 MIRBuilder
.insertInstr(MIB
);
225 struct IncomingValueHandler
: public CallLowering::ValueHandler
{
226 IncomingValueHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
227 CCAssignFn
*AssignFn
)
228 : ValueHandler(MIRBuilder
, MRI
, AssignFn
),
229 DL(MIRBuilder
.getMF().getDataLayout()) {}
231 unsigned getStackAddress(uint64_t Size
, int64_t Offset
,
232 MachinePointerInfo
&MPO
) override
{
233 auto &MFI
= MIRBuilder
.getMF().getFrameInfo();
234 int FI
= MFI
.CreateFixedObject(Size
, Offset
, true);
235 MPO
= MachinePointerInfo::getFixedStack(MIRBuilder
.getMF(), FI
);
237 unsigned AddrReg
= MRI
.createGenericVirtualRegister(
238 LLT::pointer(0, DL
.getPointerSizeInBits(0)));
239 MIRBuilder
.buildFrameIndex(AddrReg
, FI
);
243 void assignValueToAddress(unsigned ValVReg
, unsigned Addr
, uint64_t Size
,
244 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
245 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
246 MPO
, MachineMemOperand::MOLoad
| MachineMemOperand::MOInvariant
, Size
,
248 MIRBuilder
.buildLoad(ValVReg
, Addr
, *MMO
);
251 void assignValueToReg(unsigned ValVReg
, unsigned PhysReg
,
252 CCValAssign
&VA
) override
{
253 markPhysRegUsed(PhysReg
);
255 switch (VA
.getLocInfo()) {
257 // If we are copying the value from a physical register with the
258 // size larger than the size of the value itself - build the copy
259 // of the phys reg first and then build the truncation of that copy.
260 // The example of that would be copying from xmm0 to s32, for which
261 // case ValVT == LocVT == MVT::f32. If LocSize and ValSize are not equal
262 // we expect this to be handled in SExt/ZExt/AExt case.
263 unsigned PhysRegSize
=
264 MRI
.getTargetRegisterInfo()->getRegSizeInBits(PhysReg
, MRI
);
265 unsigned ValSize
= VA
.getValVT().getSizeInBits();
266 unsigned LocSize
= VA
.getLocVT().getSizeInBits();
267 if (PhysRegSize
> ValSize
&& LocSize
== ValSize
) {
268 auto Copy
= MIRBuilder
.buildCopy(LLT::scalar(PhysRegSize
), PhysReg
);
269 MIRBuilder
.buildTrunc(ValVReg
, Copy
);
273 MIRBuilder
.buildCopy(ValVReg
, PhysReg
);
276 case CCValAssign::LocInfo::SExt
:
277 case CCValAssign::LocInfo::ZExt
:
278 case CCValAssign::LocInfo::AExt
: {
279 auto Copy
= MIRBuilder
.buildCopy(LLT
{VA
.getLocVT()}, PhysReg
);
280 MIRBuilder
.buildTrunc(ValVReg
, Copy
);
286 /// How the physical register gets marked varies between formal
287 /// parameters (it's a basic-block live-in), and a call instruction
288 /// (it's an implicit-def of the BL).
289 virtual void markPhysRegUsed(unsigned PhysReg
) = 0;
292 const DataLayout
&DL
;
295 struct FormalArgHandler
: public IncomingValueHandler
{
296 FormalArgHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
297 CCAssignFn
*AssignFn
)
298 : IncomingValueHandler(MIRBuilder
, MRI
, AssignFn
) {}
300 void markPhysRegUsed(unsigned PhysReg
) override
{
301 MIRBuilder
.getMBB().addLiveIn(PhysReg
);
305 struct CallReturnHandler
: public IncomingValueHandler
{
306 CallReturnHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
307 CCAssignFn
*AssignFn
, MachineInstrBuilder
&MIB
)
308 : IncomingValueHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
) {}
310 void markPhysRegUsed(unsigned PhysReg
) override
{
311 MIB
.addDef(PhysReg
, RegState::Implicit
);
315 MachineInstrBuilder
&MIB
;
318 } // end anonymous namespace
320 bool X86CallLowering::lowerFormalArguments(MachineIRBuilder
&MIRBuilder
,
322 ArrayRef
<unsigned> VRegs
) const {
326 // TODO: handle variadic function
330 MachineFunction
&MF
= MIRBuilder
.getMF();
331 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
332 auto DL
= MF
.getDataLayout();
334 SmallVector
<ArgInfo
, 8> SplitArgs
;
336 for (auto &Arg
: F
.args()) {
338 // TODO: handle not simple cases.
339 if (Arg
.hasAttribute(Attribute::ByVal
) ||
340 Arg
.hasAttribute(Attribute::InReg
) ||
341 Arg
.hasAttribute(Attribute::StructRet
) ||
342 Arg
.hasAttribute(Attribute::SwiftSelf
) ||
343 Arg
.hasAttribute(Attribute::SwiftError
) ||
344 Arg
.hasAttribute(Attribute::Nest
))
347 ArgInfo
OrigArg(VRegs
[Idx
], Arg
.getType());
348 setArgFlags(OrigArg
, Idx
+ AttributeList::FirstArgIndex
, DL
, F
);
349 if (!splitToValueTypes(OrigArg
, SplitArgs
, DL
, MRI
,
350 [&](ArrayRef
<unsigned> Regs
) {
351 MIRBuilder
.buildMerge(VRegs
[Idx
], Regs
);
357 MachineBasicBlock
&MBB
= MIRBuilder
.getMBB();
359 MIRBuilder
.setInstr(*MBB
.begin());
361 FormalArgHandler
Handler(MIRBuilder
, MRI
, CC_X86
);
362 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
365 // Move back to the end of the basic block.
366 MIRBuilder
.setMBB(MBB
);
371 bool X86CallLowering::lowerCall(MachineIRBuilder
&MIRBuilder
,
372 CallingConv::ID CallConv
,
373 const MachineOperand
&Callee
,
374 const ArgInfo
&OrigRet
,
375 ArrayRef
<ArgInfo
> OrigArgs
) const {
376 MachineFunction
&MF
= MIRBuilder
.getMF();
377 const Function
&F
= MF
.getFunction();
378 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
379 auto &DL
= F
.getParent()->getDataLayout();
380 const X86Subtarget
&STI
= MF
.getSubtarget
<X86Subtarget
>();
381 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
382 auto TRI
= STI
.getRegisterInfo();
384 // Handle only Linux C, X86_64_SysV calling conventions for now.
385 if (!STI
.isTargetLinux() ||
386 !(CallConv
== CallingConv::C
|| CallConv
== CallingConv::X86_64_SysV
))
389 unsigned AdjStackDown
= TII
.getCallFrameSetupOpcode();
390 auto CallSeqStart
= MIRBuilder
.buildInstr(AdjStackDown
);
392 // Create a temporarily-floating call instruction so we can add the implicit
393 // uses of arg registers.
394 bool Is64Bit
= STI
.is64Bit();
395 unsigned CallOpc
= Callee
.isReg()
396 ? (Is64Bit
? X86::CALL64r
: X86::CALL32r
)
397 : (Is64Bit
? X86::CALL64pcrel32
: X86::CALLpcrel32
);
399 auto MIB
= MIRBuilder
.buildInstrNoInsert(CallOpc
).add(Callee
).addRegMask(
400 TRI
->getCallPreservedMask(MF
, CallConv
));
402 SmallVector
<ArgInfo
, 8> SplitArgs
;
403 for (const auto &OrigArg
: OrigArgs
) {
405 // TODO: handle not simple cases.
406 if (OrigArg
.Flags
.isByVal())
409 if (!splitToValueTypes(OrigArg
, SplitArgs
, DL
, MRI
,
410 [&](ArrayRef
<unsigned> Regs
) {
411 MIRBuilder
.buildUnmerge(Regs
, OrigArg
.Reg
);
415 // Do the actual argument marshalling.
416 OutgoingValueHandler
Handler(MIRBuilder
, MRI
, MIB
, CC_X86
);
417 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
420 bool IsFixed
= OrigArgs
.empty() ? true : OrigArgs
.back().IsFixed
;
421 if (STI
.is64Bit() && !IsFixed
&& !STI
.isCallingConvWin64(CallConv
)) {
422 // From AMD64 ABI document:
423 // For calls that may call functions that use varargs or stdargs
424 // (prototype-less calls or calls to functions containing ellipsis (...) in
425 // the declaration) %al is used as hidden argument to specify the number
426 // of SSE registers used. The contents of %al do not need to match exactly
427 // the number of registers, but must be an ubound on the number of SSE
428 // registers used and is in the range 0 - 8 inclusive.
430 MIRBuilder
.buildInstr(X86::MOV8ri
)
432 .addImm(Handler
.getNumXmmRegs());
433 MIB
.addUse(X86::AL
, RegState::Implicit
);
436 // Now we can add the actual call instruction to the correct basic block.
437 MIRBuilder
.insertInstr(MIB
);
439 // If Callee is a reg, since it is used by a target specific
440 // instruction, it must have a register class matching the
441 // constraint of that instruction.
443 MIB
->getOperand(0).setReg(constrainOperandRegClass(
444 MF
, *TRI
, MRI
, *MF
.getSubtarget().getInstrInfo(),
445 *MF
.getSubtarget().getRegBankInfo(), *MIB
, MIB
->getDesc(), Callee
, 0));
447 // Finally we can copy the returned value back into its virtual-register. In
448 // symmetry with the arguments, the physical register must be an
449 // implicit-define of the call instruction.
453 SmallVector
<unsigned, 8> NewRegs
;
455 if (!splitToValueTypes(OrigRet
, SplitArgs
, DL
, MRI
,
456 [&](ArrayRef
<unsigned> Regs
) {
457 NewRegs
.assign(Regs
.begin(), Regs
.end());
461 CallReturnHandler
Handler(MIRBuilder
, MRI
, RetCC_X86
, MIB
);
462 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
465 if (!NewRegs
.empty())
466 MIRBuilder
.buildMerge(OrigRet
.Reg
, NewRegs
);
469 CallSeqStart
.addImm(Handler
.getStackSize())
470 .addImm(0 /* see getFrameTotalSize */)
471 .addImm(0 /* see getFrameAdjustment */);
473 unsigned AdjStackUp
= TII
.getCallFrameDestroyOpcode();
474 MIRBuilder
.buildInstr(AdjStackUp
)
475 .addImm(Handler
.getStackSize())
476 .addImm(0 /* NumBytesForCalleeToPop */);