blob: 3d8c2c8b00aa7304a2ee5976147be709ac679768 [file] [log] [blame]
Dan Gohmand51f1962009-03-31 16:51:18 +00001//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
Chris Lattnerdc3adc82007-02-27 04:43:02 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerdc3adc82007-02-27 04:43:02 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the CCState class, used for lowering and implementing
10// calling conventions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/CallingConvLower.h"
Eric Christopher0713a9d2011-06-08 23:55:35 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Reid Klecknerce009332014-12-22 23:58:37 +000016#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000017#include "llvm/CodeGen/TargetLowering.h"
18#include "llvm/CodeGen/TargetRegisterInfo.h"
19#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
David Greeneec5883f2010-01-05 01:24:50 +000021#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000022#include "llvm/Support/ErrorHandling.h"
Reid Klecknerce009332014-12-22 23:58:37 +000023#include "llvm/Support/SaveAndRestore.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000024#include "llvm/Support/raw_ostream.h"
Oren Ben Simhon3b951572016-12-21 08:31:45 +000025#include <algorithm>
26
Chris Lattnerdc3adc82007-02-27 04:43:02 +000027using namespace llvm;
28
Eric Christopher0713a9d2011-06-08 23:55:35 +000029CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
Eric Christopherb5217502014-08-06 18:45:26 +000030 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
31 : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
Reid Klecknerc35139e2017-02-02 21:58:22 +000032 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
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.
Guillaume Chatelet6c5fb612019-08-05 09:49:09 +000043void CCState::HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT,
44 CCValAssign::LocInfo LocInfo, int MinSize,
Guillaume Chatelet94b0c322020-06-08 08:48:49 +000045 Align MinAlign, ISD::ArgFlagsTy ArgFlags) {
Guillaume Chatelet333f2ad2020-02-03 14:49:01 +010046 Align Alignment = ArgFlags.getNonZeroByValAlign();
Duncan Sandsd97eea32008-03-21 09:14:45 +000047 unsigned Size = ArgFlags.getByValSize();
Evan Chengeb30bb72008-01-15 07:49:36 +000048 if (MinSize > (int)Size)
49 Size = MinSize;
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000050 if (MinAlign > Alignment)
51 Alignment = MinAlign;
52 ensureMaxAlignment(Alignment);
Guillaume Chatelet94b0c322020-06-08 08:48:49 +000053 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Alignment);
Rui Ueyamada00f2f2016-01-14 21:06:47 +000054 Size = unsigned(alignTo(Size, MinAlign));
Guillaume Chatelet54076612020-06-08 07:25:57 +000055 unsigned Offset = AllocateStack(Size, Alignment);
Stuart Hastings493a12b2011-05-26 04:09:49 +000056 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Rafael Espindola66011c12007-08-10 14:44:42 +000057}
Chris Lattnerdc3adc82007-02-27 04:43:02 +000058
Sanjay Patel8b2150e2015-06-11 14:26:49 +000059/// Mark a register and all of its aliases as allocated.
Matt Arsenault7a46e362020-04-08 10:49:37 -040060void CCState::MarkAllocated(MCPhysReg Reg) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000061 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
Matt Arsenault7a46e362020-04-08 10:49:37 -040062 UsedRegs[*AI / 32] |= 1 << (*AI & 31);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000063}
Chris Lattnere7474412007-02-28 06:56:37 +000064
Matt Arsenault7a46e362020-04-08 10:49:37 -040065bool CCState::IsShadowAllocatedReg(MCRegister Reg) const {
Oren Ben Simhon3b951572016-12-21 08:31:45 +000066 if (!isAllocated(Reg))
67 return false;
68
69 for (auto const &ValAssign : Locs) {
70 if (ValAssign.isRegLoc()) {
71 for (MCRegAliasIterator AI(ValAssign.getLocReg(), &TRI, true);
72 AI.isValid(); ++AI) {
73 if (*AI == Reg)
74 return false;
75 }
76 }
77 }
78 return true;
79}
80
Sanjay Patel8b2150e2015-06-11 14:26:49 +000081/// Analyze an array of argument values,
Chris Lattner74bb9292007-02-28 07:09:40 +000082/// incorporating info about the formals into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000083void
84CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
85 CCAssignFn Fn) {
86 unsigned NumArgs = Ins.size();
87
Chris Lattner74bb9292007-02-28 07:09:40 +000088 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000089 MVT ArgVT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000090 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Ryan Taylor3b1459e2019-08-28 15:00:45 +000091 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this))
92 report_fatal_error("unable to allocate function argument #" + Twine(i));
Chris Lattner74bb9292007-02-28 07:09:40 +000093 }
94}
95
Sanjay Patel8b2150e2015-06-11 14:26:49 +000096/// Analyze the return values of a function, returning true if the return can
97/// be performed without sret-demotion and false otherwise.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000098bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Kenneth Uildriks07119732009-11-07 02:11:54 +000099 CCAssignFn Fn) {
100 // Determine which register each value should be copied into.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000101 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000102 MVT VT = Outs[i].VT;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000103 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Kenneth Uildriks07119732009-11-07 02:11:54 +0000104 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
105 return false;
106 }
107 return true;
108}
109
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000110/// Analyze the returned values of a return,
Chris Lattner74bb9292007-02-28 07:09:40 +0000111/// incorporating info about the result values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000112void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
113 CCAssignFn Fn) {
Chris Lattner74bb9292007-02-28 07:09:40 +0000114 // Determine which register each value should be copied into.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000115 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000116 MVT VT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000117 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Ryan Taylor3b1459e2019-08-28 15:00:45 +0000118 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
119 report_fatal_error("unable to allocate function return #" + Twine(i));
Dan Gohman4e49b592010-07-06 15:39:54 +0000120 }
121}
122
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000123/// Analyze the outgoing arguments to a call,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000124/// incorporating info about the passed values into this state.
125void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
126 CCAssignFn Fn) {
127 unsigned NumOps = Outs.size();
Chris Lattnere7474412007-02-28 06:56:37 +0000128 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000129 MVT ArgVT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000130 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
131 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
132#ifndef NDEBUG
133 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000134 << EVT(ArgVT).getEVTString() << '\n';
Dan Gohman4e49b592010-07-06 15:39:54 +0000135#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000136 llvm_unreachable(nullptr);
Dan Gohman4e49b592010-07-06 15:39:54 +0000137 }
138 }
139}
140
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000141/// Same as above except it takes vectors of types and argument flags.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000142void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Cheng6b8fae12008-09-05 16:59:26 +0000143 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
144 CCAssignFn Fn) {
145 unsigned NumOps = ArgVTs.size();
146 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000147 MVT ArgVT = ArgVTs[i];
Evan Cheng6b8fae12008-09-05 16:59:26 +0000148 ISD::ArgFlagsTy ArgFlags = Flags[i];
149 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000150#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000151 dbgs() << "Call operand #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000152 << EVT(ArgVT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000153#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000154 llvm_unreachable(nullptr);
Evan Cheng6b8fae12008-09-05 16:59:26 +0000155 }
156 }
157}
158
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000159/// Analyze the return values of a call, incorporating info about the passed
160/// values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000161void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
162 CCAssignFn Fn) {
163 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000164 MVT VT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000165 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen0e32a2c2008-09-26 19:31:26 +0000166 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000167#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000168 dbgs() << "Call result #" << i << " has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000169 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000170#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000171 llvm_unreachable(nullptr);
Chris Lattnere7474412007-02-28 06:56:37 +0000172 }
173 }
174}
Evan Cheng615739b2008-09-07 09:02:18 +0000175
Sanjay Patel8b2150e2015-06-11 14:26:49 +0000176/// Same as above except it's specialized for calls that produce a single value.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000177void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
Evan Cheng615739b2008-09-07 09:02:18 +0000178 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000179#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000180 dbgs() << "Call result has unhandled type "
Craig Topper04a5cc32012-11-14 05:20:09 +0000181 << EVT(VT).getEVTString() << '\n';
Torok Edwinfbcc6632009-07-14 16:55:14 +0000182#endif
Craig Topperc0196b12014-04-14 00:51:57 +0000183 llvm_unreachable(nullptr);
Evan Cheng615739b2008-09-07 09:02:18 +0000184 }
185}
Reid Klecknerce009332014-12-22 23:58:37 +0000186
Reid Klecknerbba20f02015-01-12 23:28:23 +0000187static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
188 if (VT.isVector())
189 return true; // Assume -msse-regparm might be in effect.
190 if (!VT.isInteger())
191 return false;
192 if (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall)
193 return true;
194 return false;
195}
196
Reid Klecknerce009332014-12-22 23:58:37 +0000197void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
198 MVT VT, CCAssignFn Fn) {
199 unsigned SavedStackOffset = StackOffset;
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000200 Align SavedMaxStackArgAlign = MaxStackArgAlign;
Reid Klecknerce009332014-12-22 23:58:37 +0000201 unsigned NumLocs = Locs.size();
202
Reid Klecknerbba20f02015-01-12 23:28:23 +0000203 // Set the 'inreg' flag if it is used for this calling convention.
Reid Klecknerce009332014-12-22 23:58:37 +0000204 ISD::ArgFlagsTy Flags;
Reid Klecknerbba20f02015-01-12 23:28:23 +0000205 if (isValueTypeInRegForCC(CallingConv, VT))
206 Flags.setInReg();
207
208 // Allocate something of this value type repeatedly until we get assigned a
209 // location in memory.
Reid Klecknerce009332014-12-22 23:58:37 +0000210 bool HaveRegParm = true;
211 while (HaveRegParm) {
212 if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
213#ifndef NDEBUG
214 dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
215 << " while computing remaining regparms\n";
216#endif
217 llvm_unreachable(nullptr);
218 }
219 HaveRegParm = Locs.back().isRegLoc();
220 }
221
222 // Copy all the registers from the value locations we added.
223 assert(NumLocs < Locs.size() && "CC assignment failed to add location");
224 for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
225 if (Locs[I].isRegLoc())
226 Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
227
228 // Clear the assigned values and stack memory. We leave the registers marked
229 // as allocated so that future queries don't return the same registers, i.e.
230 // when i64 and f64 are both passed in GPRs.
231 StackOffset = SavedStackOffset;
Jeroen Ketema740f9d72015-09-29 10:12:57 +0000232 MaxStackArgAlign = SavedMaxStackArgAlign;
Reid Klecknerce009332014-12-22 23:58:37 +0000233 Locs.resize(NumLocs);
234}
235
236void CCState::analyzeMustTailForwardedRegisters(
237 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
238 CCAssignFn Fn) {
239 // Oftentimes calling conventions will not user register parameters for
240 // variadic functions, so we need to assume we're not variadic so that we get
241 // all the registers that might be used in a non-variadic call.
242 SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
Reid Kleckner18ec96f2016-01-21 22:23:22 +0000243 SaveAndRestore<bool> SavedMustTail(AnalyzingMustTailForwardedRegs, true);
Reid Klecknerce009332014-12-22 23:58:37 +0000244
245 for (MVT RegVT : RegParmTypes) {
246 SmallVector<MCPhysReg, 8> RemainingRegs;
247 getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
248 const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
249 const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
250 for (MCPhysReg PReg : RemainingRegs) {
251 unsigned VReg = MF.addLiveIn(PReg, RC);
252 Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
253 }
254 }
255}
Matthias Braun8d414362016-03-30 22:46:04 +0000256
257bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
258 CallingConv::ID CallerCC, MachineFunction &MF,
259 LLVMContext &C,
260 const SmallVectorImpl<ISD::InputArg> &Ins,
261 CCAssignFn CalleeFn, CCAssignFn CallerFn) {
262 if (CalleeCC == CallerCC)
263 return true;
264 SmallVector<CCValAssign, 4> RVLocs1;
265 CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
266 CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
267
268 SmallVector<CCValAssign, 4> RVLocs2;
269 CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
270 CCInfo2.AnalyzeCallResult(Ins, CallerFn);
271
272 if (RVLocs1.size() != RVLocs2.size())
273 return false;
274 for (unsigned I = 0, E = RVLocs1.size(); I != E; ++I) {
275 const CCValAssign &Loc1 = RVLocs1[I];
276 const CCValAssign &Loc2 = RVLocs2[I];
David Spickett055ea582020-05-06 13:19:52 +0100277
278 if ( // Must both be in registers, or both in memory
279 Loc1.isRegLoc() != Loc2.isRegLoc() ||
280 // Must fill the same part of their locations
281 Loc1.getLocInfo() != Loc2.getLocInfo() ||
282 // Memory offset/register number must be the same
283 Loc1.getExtraInfo() != Loc2.getExtraInfo())
Matthias Braun8d414362016-03-30 22:46:04 +0000284 return false;
Matthias Braun8d414362016-03-30 22:46:04 +0000285 }
286 return true;
287}