3b3fa3ba276b5add1922a406d2a4d237d2098289
[oota-llvm.git] / lib / CodeGen / SelectionDAG / CallingConvLower.cpp
1 //===-- llvm/CallingConvLower.cpp - Calling Conventions -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/TargetMachine.h"
19 using namespace llvm;
20
21 CCState::CCState(unsigned CC, const TargetMachine &tm,
22                  SmallVector<CCValAssign, 16> &locs)
23   : CallingConv(CC), TM(tm), MRI(*TM.getRegisterInfo()), Locs(locs) {
24   // No stack is used.
25   StackOffset = 0;
26   
27   UsedRegs.resize(MRI.getNumRegs());
28 }
29
30
31 /// MarkAllocated - Mark a register and all of its aliases as allocated.
32 void CCState::MarkAllocated(unsigned Reg) {
33   UsedRegs[Reg/32] |= 1 << (Reg&31);
34   
35   if (const unsigned *RegAliases = MRI.getAliasSet(Reg))
36     for (; (Reg = *RegAliases); ++RegAliases)
37       UsedRegs[Reg/32] |= 1 << (Reg&31);
38 }
39
40 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
41 /// about the passed values into this state.
42 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
43   unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
44   for (unsigned i = 0; i != NumOps; ++i) {
45     MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
46     SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
47     unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
48     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
49       cerr << "Call operand #" << i << " has unhandled type "
50            << MVT::getValueTypeString(ArgVT) << "\n";
51       abort();
52     }
53   }
54 }
55
56 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
57 /// incorporating info about the formals into this state.
58 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
59   unsigned NumArgs = TheArgs->getNumValues()-1;
60
61   for (unsigned i = 0; i != NumArgs; ++i) {
62     MVT::ValueType ArgVT = TheArgs->getValueType(i);
63     SDOperand FlagOp = TheArgs->getOperand(3+i);
64     unsigned ArgFlags = cast<ConstantSDNode>(FlagOp)->getValue();
65     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
66       cerr << "Formal argument #" << i << " has unhandled type "
67            << MVT::getValueTypeString(ArgVT) << "\n";
68       abort();
69     }
70   }
71 }