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     // TODO: a subset of the value is in the location.
00037   };
00038 private:
00039   /// ValNo - This is the value number begin assigned (e.g. an argument number).
00040   unsigned ValNo;
00041   
00042   /// Loc is either a stack offset or a register number.
00043   unsigned Loc;
00044   
00045   /// isMem - True if this is a memory loc, false if it is a register loc.
00046   bool isMem : 1;
00047   
00048   /// Information about how the value is assigned.
00049   LocInfo HTP : 7;
00050   
00051   /// ValVT - The type of the value being assigned.
00052   MVT ValVT;
00053 
00054   /// LocVT - The type of the location being assigned to.
00055   MVT LocVT;
00056 public:
00057     
00058   static CCValAssign getReg(unsigned ValNo, MVT ValVT,
00059                             unsigned RegNo, MVT LocVT,
00060                             LocInfo HTP) {
00061     CCValAssign Ret;
00062     Ret.ValNo = ValNo;
00063     Ret.Loc = RegNo;
00064     Ret.isMem = false;
00065     Ret.HTP = HTP;
00066     Ret.ValVT = ValVT;
00067     Ret.LocVT = LocVT;
00068     return Ret;
00069   }
00070   static CCValAssign getMem(unsigned ValNo, MVT ValVT,
00071                             unsigned Offset, MVT LocVT,
00072                             LocInfo HTP) {
00073     CCValAssign Ret;
00074     Ret.ValNo = ValNo;
00075     Ret.Loc = Offset;
00076     Ret.isMem = true;
00077     Ret.HTP = HTP;
00078     Ret.ValVT = ValVT;
00079     Ret.LocVT = LocVT;
00080     return Ret;
00081   }
00082   
00083   unsigned getValNo() const { return ValNo; }
00084   MVT getValVT() const { return ValVT; }
00085 
00086   bool isRegLoc() const { return !isMem; }
00087   bool isMemLoc() const { return isMem; }
00088   
00089   unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
00090   unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
00091   MVT getLocVT() const { return LocVT; }
00092   
00093   LocInfo getLocInfo() const { return HTP; }
00094 };
00095 
00096 
00097 /// CCAssignFn - This function assigns a location for Val, updating State to
00098 /// reflect the change.
00099 typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
00100                         MVT LocVT, CCValAssign::LocInfo LocInfo,
00101                         ISD::ArgFlagsTy ArgFlags, CCState &State);
00102 
00103   
00104 /// CCState - This class holds information needed while lowering arguments and
00105 /// return values.  It captures which registers are already assigned and which
00106 /// stack slots are used.  It provides accessors to allocate these values.
00107 class CCState {
00108   unsigned CallingConv;
00109   bool IsVarArg;
00110   const TargetMachine &TM;
00111   const TargetRegisterInfo &TRI;
00112   SmallVector<CCValAssign, 16> &Locs;
00113   
00114   unsigned StackOffset;
00115   SmallVector<uint32_t, 16> UsedRegs;
00116 public:
00117   CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
00118           SmallVector<CCValAssign, 16> &locs);
00119   
00120   void addLoc(const CCValAssign &V) {
00121     Locs.push_back(V);
00122   }
00123   
00124   const TargetMachine &getTarget() const { return TM; }
00125   unsigned getCallingConv() const { return CallingConv; }
00126   bool isVarArg() const { return IsVarArg; }
00127   
00128   unsigned getNextStackOffset() const { return StackOffset; }
00129 
00130   /// isAllocated - Return true if the specified register (or an alias) is
00131   /// allocated.
00132   bool isAllocated(unsigned Reg) const {
00133     return UsedRegs[Reg/32] & (1 << (Reg&31));
00134   }
00135   
00136   /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
00137   /// incorporating info about the formals into this state.
00138   void AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn);
00139   
00140   /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
00141   /// incorporating info about the result values into this state.
00142   void AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn);
00143   
00144   /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
00145   /// about the passed values into this state.
00146   void AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn);
00147 
00148   /// AnalyzeCallOperands - Same as above except it takes vectors of types
00149   /// and argument flags.
00150   void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
00151                            SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00152                            CCAssignFn Fn);
00153 
00154   /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
00155   /// incorporating info about the passed values into this state.
00156   void AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn);
00157   
00158   /// AnalyzeCallResult - Same as above except it's specialized for calls which
00159   /// produce a single value.
00160   void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
00161 
00162   /// getFirstUnallocated - Return the first unallocated register in the set, or
00163   /// NumRegs if they are all allocated.
00164   unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
00165     for (unsigned i = 0; i != NumRegs; ++i)
00166       if (!isAllocated(Regs[i]))
00167         return i;
00168     return NumRegs;
00169   }
00170   
00171   /// AllocateReg - Attempt to allocate one register.  If it is not available,
00172   /// return zero.  Otherwise, return the register, marking it and any aliases
00173   /// as allocated.
00174   unsigned AllocateReg(unsigned Reg) {
00175     if (isAllocated(Reg)) return 0;
00176     MarkAllocated(Reg);
00177     return Reg;
00178   }
00179 
00180   /// Version of AllocateReg with extra register to be shadowed.
00181   unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
00182     if (isAllocated(Reg)) return 0;
00183     MarkAllocated(Reg);
00184     MarkAllocated(ShadowReg);
00185     return Reg;
00186   }
00187 
00188   /// AllocateReg - Attempt to allocate one of the specified registers.  If none
00189   /// are available, return zero.  Otherwise, return the first one available,
00190   /// marking it and any aliases as allocated.
00191   unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
00192     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00193     if (FirstUnalloc == NumRegs)
00194       return 0;    // Didn't find the reg.
00195 
00196     // Mark the register and any aliases as allocated.
00197     unsigned Reg = Regs[FirstUnalloc];
00198     MarkAllocated(Reg);
00199     return Reg;
00200   }
00201 
00202   /// Version of AllocateReg with list of registers to be shadowed.
00203   unsigned AllocateReg(const unsigned *Regs, const unsigned *ShadowRegs,
00204                        unsigned NumRegs) {
00205     unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
00206     if (FirstUnalloc == NumRegs)
00207       return 0;    // Didn't find the reg.
00208 
00209     // Mark the register and any aliases as allocated.
00210     unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
00211     MarkAllocated(Reg);
00212     MarkAllocated(ShadowReg);
00213     return Reg;
00214   }
00215 
00216   /// AllocateStack - Allocate a chunk of stack space with the specified size
00217   /// and alignment.
00218   unsigned AllocateStack(unsigned Size, unsigned Align) {
00219     assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
00220     StackOffset = ((StackOffset + Align-1) & ~(Align-1));
00221     unsigned Result = StackOffset;
00222     StackOffset += Size;
00223     return Result;
00224   }
00225 
00226   // HandleByVal - Allocate a stack slot large enough to pass an argument by
00227   // value. The size and alignment information of the argument is encoded in its
00228   // parameter attribute.
00229   void HandleByVal(unsigned ValNo, MVT ValVT,
00230                    MVT LocVT, CCValAssign::LocInfo LocInfo,
00231                    int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
00232 
00233 private:
00234   /// MarkAllocated - Mark a register and all of its aliases as allocated.
00235   void MarkAllocated(unsigned Reg);
00236 };
00237 
00238 
00239 
00240 } // end namespace llvm
00241 
00242 #endif