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