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"
21 #include "llvm/CallingConv.h"
24 class TargetRegisterInfo;
29 /// CCValAssign - Represent assignment of one arg/retval to a location.
33 Full, // The value fills the full location.
34 SExt, // The value is sign extended in the location.
35 ZExt, // The value is zero extended in the location.
36 AExt, // The value is extended with undefined upper bits.
37 BCvt, // The value is bit-converted in the location.
38 VExt, // The value is vector-widened in the location.
39 // FIXME: Not implemented yet. Code that uses AExt to mean
40 // vector-widen should be fixed to use VExt instead.
41 Indirect // The location contains pointer to the value.
42 // TODO: a subset of the value is in the location.
45 /// ValNo - This is the value number begin assigned (e.g. an argument number).
48 /// Loc is either a stack offset or a register number.
51 /// isMem - True if this is a memory loc, false if it is a register loc.
54 /// isCustom - True if this arg/retval requires special handling.
57 /// Information about how the value is assigned.
60 /// ValVT - The type of the value being assigned.
63 /// LocVT - The type of the location being assigned to.
67 static CCValAssign getReg(unsigned ValNo, EVT ValVT,
68 unsigned RegNo, EVT LocVT,
81 static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
82 unsigned RegNo, EVT LocVT,
85 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
90 static CCValAssign getMem(unsigned ValNo, EVT ValVT,
91 unsigned Offset, EVT LocVT,
104 static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
105 unsigned Offset, EVT LocVT,
108 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
113 unsigned getValNo() const { return ValNo; }
114 EVT getValVT() const { return ValVT; }
116 bool isRegLoc() const { return !isMem; }
117 bool isMemLoc() const { return isMem; }
119 bool needsCustom() const { return isCustom; }
121 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
122 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
123 EVT getLocVT() const { return LocVT; }
125 LocInfo getLocInfo() const { return HTP; }
126 bool isExtInLoc() const {
127 return (HTP == AExt || HTP == SExt || HTP == ZExt);
132 /// CCAssignFn - This function assigns a location for Val, updating State to
133 /// reflect the change.
134 typedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
135 EVT LocVT, CCValAssign::LocInfo LocInfo,
136 ISD::ArgFlagsTy ArgFlags, CCState &State);
138 /// CCCustomFn - This function assigns a location for Val, possibly updating
139 /// all args to reflect changes and indicates if it handled it. It must set
140 /// isCustom if it handles the arg and returns true.
141 typedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
142 EVT &LocVT, CCValAssign::LocInfo &LocInfo,
143 ISD::ArgFlagsTy &ArgFlags, CCState &State);
145 /// CCState - This class holds information needed while lowering arguments and
146 /// return values. It captures which registers are already assigned and which
147 /// stack slots are used. It provides accessors to allocate these values.
149 CallingConv::ID CallingConv;
151 const TargetMachine &TM;
152 const TargetRegisterInfo &TRI;
153 SmallVector<CCValAssign, 16> &Locs;
154 LLVMContext &Context;
156 unsigned StackOffset;
157 SmallVector<uint32_t, 16> UsedRegs;
159 CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
160 SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
162 void addLoc(const CCValAssign &V) {
166 LLVMContext &getContext() const { return Context; }
167 const TargetMachine &getTarget() const { return TM; }
168 CallingConv::ID getCallingConv() const { return CallingConv; }
169 bool isVarArg() const { return IsVarArg; }
171 unsigned getNextStackOffset() const { return StackOffset; }
173 /// isAllocated - Return true if the specified register (or an alias) is
175 bool isAllocated(unsigned Reg) const {
176 return UsedRegs[Reg/32] & (1 << (Reg&31));
179 /// AnalyzeFormalArguments - Analyze an array of argument values,
180 /// incorporating info about the formals into this state.
181 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
184 /// AnalyzeReturn - Analyze the returned values of a return,
185 /// incorporating info about the result values into this state.
186 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
189 /// CheckReturn - Analyze the return values of a function, returning
190 /// true if the return can be performed without sret-demotion, and
192 bool CheckReturn(const SmallVectorImpl<EVT> &OutTys,
193 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
196 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
197 /// incorporating info about the passed values into this state.
198 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
201 /// AnalyzeCallOperands - Same as above except it takes vectors of types
202 /// and argument flags.
203 void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
204 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
207 /// AnalyzeCallResult - Analyze the return values of a call,
208 /// incorporating info about the passed values into this state.
209 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
212 /// AnalyzeCallResult - Same as above except it's specialized for calls which
213 /// produce a single value.
214 void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
216 /// getFirstUnallocated - Return the first unallocated register in the set, or
217 /// NumRegs if they are all allocated.
218 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
219 for (unsigned i = 0; i != NumRegs; ++i)
220 if (!isAllocated(Regs[i]))
225 /// AllocateReg - Attempt to allocate one register. If it is not available,
226 /// return zero. Otherwise, return the register, marking it and any aliases
228 unsigned AllocateReg(unsigned Reg) {
229 if (isAllocated(Reg)) return 0;
234 /// Version of AllocateReg with extra register to be shadowed.
235 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
236 if (isAllocated(Reg)) return 0;
238 MarkAllocated(ShadowReg);
242 /// AllocateReg - Attempt to allocate one of the specified registers. If none
243 /// are available, return zero. Otherwise, return the first one available,
244 /// marking it and any aliases as allocated.
245 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
246 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
247 if (FirstUnalloc == NumRegs)
248 return 0; // Didn't find the reg.
250 // Mark the register and any aliases as allocated.
251 unsigned Reg = Regs[FirstUnalloc];
256 /// Version of AllocateReg with list of registers to be shadowed.
257 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
259 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
260 if (FirstUnalloc == NumRegs)
261 return 0; // Didn't find the reg.
263 // Mark the register and any aliases as allocated.
264 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
266 MarkAllocated(ShadowReg);
270 /// AllocateStack - Allocate a chunk of stack space with the specified size
272 unsigned AllocateStack(unsigned Size, unsigned Align) {
273 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
274 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
275 unsigned Result = StackOffset;
280 // HandleByVal - Allocate a stack slot large enough to pass an argument by
281 // value. The size and alignment information of the argument is encoded in its
282 // parameter attribute.
283 void HandleByVal(unsigned ValNo, EVT ValVT,
284 EVT LocVT, CCValAssign::LocInfo LocInfo,
285 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
288 /// MarkAllocated - Mark a register and all of its aliases as allocated.
289 void MarkAllocated(unsigned Reg);
294 } // end namespace llvm