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