blob: 7cad4d0311694be55de2f0bd1c44efe6cf48acb2 [file] [log] [blame]
Dan Gohmand51f1962009-03-31 16:51:18 +00001//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
Chris Lattnerdc3adc82007-02-27 04:43:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnerdc3adc82007-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 Christopher0713a9d2011-06-08 23:55:35 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Reid Klecknerce009332014-12-22 23:58:37 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
David Greeneec5883f2010-01-05 01:24:50 +000019#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000020#include "llvm/Support/ErrorHandling.h"
Reid Klecknerce009332014-12-22 23:58:37 +000021#include "llvm/Support/SaveAndRestore.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000022#include "llvm/Support/raw_ostream.h"
Stuart Hastings67c5c3e2011-02-28 17:17:53 +000023#include "llvm/Target/TargetLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000025#include "llvm/Target/TargetSubtargetInfo.h"
Oren Ben Simhon3b951572016-12-21 08:31:45 +000026#include <algorithm>
27
Chris Lattnerdc3adc82007-02-27 04:43:02 +000028using namespace llvm;
29
Eric Christopher0713a9d2011-06-08 23:55:35 +000030CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
Eric Christopherb5217502014-08-06 18:45:26 +000031 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
32 : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
Reid Klecknerc35139e2017-02-02 21:58:22 +000033 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
Chris Lattnerdc3adc82007-02-27 04:43:02 +000034 // No stack is used.
35 StackOffset = 0;
Jeroen Ketema740f9d72015-09-29 10:12:57 +000036 MaxStackArgAlign = 1;
Eric Christopher0713a9d2011-06-08 23:55:35 +000037
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +000038 clearByValRegsInfo();
Dan Gohman328e26d2008-06-30 20:25:31 +000039 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000040}
41
Sanjay Patel8b2150e2015-06-11 14:26:49 +000042/// Allocate space on the stack large enough to pass an argument by value.
43/// The size and alignment information of the argument is encoded in
44/// its parameter attribute.
Duncan Sands71049f72010-11-04 10:49:57 +000045void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
Duncan Sandsf5dda012010-11-03 11:35:31 +000046 MVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Chengeb30bb72008-01-15 07:49:36 +000047 int MinSize, int MinAlign,
Duncan Sandsd97eea32008-03-21 09:14:45 +000048 ISD::ArgFlagsTy ArgFlags) {
49 unsigned Align = ArgFlags.getByValAlign();
50 unsigned Size = ArgFlags.getByValSize();
Evan Chengeb30bb72008-01-15 07:49:36 +000051 if (MinSize > (int)Size)
52 Size = MinSize;
53 if (MinAlign > (int)Align)
54 Align = MinAlign;
Reid Kleckner18ec96f2016-01-21 22:23:22 +000055 ensureMaxAlignment(Align);
Eric Christopherb5217502014-08-06 18:45:26 +000056 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
Rui Ueyamada00f2f2016-01-14 21:06:47 +000057 Size = unsigned(alignTo(Size, MinAlign));
Stuart Hastings493a12b2011-05-26 04:09:49 +000058 unsigned Offset = AllocateStack(Size, Align);
59 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Rafael Espindola66011c12007-08-10 14:44:42 +000060}
Chris Lattnerdc3adc82007-02-27 04:43:02 +000061
Sanjay Patel8b2150e2015-06-11 14:26:49 +000062/// Mark a register and all of its aliases as allocated.
Chris Lattnerdc3adc82007-02-27 04:43:02 +000063void CCState::MarkAllocated(unsigned Reg) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000064 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
65 UsedRegs[*AI/32] |= 1 << (*AI&31);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000066}
Chris Lattnere7474412007-02-28 06:56:37 +000067
Oren Ben Simhon3b951572016-12-21 08:31:45 +000068bool CCState::IsShadowAllocatedReg(unsigned Reg) const {
69 if (!isAllocated(Reg))
70 return false;
71
72 for (auto const &ValAssign : Locs) {
73 if (ValAssign.isRegLoc()) {
74 for (MCRegAliasIterator AI(ValAssign.getLocReg(), &TRI, true);
75 AI.isValid(); ++AI) {
76 if (*AI == Reg)
77 return false;
78 }
79 }
80 }
81 return true;
82}
83
Sanjay Patel8b2150e2015-06-11 14:26:49 +000084/// Analyze an array of argument values,
Chris Lattner74bb9292007-02-28 07:09:40 +000085/// incorporating info about the formals into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000086void
87CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
88 CCAssignFn Fn) {
89 unsigned NumArgs = Ins.size();
90
Chris Lattner74bb9292007-02-28 07:09:40 +000091 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000092 MVT ArgVT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000093 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner74bb9292007-02-28 07:09:40 +000094 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000095#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +000096 dbgs() << "Formal argument #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +000097 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +000098#endif
Craig Topperc0196b12014-04-14 00:51:57 +000099 llvm_unreachable(nullptr);
Chris Lattner74bb9292007-02-28 07:09:40 +0000100 }
101 }
102}
103
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000104/// Analyze the return values of a function, returning true if the return can
105/// be performed without sret-demotion and false otherwise.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000106bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Kenneth Uildriks07119732009-11-07 02:11:54 +0000107 CCAssignFn Fn) {
108 // Determine which register each value should be copied into.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000109 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000110 MVT VT = Outs[i].VT;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000111 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Kenneth Uildriks07119732009-11-07 02:11:54 +0000112 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
113 return false;
114 }
115 return true;
116}
117
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000118/// Analyze the returned values of a return,
Chris Lattner74bb9292007-02-28 07:09:40 +0000119/// incorporating info about the result values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000120void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
121 CCAssignFn Fn) {
Chris Lattner74bb9292007-02-28 07:09:40 +0000122 // Determine which register each value should be copied into.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000123 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000124 MVT VT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000125 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
126 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
127#ifndef NDEBUG
128 dbgs() << "Return operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000129 << EVT(VT).getEVTString() << '\n';
Dan Gohman4e49b592010-07-06 15:39:54 +0000130#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000131 llvm_unreachable(nullptr);
Dan Gohman4e49b592010-07-06 15:39:54 +0000132 }
133 }
134}
135
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000136/// Analyze the outgoing arguments to a call,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000137/// incorporating info about the passed values into this state.
138void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
139 CCAssignFn Fn) {
140 unsigned NumOps = Outs.size();
Chris Lattnere7474412007-02-28 06:56:37 +0000141 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000142 MVT ArgVT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000143 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
144 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
145#ifndef NDEBUG
146 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000147 << EVT(ArgVT).getEVTString() << '\n';
Dan Gohman4e49b592010-07-06 15:39:54 +0000148#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000149 llvm_unreachable(nullptr);
Dan Gohman4e49b592010-07-06 15:39:54 +0000150 }
151 }
152}
153
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000154/// Same as above except it takes vectors of types and argument flags.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000155void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Cheng6b8fae12008-09-05 16:59:26 +0000156 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
157 CCAssignFn Fn) {
158 unsigned NumOps = ArgVTs.size();
159 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000160 MVT ArgVT = ArgVTs[i];
Evan Cheng6b8fae12008-09-05 16:59:26 +0000161 ISD::ArgFlagsTy ArgFlags = Flags[i];
162 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000163#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000164 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000165 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000166#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000167 llvm_unreachable(nullptr);
Evan Cheng6b8fae12008-09-05 16:59:26 +0000168 }
169 }
170}
171
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000172/// Analyze the return values of a call, incorporating info about the passed
173/// values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000174void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
175 CCAssignFn Fn) {
176 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000177 MVT VT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000178 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen0e32a2c2008-09-26 19:31:26 +0000179 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000180#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000181 dbgs() << "Call result #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000182 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000183#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000184 llvm_unreachable(nullptr);
Chris Lattnere7474412007-02-28 06:56:37 +0000185 }
186 }
187}
Evan Cheng615739b2008-09-07 09:02:18 +0000188
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000189/// Same as above except it's specialized for calls that produce a single value.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000190void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
Evan Cheng615739b2008-09-07 09:02:18 +0000191 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000192#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000193 dbgs() << "Call result has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000194 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000195#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000196 llvm_unreachable(nullptr);
Evan Cheng615739b2008-09-07 09:02:18 +0000197 }
198}
Reid Klecknerce009332014-12-22 23:58:37 +0000199
Reid Klecknerbba20f02015-01-12 23:28:23 +0000200static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
201 if (VT.isVector())
202 return true; // Assume -msse-regparm might be in effect.
203 if (!VT.isInteger())
204 return false;
205 if (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall)
206 return true;
207 return false;
208}
209
Reid Klecknerce009332014-12-22 23:58:37 +0000210void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
211 MVT VT, CCAssignFn Fn) {
212 unsigned SavedStackOffset = StackOffset;
Jeroen Ketema740f9d72015-09-29 10:12:57 +0000213 unsigned SavedMaxStackArgAlign = MaxStackArgAlign;
Reid Klecknerce009332014-12-22 23:58:37 +0000214 unsigned NumLocs = Locs.size();
215
Reid Klecknerbba20f02015-01-12 23:28:23 +0000216 // Set the 'inreg' flag if it is used for this calling convention.
Reid Klecknerce009332014-12-22 23:58:37 +0000217 ISD::ArgFlagsTy Flags;
Reid Klecknerbba20f02015-01-12 23:28:23 +0000218 if (isValueTypeInRegForCC(CallingConv, VT))
219 Flags.setInReg();
220
221 // Allocate something of this value type repeatedly until we get assigned a
222 // location in memory.
Reid Klecknerce009332014-12-22 23:58:37 +0000223 bool HaveRegParm = true;
224 while (HaveRegParm) {
225 if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
226#ifndef NDEBUG
227 dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
228 << " while computing remaining regparms\n";
229#endif
230 llvm_unreachable(nullptr);
231 }
232 HaveRegParm = Locs.back().isRegLoc();
233 }
234
235 // Copy all the registers from the value locations we added.
236 assert(NumLocs < Locs.size() && "CC assignment failed to add location");
237 for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
238 if (Locs[I].isRegLoc())
239 Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
240
241 // Clear the assigned values and stack memory. We leave the registers marked
242 // as allocated so that future queries don't return the same registers, i.e.
243 // when i64 and f64 are both passed in GPRs.
244 StackOffset = SavedStackOffset;
Jeroen Ketema740f9d72015-09-29 10:12:57 +0000245 MaxStackArgAlign = SavedMaxStackArgAlign;
Reid Klecknerce009332014-12-22 23:58:37 +0000246 Locs.resize(NumLocs);
247}
248
249void CCState::analyzeMustTailForwardedRegisters(
250 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
251 CCAssignFn Fn) {
252 // Oftentimes calling conventions will not user register parameters for
253 // variadic functions, so we need to assume we're not variadic so that we get
254 // all the registers that might be used in a non-variadic call.
255 SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
Reid Kleckner18ec96f2016-01-21 22:23:22 +0000256 SaveAndRestore<bool> SavedMustTail(AnalyzingMustTailForwardedRegs, true);
Reid Klecknerce009332014-12-22 23:58:37 +0000257
258 for (MVT RegVT : RegParmTypes) {
259 SmallVector<MCPhysReg, 8> RemainingRegs;
260 getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
261 const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
262 const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
263 for (MCPhysReg PReg : RemainingRegs) {
264 unsigned VReg = MF.addLiveIn(PReg, RC);
265 Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
266 }
267 }
268}
Matthias Braun8d414362016-03-30 22:46:04 +0000269
270bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
271 CallingConv::ID CallerCC, MachineFunction &MF,
272 LLVMContext &C,
273 const SmallVectorImpl<ISD::InputArg> &Ins,
274 CCAssignFn CalleeFn, CCAssignFn CallerFn) {
275 if (CalleeCC == CallerCC)
276 return true;
277 SmallVector<CCValAssign, 4> RVLocs1;
278 CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
279 CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
280
281 SmallVector<CCValAssign, 4> RVLocs2;
282 CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
283 CCInfo2.AnalyzeCallResult(Ins, CallerFn);
284
285 if (RVLocs1.size() != RVLocs2.size())
286 return false;
287 for (unsigned I = 0, E = RVLocs1.size(); I != E; ++I) {
288 const CCValAssign &Loc1 = RVLocs1[I];
289 const CCValAssign &Loc2 = RVLocs2[I];
290 if (Loc1.getLocInfo() != Loc2.getLocInfo())
291 return false;
292 bool RegLoc1 = Loc1.isRegLoc();
293 if (RegLoc1 != Loc2.isRegLoc())
294 return false;
295 if (RegLoc1) {
296 if (Loc1.getLocReg() != Loc2.getLocReg())
297 return false;
298 } else {
299 if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset())
300 return false;
301 }
302 }
303 return true;
304}