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 // TODO: a subset of the value is in the location.
39 /// ValNo - This is the value number begin assigned (e.g. an argument number).
42 /// Loc is either a stack offset or a register number.
45 /// isMem - True if this is a memory loc, false if it is a register loc.
48 /// Information about how the value is assigned.
51 /// ValVT - The type of the value being assigned.
54 /// LocVT - The type of the location being assigned to.
58 static CCValAssign getReg(unsigned ValNo, MVT ValVT,
59 unsigned RegNo, MVT LocVT,
70 static CCValAssign getMem(unsigned ValNo, MVT ValVT,
71 unsigned Offset, MVT LocVT,
83 unsigned getValNo() const { return ValNo; }
84 MVT getValVT() const { return ValVT; }
86 bool isRegLoc() const { return !isMem; }
87 bool isMemLoc() const { return isMem; }
89 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
90 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
91 MVT getLocVT() const { return LocVT; }
93 LocInfo getLocInfo() const { return HTP; }
97 /// CCAssignFn - This function assigns a location for Val, updating State to
98 /// reflect the change.
99 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
100 MVT LocVT, CCValAssign::LocInfo LocInfo,
101 ISD::ArgFlagsTy ArgFlags, CCState &State);
104 /// CCState - This class holds information needed while lowering arguments and
105 /// return values. It captures which registers are already assigned and which
106 /// stack slots are used. It provides accessors to allocate these values.
108 unsigned CallingConv;
110 const TargetMachine &TM;
111 const TargetRegisterInfo &TRI;
112 SmallVector<CCValAssign, 16> &Locs;
114 unsigned StackOffset;
115 SmallVector<uint32_t, 16> UsedRegs;
117 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
118 SmallVector<CCValAssign, 16> &locs);
120 void addLoc(const CCValAssign &V) {
124 const TargetMachine &getTarget() const { return TM; }
125 unsigned getCallingConv() const { return CallingConv; }
126 bool isVarArg() const { return IsVarArg; }
128 unsigned getNextStackOffset() const { return StackOffset; }
130 /// isAllocated - Return true if the specified register (or an alias) is
132 bool isAllocated(unsigned Reg) const {
133 return UsedRegs[Reg/32] & (1 << (Reg&31));
136 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
137 /// incorporating info about the formals into this state.
138 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
140 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
141 /// incorporating info about the result values into this state.
142 void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
144 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
145 /// about the passed values into this state.
146 void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
148 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
149 /// incorporating info about the passed values into this state.
150 void AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn);
153 /// getFirstUnallocated - Return the first unallocated register in the set, or
154 /// NumRegs if they are all allocated.
155 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
156 for (unsigned i = 0; i != NumRegs; ++i)
157 if (!isAllocated(Regs[i]))
162 /// AllocateReg - Attempt to allocate one register. If it is not available,
163 /// return zero. Otherwise, return the register, marking it and any aliases
165 unsigned AllocateReg(unsigned Reg) {
166 if (isAllocated(Reg)) return 0;
171 /// Version of AllocateReg with extra register to be shadowed.
172 unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
173 if (isAllocated(Reg)) return 0;
175 MarkAllocated(ShadowReg);
179 /// AllocateReg - Attempt to allocate one of the specified registers. If none
180 /// are available, return zero. Otherwise, return the first one available,
181 /// marking it and any aliases as allocated.
182 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
183 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
184 if (FirstUnalloc == NumRegs)
185 return 0; // Didn't find the reg.
187 // Mark the register and any aliases as allocated.
188 unsigned Reg = Regs[FirstUnalloc];
193 /// Version of AllocateReg with list of registers to be shadowed.
194 unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
196 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
197 if (FirstUnalloc == NumRegs)
198 return 0; // Didn't find the reg.
200 // Mark the register and any aliases as allocated.
201 unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
203 MarkAllocated(ShadowReg);
207 /// AllocateStack - Allocate a chunk of stack space with the specified size
209 unsigned AllocateStack(unsigned Size, unsigned Align) {
210 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
211 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
212 unsigned Result = StackOffset;
217 // HandleByVal - Allocate a stack slot large enough to pass an argument by
218 // value. The size and alignment information of the argument is encoded in its
219 // parameter attribute.
220 void HandleByVal(unsigned ValNo, MVT ValVT,
221 MVT LocVT, CCValAssign::LocInfo LocInfo,
222 int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
225 /// MarkAllocated - Mark a register and all of its aliases as allocated.
226 void MarkAllocated(unsigned Reg);
231 } // end namespace llvm