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/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/ValueTypes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/Target/TargetCallingConv.h"
26 class TargetRegisterInfo;
30 /// CCValAssign - Represent assignment of one arg/retval to a location.
34 Full, // The value fills the full location.
35 SExt, // The value is sign extended in the location.
36 ZExt, // The value is zero extended in the location.
37 AExt, // The value is extended with undefined upper bits.
38 BCvt, // The value is bit-converted in the location.
39 VExt, // The value is vector-widened in the location.
40 // FIXME: Not implemented yet. Code that uses AExt to mean
41 // vector-widen should be fixed to use VExt instead.
42 Indirect // The location contains pointer to the value.
43 // TODO: a subset of the value is in the location.
46 /// ValNo - This is the value number begin assigned (e.g. an argument number).
49 /// Loc is either a stack offset or a register number.
52 /// isMem - True if this is a memory loc, false if it is a register loc.
55 /// isCustom - True if this arg/retval requires special handling.
56 unsigned isCustom : 1;
58 /// Information about how the value is assigned.
61 /// ValVT - The type of the value being assigned.
64 /// LocVT - The type of the location being assigned to.
68 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
69 unsigned RegNo, MVT LocVT,
82 static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
83 unsigned RegNo, MVT LocVT,
86 Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
91 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
92 unsigned Offset, MVT LocVT,
105 static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
106 unsigned Offset, MVT LocVT,
109 Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
114 unsigned getValNo() const { return ValNo; }
115 MVT getValVT() const { return ValVT; }
117 bool isRegLoc() const { return !isMem; }
118 bool isMemLoc() const { return isMem; }
120 bool needsCustom() const { return isCustom; }
122 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
123 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
124 MVT getLocVT() const { return LocVT; }
126 LocInfo getLocInfo() const { return HTP; }
127 bool isExtInLoc() const {
128 return (HTP == AExt || HTP == SExt || HTP == ZExt);
133 /// CCAssignFn - This function assigns a location for Val, updating State to
134 /// reflect the change. It returns 'true' if it failed to handle Val.
135 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
136 MVT LocVT, CCValAssign::LocInfo LocInfo,
137 ISD::ArgFlagsTy ArgFlags, CCState &State);
139 /// CCCustomFn - This function assigns a location for Val, possibly updating
140 /// all args to reflect changes and indicates if it handled it. It must set
141 /// isCustom if it handles the arg and returns true.
142 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
143 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
144 ISD::ArgFlagsTy &ArgFlags, CCState &State);
146 /// ParmContext - This enum tracks whether calling convention lowering is in
147 /// the context of prologue or call generation. Not all backends make use of
148 /// this information.
149 typedef enum { Unknown, Prologue, Call } ParmContext;
151 /// CCState - This class holds information needed while lowering arguments and
152 /// return values. It captures which registers are already assigned and which
153 /// stack slots are used. It provides accessors to allocate these values.
156 CallingConv::ID CallingConv;
159 const TargetMachine &TM;
160 const TargetRegisterInfo &TRI;
161 SmallVectorImpl<CCValAssign> &Locs;
162 LLVMContext &Context;
164 unsigned StackOffset;
165 SmallVector<uint32_t, 16> UsedRegs;
167 // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
169 // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
171 // Or, in another words it tracks byval parameters that are stored in
172 // general purpose registers.
174 // For 4 byte stack alignment,
175 // instance index means byval parameter number in formal
176 // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
177 // then, for function "foo":
179 // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
181 // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
182 // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
184 // In case of 8 bytes stack alignment,
185 // ByValRegs may also contain information about wasted registers.
186 // In function shown above, r3 would be wasted according to AAPCS rules.
187 // And in that case ByValRegs[1].Waste would be "true".
188 // ByValRegs vector size still would be 2,
189 // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
191 // Supposed use-case for this collection:
192 // 1. Initially ByValRegs is empty, InRegsParamsProceed is 0.
193 // 2. HandleByVal fillups ByValRegs.
194 // 3. Argument analysis (LowerFormatArguments, for example). After
195 // some byval argument was analyzed, InRegsParamsProceed is increased.
197 ByValInfo(unsigned B, unsigned E, bool IsWaste = false) :
198 Begin(B), End(E), Waste(IsWaste) {}
199 // First register allocated for current parameter.
202 // First after last register allocated for current parameter.
205 // Means that current range of registers doesn't belong to any
206 // parameters. It was wasted due to stack alignment rules.
207 // For more information see:
208 // AAPCS, 5.5 Parameter Passing, Stage C, C.3.
211 SmallVector<ByValInfo, 4 > ByValRegs;
213 // InRegsParamsProceed - shows how many instances of ByValRegs was proceed
214 // during argument analysis.
215 unsigned InRegsParamsProceed;
218 ParmContext CallOrPrologue;
221 CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
222 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
225 void addLoc(const CCValAssign &V) {
229 LLVMContext &getContext() const { return Context; }
230 const TargetMachine &getTarget() const { return TM; }
231 MachineFunction &getMachineFunction() const { return MF; }
232 CallingConv::ID getCallingConv() const { return CallingConv; }
233 bool isVarArg() const { return IsVarArg; }
235 unsigned getNextStackOffset() const { return StackOffset; }
237 /// isAllocated - Return true if the specified register (or an alias) is
239 bool isAllocated(unsigned Reg) const {
240 return UsedRegs[Reg/32] & (1 << (Reg&31));
243 /// AnalyzeFormalArguments - Analyze an array of argument values,
244 /// incorporating info about the formals into this state.
245 void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
248 /// AnalyzeReturn - Analyze the returned values of a return,
249 /// incorporating info about the result values into this state.
250 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
253 /// CheckReturn - Analyze the return values of a function, returning
254 /// true if the return can be performed without sret-demotion, and
256 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
259 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
260 /// incorporating info about the passed values into this state.
261 void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
264 /// AnalyzeCallOperands - Same as above except it takes vectors of types
265 /// and argument flags.
266 void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
267 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
270 /// AnalyzeCallResult - Analyze the return values of a call,
271 /// incorporating info about the passed values into this state.
272 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
275 /// AnalyzeCallResult - Same as above except it's specialized for calls which
276 /// produce a single value.
277 void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
279 /// getFirstUnallocated - Return the first unallocated register in the set, or
280 /// NumRegs if they are all allocated.
281 unsigned getFirstUnallocated(const uint16_t *Regs, unsigned NumRegs) const {
282 for (unsigned i = 0; i != NumRegs; ++i)
283 if (!isAllocated(Regs[i]))
288 /// AllocateReg - Attempt to allocate one register. If it is not available,
289 /// return zero. Otherwise, return the register, marking it and any aliases
291 unsigned AllocateReg(unsigned Reg) {
292 if (isAllocated(Reg)) return 0;
297 /// Version of AllocateReg with extra register to be shadowed.
298 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
299 if (isAllocated(Reg)) return 0;
301 MarkAllocated(ShadowReg);
305 /// AllocateReg - Attempt to allocate one of the specified registers. If none
306 /// are available, return zero. Otherwise, return the first one available,
307 /// marking it and any aliases as allocated.
308 unsigned AllocateReg(const uint16_t *Regs, unsigned NumRegs) {
309 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
310 if (FirstUnalloc == NumRegs)
311 return 0; // Didn't find the reg.
313 // Mark the register and any aliases as allocated.
314 unsigned Reg = Regs[FirstUnalloc];
319 /// Version of AllocateReg with list of registers to be shadowed.
320 unsigned AllocateReg(const uint16_t *Regs, const uint16_t *ShadowRegs,
322 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
323 if (FirstUnalloc == NumRegs)
324 return 0; // Didn't find the reg.
326 // Mark the register and any aliases as allocated.
327 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
329 MarkAllocated(ShadowReg);
333 /// AllocateStack - Allocate a chunk of stack space with the specified size
335 unsigned AllocateStack(unsigned Size, unsigned Align) {
336 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
337 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
338 unsigned Result = StackOffset;
340 MF.getFrameInfo()->ensureMaxAlignment(Align);
344 /// Version of AllocateStack with extra register to be shadowed.
345 unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
346 MarkAllocated(ShadowReg);
347 return AllocateStack(Size, Align);
350 // HandleByVal - Allocate a stack slot large enough to pass an argument by
351 // value. The size and alignment information of the argument is encoded in its
352 // parameter attribute.
353 void HandleByVal(unsigned ValNo, MVT ValVT,
354 MVT LocVT, CCValAssign::LocInfo LocInfo,
355 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
357 // Returns count of byval arguments that are to be stored (even partly)
359 unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
361 // Returns count of byval in-regs arguments proceed.
362 unsigned getInRegsParamsProceed() const { return InRegsParamsProceed; }
364 // Get information about N-th byval parameter that is stored in registers.
365 // Here "ByValParamIndex" is N.
366 void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
367 unsigned& BeginReg, unsigned& EndReg) const {
368 assert(InRegsParamRecordIndex < ByValRegs.size() &&
369 "Wrong ByVal parameter index");
371 const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
372 BeginReg = info.Begin;
376 // Add information about parameter that is kept in registers.
377 void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
378 ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
381 // Goes either to next byval parameter (excluding "waste" record), or
382 // to the end of collection.
383 // Returns false, if end is reached.
384 bool nextInRegsParam() {
385 unsigned e = ByValRegs.size();
386 if (InRegsParamsProceed < e)
387 ++InRegsParamsProceed;
388 return InRegsParamsProceed < e;
391 // Clear byval registers tracking info.
392 void clearByValRegsInfo() {
393 InRegsParamsProceed = 0;
397 ParmContext getCallOrPrologue() const { return CallOrPrologue; }
400 /// MarkAllocated - Mark a register and all of its aliases as allocated.
401 void MarkAllocated(unsigned Reg);
406 } // end namespace llvm