Add 'Indirect' LocInfo class and use to pass __m128 on win64. Also minore fixes here...
[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   MVT ValVT;
58
59   /// LocVT - The type of the location being assigned to.
60   MVT LocVT;
61 public:
62
63   static CCValAssign getReg(unsigned ValNo, MVT ValVT,
64                             unsigned RegNo, MVT 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, MVT ValVT,
78                                   unsigned RegNo, MVT 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, MVT ValVT,
87                             unsigned Offset, MVT 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, MVT ValVT,
101                                   unsigned Offset, MVT 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   MVT 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   MVT 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, MVT ValVT,
131                         MVT 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, MVT &ValVT,
138                         MVT &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 ISD::FORMAL_ARGUMENTS node,
176   /// incorporating info about the formals into this state.
177   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
178
179   /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
180   /// incorporating info about the result values into this state.
181   void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
182
183   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
184   /// about the passed values into this state.
185   void AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn);
186
187   /// AnalyzeCallOperands - Same as above except it takes vectors of types
188   /// and argument flags.
189   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
190                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
191                            CCAssignFn Fn);
192
193   /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
194   /// incorporating info about the passed values into this state.
195   void AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn);
196
197   /// AnalyzeCallResult - Same as above except it's specialized for calls which
198   /// produce a single value.
199   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
200
201   /// getFirstUnallocated - Return the first unallocated register in the set, or
202   /// NumRegs if they are all allocated.
203   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
204     for (unsigned i = 0; i != NumRegs; ++i)
205       if (!isAllocated(Regs[i]))
206         return i;
207     return NumRegs;
208   }
209
210   /// AllocateReg - Attempt to allocate one register.  If it is not available,
211   /// return zero.  Otherwise, return the register, marking it and any aliases
212   /// as allocated.
213   unsigned AllocateReg(unsigned Reg) {
214     if (isAllocated(Reg)) return 0;
215     MarkAllocated(Reg);
216     return Reg;
217   }
218
219   /// Version of AllocateReg with extra register to be shadowed.
220   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
221     if (isAllocated(Reg)) return 0;
222     MarkAllocated(Reg);
223     MarkAllocated(ShadowReg);
224     return Reg;
225   }
226
227   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
228   /// are available, return zero.  Otherwise, return the first one available,
229   /// marking it and any aliases as allocated.
230   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
231     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
232     if (FirstUnalloc == NumRegs)
233       return 0;    // Didn't find the reg.
234
235     // Mark the register and any aliases as allocated.
236     unsigned Reg = Regs[FirstUnalloc];
237     MarkAllocated(Reg);
238     return Reg;
239   }
240
241   /// Version of AllocateReg with list of registers to be shadowed.
242   unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
243                        unsigned NumRegs) {
244     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
245     if (FirstUnalloc == NumRegs)
246       return 0;    // Didn't find the reg.
247
248     // Mark the register and any aliases as allocated.
249     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
250     MarkAllocated(Reg);
251     MarkAllocated(ShadowReg);
252     return Reg;
253   }
254
255   /// AllocateStack - Allocate a chunk of stack space with the specified size
256   /// and alignment.
257   unsigned AllocateStack(unsigned Size, unsigned Align) {
258     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
259     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
260     unsigned Result = StackOffset;
261     StackOffset += Size;
262     return Result;
263   }
264
265   // HandleByVal - Allocate a stack slot large enough to pass an argument by
266   // value. The size and alignment information of the argument is encoded in its
267   // parameter attribute.
268   void HandleByVal(unsigned ValNo, MVT ValVT,
269                    MVT LocVT, CCValAssign::LocInfo LocInfo,
270                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
271
272 private:
273   /// MarkAllocated - Mark a register and all of its aliases as allocated.
274   void MarkAllocated(unsigned Reg);
275 };
276
277
278
279 } // end namespace llvm
280
281 #endif