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