CallingConvLower.h revision 249423
1193323Sed//===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file declares the CCState and CCValAssign classes, used for lowering
11193323Sed// and implementing calling conventions.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15193323Sed#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
16193323Sed#define LLVM_CODEGEN_CALLINGCONVLOWER_H
17193323Sed
18193323Sed#include "llvm/ADT/SmallVector.h"
19249423Sdim#include "llvm/CodeGen/MachineFrameInfo.h"
20223017Sdim#include "llvm/CodeGen/MachineFunction.h"
21193323Sed#include "llvm/CodeGen/ValueTypes.h"
22249423Sdim#include "llvm/IR/CallingConv.h"
23210299Sed#include "llvm/Target/TargetCallingConv.h"
24193323Sed
25193323Sednamespace llvm {
26193323Sed  class TargetRegisterInfo;
27193323Sed  class TargetMachine;
28193323Sed  class CCState;
29193323Sed
30193323Sed/// CCValAssign - Represent assignment of one arg/retval to a location.
31193323Sedclass CCValAssign {
32193323Sedpublic:
33193323Sed  enum LocInfo {
34193323Sed    Full,   // The value fills the full location.
35193323Sed    SExt,   // The value is sign extended in the location.
36193323Sed    ZExt,   // The value is zero extended in the location.
37193323Sed    AExt,   // The value is extended with undefined upper bits.
38198090Srdivacky    BCvt,   // The value is bit-converted in the location.
39210299Sed    VExt,   // The value is vector-widened in the location.
40210299Sed            // FIXME: Not implemented yet. Code that uses AExt to mean
41210299Sed            // vector-widen should be fixed to use VExt instead.
42198090Srdivacky    Indirect // The location contains pointer to the value.
43193323Sed    // TODO: a subset of the value is in the location.
44193323Sed  };
45193323Sedprivate:
46193323Sed  /// ValNo - This is the value number begin assigned (e.g. an argument number).
47193323Sed  unsigned ValNo;
48198090Srdivacky
49193323Sed  /// Loc is either a stack offset or a register number.
50193323Sed  unsigned Loc;
51198090Srdivacky
52193323Sed  /// isMem - True if this is a memory loc, false if it is a register loc.
53249423Sdim  unsigned isMem : 1;
54198090Srdivacky
55193323Sed  /// isCustom - True if this arg/retval requires special handling.
56249423Sdim  unsigned isCustom : 1;
57193323Sed
58193323Sed  /// Information about how the value is assigned.
59193323Sed  LocInfo HTP : 6;
60198090Srdivacky
61193323Sed  /// ValVT - The type of the value being assigned.
62218893Sdim  MVT ValVT;
63193323Sed
64193323Sed  /// LocVT - The type of the location being assigned to.
65218893Sdim  MVT LocVT;
66193323Sedpublic:
67198090Srdivacky
68218893Sdim  static CCValAssign getReg(unsigned ValNo, MVT ValVT,
69218893Sdim                            unsigned RegNo, MVT LocVT,
70193323Sed                            LocInfo HTP) {
71193323Sed    CCValAssign Ret;
72193323Sed    Ret.ValNo = ValNo;
73193323Sed    Ret.Loc = RegNo;
74193323Sed    Ret.isMem = false;
75193323Sed    Ret.isCustom = false;
76193323Sed    Ret.HTP = HTP;
77193323Sed    Ret.ValVT = ValVT;
78193323Sed    Ret.LocVT = LocVT;
79193323Sed    return Ret;
80193323Sed  }
81193323Sed
82218893Sdim  static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
83218893Sdim                                  unsigned RegNo, MVT LocVT,
84193323Sed                                  LocInfo HTP) {
85193323Sed    CCValAssign Ret;
86193323Sed    Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
87193323Sed    Ret.isCustom = true;
88193323Sed    return Ret;
89193323Sed  }
90193323Sed
91218893Sdim  static CCValAssign getMem(unsigned ValNo, MVT ValVT,
92218893Sdim                            unsigned Offset, MVT LocVT,
93193323Sed                            LocInfo HTP) {
94193323Sed    CCValAssign Ret;
95193323Sed    Ret.ValNo = ValNo;
96193323Sed    Ret.Loc = Offset;
97193323Sed    Ret.isMem = true;
98193323Sed    Ret.isCustom = false;
99193323Sed    Ret.HTP = HTP;
100193323Sed    Ret.ValVT = ValVT;
101193323Sed    Ret.LocVT = LocVT;
102193323Sed    return Ret;
103193323Sed  }
104198090Srdivacky
105218893Sdim  static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
106218893Sdim                                  unsigned Offset, MVT LocVT,
107193323Sed                                  LocInfo HTP) {
108193323Sed    CCValAssign Ret;
109193323Sed    Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
110193323Sed    Ret.isCustom = true;
111193323Sed    return Ret;
112193323Sed  }
113193323Sed
114193323Sed  unsigned getValNo() const { return ValNo; }
115218893Sdim  MVT getValVT() const { return ValVT; }
116193323Sed
117193323Sed  bool isRegLoc() const { return !isMem; }
118193323Sed  bool isMemLoc() const { return isMem; }
119198090Srdivacky
120193323Sed  bool needsCustom() const { return isCustom; }
121193323Sed
122193323Sed  unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
123193323Sed  unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
124218893Sdim  MVT getLocVT() const { return LocVT; }
125198090Srdivacky
126193323Sed  LocInfo getLocInfo() const { return HTP; }
127198090Srdivacky  bool isExtInLoc() const {
128198090Srdivacky    return (HTP == AExt || HTP == SExt || HTP == ZExt);
129198090Srdivacky  }
130198090Srdivacky
131193323Sed};
132193323Sed
133193323Sed/// CCAssignFn - This function assigns a location for Val, updating State to
134218893Sdim/// reflect the change.  It returns 'true' if it failed to handle Val.
135218893Sdimtypedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
136218893Sdim                        MVT LocVT, CCValAssign::LocInfo LocInfo,
137193323Sed                        ISD::ArgFlagsTy ArgFlags, CCState &State);
138193323Sed
139193323Sed/// CCCustomFn - This function assigns a location for Val, possibly updating
140193323Sed/// all args to reflect changes and indicates if it handled it. It must set
141193323Sed/// isCustom if it handles the arg and returns true.
142218893Sdimtypedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
143218893Sdim                        MVT &LocVT, CCValAssign::LocInfo &LocInfo,
144193323Sed                        ISD::ArgFlagsTy &ArgFlags, CCState &State);
145193323Sed
146223017Sdim/// ParmContext - This enum tracks whether calling convention lowering is in
147223017Sdim/// the context of prologue or call generation. Not all backends make use of
148223017Sdim/// this information.
149223017Sdimtypedef enum { Unknown, Prologue, Call } ParmContext;
150221345Sdim
151193323Sed/// CCState - This class holds information needed while lowering arguments and
152193323Sed/// return values.  It captures which registers are already assigned and which
153193323Sed/// stack slots are used.  It provides accessors to allocate these values.
154193323Sedclass CCState {
155223017Sdimprivate:
156198090Srdivacky  CallingConv::ID CallingConv;
157193323Sed  bool IsVarArg;
158223017Sdim  MachineFunction &MF;
159193323Sed  const TargetMachine &TM;
160193323Sed  const TargetRegisterInfo &TRI;
161193323Sed  SmallVector<CCValAssign, 16> &Locs;
162198090Srdivacky  LLVMContext &Context;
163198090Srdivacky
164193323Sed  unsigned StackOffset;
165193323Sed  SmallVector<uint32_t, 16> UsedRegs;
166221345Sdim  unsigned FirstByValReg;
167221345Sdim  bool FirstByValRegValid;
168223017Sdim
169223017Sdimprotected:
170221345Sdim  ParmContext CallOrPrologue;
171223017Sdim
172193323Sedpublic:
173223017Sdim  CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
174223017Sdim          const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
175223017Sdim          LLVMContext &C);
176198090Srdivacky
177193323Sed  void addLoc(const CCValAssign &V) {
178193323Sed    Locs.push_back(V);
179193323Sed  }
180198090Srdivacky
181198090Srdivacky  LLVMContext &getContext() const { return Context; }
182193323Sed  const TargetMachine &getTarget() const { return TM; }
183223017Sdim  MachineFunction &getMachineFunction() const { return MF; }
184198090Srdivacky  CallingConv::ID getCallingConv() const { return CallingConv; }
185193323Sed  bool isVarArg() const { return IsVarArg; }
186198090Srdivacky
187193323Sed  unsigned getNextStackOffset() const { return StackOffset; }
188193323Sed
189193323Sed  /// isAllocated - Return true if the specified register (or an alias) is
190193323Sed  /// allocated.
191193323Sed  bool isAllocated(unsigned Reg) const {
192193323Sed    return UsedRegs[Reg/32] & (1 << (Reg&31));
193193323Sed  }
194198090Srdivacky
195198090Srdivacky  /// AnalyzeFormalArguments - Analyze an array of argument values,
196193323Sed  /// incorporating info about the formals into this state.
197198090Srdivacky  void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
198198090Srdivacky                              CCAssignFn Fn);
199198090Srdivacky
200198090Srdivacky  /// AnalyzeReturn - Analyze the returned values of a return,
201193323Sed  /// incorporating info about the result values into this state.
202198090Srdivacky  void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
203198090Srdivacky                     CCAssignFn Fn);
204193323Sed
205199481Srdivacky  /// CheckReturn - Analyze the return values of a function, returning
206199481Srdivacky  /// true if the return can be performed without sret-demotion, and
207199481Srdivacky  /// false otherwise.
208210299Sed  bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
209199481Srdivacky                   CCAssignFn Fn);
210199481Srdivacky
211198090Srdivacky  /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
212198090Srdivacky  /// incorporating info about the passed values into this state.
213198090Srdivacky  void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
214198090Srdivacky                           CCAssignFn Fn);
215198090Srdivacky
216193323Sed  /// AnalyzeCallOperands - Same as above except it takes vectors of types
217193323Sed  /// and argument flags.
218218893Sdim  void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
219193323Sed                           SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
220193323Sed                           CCAssignFn Fn);
221193323Sed
222198090Srdivacky  /// AnalyzeCallResult - Analyze the return values of a call,
223193323Sed  /// incorporating info about the passed values into this state.
224198090Srdivacky  void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
225198090Srdivacky                         CCAssignFn Fn);
226198090Srdivacky
227193323Sed  /// AnalyzeCallResult - Same as above except it's specialized for calls which
228193323Sed  /// produce a single value.
229218893Sdim  void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
230193323Sed
231193323Sed  /// getFirstUnallocated - Return the first unallocated register in the set, or
232193323Sed  /// NumRegs if they are all allocated.
233234353Sdim  unsigned getFirstUnallocated(const uint16_t *Regs, unsigned NumRegs) const {
234193323Sed    for (unsigned i = 0; i != NumRegs; ++i)
235193323Sed      if (!isAllocated(Regs[i]))
236193323Sed        return i;
237193323Sed    return NumRegs;
238193323Sed  }
239198090Srdivacky
240193323Sed  /// AllocateReg - Attempt to allocate one register.  If it is not available,
241193323Sed  /// return zero.  Otherwise, return the register, marking it and any aliases
242193323Sed  /// as allocated.
243193323Sed  unsigned AllocateReg(unsigned Reg) {
244193323Sed    if (isAllocated(Reg)) return 0;
245193323Sed    MarkAllocated(Reg);
246193323Sed    return Reg;
247193323Sed  }
248193323Sed
249193323Sed  /// Version of AllocateReg with extra register to be shadowed.
250193323Sed  unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
251193323Sed    if (isAllocated(Reg)) return 0;
252193323Sed    MarkAllocated(Reg);
253193323Sed    MarkAllocated(ShadowReg);
254193323Sed    return Reg;
255193323Sed  }
256193323Sed
257193323Sed  /// AllocateReg - Attempt to allocate one of the specified registers.  If none
258193323Sed  /// are available, return zero.  Otherwise, return the first one available,
259193323Sed  /// marking it and any aliases as allocated.
260234353Sdim  unsigned AllocateReg(const uint16_t *Regs, unsigned NumRegs) {
261193323Sed    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
262193323Sed    if (FirstUnalloc == NumRegs)
263193323Sed      return 0;    // Didn't find the reg.
264193323Sed
265193323Sed    // Mark the register and any aliases as allocated.
266193323Sed    unsigned Reg = Regs[FirstUnalloc];
267193323Sed    MarkAllocated(Reg);
268193323Sed    return Reg;
269193323Sed  }
270193323Sed
271193323Sed  /// Version of AllocateReg with list of registers to be shadowed.
272234353Sdim  unsigned AllocateReg(const uint16_t *Regs, const uint16_t *ShadowRegs,
273193323Sed                       unsigned NumRegs) {
274193323Sed    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
275193323Sed    if (FirstUnalloc == NumRegs)
276193323Sed      return 0;    // Didn't find the reg.
277193323Sed
278193323Sed    // Mark the register and any aliases as allocated.
279193323Sed    unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
280193323Sed    MarkAllocated(Reg);
281193323Sed    MarkAllocated(ShadowReg);
282193323Sed    return Reg;
283193323Sed  }
284193323Sed
285193323Sed  /// AllocateStack - Allocate a chunk of stack space with the specified size
286193323Sed  /// and alignment.
287193323Sed  unsigned AllocateStack(unsigned Size, unsigned Align) {
288193323Sed    assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
289193323Sed    StackOffset = ((StackOffset + Align-1) & ~(Align-1));
290193323Sed    unsigned Result = StackOffset;
291193323Sed    StackOffset += Size;
292243830Sdim    MF.getFrameInfo()->ensureMaxAlignment(Align);
293193323Sed    return Result;
294193323Sed  }
295193323Sed
296212904Sdim  /// Version of AllocateStack with extra register to be shadowed.
297212904Sdim  unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
298212904Sdim    MarkAllocated(ShadowReg);
299212904Sdim    return AllocateStack(Size, Align);
300212904Sdim  }
301212904Sdim
302193323Sed  // HandleByVal - Allocate a stack slot large enough to pass an argument by
303193323Sed  // value. The size and alignment information of the argument is encoded in its
304193323Sed  // parameter attribute.
305218893Sdim  void HandleByVal(unsigned ValNo, MVT ValVT,
306218893Sdim                   MVT LocVT, CCValAssign::LocInfo LocInfo,
307193323Sed                   int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
308193323Sed
309221345Sdim  // First GPR that carries part of a byval aggregate that's split
310221345Sdim  // between registers and memory.
311234353Sdim  unsigned getFirstByValReg() const { return FirstByValRegValid ? FirstByValReg : 0; }
312221345Sdim  void setFirstByValReg(unsigned r) { FirstByValReg = r; FirstByValRegValid = true; }
313221345Sdim  void clearFirstByValReg() { FirstByValReg = 0; FirstByValRegValid = false; }
314234353Sdim  bool isFirstByValRegValid() const { return FirstByValRegValid; }
315221345Sdim
316234353Sdim  ParmContext getCallOrPrologue() const { return CallOrPrologue; }
317221345Sdim
318193323Sedprivate:
319193323Sed  /// MarkAllocated - Mark a register and all of its aliases as allocated.
320193323Sed  void MarkAllocated(unsigned Reg);
321193323Sed};
322193323Sed
323193323Sed
324193323Sed
325193323Sed} // end namespace llvm
326193323Sed
327193323Sed#endif
328