blob: 5e47038054fe35cae9d5365935a39e614716f386 [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"
David Greenead61b122010-01-05 01:24:50 +000016#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000017#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000019#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengc67e6e82008-01-15 03:14:05 +000020#include "llvm/Target/TargetData.h"
Chris Lattner13513b72007-02-27 05:13:54 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000022using namespace llvm;
23
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000024CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
Owen Andersone922c022009-07-22 00:24:57 +000025 SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
Chris Lattneraeeccfc2007-06-19 00:11:09 +000026 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
Owen Andersond1474d02009-07-09 17:57:24 +000027 TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
Chris Lattner362e98a2007-02-27 04:43:02 +000028 // No stack is used.
29 StackOffset = 0;
30
Dan Gohman117b64b2008-06-30 20:25:31 +000031 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattner362e98a2007-02-27 04:43:02 +000032}
33
Evan Cheng4c46fc62008-01-15 07:49:36 +000034// 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.
Owen Andersone50ed302009-08-10 22:56:29 +000037void CCState::HandleByVal(unsigned ValNo, EVT ValVT,
38 EVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Cheng4c46fc62008-01-15 07:49:36 +000039 int MinSize, int MinAlign,
Duncan Sands276dcbd2008-03-21 09:14:45 +000040 ISD::ArgFlagsTy ArgFlags) {
41 unsigned Align = ArgFlags.getByValAlign();
42 unsigned Size = ArgFlags.getByValSize();
Evan Cheng4c46fc62008-01-15 07:49:36 +000043 if (MinSize > (int)Size)
44 Size = MinSize;
45 if (MinAlign > (int)Align)
46 Align = MinAlign;
47 unsigned Offset = AllocateStack(Size, Align);
Rafael Espindola594d37e2007-08-10 14:44:42 +000048
49 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
50}
Chris Lattner362e98a2007-02-27 04:43:02 +000051
52/// MarkAllocated - Mark a register and all of its aliases as allocated.
53void CCState::MarkAllocated(unsigned Reg) {
54 UsedRegs[Reg/32] |= 1 << (Reg&31);
55
Dan Gohman6f0d0242008-02-10 18:45:23 +000056 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
Chris Lattner362e98a2007-02-27 04:43:02 +000057 for (; (Reg = *RegAliases); ++RegAliases)
58 UsedRegs[Reg/32] |= 1 << (Reg&31);
59}
Chris Lattnerfb39f992007-02-28 06:56:37 +000060
Dan Gohman98ca4f22009-08-05 01:29:28 +000061/// AnalyzeFormalArguments - Analyze an array of argument values,
Chris Lattner66baf262007-02-28 07:09:40 +000062/// incorporating info about the formals into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +000063void
64CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
65 CCAssignFn Fn) {
66 unsigned NumArgs = Ins.size();
67
Chris Lattner66baf262007-02-28 07:09:40 +000068 for (unsigned i = 0; i != NumArgs; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +000069 EVT ArgVT = Ins[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +000070 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner66baf262007-02-28 07:09:40 +000071 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +000072#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +000073 dbgs() << "Formal argument #" << i << " has unhandled type "
Chris Lattner45cfe542009-08-23 06:03:38 +000074 << ArgVT.getEVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +000075#endif
76 llvm_unreachable(0);
Chris Lattner66baf262007-02-28 07:09:40 +000077 }
78 }
79}
80
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000081/// CheckReturn - Analyze the return values of a function, returning true if
82/// the return can be performed without sret-demotion, and false otherwise.
Bob Wilson02266e22010-07-09 16:37:18 +000083bool CCState::CheckReturn(const SmallVectorImpl<EVT> &OutTys,
84 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000085 CCAssignFn Fn) {
86 // Determine which register each value should be copied into.
Bob Wilson02266e22010-07-09 16:37:18 +000087 for (unsigned i = 0, e = OutTys.size(); i != e; ++i) {
88 EVT VT = OutTys[i];
89 ISD::ArgFlagsTy ArgFlags = ArgsFlags[i];
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +000090 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
91 return false;
92 }
93 return true;
94}
95
Dan Gohman98ca4f22009-08-05 01:29:28 +000096/// AnalyzeReturn - Analyze the returned values of a return,
Chris Lattner66baf262007-02-28 07:09:40 +000097/// incorporating info about the result values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +000098void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
99 CCAssignFn Fn) {
Chris Lattner66baf262007-02-28 07:09:40 +0000100 // Determine which register each value should be copied into.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000101 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000102 EVT VT = Outs[i].VT;
103 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
104 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
105#ifndef NDEBUG
106 dbgs() << "Return operand #" << i << " has unhandled type "
107 << VT.getEVTString();
108#endif
109 llvm_unreachable(0);
110 }
111 }
112}
113
Dan Gohman98ca4f22009-08-05 01:29:28 +0000114/// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
115/// incorporating info about the passed values into this state.
116void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
117 CCAssignFn Fn) {
118 unsigned NumOps = Outs.size();
Chris Lattnerfb39f992007-02-28 06:56:37 +0000119 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanaa8c1942010-07-06 15:39:54 +0000120 EVT ArgVT = Outs[i].VT;
121 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
122 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
123#ifndef NDEBUG
124 dbgs() << "Call operand #" << i << " has unhandled type "
125 << ArgVT.getEVTString();
126#endif
127 llvm_unreachable(0);
128 }
129 }
130}
131
Evan Chengc89d2fe2008-09-05 16:59:26 +0000132/// AnalyzeCallOperands - Same as above except it takes vectors of types
133/// and argument flags.
Owen Andersone50ed302009-08-10 22:56:29 +0000134void CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
Evan Chengc89d2fe2008-09-05 16:59:26 +0000135 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
136 CCAssignFn Fn) {
137 unsigned NumOps = ArgVTs.size();
138 for (unsigned i = 0; i != NumOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000139 EVT ArgVT = ArgVTs[i];
Evan Chengc89d2fe2008-09-05 16:59:26 +0000140 ISD::ArgFlagsTy ArgFlags = Flags[i];
141 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000142#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000143 dbgs() << "Call operand #" << i << " has unhandled type "
Chris Lattner45cfe542009-08-23 06:03:38 +0000144 << ArgVT.getEVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000145#endif
146 llvm_unreachable(0);
Evan Chengc89d2fe2008-09-05 16:59:26 +0000147 }
148 }
149}
150
Dan Gohman98ca4f22009-08-05 01:29:28 +0000151/// AnalyzeCallResult - Analyze the return values of a call,
Chris Lattner66baf262007-02-28 07:09:40 +0000152/// incorporating info about the passed values into this state.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000153void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
154 CCAssignFn Fn) {
155 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000156 EVT VT = Ins[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000157 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen86098bd2008-09-26 19:31:26 +0000158 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000159#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000160 dbgs() << "Call result #" << i << " has unhandled type "
Chris Lattner45cfe542009-08-23 06:03:38 +0000161 << VT.getEVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000162#endif
163 llvm_unreachable(0);
Chris Lattnerfb39f992007-02-28 06:56:37 +0000164 }
165 }
166}
Evan Chengc7fcfa02008-09-07 09:02:18 +0000167
168/// AnalyzeCallResult - Same as above except it's specialized for calls which
169/// produce a single value.
Owen Andersone50ed302009-08-10 22:56:29 +0000170void CCState::AnalyzeCallResult(EVT VT, CCAssignFn Fn) {
Evan Chengc7fcfa02008-09-07 09:02:18 +0000171 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000172#ifndef NDEBUG
David Greenead61b122010-01-05 01:24:50 +0000173 dbgs() << "Call result has unhandled type "
Chris Lattner45cfe542009-08-23 06:03:38 +0000174 << VT.getEVTString();
Torok Edwinc23197a2009-07-14 16:55:14 +0000175#endif
176 llvm_unreachable(0);
Evan Chengc7fcfa02008-09-07 09:02:18 +0000177 }
178}