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 /// CCState - This class holds information needed while lowering arguments and
145 /// return values. It captures which registers are already assigned and which
146 /// stack slots are used. It provides accessors to allocate these values.
148 CallingConv::ID CallingConv;
150 const TargetMachine &TM;
151 const TargetRegisterInfo &TRI;
152 SmallVector<CCValAssign, 16> &Locs;
153 LLVMContext &Context;
155 unsigned StackOffset;
156 SmallVector<uint32_t, 16> UsedRegs;
158 CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
159 SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
161 void addLoc(const CCValAssign &V) {
165 LLVMContext &getContext() const { return Context; }
166 const TargetMachine &getTarget() const { return TM; }
167 CallingConv::ID getCallingConv() const { return CallingConv; }
168 bool isVarArg() const { return IsVarArg; }
170 unsigned getNextStackOffset() const { return StackOffset; }
172 /// isAllocated - Return true if the specified register (or an alias) is
174 bool isAllocated(unsigned Reg) const {
175 return UsedRegs[Reg/32] & (1 << (Reg&31));
178 /// AnalyzeFormalArguments - Analyze an array of argument values,
179 /// incorporating info about the formals into this state.
180 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
183 /// AnalyzeReturn - Analyze the returned values of a return,
184 /// incorporating info about the result values into this state.
185 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
188 /// CheckReturn - Analyze the return values of a function, returning
189 /// true if the return can be performed without sret-demotion, and
191 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
194 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
195 /// incorporating info about the passed values into this state.
196 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
199 /// AnalyzeCallOperands - Same as above except it takes vectors of types
200 /// and argument flags.
201 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
202 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
205 /// AnalyzeCallResult - Analyze the return values of a call,
206 /// incorporating info about the passed values into this state.
207 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
210 /// AnalyzeCallResult - Same as above except it's specialized for calls which
211 /// produce a single value.
212 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
214 /// getFirstUnallocated - Return the first unallocated register in the set, or
215 /// NumRegs if they are all allocated.
216 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
217 for (unsigned i = 0; i != NumRegs; ++i)
218 if (!isAllocated(Regs[i]))
223 /// AllocateReg - Attempt to allocate one register. If it is not available,
224 /// return zero. Otherwise, return the register, marking it and any aliases
226 unsigned AllocateReg(unsigned Reg) {
227 if (isAllocated(Reg)) return 0;
232 /// Version of AllocateReg with extra register to be shadowed.
233 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
234 if (isAllocated(Reg)) return 0;
236 MarkAllocated(ShadowReg);
240 /// AllocateReg - Attempt to allocate one of the specified registers. If none
241 /// are available, return zero. Otherwise, return the first one available,
242 /// marking it and any aliases as allocated.
243 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
244 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
245 if (FirstUnalloc == NumRegs)
246 return 0; // Didn't find the reg.
248 // Mark the register and any aliases as allocated.
249 unsigned Reg = Regs[FirstUnalloc];
254 /// Version of AllocateReg with list of registers to be shadowed.
255 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
257 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
258 if (FirstUnalloc == NumRegs)
259 return 0; // Didn't find the reg.
261 // Mark the register and any aliases as allocated.
262 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
264 MarkAllocated(ShadowReg);
268 /// AllocateStack - Allocate a chunk of stack space with the specified size
270 unsigned AllocateStack(unsigned Size, unsigned Align) {
271 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
272 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
273 unsigned Result = StackOffset;
278 /// Version of AllocateStack with extra register to be shadowed.
279 unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
280 MarkAllocated(ShadowReg);
281 return AllocateStack(Size, Align);
284 // HandleByVal - Allocate a stack slot large enough to pass an argument by
285 // value. The size and alignment information of the argument is encoded in its
286 // parameter attribute.
287 void HandleByVal(unsigned ValNo, MVT ValVT,
288 MVT LocVT, CCValAssign::LocInfo LocInfo,
289 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
292 /// MarkAllocated - Mark a register and all of its aliases as allocated.
293 void MarkAllocated(unsigned Reg);
298 } // end namespace llvm