propagate struct size and alignment of byval arguments to the DAG
[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 was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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
21 namespace llvm {
22   class MRegisterInfo;
23   class TargetMachine;
24   class CCState;
25   class SDNode;
26
27 /// CCValAssign - Represent assignment of one arg/retval to a location.
28 class CCValAssign {
29 public:
30   enum LocInfo {
31     Full,   // The value fills the full location.
32     SExt,   // The value is sign extended in the location.
33     ZExt,   // The value is zero extended in the location.
34     AExt    // The value is extended with undefined upper bits.
35     // TODO: a subset of the value is in the location.
36   };
37 private:
38   /// ValNo - This is the value number begin assigned (e.g. an argument number).
39   unsigned ValNo;
40   
41   /// Loc is either a stack offset or a register number.
42   unsigned Loc;
43   
44   /// isMem - True if this is a memory loc, false if it is a register loc.
45   bool isMem : 1;
46   
47   /// Information about how the value is assigned.
48   LocInfo HTP : 7;
49   
50   /// ValVT - The type of the value being assigned.
51   MVT::ValueType ValVT;
52
53   /// LocVT - The type of the location being assigned to.
54   MVT::ValueType LocVT;
55 public:
56     
57   static CCValAssign getReg(unsigned ValNo, MVT::ValueType ValVT,
58                             unsigned RegNo, MVT::ValueType LocVT,
59                             LocInfo HTP) {
60     CCValAssign Ret;
61     Ret.ValNo = ValNo;
62     Ret.Loc = RegNo;
63     Ret.isMem = false;
64     Ret.HTP = HTP;
65     Ret.ValVT = ValVT;
66     Ret.LocVT = LocVT;
67     return Ret;
68   }
69   static CCValAssign getMem(unsigned ValNo, MVT::ValueType ValVT,
70                             unsigned Offset, MVT::ValueType LocVT,
71                             LocInfo HTP) {
72     CCValAssign Ret;
73     Ret.ValNo = ValNo;
74     Ret.Loc = Offset;
75     Ret.isMem = true;
76     Ret.HTP = HTP;
77     Ret.ValVT = ValVT;
78     Ret.LocVT = LocVT;
79     return Ret;
80   }
81   
82   unsigned getValNo() const { return ValNo; }
83   MVT::ValueType getValVT() const { return ValVT; }
84
85   bool isRegLoc() const { return !isMem; }
86   bool isMemLoc() const { return isMem; }
87   
88   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
89   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
90   MVT::ValueType getLocVT() const { return LocVT; }
91   
92   LocInfo getLocInfo() const { return HTP; }
93 };
94
95
96 /// CCAssignFn - This function assigns a location for Val, updating State to
97 /// reflect the change.
98 typedef bool CCAssignFn(unsigned ValNo, MVT::ValueType ValVT,
99                         MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
100                         unsigned ArgFlags, CCState &State);
101
102   
103 /// CCState - This class holds information needed while lowering arguments and
104 /// return values.  It captures which registers are already assigned and which
105 /// stack slots are used.  It provides accessors to allocate these values.
106 class CCState {
107   unsigned CallingConv;
108   bool IsVarArg;
109   const TargetMachine &TM;
110   const MRegisterInfo &MRI;
111   SmallVector<CCValAssign, 16> &Locs;
112   
113   unsigned StackOffset;
114   SmallVector<uint32_t, 16> UsedRegs;
115 public:
116   CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
117           SmallVector<CCValAssign, 16> &locs);
118   
119   void addLoc(const CCValAssign &V) {
120     Locs.push_back(V);
121   }
122   
123   const TargetMachine &getTarget() const { return TM; }
124   unsigned getCallingConv() const { return CallingConv; }
125   bool isVarArg() const { return IsVarArg; }
126   
127   unsigned getNextStackOffset() const { return StackOffset; }
128
129   /// isAllocated - Return true if the specified register (or an alias) is
130   /// allocated.
131   bool isAllocated(unsigned Reg) const {
132     return UsedRegs[Reg/32] & (1 << (Reg&31));
133   }
134   
135   /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
136   /// incorporating info about the formals into this state.
137   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
138   
139   /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
140   /// incorporating info about the result values into this state.
141   void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
142   
143   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
144   /// about the passed values into this state.
145   void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
146
147   /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
148   /// incorporating info about the passed values into this state.
149   void AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn);
150   
151
152   /// getFirstUnallocated - Return the first unallocated register in the set, or
153   /// NumRegs if they are all allocated.
154   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
155     for (unsigned i = 0; i != NumRegs; ++i)
156       if (!isAllocated(Regs[i]))
157         return i;
158     return NumRegs;
159   }
160   
161   /// AllocateReg - Attempt to allocate one register.  If it is not available,
162   /// return zero.  Otherwise, return the register, marking it and any aliases
163   /// as allocated.
164   unsigned AllocateReg(unsigned Reg) {
165     if (isAllocated(Reg)) return 0;
166     MarkAllocated(Reg);
167     return Reg;
168   }
169   
170   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
171   /// are available, return zero.  Otherwise, return the first one available,
172   /// marking it and any aliases as allocated.
173   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
174     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
175     if (FirstUnalloc == NumRegs)
176       return 0;    // Didn't find the reg.
177      
178     // Mark the register and any aliases as allocated.
179     unsigned Reg = Regs[FirstUnalloc];
180     MarkAllocated(Reg);
181     return Reg;
182   }
183   
184   /// AllocateStack - Allocate a chunk of stack space with the specified size
185   /// and alignment.
186   unsigned AllocateStack(unsigned Size, unsigned Align) {
187     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
188     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
189     unsigned Result = StackOffset;
190     StackOffset += Size;
191     return Result;
192   }
193
194   void HandleStruct(unsigned ValNo, MVT::ValueType ValVT,
195                     MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
196                     unsigned ArgFlags);
197 private:
198   /// MarkAllocated - Mark a register and all of its aliases as allocated.
199   void MarkAllocated(unsigned Reg);
200 };
201
202
203
204 } // end namespace llvm
205
206 #endif