blob: 2ad80b4d3a7599599f33ac2f4c4b877e44fdc75f [file] [log] [blame]
Dan Gohmand51f1962009-03-31 16:51:18 +00001//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
Chris Lattnerdc3adc82007-02-27 04:43:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnerdc3adc82007-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 Greeneec5883f2010-01-05 01:24:50 +000016#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000017#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/raw_ostream.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000019#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b212ea2008-01-15 03:14:05 +000020#include "llvm/Target/TargetData.h"
Chris Lattner9f0591942007-02-27 05:13:54 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnerdc3adc82007-02-27 04:43:02 +000022using namespace llvm;
23
Sandeep Patel68c5f472009-09-02 08:44:58 +000024CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
Owen Anderson47db9412009-07-22 00:24:57 +000025 SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
Chris Lattner26be02f2007-06-19 00:11:09 +000026 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
Owen Anderson0504e0a2009-07-09 17:57:24 +000027 TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
Chris Lattnerdc3adc82007-02-27 04:43:02 +000028 // No stack is used.
29 StackOffset = 0;
30
Dan Gohman328e26d2008-06-30 20:25:31 +000031 UsedRegs.resize((TRI.getNumRegs()+31)/32);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000032}
33
Evan Chengeb30bb72008-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.
Duncan Sands71049f72010-11-04 10:49:57 +000037void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
Duncan Sandsf5dda012010-11-03 11:35:31 +000038 MVT LocVT, CCValAssign::LocInfo LocInfo,
Evan Chengeb30bb72008-01-15 07:49:36 +000039 int MinSize, int MinAlign,
Duncan Sandsd97eea32008-03-21 09:14:45 +000040 ISD::ArgFlagsTy ArgFlags) {
41 unsigned Align = ArgFlags.getByValAlign();
42 unsigned Size = ArgFlags.getByValSize();
Evan Chengeb30bb72008-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 Espindola66011c12007-08-10 14:44:42 +000048
49 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
50}
Chris Lattnerdc3adc82007-02-27 04:43:02 +000051
52/// MarkAllocated - Mark a register and all of its aliases as allocated.
53void CCState::MarkAllocated(unsigned Reg) {
Jakob Stoklund Olesen47b93402010-12-14 23:28:01 +000054 for (const unsigned *Alias = TRI.getOverlaps(Reg);
55 unsigned Reg = *Alias; ++Alias)
56 UsedRegs[Reg/32] |= 1 << (Reg&31);
Chris Lattnerdc3adc82007-02-27 04:43:02 +000057}
Chris Lattnere7474412007-02-28 06:56:37 +000058
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000059/// AnalyzeFormalArguments - Analyze an array of argument values,
Chris Lattner74bb9292007-02-28 07:09:40 +000060/// incorporating info about the formals into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000061void
62CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
63 CCAssignFn Fn) {
64 unsigned NumArgs = Ins.size();
65
Chris Lattner74bb9292007-02-28 07:09:40 +000066 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000067 MVT ArgVT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000068 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
Chris Lattner74bb9292007-02-28 07:09:40 +000069 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000070#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +000071 dbgs() << "Formal argument #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +000072 << EVT(ArgVT).getEVTString();
Torok Edwinfbcc6632009-07-14 16:55:14 +000073#endif
74 llvm_unreachable(0);
Chris Lattner74bb9292007-02-28 07:09:40 +000075 }
76 }
77}
78
Kenneth Uildriks07119732009-11-07 02:11:54 +000079/// CheckReturn - Analyze the return values of a function, returning true if
80/// the return can be performed without sret-demotion, and false otherwise.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000081bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
Kenneth Uildriks07119732009-11-07 02:11:54 +000082 CCAssignFn Fn) {
83 // Determine which register each value should be copied into.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000084 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000085 MVT VT = Outs[i].VT;
Dan Gohmand7b5ce32010-07-10 09:00:22 +000086 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Kenneth Uildriks07119732009-11-07 02:11:54 +000087 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
88 return false;
89 }
90 return true;
91}
92
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000093/// AnalyzeReturn - Analyze the returned values of a return,
Chris Lattner74bb9292007-02-28 07:09:40 +000094/// incorporating info about the result values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000095void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
96 CCAssignFn Fn) {
Chris Lattner74bb9292007-02-28 07:09:40 +000097 // Determine which register each value should be copied into.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000098 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +000099 MVT VT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000100 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
101 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
102#ifndef NDEBUG
103 dbgs() << "Return operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +0000104 << EVT(VT).getEVTString();
Dan Gohman4e49b592010-07-06 15:39:54 +0000105#endif
106 llvm_unreachable(0);
107 }
108 }
109}
110
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000111/// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
112/// incorporating info about the passed values into this state.
113void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
114 CCAssignFn Fn) {
115 unsigned NumOps = Outs.size();
Chris Lattnere7474412007-02-28 06:56:37 +0000116 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000117 MVT ArgVT = Outs[i].VT;
Dan Gohman4e49b592010-07-06 15:39:54 +0000118 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
119 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
120#ifndef NDEBUG
121 dbgs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +0000122 << EVT(ArgVT).getEVTString();
Dan Gohman4e49b592010-07-06 15:39:54 +0000123#endif
124 llvm_unreachable(0);
125 }
126 }
127}
128
Evan Cheng6b8fae12008-09-05 16:59:26 +0000129/// AnalyzeCallOperands - Same as above except it takes vectors of types
130/// and argument flags.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000131void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
Evan Cheng6b8fae12008-09-05 16:59:26 +0000132 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
133 CCAssignFn Fn) {
134 unsigned NumOps = ArgVTs.size();
135 for (unsigned i = 0; i != NumOps; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000136 MVT ArgVT = ArgVTs[i];
Evan Cheng6b8fae12008-09-05 16:59:26 +0000137 ISD::ArgFlagsTy ArgFlags = Flags[i];
138 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000139#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000140 dbgs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +0000141 << EVT(ArgVT).getEVTString();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000142#endif
143 llvm_unreachable(0);
Evan Cheng6b8fae12008-09-05 16:59:26 +0000144 }
145 }
146}
147
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000148/// AnalyzeCallResult - Analyze the return values of a call,
Chris Lattner74bb9292007-02-28 07:09:40 +0000149/// incorporating info about the passed values into this state.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000150void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
151 CCAssignFn Fn) {
152 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000153 MVT VT = Ins[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000154 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Dale Johannesen0e32a2c2008-09-26 19:31:26 +0000155 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000156#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000157 dbgs() << "Call result #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +0000158 << EVT(VT).getEVTString();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000159#endif
160 llvm_unreachable(0);
Chris Lattnere7474412007-02-28 06:56:37 +0000161 }
162 }
163}
Evan Cheng615739b2008-09-07 09:02:18 +0000164
165/// AnalyzeCallResult - Same as above except it's specialized for calls which
166/// produce a single value.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000167void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
Evan Cheng615739b2008-09-07 09:02:18 +0000168 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000169#ifndef NDEBUG
David Greeneec5883f2010-01-05 01:24:50 +0000170 dbgs() << "Call result has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +0000171 << EVT(VT).getEVTString();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000172#endif
173 llvm_unreachable(0);
Evan Cheng615739b2008-09-07 09:02:18 +0000174 }
175}