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