blob: fb29b1db7a437bbf8ec2cdcbda6db80a3a7b132b [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"
Chris Lattnerdc3adc82007-02-27 04:43:02 +000026using namespace llvm;
27
Eric Christopher0713a9d2011-06-08 23:55:35 +000028CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
Eric Christopherb5217502014-08-06 18:45:26 +000029 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
30 : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
31 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C),
Eric Christopherd9134482014-08-04 21:25:23 +000032 CallOrPrologue(Unknown) {
Chris Lattnerdc3adc82007-02-27 04:43:02 +000033 // No stack is used.
34 StackOffset = 0;
Eric Christopher0713a9d2011-06-08 23:55:35 +000035
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +000036 clearByValRegsInfo();
Dan Gohman328e26d2008-06-30 20:25:31 +000037 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000038}
39
Sanjay Patel8b2150e2015-06-11 14:26:49 +000040/// 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.
Duncan Sands71049f72010-11-04 10:49:57 +000043void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
Duncan Sandsf5dda012010-11-03 11:35:31 +000044 MVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Chengeb30bb72008-01-15 07:49:36 +000045 int MinSize, int MinAlign,
Duncan Sandsd97eea32008-03-21 09:14:45 +000046 ISD::ArgFlagsTy ArgFlags) {
47 unsigned Align = ArgFlags.getByValAlign();
48 unsigned Size = ArgFlags.getByValSize();
Evan Chengeb30bb72008-01-15 07:49:36 +000049 if (MinSize > (int)Size)
50 Size = MinSize;
51 if (MinAlign > (int)Align)
52 Align = MinAlign;
Chad Rosier73696922012-06-19 22:59:12 +000053 MF.getFrameInfo()->ensureMaxAlignment(Align);
Eric Christopherb5217502014-08-06 18:45:26 +000054 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
Reid Klecknere41d9572014-08-06 17:57:23 +000055 Size = unsigned(RoundUpToAlignment(Size, MinAlign));
Stuart Hastings493a12b2011-05-26 04:09:49 +000056 unsigned Offset = AllocateStack(Size, Align);
57 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Rafael Espindola66011c12007-08-10 14:44:42 +000058}
Chris Lattnerdc3adc82007-02-27 04:43:02 +000059
Sanjay Patel8b2150e2015-06-11 14:26:49 +000060/// Mark a register and all of its aliases as allocated.
Chris Lattnerdc3adc82007-02-27 04:43:02 +000061void CCState::MarkAllocated(unsigned Reg) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000062 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63 UsedRegs[*AI/32] |= 1 << (*AI&31);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000064}
Chris Lattnere7474412007-02-28 06:56:37 +000065
Sanjay Patel8b2150e2015-06-11 14:26:49 +000066/// Analyze an array of argument values,
Chris Lattner74bb9292007-02-28 07:09:40 +000067/// incorporating info about the formals into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000068void
69CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
70 CCAssignFn Fn) {
71 unsigned NumArgs = Ins.size();
72
Chris Lattner74bb9292007-02-28 07:09:40 +000073 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000074 MVT ArgVT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000075 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner74bb9292007-02-28 07:09:40 +000076 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000077#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +000078 dbgs() << "Formal argument #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +000079 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +000080#endif
Craig Topperc0196b12014-04-14 00:51:57 +000081 llvm_unreachable(nullptr);
Chris Lattner74bb9292007-02-28 07:09:40 +000082 }
83 }
84}
85
Sanjay Patel8b2150e2015-06-11 14:26:49 +000086/// Analyze the return values of a function, returning true if the return can
87/// be performed without sret-demotion and false otherwise.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000088bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Kenneth Uildriks07119732009-11-07 02:11:54 +000089 CCAssignFn Fn) {
90 // Determine which register each value should be copied into.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000091 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000092 MVT VT = Outs[i].VT;
Dan Gohmand7b5ce32010-07-10 09:00:22 +000093 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Kenneth Uildriks07119732009-11-07 02:11:54 +000094 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
95 return false;
96 }
97 return true;
98}
99
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000100/// Analyze the returned values of a return,
Chris Lattner74bb9292007-02-28 07:09:40 +0000101/// incorporating info about the result values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000102void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
103 CCAssignFn Fn) {
Chris Lattner74bb9292007-02-28 07:09:40 +0000104 // Determine which register each value should be copied into.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000105 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000106 MVT VT = Outs[i].VT;
Dan Gohman4e49b592010-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 Topper04a5cc32012-11-14 05:20:09 +0000111 << EVT(VT).getEVTString() << '\n';
Dan Gohman4e49b592010-07-06 15:39:54 +0000112#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000113 llvm_unreachable(nullptr);
Dan Gohman4e49b592010-07-06 15:39:54 +0000114 }
115 }
116}
117
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000118/// Analyze the outgoing arguments to a call,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000119/// 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 Lattnere7474412007-02-28 06:56:37 +0000123 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000124 MVT ArgVT = Outs[i].VT;
Dan Gohman4e49b592010-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 Topper04a5cc32012-11-14 05:20:09 +0000129 << EVT(ArgVT).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/// Same as above except it takes vectors of types and argument flags.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000137void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Cheng6b8fae12008-09-05 16:59:26 +0000138 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
139 CCAssignFn Fn) {
140 unsigned NumOps = ArgVTs.size();
141 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000142 MVT ArgVT = ArgVTs[i];
Evan Cheng6b8fae12008-09-05 16:59:26 +0000143 ISD::ArgFlagsTy ArgFlags = Flags[i];
144 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000145#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000146 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000147 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000148#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000149 llvm_unreachable(nullptr);
Evan Cheng6b8fae12008-09-05 16:59:26 +0000150 }
151 }
152}
153
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000154/// Analyze the return values of a call, incorporating info about the passed
155/// values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000156void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
157 CCAssignFn Fn) {
158 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000159 MVT VT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000160 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen0e32a2c2008-09-26 19:31:26 +0000161 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000162#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000163 dbgs() << "Call result #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000164 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000165#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000166 llvm_unreachable(nullptr);
Chris Lattnere7474412007-02-28 06:56:37 +0000167 }
168 }
169}
Evan Cheng615739b2008-09-07 09:02:18 +0000170
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000171/// Same as above except it's specialized for calls that produce a single value.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000172void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
Evan Cheng615739b2008-09-07 09:02:18 +0000173 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000174#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000175 dbgs() << "Call result has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000176 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000177#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000178 llvm_unreachable(nullptr);
Evan Cheng615739b2008-09-07 09:02:18 +0000179 }
180}
Reid Klecknerce009332014-12-22 23:58:37 +0000181
Reid Klecknerbba20f02015-01-12 23:28:23 +0000182static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
183 if (VT.isVector())
184 return true; // Assume -msse-regparm might be in effect.
185 if (!VT.isInteger())
186 return false;
187 if (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall)
188 return true;
189 return false;
190}
191
Reid Klecknerce009332014-12-22 23:58:37 +0000192void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
193 MVT VT, CCAssignFn Fn) {
194 unsigned SavedStackOffset = StackOffset;
195 unsigned NumLocs = Locs.size();
196
Reid Klecknerbba20f02015-01-12 23:28:23 +0000197 // Set the 'inreg' flag if it is used for this calling convention.
Reid Klecknerce009332014-12-22 23:58:37 +0000198 ISD::ArgFlagsTy Flags;
Reid Klecknerbba20f02015-01-12 23:28:23 +0000199 if (isValueTypeInRegForCC(CallingConv, VT))
200 Flags.setInReg();
201
202 // Allocate something of this value type repeatedly until we get assigned a
203 // location in memory.
Reid Klecknerce009332014-12-22 23:58:37 +0000204 bool HaveRegParm = true;
205 while (HaveRegParm) {
206 if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
207#ifndef NDEBUG
208 dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
209 << " while computing remaining regparms\n";
210#endif
211 llvm_unreachable(nullptr);
212 }
213 HaveRegParm = Locs.back().isRegLoc();
214 }
215
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()));
221
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);
227}
228
229void CCState::analyzeMustTailForwardedRegisters(
230 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
231 CCAssignFn Fn) {
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);
236
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));
245 }
246 }
247}