SDIsel processes llvm.dbg.declare by recording the variable debug information descrip...
[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/CodeGen/SelectionDAGNodes.h"
17 #include "llvm/Target/MRegisterInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetMachine.h"
20 using namespace llvm;
21
22 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
23                  SmallVector<CCValAssign, 16> &locs)
24   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
25     MRI(*TM.getRegisterInfo()), Locs(locs) {
26   // No stack is used.
27   StackOffset = 0;
28   
29   UsedRegs.resize(MRI.getNumRegs());
30 }
31
32 // HandleByVal - Allocate a stack slot large enough to pass an argument by
33 // value. The size and alignment information of the argument is encoded in its
34 // parameter attribute.
35 void CCState::HandleByVal(unsigned ValNo, MVT::ValueType ValVT,
36                           MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
37                           int MinSize, int MinAlign,
38                           unsigned ArgFlags) {
39   unsigned Align  = 1 << ((ArgFlags & ISD::ParamFlags::ByValAlign) >>
40                           ISD::ParamFlags::ByValAlignOffs);
41   unsigned Size   = (ArgFlags & ISD::ParamFlags::ByValSize) >>
42       ISD::ParamFlags::ByValSizeOffs;
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 = MRI.getAliasSet(Reg))
57     for (; (Reg = *RegAliases); ++RegAliases)
58       UsedRegs[Reg/32] |= 1 << (Reg&31);
59 }
60
61 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
62 /// incorporating info about the formals into this state.
63 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
64   unsigned NumArgs = TheArgs->getNumValues()-1;
65   
66   for (unsigned i = 0; i != NumArgs; ++i) {
67     MVT::ValueType ArgVT = TheArgs->getValueType(i);
68     SDOperand FlagOp = TheArgs->getOperand(3+i);
69     unsigned ArgFlags = cast<ConstantSDNode>(FlagOp)->getValue();
70     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
71       cerr << "Formal argument #" << i << " has unhandled type "
72            << MVT::getValueTypeString(ArgVT) << "\n";
73       abort();
74     }
75   }
76 }
77
78 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
79 /// incorporating info about the result values into this state.
80 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
81   // Determine which register each value should be copied into.
82   for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
83     MVT::ValueType VT = TheRet->getOperand(i*2+1).getValueType();
84     if (Fn(i, VT, VT, CCValAssign::Full,
85            cast<ConstantSDNode>(TheRet->getOperand(i*2+2))->getValue(), *this)){
86       cerr << "Return operand #" << i << " has unhandled type "
87            << MVT::getValueTypeString(VT) << "\n";
88       abort();
89     }
90   }
91 }
92
93
94 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
95 /// about the passed values into this state.
96 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
97   unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
98   for (unsigned i = 0; i != NumOps; ++i) {
99     MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
100     SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
101     unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
102     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
103       cerr << "Call operand #" << i << " has unhandled type "
104            << MVT::getValueTypeString(ArgVT) << "\n";
105       abort();
106     }
107   }
108 }
109
110 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
111 /// incorporating info about the passed values into this state.
112 void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
113   for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
114     MVT::ValueType VT = TheCall->getValueType(i);
115     if (Fn(i, VT, VT, CCValAssign::Full, 0, *this)) {
116       cerr << "Call result #" << i << " has unhandled type "
117            << MVT::getValueTypeString(VT) << "\n";
118       abort();
119     }
120   }
121 }