1 //===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the CCState and CCValAssign classes, used for lowering
11 // and implementing calling conventions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
16 #define LLVM_CODEGEN_CALLINGCONVLOWER_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
23 class TargetRegisterInfo;
28 /// CCValAssign - Represent assignment of one arg/retval to a location.
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 BCvt // The value is bit-converted in the location.
37 // TODO: a subset of the value is in the location.
40 /// ValNo - This is the value number begin assigned (e.g. an argument number).
43 /// Loc is either a stack offset or a register number.
46 /// isMem - True if this is a memory loc, false if it is a register loc.
49 /// isCustom - True if this arg/retval requires special handling.
52 /// Information about how the value is assigned.
55 /// ValVT - The type of the value being assigned.
58 /// LocVT - The type of the location being assigned to.
62 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
63 unsigned RegNo, MVT LocVT,
76 static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
77 unsigned RegNo, MVT LocVT,
80 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
85 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
86 unsigned Offset, MVT LocVT,
99 static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
100 unsigned Offset, MVT LocVT,
103 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
108 unsigned getValNo() const { return ValNo; }
109 MVT getValVT() const { return ValVT; }
111 bool isRegLoc() const { return !isMem; }
112 bool isMemLoc() const { return isMem; }
114 bool needsCustom() const { return isCustom; }
116 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
117 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
118 MVT getLocVT() const { return LocVT; }
120 LocInfo getLocInfo() const { return HTP; }
123 /// CCAssignFn - This function assigns a location for Val, updating State to
124 /// reflect the change.
125 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
126 MVT LocVT, CCValAssign::LocInfo LocInfo,
127 ISD::ArgFlagsTy ArgFlags, CCState &State);
129 /// CCCustomFn - This function assigns a location for Val, possibly updating
130 /// all args to reflect changes and indicates if it handled it. It must set
131 /// isCustom if it handles the arg and returns true.
132 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
133 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
134 ISD::ArgFlagsTy &ArgFlags, CCState &State);
136 /// CCState - This class holds information needed while lowering arguments and
137 /// return values. It captures which registers are already assigned and which
138 /// stack slots are used. It provides accessors to allocate these values.
140 unsigned CallingConv;
142 const TargetMachine &TM;
143 const TargetRegisterInfo &TRI;
144 SmallVector<CCValAssign, 16> &Locs;
146 unsigned StackOffset;
147 SmallVector<uint32_t, 16> UsedRegs;
149 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
150 SmallVector<CCValAssign, 16> &locs);
152 void addLoc(const CCValAssign &V) {
156 const TargetMachine &getTarget() const { return TM; }
157 unsigned getCallingConv() const { return CallingConv; }
158 bool isVarArg() const { return IsVarArg; }
160 unsigned getNextStackOffset() const { return StackOffset; }
162 /// isAllocated - Return true if the specified register (or an alias) is
164 bool isAllocated(unsigned Reg) const {
165 return UsedRegs[Reg/32] & (1 << (Reg&31));
168 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
169 /// incorporating info about the formals into this state.
170 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
172 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
173 /// incorporating info about the result values into this state.
174 void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
176 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
177 /// about the passed values into this state.
178 void AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn);
180 /// AnalyzeCallOperands - Same as above except it takes vectors of types
181 /// and argument flags.
182 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
183 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
186 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
187 /// incorporating info about the passed values into this state.
188 void AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn);
190 /// AnalyzeCallResult - Same as above except it's specialized for calls which
191 /// produce a single value.
192 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
194 /// getFirstUnallocated - Return the first unallocated register in the set, or
195 /// NumRegs if they are all allocated.
196 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
197 for (unsigned i = 0; i != NumRegs; ++i)
198 if (!isAllocated(Regs[i]))
203 /// AllocateReg - Attempt to allocate one register. If it is not available,
204 /// return zero. Otherwise, return the register, marking it and any aliases
206 unsigned AllocateReg(unsigned Reg) {
207 if (isAllocated(Reg)) return 0;
212 /// Version of AllocateReg with extra register to be shadowed.
213 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
214 if (isAllocated(Reg)) return 0;
216 MarkAllocated(ShadowReg);
220 /// AllocateReg - Attempt to allocate one of the specified registers. If none
221 /// are available, return zero. Otherwise, return the first one available,
222 /// marking it and any aliases as allocated.
223 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
224 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
225 if (FirstUnalloc == NumRegs)
226 return 0; // Didn't find the reg.
228 // Mark the register and any aliases as allocated.
229 unsigned Reg = Regs[FirstUnalloc];
234 /// Version of AllocateReg with list of registers to be shadowed.
235 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
237 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
238 if (FirstUnalloc == NumRegs)
239 return 0; // Didn't find the reg.
241 // Mark the register and any aliases as allocated.
242 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
244 MarkAllocated(ShadowReg);
248 /// AllocateStack - Allocate a chunk of stack space with the specified size
250 unsigned AllocateStack(unsigned Size, unsigned Align) {
251 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
252 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
253 unsigned Result = StackOffset;
258 // HandleByVal - Allocate a stack slot large enough to pass an argument by
259 // value. The size and alignment information of the argument is encoded in its
260 // parameter attribute.
261 void HandleByVal(unsigned ValNo, MVT ValVT,
262 MVT LocVT, CCValAssign::LocInfo LocInfo,
263 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
266 /// MarkAllocated - Mark a register and all of its aliases as allocated.
267 void MarkAllocated(unsigned Reg);
272 } // end namespace llvm