Implement "general dynamic", "initial exec" and "local exec" TLS models for
[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   /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
134   /// incorporating info about the formals into this state.
135   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
136   
137   /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
138   /// incorporating info about the result values into this state.
139   void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
140   
141   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
142   /// about the passed values into this state.
143   void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
144
145   /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
146   /// incorporating info about the passed values into this state.
147   void AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn);
148   
149
150   /// getFirstUnallocated - Return the first unallocated register in the set, or
151   /// NumRegs if they are all allocated.
152   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
153     for (unsigned i = 0; i != NumRegs; ++i)
154       if (!isAllocated(Regs[i]))
155         return i;
156     return NumRegs;
157   }
158   
159   /// AllocateReg - Attempt to allocate one register.  If it is not available,
160   /// return zero.  Otherwise, return the register, marking it and any aliases
161   /// as allocated.
162   unsigned AllocateReg(unsigned Reg) {
163     if (isAllocated(Reg)) return 0;
164     MarkAllocated(Reg);
165     return Reg;
166   }
167   
168   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
169   /// are available, return zero.  Otherwise, return the first one available,
170   /// marking it and any aliases as allocated.
171   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
172     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
173     if (FirstUnalloc == NumRegs)
174       return 0;    // Didn't find the reg.
175      
176     // Mark the register and any aliases as allocated.
177     unsigned Reg = Regs[FirstUnalloc];
178     MarkAllocated(Reg);
179     return Reg;
180   }
181   
182   /// AllocateStack - Allocate a chunk of stack space with the specified size
183   /// and alignment.
184   unsigned AllocateStack(unsigned Size, unsigned Align) {
185     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
186     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
187     unsigned Result = StackOffset;
188     StackOffset += Size;
189     return Result;
190   }
191 private:
192   /// MarkAllocated - Mark a register and all of its aliases as allocated.
193   void MarkAllocated(unsigned Reg);
194 };
195
196
197
198 } // end namespace llvm
199
200 #endif