CallingConvLower.h revision 198090
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  /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
187  /// incorporating info about the passed values into this state.
188  void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
189                           CCAssignFn Fn);
190
191  /// AnalyzeCallOperands - Same as above except it takes vectors of types
192  /// and argument flags.
193  void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
194                           SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
195                           CCAssignFn Fn);
196
197  /// AnalyzeCallResult - Analyze the return values of a call,
198  /// incorporating info about the passed values into this state.
199  void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
200                         CCAssignFn Fn);
201
202  /// AnalyzeCallResult - Same as above except it's specialized for calls which
203  /// produce a single value.
204  void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
205
206  /// getFirstUnallocated - Return the first unallocated register in the set, or
207  /// NumRegs if they are all allocated.
208  unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
209    for (unsigned i = 0; i != NumRegs; ++i)
210      if (!isAllocated(Regs[i]))
211        return i;
212    return NumRegs;
213  }
214
215  /// AllocateReg - Attempt to allocate one register.  If it is not available,
216  /// return zero.  Otherwise, return the register, marking it and any aliases
217  /// as allocated.
218  unsigned AllocateReg(unsigned Reg) {
219    if (isAllocated(Reg)) return 0;
220    MarkAllocated(Reg);
221    return Reg;
222  }
223
224  /// Version of AllocateReg with extra register to be shadowed.
225  unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
226    if (isAllocated(Reg)) return 0;
227    MarkAllocated(Reg);
228    MarkAllocated(ShadowReg);
229    return Reg;
230  }
231
232  /// AllocateReg - Attempt to allocate one of the specified registers.  If none
233  /// are available, return zero.  Otherwise, return the first one available,
234  /// marking it and any aliases as allocated.
235  unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
236    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
237    if (FirstUnalloc == NumRegs)
238      return 0;    // Didn't find the reg.
239
240    // Mark the register and any aliases as allocated.
241    unsigned Reg = Regs[FirstUnalloc];
242    MarkAllocated(Reg);
243    return Reg;
244  }
245
246  /// Version of AllocateReg with list of registers to be shadowed.
247  unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
248                       unsigned NumRegs) {
249    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
250    if (FirstUnalloc == NumRegs)
251      return 0;    // Didn't find the reg.
252
253    // Mark the register and any aliases as allocated.
254    unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
255    MarkAllocated(Reg);
256    MarkAllocated(ShadowReg);
257    return Reg;
258  }
259
260  /// AllocateStack - Allocate a chunk of stack space with the specified size
261  /// and alignment.
262  unsigned AllocateStack(unsigned Size, unsigned Align) {
263    assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
264    StackOffset = ((StackOffset + Align-1) & ~(Align-1));
265    unsigned Result = StackOffset;
266    StackOffset += Size;
267    return Result;
268  }
269
270  // HandleByVal - Allocate a stack slot large enough to pass an argument by
271  // value. The size and alignment information of the argument is encoded in its
272  // parameter attribute.
273  void HandleByVal(unsigned ValNo, EVT ValVT,
274                   EVT LocVT, CCValAssign::LocInfo LocInfo,
275                   int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
276
277private:
278  /// MarkAllocated - Mark a register and all of its aliases as allocated.
279  void MarkAllocated(unsigned Reg);
280};
281
282
283
284} // end namespace llvm
285
286#endif
287