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);
64 assert(OrigArg
.Regs
.size() == 1 && "Can't handle multple regs yet");
66 if (OrigArg
.Ty
->isVoidTy())
70 unsigned NumParts
= TLI
.getNumRegisters(Context
, VT
);
73 // replace the original type ( pointer -> GPR ).
74 SplitArgs
.emplace_back(OrigArg
.Regs
[0], VT
.getTypeForEVT(Context
),
75 OrigArg
.Flags
, OrigArg
.IsFixed
);
79 SmallVector
<Register
, 8> SplitRegs
;
81 EVT PartVT
= TLI
.getRegisterType(Context
, VT
);
82 Type
*PartTy
= PartVT
.getTypeForEVT(Context
);
84 for (unsigned i
= 0; i
< NumParts
; ++i
) {
86 ArgInfo
{MRI
.createGenericVirtualRegister(getLLTForType(*PartTy
, DL
)),
87 PartTy
, OrigArg
.Flags
};
88 SplitArgs
.push_back(Info
);
89 SplitRegs
.push_back(Info
.Regs
[0]);
92 PerformArgSplit(SplitRegs
);
98 struct OutgoingValueHandler
: public CallLowering::ValueHandler
{
99 OutgoingValueHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
100 MachineInstrBuilder
&MIB
, CCAssignFn
*AssignFn
)
101 : ValueHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
),
102 DL(MIRBuilder
.getMF().getDataLayout()),
103 STI(MIRBuilder
.getMF().getSubtarget
<X86Subtarget
>()) {}
105 Register
getStackAddress(uint64_t Size
, int64_t Offset
,
106 MachinePointerInfo
&MPO
) override
{
107 LLT p0
= LLT::pointer(0, DL
.getPointerSizeInBits(0));
108 LLT SType
= LLT::scalar(DL
.getPointerSizeInBits(0));
109 Register SPReg
= MRI
.createGenericVirtualRegister(p0
);
110 MIRBuilder
.buildCopy(SPReg
, STI
.getRegisterInfo()->getStackRegister());
112 Register OffsetReg
= MRI
.createGenericVirtualRegister(SType
);
113 MIRBuilder
.buildConstant(OffsetReg
, Offset
);
115 Register AddrReg
= MRI
.createGenericVirtualRegister(p0
);
116 MIRBuilder
.buildGEP(AddrReg
, SPReg
, OffsetReg
);
118 MPO
= MachinePointerInfo::getStack(MIRBuilder
.getMF(), Offset
);
122 void assignValueToReg(Register ValVReg
, Register PhysReg
,
123 CCValAssign
&VA
) override
{
124 MIB
.addUse(PhysReg
, RegState::Implicit
);
127 // If we are copying the value to a physical register with the
128 // size larger than the size of the value itself - build AnyExt
129 // to the size of the register first and only then do the copy.
130 // The example of that would be copying from s32 to xmm0, for which
131 // case ValVT == LocVT == MVT::f32. If LocSize and ValSize are not equal
132 // we expect normal extendRegister mechanism to work.
133 unsigned PhysRegSize
=
134 MRI
.getTargetRegisterInfo()->getRegSizeInBits(PhysReg
, MRI
);
135 unsigned ValSize
= VA
.getValVT().getSizeInBits();
136 unsigned LocSize
= VA
.getLocVT().getSizeInBits();
137 if (PhysRegSize
> ValSize
&& LocSize
== ValSize
) {
138 assert((PhysRegSize
== 128 || PhysRegSize
== 80) && "We expect that to be 128 bit");
139 auto MIB
= MIRBuilder
.buildAnyExt(LLT::scalar(PhysRegSize
), ValVReg
);
140 ExtReg
= MIB
->getOperand(0).getReg();
142 ExtReg
= extendRegister(ValVReg
, VA
);
144 MIRBuilder
.buildCopy(PhysReg
, ExtReg
);
147 void assignValueToAddress(Register ValVReg
, Register Addr
, uint64_t Size
,
148 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
149 Register ExtReg
= extendRegister(ValVReg
, VA
);
150 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
151 MPO
, MachineMemOperand::MOStore
, VA
.getLocVT().getStoreSize(),
153 MIRBuilder
.buildStore(ExtReg
, Addr
, *MMO
);
156 bool assignArg(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
157 CCValAssign::LocInfo LocInfo
,
158 const CallLowering::ArgInfo
&Info
, CCState
&State
) override
{
159 bool Res
= AssignFn(ValNo
, ValVT
, LocVT
, LocInfo
, Info
.Flags
, State
);
160 StackSize
= State
.getNextStackOffset();
162 static const MCPhysReg XMMArgRegs
[] = {X86::XMM0
, X86::XMM1
, X86::XMM2
,
163 X86::XMM3
, X86::XMM4
, X86::XMM5
,
164 X86::XMM6
, X86::XMM7
};
166 NumXMMRegs
= State
.getFirstUnallocated(XMMArgRegs
);
171 uint64_t getStackSize() { return StackSize
; }
172 uint64_t getNumXmmRegs() { return NumXMMRegs
; }
175 MachineInstrBuilder
&MIB
;
176 uint64_t StackSize
= 0;
177 const DataLayout
&DL
;
178 const X86Subtarget
&STI
;
179 unsigned NumXMMRegs
= 0;
182 } // end anonymous namespace
184 bool X86CallLowering::lowerReturn(
185 MachineIRBuilder
&MIRBuilder
, const Value
*Val
,
186 ArrayRef
<Register
> VRegs
) const {
187 assert(((Val
&& !VRegs
.empty()) || (!Val
&& VRegs
.empty())) &&
188 "Return value without a vreg");
189 auto MIB
= MIRBuilder
.buildInstrNoInsert(X86::RET
).addImm(0);
191 if (!VRegs
.empty()) {
192 MachineFunction
&MF
= MIRBuilder
.getMF();
193 const Function
&F
= MF
.getFunction();
194 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
195 auto &DL
= MF
.getDataLayout();
196 LLVMContext
&Ctx
= Val
->getType()->getContext();
197 const X86TargetLowering
&TLI
= *getTLI
<X86TargetLowering
>();
199 SmallVector
<EVT
, 4> SplitEVTs
;
200 ComputeValueVTs(TLI
, DL
, Val
->getType(), SplitEVTs
);
201 assert(VRegs
.size() == SplitEVTs
.size() &&
202 "For each split Type there should be exactly one VReg.");
204 SmallVector
<ArgInfo
, 8> SplitArgs
;
205 for (unsigned i
= 0; i
< SplitEVTs
.size(); ++i
) {
206 ArgInfo CurArgInfo
= ArgInfo
{VRegs
[i
], SplitEVTs
[i
].getTypeForEVT(Ctx
)};
207 setArgFlags(CurArgInfo
, AttributeList::ReturnIndex
, DL
, F
);
208 if (!splitToValueTypes(CurArgInfo
, SplitArgs
, DL
, MRI
,
209 [&](ArrayRef
<Register
> Regs
) {
210 MIRBuilder
.buildUnmerge(Regs
, VRegs
[i
]);
215 OutgoingValueHandler
Handler(MIRBuilder
, MRI
, MIB
, RetCC_X86
);
216 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
220 MIRBuilder
.insertInstr(MIB
);
226 struct IncomingValueHandler
: public CallLowering::ValueHandler
{
227 IncomingValueHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
228 CCAssignFn
*AssignFn
)
229 : ValueHandler(MIRBuilder
, MRI
, AssignFn
),
230 DL(MIRBuilder
.getMF().getDataLayout()) {}
232 bool isIncomingArgumentHandler() const override
{ return true; }
234 Register
getStackAddress(uint64_t Size
, int64_t Offset
,
235 MachinePointerInfo
&MPO
) override
{
236 auto &MFI
= MIRBuilder
.getMF().getFrameInfo();
237 int FI
= MFI
.CreateFixedObject(Size
, Offset
, true);
238 MPO
= MachinePointerInfo::getFixedStack(MIRBuilder
.getMF(), FI
);
240 Register AddrReg
= MRI
.createGenericVirtualRegister(
241 LLT::pointer(0, DL
.getPointerSizeInBits(0)));
242 MIRBuilder
.buildFrameIndex(AddrReg
, FI
);
246 void assignValueToAddress(Register ValVReg
, Register Addr
, uint64_t Size
,
247 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
248 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
249 MPO
, MachineMemOperand::MOLoad
| MachineMemOperand::MOInvariant
, Size
,
251 MIRBuilder
.buildLoad(ValVReg
, Addr
, *MMO
);
254 void assignValueToReg(Register ValVReg
, Register PhysReg
,
255 CCValAssign
&VA
) override
{
256 markPhysRegUsed(PhysReg
);
258 switch (VA
.getLocInfo()) {
260 // If we are copying the value from a physical register with the
261 // size larger than the size of the value itself - build the copy
262 // of the phys reg first and then build the truncation of that copy.
263 // The example of that would be copying from xmm0 to s32, for which
264 // case ValVT == LocVT == MVT::f32. If LocSize and ValSize are not equal
265 // we expect this to be handled in SExt/ZExt/AExt case.
266 unsigned PhysRegSize
=
267 MRI
.getTargetRegisterInfo()->getRegSizeInBits(PhysReg
, MRI
);
268 unsigned ValSize
= VA
.getValVT().getSizeInBits();
269 unsigned LocSize
= VA
.getLocVT().getSizeInBits();
270 if (PhysRegSize
> ValSize
&& LocSize
== ValSize
) {
271 auto Copy
= MIRBuilder
.buildCopy(LLT::scalar(PhysRegSize
), PhysReg
);
272 MIRBuilder
.buildTrunc(ValVReg
, Copy
);
276 MIRBuilder
.buildCopy(ValVReg
, PhysReg
);
279 case CCValAssign::LocInfo::SExt
:
280 case CCValAssign::LocInfo::ZExt
:
281 case CCValAssign::LocInfo::AExt
: {
282 auto Copy
= MIRBuilder
.buildCopy(LLT
{VA
.getLocVT()}, PhysReg
);
283 MIRBuilder
.buildTrunc(ValVReg
, Copy
);
289 /// How the physical register gets marked varies between formal
290 /// parameters (it's a basic-block live-in), and a call instruction
291 /// (it's an implicit-def of the BL).
292 virtual void markPhysRegUsed(unsigned PhysReg
) = 0;
295 const DataLayout
&DL
;
298 struct FormalArgHandler
: public IncomingValueHandler
{
299 FormalArgHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
300 CCAssignFn
*AssignFn
)
301 : IncomingValueHandler(MIRBuilder
, MRI
, AssignFn
) {}
303 void markPhysRegUsed(unsigned PhysReg
) override
{
304 MIRBuilder
.getMRI()->addLiveIn(PhysReg
);
305 MIRBuilder
.getMBB().addLiveIn(PhysReg
);
309 struct CallReturnHandler
: public IncomingValueHandler
{
310 CallReturnHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
311 CCAssignFn
*AssignFn
, MachineInstrBuilder
&MIB
)
312 : IncomingValueHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
) {}
314 void markPhysRegUsed(unsigned PhysReg
) override
{
315 MIB
.addDef(PhysReg
, RegState::Implicit
);
319 MachineInstrBuilder
&MIB
;
322 } // end anonymous namespace
324 bool X86CallLowering::lowerFormalArguments(
325 MachineIRBuilder
&MIRBuilder
, const Function
&F
,
326 ArrayRef
<ArrayRef
<Register
>> VRegs
) const {
330 // TODO: handle variadic function
334 MachineFunction
&MF
= MIRBuilder
.getMF();
335 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
336 auto DL
= MF
.getDataLayout();
338 SmallVector
<ArgInfo
, 8> SplitArgs
;
340 for (auto &Arg
: F
.args()) {
342 // TODO: handle not simple cases.
343 if (Arg
.hasAttribute(Attribute::ByVal
) ||
344 Arg
.hasAttribute(Attribute::InReg
) ||
345 Arg
.hasAttribute(Attribute::StructRet
) ||
346 Arg
.hasAttribute(Attribute::SwiftSelf
) ||
347 Arg
.hasAttribute(Attribute::SwiftError
) ||
348 Arg
.hasAttribute(Attribute::Nest
) || VRegs
[Idx
].size() > 1)
351 ArgInfo
OrigArg(VRegs
[Idx
], Arg
.getType());
352 setArgFlags(OrigArg
, Idx
+ AttributeList::FirstArgIndex
, DL
, F
);
353 if (!splitToValueTypes(OrigArg
, SplitArgs
, DL
, MRI
,
354 [&](ArrayRef
<Register
> Regs
) {
355 MIRBuilder
.buildMerge(VRegs
[Idx
][0], Regs
);
361 MachineBasicBlock
&MBB
= MIRBuilder
.getMBB();
363 MIRBuilder
.setInstr(*MBB
.begin());
365 FormalArgHandler
Handler(MIRBuilder
, MRI
, CC_X86
);
366 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
369 // Move back to the end of the basic block.
370 MIRBuilder
.setMBB(MBB
);
375 bool X86CallLowering::lowerCall(MachineIRBuilder
&MIRBuilder
,
376 CallLoweringInfo
&Info
) const {
377 MachineFunction
&MF
= MIRBuilder
.getMF();
378 const Function
&F
= MF
.getFunction();
379 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
380 auto &DL
= F
.getParent()->getDataLayout();
381 const X86Subtarget
&STI
= MF
.getSubtarget
<X86Subtarget
>();
382 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
383 auto TRI
= STI
.getRegisterInfo();
385 // Handle only Linux C, X86_64_SysV calling conventions for now.
386 if (!STI
.isTargetLinux() || !(Info
.CallConv
== CallingConv::C
||
387 Info
.CallConv
== CallingConv::X86_64_SysV
))
390 unsigned AdjStackDown
= TII
.getCallFrameSetupOpcode();
391 auto CallSeqStart
= MIRBuilder
.buildInstr(AdjStackDown
);
393 // Create a temporarily-floating call instruction so we can add the implicit
394 // uses of arg registers.
395 bool Is64Bit
= STI
.is64Bit();
396 unsigned CallOpc
= Info
.Callee
.isReg()
397 ? (Is64Bit
? X86::CALL64r
: X86::CALL32r
)
398 : (Is64Bit
? X86::CALL64pcrel32
: X86::CALLpcrel32
);
400 auto MIB
= MIRBuilder
.buildInstrNoInsert(CallOpc
)
402 .addRegMask(TRI
->getCallPreservedMask(MF
, Info
.CallConv
));
404 SmallVector
<ArgInfo
, 8> SplitArgs
;
405 for (const auto &OrigArg
: Info
.OrigArgs
) {
407 // TODO: handle not simple cases.
408 if (OrigArg
.Flags
.isByVal())
411 if (OrigArg
.Regs
.size() > 1)
414 if (!splitToValueTypes(OrigArg
, SplitArgs
, DL
, MRI
,
415 [&](ArrayRef
<Register
> Regs
) {
416 MIRBuilder
.buildUnmerge(Regs
, OrigArg
.Regs
[0]);
420 // Do the actual argument marshalling.
421 OutgoingValueHandler
Handler(MIRBuilder
, MRI
, MIB
, CC_X86
);
422 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
425 bool IsFixed
= Info
.OrigArgs
.empty() ? true : Info
.OrigArgs
.back().IsFixed
;
426 if (STI
.is64Bit() && !IsFixed
&& !STI
.isCallingConvWin64(Info
.CallConv
)) {
427 // From AMD64 ABI document:
428 // For calls that may call functions that use varargs or stdargs
429 // (prototype-less calls or calls to functions containing ellipsis (...) in
430 // the declaration) %al is used as hidden argument to specify the number
431 // of SSE registers used. The contents of %al do not need to match exactly
432 // the number of registers, but must be an ubound on the number of SSE
433 // registers used and is in the range 0 - 8 inclusive.
435 MIRBuilder
.buildInstr(X86::MOV8ri
)
437 .addImm(Handler
.getNumXmmRegs());
438 MIB
.addUse(X86::AL
, RegState::Implicit
);
441 // Now we can add the actual call instruction to the correct basic block.
442 MIRBuilder
.insertInstr(MIB
);
444 // If Callee is a reg, since it is used by a target specific
445 // instruction, it must have a register class matching the
446 // constraint of that instruction.
447 if (Info
.Callee
.isReg())
448 MIB
->getOperand(0).setReg(constrainOperandRegClass(
449 MF
, *TRI
, MRI
, *MF
.getSubtarget().getInstrInfo(),
450 *MF
.getSubtarget().getRegBankInfo(), *MIB
, MIB
->getDesc(), Info
.Callee
,
453 // Finally we can copy the returned value back into its virtual-register. In
454 // symmetry with the arguments, the physical register must be an
455 // implicit-define of the call instruction.
457 if (!Info
.OrigRet
.Ty
->isVoidTy()) {
458 if (Info
.OrigRet
.Regs
.size() > 1)
462 SmallVector
<Register
, 8> NewRegs
;
464 if (!splitToValueTypes(Info
.OrigRet
, SplitArgs
, DL
, MRI
,
465 [&](ArrayRef
<Register
> Regs
) {
466 NewRegs
.assign(Regs
.begin(), Regs
.end());
470 CallReturnHandler
Handler(MIRBuilder
, MRI
, RetCC_X86
, MIB
);
471 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
474 if (!NewRegs
.empty())
475 MIRBuilder
.buildMerge(Info
.OrigRet
.Regs
[0], NewRegs
);
478 CallSeqStart
.addImm(Handler
.getStackSize())
479 .addImm(0 /* see getFrameTotalSize */)
480 .addImm(0 /* see getFrameAdjustment */);
482 unsigned AdjStackUp
= TII
.getCallFrameDestroyOpcode();
483 MIRBuilder
.buildInstr(AdjStackUp
)
484 .addImm(Handler
.getStackSize())
485 .addImm(0 /* NumBytesForCalleeToPop */);