1 //===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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.
51 MVT::ValueType ValVT : 8;
53 /// LocVT - The type of the location being assigned to.
54 MVT::ValueType LocVT : 8;
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;
108 const TargetMachine &TM;
109 const MRegisterInfo &MRI;
110 SmallVector<CCValAssign, 16> &Locs;
112 unsigned StackOffset;
113 SmallVector<uint32_t, 16> UsedRegs;
115 CCState(unsigned CC, const TargetMachine &TM,
116 SmallVector<CCValAssign, 16> &locs);
118 void addLoc(const CCValAssign &V) {
122 const TargetMachine &getTarget() const { return TM; }
123 unsigned getCallingConv() const { return CallingConv; }
125 unsigned getNextStackOffset() const { return StackOffset; }
127 /// isAllocated - Return true if the specified register (or an alias) is
129 bool isAllocated(unsigned Reg) const {
130 return UsedRegs[Reg/32] & (1 << (Reg&31));
133 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
134 /// incorporating info about the formals into this state.
135 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
137 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
138 /// incorporating info about the result values into this state.
139 void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
141 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
142 /// about the passed values into this state.
143 void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
145 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
146 /// incorporating info about the passed values into this state.
147 void AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn);
150 /// getFirstUnallocated - Return the first unallocated register in the set, or
151 /// NumRegs if they are all allocated.
152 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
153 for (unsigned i = 0; i != NumRegs; ++i)
154 if (!isAllocated(Regs[i]))
159 /// AllocateReg - Attempt to allocate one register. If it is not available,
160 /// return zero. Otherwise, return the register, marking it and any aliases
162 unsigned AllocateReg(unsigned Reg) {
163 if (isAllocated(Reg)) return 0;
168 /// AllocateReg - Attempt to allocate one of the specified registers. If none
169 /// are available, return zero. Otherwise, return the first one available,
170 /// marking it and any aliases as allocated.
171 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
172 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
173 if (FirstUnalloc == NumRegs)
174 return 0; // Didn't find the reg.
176 // Mark the register and any aliases as allocated.
177 unsigned Reg = Regs[FirstUnalloc];
182 /// AllocateStack - Allocate a chunk of stack space with the specified size
184 unsigned AllocateStack(unsigned Size, unsigned Align) {
185 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
186 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
187 unsigned Result = StackOffset;
192 /// MarkAllocated - Mark a register and all of its aliases as allocated.
193 void MarkAllocated(unsigned Reg);
198 } // end namespace llvm