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/CodeGen/MachineFrameInfo.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetLowering.h"
26 CCState::CCState(CallingConv::ID CC
, bool isVarArg
, MachineFunction
&mf
,
27 const TargetMachine
&tm
, SmallVector
<CCValAssign
, 16> &locs
,
29 : CallingConv(CC
), IsVarArg(isVarArg
), MF(mf
), TM(tm
),
30 TRI(*TM
.getRegisterInfo()), Locs(locs
), Context(C
),
31 CallOrPrologue(Unknown
) {
36 UsedRegs
.resize((TRI
.getNumRegs()+31)/32);
39 // HandleByVal - Allocate space on the stack large enough to pass an argument
40 // by value. The size and alignment information of the argument is encoded in
41 // its parameter attribute.
42 void CCState::HandleByVal(unsigned ValNo
, MVT ValVT
,
43 MVT LocVT
, CCValAssign::LocInfo LocInfo
,
44 int MinSize
, int MinAlign
,
45 ISD::ArgFlagsTy ArgFlags
) {
46 unsigned Align
= ArgFlags
.getByValAlign();
47 unsigned Size
= ArgFlags
.getByValSize();
48 if (MinSize
> (int)Size
)
50 if (MinAlign
> (int)Align
)
52 if (MF
.getFrameInfo()->getMaxAlignment() < Align
)
53 MF
.getFrameInfo()->setMaxAlignment(Align
);
54 TM
.getTargetLowering()->HandleByVal(this, Size
);
55 unsigned Offset
= AllocateStack(Size
, Align
);
56 addLoc(CCValAssign::getMem(ValNo
, ValVT
, Offset
, LocVT
, LocInfo
));
59 /// MarkAllocated - Mark a register and all of its aliases as allocated.
60 void CCState::MarkAllocated(unsigned Reg
) {
61 for (const unsigned *Alias
= TRI
.getOverlaps(Reg
);
62 unsigned Reg
= *Alias
; ++Alias
)
63 UsedRegs
[Reg
/32] |= 1 << (Reg
&31);
66 /// AnalyzeFormalArguments - Analyze an array of argument values,
67 /// incorporating info about the formals into this state.
69 CCState::AnalyzeFormalArguments(const SmallVectorImpl
<ISD::InputArg
> &Ins
,
71 unsigned NumArgs
= Ins
.size();
73 for (unsigned i
= 0; i
!= NumArgs
; ++i
) {
74 MVT ArgVT
= Ins
[i
].VT
;
75 ISD::ArgFlagsTy ArgFlags
= Ins
[i
].Flags
;
76 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
78 dbgs() << "Formal argument #" << i
<< " has unhandled type "
79 << EVT(ArgVT
).getEVTString();
86 /// CheckReturn - Analyze the return values of a function, returning true if
87 /// the return can be performed without sret-demotion, and false otherwise.
88 bool CCState::CheckReturn(const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
90 // Determine which register each value should be copied into.
91 for (unsigned i
= 0, e
= Outs
.size(); i
!= e
; ++i
) {
93 ISD::ArgFlagsTy ArgFlags
= Outs
[i
].Flags
;
94 if (Fn(i
, VT
, VT
, CCValAssign::Full
, ArgFlags
, *this))
100 /// AnalyzeReturn - Analyze the returned values of a return,
101 /// incorporating info about the result values into this state.
102 void CCState::AnalyzeReturn(const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
104 // Determine which register each value should be copied into.
105 for (unsigned i
= 0, e
= Outs
.size(); i
!= e
; ++i
) {
107 ISD::ArgFlagsTy ArgFlags
= Outs
[i
].Flags
;
108 if (Fn(i
, VT
, VT
, CCValAssign::Full
, ArgFlags
, *this)) {
110 dbgs() << "Return operand #" << i
<< " has unhandled type "
111 << EVT(VT
).getEVTString();
118 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
119 /// incorporating info about the passed values into this state.
120 void CCState::AnalyzeCallOperands(const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
122 unsigned NumOps
= Outs
.size();
123 for (unsigned i
= 0; i
!= NumOps
; ++i
) {
124 MVT ArgVT
= Outs
[i
].VT
;
125 ISD::ArgFlagsTy ArgFlags
= Outs
[i
].Flags
;
126 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
128 dbgs() << "Call operand #" << i
<< " has unhandled type "
129 << EVT(ArgVT
).getEVTString();
136 /// AnalyzeCallOperands - Same as above except it takes vectors of types
137 /// and argument flags.
138 void CCState::AnalyzeCallOperands(SmallVectorImpl
<MVT
> &ArgVTs
,
139 SmallVectorImpl
<ISD::ArgFlagsTy
> &Flags
,
141 unsigned NumOps
= ArgVTs
.size();
142 for (unsigned i
= 0; i
!= NumOps
; ++i
) {
143 MVT ArgVT
= ArgVTs
[i
];
144 ISD::ArgFlagsTy ArgFlags
= Flags
[i
];
145 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
147 dbgs() << "Call operand #" << i
<< " has unhandled type "
148 << EVT(ArgVT
).getEVTString();
155 /// AnalyzeCallResult - Analyze the return values of a call,
156 /// incorporating info about the passed values into this state.
157 void CCState::AnalyzeCallResult(const SmallVectorImpl
<ISD::InputArg
> &Ins
,
159 for (unsigned i
= 0, e
= Ins
.size(); i
!= e
; ++i
) {
161 ISD::ArgFlagsTy Flags
= Ins
[i
].Flags
;
162 if (Fn(i
, VT
, VT
, CCValAssign::Full
, Flags
, *this)) {
164 dbgs() << "Call result #" << i
<< " has unhandled type "
165 << EVT(VT
).getEVTString() << "\n";
172 /// AnalyzeCallResult - Same as above except it's specialized for calls which
173 /// produce a single value.
174 void CCState::AnalyzeCallResult(MVT VT
, CCAssignFn Fn
) {
175 if (Fn(0, VT
, VT
, CCValAssign::Full
, ISD::ArgFlagsTy(), *this)) {
177 dbgs() << "Call result has unhandled type "
178 << EVT(VT
).getEVTString();