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 // TODO: a subset of the value is in the location.
40 /// ValNo - This is the value number begin assigned (e.g. an argument number).
43 /// Loc is either a stack offset or a register number.
46 /// isMem - True if this is a memory loc, false if it is a register loc.
49 /// isCustom - True if this arg/retval requires special handling.
52 /// Information about how the value is assigned.
55 /// ValVT - The type of the value being assigned.
58 /// LocVT - The type of the location being assigned to.
62 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
63 unsigned RegNo, MVT LocVT,
76 static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
77 unsigned RegNo, MVT LocVT,
80 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
85 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
86 unsigned Offset, MVT LocVT,
99 static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
100 unsigned Offset, MVT LocVT,
103 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
108 unsigned getValNo() const { return ValNo; }
109 MVT getValVT() const { return ValVT; }
111 bool isRegLoc() const { return !isMem; }
112 bool isMemLoc() const { return isMem; }
114 bool needsCustom() const { return isCustom; }
116 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
117 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
118 MVT getLocVT() const { return LocVT; }
120 LocInfo getLocInfo() const { return HTP; }
123 /// CCAssignFn - This function assigns a location for Val, updating State to
124 /// reflect the change.
125 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
126 MVT LocVT, CCValAssign::LocInfo LocInfo,
127 ISD::ArgFlagsTy ArgFlags, CCState &State);
129 /// CCCustomFn - This function assigns a location for Val, possibly updating
130 /// all args to reflect changes and indicates if it handled it. It must set
131 /// isCustom if it handles the arg and returns true.
132 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
133 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
134 ISD::ArgFlagsTy &ArgFlags, CCState &State);
136 /// CCState - This class holds information needed while lowering arguments and
137 /// return values. It captures which registers are already assigned and which
138 /// stack slots are used. It provides accessors to allocate these values.
140 unsigned CallingConv;
142 const TargetMachine &TM;
143 const TargetRegisterInfo &TRI;
144 SmallVector<CCValAssign, 16> &Locs;
145 LLVMContext *Context;
147 unsigned StackOffset;
148 SmallVector<uint32_t, 16> UsedRegs;
150 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
151 SmallVector<CCValAssign, 16> &locs, LLVMContext *C);
153 void addLoc(const CCValAssign &V) {
157 LLVMContext *getContext() const { return Context; }
158 const TargetMachine &getTarget() const { return TM; }
159 unsigned getCallingConv() const { return CallingConv; }
160 bool isVarArg() const { return IsVarArg; }
162 unsigned getNextStackOffset() const { return StackOffset; }
164 /// isAllocated - Return true if the specified register (or an alias) is
166 bool isAllocated(unsigned Reg) const {
167 return UsedRegs[Reg/32] & (1 << (Reg&31));
170 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
171 /// incorporating info about the formals into this state.
172 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
174 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
175 /// incorporating info about the result values into this state.
176 void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
178 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
179 /// about the passed values into this state.
180 void AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn);
182 /// AnalyzeCallOperands - Same as above except it takes vectors of types
183 /// and argument flags.
184 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
185 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
188 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
189 /// incorporating info about the passed values into this state.
190 void AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn);
192 /// AnalyzeCallResult - Same as above except it's specialized for calls which
193 /// produce a single value.
194 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
196 /// getFirstUnallocated - Return the first unallocated register in the set, or
197 /// NumRegs if they are all allocated.
198 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
199 for (unsigned i = 0; i != NumRegs; ++i)
200 if (!isAllocated(Regs[i]))
205 /// AllocateReg - Attempt to allocate one register. If it is not available,
206 /// return zero. Otherwise, return the register, marking it and any aliases
208 unsigned AllocateReg(unsigned Reg) {
209 if (isAllocated(Reg)) return 0;
214 /// Version of AllocateReg with extra register to be shadowed.
215 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
216 if (isAllocated(Reg)) return 0;
218 MarkAllocated(ShadowReg);
222 /// AllocateReg - Attempt to allocate one of the specified registers. If none
223 /// are available, return zero. Otherwise, return the first one available,
224 /// marking it and any aliases as allocated.
225 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
226 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
227 if (FirstUnalloc == NumRegs)
228 return 0; // Didn't find the reg.
230 // Mark the register and any aliases as allocated.
231 unsigned Reg = Regs[FirstUnalloc];
236 /// Version of AllocateReg with list of registers to be shadowed.
237 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
239 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
240 if (FirstUnalloc == NumRegs)
241 return 0; // Didn't find the reg.
243 // Mark the register and any aliases as allocated.
244 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
246 MarkAllocated(ShadowReg);
250 /// AllocateStack - Allocate a chunk of stack space with the specified size
252 unsigned AllocateStack(unsigned Size, unsigned Align) {
253 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
254 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
255 unsigned Result = StackOffset;
260 // HandleByVal - Allocate a stack slot large enough to pass an argument by
261 // value. The size and alignment information of the argument is encoded in its
262 // parameter attribute.
263 void HandleByVal(unsigned ValNo, MVT ValVT,
264 MVT LocVT, CCValAssign::LocInfo LocInfo,
265 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
268 /// MarkAllocated - Mark a register and all of its aliases as allocated.
269 void MarkAllocated(unsigned Reg);
274 } // end namespace llvm