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   // There is no need to differentiate between a pending CCValAssign and other
00116   // kinds, as they are stored in a different list.
00117   static CCValAssign getPending(unsigned ValNo, MVT ValVT, MVT LocVT,
00118                                 LocInfo HTP) {
00119     return getReg(ValNo, ValVT, 0, LocVT, HTP);
00120   }
00121 
00122   void convertToReg(unsigned RegNo) {
00123     Loc = RegNo;
00124     isMem = false;
00125   }
00126 
00127   void convertToMem(unsigned Offset) {
00128     Loc = Offset;
00129     isMem = true;
00130   }
00131 
00132   unsigned getValNo() const { return ValNo; }
00133   MVT getValVT() const { return ValVT; }
00134 
00135   bool isRegLoc() const { return !isMem; }
00136   bool isMemLoc() const { return isMem; }
00137 
00138   bool needsCustom() const { return isCustom; }
00139 
00140   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
00141   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
00142   MVT getLocVT() const { return LocVT; }
00143 
00144   LocInfo getLocInfo() const { return HTP; }
00145   bool isExtInLoc() const {
00146     return (HTP == AExt || HTP == SExt || HTP == ZExt);
00147   }
00148 
00149 };
00150 
00151 /// CCAssignFn - This function assigns a location for Val, updating State to
00152 /// reflect the change.  It returns 'true' if it failed to handle Val.
00153 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
00154                         MVT LocVT, CCValAssign::LocInfo LocInfo,
00155                         ISD::ArgFlagsTy ArgFlags, CCState &State);
00156 
00157 /// CCCustomFn - This function assigns a location for Val, possibly updating
00158 /// all args to reflect changes and indicates if it handled it. It must set
00159 /// isCustom if it handles the arg and returns true.
00160 typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
00161                         MVT &LocVT, CCValAssign::LocInfo &LocInfo,
00162                         ISD::ArgFlagsTy &ArgFlags, CCState &State);
00163 
00164 /// ParmContext - This enum tracks whether calling convention lowering is in
00165 /// the context of prologue or call generation. Not all backends make use of
00166 /// this information.
00167 typedef enum { Unknown, Prologue, Call } ParmContext;
00168 
00169 /// CCState - This class holds information needed while lowering arguments and
00170 /// return values.  It captures which registers are already assigned and which
00171 /// stack slots are used.  It provides accessors to allocate these values.
00172 class CCState {
00173 private:
00174   CallingConv::ID CallingConv;
00175   bool IsVarArg;
00176   MachineFunction &MF;
00177   const TargetRegisterInfo &TRI;
00178   SmallVectorImpl<CCValAssign> &Locs;
00179   LLVMContext &Context;
00180 
00181   unsigned StackOffset;
00182   SmallVector<uint32_t, 16> UsedRegs;
00183   SmallVector<CCValAssign, 4> PendingLocs;
00184 
00185   // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
00186   //
00187   // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
00188   // tracking.
00189   // Or, in another words it tracks byval parameters that are stored in
00190   // general purpose registers.
00191   //
00192   // For 4 byte stack alignment,
00193   // instance index means byval parameter number in formal
00194   // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
00195   // then, for function "foo":
00196   //
00197   // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
00198   //
00199   // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
00200   // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
00201   //
00202   // In case of 8 bytes stack alignment,
00203   // ByValRegs may also contain information about wasted registers.
00204   // In function shown above, r3 would be wasted according to AAPCS rules.
00205   // And in that case ByValRegs[1].Waste would be "true".
00206   // ByValRegs vector size still would be 2,
00207   // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
00208   //
00209   // Supposed use-case for this collection:
00210   // 1. Initially ByValRegs is empty, InRegsParamsProceed is 0.
00211   // 2. HandleByVal fillups ByValRegs.
00212   // 3. Argument analysis (LowerFormatArguments, for example). After
00213   // some byval argument was analyzed, InRegsParamsProceed is increased.
00214   struct ByValInfo {
00215     ByValInfo(unsigned B, unsigned E, bool IsWaste = false) :
00216       Begin(B), End(E), Waste(IsWaste) {}
00217     // First register allocated for current parameter.
00218     unsigned Begin;
00219 
00220     // First after last register allocated for current parameter.
00221     unsigned End;
00222 
00223     // Means that current range of registers doesn't belong to any
00224     // parameters. It was wasted due to stack alignment rules.
00225     // For more information see:
00226     // AAPCS, 5.5 Parameter Passing, Stage C, C.3.
00227     bool Waste;
00228   };
00229   SmallVector<ByValInfo, 4 > ByValRegs;
00230 
00231   // InRegsParamsProceed - shows how many instances of ByValRegs was proceed
00232   // during argument analysis.
00233   unsigned InRegsParamsProceed;
00234 
00235 protected:
00236   ParmContext CallOrPrologue;
00237 
00238 public:
00239   CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
00240           SmallVectorImpl<CCValAssign> &locs, LLVMContext &C);
00241 
00242   void addLoc(const CCValAssign &V) {
00243     Locs.push_back(V);
00244   }
00245 
00246   LLVMContext &getContext() const { return Context; }
00247   MachineFunction &getMachineFunction() const { return MF; }
00248   CallingConv::ID getCallingConv() const { return CallingConv; }
00249   bool isVarArg() const { return IsVarArg; }
00250 
00251   unsigned getNextStackOffset() const { return StackOffset; }
00252 
00253   /// isAllocated - Return true if the specified register (or an alias) is
00254   /// allocated.
00255   bool isAllocated(unsigned Reg) const {
00256     return UsedRegs[Reg/32] & (1 << (Reg&31));
00257   }
00258 
00259   /// AnalyzeFormalArguments - Analyze an array of argument values,
00260   /// incorporating info about the formals into this state.
00261   void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
00262                               CCAssignFn Fn);
00263 
00264   /// AnalyzeReturn - Analyze the returned values of a return,
00265   /// incorporating info about the result values into this state.
00266   void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
00267                      CCAssignFn Fn);
00268 
00269   /// CheckReturn - Analyze the return values of a function, returning
00270   /// true if the return can be performed without sret-demotion, and
00271   /// false otherwise.
00272   bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
00273                    CCAssignFn Fn);
00274 
00275   /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
00276   /// incorporating info about the passed values into this state.
00277   void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
00278                            CCAssignFn Fn);
00279 
00280   /// AnalyzeCallOperands - Same as above except it takes vectors of types
00281   /// and argument flags.
00282   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
00283                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00284                            CCAssignFn Fn);
00285 
00286   /// AnalyzeCallResult - Analyze the return values of a call,
00287   /// incorporating info about the passed values into this state.
00288   void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
00289                          CCAssignFn Fn);
00290 
00291   /// AnalyzeCallResult - Same as above except it's specialized for calls which
00292   /// produce a single value.
00293   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
00294 
00295   /// getFirstUnallocated - Return the first unallocated register in the set, or
00296   /// NumRegs if they are all allocated.
00297   unsigned getFirstUnallocated(const MCPhysReg *Regs, unsigned NumRegs) const {
00298     for (unsigned i = 0; i != NumRegs; ++i)
00299       if (!isAllocated(Regs[i]))
00300         return i;
00301     return NumRegs;
00302   }
00303 
00304   /// AllocateReg - Attempt to allocate one register.  If it is not available,
00305   /// return zero.  Otherwise, return the register, marking it and any aliases
00306   /// as allocated.
00307   unsigned AllocateReg(unsigned Reg) {
00308     if (isAllocated(Reg)) return 0;
00309     MarkAllocated(Reg);
00310     return Reg;
00311   }
00312 
00313   /// Version of AllocateReg with extra register to be shadowed.
00314   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
00315     if (isAllocated(Reg)) return 0;
00316     MarkAllocated(Reg);
00317     MarkAllocated(ShadowReg);
00318     return Reg;
00319   }
00320 
00321   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
00322   /// are available, return zero.  Otherwise, return the first one available,
00323   /// marking it and any aliases as allocated.
00324   unsigned AllocateReg(const MCPhysReg *Regs, unsigned NumRegs) {
00325     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00326     if (FirstUnalloc == NumRegs)
00327       return 0;    // Didn't find the reg.
00328 
00329     // Mark the register and any aliases as allocated.
00330     unsigned Reg = Regs[FirstUnalloc];
00331     MarkAllocated(Reg);
00332     return Reg;
00333   }
00334 
00335   /// AllocateRegBlock - Attempt to allocate a block of RegsRequired consecutive
00336   /// registers. If this is not possible, return zero. Otherwise, return the first
00337   /// register of the block that were allocated, marking the entire block as allocated.
00338   unsigned AllocateRegBlock(const uint16_t *Regs, unsigned NumRegs, unsigned RegsRequired) {
00339     for (unsigned StartIdx = 0; StartIdx <= NumRegs - RegsRequired; ++StartIdx) {
00340       bool BlockAvailable = true;
00341       // Check for already-allocated regs in this block
00342       for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
00343         if (isAllocated(Regs[StartIdx + BlockIdx])) {
00344           BlockAvailable = false;
00345           break;
00346         }
00347       }
00348       if (BlockAvailable) {
00349         // Mark the entire block as allocated
00350         for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
00351           MarkAllocated(Regs[StartIdx + BlockIdx]);
00352         }
00353         return Regs[StartIdx];
00354       }
00355     }
00356     // No block was available
00357     return 0;
00358   }
00359 
00360   /// Version of AllocateReg with list of registers to be shadowed.
00361   unsigned AllocateReg(const MCPhysReg *Regs, const MCPhysReg *ShadowRegs,
00362                        unsigned NumRegs) {
00363     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00364     if (FirstUnalloc == NumRegs)
00365       return 0;    // Didn't find the reg.
00366 
00367     // Mark the register and any aliases as allocated.
00368     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
00369     MarkAllocated(Reg);
00370     MarkAllocated(ShadowReg);
00371     return Reg;
00372   }
00373 
00374   /// AllocateStack - Allocate a chunk of stack space with the specified size
00375   /// and alignment.
00376   unsigned AllocateStack(unsigned Size, unsigned Align) {
00377     assert(Align && ((Align - 1) & Align) == 0); // Align is power of 2.
00378     StackOffset = ((StackOffset + Align - 1) & ~(Align - 1));
00379     unsigned Result = StackOffset;
00380     StackOffset += Size;
00381     MF.getFrameInfo()->ensureMaxAlignment(Align);
00382     return Result;
00383   }
00384 
00385   /// Version of AllocateStack with extra register to be shadowed.
00386   unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
00387     MarkAllocated(ShadowReg);
00388     return AllocateStack(Size, Align);
00389   }
00390 
00391   /// Version of AllocateStack with list of extra registers to be shadowed.
00392   /// Note that, unlike AllocateReg, this shadows ALL of the shadow registers.
00393   unsigned AllocateStack(unsigned Size, unsigned Align,
00394                          const MCPhysReg *ShadowRegs, unsigned NumShadowRegs) {
00395     for (unsigned i = 0; i < NumShadowRegs; ++i)
00396       MarkAllocated(ShadowRegs[i]);
00397     return AllocateStack(Size, Align);
00398   }
00399 
00400   // HandleByVal - Allocate a stack slot large enough to pass an argument by
00401   // value. The size and alignment information of the argument is encoded in its
00402   // parameter attribute.
00403   void HandleByVal(unsigned ValNo, MVT ValVT,
00404                    MVT LocVT, CCValAssign::LocInfo LocInfo,
00405                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
00406 
00407   // Returns count of byval arguments that are to be stored (even partly)
00408   // in registers.
00409   unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
00410 
00411   // Returns count of byval in-regs arguments proceed.
00412   unsigned getInRegsParamsProceed() const { return InRegsParamsProceed; }
00413 
00414   // Get information about N-th byval parameter that is stored in registers.
00415   // Here "ByValParamIndex" is N.
00416   void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
00417                           unsigned& BeginReg, unsigned& EndReg) const {
00418     assert(InRegsParamRecordIndex < ByValRegs.size() &&
00419            "Wrong ByVal parameter index");
00420 
00421     const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
00422     BeginReg = info.Begin;
00423     EndReg = info.End;
00424   }
00425 
00426   // Add information about parameter that is kept in registers.
00427   void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
00428     ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
00429   }
00430 
00431   // Goes either to next byval parameter (excluding "waste" record), or
00432   // to the end of collection.
00433   // Returns false, if end is reached.
00434   bool nextInRegsParam() {
00435     unsigned e = ByValRegs.size();
00436     if (InRegsParamsProceed < e)
00437       ++InRegsParamsProceed;
00438     return InRegsParamsProceed < e;
00439   }
00440 
00441   // Clear byval registers tracking info.
00442   void clearByValRegsInfo() {
00443     InRegsParamsProceed = 0;
00444     ByValRegs.clear();
00445   }
00446 
00447   // Rewind byval registers tracking info.
00448   void rewindByValRegsInfo() {
00449     InRegsParamsProceed = 0;
00450   }
00451 
00452   ParmContext getCallOrPrologue() const { return CallOrPrologue; }
00453 
00454   // Get list of pending assignments
00455   SmallVectorImpl<llvm::CCValAssign> &getPendingLocs() {
00456     return PendingLocs;
00457   }
00458 
00459 private:
00460   /// MarkAllocated - Mark a register and all of its aliases as allocated.
00461   void MarkAllocated(unsigned Reg);
00462 };
00463 
00464 
00465 
00466 } // end namespace llvm
00467 
00468 #endif