LLVM API Documentation

CallingConvLower.h
Go to the documentation of this file.
00001 //===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the CCState and CCValAssign classes, used for lowering
00011 // and implementing calling conventions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
00016 #define LLVM_CODEGEN_CALLINGCONVLOWER_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/CodeGen/MachineFrameInfo.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/IR/CallingConv.h"
00022 #include "llvm/Target/TargetCallingConv.h"
00023 
00024 namespace llvm {
00025 class CCState;
00026 class MVT;
00027 class TargetMachine;
00028 class TargetRegisterInfo;
00029 
00030 /// CCValAssign - Represent assignment of one arg/retval to a location.
00031 class CCValAssign {
00032 public:
00033   enum LocInfo {
00034     Full,   // The value fills the full location.
00035     SExt,   // The value is sign extended in the location.
00036     ZExt,   // The value is zero extended in the location.
00037     AExt,   // The value is extended with undefined upper bits.
00038     BCvt,   // The value is bit-converted in the location.
00039     VExt,   // The value is vector-widened in the location.
00040             // FIXME: Not implemented yet. Code that uses AExt to mean
00041             // vector-widen should be fixed to use VExt instead.
00042     FPExt,  // The floating-point value is fp-extended in the location.
00043     Indirect // The location contains pointer to the value.
00044     // TODO: a subset of the value is in the location.
00045   };
00046 private:
00047   /// ValNo - This is the value number begin assigned (e.g. an argument number).
00048   unsigned ValNo;
00049 
00050   /// Loc is either a stack offset or a register number.
00051   unsigned Loc;
00052 
00053   /// isMem - True if this is a memory loc, false if it is a register loc.
00054   unsigned isMem : 1;
00055 
00056   /// isCustom - True if this arg/retval requires special handling.
00057   unsigned isCustom : 1;
00058 
00059   /// Information about how the value is assigned.
00060   LocInfo HTP : 6;
00061 
00062   /// ValVT - The type of the value being assigned.
00063   MVT ValVT;
00064 
00065   /// LocVT - The type of the location being assigned to.
00066   MVT LocVT;
00067 public:
00068 
00069   static CCValAssign getReg(unsigned ValNo, MVT ValVT,
00070                             unsigned RegNo, MVT LocVT,
00071                             LocInfo HTP) {
00072     CCValAssign Ret;
00073     Ret.ValNo = ValNo;
00074     Ret.Loc = RegNo;
00075     Ret.isMem = false;
00076     Ret.isCustom = false;
00077     Ret.HTP = HTP;
00078     Ret.ValVT = ValVT;
00079     Ret.LocVT = LocVT;
00080     return Ret;
00081   }
00082 
00083   static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
00084                                   unsigned RegNo, MVT LocVT,
00085                                   LocInfo HTP) {
00086     CCValAssign Ret;
00087     Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
00088     Ret.isCustom = true;
00089     return Ret;
00090   }
00091 
00092   static CCValAssign getMem(unsigned ValNo, MVT ValVT,
00093                             unsigned Offset, MVT LocVT,
00094                             LocInfo HTP) {
00095     CCValAssign Ret;
00096     Ret.ValNo = ValNo;
00097     Ret.Loc = Offset;
00098     Ret.isMem = true;
00099     Ret.isCustom = false;
00100     Ret.HTP = HTP;
00101     Ret.ValVT = ValVT;
00102     Ret.LocVT = LocVT;
00103     return Ret;
00104   }
00105 
00106   static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
00107                                   unsigned Offset, MVT LocVT,
00108                                   LocInfo HTP) {
00109     CCValAssign Ret;
00110     Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
00111     Ret.isCustom = true;
00112     return Ret;
00113   }
00114 
00115   unsigned getValNo() const { return ValNo; }
00116   MVT getValVT() const { return ValVT; }
00117 
00118   bool isRegLoc() const { return !isMem; }
00119   bool isMemLoc() const { return isMem; }
00120 
00121   bool needsCustom() const { return isCustom; }
00122 
00123   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
00124   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
00125   MVT getLocVT() const { return LocVT; }
00126 
00127   LocInfo getLocInfo() const { return HTP; }
00128   bool isExtInLoc() const {
00129     return (HTP == AExt || HTP == SExt || HTP == ZExt);
00130   }
00131 
00132 };
00133 
00134 /// CCAssignFn - This function assigns a location for Val, updating State to
00135 /// reflect the change.  It returns 'true' if it failed to handle Val.
00136 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
00137                         MVT LocVT, CCValAssign::LocInfo LocInfo,
00138                         ISD::ArgFlagsTy ArgFlags, CCState &State);
00139 
00140 /// CCCustomFn - This function assigns a location for Val, possibly updating
00141 /// all args to reflect changes and indicates if it handled it. It must set
00142 /// isCustom if it handles the arg and returns true.
00143 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
00144                         MVT &LocVT, CCValAssign::LocInfo &LocInfo,
00145                         ISD::ArgFlagsTy &ArgFlags, CCState &State);
00146 
00147 /// ParmContext - This enum tracks whether calling convention lowering is in
00148 /// the context of prologue or call generation. Not all backends make use of
00149 /// this information.
00150 typedef enum { Unknown, Prologue, Call } ParmContext;
00151 
00152 /// CCState - This class holds information needed while lowering arguments and
00153 /// return values.  It captures which registers are already assigned and which
00154 /// stack slots are used.  It provides accessors to allocate these values.
00155 class CCState {
00156 private:
00157   CallingConv::ID CallingConv;
00158   bool IsVarArg;
00159   MachineFunction &MF;
00160   const TargetMachine &TM;
00161   const TargetRegisterInfo &TRI;
00162   SmallVectorImpl<CCValAssign> &Locs;
00163   LLVMContext &Context;
00164 
00165   unsigned StackOffset;
00166   SmallVector<uint32_t, 16> UsedRegs;
00167 
00168   // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
00169   //
00170   // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
00171   // tracking.
00172   // Or, in another words it tracks byval parameters that are stored in
00173   // general purpose registers.
00174   //
00175   // For 4 byte stack alignment,
00176   // instance index means byval parameter number in formal
00177   // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
00178   // then, for function "foo":
00179   //
00180   // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
00181   //
00182   // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
00183   // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
00184   //
00185   // In case of 8 bytes stack alignment,
00186   // ByValRegs may also contain information about wasted registers.
00187   // In function shown above, r3 would be wasted according to AAPCS rules.
00188   // And in that case ByValRegs[1].Waste would be "true".
00189   // ByValRegs vector size still would be 2,
00190   // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
00191   //
00192   // Supposed use-case for this collection:
00193   // 1. Initially ByValRegs is empty, InRegsParamsProceed is 0.
00194   // 2. HandleByVal fillups ByValRegs.
00195   // 3. Argument analysis (LowerFormatArguments, for example). After
00196   // some byval argument was analyzed, InRegsParamsProceed is increased.
00197   struct ByValInfo {
00198     ByValInfo(unsigned B, unsigned E, bool IsWaste = false) :
00199       Begin(B), End(E), Waste(IsWaste) {}
00200     // First register allocated for current parameter.
00201     unsigned Begin;
00202 
00203     // First after last register allocated for current parameter.
00204     unsigned End;
00205 
00206     // Means that current range of registers doesn't belong to any
00207     // parameters. It was wasted due to stack alignment rules.
00208     // For more information see:
00209     // AAPCS, 5.5 Parameter Passing, Stage C, C.3.
00210     bool Waste;
00211   };
00212   SmallVector<ByValInfo, 4 > ByValRegs;
00213 
00214   // InRegsParamsProceed - shows how many instances of ByValRegs was proceed
00215   // during argument analysis.
00216   unsigned InRegsParamsProceed;
00217 
00218 protected:
00219   ParmContext CallOrPrologue;
00220 
00221 public:
00222   CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
00223           const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
00224           LLVMContext &C);
00225 
00226   void addLoc(const CCValAssign &V) {
00227     Locs.push_back(V);
00228   }
00229 
00230   LLVMContext &getContext() const { return Context; }
00231   const TargetMachine &getTarget() const { return TM; }
00232   MachineFunction &getMachineFunction() const { return MF; }
00233   CallingConv::ID getCallingConv() const { return CallingConv; }
00234   bool isVarArg() const { return IsVarArg; }
00235 
00236   unsigned getNextStackOffset() const { return StackOffset; }
00237 
00238   /// isAllocated - Return true if the specified register (or an alias) is
00239   /// allocated.
00240   bool isAllocated(unsigned Reg) const {
00241     return UsedRegs[Reg/32] & (1 << (Reg&31));
00242   }
00243 
00244   /// AnalyzeFormalArguments - Analyze an array of argument values,
00245   /// incorporating info about the formals into this state.
00246   void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
00247                               CCAssignFn Fn);
00248 
00249   /// AnalyzeReturn - Analyze the returned values of a return,
00250   /// incorporating info about the result values into this state.
00251   void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
00252                      CCAssignFn Fn);
00253 
00254   /// CheckReturn - Analyze the return values of a function, returning
00255   /// true if the return can be performed without sret-demotion, and
00256   /// false otherwise.
00257   bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
00258                    CCAssignFn Fn);
00259 
00260   /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
00261   /// incorporating info about the passed values into this state.
00262   void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
00263                            CCAssignFn Fn);
00264 
00265   /// AnalyzeCallOperands - Same as above except it takes vectors of types
00266   /// and argument flags.
00267   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
00268                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00269                            CCAssignFn Fn);
00270 
00271   /// AnalyzeCallResult - Analyze the return values of a call,
00272   /// incorporating info about the passed values into this state.
00273   void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
00274                          CCAssignFn Fn);
00275 
00276   /// AnalyzeCallResult - Same as above except it's specialized for calls which
00277   /// produce a single value.
00278   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
00279 
00280   /// getFirstUnallocated - Return the first unallocated register in the set, or
00281   /// NumRegs if they are all allocated.
00282   unsigned getFirstUnallocated(const MCPhysReg *Regs, unsigned NumRegs) const {
00283     for (unsigned i = 0; i != NumRegs; ++i)
00284       if (!isAllocated(Regs[i]))
00285         return i;
00286     return NumRegs;
00287   }
00288 
00289   /// AllocateReg - Attempt to allocate one register.  If it is not available,
00290   /// return zero.  Otherwise, return the register, marking it and any aliases
00291   /// as allocated.
00292   unsigned AllocateReg(unsigned Reg) {
00293     if (isAllocated(Reg)) return 0;
00294     MarkAllocated(Reg);
00295     return Reg;
00296   }
00297 
00298   /// Version of AllocateReg with extra register to be shadowed.
00299   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
00300     if (isAllocated(Reg)) return 0;
00301     MarkAllocated(Reg);
00302     MarkAllocated(ShadowReg);
00303     return Reg;
00304   }
00305 
00306   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
00307   /// are available, return zero.  Otherwise, return the first one available,
00308   /// marking it and any aliases as allocated.
00309   unsigned AllocateReg(const MCPhysReg *Regs, unsigned NumRegs) {
00310     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00311     if (FirstUnalloc == NumRegs)
00312       return 0;    // Didn't find the reg.
00313 
00314     // Mark the register and any aliases as allocated.
00315     unsigned Reg = Regs[FirstUnalloc];
00316     MarkAllocated(Reg);
00317     return Reg;
00318   }
00319 
00320   /// Version of AllocateReg with list of registers to be shadowed.
00321   unsigned AllocateReg(const MCPhysReg *Regs, const MCPhysReg *ShadowRegs,
00322                        unsigned NumRegs) {
00323     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00324     if (FirstUnalloc == NumRegs)
00325       return 0;    // Didn't find the reg.
00326 
00327     // Mark the register and any aliases as allocated.
00328     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
00329     MarkAllocated(Reg);
00330     MarkAllocated(ShadowReg);
00331     return Reg;
00332   }
00333 
00334   /// AllocateStack - Allocate a chunk of stack space with the specified size
00335   /// and alignment.
00336   unsigned AllocateStack(unsigned Size, unsigned Align) {
00337     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
00338     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
00339     unsigned Result = StackOffset;
00340     StackOffset += Size;
00341     MF.getFrameInfo()->ensureMaxAlignment(Align);
00342     return Result;
00343   }
00344 
00345   /// Version of AllocateStack with extra register to be shadowed.
00346   unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
00347     MarkAllocated(ShadowReg);
00348     return AllocateStack(Size, Align);
00349   }
00350 
00351   /// Version of AllocateStack with list of extra registers to be shadowed.
00352   /// Note that, unlike AllocateReg, this shadows ALL of the shadow registers.
00353   unsigned AllocateStack(unsigned Size, unsigned Align,
00354                          const MCPhysReg *ShadowRegs, unsigned NumShadowRegs) {
00355     for (unsigned i = 0; i < NumShadowRegs; ++i)
00356       MarkAllocated(ShadowRegs[i]);
00357     return AllocateStack(Size, Align);
00358   }
00359 
00360   // HandleByVal - Allocate a stack slot large enough to pass an argument by
00361   // value. The size and alignment information of the argument is encoded in its
00362   // parameter attribute.
00363   void HandleByVal(unsigned ValNo, MVT ValVT,
00364                    MVT LocVT, CCValAssign::LocInfo LocInfo,
00365                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
00366 
00367   // Returns count of byval arguments that are to be stored (even partly)
00368   // in registers.
00369   unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
00370 
00371   // Returns count of byval in-regs arguments proceed.
00372   unsigned getInRegsParamsProceed() const { return InRegsParamsProceed; }
00373 
00374   // Get information about N-th byval parameter that is stored in registers.
00375   // Here "ByValParamIndex" is N.
00376   void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
00377                           unsigned& BeginReg, unsigned& EndReg) const {
00378     assert(InRegsParamRecordIndex < ByValRegs.size() &&
00379            "Wrong ByVal parameter index");
00380 
00381     const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
00382     BeginReg = info.Begin;
00383     EndReg = info.End;
00384   }
00385 
00386   // Add information about parameter that is kept in registers.
00387   void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
00388     ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
00389   }
00390 
00391   // Goes either to next byval parameter (excluding "waste" record), or
00392   // to the end of collection.
00393   // Returns false, if end is reached.
00394   bool nextInRegsParam() {
00395     unsigned e = ByValRegs.size();
00396     if (InRegsParamsProceed < e)
00397       ++InRegsParamsProceed;
00398     return InRegsParamsProceed < e;
00399   }
00400 
00401   // Clear byval registers tracking info.
00402   void clearByValRegsInfo() {
00403     InRegsParamsProceed = 0;
00404     ByValRegs.clear();
00405   }
00406 
00407   // Rewind byval registers tracking info.
00408   void rewindByValRegsInfo() {
00409     InRegsParamsProceed = 0;
00410   }
00411 
00412   ParmContext getCallOrPrologue() const { return CallOrPrologue; }
00413 
00414 private:
00415   /// MarkAllocated - Mark a register and all of its aliases as allocated.
00416   void MarkAllocated(unsigned Reg);
00417 };
00418 
00419 
00420 
00421 } // end namespace llvm
00422 
00423 #endif