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"
27 /// CCValAssign - Represent assignment of one arg/retval to a location.
31 Full, // The value fills the full location.
32 SExt, // The value is sign extended in the location.
33 ZExt, // The value is zero extended in the location.
34 AExt // The value is extended with undefined upper bits.
35 // TODO: a subset of the value is in the location.
38 /// ValNo - This is the value number begin assigned (e.g. an argument number).
41 /// Loc is either a stack offset or a register number.
44 /// isMem - True if this is a memory loc, false if it is a register loc.
47 /// Information about how the value is assigned.
50 /// ValVT - The type of the value being assigned.
53 /// LocVT - The type of the location being assigned to.
57 static CCValAssign getReg(unsigned ValNo, MVT::ValueType ValVT,
58 unsigned RegNo, MVT::ValueType LocVT,
69 static CCValAssign getMem(unsigned ValNo, MVT::ValueType ValVT,
70 unsigned Offset, MVT::ValueType LocVT,
82 unsigned getValNo() const { return ValNo; }
83 MVT::ValueType getValVT() const { return ValVT; }
85 bool isRegLoc() const { return !isMem; }
86 bool isMemLoc() const { return isMem; }
88 unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
89 unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
90 MVT::ValueType getLocVT() const { return LocVT; }
92 LocInfo getLocInfo() const { return HTP; }
96 /// CCAssignFn - This function assigns a location for Val, updating State to
97 /// reflect the change.
98 typedef bool CCAssignFn(unsigned ValNo, MVT::ValueType ValVT,
99 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
100 unsigned ArgFlags, CCState &State);
103 /// CCState - This class holds information needed while lowering arguments and
104 /// return values. It captures which registers are already assigned and which
105 /// stack slots are used. It provides accessors to allocate these values.
107 unsigned CallingConv;
109 const TargetMachine &TM;
110 const MRegisterInfo &MRI;
111 SmallVector<CCValAssign, 16> &Locs;
113 unsigned StackOffset;
114 SmallVector<uint32_t, 16> UsedRegs;
116 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
117 SmallVector<CCValAssign, 16> &locs);
119 void addLoc(const CCValAssign &V) {
123 const TargetMachine &getTarget() const { return TM; }
124 unsigned getCallingConv() const { return CallingConv; }
125 bool isVarArg() const { return IsVarArg; }
127 unsigned getNextStackOffset() const { return StackOffset; }
129 /// isAllocated - Return true if the specified register (or an alias) is
131 bool isAllocated(unsigned Reg) const {
132 return UsedRegs[Reg/32] & (1 << (Reg&31));
135 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
136 /// incorporating info about the formals into this state.
137 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
139 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
140 /// incorporating info about the result values into this state.
141 void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
143 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
144 /// about the passed values into this state.
145 void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
147 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
148 /// incorporating info about the passed values into this state.
149 void AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn);
152 /// getFirstUnallocated - Return the first unallocated register in the set, or
153 /// NumRegs if they are all allocated.
154 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
155 for (unsigned i = 0; i != NumRegs; ++i)
156 if (!isAllocated(Regs[i]))
161 /// AllocateReg - Attempt to allocate one register. If it is not available,
162 /// return zero. Otherwise, return the register, marking it and any aliases
164 unsigned AllocateReg(unsigned Reg) {
165 if (isAllocated(Reg)) return 0;
170 /// AllocateReg - Attempt to allocate one of the specified registers. If none
171 /// are available, return zero. Otherwise, return the first one available,
172 /// marking it and any aliases as allocated.
173 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
174 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
175 if (FirstUnalloc == NumRegs)
176 return 0; // Didn't find the reg.
178 // Mark the register and any aliases as allocated.
179 unsigned Reg = Regs[FirstUnalloc];
184 /// AllocateStack - Allocate a chunk of stack space with the specified size
186 unsigned AllocateStack(unsigned Size, unsigned Align) {
187 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
188 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
189 unsigned Result = StackOffset;
194 void HandleStruct(unsigned ValNo, MVT::ValueType ValVT,
195 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
198 /// MarkAllocated - Mark a register and all of its aliases as allocated.
199 void MarkAllocated(unsigned Reg);
204 } // end namespace llvm