Convert more assert(0)+abort() -> LLVM_UNREACHABLE,
[oota-llvm.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(unsigned 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, MVT ValVT,
37                           MVT 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 ISD::FORMAL_ARGUMENTS node,
61 /// incorporating info about the formals into this state.
62 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
63   unsigned NumArgs = TheArgs->getNumValues()-1;
64   
65   for (unsigned i = 0; i != NumArgs; ++i) {
66     MVT ArgVT = TheArgs->getValueType(i);
67     ISD::ArgFlagsTy ArgFlags =
68       cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
69     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
70       std::string msg;
71       raw_string_ostream Msg(msg);
72       Msg << "Formal argument #" << i << " has unhandled type "
73            << ArgVT.getMVTString();
74       llvm_report_error(Msg.str());
75     }
76   }
77 }
78
79 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
80 /// incorporating info about the result values into this state.
81 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
82   // Determine which register each value should be copied into.
83   for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
84     MVT VT = TheRet->getOperand(i*2+1).getValueType();
85     ISD::ArgFlagsTy ArgFlags =
86       cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
87     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
88       std::string msg;
89       raw_string_ostream Msg(msg);
90       Msg << "Return operand #" << i << " has unhandled type "
91            << VT.getMVTString();
92       llvm_report_error(Msg.str());
93     }
94   }
95 }
96
97
98 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
99 /// about the passed values into this state.
100 void CCState::AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn) {
101   unsigned NumOps = TheCall->getNumArgs();
102   for (unsigned i = 0; i != NumOps; ++i) {
103     MVT ArgVT = TheCall->getArg(i).getValueType();
104     ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
105     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
106       std::string msg;
107       raw_string_ostream Msg(msg);
108       Msg << "Call operand #" << i << " has unhandled type "
109            << ArgVT.getMVTString();
110       llvm_report_error(Msg.str());
111     }
112   }
113 }
114
115 /// AnalyzeCallOperands - Same as above except it takes vectors of types
116 /// and argument flags.
117 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
118                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
119                                   CCAssignFn Fn) {
120   unsigned NumOps = ArgVTs.size();
121   for (unsigned i = 0; i != NumOps; ++i) {
122     MVT ArgVT = ArgVTs[i];
123     ISD::ArgFlagsTy ArgFlags = Flags[i];
124     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
125       std::string msg;
126       raw_string_ostream Msg(msg);
127       Msg << "Call operand #" << i << " has unhandled type "
128            << ArgVT.getMVTString();
129       llvm_report_error(Msg.str());
130     }
131   }
132 }
133
134 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
135 /// incorporating info about the passed values into this state.
136 void CCState::AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn) {
137   for (unsigned i = 0, e = TheCall->getNumRetVals(); i != e; ++i) {
138     MVT VT = TheCall->getRetValType(i);
139     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
140     if (TheCall->isInreg())
141       Flags.setInReg();
142     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
143       std::string msg;
144       raw_string_ostream Msg(msg);
145       Msg << "Call result #" << i << " has unhandled type "
146            << VT.getMVTString();
147       llvm_report_error(Msg.str());
148     }
149   }
150 }
151
152 /// AnalyzeCallResult - Same as above except it's specialized for calls which
153 /// produce a single value.
154 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
155   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
156     std::string msg;
157     raw_string_ostream Msg(msg);
158     Msg << "Call result has unhandled type "
159          << VT.getMVTString();
160     llvm_report_error(Msg.str());
161   }
162 }