blob: 034ffb34b9cc28d4130d8a489161704fc5670a9b [file] [log] [blame]
Dan Gohman456e2812009-03-31 16:51:18 +00001//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
Chris Lattner362e98a2007-02-27 04:43:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner362e98a2007-02-27 04:43:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CCState class, used for lowering and implementing
11// calling conventions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/CallingConvLower.h"
Eric Christopher471e4222011-06-08 23:55:35 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Stephen Hinesebe69fe2015-03-23 12:10:34 -070017#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
David Greenead61b122010-01-05 01:24:50 +000019#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000020#include "llvm/Support/ErrorHandling.h"
Stephen Hinesebe69fe2015-03-23 12:10:34 -070021#include "llvm/Support/SaveAndRestore.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000022#include "llvm/Support/raw_ostream.h"
Stuart Hastingsf222e592011-02-28 17:17:53 +000023#include "llvm/Target/TargetLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Target/TargetRegisterInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080025#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000026using namespace llvm;
27
Eric Christopher471e4222011-06-08 23:55:35 +000028CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
Stephen Hines37ed9c12014-12-01 14:51:49 -080029 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
30 : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
31 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C),
32 CallOrPrologue(Unknown) {
Chris Lattner362e98a2007-02-27 04:43:02 +000033 // No stack is used.
34 StackOffset = 0;
Eric Christopher471e4222011-06-08 23:55:35 +000035
Stepan Dyatkovskiy46abfcf2013-05-05 07:48:36 +000036 clearByValRegsInfo();
Dan Gohman117b64b2008-06-30 20:25:31 +000037 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattner362e98a2007-02-27 04:43:02 +000038}
39
Eric Christopher471e4222011-06-08 23:55:35 +000040// HandleByVal - Allocate space on the stack large enough to pass an argument
41// by value. The size and alignment information of the argument is encoded in
42// its parameter attribute.
Duncan Sands1e96bab2010-11-04 10:49:57 +000043void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +000044 MVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Cheng4c46fc62008-01-15 07:49:36 +000045 int MinSize, int MinAlign,
Duncan Sands276dcbd2008-03-21 09:14:45 +000046 ISD::ArgFlagsTy ArgFlags) {
47 unsigned Align = ArgFlags.getByValAlign();
48 unsigned Size = ArgFlags.getByValSize();
Evan Cheng4c46fc62008-01-15 07:49:36 +000049 if (MinSize > (int)Size)
50 Size = MinSize;
51 if (MinAlign > (int)Align)
52 Align = MinAlign;
Chad Rosier2531a642012-06-19 22:59:12 +000053 MF.getFrameInfo()->ensureMaxAlignment(Align);
Stephen Hines37ed9c12014-12-01 14:51:49 -080054 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
55 Size = unsigned(RoundUpToAlignment(Size, MinAlign));
Stuart Hastings2aa0f232011-05-26 04:09:49 +000056 unsigned Offset = AllocateStack(Size, Align);
57 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Rafael Espindola594d37e2007-08-10 14:44:42 +000058}
Chris Lattner362e98a2007-02-27 04:43:02 +000059
60/// MarkAllocated - Mark a register and all of its aliases as allocated.
61void CCState::MarkAllocated(unsigned Reg) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000062 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63 UsedRegs[*AI/32] |= 1 << (*AI&31);
Chris Lattner362e98a2007-02-27 04:43:02 +000064}
Chris Lattnerfb39f992007-02-28 06:56:37 +000065
Dan Gohman98ca4f22009-08-05 01:29:28 +000066/// AnalyzeFormalArguments - Analyze an array of argument values,
Chris Lattner66baf262007-02-28 07:09:40 +000067/// incorporating info about the formals into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +000068void
69CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
70 CCAssignFn Fn) {
71 unsigned NumArgs = Ins.size();
72
Chris Lattner66baf262007-02-28 07:09:40 +000073 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +000074 MVT ArgVT = Ins[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +000075 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner66baf262007-02-28 07:09:40 +000076 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +000077#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +000078 dbgs() << "Formal argument #" << i << " has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +000079 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinc23197a2009-07-14 16:55:14 +000080#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -070081 llvm_unreachable(nullptr);
Chris Lattner66baf262007-02-28 07:09:40 +000082 }
83 }
84}
85
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000086/// CheckReturn - Analyze the return values of a function, returning true if
87/// the return can be performed without sret-demotion, and false otherwise.
Dan Gohman84023e02010-07-10 09:00:22 +000088bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000089 CCAssignFn Fn) {
90 // Determine which register each value should be copied into.
Dan Gohman84023e02010-07-10 09:00:22 +000091 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +000092 MVT VT = Outs[i].VT;
Dan Gohman84023e02010-07-10 09:00:22 +000093 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000094 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
95 return false;
96 }
97 return true;
98}
99
Dan Gohman98ca4f22009-08-05 01:29:28 +0000100/// AnalyzeReturn - Analyze the returned values of a return,
Chris Lattner66baf262007-02-28 07:09:40 +0000101/// incorporating info about the result values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000102void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
103 CCAssignFn Fn) {
Chris Lattner66baf262007-02-28 07:09:40 +0000104 // Determine which register each value should be copied into.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000105 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000106 MVT VT = Outs[i].VT;
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000107 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
108 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
109#ifndef NDEBUG
110 dbgs() << "Return operand #" << i << " has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +0000111 << EVT(VT).getEVTString() << '\n';
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000112#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -0700113 llvm_unreachable(nullptr);
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000114 }
115 }
116}
117
Dan Gohman98ca4f22009-08-05 01:29:28 +0000118/// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
119/// incorporating info about the passed values into this state.
120void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
121 CCAssignFn Fn) {
122 unsigned NumOps = Outs.size();
Chris Lattnerfb39f992007-02-28 06:56:37 +0000123 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000124 MVT ArgVT = Outs[i].VT;
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000125 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
126 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
127#ifndef NDEBUG
128 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +0000129 << EVT(ArgVT).getEVTString() << '\n';
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000130#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -0700131 llvm_unreachable(nullptr);
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000132 }
133 }
134}
135
Evan Chengc89d2fe2008-09-05 16:59:26 +0000136/// AnalyzeCallOperands - Same as above except it takes vectors of types
137/// and argument flags.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000138void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Chengc89d2fe2008-09-05 16:59:26 +0000139 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
140 CCAssignFn Fn) {
141 unsigned NumOps = ArgVTs.size();
142 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000143 MVT ArgVT = ArgVTs[i];
Evan Chengc89d2fe2008-09-05 16:59:26 +0000144 ISD::ArgFlagsTy ArgFlags = Flags[i];
145 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000146#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000147 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +0000148 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinc23197a2009-07-14 16:55:14 +0000149#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -0700150 llvm_unreachable(nullptr);
Evan Chengc89d2fe2008-09-05 16:59:26 +0000151 }
152 }
153}
154
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155/// AnalyzeCallResult - Analyze the return values of a call,
Chris Lattner66baf262007-02-28 07:09:40 +0000156/// incorporating info about the passed values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000157void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
158 CCAssignFn Fn) {
159 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000160 MVT VT = Ins[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000161 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen86098bd2008-09-26 19:31:26 +0000162 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000164 dbgs() << "Call result #" << i << " has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +0000165 << EVT(VT).getEVTString() << '\n';
Torok Edwinc23197a2009-07-14 16:55:14 +0000166#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -0700167 llvm_unreachable(nullptr);
Chris Lattnerfb39f992007-02-28 06:56:37 +0000168 }
169 }
170}
Evan Chengc7fcfa02008-09-07 09:02:18 +0000171
172/// AnalyzeCallResult - Same as above except it's specialized for calls which
173/// produce a single value.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000174void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
Evan Chengc7fcfa02008-09-07 09:02:18 +0000175 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000176#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000177 dbgs() << "Call result has unhandled type "
Craig Topper32631d12012-11-14 05:20:09 +0000178 << EVT(VT).getEVTString() << '\n';
Torok Edwinc23197a2009-07-14 16:55:14 +0000179#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -0700180 llvm_unreachable(nullptr);
Evan Chengc7fcfa02008-09-07 09:02:18 +0000181 }
182}
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700183
184static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
185 if (VT.isVector())
186 return true; // Assume -msse-regparm might be in effect.
187 if (!VT.isInteger())
188 return false;
189 if (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall)
190 return true;
191 return false;
192}
193
194void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
195 MVT VT, CCAssignFn Fn) {
196 unsigned SavedStackOffset = StackOffset;
197 unsigned NumLocs = Locs.size();
198
199 // Set the 'inreg' flag if it is used for this calling convention.
200 ISD::ArgFlagsTy Flags;
201 if (isValueTypeInRegForCC(CallingConv, VT))
202 Flags.setInReg();
203
204 // Allocate something of this value type repeatedly until we get assigned a
205 // location in memory.
206 bool HaveRegParm = true;
207 while (HaveRegParm) {
208 if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
209#ifndef NDEBUG
210 dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
211 << " while computing remaining regparms\n";
212#endif
213 llvm_unreachable(nullptr);
214 }
215 HaveRegParm = Locs.back().isRegLoc();
216 }
217
218 // Copy all the registers from the value locations we added.
219 assert(NumLocs < Locs.size() && "CC assignment failed to add location");
220 for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
221 if (Locs[I].isRegLoc())
222 Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
223
224 // Clear the assigned values and stack memory. We leave the registers marked
225 // as allocated so that future queries don't return the same registers, i.e.
226 // when i64 and f64 are both passed in GPRs.
227 StackOffset = SavedStackOffset;
228 Locs.resize(NumLocs);
229}
230
231void CCState::analyzeMustTailForwardedRegisters(
232 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
233 CCAssignFn Fn) {
234 // Oftentimes calling conventions will not user register parameters for
235 // variadic functions, so we need to assume we're not variadic so that we get
236 // all the registers that might be used in a non-variadic call.
237 SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
238
239 for (MVT RegVT : RegParmTypes) {
240 SmallVector<MCPhysReg, 8> RemainingRegs;
241 getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
242 const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
243 const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
244 for (MCPhysReg PReg : RemainingRegs) {
245 unsigned VReg = MF.addLiveIn(PReg, RC);
246 Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
247 }
248 }
249}