1 //===-- llvm/CallingConvLower.cpp - Calling Convention lowering -----------===//
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 Hexagon_CCState class, used for lowering and
11 // implementing calling conventions. Adapted from the machine independent
12 // version of the class (CCState) but this handles calls to varargs functions
14 //===----------------------------------------------------------------------===//
16 #include "HexagonCallingConvLower.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
27 Hexagon_CCState::Hexagon_CCState(CallingConv::ID CC, bool isVarArg,
28 const TargetMachine &tm,
29 SmallVectorImpl<CCValAssign> &locs,
31 : CallingConv(CC), IsVarArg(isVarArg), TM(tm), Locs(locs), Context(c) {
36 (TM.getSubtargetImpl()->getRegisterInfo()->getNumRegs() + 31) / 32);
39 // HandleByVal - Allocate a stack slot large enough to pass an argument by
40 // value. The size and alignment information of the argument is encoded in its
41 // parameter attribute.
42 void Hexagon_CCState::HandleByVal(unsigned ValNo, EVT ValVT,
43 EVT 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 unsigned Offset = AllocateStack(Size, Align);
54 addLoc(CCValAssign::getMem(ValNo, ValVT.getSimpleVT(), Offset,
55 LocVT.getSimpleVT(), LocInfo));
58 /// MarkAllocated - Mark a register and all of its aliases as allocated.
59 void Hexagon_CCState::MarkAllocated(unsigned Reg) {
60 const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
61 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
62 UsedRegs[*AI/32] |= 1 << (*AI&31);
65 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
66 /// incorporating info about the formals into this state.
68 Hexagon_CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg>
70 Hexagon_CCAssignFn Fn,
71 unsigned SretValueInRegs) {
72 unsigned NumArgs = Ins.size();
75 // If the function returns a small struct in registers, skip
76 // over the first (dummy) argument.
77 if (SretValueInRegs != 0) {
82 for (; i != NumArgs; ++i) {
83 EVT ArgVT = Ins[i].VT;
84 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
85 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this, 0, 0, false)) {
86 dbgs() << "Formal argument #" << i << " has unhandled type "
87 << ArgVT.getEVTString() << "\n";
93 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
94 /// incorporating info about the result values into this state.
96 Hexagon_CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
97 Hexagon_CCAssignFn Fn,
98 unsigned SretValueInRegs) {
100 // For Hexagon, Return small structures in registers.
101 if (SretValueInRegs != 0) {
102 if (SretValueInRegs <= 32) {
103 unsigned Reg = Hexagon::R0;
104 addLoc(CCValAssign::getReg(0, MVT::i32, Reg, MVT::i32,
108 if (SretValueInRegs <= 64) {
109 unsigned Reg = Hexagon::D0;
110 addLoc(CCValAssign::getReg(0, MVT::i64, Reg, MVT::i64,
117 // Determine which register each value should be copied into.
118 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
120 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
121 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this, -1, -1, false)){
122 dbgs() << "Return operand #" << i << " has unhandled type "
123 << VT.getEVTString() << "\n";
130 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
131 /// about the passed values into this state.
133 Hexagon_CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg>
135 Hexagon_CCAssignFn Fn,
136 int NonVarArgsParams,
137 unsigned SretValueSize) {
138 unsigned NumOps = Outs.size();
141 // If the called function returns a small struct in registers, skip
142 // the first actual parameter. We do not want to pass a pointer to
143 // the stack location.
144 if (SretValueSize != 0) {
148 for (; i != NumOps; ++i) {
149 EVT ArgVT = Outs[i].VT;
150 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
151 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this,
152 NonVarArgsParams, i+1, false)) {
153 dbgs() << "Call operand #" << i << " has unhandled type "
154 << ArgVT.getEVTString() << "\n";
160 /// AnalyzeCallOperands - Same as above except it takes vectors of types
161 /// and argument flags.
163 Hexagon_CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
164 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
165 Hexagon_CCAssignFn Fn) {
166 unsigned NumOps = ArgVTs.size();
167 for (unsigned i = 0; i != NumOps; ++i) {
168 EVT ArgVT = ArgVTs[i];
169 ISD::ArgFlagsTy ArgFlags = Flags[i];
170 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this, -1, -1,
172 dbgs() << "Call operand #" << i << " has unhandled type "
173 << ArgVT.getEVTString() << "\n";
179 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
180 /// incorporating info about the passed values into this state.
182 Hexagon_CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
183 Hexagon_CCAssignFn Fn,
184 unsigned SretValueInRegs) {
186 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
188 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
189 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this, -1, -1, false)) {
190 dbgs() << "Call result #" << i << " has unhandled type "
191 << VT.getEVTString() << "\n";
197 /// AnalyzeCallResult - Same as above except it's specialized for calls which
198 /// produce a single value.
199 void Hexagon_CCState::AnalyzeCallResult(EVT VT, Hexagon_CCAssignFn Fn) {
200 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this, -1, -1,
202 dbgs() << "Call result has unhandled type "
203 << VT.getEVTString() << "\n";