AArch64: treat [N x Ty] as a block during procedure calls.
[oota-llvm.git] / include / llvm / CodeGen / CallingConvLower.h
1 //===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
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 declares the CCState and CCValAssign classes, used for lowering
11 // and implementing calling conventions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
16 #define LLVM_CODEGEN_CALLINGCONVLOWER_H
17
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/Target/TargetCallingConv.h"
23
24 namespace llvm {
25 class CCState;
26 class MVT;
27 class TargetMachine;
28 class TargetRegisterInfo;
29
30 /// CCValAssign - Represent assignment of one arg/retval to a location.
31 class CCValAssign {
32 public:
33   enum LocInfo {
34     Full,      // The value fills the full location.
35     SExt,      // The value is sign extended in the location.
36     ZExt,      // The value is zero extended in the location.
37     AExt,      // The value is extended with undefined upper bits.
38     SExtUpper, // The value is in the upper bits of the location and should be
39                // sign extended when retrieved.
40     ZExtUpper, // The value is in the upper bits of the location and should be
41                // zero extended when retrieved.
42     AExtUpper, // The value is in the upper bits of the location and should be
43                // extended with undefined upper bits when retrieved.
44     BCvt,      // The value is bit-converted in the location.
45     VExt,      // The value is vector-widened in the location.
46                // FIXME: Not implemented yet. Code that uses AExt to mean
47                // vector-widen should be fixed to use VExt instead.
48     FPExt,     // The floating-point value is fp-extended in the location.
49     Indirect   // The location contains pointer to the value.
50     // TODO: a subset of the value is in the location.
51   };
52
53 private:
54   /// ValNo - This is the value number begin assigned (e.g. an argument number).
55   unsigned ValNo;
56
57   /// Loc is either a stack offset or a register number.
58   unsigned Loc;
59
60   /// isMem - True if this is a memory loc, false if it is a register loc.
61   unsigned isMem : 1;
62
63   /// isCustom - True if this arg/retval requires special handling.
64   unsigned isCustom : 1;
65
66   /// Information about how the value is assigned.
67   LocInfo HTP : 6;
68
69   /// ValVT - The type of the value being assigned.
70   MVT ValVT;
71
72   /// LocVT - The type of the location being assigned to.
73   MVT LocVT;
74 public:
75
76   static CCValAssign getReg(unsigned ValNo, MVT ValVT,
77                             unsigned RegNo, MVT LocVT,
78                             LocInfo HTP) {
79     CCValAssign Ret;
80     Ret.ValNo = ValNo;
81     Ret.Loc = RegNo;
82     Ret.isMem = false;
83     Ret.isCustom = false;
84     Ret.HTP = HTP;
85     Ret.ValVT = ValVT;
86     Ret.LocVT = LocVT;
87     return Ret;
88   }
89
90   static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
91                                   unsigned RegNo, MVT LocVT,
92                                   LocInfo HTP) {
93     CCValAssign Ret;
94     Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
95     Ret.isCustom = true;
96     return Ret;
97   }
98
99   static CCValAssign getMem(unsigned ValNo, MVT ValVT,
100                             unsigned Offset, MVT LocVT,
101                             LocInfo HTP) {
102     CCValAssign Ret;
103     Ret.ValNo = ValNo;
104     Ret.Loc = Offset;
105     Ret.isMem = true;
106     Ret.isCustom = false;
107     Ret.HTP = HTP;
108     Ret.ValVT = ValVT;
109     Ret.LocVT = LocVT;
110     return Ret;
111   }
112
113   static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
114                                   unsigned Offset, MVT LocVT,
115                                   LocInfo HTP) {
116     CCValAssign Ret;
117     Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
118     Ret.isCustom = true;
119     return Ret;
120   }
121
122   // There is no need to differentiate between a pending CCValAssign and other
123   // kinds, as they are stored in a different list.
124   static CCValAssign getPending(unsigned ValNo, MVT ValVT, MVT LocVT,
125                                 LocInfo HTP) {
126     return getReg(ValNo, ValVT, 0, LocVT, HTP);
127   }
128
129   void convertToReg(unsigned RegNo) {
130     Loc = RegNo;
131     isMem = false;
132   }
133
134   void convertToMem(unsigned Offset) {
135     Loc = Offset;
136     isMem = true;
137   }
138
139   unsigned getValNo() const { return ValNo; }
140   MVT getValVT() const { return ValVT; }
141
142   bool isRegLoc() const { return !isMem; }
143   bool isMemLoc() const { return isMem; }
144
145   bool needsCustom() const { return isCustom; }
146
147   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
148   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
149   MVT getLocVT() const { return LocVT; }
150
151   LocInfo getLocInfo() const { return HTP; }
152   bool isExtInLoc() const {
153     return (HTP == AExt || HTP == SExt || HTP == ZExt);
154   }
155
156   bool isUpperBitsInLoc() const {
157     return HTP == AExtUpper || HTP == SExtUpper || HTP == ZExtUpper;
158   }
159 };
160
161 /// CCAssignFn - This function assigns a location for Val, updating State to
162 /// reflect the change.  It returns 'true' if it failed to handle Val.
163 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
164                         MVT LocVT, CCValAssign::LocInfo LocInfo,
165                         ISD::ArgFlagsTy ArgFlags, CCState &State);
166
167 /// CCCustomFn - This function assigns a location for Val, possibly updating
168 /// all args to reflect changes and indicates if it handled it. It must set
169 /// isCustom if it handles the arg and returns true.
170 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
171                         MVT &LocVT, CCValAssign::LocInfo &LocInfo,
172                         ISD::ArgFlagsTy &ArgFlags, CCState &State);
173
174 /// ParmContext - This enum tracks whether calling convention lowering is in
175 /// the context of prologue or call generation. Not all backends make use of
176 /// this information.
177 typedef enum { Unknown, Prologue, Call } ParmContext;
178
179 /// CCState - This class holds information needed while lowering arguments and
180 /// return values.  It captures which registers are already assigned and which
181 /// stack slots are used.  It provides accessors to allocate these values.
182 class CCState {
183 private:
184   CallingConv::ID CallingConv;
185   bool IsVarArg;
186   MachineFunction &MF;
187   const TargetRegisterInfo &TRI;
188   SmallVectorImpl<CCValAssign> &Locs;
189   LLVMContext &Context;
190
191   unsigned StackOffset;
192   SmallVector<uint32_t, 16> UsedRegs;
193   SmallVector<CCValAssign, 4> PendingLocs;
194
195   // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
196   //
197   // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
198   // tracking.
199   // Or, in another words it tracks byval parameters that are stored in
200   // general purpose registers.
201   //
202   // For 4 byte stack alignment,
203   // instance index means byval parameter number in formal
204   // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
205   // then, for function "foo":
206   //
207   // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
208   //
209   // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
210   // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
211   //
212   // In case of 8 bytes stack alignment,
213   // ByValRegs may also contain information about wasted registers.
214   // In function shown above, r3 would be wasted according to AAPCS rules.
215   // And in that case ByValRegs[1].Waste would be "true".
216   // ByValRegs vector size still would be 2,
217   // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
218   //
219   // Supposed use-case for this collection:
220   // 1. Initially ByValRegs is empty, InRegsParamsProcessed is 0.
221   // 2. HandleByVal fillups ByValRegs.
222   // 3. Argument analysis (LowerFormatArguments, for example). After
223   // some byval argument was analyzed, InRegsParamsProcessed is increased.
224   struct ByValInfo {
225     ByValInfo(unsigned B, unsigned E, bool IsWaste = false) :
226       Begin(B), End(E), Waste(IsWaste) {}
227     // First register allocated for current parameter.
228     unsigned Begin;
229
230     // First after last register allocated for current parameter.
231     unsigned End;
232
233     // Means that current range of registers doesn't belong to any
234     // parameters. It was wasted due to stack alignment rules.
235     // For more information see:
236     // AAPCS, 5.5 Parameter Passing, Stage C, C.3.
237     bool Waste;
238   };
239   SmallVector<ByValInfo, 4 > ByValRegs;
240
241   // InRegsParamsProcessed - shows how many instances of ByValRegs was proceed
242   // during argument analysis.
243   unsigned InRegsParamsProcessed;
244
245 protected:
246   ParmContext CallOrPrologue;
247
248 public:
249   CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
250           SmallVectorImpl<CCValAssign> &locs, LLVMContext &C);
251
252   void addLoc(const CCValAssign &V) {
253     Locs.push_back(V);
254   }
255
256   LLVMContext &getContext() const { return Context; }
257   MachineFunction &getMachineFunction() const { return MF; }
258   CallingConv::ID getCallingConv() const { return CallingConv; }
259   bool isVarArg() const { return IsVarArg; }
260
261   unsigned getNextStackOffset() const { return StackOffset; }
262
263   /// isAllocated - Return true if the specified register (or an alias) is
264   /// allocated.
265   bool isAllocated(unsigned Reg) const {
266     return UsedRegs[Reg/32] & (1 << (Reg&31));
267   }
268
269   /// AnalyzeFormalArguments - Analyze an array of argument values,
270   /// incorporating info about the formals into this state.
271   void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
272                               CCAssignFn Fn);
273
274   /// AnalyzeReturn - Analyze the returned values of a return,
275   /// incorporating info about the result values into this state.
276   void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
277                      CCAssignFn Fn);
278
279   /// CheckReturn - Analyze the return values of a function, returning
280   /// true if the return can be performed without sret-demotion, and
281   /// false otherwise.
282   bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
283                    CCAssignFn Fn);
284
285   /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
286   /// incorporating info about the passed values into this state.
287   void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
288                            CCAssignFn Fn);
289
290   /// AnalyzeCallOperands - Same as above except it takes vectors of types
291   /// and argument flags.
292   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
293                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
294                            CCAssignFn Fn);
295
296   /// AnalyzeCallResult - Analyze the return values of a call,
297   /// incorporating info about the passed values into this state.
298   void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
299                          CCAssignFn Fn);
300
301   /// AnalyzeCallResult - Same as above except it's specialized for calls which
302   /// produce a single value.
303   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
304
305   /// getFirstUnallocated - Return the first unallocated register in the set, or
306   /// NumRegs if they are all allocated.
307   unsigned getFirstUnallocated(const MCPhysReg *Regs, unsigned NumRegs) const {
308     for (unsigned i = 0; i != NumRegs; ++i)
309       if (!isAllocated(Regs[i]))
310         return i;
311     return NumRegs;
312   }
313
314   /// AllocateReg - Attempt to allocate one register.  If it is not available,
315   /// return zero.  Otherwise, return the register, marking it and any aliases
316   /// as allocated.
317   unsigned AllocateReg(unsigned Reg) {
318     if (isAllocated(Reg)) return 0;
319     MarkAllocated(Reg);
320     return Reg;
321   }
322
323   /// Version of AllocateReg with extra register to be shadowed.
324   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
325     if (isAllocated(Reg)) return 0;
326     MarkAllocated(Reg);
327     MarkAllocated(ShadowReg);
328     return Reg;
329   }
330
331   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
332   /// are available, return zero.  Otherwise, return the first one available,
333   /// marking it and any aliases as allocated.
334   unsigned AllocateReg(const MCPhysReg *Regs, unsigned NumRegs) {
335     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
336     if (FirstUnalloc == NumRegs)
337       return 0;    // Didn't find the reg.
338
339     // Mark the register and any aliases as allocated.
340     unsigned Reg = Regs[FirstUnalloc];
341     MarkAllocated(Reg);
342     return Reg;
343   }
344
345   /// AllocateRegBlock - Attempt to allocate a block of RegsRequired consecutive
346   /// registers. If this is not possible, return zero. Otherwise, return the first
347   /// register of the block that were allocated, marking the entire block as allocated.
348   unsigned AllocateRegBlock(ArrayRef<const uint16_t> Regs,
349                             unsigned RegsRequired) {
350     if (RegsRequired > Regs.size())
351       return 0;
352
353     for (unsigned StartIdx = 0; StartIdx <= Regs.size() - RegsRequired;
354          ++StartIdx) {
355       bool BlockAvailable = true;
356       // Check for already-allocated regs in this block
357       for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
358         if (isAllocated(Regs[StartIdx + BlockIdx])) {
359           BlockAvailable = false;
360           break;
361         }
362       }
363       if (BlockAvailable) {
364         // Mark the entire block as allocated
365         for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
366           MarkAllocated(Regs[StartIdx + BlockIdx]);
367         }
368         return Regs[StartIdx];
369       }
370     }
371     // No block was available
372     return 0;
373   }
374
375   /// Version of AllocateReg with list of registers to be shadowed.
376   unsigned AllocateReg(const MCPhysReg *Regs, const MCPhysReg *ShadowRegs,
377                        unsigned NumRegs) {
378     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
379     if (FirstUnalloc == NumRegs)
380       return 0;    // Didn't find the reg.
381
382     // Mark the register and any aliases as allocated.
383     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
384     MarkAllocated(Reg);
385     MarkAllocated(ShadowReg);
386     return Reg;
387   }
388
389   /// AllocateStack - Allocate a chunk of stack space with the specified size
390   /// and alignment.
391   unsigned AllocateStack(unsigned Size, unsigned Align) {
392     assert(Align && ((Align - 1) & Align) == 0); // Align is power of 2.
393     StackOffset = ((StackOffset + Align - 1) & ~(Align - 1));
394     unsigned Result = StackOffset;
395     StackOffset += Size;
396     MF.getFrameInfo()->ensureMaxAlignment(Align);
397     return Result;
398   }
399
400   /// Version of AllocateStack with extra register to be shadowed.
401   unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
402     MarkAllocated(ShadowReg);
403     return AllocateStack(Size, Align);
404   }
405
406   /// Version of AllocateStack with list of extra registers to be shadowed.
407   /// Note that, unlike AllocateReg, this shadows ALL of the shadow registers.
408   unsigned AllocateStack(unsigned Size, unsigned Align,
409                          const MCPhysReg *ShadowRegs, unsigned NumShadowRegs) {
410     for (unsigned i = 0; i < NumShadowRegs; ++i)
411       MarkAllocated(ShadowRegs[i]);
412     return AllocateStack(Size, Align);
413   }
414
415   // HandleByVal - Allocate a stack slot large enough to pass an argument by
416   // value. The size and alignment information of the argument is encoded in its
417   // parameter attribute.
418   void HandleByVal(unsigned ValNo, MVT ValVT,
419                    MVT LocVT, CCValAssign::LocInfo LocInfo,
420                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
421
422   // Returns count of byval arguments that are to be stored (even partly)
423   // in registers.
424   unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
425
426   // Returns count of byval in-regs arguments proceed.
427   unsigned getInRegsParamsProcessed() const { return InRegsParamsProcessed; }
428
429   // Get information about N-th byval parameter that is stored in registers.
430   // Here "ByValParamIndex" is N.
431   void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
432                           unsigned& BeginReg, unsigned& EndReg) const {
433     assert(InRegsParamRecordIndex < ByValRegs.size() &&
434            "Wrong ByVal parameter index");
435
436     const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
437     BeginReg = info.Begin;
438     EndReg = info.End;
439   }
440
441   // Add information about parameter that is kept in registers.
442   void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
443     ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
444   }
445
446   // Goes either to next byval parameter (excluding "waste" record), or
447   // to the end of collection.
448   // Returns false, if end is reached.
449   bool nextInRegsParam() {
450     unsigned e = ByValRegs.size();
451     if (InRegsParamsProcessed < e)
452       ++InRegsParamsProcessed;
453     return InRegsParamsProcessed < e;
454   }
455
456   // Clear byval registers tracking info.
457   void clearByValRegsInfo() {
458     InRegsParamsProcessed = 0;
459     ByValRegs.clear();
460   }
461
462   // Rewind byval registers tracking info.
463   void rewindByValRegsInfo() {
464     InRegsParamsProcessed = 0;
465   }
466
467   ParmContext getCallOrPrologue() const { return CallOrPrologue; }
468
469   // Get list of pending assignments
470   SmallVectorImpl<llvm::CCValAssign> &getPendingLocs() {
471     return PendingLocs;
472   }
473
474 private:
475   /// MarkAllocated - Mark a register and all of its aliases as allocated.
476   void MarkAllocated(unsigned Reg);
477 };
478
479
480
481 } // end namespace llvm
482
483 #endif