add methods to analyze calls and formals.
[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 : 8;
52
53   /// LocVT - The type of the location being assigned to.
54   MVT::ValueType LocVT : 8;
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   const TargetMachine &TM;
109   const MRegisterInfo &MRI;
110   SmallVector<CCValAssign, 16> &Locs;
111   
112   unsigned StackOffset;
113   SmallVector<uint32_t, 16> UsedRegs;
114 public:
115   CCState(unsigned CC, const TargetMachine &TM,
116           SmallVector<CCValAssign, 16> &locs);
117   
118   void addLoc(const CCValAssign &V) {
119     Locs.push_back(V);
120   }
121   
122   const TargetMachine &getTarget() const { return TM; }
123   unsigned getCallingConv() const { return CallingConv; }
124   
125   unsigned getNextStackOffset() const { return StackOffset; }
126
127   /// isAllocated - Return true if the specified register (or an alias) is
128   /// allocated.
129   bool isAllocated(unsigned Reg) const {
130     return UsedRegs[Reg/32] & (1 << (Reg&31));
131   }
132   
133   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
134   /// about the passed values into this state.
135   void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
136
137   /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
138   /// incorporating info about the formals into this state.
139   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
140   
141
142   /// getFirstUnallocated - Return the first unallocated register in the set, or
143   /// NumRegs if they are all allocated.
144   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
145     for (unsigned i = 0; i != NumRegs; ++i)
146       if (!isAllocated(Regs[i]))
147         return i;
148     return NumRegs;
149   }
150   
151   /// AllocateReg - Attempt to allocate one register.  If it is not available,
152   /// return zero.  Otherwise, return the register, marking it and any aliases
153   /// as allocated.
154   unsigned AllocateReg(unsigned Reg) {
155     if (isAllocated(Reg)) return 0;
156     MarkAllocated(Reg);
157     return Reg;
158   }
159   
160   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
161   /// are available, return zero.  Otherwise, return the first one available,
162   /// marking it and any aliases as allocated.
163   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
164     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
165     if (FirstUnalloc == NumRegs)
166       return 0;    // Didn't find the reg.
167      
168     // Mark the register and any aliases as allocated.
169     unsigned Reg = Regs[FirstUnalloc];
170     MarkAllocated(Reg);
171     return Reg;
172   }
173   
174   /// AllocateStack - Allocate a chunk of stack space with the specified size
175   /// and alignment.
176   unsigned AllocateStack(unsigned Size, unsigned Align) {
177     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
178     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
179     unsigned Result = StackOffset;
180     StackOffset += Size;
181     return Result;
182   }
183 private:
184   /// MarkAllocated - Mark a register and all of its aliases as allocated.
185   void MarkAllocated(unsigned Reg);
186 };
187
188
189
190 } // end namespace llvm
191
192 #endif