- Doh. Pass vector by value is bad.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / CallingConvLower.cpp
1 //===-- llvm/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/Target/TargetRegisterInfo.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Target/TargetMachine.h"
19 using namespace llvm;
20
21 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
22                  SmallVector<CCValAssign, 16> &locs)
23   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
24     TRI(*TM.getRegisterInfo()), Locs(locs) {
25   // No stack is used.
26   StackOffset = 0;
27   
28   UsedRegs.resize((TRI.getNumRegs()+31)/32);
29 }
30
31 // HandleByVal - Allocate a stack slot large enough to pass an argument by
32 // value. The size and alignment information of the argument is encoded in its
33 // parameter attribute.
34 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
35                           MVT LocVT, CCValAssign::LocInfo LocInfo,
36                           int MinSize, int MinAlign,
37                           ISD::ArgFlagsTy ArgFlags) {
38   unsigned Align = ArgFlags.getByValAlign();
39   unsigned Size  = ArgFlags.getByValSize();
40   if (MinSize > (int)Size)
41     Size = MinSize;
42   if (MinAlign > (int)Align)
43     Align = MinAlign;
44   unsigned Offset = AllocateStack(Size, Align);
45
46   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
47 }
48
49 /// MarkAllocated - Mark a register and all of its aliases as allocated.
50 void CCState::MarkAllocated(unsigned Reg) {
51   UsedRegs[Reg/32] |= 1 << (Reg&31);
52   
53   if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
54     for (; (Reg = *RegAliases); ++RegAliases)
55       UsedRegs[Reg/32] |= 1 << (Reg&31);
56 }
57
58 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
59 /// incorporating info about the formals into this state.
60 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
61   unsigned NumArgs = TheArgs->getNumValues()-1;
62   
63   for (unsigned i = 0; i != NumArgs; ++i) {
64     MVT ArgVT = TheArgs->getValueType(i);
65     ISD::ArgFlagsTy ArgFlags =
66       cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
67     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
68       cerr << "Formal argument #" << i << " has unhandled type "
69            << ArgVT.getMVTString() << "\n";
70       abort();
71     }
72   }
73 }
74
75 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
76 /// incorporating info about the result values into this state.
77 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
78   // Determine which register each value should be copied into.
79   for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
80     MVT VT = TheRet->getOperand(i*2+1).getValueType();
81     ISD::ArgFlagsTy ArgFlags =
82       cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
83     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
84       cerr << "Return operand #" << i << " has unhandled type "
85            << VT.getMVTString() << "\n";
86       abort();
87     }
88   }
89 }
90
91
92 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
93 /// about the passed values into this state.
94 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
95   unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
96   for (unsigned i = 0; i != NumOps; ++i) {
97     MVT ArgVT = TheCall->getOperand(5+2*i).getValueType();
98     ISD::ArgFlagsTy ArgFlags =
99       cast<ARG_FLAGSSDNode>(TheCall->getOperand(5+2*i+1))->getArgFlags();
100     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
101       cerr << "Call operand #" << i << " has unhandled type "
102            << ArgVT.getMVTString() << "\n";
103       abort();
104     }
105   }
106 }
107
108 /// AnalyzeCallOperands - Same as above except it takes vectors of types
109 /// and argument flags.
110 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
111                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
112                                   CCAssignFn Fn) {
113   unsigned NumOps = ArgVTs.size();
114   for (unsigned i = 0; i != NumOps; ++i) {
115     MVT ArgVT = ArgVTs[i];
116     ISD::ArgFlagsTy ArgFlags = Flags[i];
117     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
118       cerr << "Call operand #" << i << " has unhandled type "
119            << ArgVT.getMVTString() << "\n";
120       abort();
121     }
122   }
123 }
124
125 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
126 /// incorporating info about the passed values into this state.
127 void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
128   for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
129     MVT VT = TheCall->getValueType(i);
130     if (Fn(i, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
131       cerr << "Call result #" << i << " has unhandled type "
132            << VT.getMVTString() << "\n";
133       abort();
134     }
135   }
136 }
137
138 /// AnalyzeCallResult - Same as above except it's specialized for calls which
139 /// produce a single value.
140 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
141   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
142     cerr << "Call result has unhandled type "
143          << VT.getMVTString() << "\n";
144     abort();
145   }
146 }