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 Indirect // The location contains pointer to the value.
39 // TODO: a subset of the value is in the location.
42 /// ValNo - This is the value number begin assigned (e.g. an argument number).
45 /// Loc is either a stack offset or a register number.
48 /// isMem - True if this is a memory loc, false if it is a register loc.
51 /// isCustom - True if this arg/retval requires special handling.
54 /// Information about how the value is assigned.
57 /// ValVT - The type of the value being assigned.
60 /// LocVT - The type of the location being assigned to.
64 static CCValAssign getReg(unsigned ValNo, EVT ValVT,
65 unsigned RegNo, EVT LocVT,
78 static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
79 unsigned RegNo, EVT LocVT,
82 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
87 static CCValAssign getMem(unsigned ValNo, EVT ValVT,
88 unsigned Offset, EVT LocVT,
101 static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
102 unsigned Offset, EVT LocVT,
105 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
110 unsigned getValNo() const { return ValNo; }
111 EVT getValVT() const { return ValVT; }
113 bool isRegLoc() const { return !isMem; }
114 bool isMemLoc() const { return isMem; }
116 bool needsCustom() const { return isCustom; }
118 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
119 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
120 EVT getLocVT() const { return LocVT; }
122 LocInfo getLocInfo() const { return HTP; }
123 bool isExtInLoc() const {
124 return (HTP == AExt || HTP == SExt || HTP == ZExt);
129 /// CCAssignFn - This function assigns a location for Val, updating State to
130 /// reflect the change.
131 typedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
132 EVT LocVT, CCValAssign::LocInfo LocInfo,
133 ISD::ArgFlagsTy ArgFlags, CCState &State);
135 /// CCCustomFn - This function assigns a location for Val, possibly updating
136 /// all args to reflect changes and indicates if it handled it. It must set
137 /// isCustom if it handles the arg and returns true.
138 typedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
139 EVT &LocVT, CCValAssign::LocInfo &LocInfo,
140 ISD::ArgFlagsTy &ArgFlags, CCState &State);
142 /// CCState - This class holds information needed while lowering arguments and
143 /// return values. It captures which registers are already assigned and which
144 /// stack slots are used. It provides accessors to allocate these values.
146 CallingConv::ID CallingConv;
148 const TargetMachine &TM;
149 const TargetRegisterInfo &TRI;
150 SmallVector<CCValAssign, 16> &Locs;
151 LLVMContext &Context;
153 unsigned StackOffset;
154 SmallVector<uint32_t, 16> UsedRegs;
156 CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
157 SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
159 void addLoc(const CCValAssign &V) {
163 LLVMContext &getContext() const { return Context; }
164 const TargetMachine &getTarget() const { return TM; }
165 CallingConv::ID getCallingConv() const { return CallingConv; }
166 bool isVarArg() const { return IsVarArg; }
168 unsigned getNextStackOffset() const { return StackOffset; }
170 /// isAllocated - Return true if the specified register (or an alias) is
172 bool isAllocated(unsigned Reg) const {
173 return UsedRegs[Reg/32] & (1 << (Reg&31));
176 /// AnalyzeFormalArguments - Analyze an array of argument values,
177 /// incorporating info about the formals into this state.
178 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
181 /// AnalyzeReturn - Analyze the returned values of a return,
182 /// incorporating info about the result values into this state.
183 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
186 /// CheckReturn - Analyze the return values of a function, returning
187 /// true if the return can be performed without sret-demotion, and
189 bool CheckReturn(const SmallVectorImpl<EVT> &OutTys,
190 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
193 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
194 /// incorporating info about the passed values into this state.
195 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
198 /// AnalyzeCallOperands - Same as above except it takes vectors of types
199 /// and argument flags.
200 void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
201 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
204 /// AnalyzeCallResult - Analyze the return values of a call,
205 /// incorporating info about the passed values into this state.
206 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
209 /// AnalyzeCallResult - Same as above except it's specialized for calls which
210 /// produce a single value.
211 void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
213 /// getFirstUnallocated - Return the first unallocated register in the set, or
214 /// NumRegs if they are all allocated.
215 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
216 for (unsigned i = 0; i != NumRegs; ++i)
217 if (!isAllocated(Regs[i]))
222 /// AllocateReg - Attempt to allocate one register. If it is not available,
223 /// return zero. Otherwise, return the register, marking it and any aliases
225 unsigned AllocateReg(unsigned Reg) {
226 if (isAllocated(Reg)) return 0;
231 /// Version of AllocateReg with extra register to be shadowed.
232 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
233 if (isAllocated(Reg)) return 0;
235 MarkAllocated(ShadowReg);
239 /// AllocateReg - Attempt to allocate one of the specified registers. If none
240 /// are available, return zero. Otherwise, return the first one available,
241 /// marking it and any aliases as allocated.
242 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
243 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
244 if (FirstUnalloc == NumRegs)
245 return 0; // Didn't find the reg.
247 // Mark the register and any aliases as allocated.
248 unsigned Reg = Regs[FirstUnalloc];
253 /// Version of AllocateReg with list of registers to be shadowed.
254 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
256 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
257 if (FirstUnalloc == NumRegs)
258 return 0; // Didn't find the reg.
260 // Mark the register and any aliases as allocated.
261 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
263 MarkAllocated(ShadowReg);
267 /// AllocateStack - Allocate a chunk of stack space with the specified size
269 unsigned AllocateStack(unsigned Size, unsigned Align) {
270 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
271 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
272 unsigned Result = StackOffset;
277 // HandleByVal - Allocate a stack slot large enough to pass an argument by
278 // value. The size and alignment information of the argument is encoded in its
279 // parameter attribute.
280 void HandleByVal(unsigned ValNo, EVT ValVT,
281 EVT LocVT, CCValAssign::LocInfo LocInfo,
282 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
285 /// MarkAllocated - Mark a register and all of its aliases as allocated.
286 void MarkAllocated(unsigned Reg);
291 } // end namespace llvm