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/Target/TargetCallingConv.h"
21 #include "llvm/CallingConv.h"
24 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 VExt, // The value is vector-widened in the location.
38 // FIXME: Not implemented yet. Code that uses AExt to mean
39 // vector-widen should be fixed to use VExt instead.
40 Indirect // The location contains pointer to the value.
41 // TODO: a subset of the value is in the location.
44 /// ValNo - This is the value number begin assigned (e.g. an argument number).
47 /// Loc is either a stack offset or a register number.
50 /// isMem - True if this is a memory loc, false if it is a register loc.
53 /// isCustom - True if this arg/retval requires special handling.
56 /// Information about how the value is assigned.
59 /// ValVT - The type of the value being assigned.
62 /// LocVT - The type of the location being assigned to.
66 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
67 unsigned RegNo, MVT LocVT,
80 static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
81 unsigned RegNo, MVT LocVT,
84 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
89 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
90 unsigned Offset, MVT LocVT,
103 static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
104 unsigned Offset, MVT LocVT,
107 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
112 unsigned getValNo() const { return ValNo; }
113 MVT getValVT() const { return ValVT; }
115 bool isRegLoc() const { return !isMem; }
116 bool isMemLoc() const { return isMem; }
118 bool needsCustom() const { return isCustom; }
120 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
121 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
122 MVT getLocVT() const { return LocVT; }
124 LocInfo getLocInfo() const { return HTP; }
125 bool isExtInLoc() const {
126 return (HTP == AExt || HTP == SExt || HTP == ZExt);
131 /// CCAssignFn - This function assigns a location for Val, updating State to
132 /// reflect the change. It returns 'true' if it failed to handle Val.
133 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
134 MVT LocVT, CCValAssign::LocInfo LocInfo,
135 ISD::ArgFlagsTy ArgFlags, CCState &State);
137 /// CCCustomFn - This function assigns a location for Val, possibly updating
138 /// all args to reflect changes and indicates if it handled it. It must set
139 /// isCustom if it handles the arg and returns true.
140 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
141 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
142 ISD::ArgFlagsTy &ArgFlags, CCState &State);
144 typedef enum { Invalid, Prologue, Call } ParmContext;
146 /// CCState - This class holds information needed while lowering arguments and
147 /// return values. It captures which registers are already assigned and which
148 /// stack slots are used. It provides accessors to allocate these values.
150 CallingConv::ID CallingConv;
152 const TargetMachine &TM;
153 const TargetRegisterInfo &TRI;
154 SmallVector<CCValAssign, 16> &Locs;
155 LLVMContext &Context;
157 unsigned StackOffset;
158 SmallVector<uint32_t, 16> UsedRegs;
159 unsigned FirstByValReg;
160 bool FirstByValRegValid;
161 ParmContext CallOrPrologue;
163 CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
164 SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
166 void addLoc(const CCValAssign &V) {
170 LLVMContext &getContext() const { return Context; }
171 const TargetMachine &getTarget() const { return TM; }
172 CallingConv::ID getCallingConv() const { return CallingConv; }
173 bool isVarArg() const { return IsVarArg; }
175 unsigned getNextStackOffset() const { return StackOffset; }
177 /// isAllocated - Return true if the specified register (or an alias) is
179 bool isAllocated(unsigned Reg) const {
180 return UsedRegs[Reg/32] & (1 << (Reg&31));
183 /// AnalyzeFormalArguments - Analyze an array of argument values,
184 /// incorporating info about the formals into this state.
185 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
188 /// AnalyzeReturn - Analyze the returned values of a return,
189 /// incorporating info about the result values into this state.
190 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
193 /// CheckReturn - Analyze the return values of a function, returning
194 /// true if the return can be performed without sret-demotion, and
196 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
199 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
200 /// incorporating info about the passed values into this state.
201 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
204 /// AnalyzeCallOperands - Same as above except it takes vectors of types
205 /// and argument flags.
206 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
207 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
210 /// AnalyzeCallResult - Analyze the return values of a call,
211 /// incorporating info about the passed values into this state.
212 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
215 /// AnalyzeCallResult - Same as above except it's specialized for calls which
216 /// produce a single value.
217 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
219 /// getFirstUnallocated - Return the first unallocated register in the set, or
220 /// NumRegs if they are all allocated.
221 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
222 for (unsigned i = 0; i != NumRegs; ++i)
223 if (!isAllocated(Regs[i]))
228 /// AllocateReg - Attempt to allocate one register. If it is not available,
229 /// return zero. Otherwise, return the register, marking it and any aliases
231 unsigned AllocateReg(unsigned Reg) {
232 if (isAllocated(Reg)) return 0;
237 /// Version of AllocateReg with extra register to be shadowed.
238 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
239 if (isAllocated(Reg)) return 0;
241 MarkAllocated(ShadowReg);
245 /// AllocateReg - Attempt to allocate one of the specified registers. If none
246 /// are available, return zero. Otherwise, return the first one available,
247 /// marking it and any aliases as allocated.
248 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
249 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
250 if (FirstUnalloc == NumRegs)
251 return 0; // Didn't find the reg.
253 // Mark the register and any aliases as allocated.
254 unsigned Reg = Regs[FirstUnalloc];
259 /// Version of AllocateReg with list of registers to be shadowed.
260 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
262 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
263 if (FirstUnalloc == NumRegs)
264 return 0; // Didn't find the reg.
266 // Mark the register and any aliases as allocated.
267 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
269 MarkAllocated(ShadowReg);
273 /// AllocateStack - Allocate a chunk of stack space with the specified size
275 unsigned AllocateStack(unsigned Size, unsigned Align) {
276 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
277 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
278 unsigned Result = StackOffset;
283 /// Version of AllocateStack with extra register to be shadowed.
284 unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
285 MarkAllocated(ShadowReg);
286 return AllocateStack(Size, Align);
289 // HandleByVal - Allocate a stack slot large enough to pass an argument by
290 // value. The size and alignment information of the argument is encoded in its
291 // parameter attribute.
292 void HandleByVal(unsigned ValNo, MVT ValVT,
293 MVT LocVT, CCValAssign::LocInfo LocInfo,
294 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
296 // First GPR that carries part of a byval aggregate that's split
297 // between registers and memory.
298 unsigned getFirstByValReg() { return FirstByValRegValid ? FirstByValReg : 0; }
299 void setFirstByValReg(unsigned r) { FirstByValReg = r; FirstByValRegValid = true; }
300 void clearFirstByValReg() { FirstByValReg = 0; FirstByValRegValid = false; }
301 bool isFirstByValRegValid() { return FirstByValRegValid; }
303 ParmContext getCallOrPrologue() { return CallOrPrologue; }
304 void setCallOrPrologue(ParmContext pc) { CallOrPrologue = pc; }
307 /// MarkAllocated - Mark a register and all of its aliases as allocated.
308 void MarkAllocated(unsigned Reg);
313 } // end namespace llvm