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