1
//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
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 implements the CCState class, used for lowering and implementing
11
// calling conventions.
13
//===----------------------------------------------------------------------===//
15
#include "llvm/CodeGen/CallingConvLower.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/ErrorHandling.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include "llvm/Target/TargetRegisterInfo.h"
20
#include "llvm/Target/TargetData.h"
21
#include "llvm/Target/TargetMachine.h"
24
CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
25
SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
26
: CallingConv(CC), IsVarArg(isVarArg), TM(tm),
27
TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
31
UsedRegs.resize((TRI.getNumRegs()+31)/32);
34
// HandleByVal - Allocate a stack slot large enough to pass an argument by
35
// value. The size and alignment information of the argument is encoded in its
36
// parameter attribute.
37
void CCState::HandleByVal(unsigned ValNo, EVT ValVT,
38
EVT LocVT, CCValAssign::LocInfo LocInfo,
39
int MinSize, int MinAlign,
40
ISD::ArgFlagsTy ArgFlags) {
41
unsigned Align = ArgFlags.getByValAlign();
42
unsigned Size = ArgFlags.getByValSize();
43
if (MinSize > (int)Size)
45
if (MinAlign > (int)Align)
47
unsigned Offset = AllocateStack(Size, Align);
49
addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
52
/// MarkAllocated - Mark a register and all of its aliases as allocated.
53
void CCState::MarkAllocated(unsigned Reg) {
54
UsedRegs[Reg/32] |= 1 << (Reg&31);
56
if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
57
for (; (Reg = *RegAliases); ++RegAliases)
58
UsedRegs[Reg/32] |= 1 << (Reg&31);
61
/// AnalyzeFormalArguments - Analyze an array of argument values,
62
/// incorporating info about the formals into this state.
64
CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
66
unsigned NumArgs = Ins.size();
68
for (unsigned i = 0; i != NumArgs; ++i) {
69
EVT ArgVT = Ins[i].VT;
70
ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
71
if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
73
dbgs() << "Formal argument #" << i << " has unhandled type "
74
<< ArgVT.getEVTString();
81
/// CheckReturn - Analyze the return values of a function, returning true if
82
/// the return can be performed without sret-demotion, and false otherwise.
83
bool CCState::CheckReturn(const SmallVectorImpl<EVT> &OutTys,
84
const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
86
// Determine which register each value should be copied into.
87
for (unsigned i = 0, e = OutTys.size(); i != e; ++i) {
89
ISD::ArgFlagsTy ArgFlags = ArgsFlags[i];
90
if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
96
/// AnalyzeReturn - Analyze the returned values of a return,
97
/// incorporating info about the result values into this state.
98
void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
100
// Determine which register each value should be copied into.
101
for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
102
EVT VT = Outs[i].Val.getValueType();
103
ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
104
if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
106
dbgs() << "Return operand #" << i << " has unhandled type "
107
<< VT.getEVTString();
115
/// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
116
/// incorporating info about the passed values into this state.
117
void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
119
unsigned NumOps = Outs.size();
120
for (unsigned i = 0; i != NumOps; ++i) {
121
EVT ArgVT = Outs[i].Val.getValueType();
122
ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
123
if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
125
dbgs() << "Call operand #" << i << " has unhandled type "
126
<< ArgVT.getEVTString();
133
/// AnalyzeCallOperands - Same as above except it takes vectors of types
134
/// and argument flags.
135
void CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
136
SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
138
unsigned NumOps = ArgVTs.size();
139
for (unsigned i = 0; i != NumOps; ++i) {
140
EVT ArgVT = ArgVTs[i];
141
ISD::ArgFlagsTy ArgFlags = Flags[i];
142
if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
144
dbgs() << "Call operand #" << i << " has unhandled type "
145
<< ArgVT.getEVTString();
152
/// AnalyzeCallResult - Analyze the return values of a call,
153
/// incorporating info about the passed values into this state.
154
void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
156
for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
158
ISD::ArgFlagsTy Flags = Ins[i].Flags;
159
if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
161
dbgs() << "Call result #" << i << " has unhandled type "
162
<< VT.getEVTString();
169
/// AnalyzeCallResult - Same as above except it's specialized for calls which
170
/// produce a single value.
171
void CCState::AnalyzeCallResult(EVT VT, CCAssignFn Fn) {
172
if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
174
dbgs() << "Call result has unhandled type "
175
<< VT.getEVTString();