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