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/ValueTypes.h"
00020 #include "llvm/CodeGen/SelectionDAGNodes.h"
00021 
00022 namespace llvm {
00023   class TargetRegisterInfo;
00024   class TargetMachine;
00025   class CCState;
00026   class SDNode;
00027 
00028 /// CCValAssign - Represent assignment of one arg/retval to a location.
00029 class CCValAssign {
00030 public:
00031   enum LocInfo {
00032     Full,   // The value fills the full location.
00033     SExt,   // The value is sign extended in the location.
00034     ZExt,   // The value is zero extended in the location.
00035     AExt,   // The value is extended with undefined upper bits.
00036     BCvt,   // The value is bit-converted in the location.
00037     Indirect // The location contains pointer to the value.
00038     // TODO: a subset of the value is in the location.
00039   };
00040 private:
00041   /// ValNo - This is the value number begin assigned (e.g. an argument number).
00042   unsigned ValNo;
00043 
00044   /// Loc is either a stack offset or a register number.
00045   unsigned Loc;
00046 
00047   /// isMem - True if this is a memory loc, false if it is a register loc.
00048   bool isMem : 1;
00049 
00050   /// isCustom - True if this arg/retval requires special handling.
00051   bool isCustom : 1;
00052 
00053   /// Information about how the value is assigned.
00054   LocInfo HTP : 6;
00055 
00056   /// ValVT - The type of the value being assigned.
00057   EVT ValVT;
00058 
00059   /// LocVT - The type of the location being assigned to.
00060   EVT LocVT;
00061 public:
00062 
00063   static CCValAssign getReg(unsigned ValNo, EVT ValVT,
00064                             unsigned RegNo, EVT LocVT,
00065                             LocInfo HTP) {
00066     CCValAssign Ret;
00067     Ret.ValNo = ValNo;
00068     Ret.Loc = RegNo;
00069     Ret.isMem = false;
00070     Ret.isCustom = false;
00071     Ret.HTP = HTP;
00072     Ret.ValVT = ValVT;
00073     Ret.LocVT = LocVT;
00074     return Ret;
00075   }
00076 
00077   static CCValAssign getCustomReg(unsigned ValNo, EVT ValVT,
00078                                   unsigned RegNo, EVT LocVT,
00079                                   LocInfo HTP) {
00080     CCValAssign Ret;
00081     Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
00082     Ret.isCustom = true;
00083     return Ret;
00084   }
00085 
00086   static CCValAssign getMem(unsigned ValNo, EVT ValVT,
00087                             unsigned Offset, EVT LocVT,
00088                             LocInfo HTP) {
00089     CCValAssign Ret;
00090     Ret.ValNo = ValNo;
00091     Ret.Loc = Offset;
00092     Ret.isMem = true;
00093     Ret.isCustom = false;
00094     Ret.HTP = HTP;
00095     Ret.ValVT = ValVT;
00096     Ret.LocVT = LocVT;
00097     return Ret;
00098   }
00099 
00100   static CCValAssign getCustomMem(unsigned ValNo, EVT ValVT,
00101                                   unsigned Offset, EVT LocVT,
00102                                   LocInfo HTP) {
00103     CCValAssign Ret;
00104     Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
00105     Ret.isCustom = true;
00106     return Ret;
00107   }
00108 
00109   unsigned getValNo() const { return ValNo; }
00110   EVT getValVT() const { return ValVT; }
00111 
00112   bool isRegLoc() const { return !isMem; }
00113   bool isMemLoc() const { return isMem; }
00114 
00115   bool needsCustom() const { return isCustom; }
00116 
00117   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
00118   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
00119   EVT getLocVT() const { return LocVT; }
00120 
00121   LocInfo getLocInfo() const { return HTP; }
00122   bool isExtInLoc() const {
00123     return (HTP == AExt || HTP == SExt || HTP == ZExt);
00124   }
00125 
00126 };
00127 
00128 /// CCAssignFn - This function assigns a location for Val, updating State to
00129 /// reflect the change.
00130 typedef bool CCAssignFn(unsigned ValNo, EVT ValVT,
00131                         EVT LocVT, CCValAssign::LocInfo LocInfo,
00132                         ISD::ArgFlagsTy ArgFlags, CCState &State);
00133 
00134 /// CCCustomFn - This function assigns a location for Val, possibly updating
00135 /// all args to reflect changes and indicates if it handled it. It must set
00136 /// isCustom if it handles the arg and returns true.
00137 typedef bool CCCustomFn(unsigned &ValNo, EVT &ValVT,
00138                         EVT &LocVT, CCValAssign::LocInfo &LocInfo,
00139                         ISD::ArgFlagsTy &ArgFlags, CCState &State);
00140 
00141 /// CCState - This class holds information needed while lowering arguments and
00142 /// return values.  It captures which registers are already assigned and which
00143 /// stack slots are used.  It provides accessors to allocate these values.
00144 class CCState {
00145   unsigned CallingConv;
00146   bool IsVarArg;
00147   const TargetMachine &TM;
00148   const TargetRegisterInfo &TRI;
00149   SmallVector<CCValAssign, 16> &Locs;
00150   LLVMContext &Context;
00151 
00152   unsigned StackOffset;
00153   SmallVector<uint32_t, 16> UsedRegs;
00154 public:
00155   CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
00156           SmallVector<CCValAssign, 16> &locs, LLVMContext &C);
00157 
00158   void addLoc(const CCValAssign &V) {
00159     Locs.push_back(V);
00160   }
00161 
00162   LLVMContext &getContext() const { return Context; }
00163   const TargetMachine &getTarget() const { return TM; }
00164   unsigned getCallingConv() const { return CallingConv; }
00165   bool isVarArg() const { return IsVarArg; }
00166 
00167   unsigned getNextStackOffset() const { return StackOffset; }
00168 
00169   /// isAllocated - Return true if the specified register (or an alias) is
00170   /// allocated.
00171   bool isAllocated(unsigned Reg) const {
00172     return UsedRegs[Reg/32] & (1 << (Reg&31));
00173   }
00174 
00175   /// AnalyzeFormalArguments - Analyze an array of argument values,
00176   /// incorporating info about the formals into this state.
00177   void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
00178                               CCAssignFn Fn);
00179 
00180   /// AnalyzeReturn - Analyze the returned values of a return,
00181   /// incorporating info about the result values into this state.
00182   void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
00183                      CCAssignFn Fn);
00184 
00185   /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
00186   /// incorporating info about the passed values into this state.
00187   void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
00188                            CCAssignFn Fn);
00189 
00190   /// AnalyzeCallOperands - Same as above except it takes vectors of types
00191   /// and argument flags.
00192   void AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
00193                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00194                            CCAssignFn Fn);
00195 
00196   /// AnalyzeCallResult - Analyze the return values of a call,
00197   /// incorporating info about the passed values into this state.
00198   void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
00199                          CCAssignFn Fn);
00200 
00201   /// AnalyzeCallResult - Same as above except it's specialized for calls which
00202   /// produce a single value.
00203   void AnalyzeCallResult(EVT VT, CCAssignFn Fn);
00204 
00205   /// getFirstUnallocated - Return the first unallocated register in the set, or
00206   /// NumRegs if they are all allocated.
00207   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
00208     for (unsigned i = 0; i != NumRegs; ++i)
00209       if (!isAllocated(Regs[i]))
00210         return i;
00211     return NumRegs;
00212   }
00213 
00214   /// AllocateReg - Attempt to allocate one register.  If it is not available,
00215   /// return zero.  Otherwise, return the register, marking it and any aliases
00216   /// as allocated.
00217   unsigned AllocateReg(unsigned Reg) {
00218     if (isAllocated(Reg)) return 0;
00219     MarkAllocated(Reg);
00220     return Reg;
00221   }
00222 
00223   /// Version of AllocateReg with extra register to be shadowed.
00224   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
00225     if (isAllocated(Reg)) return 0;
00226     MarkAllocated(Reg);
00227     MarkAllocated(ShadowReg);
00228     return Reg;
00229   }
00230 
00231   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
00232   /// are available, return zero.  Otherwise, return the first one available,
00233   /// marking it and any aliases as allocated.
00234   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
00235     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00236     if (FirstUnalloc == NumRegs)
00237       return 0;    // Didn't find the reg.
00238 
00239     // Mark the register and any aliases as allocated.
00240     unsigned Reg = Regs[FirstUnalloc];
00241     MarkAllocated(Reg);
00242     return Reg;
00243   }
00244 
00245   /// Version of AllocateReg with list of registers to be shadowed.
00246   unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
00247                        unsigned NumRegs) {
00248     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00249     if (FirstUnalloc == NumRegs)
00250       return 0;    // Didn't find the reg.
00251 
00252     // Mark the register and any aliases as allocated.
00253     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
00254     MarkAllocated(Reg);
00255     MarkAllocated(ShadowReg);
00256     return Reg;
00257   }
00258 
00259   /// AllocateStack - Allocate a chunk of stack space with the specified size
00260   /// and alignment.
00261   unsigned AllocateStack(unsigned Size, unsigned Align) {
00262     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
00263     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
00264     unsigned Result = StackOffset;
00265     StackOffset += Size;
00266     return Result;
00267   }
00268 
00269   // HandleByVal - Allocate a stack slot large enough to pass an argument by
00270   // value. The size and alignment information of the argument is encoded in its
00271   // parameter attribute.
00272   void HandleByVal(unsigned ValNo, EVT ValVT,
00273                    EVT LocVT, CCValAssign::LocInfo LocInfo,
00274                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
00275 
00276 private:
00277   /// MarkAllocated - Mark a register and all of its aliases as allocated.
00278   void MarkAllocated(unsigned Reg);
00279 };
00280 
00281 
00282 
00283 } // end namespace llvm
00284 
00285 #endif