Home | History | Annotate | Download | only in CodeGen
      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/CodeGen/ValueTypes.h"
     22 #include "llvm/IR/CallingConv.h"
     23 #include "llvm/Target/TargetCallingConv.h"
     24 
     25 namespace llvm {
     26   class TargetRegisterInfo;
     27   class TargetMachine;
     28   class CCState;
     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     BCvt,   // The value is bit-converted in the location.
     39     VExt,   // The value is vector-widened in the location.
     40             // FIXME: Not implemented yet. Code that uses AExt to mean
     41             // vector-widen should be fixed to use VExt instead.
     42     Indirect // The location contains pointer to the value.
     43     // TODO: a subset of the value is in the location.
     44   };
     45 private:
     46   /// ValNo - This is the value number begin assigned (e.g. an argument number).
     47   unsigned ValNo;
     48 
     49   /// Loc is either a stack offset or a register number.
     50   unsigned Loc;
     51 
     52   /// isMem - True if this is a memory loc, false if it is a register loc.
     53   unsigned isMem : 1;
     54 
     55   /// isCustom - True if this arg/retval requires special handling.
     56   unsigned isCustom : 1;
     57 
     58   /// Information about how the value is assigned.
     59   LocInfo HTP : 6;
     60 
     61   /// ValVT - The type of the value being assigned.
     62   MVT ValVT;
     63 
     64   /// LocVT - The type of the location being assigned to.
     65   MVT LocVT;
     66 public:
     67 
     68   static CCValAssign getReg(unsigned ValNo, MVT ValVT,
     69                             unsigned RegNo, MVT LocVT,
     70                             LocInfo HTP) {
     71     CCValAssign Ret;
     72     Ret.ValNo = ValNo;
     73     Ret.Loc = RegNo;
     74     Ret.isMem = false;
     75     Ret.isCustom = false;
     76     Ret.HTP = HTP;
     77     Ret.ValVT = ValVT;
     78     Ret.LocVT = LocVT;
     79     return Ret;
     80   }
     81 
     82   static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
     83                                   unsigned RegNo, MVT LocVT,
     84                                   LocInfo HTP) {
     85     CCValAssign Ret;
     86     Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
     87     Ret.isCustom = true;
     88     return Ret;
     89   }
     90 
     91   static CCValAssign getMem(unsigned ValNo, MVT ValVT,
     92                             unsigned Offset, MVT LocVT,
     93                             LocInfo HTP) {
     94     CCValAssign Ret;
     95     Ret.ValNo = ValNo;
     96     Ret.Loc = Offset;
     97     Ret.isMem = true;
     98     Ret.isCustom = false;
     99     Ret.HTP = HTP;
    100     Ret.ValVT = ValVT;
    101     Ret.LocVT = LocVT;
    102     return Ret;
    103   }
    104 
    105   static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
    106                                   unsigned Offset, MVT LocVT,
    107                                   LocInfo HTP) {
    108     CCValAssign Ret;
    109     Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
    110     Ret.isCustom = true;
    111     return Ret;
    112   }
    113 
    114   unsigned getValNo() const { return ValNo; }
    115   MVT getValVT() const { return ValVT; }
    116 
    117   bool isRegLoc() const { return !isMem; }
    118   bool isMemLoc() const { return isMem; }
    119 
    120   bool needsCustom() const { return isCustom; }
    121 
    122   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
    123   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
    124   MVT getLocVT() const { return LocVT; }
    125 
    126   LocInfo getLocInfo() const { return HTP; }
    127   bool isExtInLoc() const {
    128     return (HTP == AExt || HTP == SExt || HTP == ZExt);
    129   }
    130 
    131 };
    132 
    133 /// CCAssignFn - This function assigns a location for Val, updating State to
    134 /// reflect the change.  It returns 'true' if it failed to handle Val.
    135 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
    136                         MVT LocVT, CCValAssign::LocInfo LocInfo,
    137                         ISD::ArgFlagsTy ArgFlags, CCState &State);
    138 
    139 /// CCCustomFn - This function assigns a location for Val, possibly updating
    140 /// all args to reflect changes and indicates if it handled it. It must set
    141 /// isCustom if it handles the arg and returns true.
    142 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
    143                         MVT &LocVT, CCValAssign::LocInfo &LocInfo,
    144                         ISD::ArgFlagsTy &ArgFlags, CCState &State);
    145 
    146 /// ParmContext - This enum tracks whether calling convention lowering is in
    147 /// the context of prologue or call generation. Not all backends make use of
    148 /// this information.
    149 typedef enum { Unknown, Prologue, Call } ParmContext;
    150 
    151 /// CCState - This class holds information needed while lowering arguments and
    152 /// return values.  It captures which registers are already assigned and which
    153 /// stack slots are used.  It provides accessors to allocate these values.
    154 class CCState {
    155 private:
    156   CallingConv::ID CallingConv;
    157   bool IsVarArg;
    158   MachineFunction &MF;
    159   const TargetMachine &TM;
    160   const TargetRegisterInfo &TRI;
    161   SmallVector<CCValAssign, 16> &Locs;
    162   LLVMContext &Context;
    163 
    164   unsigned StackOffset;
    165   SmallVector<uint32_t, 16> UsedRegs;
    166   unsigned FirstByValReg;
    167   bool FirstByValRegValid;
    168 
    169 protected:
    170   ParmContext CallOrPrologue;
    171 
    172 public:
    173   CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
    174           const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
    175           LLVMContext &C);
    176 
    177   void addLoc(const CCValAssign &V) {
    178     Locs.push_back(V);
    179   }
    180 
    181   LLVMContext &getContext() const { return Context; }
    182   const TargetMachine &getTarget() const { return TM; }
    183   MachineFunction &getMachineFunction() const { return MF; }
    184   CallingConv::ID getCallingConv() const { return CallingConv; }
    185   bool isVarArg() const { return IsVarArg; }
    186 
    187   unsigned getNextStackOffset() const { return StackOffset; }
    188 
    189   /// isAllocated - Return true if the specified register (or an alias) is
    190   /// allocated.
    191   bool isAllocated(unsigned Reg) const {
    192     return UsedRegs[Reg/32] & (1 << (Reg&31));
    193   }
    194 
    195   /// AnalyzeFormalArguments - Analyze an array of argument values,
    196   /// incorporating info about the formals into this state.
    197   void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
    198                               CCAssignFn Fn);
    199 
    200   /// AnalyzeReturn - Analyze the returned values of a return,
    201   /// incorporating info about the result values into this state.
    202   void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
    203                      CCAssignFn Fn);
    204 
    205   /// CheckReturn - Analyze the return values of a function, returning
    206   /// true if the return can be performed without sret-demotion, and
    207   /// false otherwise.
    208   bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
    209                    CCAssignFn Fn);
    210 
    211   /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
    212   /// incorporating info about the passed values into this state.
    213   void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
    214                            CCAssignFn Fn);
    215 
    216   /// AnalyzeCallOperands - Same as above except it takes vectors of types
    217   /// and argument flags.
    218   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
    219                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
    220                            CCAssignFn Fn);
    221 
    222   /// AnalyzeCallResult - Analyze the return values of a call,
    223   /// incorporating info about the passed values into this state.
    224   void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
    225                          CCAssignFn Fn);
    226 
    227   /// AnalyzeCallResult - Same as above except it's specialized for calls which
    228   /// produce a single value.
    229   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
    230 
    231   /// getFirstUnallocated - Return the first unallocated register in the set, or
    232   /// NumRegs if they are all allocated.
    233   unsigned getFirstUnallocated(const uint16_t *Regs, unsigned NumRegs) const {
    234     for (unsigned i = 0; i != NumRegs; ++i)
    235       if (!isAllocated(Regs[i]))
    236         return i;
    237     return NumRegs;
    238   }
    239 
    240   /// AllocateReg - Attempt to allocate one register.  If it is not available,
    241   /// return zero.  Otherwise, return the register, marking it and any aliases
    242   /// as allocated.
    243   unsigned AllocateReg(unsigned Reg) {
    244     if (isAllocated(Reg)) return 0;
    245     MarkAllocated(Reg);
    246     return Reg;
    247   }
    248 
    249   /// Version of AllocateReg with extra register to be shadowed.
    250   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
    251     if (isAllocated(Reg)) return 0;
    252     MarkAllocated(Reg);
    253     MarkAllocated(ShadowReg);
    254     return Reg;
    255   }
    256 
    257   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
    258   /// are available, return zero.  Otherwise, return the first one available,
    259   /// marking it and any aliases as allocated.
    260   unsigned AllocateReg(const uint16_t *Regs, unsigned NumRegs) {
    261     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
    262     if (FirstUnalloc == NumRegs)
    263       return 0;    // Didn't find the reg.
    264 
    265     // Mark the register and any aliases as allocated.
    266     unsigned Reg = Regs[FirstUnalloc];
    267     MarkAllocated(Reg);
    268     return Reg;
    269   }
    270 
    271   /// Version of AllocateReg with list of registers to be shadowed.
    272   unsigned AllocateReg(const uint16_t *Regs, const uint16_t *ShadowRegs,
    273                        unsigned NumRegs) {
    274     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
    275     if (FirstUnalloc == NumRegs)
    276       return 0;    // Didn't find the reg.
    277 
    278     // Mark the register and any aliases as allocated.
    279     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
    280     MarkAllocated(Reg);
    281     MarkAllocated(ShadowReg);
    282     return Reg;
    283   }
    284 
    285   /// AllocateStack - Allocate a chunk of stack space with the specified size
    286   /// and alignment.
    287   unsigned AllocateStack(unsigned Size, unsigned Align) {
    288     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
    289     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
    290     unsigned Result = StackOffset;
    291     StackOffset += Size;
    292     MF.getFrameInfo()->ensureMaxAlignment(Align);
    293     return Result;
    294   }
    295 
    296   /// Version of AllocateStack with extra register to be shadowed.
    297   unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
    298     MarkAllocated(ShadowReg);
    299     return AllocateStack(Size, Align);
    300   }
    301 
    302   // HandleByVal - Allocate a stack slot large enough to pass an argument by
    303   // value. The size and alignment information of the argument is encoded in its
    304   // parameter attribute.
    305   void HandleByVal(unsigned ValNo, MVT ValVT,
    306                    MVT LocVT, CCValAssign::LocInfo LocInfo,
    307                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
    308 
    309   // First GPR that carries part of a byval aggregate that's split
    310   // between registers and memory.
    311   unsigned getFirstByValReg() const { return FirstByValRegValid ? FirstByValReg : 0; }
    312   void setFirstByValReg(unsigned r) { FirstByValReg = r; FirstByValRegValid = true; }
    313   void clearFirstByValReg() { FirstByValReg = 0; FirstByValRegValid = false; }
    314   bool isFirstByValRegValid() const { return FirstByValRegValid; }
    315 
    316   ParmContext getCallOrPrologue() const { return CallOrPrologue; }
    317 
    318 private:
    319   /// MarkAllocated - Mark a register and all of its aliases as allocated.
    320   void MarkAllocated(unsigned Reg);
    321 };
    322 
    323 
    324 
    325 } // end namespace llvm
    326 
    327 #endif
    328