CallingConvLower.h revision 208954
1//===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the CCState and CCValAssign classes, used for lowering
11// and implementing calling conventions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
16#define LLVM_CODEGEN_CALLINGCONVLOWER_H
17
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/CodeGen/ValueTypes.h"
20#include "llvm/CodeGen/SelectionDAGNodes.h"
21#include "llvm/CallingConv.h"
22
23namespace llvm {
24  class TargetRegisterInfo;
25  class TargetMachine;
26  class CCState;
27  class SDNode;
28
29/// CCValAssign - Represent assignment of one arg/retval to a location.
30class CCValAssign {
31public:
32  enum LocInfo {
33    Full,   // The value fills the full location.
34    SExt,   // The value is sign extended in the location.
35    ZExt,   // The value is zero extended in the location.
36    AExt,   // The value is extended with undefined upper bits.
37    BCvt,   // The value is bit-converted in the location.
38    Indirect // The location contains pointer to the value.
39    // TODO: a subset of the value is in the location.
40  };
41private:
42  /// ValNo - This is the value number begin assigned (e.g. an argument number).
43  unsigned ValNo;
44
45  /// Loc is either a stack offset or a register number.
46  unsigned Loc;
47
48  /// isMem - True if this is a memory loc, false if it is a register loc.
49  bool isMem : 1;
50
51  /// isCustom - True if this arg/retval requires special handling.
52  bool isCustom : 1;
53
54  /// Information about how the value is assigned.
55  LocInfo HTP : 6;
56
57  /// ValVT - The type of the value being assigned.
58  EVT ValVT;
59
60  /// LocVT - The type of the location being assigned to.
61  EVT LocVT;
62public:
63
64  static CCValAssign getReg(unsigned ValNo, EVT ValVT,
65                            unsigned RegNo, EVT LocVT,
66                            LocInfo HTP) {
67    CCValAssign Ret;
68    Ret.ValNo = ValNo;
69    Ret.Loc = RegNo;
70    Ret.isMem = false;
71    Ret.isCustom = false;
72    Ret.HTP = HTP;
73    Ret.ValVT = ValVT;
74    Ret.LocVT = LocVT;
75    return Ret;
76  }
77
78  static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
79                                  unsigned RegNo, EVT LocVT,
80                                  LocInfo HTP) {
81    CCValAssign Ret;
82    Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
83    Ret.isCustom = true;
84    return Ret;
85  }
86
87  static CCValAssign getMem(unsigned ValNo, EVT ValVT,
88                            unsigned Offset, EVT LocVT,
89                            LocInfo HTP) {
90    CCValAssign Ret;
91    Ret.ValNo = ValNo;
92    Ret.Loc = Offset;
93    Ret.isMem = true;
94    Ret.isCustom = false;
95    Ret.HTP = HTP;
96    Ret.ValVT = ValVT;
97    Ret.LocVT = LocVT;
98    return Ret;
99  }
100
101  static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
102                                  unsigned Offset, EVT LocVT,
103                                  LocInfo HTP) {
104    CCValAssign Ret;
105    Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
106    Ret.isCustom = true;
107    return Ret;
108  }
109
110  unsigned getValNo() const { return ValNo; }
111  EVT getValVT() const { return ValVT; }
112
113  bool isRegLoc() const { return !isMem; }
114  bool isMemLoc() const { return isMem; }
115
116  bool needsCustom() const { return isCustom; }
117
118  unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
119  unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
120  EVT getLocVT() const { return LocVT; }
121
122  LocInfo getLocInfo() const { return HTP; }
123  bool isExtInLoc() const {
124    return (HTP == AExt || HTP == SExt || HTP == ZExt);
125  }
126
127};
128
129/// CCAssignFn - This function assigns a location for Val, updating State to
130/// reflect the change.
131typedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
132                        EVT LocVT, CCValAssign::LocInfo LocInfo,
133                        ISD::ArgFlagsTy ArgFlags, CCState &State);
134
135/// CCCustomFn - This function assigns a location for Val, possibly updating
136/// all args to reflect changes and indicates if it handled it. It must set
137/// isCustom if it handles the arg and returns true.
138typedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
139                        EVT &LocVT, CCValAssign::LocInfo &LocInfo,
140                        ISD::ArgFlagsTy &ArgFlags, CCState &State);
141
142/// CCState - This class holds information needed while lowering arguments and
143/// return values.  It captures which registers are already assigned and which
144/// stack slots are used.  It provides accessors to allocate these values.
145class CCState {
146  CallingConv::ID CallingConv;
147  bool IsVarArg;
148  const TargetMachine &TM;
149  const TargetRegisterInfo &TRI;
150  SmallVector<CCValAssign, 16> &Locs;
151  LLVMContext &Context;
152
153  unsigned StackOffset;
154  SmallVector<uint32_t, 16> UsedRegs;
155public:
156  CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
157          SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
158
159  void addLoc(const CCValAssign &V) {
160    Locs.push_back(V);
161  }
162
163  LLVMContext &getContext() const { return Context; }
164  const TargetMachine &getTarget() const { return TM; }
165  CallingConv::ID getCallingConv() const { return CallingConv; }
166  bool isVarArg() const { return IsVarArg; }
167
168  unsigned getNextStackOffset() const { return StackOffset; }
169
170  /// isAllocated - Return true if the specified register (or an alias) is
171  /// allocated.
172  bool isAllocated(unsigned Reg) const {
173    return UsedRegs[Reg/32] & (1 << (Reg&31));
174  }
175
176  /// AnalyzeFormalArguments - Analyze an array of argument values,
177  /// incorporating info about the formals into this state.
178  void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
179                              CCAssignFn Fn);
180
181  /// AnalyzeReturn - Analyze the returned values of a return,
182  /// incorporating info about the result values into this state.
183  void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
184                     CCAssignFn Fn);
185
186  /// CheckReturn - Analyze the return values of a function, returning
187  /// true if the return can be performed without sret-demotion, and
188  /// false otherwise.
189  bool CheckReturn(const SmallVectorImpl<EVT> &OutTys,
190                   const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
191                   CCAssignFn Fn);
192
193  /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
194  /// incorporating info about the passed values into this state.
195  void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
196                           CCAssignFn Fn);
197
198  /// AnalyzeCallOperands - Same as above except it takes vectors of types
199  /// and argument flags.
200  void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
201                           SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
202                           CCAssignFn Fn);
203
204  /// AnalyzeCallResult - Analyze the return values of a call,
205  /// incorporating info about the passed values into this state.
206  void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
207                         CCAssignFn Fn);
208
209  /// AnalyzeCallResult - Same as above except it's specialized for calls which
210  /// produce a single value.
211  void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
212
213  /// getFirstUnallocated - Return the first unallocated register in the set, or
214  /// NumRegs if they are all allocated.
215  unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
216    for (unsigned i = 0; i != NumRegs; ++i)
217      if (!isAllocated(Regs[i]))
218        return i;
219    return NumRegs;
220  }
221
222  /// AllocateReg - Attempt to allocate one register.  If it is not available,
223  /// return zero.  Otherwise, return the register, marking it and any aliases
224  /// as allocated.
225  unsigned AllocateReg(unsigned Reg) {
226    if (isAllocated(Reg)) return 0;
227    MarkAllocated(Reg);
228    return Reg;
229  }
230
231  /// Version of AllocateReg with extra register to be shadowed.
232  unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
233    if (isAllocated(Reg)) return 0;
234    MarkAllocated(Reg);
235    MarkAllocated(ShadowReg);
236    return Reg;
237  }
238
239  /// AllocateReg - Attempt to allocate one of the specified registers.  If none
240  /// are available, return zero.  Otherwise, return the first one available,
241  /// marking it and any aliases as allocated.
242  unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
243    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
244    if (FirstUnalloc == NumRegs)
245      return 0;    // Didn't find the reg.
246
247    // Mark the register and any aliases as allocated.
248    unsigned Reg = Regs[FirstUnalloc];
249    MarkAllocated(Reg);
250    return Reg;
251  }
252
253  /// Version of AllocateReg with list of registers to be shadowed.
254  unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
255                       unsigned NumRegs) {
256    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
257    if (FirstUnalloc == NumRegs)
258      return 0;    // Didn't find the reg.
259
260    // Mark the register and any aliases as allocated.
261    unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
262    MarkAllocated(Reg);
263    MarkAllocated(ShadowReg);
264    return Reg;
265  }
266
267  /// AllocateStack - Allocate a chunk of stack space with the specified size
268  /// and alignment.
269  unsigned AllocateStack(unsigned Size, unsigned Align) {
270    assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
271    StackOffset = ((StackOffset + Align-1) & ~(Align-1));
272    unsigned Result = StackOffset;
273    StackOffset += Size;
274    return Result;
275  }
276
277  // HandleByVal - Allocate a stack slot large enough to pass an argument by
278  // value. The size and alignment information of the argument is encoded in its
279  // parameter attribute.
280  void HandleByVal(unsigned ValNo, EVT ValVT,
281                   EVT LocVT, CCValAssign::LocInfo LocInfo,
282                   int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
283
284private:
285  /// MarkAllocated - Mark a register and all of its aliases as allocated.
286  void MarkAllocated(unsigned Reg);
287};
288
289
290
291} // end namespace llvm
292
293#endif
294