blob: 9289711e6ef5d372602461e46343f80ada805ec1 [file] [log] [blame]
Dan Gohman456e2812009-03-31 16:51:18 +00001//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
Chris Lattner362e98a2007-02-27 04:43:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner362e98a2007-02-27 04:43:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CCState class, used for lowering and implementing
11// calling conventions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/CallingConvLower.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000016#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000018#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengc67e6e82008-01-15 03:14:05 +000019#include "llvm/Target/TargetData.h"
Chris Lattner13513b72007-02-27 05:13:54 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000021using namespace llvm;
22
Chris Lattneraeeccfc2007-06-19 00:11:09 +000023CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
Owen Andersond1474d02009-07-09 17:57:24 +000024 SmallVector<CCValAssign, 16> &locs, LLVMContext *C)
Chris Lattneraeeccfc2007-06-19 00:11:09 +000025 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
Owen Andersond1474d02009-07-09 17:57:24 +000026 TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
Chris Lattner362e98a2007-02-27 04:43:02 +000027 // No stack is used.
28 StackOffset = 0;
29
Dan Gohman117b64b2008-06-30 20:25:31 +000030 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattner362e98a2007-02-27 04:43:02 +000031}
32
Evan Cheng4c46fc62008-01-15 07:49:36 +000033// HandleByVal - Allocate a stack slot large enough to pass an argument by
34// value. The size and alignment information of the argument is encoded in its
35// parameter attribute.
Duncan Sands83ec4b62008-06-06 12:08:01 +000036void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
37 MVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Cheng4c46fc62008-01-15 07:49:36 +000038 int MinSize, int MinAlign,
Duncan Sands276dcbd2008-03-21 09:14:45 +000039 ISD::ArgFlagsTy ArgFlags) {
40 unsigned Align = ArgFlags.getByValAlign();
41 unsigned Size = ArgFlags.getByValSize();
Evan Cheng4c46fc62008-01-15 07:49:36 +000042 if (MinSize > (int)Size)
43 Size = MinSize;
44 if (MinAlign > (int)Align)
45 Align = MinAlign;
46 unsigned Offset = AllocateStack(Size, Align);
Rafael Espindola594d37e2007-08-10 14:44:42 +000047
48 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
49}
Chris Lattner362e98a2007-02-27 04:43:02 +000050
51/// MarkAllocated - Mark a register and all of its aliases as allocated.
52void CCState::MarkAllocated(unsigned Reg) {
53 UsedRegs[Reg/32] |= 1 << (Reg&31);
54
Dan Gohman6f0d0242008-02-10 18:45:23 +000055 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
Chris Lattner362e98a2007-02-27 04:43:02 +000056 for (; (Reg = *RegAliases); ++RegAliases)
57 UsedRegs[Reg/32] |= 1 << (Reg&31);
58}
Chris Lattnerfb39f992007-02-28 06:56:37 +000059
Chris Lattner66baf262007-02-28 07:09:40 +000060/// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
61/// incorporating info about the formals into this state.
62void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
63 unsigned NumArgs = TheArgs->getNumValues()-1;
64
65 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000066 MVT ArgVT = TheArgs->getValueType(i);
Duncan Sands276dcbd2008-03-21 09:14:45 +000067 ISD::ArgFlagsTy ArgFlags =
68 cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
Chris Lattner66baf262007-02-28 07:09:40 +000069 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwin7d696d82009-07-11 13:10:19 +000070 std::string msg;
71 raw_string_ostream Msg(msg);
72 Msg << "Formal argument #" << i << " has unhandled type "
73 << ArgVT.getMVTString();
74 llvm_report_error(Msg.str());
Chris Lattner66baf262007-02-28 07:09:40 +000075 }
76 }
77}
78
79/// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
80/// incorporating info about the result values into this state.
81void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
82 // Determine which register each value should be copied into.
83 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000084 MVT VT = TheRet->getOperand(i*2+1).getValueType();
Duncan Sands276dcbd2008-03-21 09:14:45 +000085 ISD::ArgFlagsTy ArgFlags =
86 cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
87 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
Torok Edwin7d696d82009-07-11 13:10:19 +000088 std::string msg;
89 raw_string_ostream Msg(msg);
90 Msg << "Return operand #" << i << " has unhandled type "
91 << VT.getMVTString();
92 llvm_report_error(Msg.str());
Chris Lattner66baf262007-02-28 07:09:40 +000093 }
94 }
95}
96
97
Chris Lattnerfb39f992007-02-28 06:56:37 +000098/// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
99/// about the passed values into this state.
Dan Gohman095cc292008-09-13 01:54:27 +0000100void CCState::AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn) {
101 unsigned NumOps = TheCall->getNumArgs();
Chris Lattnerfb39f992007-02-28 06:56:37 +0000102 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +0000103 MVT ArgVT = TheCall->getArg(i).getValueType();
104 ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
Chris Lattnerfb39f992007-02-28 06:56:37 +0000105 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000106 std::string msg;
107 raw_string_ostream Msg(msg);
108 Msg << "Call operand #" << i << " has unhandled type "
109 << ArgVT.getMVTString();
110 llvm_report_error(Msg.str());
Chris Lattnerfb39f992007-02-28 06:56:37 +0000111 }
112 }
113}
114
Evan Chengc89d2fe2008-09-05 16:59:26 +0000115/// AnalyzeCallOperands - Same as above except it takes vectors of types
116/// and argument flags.
Evan Chengc7fcfa02008-09-07 09:02:18 +0000117void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Chengc89d2fe2008-09-05 16:59:26 +0000118 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
119 CCAssignFn Fn) {
120 unsigned NumOps = ArgVTs.size();
121 for (unsigned i = 0; i != NumOps; ++i) {
122 MVT ArgVT = ArgVTs[i];
123 ISD::ArgFlagsTy ArgFlags = Flags[i];
124 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000125 std::string msg;
126 raw_string_ostream Msg(msg);
127 Msg << "Call operand #" << i << " has unhandled type "
128 << ArgVT.getMVTString();
129 llvm_report_error(Msg.str());
Evan Chengc89d2fe2008-09-05 16:59:26 +0000130 }
131 }
132}
133
Chris Lattner66baf262007-02-28 07:09:40 +0000134/// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
135/// incorporating info about the passed values into this state.
Dan Gohman095cc292008-09-13 01:54:27 +0000136void CCState::AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn) {
137 for (unsigned i = 0, e = TheCall->getNumRetVals(); i != e; ++i) {
138 MVT VT = TheCall->getRetValType(i);
Dale Johannesen86098bd2008-09-26 19:31:26 +0000139 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
140 if (TheCall->isInreg())
141 Flags.setInReg();
142 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000143 std::string msg;
144 raw_string_ostream Msg(msg);
145 Msg << "Call result #" << i << " has unhandled type "
146 << VT.getMVTString();
147 llvm_report_error(Msg.str());
Chris Lattnerfb39f992007-02-28 06:56:37 +0000148 }
149 }
150}
Evan Chengc7fcfa02008-09-07 09:02:18 +0000151
152/// AnalyzeCallResult - Same as above except it's specialized for calls which
153/// produce a single value.
154void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
155 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000156 std::string msg;
157 raw_string_ostream Msg(msg);
158 Msg << "Call result has unhandled type "
159 << VT.getMVTString();
160 llvm_report_error(Msg.str());
Evan Chengc7fcfa02008-09-07 09:02:18 +0000161 }
162}