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