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 Indirect // The location contains pointer to the value.
38 // TODO: a subset of the value is in the location.
41 /// ValNo - This is the value number begin assigned (e.g. an argument number).
44 /// Loc is either a stack offset or a register number.
47 /// isMem - True if this is a memory loc, false if it is a register loc.
50 /// isCustom - True if this arg/retval requires special handling.
53 /// Information about how the value is assigned.
56 /// ValVT - The type of the value being assigned.
59 /// LocVT - The type of the location being assigned to.
63 static CCValAssign getReg(unsigned ValNo, EVT ValVT,
64 unsigned RegNo, EVT LocVT,
77 static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
78 unsigned RegNo, EVT LocVT,
81 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
86 static CCValAssign getMem(unsigned ValNo, EVT ValVT,
87 unsigned Offset, EVT LocVT,
100 static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
101 unsigned Offset, EVT LocVT,
104 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
109 unsigned getValNo() const { return ValNo; }
110 EVT getValVT() const { return ValVT; }
112 bool isRegLoc() const { return !isMem; }
113 bool isMemLoc() const { return isMem; }
115 bool needsCustom() const { return isCustom; }
117 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
118 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
119 EVT getLocVT() const { return LocVT; }
121 LocInfo getLocInfo() const { return HTP; }
122 bool isExtInLoc() const {
123 return (HTP == AExt || HTP == SExt || HTP == ZExt);
128 /// CCAssignFn - This function assigns a location for Val, updating State to
129 /// reflect the change.
130 typedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
131 EVT LocVT, CCValAssign::LocInfo LocInfo,
132 ISD::ArgFlagsTy ArgFlags, CCState &State);
134 /// CCCustomFn - This function assigns a location for Val, possibly updating
135 /// all args to reflect changes and indicates if it handled it. It must set
136 /// isCustom if it handles the arg and returns true.
137 typedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
138 EVT &LocVT, CCValAssign::LocInfo &LocInfo,
139 ISD::ArgFlagsTy &ArgFlags, CCState &State);
141 /// CCState - This class holds information needed while lowering arguments and
142 /// return values. It captures which registers are already assigned and which
143 /// stack slots are used. It provides accessors to allocate these values.
145 unsigned CallingConv;
147 const TargetMachine &TM;
148 const TargetRegisterInfo &TRI;
149 SmallVector<CCValAssign, 16> &Locs;
150 LLVMContext &Context;
152 unsigned StackOffset;
153 SmallVector<uint32_t, 16> UsedRegs;
155 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
156 SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
158 void addLoc(const CCValAssign &V) {
162 LLVMContext &getContext() const { return Context; }
163 const TargetMachine &getTarget() const { return TM; }
164 unsigned getCallingConv() const { return CallingConv; }
165 bool isVarArg() const { return IsVarArg; }
167 unsigned getNextStackOffset() const { return StackOffset; }
169 /// isAllocated - Return true if the specified register (or an alias) is
171 bool isAllocated(unsigned Reg) const {
172 return UsedRegs[Reg/32] & (1 << (Reg&31));
175 /// AnalyzeFormalArguments - Analyze an array of argument values,
176 /// incorporating info about the formals into this state.
177 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
180 /// AnalyzeReturn - Analyze the returned values of a return,
181 /// incorporating info about the result values into this state.
182 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
185 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
186 /// incorporating info about the passed values into this state.
187 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
190 /// AnalyzeCallOperands - Same as above except it takes vectors of types
191 /// and argument flags.
192 void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
193 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
196 /// AnalyzeCallResult - Analyze the return values of a call,
197 /// incorporating info about the passed values into this state.
198 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
201 /// AnalyzeCallResult - Same as above except it's specialized for calls which
202 /// produce a single value.
203 void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
205 /// getFirstUnallocated - Return the first unallocated register in the set, or
206 /// NumRegs if they are all allocated.
207 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
208 for (unsigned i = 0; i != NumRegs; ++i)
209 if (!isAllocated(Regs[i]))
214 /// AllocateReg - Attempt to allocate one register. If it is not available,
215 /// return zero. Otherwise, return the register, marking it and any aliases
217 unsigned AllocateReg(unsigned Reg) {
218 if (isAllocated(Reg)) return 0;
223 /// Version of AllocateReg with extra register to be shadowed.
224 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
225 if (isAllocated(Reg)) return 0;
227 MarkAllocated(ShadowReg);
231 /// AllocateReg - Attempt to allocate one of the specified registers. If none
232 /// are available, return zero. Otherwise, return the first one available,
233 /// marking it and any aliases as allocated.
234 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
235 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
236 if (FirstUnalloc == NumRegs)
237 return 0; // Didn't find the reg.
239 // Mark the register and any aliases as allocated.
240 unsigned Reg = Regs[FirstUnalloc];
245 /// Version of AllocateReg with list of registers to be shadowed.
246 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
248 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
249 if (FirstUnalloc == NumRegs)
250 return 0; // Didn't find the reg.
252 // Mark the register and any aliases as allocated.
253 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
255 MarkAllocated(ShadowReg);
259 /// AllocateStack - Allocate a chunk of stack space with the specified size
261 unsigned AllocateStack(unsigned Size, unsigned Align) {
262 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
263 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
264 unsigned Result = StackOffset;
269 // HandleByVal - Allocate a stack slot large enough to pass an argument by
270 // value. The size and alignment information of the argument is encoded in its
271 // parameter attribute.
272 void HandleByVal(unsigned ValNo, EVT ValVT,
273 EVT LocVT, CCValAssign::LocInfo LocInfo,
274 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
277 /// MarkAllocated - Mark a register and all of its aliases as allocated.
278 void MarkAllocated(unsigned Reg);
283 } // end namespace llvm