blob: d6137a538b334262f7478a6f4f910f517d424495 [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 Andersone922c022009-07-22 00:24:57 +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
Dan Gohman98ca4f22009-08-05 01:29:28 +000060/// AnalyzeFormalArguments - Analyze an array of argument values,
Chris Lattner66baf262007-02-28 07:09:40 +000061/// incorporating info about the formals into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +000062void
63CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
64 CCAssignFn Fn) {
65 unsigned NumArgs = Ins.size();
66
Chris Lattner66baf262007-02-28 07:09:40 +000067 for (unsigned i = 0; i != NumArgs; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +000068 MVT ArgVT = Ins[i].VT;
69 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner66baf262007-02-28 07:09:40 +000070 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +000071#ifndef NDEBUG
72 cerr << "Formal argument #" << i << " has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +000073 << ArgVT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +000074#endif
75 llvm_unreachable(0);
Chris Lattner66baf262007-02-28 07:09:40 +000076 }
77 }
78}
79
Dan Gohman98ca4f22009-08-05 01:29:28 +000080/// AnalyzeReturn - Analyze the returned values of a return,
Chris Lattner66baf262007-02-28 07:09:40 +000081/// incorporating info about the result values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +000082void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
83 CCAssignFn Fn) {
Chris Lattner66baf262007-02-28 07:09:40 +000084 // Determine which register each value should be copied into.
Dan Gohman98ca4f22009-08-05 01:29:28 +000085 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
86 MVT VT = Outs[i].Val.getValueType();
87 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Torok Edwinc23197a2009-07-14 16:55:14 +000088 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
89#ifndef NDEBUG
90 cerr << "Return operand #" << i << " has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +000091 << VT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +000092#endif
93 llvm_unreachable(0);
Chris Lattner66baf262007-02-28 07:09:40 +000094 }
95 }
96}
97
98
Dan Gohman98ca4f22009-08-05 01:29:28 +000099/// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
100/// incorporating info about the passed values into this state.
101void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
102 CCAssignFn Fn) {
103 unsigned NumOps = Outs.size();
Chris Lattnerfb39f992007-02-28 06:56:37 +0000104 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000105 MVT ArgVT = Outs[i].Val.getValueType();
106 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Chris Lattnerfb39f992007-02-28 06:56:37 +0000107 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000108#ifndef NDEBUG
109 cerr << "Call operand #" << i << " has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +0000110 << ArgVT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000111#endif
112 llvm_unreachable(0);
Chris Lattnerfb39f992007-02-28 06:56:37 +0000113 }
114 }
115}
116
Evan Chengc89d2fe2008-09-05 16:59:26 +0000117/// AnalyzeCallOperands - Same as above except it takes vectors of types
118/// and argument flags.
Evan Chengc7fcfa02008-09-07 09:02:18 +0000119void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Chengc89d2fe2008-09-05 16:59:26 +0000120 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
121 CCAssignFn Fn) {
122 unsigned NumOps = ArgVTs.size();
123 for (unsigned i = 0; i != NumOps; ++i) {
124 MVT ArgVT = ArgVTs[i];
125 ISD::ArgFlagsTy ArgFlags = Flags[i];
126 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000127#ifndef NDEBUG
128 cerr << "Call operand #" << i << " has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +0000129 << ArgVT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000130#endif
131 llvm_unreachable(0);
Evan Chengc89d2fe2008-09-05 16:59:26 +0000132 }
133 }
134}
135
Dan Gohman98ca4f22009-08-05 01:29:28 +0000136/// AnalyzeCallResult - Analyze the return values of a call,
Chris Lattner66baf262007-02-28 07:09:40 +0000137/// incorporating info about the passed values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000138void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
139 CCAssignFn Fn) {
140 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
141 MVT VT = Ins[i].VT;
142 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen86098bd2008-09-26 19:31:26 +0000143 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000144#ifndef NDEBUG
145 cerr << "Call result #" << i << " has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +0000146 << VT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000147#endif
148 llvm_unreachable(0);
Chris Lattnerfb39f992007-02-28 06:56:37 +0000149 }
150 }
151}
Evan Chengc7fcfa02008-09-07 09:02:18 +0000152
153/// AnalyzeCallResult - Same as above except it's specialized for calls which
154/// produce a single value.
155void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
156 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000157#ifndef NDEBUG
158 cerr << "Call result has unhandled type "
Torok Edwin7d696d82009-07-11 13:10:19 +0000159 << VT.getMVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000160#endif
161 llvm_unreachable(0);
Evan Chengc7fcfa02008-09-07 09:02:18 +0000162 }
163}