ba097e089aa19cefe2820baab7871a0ee3df5590
[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 using namespace llvm;
23
24 CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
25                  SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
26   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
27     TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
28   // No stack is used.
29   StackOffset = 0;
30   
31   UsedRegs.resize((TRI.getNumRegs()+31)/32);
32 }
33
34 // 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.
37 void CCState::HandleByVal(unsigned ValNo, EVT ValVT,
38                           EVT LocVT, CCValAssign::LocInfo LocInfo,
39                           int MinSize, int MinAlign,
40                           ISD::ArgFlagsTy ArgFlags) {
41   unsigned Align = ArgFlags.getByValAlign();
42   unsigned Size  = ArgFlags.getByValSize();
43   if (MinSize > (int)Size)
44     Size = MinSize;
45   if (MinAlign > (int)Align)
46     Align = MinAlign;
47   unsigned Offset = AllocateStack(Size, Align);
48
49   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
50 }
51
52 /// MarkAllocated - Mark a register and all of its aliases as allocated.
53 void CCState::MarkAllocated(unsigned Reg) {
54   UsedRegs[Reg/32] |= 1 << (Reg&31);
55   
56   if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
57     for (; (Reg = *RegAliases); ++RegAliases)
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     EVT 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              << 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<EVT> &OutTys,
84                           const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
85                           CCAssignFn Fn) {
86   // Determine which register each value should be copied into.
87   for (unsigned i = 0, e = OutTys.size(); i != e; ++i) {
88     EVT VT = OutTys[i];
89     ISD::ArgFlagsTy ArgFlags = ArgsFlags[i];
90     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
91       return false;
92   }
93   return true;
94 }
95
96 /// AnalyzeReturn - Analyze the returned values of a return,
97 /// incorporating info about the result values into this state.
98 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
99                             CCAssignFn Fn) {
100   // Determine which register each value should be copied into.
101   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
102     EVT VT = Outs[i].Val.getValueType();
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
114 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArgReg> &Outs,
115                             CCAssignFn Fn) {
116   // Determine which register each value should be copied into.
117   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
118     EVT VT = Outs[i].VT;
119     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
120     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
121 #ifndef NDEBUG
122       dbgs() << "Return operand #" << i << " has unhandled type "
123              << VT.getEVTString();
124 #endif
125       llvm_unreachable(0);
126     }
127   }
128 }
129
130
131 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
132 /// incorporating info about the passed values into this state.
133 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
134                                   CCAssignFn Fn) {
135   unsigned NumOps = Outs.size();
136   for (unsigned i = 0; i != NumOps; ++i) {
137     EVT ArgVT = Outs[i].Val.getValueType();
138     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
139     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
140 #ifndef NDEBUG
141       dbgs() << "Call operand #" << i << " has unhandled type "
142              << ArgVT.getEVTString();
143 #endif
144       llvm_unreachable(0);
145     }
146   }
147 }
148
149 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
150 /// incorporating info about the passed values into this state.
151 void
152 CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArgReg> &Outs,
153                              CCAssignFn Fn) {
154   unsigned NumOps = Outs.size();
155   for (unsigned i = 0; i != NumOps; ++i) {
156     EVT ArgVT = Outs[i].VT;
157     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
158     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
159 #ifndef NDEBUG
160       dbgs() << "Call operand #" << i << " has unhandled type "
161              << ArgVT.getEVTString();
162 #endif
163       llvm_unreachable(0);
164     }
165   }
166 }
167
168 /// AnalyzeCallOperands - Same as above except it takes vectors of types
169 /// and argument flags.
170 void CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
171                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
172                                   CCAssignFn Fn) {
173   unsigned NumOps = ArgVTs.size();
174   for (unsigned i = 0; i != NumOps; ++i) {
175     EVT ArgVT = ArgVTs[i];
176     ISD::ArgFlagsTy ArgFlags = Flags[i];
177     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
178 #ifndef NDEBUG
179       dbgs() << "Call operand #" << i << " has unhandled type "
180              << ArgVT.getEVTString();
181 #endif
182       llvm_unreachable(0);
183     }
184   }
185 }
186
187 /// AnalyzeCallResult - Analyze the return values of a call,
188 /// incorporating info about the passed values into this state.
189 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
190                                 CCAssignFn Fn) {
191   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
192     EVT VT = Ins[i].VT;
193     ISD::ArgFlagsTy Flags = Ins[i].Flags;
194     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
195 #ifndef NDEBUG
196       dbgs() << "Call result #" << i << " has unhandled type "
197              << VT.getEVTString();
198 #endif
199       llvm_unreachable(0);
200     }
201   }
202 }
203
204 /// AnalyzeCallResult - Same as above except it's specialized for calls which
205 /// produce a single value.
206 void CCState::AnalyzeCallResult(EVT VT, CCAssignFn Fn) {
207   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
208 #ifndef NDEBUG
209     dbgs() << "Call result has unhandled type "
210            << VT.getEVTString();
211 #endif
212     llvm_unreachable(0);
213   }
214 }