CallingConvLower.h revision 212904
1193323Sed//===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file declares the CCState and CCValAssign classes, used for lowering
11193323Sed// and implementing calling conventions.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15193323Sed#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
16193323Sed#define LLVM_CODEGEN_CALLINGCONVLOWER_H
17193323Sed
18193323Sed#include "llvm/ADT/SmallVector.h"
19193323Sed#include "llvm/CodeGen/ValueTypes.h"
20210299Sed#include "llvm/Target/TargetCallingConv.h"
21198090Srdivacky#include "llvm/CallingConv.h"
22193323Sed
23193323Sednamespace llvm {
24193323Sed  class TargetRegisterInfo;
25193323Sed  class TargetMachine;
26193323Sed  class CCState;
27193323Sed
28193323Sed/// CCValAssign - Represent assignment of one arg/retval to a location.
29193323Sedclass CCValAssign {
30193323Sedpublic:
31193323Sed  enum LocInfo {
32193323Sed    Full,   // The value fills the full location.
33193323Sed    SExt,   // The value is sign extended in the location.
34193323Sed    ZExt,   // The value is zero extended in the location.
35193323Sed    AExt,   // The value is extended with undefined upper bits.
36198090Srdivacky    BCvt,   // The value is bit-converted in the location.
37210299Sed    VExt,   // The value is vector-widened in the location.
38210299Sed            // FIXME: Not implemented yet. Code that uses AExt to mean
39210299Sed            // vector-widen should be fixed to use VExt instead.
40198090Srdivacky    Indirect // The location contains pointer to the value.
41193323Sed    // TODO: a subset of the value is in the location.
42193323Sed  };
43193323Sedprivate:
44193323Sed  /// ValNo - This is the value number begin assigned (e.g. an argument number).
45193323Sed  unsigned ValNo;
46198090Srdivacky
47193323Sed  /// Loc is either a stack offset or a register number.
48193323Sed  unsigned Loc;
49198090Srdivacky
50193323Sed  /// isMem - True if this is a memory loc, false if it is a register loc.
51193323Sed  bool isMem : 1;
52198090Srdivacky
53193323Sed  /// isCustom - True if this arg/retval requires special handling.
54193323Sed  bool isCustom : 1;
55193323Sed
56193323Sed  /// Information about how the value is assigned.
57193323Sed  LocInfo HTP : 6;
58198090Srdivacky
59193323Sed  /// ValVT - The type of the value being assigned.
60198090Srdivacky  EVT ValVT;
61193323Sed
62193323Sed  /// LocVT - The type of the location being assigned to.
63198090Srdivacky  EVT LocVT;
64193323Sedpublic:
65198090Srdivacky
66198090Srdivacky  static CCValAssign getReg(unsigned ValNo, EVT ValVT,
67198090Srdivacky                            unsigned RegNo, EVT LocVT,
68193323Sed                            LocInfo HTP) {
69193323Sed    CCValAssign Ret;
70193323Sed    Ret.ValNo = ValNo;
71193323Sed    Ret.Loc = RegNo;
72193323Sed    Ret.isMem = false;
73193323Sed    Ret.isCustom = false;
74193323Sed    Ret.HTP = HTP;
75193323Sed    Ret.ValVT = ValVT;
76193323Sed    Ret.LocVT = LocVT;
77193323Sed    return Ret;
78193323Sed  }
79193323Sed
80198090Srdivacky  static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
81198090Srdivacky                                  unsigned RegNo, EVT LocVT,
82193323Sed                                  LocInfo HTP) {
83193323Sed    CCValAssign Ret;
84193323Sed    Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
85193323Sed    Ret.isCustom = true;
86193323Sed    return Ret;
87193323Sed  }
88193323Sed
89198090Srdivacky  static CCValAssign getMem(unsigned ValNo, EVT ValVT,
90198090Srdivacky                            unsigned Offset, EVT LocVT,
91193323Sed                            LocInfo HTP) {
92193323Sed    CCValAssign Ret;
93193323Sed    Ret.ValNo = ValNo;
94193323Sed    Ret.Loc = Offset;
95193323Sed    Ret.isMem = true;
96193323Sed    Ret.isCustom = false;
97193323Sed    Ret.HTP = HTP;
98193323Sed    Ret.ValVT = ValVT;
99193323Sed    Ret.LocVT = LocVT;
100193323Sed    return Ret;
101193323Sed  }
102198090Srdivacky
103198090Srdivacky  static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
104198090Srdivacky                                  unsigned Offset, EVT LocVT,
105193323Sed                                  LocInfo HTP) {
106193323Sed    CCValAssign Ret;
107193323Sed    Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
108193323Sed    Ret.isCustom = true;
109193323Sed    return Ret;
110193323Sed  }
111193323Sed
112193323Sed  unsigned getValNo() const { return ValNo; }
113198090Srdivacky  EVT getValVT() const { return ValVT; }
114193323Sed
115193323Sed  bool isRegLoc() const { return !isMem; }
116193323Sed  bool isMemLoc() const { return isMem; }
117198090Srdivacky
118193323Sed  bool needsCustom() const { return isCustom; }
119193323Sed
120193323Sed  unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
121193323Sed  unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
122198090Srdivacky  EVT getLocVT() const { return LocVT; }
123198090Srdivacky
124193323Sed  LocInfo getLocInfo() const { return HTP; }
125198090Srdivacky  bool isExtInLoc() const {
126198090Srdivacky    return (HTP == AExt || HTP == SExt || HTP == ZExt);
127198090Srdivacky  }
128198090Srdivacky
129193323Sed};
130193323Sed
131193323Sed/// CCAssignFn - This function assigns a location for Val, updating State to
132193323Sed/// reflect the change.
133198090Srdivackytypedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
134198090Srdivacky                        EVT LocVT, CCValAssign::LocInfo LocInfo,
135193323Sed                        ISD::ArgFlagsTy ArgFlags, CCState &State);
136193323Sed
137193323Sed/// CCCustomFn - This function assigns a location for Val, possibly updating
138193323Sed/// all args to reflect changes and indicates if it handled it. It must set
139193323Sed/// isCustom if it handles the arg and returns true.
140198090Srdivackytypedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
141198090Srdivacky                        EVT &LocVT, CCValAssign::LocInfo &LocInfo,
142193323Sed                        ISD::ArgFlagsTy &ArgFlags, CCState &State);
143193323Sed
144193323Sed/// CCState - This class holds information needed while lowering arguments and
145193323Sed/// return values.  It captures which registers are already assigned and which
146193323Sed/// stack slots are used.  It provides accessors to allocate these values.
147193323Sedclass CCState {
148198090Srdivacky  CallingConv::ID CallingConv;
149193323Sed  bool IsVarArg;
150193323Sed  const TargetMachine &TM;
151193323Sed  const TargetRegisterInfo &TRI;
152193323Sed  SmallVector<CCValAssign, 16> &Locs;
153198090Srdivacky  LLVMContext &Context;
154198090Srdivacky
155193323Sed  unsigned StackOffset;
156193323Sed  SmallVector<uint32_t, 16> UsedRegs;
157193323Sedpublic:
158198090Srdivacky  CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &TM,
159198090Srdivacky          SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
160198090Srdivacky
161193323Sed  void addLoc(const CCValAssign &V) {
162193323Sed    Locs.push_back(V);
163193323Sed  }
164198090Srdivacky
165198090Srdivacky  LLVMContext &getContext() const { return Context; }
166193323Sed  const TargetMachine &getTarget() const { return TM; }
167198090Srdivacky  CallingConv::ID getCallingConv() const { return CallingConv; }
168193323Sed  bool isVarArg() const { return IsVarArg; }
169198090Srdivacky
170193323Sed  unsigned getNextStackOffset() const { return StackOffset; }
171193323Sed
172193323Sed  /// isAllocated - Return true if the specified register (or an alias) is
173193323Sed  /// allocated.
174193323Sed  bool isAllocated(unsigned Reg) const {
175193323Sed    return UsedRegs[Reg/32] & (1 << (Reg&31));
176193323Sed  }
177198090Srdivacky
178198090Srdivacky  /// AnalyzeFormalArguments - Analyze an array of argument values,
179193323Sed  /// incorporating info about the formals into this state.
180198090Srdivacky  void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
181198090Srdivacky                              CCAssignFn Fn);
182198090Srdivacky
183198090Srdivacky  /// AnalyzeReturn - Analyze the returned values of a return,
184193323Sed  /// incorporating info about the result values into this state.
185198090Srdivacky  void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
186198090Srdivacky                     CCAssignFn Fn);
187193323Sed
188199481Srdivacky  /// CheckReturn - Analyze the return values of a function, returning
189199481Srdivacky  /// true if the return can be performed without sret-demotion, and
190199481Srdivacky  /// false otherwise.
191210299Sed  bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
192199481Srdivacky                   CCAssignFn Fn);
193199481Srdivacky
194198090Srdivacky  /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
195198090Srdivacky  /// incorporating info about the passed values into this state.
196198090Srdivacky  void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
197198090Srdivacky                           CCAssignFn Fn);
198198090Srdivacky
199193323Sed  /// AnalyzeCallOperands - Same as above except it takes vectors of types
200193323Sed  /// and argument flags.
201198090Srdivacky  void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
202193323Sed                           SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
203193323Sed                           CCAssignFn Fn);
204193323Sed
205198090Srdivacky  /// AnalyzeCallResult - Analyze the return values of a call,
206193323Sed  /// incorporating info about the passed values into this state.
207198090Srdivacky  void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
208198090Srdivacky                         CCAssignFn Fn);
209198090Srdivacky
210193323Sed  /// AnalyzeCallResult - Same as above except it's specialized for calls which
211193323Sed  /// produce a single value.
212198090Srdivacky  void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
213193323Sed
214193323Sed  /// getFirstUnallocated - Return the first unallocated register in the set, or
215193323Sed  /// NumRegs if they are all allocated.
216193323Sed  unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
217193323Sed    for (unsigned i = 0; i != NumRegs; ++i)
218193323Sed      if (!isAllocated(Regs[i]))
219193323Sed        return i;
220193323Sed    return NumRegs;
221193323Sed  }
222198090Srdivacky
223193323Sed  /// AllocateReg - Attempt to allocate one register.  If it is not available,
224193323Sed  /// return zero.  Otherwise, return the register, marking it and any aliases
225193323Sed  /// as allocated.
226193323Sed  unsigned AllocateReg(unsigned Reg) {
227193323Sed    if (isAllocated(Reg)) return 0;
228193323Sed    MarkAllocated(Reg);
229193323Sed    return Reg;
230193323Sed  }
231193323Sed
232193323Sed  /// Version of AllocateReg with extra register to be shadowed.
233193323Sed  unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
234193323Sed    if (isAllocated(Reg)) return 0;
235193323Sed    MarkAllocated(Reg);
236193323Sed    MarkAllocated(ShadowReg);
237193323Sed    return Reg;
238193323Sed  }
239193323Sed
240193323Sed  /// AllocateReg - Attempt to allocate one of the specified registers.  If none
241193323Sed  /// are available, return zero.  Otherwise, return the first one available,
242193323Sed  /// marking it and any aliases as allocated.
243193323Sed  unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
244193323Sed    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
245193323Sed    if (FirstUnalloc == NumRegs)
246193323Sed      return 0;    // Didn't find the reg.
247193323Sed
248193323Sed    // Mark the register and any aliases as allocated.
249193323Sed    unsigned Reg = Regs[FirstUnalloc];
250193323Sed    MarkAllocated(Reg);
251193323Sed    return Reg;
252193323Sed  }
253193323Sed
254193323Sed  /// Version of AllocateReg with list of registers to be shadowed.
255193323Sed  unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
256193323Sed                       unsigned NumRegs) {
257193323Sed    unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
258193323Sed    if (FirstUnalloc == NumRegs)
259193323Sed      return 0;    // Didn't find the reg.
260193323Sed
261193323Sed    // Mark the register and any aliases as allocated.
262193323Sed    unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
263193323Sed    MarkAllocated(Reg);
264193323Sed    MarkAllocated(ShadowReg);
265193323Sed    return Reg;
266193323Sed  }
267193323Sed
268193323Sed  /// AllocateStack - Allocate a chunk of stack space with the specified size
269193323Sed  /// and alignment.
270193323Sed  unsigned AllocateStack(unsigned Size, unsigned Align) {
271193323Sed    assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
272193323Sed    StackOffset = ((StackOffset + Align-1) & ~(Align-1));
273193323Sed    unsigned Result = StackOffset;
274193323Sed    StackOffset += Size;
275193323Sed    return Result;
276193323Sed  }
277193323Sed
278212904Sdim  /// Version of AllocateStack with extra register to be shadowed.
279212904Sdim  unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
280212904Sdim    MarkAllocated(ShadowReg);
281212904Sdim    return AllocateStack(Size, Align);
282212904Sdim  }
283212904Sdim
284193323Sed  // HandleByVal - Allocate a stack slot large enough to pass an argument by
285193323Sed  // value. The size and alignment information of the argument is encoded in its
286193323Sed  // parameter attribute.
287198090Srdivacky  void HandleByVal(unsigned ValNo, EVT ValVT,
288198090Srdivacky                   EVT LocVT, CCValAssign::LocInfo LocInfo,
289193323Sed                   int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
290193323Sed
291193323Sedprivate:
292193323Sed  /// MarkAllocated - Mark a register and all of its aliases as allocated.
293193323Sed  void MarkAllocated(unsigned Reg);
294193323Sed};
295193323Sed
296193323Sed
297193323Sed
298193323Sed} // end namespace llvm
299193323Sed
300193323Sed#endif
301