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