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