Get rid of the Pass+Context magic.
[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     // TODO: a subset of the value is in the location.
38   };
39 private:
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;
60 public:
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.
125 typedef 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.
132 typedef 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.
139 class CCState {
140   unsigned CallingConv;
141   bool IsVarArg;
142   const TargetMachine &TM;
143   const TargetRegisterInfo &TRI;
144   SmallVector<CCValAssign, 16> &Locs;
145   LLVMContext &Context;
146   
147   unsigned StackOffset;
148   SmallVector<uint32_t, 16> UsedRegs;
149 public:
150   CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
151           SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
152   
153   void addLoc(const CCValAssign &V) {
154     Locs.push_back(V);
155   }
156   
157   LLVMContext &getContext() const { return Context; }
158   const TargetMachine &getTarget() const { return TM; }
159   unsigned getCallingConv() const { return CallingConv; }
160   bool isVarArg() const { return IsVarArg; }
161   
162   unsigned getNextStackOffset() const { return StackOffset; }
163
164   /// isAllocated - Return true if the specified register (or an alias) is
165   /// allocated.
166   bool isAllocated(unsigned Reg) const {
167     return UsedRegs[Reg/32] & (1 << (Reg&31));
168   }
169   
170   /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
171   /// incorporating info about the formals into this state.
172   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
173   
174   /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
175   /// incorporating info about the result values into this state.
176   void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
177   
178   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
179   /// about the passed values into this state.
180   void AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn);
181
182   /// AnalyzeCallOperands - Same as above except it takes vectors of types
183   /// and argument flags.
184   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
185                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
186                            CCAssignFn Fn);
187
188   /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
189   /// incorporating info about the passed values into this state.
190   void AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn);
191   
192   /// AnalyzeCallResult - Same as above except it's specialized for calls which
193   /// produce a single value.
194   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
195
196   /// getFirstUnallocated - Return the first unallocated register in the set, or
197   /// NumRegs if they are all allocated.
198   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
199     for (unsigned i = 0; i != NumRegs; ++i)
200       if (!isAllocated(Regs[i]))
201         return i;
202     return NumRegs;
203   }
204   
205   /// AllocateReg - Attempt to allocate one register.  If it is not available,
206   /// return zero.  Otherwise, return the register, marking it and any aliases
207   /// as allocated.
208   unsigned AllocateReg(unsigned Reg) {
209     if (isAllocated(Reg)) return 0;
210     MarkAllocated(Reg);
211     return Reg;
212   }
213
214   /// Version of AllocateReg with extra register to be shadowed.
215   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
216     if (isAllocated(Reg)) return 0;
217     MarkAllocated(Reg);
218     MarkAllocated(ShadowReg);
219     return Reg;
220   }
221
222   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
223   /// are available, return zero.  Otherwise, return the first one available,
224   /// marking it and any aliases as allocated.
225   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
226     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
227     if (FirstUnalloc == NumRegs)
228       return 0;    // Didn't find the reg.
229
230     // Mark the register and any aliases as allocated.
231     unsigned Reg = Regs[FirstUnalloc];
232     MarkAllocated(Reg);
233     return Reg;
234   }
235
236   /// Version of AllocateReg with list of registers to be shadowed.
237   unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
238                        unsigned NumRegs) {
239     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
240     if (FirstUnalloc == NumRegs)
241       return 0;    // Didn't find the reg.
242
243     // Mark the register and any aliases as allocated.
244     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
245     MarkAllocated(Reg);
246     MarkAllocated(ShadowReg);
247     return Reg;
248   }
249
250   /// AllocateStack - Allocate a chunk of stack space with the specified size
251   /// and alignment.
252   unsigned AllocateStack(unsigned Size, unsigned Align) {
253     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
254     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
255     unsigned Result = StackOffset;
256     StackOffset += Size;
257     return Result;
258   }
259
260   // HandleByVal - Allocate a stack slot large enough to pass an argument by
261   // value. The size and alignment information of the argument is encoded in its
262   // parameter attribute.
263   void HandleByVal(unsigned ValNo, MVT ValVT,
264                    MVT LocVT, CCValAssign::LocInfo LocInfo,
265                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
266
267 private:
268   /// MarkAllocated - Mark a register and all of its aliases as allocated.
269   void MarkAllocated(unsigned Reg);
270 };
271
272
273
274 } // end namespace llvm
275
276 #endif