1193323Sed//===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file declares the CCState and CCValAssign classes, used for lowering 11193323Sed// and implementing calling conventions. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H 16193323Sed#define LLVM_CODEGEN_CALLINGCONVLOWER_H 17193323Sed 18193323Sed#include "llvm/ADT/SmallVector.h" 19249423Sdim#include "llvm/CodeGen/MachineFrameInfo.h" 20223017Sdim#include "llvm/CodeGen/MachineFunction.h" 21193323Sed#include "llvm/CodeGen/ValueTypes.h" 22249423Sdim#include "llvm/IR/CallingConv.h" 23210299Sed#include "llvm/Target/TargetCallingConv.h" 24193323Sed 25193323Sednamespace llvm { 26193323Sed class TargetRegisterInfo; 27193323Sed class TargetMachine; 28193323Sed class CCState; 29193323Sed 30193323Sed/// CCValAssign - Represent assignment of one arg/retval to a location. 31193323Sedclass CCValAssign { 32193323Sedpublic: 33193323Sed enum LocInfo { 34193323Sed Full, // The value fills the full location. 35193323Sed SExt, // The value is sign extended in the location. 36193323Sed ZExt, // The value is zero extended in the location. 37193323Sed AExt, // The value is extended with undefined upper bits. 38198090Srdivacky BCvt, // The value is bit-converted in the location. 39210299Sed VExt, // The value is vector-widened in the location. 40210299Sed // FIXME: Not implemented yet. Code that uses AExt to mean 41210299Sed // vector-widen should be fixed to use VExt instead. 42198090Srdivacky Indirect // The location contains pointer to the value. 43193323Sed // TODO: a subset of the value is in the location. 44193323Sed }; 45193323Sedprivate: 46193323Sed /// ValNo - This is the value number begin assigned (e.g. an argument number). 47193323Sed unsigned ValNo; 48198090Srdivacky 49193323Sed /// Loc is either a stack offset or a register number. 50193323Sed unsigned Loc; 51198090Srdivacky 52193323Sed /// isMem - True if this is a memory loc, false if it is a register loc. 53249423Sdim unsigned isMem : 1; 54198090Srdivacky 55193323Sed /// isCustom - True if this arg/retval requires special handling. 56249423Sdim unsigned isCustom : 1; 57193323Sed 58193323Sed /// Information about how the value is assigned. 59193323Sed LocInfo HTP : 6; 60198090Srdivacky 61193323Sed /// ValVT - The type of the value being assigned. 62218893Sdim MVT ValVT; 63193323Sed 64193323Sed /// LocVT - The type of the location being assigned to. 65218893Sdim MVT LocVT; 66193323Sedpublic: 67198090Srdivacky 68218893Sdim static CCValAssign getReg(unsigned ValNo, MVT ValVT, 69218893Sdim unsigned RegNo, MVT LocVT, 70193323Sed LocInfo HTP) { 71193323Sed CCValAssign Ret; 72193323Sed Ret.ValNo = ValNo; 73193323Sed Ret.Loc = RegNo; 74193323Sed Ret.isMem = false; 75193323Sed Ret.isCustom = false; 76193323Sed Ret.HTP = HTP; 77193323Sed Ret.ValVT = ValVT; 78193323Sed Ret.LocVT = LocVT; 79193323Sed return Ret; 80193323Sed } 81193323Sed 82218893Sdim static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT, 83218893Sdim unsigned RegNo, MVT LocVT, 84193323Sed LocInfo HTP) { 85193323Sed CCValAssign Ret; 86193323Sed Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP); 87193323Sed Ret.isCustom = true; 88193323Sed return Ret; 89193323Sed } 90193323Sed 91218893Sdim static CCValAssign getMem(unsigned ValNo, MVT ValVT, 92218893Sdim unsigned Offset, MVT LocVT, 93193323Sed LocInfo HTP) { 94193323Sed CCValAssign Ret; 95193323Sed Ret.ValNo = ValNo; 96193323Sed Ret.Loc = Offset; 97193323Sed Ret.isMem = true; 98193323Sed Ret.isCustom = false; 99193323Sed Ret.HTP = HTP; 100193323Sed Ret.ValVT = ValVT; 101193323Sed Ret.LocVT = LocVT; 102193323Sed return Ret; 103193323Sed } 104198090Srdivacky 105218893Sdim static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT, 106218893Sdim unsigned Offset, MVT LocVT, 107193323Sed LocInfo HTP) { 108193323Sed CCValAssign Ret; 109193323Sed Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP); 110193323Sed Ret.isCustom = true; 111193323Sed return Ret; 112193323Sed } 113193323Sed 114193323Sed unsigned getValNo() const { return ValNo; } 115218893Sdim MVT getValVT() const { return ValVT; } 116193323Sed 117193323Sed bool isRegLoc() const { return !isMem; } 118193323Sed bool isMemLoc() const { return isMem; } 119198090Srdivacky 120193323Sed bool needsCustom() const { return isCustom; } 121193323Sed 122193323Sed unsigned getLocReg() const { assert(isRegLoc()); return Loc; } 123193323Sed unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; } 124218893Sdim MVT getLocVT() const { return LocVT; } 125198090Srdivacky 126193323Sed LocInfo getLocInfo() const { return HTP; } 127198090Srdivacky bool isExtInLoc() const { 128198090Srdivacky return (HTP == AExt || HTP == SExt || HTP == ZExt); 129198090Srdivacky } 130198090Srdivacky 131193323Sed}; 132193323Sed 133193323Sed/// CCAssignFn - This function assigns a location for Val, updating State to 134218893Sdim/// reflect the change. It returns 'true' if it failed to handle Val. 135218893Sdimtypedef bool CCAssignFn(unsigned ValNo, MVT ValVT, 136218893Sdim MVT LocVT, CCValAssign::LocInfo LocInfo, 137193323Sed ISD::ArgFlagsTy ArgFlags, CCState &State); 138193323Sed 139193323Sed/// CCCustomFn - This function assigns a location for Val, possibly updating 140193323Sed/// all args to reflect changes and indicates if it handled it. It must set 141193323Sed/// isCustom if it handles the arg and returns true. 142218893Sdimtypedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT, 143218893Sdim MVT &LocVT, CCValAssign::LocInfo &LocInfo, 144193323Sed ISD::ArgFlagsTy &ArgFlags, CCState &State); 145193323Sed 146223017Sdim/// ParmContext - This enum tracks whether calling convention lowering is in 147223017Sdim/// the context of prologue or call generation. Not all backends make use of 148223017Sdim/// this information. 149223017Sdimtypedef enum { Unknown, Prologue, Call } ParmContext; 150221345Sdim 151193323Sed/// CCState - This class holds information needed while lowering arguments and 152193323Sed/// return values. It captures which registers are already assigned and which 153193323Sed/// stack slots are used. It provides accessors to allocate these values. 154193323Sedclass CCState { 155223017Sdimprivate: 156198090Srdivacky CallingConv::ID CallingConv; 157193323Sed bool IsVarArg; 158223017Sdim MachineFunction &MF; 159193323Sed const TargetMachine &TM; 160193323Sed const TargetRegisterInfo &TRI; 161193323Sed SmallVector<CCValAssign, 16> &Locs; 162198090Srdivacky LLVMContext &Context; 163198090Srdivacky 164193323Sed unsigned StackOffset; 165193323Sed SmallVector<uint32_t, 16> UsedRegs; 166223017Sdim 167251662Sdim // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs: 168251662Sdim // 169251662Sdim // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers 170251662Sdim // tracking. 171251662Sdim // Or, in another words it tracks byval parameters that are stored in 172251662Sdim // general purpose registers. 173251662Sdim // 174251662Sdim // For 4 byte stack alignment, 175251662Sdim // instance index means byval parameter number in formal 176251662Sdim // arguments set. Assume, we have some "struct_type" with size = 4 bytes, 177251662Sdim // then, for function "foo": 178251662Sdim // 179251662Sdim // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t) 180251662Sdim // 181251662Sdim // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2) 182251662Sdim // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4). 183251662Sdim // 184251662Sdim // In case of 8 bytes stack alignment, 185251662Sdim // ByValRegs may also contain information about wasted registers. 186251662Sdim // In function shown above, r3 would be wasted according to AAPCS rules. 187251662Sdim // And in that case ByValRegs[1].Waste would be "true". 188251662Sdim // ByValRegs vector size still would be 2, 189251662Sdim // while "%t" goes to the stack: it wouldn't be described in ByValRegs. 190251662Sdim // 191251662Sdim // Supposed use-case for this collection: 192251662Sdim // 1. Initially ByValRegs is empty, InRegsParamsProceed is 0. 193251662Sdim // 2. HandleByVal fillups ByValRegs. 194251662Sdim // 3. Argument analysis (LowerFormatArguments, for example). After 195251662Sdim // some byval argument was analyzed, InRegsParamsProceed is increased. 196251662Sdim struct ByValInfo { 197251662Sdim ByValInfo(unsigned B, unsigned E, bool IsWaste = false) : 198251662Sdim Begin(B), End(E), Waste(IsWaste) {} 199251662Sdim // First register allocated for current parameter. 200251662Sdim unsigned Begin; 201251662Sdim 202251662Sdim // First after last register allocated for current parameter. 203251662Sdim unsigned End; 204251662Sdim 205251662Sdim // Means that current range of registers doesn't belong to any 206251662Sdim // parameters. It was wasted due to stack alignment rules. 207251662Sdim // For more information see: 208251662Sdim // AAPCS, 5.5 Parameter Passing, Stage C, C.3. 209251662Sdim bool Waste; 210251662Sdim }; 211251662Sdim SmallVector<ByValInfo, 4 > ByValRegs; 212251662Sdim 213251662Sdim // InRegsParamsProceed - shows how many instances of ByValRegs was proceed 214251662Sdim // during argument analysis. 215251662Sdim unsigned InRegsParamsProceed; 216251662Sdim 217223017Sdimprotected: 218221345Sdim ParmContext CallOrPrologue; 219223017Sdim 220193323Sedpublic: 221223017Sdim CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 222223017Sdim const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs, 223223017Sdim LLVMContext &C); 224198090Srdivacky 225193323Sed void addLoc(const CCValAssign &V) { 226193323Sed Locs.push_back(V); 227193323Sed } 228198090Srdivacky 229198090Srdivacky LLVMContext &getContext() const { return Context; } 230193323Sed const TargetMachine &getTarget() const { return TM; } 231223017Sdim MachineFunction &getMachineFunction() const { return MF; } 232198090Srdivacky CallingConv::ID getCallingConv() const { return CallingConv; } 233193323Sed bool isVarArg() const { return IsVarArg; } 234198090Srdivacky 235193323Sed unsigned getNextStackOffset() const { return StackOffset; } 236193323Sed 237193323Sed /// isAllocated - Return true if the specified register (or an alias) is 238193323Sed /// allocated. 239193323Sed bool isAllocated(unsigned Reg) const { 240193323Sed return UsedRegs[Reg/32] & (1 << (Reg&31)); 241193323Sed } 242198090Srdivacky 243198090Srdivacky /// AnalyzeFormalArguments - Analyze an array of argument values, 244193323Sed /// incorporating info about the formals into this state. 245198090Srdivacky void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins, 246198090Srdivacky CCAssignFn Fn); 247198090Srdivacky 248198090Srdivacky /// AnalyzeReturn - Analyze the returned values of a return, 249193323Sed /// incorporating info about the result values into this state. 250198090Srdivacky void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, 251198090Srdivacky CCAssignFn Fn); 252193323Sed 253199481Srdivacky /// CheckReturn - Analyze the return values of a function, returning 254199481Srdivacky /// true if the return can be performed without sret-demotion, and 255199481Srdivacky /// false otherwise. 256210299Sed bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags, 257199481Srdivacky CCAssignFn Fn); 258199481Srdivacky 259198090Srdivacky /// AnalyzeCallOperands - Analyze the outgoing arguments to a call, 260198090Srdivacky /// incorporating info about the passed values into this state. 261198090Srdivacky void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs, 262198090Srdivacky CCAssignFn Fn); 263198090Srdivacky 264193323Sed /// AnalyzeCallOperands - Same as above except it takes vectors of types 265193323Sed /// and argument flags. 266218893Sdim void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs, 267193323Sed SmallVectorImpl<ISD::ArgFlagsTy> &Flags, 268193323Sed CCAssignFn Fn); 269193323Sed 270198090Srdivacky /// AnalyzeCallResult - Analyze the return values of a call, 271193323Sed /// incorporating info about the passed values into this state. 272198090Srdivacky void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, 273198090Srdivacky CCAssignFn Fn); 274198090Srdivacky 275193323Sed /// AnalyzeCallResult - Same as above except it's specialized for calls which 276193323Sed /// produce a single value. 277218893Sdim void AnalyzeCallResult(MVT VT, CCAssignFn Fn); 278193323Sed 279193323Sed /// getFirstUnallocated - Return the first unallocated register in the set, or 280193323Sed /// NumRegs if they are all allocated. 281234353Sdim unsigned getFirstUnallocated(const uint16_t *Regs, unsigned NumRegs) const { 282193323Sed for (unsigned i = 0; i != NumRegs; ++i) 283193323Sed if (!isAllocated(Regs[i])) 284193323Sed return i; 285193323Sed return NumRegs; 286193323Sed } 287198090Srdivacky 288193323Sed /// AllocateReg - Attempt to allocate one register. If it is not available, 289193323Sed /// return zero. Otherwise, return the register, marking it and any aliases 290193323Sed /// as allocated. 291193323Sed unsigned AllocateReg(unsigned Reg) { 292193323Sed if (isAllocated(Reg)) return 0; 293193323Sed MarkAllocated(Reg); 294193323Sed return Reg; 295193323Sed } 296193323Sed 297193323Sed /// Version of AllocateReg with extra register to be shadowed. 298193323Sed unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) { 299193323Sed if (isAllocated(Reg)) return 0; 300193323Sed MarkAllocated(Reg); 301193323Sed MarkAllocated(ShadowReg); 302193323Sed return Reg; 303193323Sed } 304193323Sed 305193323Sed /// AllocateReg - Attempt to allocate one of the specified registers. If none 306193323Sed /// are available, return zero. Otherwise, return the first one available, 307193323Sed /// marking it and any aliases as allocated. 308234353Sdim unsigned AllocateReg(const uint16_t *Regs, unsigned NumRegs) { 309193323Sed unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs); 310193323Sed if (FirstUnalloc == NumRegs) 311193323Sed return 0; // Didn't find the reg. 312193323Sed 313193323Sed // Mark the register and any aliases as allocated. 314193323Sed unsigned Reg = Regs[FirstUnalloc]; 315193323Sed MarkAllocated(Reg); 316193323Sed return Reg; 317193323Sed } 318193323Sed 319193323Sed /// Version of AllocateReg with list of registers to be shadowed. 320234353Sdim unsigned AllocateReg(const uint16_t *Regs, const uint16_t *ShadowRegs, 321193323Sed unsigned NumRegs) { 322193323Sed unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs); 323193323Sed if (FirstUnalloc == NumRegs) 324193323Sed return 0; // Didn't find the reg. 325193323Sed 326193323Sed // Mark the register and any aliases as allocated. 327193323Sed unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc]; 328193323Sed MarkAllocated(Reg); 329193323Sed MarkAllocated(ShadowReg); 330193323Sed return Reg; 331193323Sed } 332193323Sed 333193323Sed /// AllocateStack - Allocate a chunk of stack space with the specified size 334193323Sed /// and alignment. 335193323Sed unsigned AllocateStack(unsigned Size, unsigned Align) { 336193323Sed assert(Align && ((Align-1) & Align) == 0); // Align is power of 2. 337193323Sed StackOffset = ((StackOffset + Align-1) & ~(Align-1)); 338193323Sed unsigned Result = StackOffset; 339193323Sed StackOffset += Size; 340243830Sdim MF.getFrameInfo()->ensureMaxAlignment(Align); 341193323Sed return Result; 342193323Sed } 343193323Sed 344212904Sdim /// Version of AllocateStack with extra register to be shadowed. 345212904Sdim unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) { 346212904Sdim MarkAllocated(ShadowReg); 347212904Sdim return AllocateStack(Size, Align); 348212904Sdim } 349212904Sdim 350193323Sed // HandleByVal - Allocate a stack slot large enough to pass an argument by 351193323Sed // value. The size and alignment information of the argument is encoded in its 352193323Sed // parameter attribute. 353218893Sdim void HandleByVal(unsigned ValNo, MVT ValVT, 354218893Sdim MVT LocVT, CCValAssign::LocInfo LocInfo, 355193323Sed int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags); 356193323Sed 357251662Sdim // Returns count of byval arguments that are to be stored (even partly) 358251662Sdim // in registers. 359251662Sdim unsigned getInRegsParamsCount() const { return ByValRegs.size(); } 360221345Sdim 361251662Sdim // Returns count of byval in-regs arguments proceed. 362251662Sdim unsigned getInRegsParamsProceed() const { return InRegsParamsProceed; } 363251662Sdim 364251662Sdim // Get information about N-th byval parameter that is stored in registers. 365251662Sdim // Here "ByValParamIndex" is N. 366251662Sdim void getInRegsParamInfo(unsigned InRegsParamRecordIndex, 367251662Sdim unsigned& BeginReg, unsigned& EndReg) const { 368251662Sdim assert(InRegsParamRecordIndex < ByValRegs.size() && 369251662Sdim "Wrong ByVal parameter index"); 370251662Sdim 371251662Sdim const ByValInfo& info = ByValRegs[InRegsParamRecordIndex]; 372251662Sdim BeginReg = info.Begin; 373251662Sdim EndReg = info.End; 374251662Sdim } 375251662Sdim 376251662Sdim // Add information about parameter that is kept in registers. 377251662Sdim void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) { 378251662Sdim ByValRegs.push_back(ByValInfo(RegBegin, RegEnd)); 379251662Sdim } 380251662Sdim 381251662Sdim // Goes either to next byval parameter (excluding "waste" record), or 382251662Sdim // to the end of collection. 383251662Sdim // Returns false, if end is reached. 384251662Sdim bool nextInRegsParam() { 385251662Sdim unsigned e = ByValRegs.size(); 386251662Sdim if (InRegsParamsProceed < e) 387251662Sdim ++InRegsParamsProceed; 388251662Sdim return InRegsParamsProceed < e; 389251662Sdim } 390251662Sdim 391251662Sdim // Clear byval registers tracking info. 392251662Sdim void clearByValRegsInfo() { 393251662Sdim InRegsParamsProceed = 0; 394251662Sdim ByValRegs.clear(); 395251662Sdim } 396251662Sdim 397234353Sdim ParmContext getCallOrPrologue() const { return CallOrPrologue; } 398221345Sdim 399193323Sedprivate: 400193323Sed /// MarkAllocated - Mark a register and all of its aliases as allocated. 401193323Sed void MarkAllocated(unsigned Reg); 402193323Sed}; 403193323Sed 404193323Sed 405193323Sed 406193323Sed} // end namespace llvm 407193323Sed 408193323Sed#endif 409