1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CCState class, used for lowering and implementing
11 // calling conventions.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/CallingConvLower.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/SaveAndRestore.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetSubtargetInfo.h"
28 CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
29 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
30 : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
31 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C),
32 CallOrPrologue(Unknown) {
37 UsedRegs.resize((TRI.getNumRegs()+31)/32);
40 /// Allocate space on the stack large enough to pass an argument by value.
41 /// The size and alignment information of the argument is encoded in
42 /// its parameter attribute.
43 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
44 MVT LocVT, CCValAssign::LocInfo LocInfo,
45 int MinSize, int MinAlign,
46 ISD::ArgFlagsTy ArgFlags) {
47 unsigned Align = ArgFlags.getByValAlign();
48 unsigned Size = ArgFlags.getByValSize();
49 if (MinSize > (int)Size)
51 if (MinAlign > (int)Align)
53 MF.getFrameInfo()->ensureMaxAlignment(Align);
54 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
55 Size = unsigned(RoundUpToAlignment(Size, MinAlign));
56 unsigned Offset = AllocateStack(Size, Align);
57 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
60 /// Mark a register and all of its aliases as allocated.
61 void CCState::MarkAllocated(unsigned Reg) {
62 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63 UsedRegs[*AI/32] |= 1 << (*AI&31);
66 /// Analyze an array of argument values,
67 /// incorporating info about the formals into this state.
69 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
71 unsigned NumArgs = Ins.size();
73 for (unsigned i = 0; i != NumArgs; ++i) {
74 MVT ArgVT = Ins[i].VT;
75 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
76 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
78 dbgs() << "Formal argument #" << i << " has unhandled type "
79 << EVT(ArgVT).getEVTString() << '\n';
81 llvm_unreachable(nullptr);
86 /// Analyze the return values of a function, returning true if the return can
87 /// be performed without sret-demotion and false otherwise.
88 bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
90 // Determine which register each value should be copied into.
91 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
93 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
94 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
100 /// Analyze the returned values of a return,
101 /// incorporating info about the result values into this state.
102 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
104 // Determine which register each value should be copied into.
105 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
107 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
108 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
110 dbgs() << "Return operand #" << i << " has unhandled type "
111 << EVT(VT).getEVTString() << '\n';
113 llvm_unreachable(nullptr);
118 /// Analyze the outgoing arguments to a call,
119 /// incorporating info about the passed values into this state.
120 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
122 unsigned NumOps = Outs.size();
123 for (unsigned i = 0; i != NumOps; ++i) {
124 MVT ArgVT = Outs[i].VT;
125 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
126 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
128 dbgs() << "Call operand #" << i << " has unhandled type "
129 << EVT(ArgVT).getEVTString() << '\n';
131 llvm_unreachable(nullptr);
136 /// Same as above except it takes vectors of types and argument flags.
137 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
138 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
140 unsigned NumOps = ArgVTs.size();
141 for (unsigned i = 0; i != NumOps; ++i) {
142 MVT ArgVT = ArgVTs[i];
143 ISD::ArgFlagsTy ArgFlags = Flags[i];
144 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
146 dbgs() << "Call operand #" << i << " has unhandled type "
147 << EVT(ArgVT).getEVTString() << '\n';
149 llvm_unreachable(nullptr);
154 /// Analyze the return values of a call, incorporating info about the passed
155 /// values into this state.
156 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
158 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
160 ISD::ArgFlagsTy Flags = Ins[i].Flags;
161 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
163 dbgs() << "Call result #" << i << " has unhandled type "
164 << EVT(VT).getEVTString() << '\n';
166 llvm_unreachable(nullptr);
171 /// Same as above except it's specialized for calls that produce a single value.
172 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
173 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
175 dbgs() << "Call result has unhandled type "
176 << EVT(VT).getEVTString() << '\n';
178 llvm_unreachable(nullptr);
182 static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
184 return true; // Assume -msse-regparm might be in effect.
187 if (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall)
192 void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
193 MVT VT, CCAssignFn Fn) {
194 unsigned SavedStackOffset = StackOffset;
195 unsigned NumLocs = Locs.size();
197 // Set the 'inreg' flag if it is used for this calling convention.
198 ISD::ArgFlagsTy Flags;
199 if (isValueTypeInRegForCC(CallingConv, VT))
202 // Allocate something of this value type repeatedly until we get assigned a
203 // location in memory.
204 bool HaveRegParm = true;
205 while (HaveRegParm) {
206 if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
208 dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
209 << " while computing remaining regparms\n";
211 llvm_unreachable(nullptr);
213 HaveRegParm = Locs.back().isRegLoc();
216 // Copy all the registers from the value locations we added.
217 assert(NumLocs < Locs.size() && "CC assignment failed to add location");
218 for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
219 if (Locs[I].isRegLoc())
220 Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
222 // Clear the assigned values and stack memory. We leave the registers marked
223 // as allocated so that future queries don't return the same registers, i.e.
224 // when i64 and f64 are both passed in GPRs.
225 StackOffset = SavedStackOffset;
226 Locs.resize(NumLocs);
229 void CCState::analyzeMustTailForwardedRegisters(
230 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
232 // Oftentimes calling conventions will not user register parameters for
233 // variadic functions, so we need to assume we're not variadic so that we get
234 // all the registers that might be used in a non-variadic call.
235 SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
237 for (MVT RegVT : RegParmTypes) {
238 SmallVector<MCPhysReg, 8> RemainingRegs;
239 getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
240 const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
241 const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
242 for (MCPhysReg PReg : RemainingRegs) {
243 unsigned VReg = MF.addLiveIn(PReg, RC);
244 Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));