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 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
134 /// about the passed values into this state.
135 void AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn);
137 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
138 /// incorporating info about the formals into this state.
139 void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
142 /// getFirstUnallocated - Return the first unallocated register in the set, or
143 /// NumRegs if they are all allocated.
144 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
145 for (unsigned i = 0; i != NumRegs; ++i)
146 if (!isAllocated(Regs[i]))
151 /// AllocateReg - Attempt to allocate one register. If it is not available,
152 /// return zero. Otherwise, return the register, marking it and any aliases
154 unsigned AllocateReg(unsigned Reg) {
155 if (isAllocated(Reg)) return 0;
160 /// AllocateReg - Attempt to allocate one of the specified registers. If none
161 /// are available, return zero. Otherwise, return the first one available,
162 /// marking it and any aliases as allocated.
163 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
164 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
165 if (FirstUnalloc == NumRegs)
166 return 0; // Didn't find the reg.
168 // Mark the register and any aliases as allocated.
169 unsigned Reg = Regs[FirstUnalloc];
174 /// AllocateStack - Allocate a chunk of stack space with the specified size
176 unsigned AllocateStack(unsigned Size, unsigned Align) {
177 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
178 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
179 unsigned Result = StackOffset;
184 /// MarkAllocated - Mark a register and all of its aliases as allocated.
185 void MarkAllocated(unsigned Reg);
190 } // end namespace llvm