blob: 2fe4e371263b2961d7c9a41ae3d6e0f123c6390a [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/CallingConvLower.h - Calling Conventions ------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the CCState and CCValAssign classes, used for lowering
10// and implementing calling conventions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
15#define LLVM_CODEGEN_CALLINGCONVLOWER_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020019#include "llvm/CodeGen/Register.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#include "llvm/CodeGen/TargetCallingConv.h"
21#include "llvm/IR/CallingConv.h"
22#include "llvm/MC/MCRegisterInfo.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020023#include "llvm/Support/Alignment.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024
25namespace llvm {
26
27class CCState;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020028class MachineFunction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029class MVT;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030class TargetRegisterInfo;
31
32/// CCValAssign - Represent assignment of one arg/retval to a location.
33class CCValAssign {
34public:
35 enum LocInfo {
36 Full, // The value fills the full location.
37 SExt, // The value is sign extended in the location.
38 ZExt, // The value is zero extended in the location.
39 AExt, // The value is extended with undefined upper bits.
40 SExtUpper, // The value is in the upper bits of the location and should be
41 // sign extended when retrieved.
42 ZExtUpper, // The value is in the upper bits of the location and should be
43 // zero extended when retrieved.
44 AExtUpper, // The value is in the upper bits of the location and should be
45 // extended with undefined upper bits when retrieved.
46 BCvt, // The value is bit-converted in the location.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020047 Trunc, // The value is truncated in the location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 VExt, // The value is vector-widened in the location.
49 // FIXME: Not implemented yet. Code that uses AExt to mean
50 // vector-widen should be fixed to use VExt instead.
51 FPExt, // The floating-point value is fp-extended in the location.
52 Indirect // The location contains pointer to the value.
53 // TODO: a subset of the value is in the location.
54 };
55
56private:
57 /// ValNo - This is the value number begin assigned (e.g. an argument number).
58 unsigned ValNo;
59
60 /// Loc is either a stack offset or a register number.
61 unsigned Loc;
62
63 /// isMem - True if this is a memory loc, false if it is a register loc.
64 unsigned isMem : 1;
65
66 /// isCustom - True if this arg/retval requires special handling.
67 unsigned isCustom : 1;
68
69 /// Information about how the value is assigned.
70 LocInfo HTP : 6;
71
72 /// ValVT - The type of the value being assigned.
73 MVT ValVT;
74
75 /// LocVT - The type of the location being assigned to.
76 MVT LocVT;
77public:
78
79 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
80 unsigned RegNo, MVT LocVT,
81 LocInfo HTP) {
82 CCValAssign Ret;
83 Ret.ValNo = ValNo;
84 Ret.Loc = RegNo;
85 Ret.isMem = false;
86 Ret.isCustom = false;
87 Ret.HTP = HTP;
88 Ret.ValVT = ValVT;
89 Ret.LocVT = LocVT;
90 return Ret;
91 }
92
93 static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
94 unsigned RegNo, MVT LocVT,
95 LocInfo HTP) {
96 CCValAssign Ret;
97 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
98 Ret.isCustom = true;
99 return Ret;
100 }
101
102 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
103 unsigned Offset, MVT LocVT,
104 LocInfo HTP) {
105 CCValAssign Ret;
106 Ret.ValNo = ValNo;
107 Ret.Loc = Offset;
108 Ret.isMem = true;
109 Ret.isCustom = false;
110 Ret.HTP = HTP;
111 Ret.ValVT = ValVT;
112 Ret.LocVT = LocVT;
113 return Ret;
114 }
115
116 static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
117 unsigned Offset, MVT LocVT,
118 LocInfo HTP) {
119 CCValAssign Ret;
120 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
121 Ret.isCustom = true;
122 return Ret;
123 }
124
125 // There is no need to differentiate between a pending CCValAssign and other
126 // kinds, as they are stored in a different list.
127 static CCValAssign getPending(unsigned ValNo, MVT ValVT, MVT LocVT,
128 LocInfo HTP, unsigned ExtraInfo = 0) {
129 return getReg(ValNo, ValVT, ExtraInfo, LocVT, HTP);
130 }
131
132 void convertToReg(unsigned RegNo) {
133 Loc = RegNo;
134 isMem = false;
135 }
136
137 void convertToMem(unsigned Offset) {
138 Loc = Offset;
139 isMem = true;
140 }
141
142 unsigned getValNo() const { return ValNo; }
143 MVT getValVT() const { return ValVT; }
144
145 bool isRegLoc() const { return !isMem; }
146 bool isMemLoc() const { return isMem; }
147
148 bool needsCustom() const { return isCustom; }
149
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100150 Register getLocReg() const { assert(isRegLoc()); return Loc; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
152 unsigned getExtraInfo() const { return Loc; }
153 MVT getLocVT() const { return LocVT; }
154
155 LocInfo getLocInfo() const { return HTP; }
156 bool isExtInLoc() const {
157 return (HTP == AExt || HTP == SExt || HTP == ZExt);
158 }
159
160 bool isUpperBitsInLoc() const {
161 return HTP == AExtUpper || HTP == SExtUpper || HTP == ZExtUpper;
162 }
163};
164
165/// Describes a register that needs to be forwarded from the prologue to a
166/// musttail call.
167struct ForwardedRegister {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168 ForwardedRegister(Register VReg, MCPhysReg PReg, MVT VT)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100169 : VReg(VReg), PReg(PReg), VT(VT) {}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200170 Register VReg;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100171 MCPhysReg PReg;
172 MVT VT;
173};
174
175/// CCAssignFn - This function assigns a location for Val, updating State to
176/// reflect the change. It returns 'true' if it failed to handle Val.
177typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
178 MVT LocVT, CCValAssign::LocInfo LocInfo,
179 ISD::ArgFlagsTy ArgFlags, CCState &State);
180
181/// CCCustomFn - This function assigns a location for Val, possibly updating
182/// all args to reflect changes and indicates if it handled it. It must set
183/// isCustom if it handles the arg and returns true.
184typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
185 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
186 ISD::ArgFlagsTy &ArgFlags, CCState &State);
187
188/// CCState - This class holds information needed while lowering arguments and
189/// return values. It captures which registers are already assigned and which
190/// stack slots are used. It provides accessors to allocate these values.
191class CCState {
192private:
193 CallingConv::ID CallingConv;
194 bool IsVarArg;
195 bool AnalyzingMustTailForwardedRegs = false;
196 MachineFunction &MF;
197 const TargetRegisterInfo &TRI;
198 SmallVectorImpl<CCValAssign> &Locs;
199 LLVMContext &Context;
200
201 unsigned StackOffset;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200202 Align MaxStackArgAlign;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100203 SmallVector<uint32_t, 16> UsedRegs;
204 SmallVector<CCValAssign, 4> PendingLocs;
205 SmallVector<ISD::ArgFlagsTy, 4> PendingArgFlags;
206
207 // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
208 //
209 // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
210 // tracking.
211 // Or, in another words it tracks byval parameters that are stored in
212 // general purpose registers.
213 //
214 // For 4 byte stack alignment,
215 // instance index means byval parameter number in formal
216 // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
217 // then, for function "foo":
218 //
219 // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
220 //
221 // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
222 // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
223 //
224 // In case of 8 bytes stack alignment,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225 // In function shown above, r3 would be wasted according to AAPCS rules.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100226 // ByValRegs vector size still would be 2,
227 // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
228 //
229 // Supposed use-case for this collection:
230 // 1. Initially ByValRegs is empty, InRegsParamsProcessed is 0.
231 // 2. HandleByVal fillups ByValRegs.
232 // 3. Argument analysis (LowerFormatArguments, for example). After
233 // some byval argument was analyzed, InRegsParamsProcessed is increased.
234 struct ByValInfo {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200235 ByValInfo(unsigned B, unsigned E) : Begin(B), End(E) {}
236
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100237 // First register allocated for current parameter.
238 unsigned Begin;
239
240 // First after last register allocated for current parameter.
241 unsigned End;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100242 };
243 SmallVector<ByValInfo, 4 > ByValRegs;
244
245 // InRegsParamsProcessed - shows how many instances of ByValRegs was proceed
246 // during argument analysis.
247 unsigned InRegsParamsProcessed;
248
249public:
250 CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
251 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C);
252
253 void addLoc(const CCValAssign &V) {
254 Locs.push_back(V);
255 }
256
257 LLVMContext &getContext() const { return Context; }
258 MachineFunction &getMachineFunction() const { return MF; }
259 CallingConv::ID getCallingConv() const { return CallingConv; }
260 bool isVarArg() const { return IsVarArg; }
261
262 /// getNextStackOffset - Return the next stack offset such that all stack
263 /// slots satisfy their alignment requirements.
264 unsigned getNextStackOffset() const {
265 return StackOffset;
266 }
267
268 /// getAlignedCallFrameSize - Return the size of the call frame needed to
269 /// be able to store all arguments and such that the alignment requirement
270 /// of each of the arguments is satisfied.
271 unsigned getAlignedCallFrameSize() const {
272 return alignTo(StackOffset, MaxStackArgAlign);
273 }
274
275 /// isAllocated - Return true if the specified register (or an alias) is
276 /// allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200277 bool isAllocated(MCRegister Reg) const {
278 return UsedRegs[Reg / 32] & (1 << (Reg & 31));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100279 }
280
281 /// AnalyzeFormalArguments - Analyze an array of argument values,
282 /// incorporating info about the formals into this state.
283 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
284 CCAssignFn Fn);
285
286 /// The function will invoke AnalyzeFormalArguments.
287 void AnalyzeArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
288 CCAssignFn Fn) {
289 AnalyzeFormalArguments(Ins, Fn);
290 }
291
292 /// AnalyzeReturn - Analyze the returned values of a return,
293 /// incorporating info about the result values into this state.
294 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
295 CCAssignFn Fn);
296
297 /// CheckReturn - Analyze the return values of a function, returning
298 /// true if the return can be performed without sret-demotion, and
299 /// false otherwise.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100300 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301 CCAssignFn Fn);
302
303 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
304 /// incorporating info about the passed values into this state.
305 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
306 CCAssignFn Fn);
307
308 /// AnalyzeCallOperands - Same as above except it takes vectors of types
309 /// and argument flags.
310 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
311 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
312 CCAssignFn Fn);
313
314 /// The function will invoke AnalyzeCallOperands.
315 void AnalyzeArguments(const SmallVectorImpl<ISD::OutputArg> &Outs,
316 CCAssignFn Fn) {
317 AnalyzeCallOperands(Outs, Fn);
318 }
319
320 /// AnalyzeCallResult - Analyze the return values of a call,
321 /// incorporating info about the passed values into this state.
322 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
323 CCAssignFn Fn);
324
325 /// A shadow allocated register is a register that was allocated
326 /// but wasn't added to the location list (Locs).
327 /// \returns true if the register was allocated as shadow or false otherwise.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200328 bool IsShadowAllocatedReg(MCRegister Reg) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100329
330 /// AnalyzeCallResult - Same as above except it's specialized for calls which
331 /// produce a single value.
332 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
333
334 /// getFirstUnallocated - Return the index of the first unallocated register
335 /// in the set, or Regs.size() if they are all allocated.
336 unsigned getFirstUnallocated(ArrayRef<MCPhysReg> Regs) const {
337 for (unsigned i = 0; i < Regs.size(); ++i)
338 if (!isAllocated(Regs[i]))
339 return i;
340 return Regs.size();
341 }
342
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200343 void DeallocateReg(MCPhysReg Reg) {
344 assert(isAllocated(Reg) && "Trying to deallocate an unallocated register");
345 MarkUnallocated(Reg);
346 }
347
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100348 /// AllocateReg - Attempt to allocate one register. If it is not available,
349 /// return zero. Otherwise, return the register, marking it and any aliases
350 /// as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200351 MCRegister AllocateReg(MCPhysReg Reg) {
352 if (isAllocated(Reg))
353 return MCRegister();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100354 MarkAllocated(Reg);
355 return Reg;
356 }
357
358 /// Version of AllocateReg with extra register to be shadowed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200359 MCRegister AllocateReg(MCPhysReg Reg, MCPhysReg ShadowReg) {
360 if (isAllocated(Reg))
361 return MCRegister();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100362 MarkAllocated(Reg);
363 MarkAllocated(ShadowReg);
364 return Reg;
365 }
366
367 /// AllocateReg - Attempt to allocate one of the specified registers. If none
368 /// are available, return zero. Otherwise, return the first one available,
369 /// marking it and any aliases as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200370 MCPhysReg AllocateReg(ArrayRef<MCPhysReg> Regs) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100371 unsigned FirstUnalloc = getFirstUnallocated(Regs);
372 if (FirstUnalloc == Regs.size())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200373 return MCRegister(); // Didn't find the reg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100374
375 // Mark the register and any aliases as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200376 MCPhysReg Reg = Regs[FirstUnalloc];
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100377 MarkAllocated(Reg);
378 return Reg;
379 }
380
381 /// AllocateRegBlock - Attempt to allocate a block of RegsRequired consecutive
382 /// registers. If this is not possible, return zero. Otherwise, return the first
383 /// register of the block that were allocated, marking the entire block as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200384 MCPhysReg AllocateRegBlock(ArrayRef<MCPhysReg> Regs, unsigned RegsRequired) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100385 if (RegsRequired > Regs.size())
386 return 0;
387
388 for (unsigned StartIdx = 0; StartIdx <= Regs.size() - RegsRequired;
389 ++StartIdx) {
390 bool BlockAvailable = true;
391 // Check for already-allocated regs in this block
392 for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
393 if (isAllocated(Regs[StartIdx + BlockIdx])) {
394 BlockAvailable = false;
395 break;
396 }
397 }
398 if (BlockAvailable) {
399 // Mark the entire block as allocated
400 for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
401 MarkAllocated(Regs[StartIdx + BlockIdx]);
402 }
403 return Regs[StartIdx];
404 }
405 }
406 // No block was available
407 return 0;
408 }
409
410 /// Version of AllocateReg with list of registers to be shadowed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200411 MCRegister AllocateReg(ArrayRef<MCPhysReg> Regs, const MCPhysReg *ShadowRegs) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100412 unsigned FirstUnalloc = getFirstUnallocated(Regs);
413 if (FirstUnalloc == Regs.size())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200414 return MCRegister(); // Didn't find the reg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415
416 // Mark the register and any aliases as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200417 MCRegister Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100418 MarkAllocated(Reg);
419 MarkAllocated(ShadowReg);
420 return Reg;
421 }
422
423 /// AllocateStack - Allocate a chunk of stack space with the specified size
424 /// and alignment.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200425 unsigned AllocateStack(unsigned Size, Align Alignment) {
426 StackOffset = alignTo(StackOffset, Alignment);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100427 unsigned Result = StackOffset;
428 StackOffset += Size;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200429 MaxStackArgAlign = std::max(Alignment, MaxStackArgAlign);
430 ensureMaxAlignment(Alignment);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100431 return Result;
432 }
433
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200434 // FIXME: Deprecate this function when transition to Align is over.
435 LLVM_ATTRIBUTE_DEPRECATED(unsigned AllocateStack(unsigned Size,
436 unsigned Alignment),
437 "Use the version that takes Align instead.") {
438 return AllocateStack(Size, Align(Alignment));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100439 }
440
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200441 void ensureMaxAlignment(Align Alignment);
442
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100443 /// Version of AllocateStack with extra register to be shadowed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200444 LLVM_ATTRIBUTE_DEPRECATED(unsigned AllocateStack(unsigned Size,
445 unsigned Alignment,
446 unsigned ShadowReg),
447 "Use the version that takes Align instead.") {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448 MarkAllocated(ShadowReg);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200449 return AllocateStack(Size, Align(Alignment));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100450 }
451
452 /// Version of AllocateStack with list of extra registers to be shadowed.
453 /// Note that, unlike AllocateReg, this shadows ALL of the shadow registers.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200454 unsigned AllocateStack(unsigned Size, Align Alignment,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100455 ArrayRef<MCPhysReg> ShadowRegs) {
456 for (unsigned i = 0; i < ShadowRegs.size(); ++i)
457 MarkAllocated(ShadowRegs[i]);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200458 return AllocateStack(Size, Alignment);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100459 }
460
461 // HandleByVal - Allocate a stack slot large enough to pass an argument by
462 // value. The size and alignment information of the argument is encoded in its
463 // parameter attribute.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200464 void HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT,
465 CCValAssign::LocInfo LocInfo, int MinSize, Align MinAlign,
466 ISD::ArgFlagsTy ArgFlags);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100467
468 // Returns count of byval arguments that are to be stored (even partly)
469 // in registers.
470 unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
471
472 // Returns count of byval in-regs arguments proceed.
473 unsigned getInRegsParamsProcessed() const { return InRegsParamsProcessed; }
474
475 // Get information about N-th byval parameter that is stored in registers.
476 // Here "ByValParamIndex" is N.
477 void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
478 unsigned& BeginReg, unsigned& EndReg) const {
479 assert(InRegsParamRecordIndex < ByValRegs.size() &&
480 "Wrong ByVal parameter index");
481
482 const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
483 BeginReg = info.Begin;
484 EndReg = info.End;
485 }
486
487 // Add information about parameter that is kept in registers.
488 void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
489 ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
490 }
491
492 // Goes either to next byval parameter (excluding "waste" record), or
493 // to the end of collection.
494 // Returns false, if end is reached.
495 bool nextInRegsParam() {
496 unsigned e = ByValRegs.size();
497 if (InRegsParamsProcessed < e)
498 ++InRegsParamsProcessed;
499 return InRegsParamsProcessed < e;
500 }
501
502 // Clear byval registers tracking info.
503 void clearByValRegsInfo() {
504 InRegsParamsProcessed = 0;
505 ByValRegs.clear();
506 }
507
508 // Rewind byval registers tracking info.
509 void rewindByValRegsInfo() {
510 InRegsParamsProcessed = 0;
511 }
512
513 // Get list of pending assignments
514 SmallVectorImpl<CCValAssign> &getPendingLocs() {
515 return PendingLocs;
516 }
517
518 // Get a list of argflags for pending assignments.
519 SmallVectorImpl<ISD::ArgFlagsTy> &getPendingArgFlags() {
520 return PendingArgFlags;
521 }
522
523 /// Compute the remaining unused register parameters that would be used for
524 /// the given value type. This is useful when varargs are passed in the
525 /// registers that normal prototyped parameters would be passed in, or for
526 /// implementing perfect forwarding.
527 void getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs, MVT VT,
528 CCAssignFn Fn);
529
530 /// Compute the set of registers that need to be preserved and forwarded to
531 /// any musttail calls.
532 void analyzeMustTailForwardedRegisters(
533 SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
534 CCAssignFn Fn);
535
536 /// Returns true if the results of the two calling conventions are compatible.
537 /// This is usually part of the check for tailcall eligibility.
538 static bool resultsCompatible(CallingConv::ID CalleeCC,
539 CallingConv::ID CallerCC, MachineFunction &MF,
540 LLVMContext &C,
541 const SmallVectorImpl<ISD::InputArg> &Ins,
542 CCAssignFn CalleeFn, CCAssignFn CallerFn);
543
544 /// The function runs an additional analysis pass over function arguments.
545 /// It will mark each argument with the attribute flag SecArgPass.
546 /// After running, it will sort the locs list.
547 template <class T>
548 void AnalyzeArgumentsSecondPass(const SmallVectorImpl<T> &Args,
549 CCAssignFn Fn) {
550 unsigned NumFirstPassLocs = Locs.size();
551
552 /// Creates similar argument list to \p Args in which each argument is
553 /// marked using SecArgPass flag.
554 SmallVector<T, 16> SecPassArg;
555 // SmallVector<ISD::InputArg, 16> SecPassArg;
556 for (auto Arg : Args) {
557 Arg.Flags.setSecArgPass();
558 SecPassArg.push_back(Arg);
559 }
560
561 // Run the second argument pass
562 AnalyzeArguments(SecPassArg, Fn);
563
564 // Sort the locations of the arguments according to their original position.
565 SmallVector<CCValAssign, 16> TmpArgLocs;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100566 TmpArgLocs.swap(Locs);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100567 auto B = TmpArgLocs.begin(), E = TmpArgLocs.end();
568 std::merge(B, B + NumFirstPassLocs, B + NumFirstPassLocs, E,
569 std::back_inserter(Locs),
570 [](const CCValAssign &A, const CCValAssign &B) -> bool {
571 return A.getValNo() < B.getValNo();
572 });
573 }
574
575private:
576 /// MarkAllocated - Mark a register and all of its aliases as allocated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200577 void MarkAllocated(MCPhysReg Reg);
578
579 void MarkUnallocated(MCPhysReg Reg);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100580};
581
582} // end namespace llvm
583
584#endif // LLVM_CODEGEN_CALLINGCONVLOWER_H