Support for byval parameters on ARM. Will be enabled by a forthcoming
[oota-llvm.git] / lib / CodeGen / CallingConvLower.cpp
1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetLowering.h"
23 using namespace llvm;
24
25 CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
26                  SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
27   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
28     TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
29   // No stack is used.
30   StackOffset = 0;
31   
32   UsedRegs.resize((TRI.getNumRegs()+31)/32);
33 }
34
35 // HandleByVal - Allocate a stack slot large enough to pass an argument by
36 // value. The size and alignment information of the argument is encoded in its
37 // parameter attribute.
38 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
39                           MVT LocVT, CCValAssign::LocInfo LocInfo,
40                           int MinSize, int MinAlign,
41                           ISD::ArgFlagsTy ArgFlags) {
42   unsigned Align = ArgFlags.getByValAlign();
43   unsigned Size  = ArgFlags.getByValSize();
44   if (MinSize > (int)Size)
45     Size = MinSize;
46   if (MinAlign > (int)Align)
47     Align = MinAlign;
48   unsigned Offset = AllocateStack(Size, Align);
49
50   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
51   TM.getTargetLowering()->HandleByVal(const_cast<CCState*>(this));
52 }
53
54 /// MarkAllocated - Mark a register and all of its aliases as allocated.
55 void CCState::MarkAllocated(unsigned Reg) {
56   for (const unsigned *Alias = TRI.getOverlaps(Reg);
57        unsigned Reg = *Alias; ++Alias)
58     UsedRegs[Reg/32] |= 1 << (Reg&31);
59 }
60
61 /// AnalyzeFormalArguments - Analyze an array of argument values,
62 /// incorporating info about the formals into this state.
63 void
64 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
65                                 CCAssignFn Fn) {
66   unsigned NumArgs = Ins.size();
67
68   for (unsigned i = 0; i != NumArgs; ++i) {
69     MVT ArgVT = Ins[i].VT;
70     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
71     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
72 #ifndef NDEBUG
73       dbgs() << "Formal argument #" << i << " has unhandled type "
74              << EVT(ArgVT).getEVTString();
75 #endif
76       llvm_unreachable(0);
77     }
78   }
79 }
80
81 /// CheckReturn - Analyze the return values of a function, returning true if
82 /// the return can be performed without sret-demotion, and false otherwise.
83 bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
84                           CCAssignFn Fn) {
85   // Determine which register each value should be copied into.
86   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
87     MVT VT = Outs[i].VT;
88     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
89     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
90       return false;
91   }
92   return true;
93 }
94
95 /// AnalyzeReturn - Analyze the returned values of a return,
96 /// incorporating info about the result values into this state.
97 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
98                             CCAssignFn Fn) {
99   // Determine which register each value should be copied into.
100   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
101     MVT VT = Outs[i].VT;
102     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
103     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
104 #ifndef NDEBUG
105       dbgs() << "Return operand #" << i << " has unhandled type "
106              << EVT(VT).getEVTString();
107 #endif
108       llvm_unreachable(0);
109     }
110   }
111 }
112
113 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
114 /// incorporating info about the passed values into this state.
115 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
116                                   CCAssignFn Fn) {
117   unsigned NumOps = Outs.size();
118   for (unsigned i = 0; i != NumOps; ++i) {
119     MVT ArgVT = Outs[i].VT;
120     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
121     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
122 #ifndef NDEBUG
123       dbgs() << "Call operand #" << i << " has unhandled type "
124              << EVT(ArgVT).getEVTString();
125 #endif
126       llvm_unreachable(0);
127     }
128   }
129 }
130
131 /// AnalyzeCallOperands - Same as above except it takes vectors of types
132 /// and argument flags.
133 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
134                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
135                                   CCAssignFn Fn) {
136   unsigned NumOps = ArgVTs.size();
137   for (unsigned i = 0; i != NumOps; ++i) {
138     MVT ArgVT = ArgVTs[i];
139     ISD::ArgFlagsTy ArgFlags = Flags[i];
140     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
141 #ifndef NDEBUG
142       dbgs() << "Call operand #" << i << " has unhandled type "
143              << EVT(ArgVT).getEVTString();
144 #endif
145       llvm_unreachable(0);
146     }
147   }
148 }
149
150 /// AnalyzeCallResult - Analyze the return values of a call,
151 /// incorporating info about the passed values into this state.
152 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
153                                 CCAssignFn Fn) {
154   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
155     MVT VT = Ins[i].VT;
156     ISD::ArgFlagsTy Flags = Ins[i].Flags;
157     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
158 #ifndef NDEBUG
159       dbgs() << "Call result #" << i << " has unhandled type "
160              << EVT(VT).getEVTString();
161 #endif
162       llvm_unreachable(0);
163     }
164   }
165 }
166
167 /// AnalyzeCallResult - Same as above except it's specialized for calls which
168 /// produce a single value.
169 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
170   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
171 #ifndef NDEBUG
172     dbgs() << "Call result has unhandled type "
173            << EVT(VT).getEVTString();
174 #endif
175     llvm_unreachable(0);
176   }
177 }