1009b1c42SEd Schouten //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the CCState class, used for lowering and implementing
10009b1c42SEd Schouten // calling conventions.
11009b1c42SEd Schouten //
12009b1c42SEd Schouten //===----------------------------------------------------------------------===//
13009b1c42SEd Schouten
14009b1c42SEd Schouten #include "llvm/CodeGen/CallingConvLower.h"
1556fe8f14SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
16b60736ecSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
17044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
18044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
19044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
20145449b1SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
21829000e0SRoman Divacky #include "llvm/Support/Debug.h"
2259850d08SRoman Divacky #include "llvm/Support/ErrorHandling.h"
2367c32a98SDimitry Andric #include "llvm/Support/SaveAndRestore.h"
2459850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
25b915e9e0SDimitry Andric
26009b1c42SEd Schouten using namespace llvm;
27009b1c42SEd Schouten
CCState(CallingConv::ID CC,bool IsVarArg,MachineFunction & MF,SmallVectorImpl<CCValAssign> & Locs,LLVMContext & Context,bool NegativeOffsets)287fa27ce4SDimitry Andric CCState::CCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
297fa27ce4SDimitry Andric SmallVectorImpl<CCValAssign> &Locs, LLVMContext &Context,
307fa27ce4SDimitry Andric bool NegativeOffsets)
317fa27ce4SDimitry Andric : CallingConv(CC), IsVarArg(IsVarArg), MF(MF),
327fa27ce4SDimitry Andric TRI(*MF.getSubtarget().getRegisterInfo()), Locs(Locs), Context(Context),
337fa27ce4SDimitry Andric NegativeOffsets(NegativeOffsets) {
347fa27ce4SDimitry Andric
35009b1c42SEd Schouten // No stack is used.
367fa27ce4SDimitry Andric StackSize = 0;
37009b1c42SEd Schouten
3859d6cff9SDimitry Andric clearByValRegsInfo();
39009b1c42SEd Schouten UsedRegs.resize((TRI.getNumRegs()+31)/32);
40009b1c42SEd Schouten }
41009b1c42SEd Schouten
423a0822f0SDimitry Andric /// Allocate space on the stack large enough to pass an argument by value.
433a0822f0SDimitry Andric /// The size and alignment information of the argument is encoded in
443a0822f0SDimitry Andric /// its parameter attribute.
HandleByVal(unsigned ValNo,MVT ValVT,MVT LocVT,CCValAssign::LocInfo LocInfo,int MinSize,Align MinAlign,ISD::ArgFlagsTy ArgFlags)451d5ae102SDimitry Andric void CCState::HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT,
461d5ae102SDimitry Andric CCValAssign::LocInfo LocInfo, int MinSize,
47cfca06d7SDimitry Andric Align MinAlign, ISD::ArgFlagsTy ArgFlags) {
48cfca06d7SDimitry Andric Align Alignment = ArgFlags.getNonZeroByValAlign();
49009b1c42SEd Schouten unsigned Size = ArgFlags.getByValSize();
50009b1c42SEd Schouten if (MinSize > (int)Size)
51009b1c42SEd Schouten Size = MinSize;
521d5ae102SDimitry Andric if (MinAlign > Alignment)
531d5ae102SDimitry Andric Alignment = MinAlign;
541d5ae102SDimitry Andric ensureMaxAlignment(Alignment);
55cfca06d7SDimitry Andric MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Alignment);
5601095a5dSDimitry Andric Size = unsigned(alignTo(Size, MinAlign));
577fa27ce4SDimitry Andric uint64_t Offset = AllocateStack(Size, Alignment);
58009b1c42SEd Schouten addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
59009b1c42SEd Schouten }
60009b1c42SEd Schouten
613a0822f0SDimitry Andric /// Mark a register and all of its aliases as allocated.
MarkAllocated(MCPhysReg Reg)62cfca06d7SDimitry Andric void CCState::MarkAllocated(MCPhysReg Reg) {
6358b69754SDimitry Andric for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
6458b69754SDimitry Andric UsedRegs[*AI / 32] |= 1 << (*AI & 31);
65009b1c42SEd Schouten }
66009b1c42SEd Schouten
MarkUnallocated(MCPhysReg Reg)67b60736ecSDimitry Andric void CCState::MarkUnallocated(MCPhysReg Reg) {
68b60736ecSDimitry Andric for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
69b60736ecSDimitry Andric UsedRegs[*AI / 32] &= ~(1 << (*AI & 31));
70b60736ecSDimitry Andric }
71b60736ecSDimitry Andric
IsShadowAllocatedReg(MCRegister Reg) const72cfca06d7SDimitry Andric bool CCState::IsShadowAllocatedReg(MCRegister Reg) const {
73b915e9e0SDimitry Andric if (!isAllocated(Reg))
74b915e9e0SDimitry Andric return false;
75b915e9e0SDimitry Andric
76145449b1SDimitry Andric for (auto const &ValAssign : Locs)
77145449b1SDimitry Andric if (ValAssign.isRegLoc() && TRI.regsOverlap(ValAssign.getLocReg(), Reg))
78b915e9e0SDimitry Andric return false;
79b915e9e0SDimitry Andric return true;
80b915e9e0SDimitry Andric }
81b915e9e0SDimitry Andric
823a0822f0SDimitry Andric /// Analyze an array of argument values,
83009b1c42SEd Schouten /// incorporating info about the formals into this state.
8459850d08SRoman Divacky void
AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> & Ins,CCAssignFn Fn)8559850d08SRoman Divacky CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
8659850d08SRoman Divacky CCAssignFn Fn) {
8759850d08SRoman Divacky unsigned NumArgs = Ins.size();
88009b1c42SEd Schouten
89009b1c42SEd Schouten for (unsigned i = 0; i != NumArgs; ++i) {
90cf099d11SDimitry Andric MVT ArgVT = Ins[i].VT;
9159850d08SRoman Divacky ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
921d5ae102SDimitry Andric if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this))
931d5ae102SDimitry Andric report_fatal_error("unable to allocate function argument #" + Twine(i));
94009b1c42SEd Schouten }
95009b1c42SEd Schouten }
96009b1c42SEd Schouten
973a0822f0SDimitry Andric /// Analyze the return values of a function, returning true if the return can
983a0822f0SDimitry Andric /// be performed without sret-demotion and false otherwise.
CheckReturn(const SmallVectorImpl<ISD::OutputArg> & Outs,CCAssignFn Fn)9966e41e3cSRoman Divacky bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
100907da171SRoman Divacky CCAssignFn Fn) {
101907da171SRoman Divacky // Determine which register each value should be copied into.
10266e41e3cSRoman Divacky for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
103cf099d11SDimitry Andric MVT VT = Outs[i].VT;
10466e41e3cSRoman Divacky ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
105907da171SRoman Divacky if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
106907da171SRoman Divacky return false;
107907da171SRoman Divacky }
108907da171SRoman Divacky return true;
109907da171SRoman Divacky }
110907da171SRoman Divacky
1113a0822f0SDimitry Andric /// Analyze the returned values of a return,
112009b1c42SEd Schouten /// incorporating info about the result values into this state.
AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> & Outs,CCAssignFn Fn)11359850d08SRoman Divacky void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
11459850d08SRoman Divacky CCAssignFn Fn) {
115009b1c42SEd Schouten // Determine which register each value should be copied into.
11659850d08SRoman Divacky for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
117cf099d11SDimitry Andric MVT VT = Outs[i].VT;
11859850d08SRoman Divacky ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1191d5ae102SDimitry Andric if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
1201d5ae102SDimitry Andric report_fatal_error("unable to allocate function return #" + Twine(i));
121009b1c42SEd Schouten }
122009b1c42SEd Schouten }
123009b1c42SEd Schouten
1243a0822f0SDimitry Andric /// Analyze the outgoing arguments to a call,
12559850d08SRoman Divacky /// incorporating info about the passed values into this state.
AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> & Outs,CCAssignFn Fn)12659850d08SRoman Divacky void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
12759850d08SRoman Divacky CCAssignFn Fn) {
12859850d08SRoman Divacky unsigned NumOps = Outs.size();
129009b1c42SEd Schouten for (unsigned i = 0; i != NumOps; ++i) {
130cf099d11SDimitry Andric MVT ArgVT = Outs[i].VT;
13159850d08SRoman Divacky ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
132009b1c42SEd Schouten if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
13359850d08SRoman Divacky #ifndef NDEBUG
134829000e0SRoman Divacky dbgs() << "Call operand #" << i << " has unhandled type "
1357fa27ce4SDimitry Andric << ArgVT << '\n';
13659850d08SRoman Divacky #endif
1375ca98fd9SDimitry Andric llvm_unreachable(nullptr);
138009b1c42SEd Schouten }
139009b1c42SEd Schouten }
140009b1c42SEd Schouten }
141009b1c42SEd Schouten
1423a0822f0SDimitry Andric /// Same as above except it takes vectors of types and argument flags.
AnalyzeCallOperands(SmallVectorImpl<MVT> & ArgVTs,SmallVectorImpl<ISD::ArgFlagsTy> & Flags,CCAssignFn Fn)143cf099d11SDimitry Andric void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
144009b1c42SEd Schouten SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
145009b1c42SEd Schouten CCAssignFn Fn) {
146009b1c42SEd Schouten unsigned NumOps = ArgVTs.size();
147009b1c42SEd Schouten for (unsigned i = 0; i != NumOps; ++i) {
148cf099d11SDimitry Andric MVT ArgVT = ArgVTs[i];
149009b1c42SEd Schouten ISD::ArgFlagsTy ArgFlags = Flags[i];
150009b1c42SEd Schouten if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
15159850d08SRoman Divacky #ifndef NDEBUG
152829000e0SRoman Divacky dbgs() << "Call operand #" << i << " has unhandled type "
1537fa27ce4SDimitry Andric << ArgVT << '\n';
15459850d08SRoman Divacky #endif
1555ca98fd9SDimitry Andric llvm_unreachable(nullptr);
156009b1c42SEd Schouten }
157009b1c42SEd Schouten }
158009b1c42SEd Schouten }
159009b1c42SEd Schouten
1603a0822f0SDimitry Andric /// Analyze the return values of a call, incorporating info about the passed
1613a0822f0SDimitry Andric /// values into this state.
AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> & Ins,CCAssignFn Fn)16259850d08SRoman Divacky void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
16359850d08SRoman Divacky CCAssignFn Fn) {
16459850d08SRoman Divacky for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
165cf099d11SDimitry Andric MVT VT = Ins[i].VT;
16659850d08SRoman Divacky ISD::ArgFlagsTy Flags = Ins[i].Flags;
167009b1c42SEd Schouten if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
16859850d08SRoman Divacky #ifndef NDEBUG
169829000e0SRoman Divacky dbgs() << "Call result #" << i << " has unhandled type "
1707fa27ce4SDimitry Andric << VT << '\n';
17159850d08SRoman Divacky #endif
1725ca98fd9SDimitry Andric llvm_unreachable(nullptr);
173009b1c42SEd Schouten }
174009b1c42SEd Schouten }
175009b1c42SEd Schouten }
176009b1c42SEd Schouten
1773a0822f0SDimitry Andric /// Same as above except it's specialized for calls that produce a single value.
AnalyzeCallResult(MVT VT,CCAssignFn Fn)178cf099d11SDimitry Andric void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
179009b1c42SEd Schouten if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
18059850d08SRoman Divacky #ifndef NDEBUG
181829000e0SRoman Divacky dbgs() << "Call result has unhandled type "
1827fa27ce4SDimitry Andric << VT << '\n';
18359850d08SRoman Divacky #endif
1845ca98fd9SDimitry Andric llvm_unreachable(nullptr);
185009b1c42SEd Schouten }
186009b1c42SEd Schouten }
18767c32a98SDimitry Andric
ensureMaxAlignment(Align Alignment)188b60736ecSDimitry Andric void CCState::ensureMaxAlignment(Align Alignment) {
189b60736ecSDimitry Andric if (!AnalyzingMustTailForwardedRegs)
190b60736ecSDimitry Andric MF.getFrameInfo().ensureMaxAlignment(Alignment);
191b60736ecSDimitry Andric }
192b60736ecSDimitry Andric
isValueTypeInRegForCC(CallingConv::ID CC,MVT VT)19367c32a98SDimitry Andric static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
19467c32a98SDimitry Andric if (VT.isVector())
19567c32a98SDimitry Andric return true; // Assume -msse-regparm might be in effect.
19667c32a98SDimitry Andric if (!VT.isInteger())
19767c32a98SDimitry Andric return false;
198b60736ecSDimitry Andric return (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall);
19967c32a98SDimitry Andric }
20067c32a98SDimitry Andric
getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> & Regs,MVT VT,CCAssignFn Fn)20167c32a98SDimitry Andric void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
20267c32a98SDimitry Andric MVT VT, CCAssignFn Fn) {
2037fa27ce4SDimitry Andric uint64_t SavedStackSize = StackSize;
2041d5ae102SDimitry Andric Align SavedMaxStackArgAlign = MaxStackArgAlign;
20567c32a98SDimitry Andric unsigned NumLocs = Locs.size();
20667c32a98SDimitry Andric
20767c32a98SDimitry Andric // Set the 'inreg' flag if it is used for this calling convention.
20867c32a98SDimitry Andric ISD::ArgFlagsTy Flags;
20967c32a98SDimitry Andric if (isValueTypeInRegForCC(CallingConv, VT))
21067c32a98SDimitry Andric Flags.setInReg();
21167c32a98SDimitry Andric
21267c32a98SDimitry Andric // Allocate something of this value type repeatedly until we get assigned a
21367c32a98SDimitry Andric // location in memory.
214b60736ecSDimitry Andric bool HaveRegParm;
215b60736ecSDimitry Andric do {
21667c32a98SDimitry Andric if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
21767c32a98SDimitry Andric #ifndef NDEBUG
2187fa27ce4SDimitry Andric dbgs() << "Call has unhandled type " << VT
21967c32a98SDimitry Andric << " while computing remaining regparms\n";
22067c32a98SDimitry Andric #endif
22167c32a98SDimitry Andric llvm_unreachable(nullptr);
22267c32a98SDimitry Andric }
22367c32a98SDimitry Andric HaveRegParm = Locs.back().isRegLoc();
224b60736ecSDimitry Andric } while (HaveRegParm);
22567c32a98SDimitry Andric
22667c32a98SDimitry Andric // Copy all the registers from the value locations we added.
22767c32a98SDimitry Andric assert(NumLocs < Locs.size() && "CC assignment failed to add location");
22867c32a98SDimitry Andric for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
22967c32a98SDimitry Andric if (Locs[I].isRegLoc())
23067c32a98SDimitry Andric Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
23167c32a98SDimitry Andric
23267c32a98SDimitry Andric // Clear the assigned values and stack memory. We leave the registers marked
23367c32a98SDimitry Andric // as allocated so that future queries don't return the same registers, i.e.
23467c32a98SDimitry Andric // when i64 and f64 are both passed in GPRs.
2357fa27ce4SDimitry Andric StackSize = SavedStackSize;
236dd58ef01SDimitry Andric MaxStackArgAlign = SavedMaxStackArgAlign;
237e3b55780SDimitry Andric Locs.truncate(NumLocs);
23867c32a98SDimitry Andric }
23967c32a98SDimitry Andric
analyzeMustTailForwardedRegisters(SmallVectorImpl<ForwardedRegister> & Forwards,ArrayRef<MVT> RegParmTypes,CCAssignFn Fn)24067c32a98SDimitry Andric void CCState::analyzeMustTailForwardedRegisters(
24167c32a98SDimitry Andric SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
24267c32a98SDimitry Andric CCAssignFn Fn) {
24367c32a98SDimitry Andric // Oftentimes calling conventions will not user register parameters for
24467c32a98SDimitry Andric // variadic functions, so we need to assume we're not variadic so that we get
24567c32a98SDimitry Andric // all the registers that might be used in a non-variadic call.
246e3b55780SDimitry Andric SaveAndRestore SavedVarArg(IsVarArg, false);
247e3b55780SDimitry Andric SaveAndRestore SavedMustTail(AnalyzingMustTailForwardedRegs, true);
24867c32a98SDimitry Andric
24967c32a98SDimitry Andric for (MVT RegVT : RegParmTypes) {
25067c32a98SDimitry Andric SmallVector<MCPhysReg, 8> RemainingRegs;
25167c32a98SDimitry Andric getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
25267c32a98SDimitry Andric const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
25367c32a98SDimitry Andric const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
25467c32a98SDimitry Andric for (MCPhysReg PReg : RemainingRegs) {
255b60736ecSDimitry Andric Register VReg = MF.addLiveIn(PReg, RC);
25667c32a98SDimitry Andric Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
25767c32a98SDimitry Andric }
25867c32a98SDimitry Andric }
25967c32a98SDimitry Andric }
26001095a5dSDimitry Andric
resultsCompatible(CallingConv::ID CalleeCC,CallingConv::ID CallerCC,MachineFunction & MF,LLVMContext & C,const SmallVectorImpl<ISD::InputArg> & Ins,CCAssignFn CalleeFn,CCAssignFn CallerFn)26101095a5dSDimitry Andric bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
26201095a5dSDimitry Andric CallingConv::ID CallerCC, MachineFunction &MF,
26301095a5dSDimitry Andric LLVMContext &C,
26401095a5dSDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins,
26501095a5dSDimitry Andric CCAssignFn CalleeFn, CCAssignFn CallerFn) {
26601095a5dSDimitry Andric if (CalleeCC == CallerCC)
26701095a5dSDimitry Andric return true;
26801095a5dSDimitry Andric SmallVector<CCValAssign, 4> RVLocs1;
26901095a5dSDimitry Andric CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
27001095a5dSDimitry Andric CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
27101095a5dSDimitry Andric
27201095a5dSDimitry Andric SmallVector<CCValAssign, 4> RVLocs2;
27301095a5dSDimitry Andric CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
27401095a5dSDimitry Andric CCInfo2.AnalyzeCallResult(Ins, CallerFn);
27501095a5dSDimitry Andric
276e3b55780SDimitry Andric auto AreCompatible = [](const CCValAssign &Loc1, const CCValAssign &Loc2) {
277e3b55780SDimitry Andric assert(!Loc1.isPendingLoc() && !Loc2.isPendingLoc() &&
278e3b55780SDimitry Andric "The location must have been decided by now");
279e3b55780SDimitry Andric // Must fill the same part of their locations.
280e3b55780SDimitry Andric if (Loc1.getLocInfo() != Loc2.getLocInfo())
28101095a5dSDimitry Andric return false;
282e3b55780SDimitry Andric // Must both be in the same registers, or both in memory at the same offset.
283e3b55780SDimitry Andric if (Loc1.isRegLoc() && Loc2.isRegLoc())
284e3b55780SDimitry Andric return Loc1.getLocReg() == Loc2.getLocReg();
285e3b55780SDimitry Andric if (Loc1.isMemLoc() && Loc2.isMemLoc())
286e3b55780SDimitry Andric return Loc1.getLocMemOffset() == Loc2.getLocMemOffset();
287e3b55780SDimitry Andric llvm_unreachable("Unknown location kind");
288e3b55780SDimitry Andric };
289cfca06d7SDimitry Andric
290e3b55780SDimitry Andric return std::equal(RVLocs1.begin(), RVLocs1.end(), RVLocs2.begin(),
291e3b55780SDimitry Andric RVLocs2.end(), AreCompatible);
29201095a5dSDimitry Andric }
293