1 //===- llvm/CodeGen/GlobalISel/CallLowering.h - Call lowering ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file describes how to lower LLVM calls to machine code calls. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H 15 #define LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/MachineOperand.h" 21 #include "llvm/CodeGen/TargetCallingConv.h" 22 #include "llvm/IR/Attributes.h" 23 #include "llvm/IR/CallingConv.h" 24 #include "llvm/IR/Type.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/MachineValueType.h" 27 #include <cstdint> 28 #include <functional> 29 30 namespace llvm { 31 32 class CallBase; 33 class DataLayout; 34 class Function; 35 class FunctionLoweringInfo; 36 class MachineIRBuilder; 37 struct MachinePointerInfo; 38 class MachineRegisterInfo; 39 class TargetLowering; 40 class Value; 41 42 class CallLowering { 43 const TargetLowering *TLI; 44 45 virtual void anchor(); 46 public: 47 struct BaseArgInfo { 48 Type *Ty; 49 SmallVector<ISD::ArgFlagsTy, 4> Flags; 50 bool IsFixed; 51 52 BaseArgInfo(Type *Ty, 53 ArrayRef<ISD::ArgFlagsTy> Flags = ArrayRef<ISD::ArgFlagsTy>(), 54 bool IsFixed = true) TyBaseArgInfo55 : Ty(Ty), Flags(Flags.begin(), Flags.end()), IsFixed(IsFixed) {} 56 BaseArgInfoBaseArgInfo57 BaseArgInfo() : Ty(nullptr), IsFixed(false) {} 58 }; 59 60 struct ArgInfo : public BaseArgInfo { 61 SmallVector<Register, 4> Regs; 62 // If the argument had to be split into multiple parts according to the 63 // target calling convention, then this contains the original vregs 64 // if the argument was an incoming arg. 65 SmallVector<Register, 2> OrigRegs; 66 67 ArgInfo(ArrayRef<Register> Regs, Type *Ty, 68 ArrayRef<ISD::ArgFlagsTy> Flags = ArrayRef<ISD::ArgFlagsTy>(), 69 bool IsFixed = true) BaseArgInfoArgInfo70 : BaseArgInfo(Ty, Flags, IsFixed), Regs(Regs.begin(), Regs.end()) { 71 if (!Regs.empty() && Flags.empty()) 72 this->Flags.push_back(ISD::ArgFlagsTy()); 73 // FIXME: We should have just one way of saying "no register". 74 assert(((Ty->isVoidTy() || Ty->isEmptyTy()) == 75 (Regs.empty() || Regs[0] == 0)) && 76 "only void types should have no register"); 77 } 78 ArgInfoArgInfo79 ArgInfo() : BaseArgInfo() {} 80 }; 81 82 struct CallLoweringInfo { 83 /// Calling convention to be used for the call. 84 CallingConv::ID CallConv = CallingConv::C; 85 86 /// Destination of the call. It should be either a register, globaladdress, 87 /// or externalsymbol. 88 MachineOperand Callee = MachineOperand::CreateImm(0); 89 90 /// Descriptor for the return type of the function. 91 ArgInfo OrigRet; 92 93 /// List of descriptors of the arguments passed to the function. 94 SmallVector<ArgInfo, 8> OrigArgs; 95 96 /// Valid if the call has a swifterror inout parameter, and contains the 97 /// vreg that the swifterror should be copied into after the call. 98 Register SwiftErrorVReg; 99 100 MDNode *KnownCallees = nullptr; 101 102 /// True if the call must be tail call optimized. 103 bool IsMustTailCall = false; 104 105 /// True if the call passes all target-independent checks for tail call 106 /// optimization. 107 bool IsTailCall = false; 108 109 /// True if the call was lowered as a tail call. This is consumed by the 110 /// legalizer. This allows the legalizer to lower libcalls as tail calls. 111 bool LoweredTailCall = false; 112 113 /// True if the call is to a vararg function. 114 bool IsVarArg = false; 115 116 /// True if the function's return value can be lowered to registers. 117 bool CanLowerReturn = true; 118 119 /// VReg to hold the hidden sret parameter. 120 Register DemoteRegister; 121 122 /// The stack index for sret demotion. 123 int DemoteStackIndex; 124 }; 125 126 /// Argument handling is mostly uniform between the four places that 127 /// make these decisions: function formal arguments, call 128 /// instruction args, call instruction returns and function 129 /// returns. However, once a decision has been made on where an 130 /// argument should go, exactly what happens can vary slightly. This 131 /// class abstracts the differences. 132 struct ValueHandler { ValueHandlerValueHandler133 ValueHandler(bool IsIncoming, MachineIRBuilder &MIRBuilder, 134 MachineRegisterInfo &MRI, CCAssignFn *AssignFn) 135 : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn), 136 IsIncomingArgumentHandler(IsIncoming) {} 137 138 virtual ~ValueHandler() = default; 139 140 /// Returns true if the handler is dealing with incoming arguments, 141 /// i.e. those that move values from some physical location to vregs. isIncomingArgumentHandlerValueHandler142 bool isIncomingArgumentHandler() const { 143 return IsIncomingArgumentHandler; 144 } 145 146 /// Materialize a VReg containing the address of the specified 147 /// stack-based object. This is either based on a FrameIndex or 148 /// direct SP manipulation, depending on the context. \p MPO 149 /// should be initialized to an appropriate description of the 150 /// address created. 151 virtual Register getStackAddress(uint64_t Size, int64_t Offset, 152 MachinePointerInfo &MPO) = 0; 153 154 /// The specified value has been assigned to a physical register, 155 /// handle the appropriate COPY (either to or from) and mark any 156 /// relevant uses/defines as needed. 157 virtual void assignValueToReg(Register ValVReg, Register PhysReg, 158 CCValAssign &VA) = 0; 159 160 /// The specified value has been assigned to a stack 161 /// location. Load or store it there, with appropriate extension 162 /// if necessary. 163 virtual void assignValueToAddress(Register ValVReg, Register Addr, 164 uint64_t Size, MachinePointerInfo &MPO, 165 CCValAssign &VA) = 0; 166 167 /// An overload which takes an ArgInfo if additional information about 168 /// the arg is needed. assignValueToAddressValueHandler169 virtual void assignValueToAddress(const ArgInfo &Arg, Register Addr, 170 uint64_t Size, MachinePointerInfo &MPO, 171 CCValAssign &VA) { 172 assert(Arg.Regs.size() == 1); 173 assignValueToAddress(Arg.Regs[0], Addr, Size, MPO, VA); 174 } 175 176 /// Handle custom values, which may be passed into one or more of \p VAs. 177 /// \return The number of \p VAs that have been assigned after the first 178 /// one, and which should therefore be skipped from further 179 /// processing. assignCustomValueValueHandler180 virtual unsigned assignCustomValue(const ArgInfo &Arg, 181 ArrayRef<CCValAssign> VAs) { 182 // This is not a pure virtual method because not all targets need to worry 183 // about custom values. 184 llvm_unreachable("Custom values not supported"); 185 } 186 187 /// Extend a register to the location type given in VA, capped at extending 188 /// to at most MaxSize bits. If MaxSizeBits is 0 then no maximum is set. 189 Register extendRegister(Register ValReg, CCValAssign &VA, 190 unsigned MaxSizeBits = 0); 191 assignArgValueHandler192 virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, 193 CCValAssign::LocInfo LocInfo, const ArgInfo &Info, 194 ISD::ArgFlagsTy Flags, CCState &State) { 195 return AssignFn(ValNo, ValVT, LocVT, LocInfo, Flags, State); 196 } 197 198 MachineIRBuilder &MIRBuilder; 199 MachineRegisterInfo &MRI; 200 CCAssignFn *AssignFn; 201 202 private: 203 bool IsIncomingArgumentHandler; 204 virtual void anchor(); 205 }; 206 207 struct IncomingValueHandler : public ValueHandler { IncomingValueHandlerIncomingValueHandler208 IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 209 CCAssignFn *AssignFn) 210 : ValueHandler(true, MIRBuilder, MRI, AssignFn) {} 211 }; 212 213 struct OutgoingValueHandler : public ValueHandler { OutgoingValueHandlerOutgoingValueHandler214 OutgoingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 215 CCAssignFn *AssignFn) 216 : ValueHandler(false, MIRBuilder, MRI, AssignFn) {} 217 }; 218 219 protected: 220 /// Getter for generic TargetLowering class. getTLI()221 const TargetLowering *getTLI() const { 222 return TLI; 223 } 224 225 /// Getter for target specific TargetLowering class. 226 template <class XXXTargetLowering> getTLI()227 const XXXTargetLowering *getTLI() const { 228 return static_cast<const XXXTargetLowering *>(TLI); 229 } 230 231 /// \returns Flags corresponding to the attributes on the \p ArgIdx-th 232 /// parameter of \p Call. 233 ISD::ArgFlagsTy getAttributesForArgIdx(const CallBase &Call, 234 unsigned ArgIdx) const; 235 236 /// Adds flags to \p Flags based off of the attributes in \p Attrs. 237 /// \p OpIdx is the index in \p Attrs to add flags from. 238 void addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags, 239 const AttributeList &Attrs, 240 unsigned OpIdx) const; 241 242 template <typename FuncInfoTy> 243 void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, 244 const FuncInfoTy &FuncInfo) const; 245 246 /// Generate instructions for packing \p SrcRegs into one big register 247 /// corresponding to the aggregate type \p PackedTy. 248 /// 249 /// \param SrcRegs should contain one virtual register for each base type in 250 /// \p PackedTy, as returned by computeValueLLTs. 251 /// 252 /// \return The packed register. 253 Register packRegs(ArrayRef<Register> SrcRegs, Type *PackedTy, 254 MachineIRBuilder &MIRBuilder) const; 255 256 /// Generate instructions for unpacking \p SrcReg into the \p DstRegs 257 /// corresponding to the aggregate type \p PackedTy. 258 /// 259 /// \param DstRegs should contain one virtual register for each base type in 260 /// \p PackedTy, as returned by computeValueLLTs. 261 void unpackRegs(ArrayRef<Register> DstRegs, Register SrcReg, Type *PackedTy, 262 MachineIRBuilder &MIRBuilder) const; 263 264 /// Invoke Handler::assignArg on each of the given \p Args and then use 265 /// \p Handler to move them to the assigned locations. 266 /// 267 /// \return True if everything has succeeded, false otherwise. 268 bool handleAssignments(MachineIRBuilder &MIRBuilder, 269 SmallVectorImpl<ArgInfo> &Args, 270 ValueHandler &Handler) const; 271 bool handleAssignments(CCState &CCState, 272 SmallVectorImpl<CCValAssign> &ArgLocs, 273 MachineIRBuilder &MIRBuilder, 274 SmallVectorImpl<ArgInfo> &Args, 275 ValueHandler &Handler) const; 276 277 /// Analyze passed or returned values from a call, supplied in \p ArgInfo, 278 /// incorporating info about the passed values into \p CCState. 279 /// 280 /// Used to check if arguments are suitable for tail call lowering. 281 bool analyzeArgInfo(CCState &CCState, SmallVectorImpl<ArgInfo> &Args, 282 CCAssignFn &AssignFnFixed, 283 CCAssignFn &AssignFnVarArg) const; 284 285 /// Check whether parameters to a call that are passed in callee saved 286 /// registers are the same as from the calling function. This needs to be 287 /// checked for tail call eligibility. 288 bool parametersInCSRMatch(const MachineRegisterInfo &MRI, 289 const uint32_t *CallerPreservedMask, 290 const SmallVectorImpl<CCValAssign> &ArgLocs, 291 const SmallVectorImpl<ArgInfo> &OutVals) const; 292 293 /// \returns True if the calling convention for a callee and its caller pass 294 /// results in the same way. Typically used for tail call eligibility checks. 295 /// 296 /// \p Info is the CallLoweringInfo for the call. 297 /// \p MF is the MachineFunction for the caller. 298 /// \p InArgs contains the results of the call. 299 /// \p CalleeAssignFnFixed is the CCAssignFn to be used for the callee for 300 /// fixed arguments. 301 /// \p CalleeAssignFnVarArg is similar, but for varargs. 302 /// \p CallerAssignFnFixed is the CCAssignFn to be used for the caller for 303 /// fixed arguments. 304 /// \p CallerAssignFnVarArg is similar, but for varargs. 305 bool resultsCompatible(CallLoweringInfo &Info, MachineFunction &MF, 306 SmallVectorImpl<ArgInfo> &InArgs, 307 CCAssignFn &CalleeAssignFnFixed, 308 CCAssignFn &CalleeAssignFnVarArg, 309 CCAssignFn &CallerAssignFnFixed, 310 CCAssignFn &CallerAssignFnVarArg) const; 311 312 public: CallLowering(const TargetLowering * TLI)313 CallLowering(const TargetLowering *TLI) : TLI(TLI) {} 314 virtual ~CallLowering() = default; 315 316 /// \return true if the target is capable of handling swifterror values that 317 /// have been promoted to a specified register. The extended versions of 318 /// lowerReturn and lowerCall should be implemented. supportSwiftError()319 virtual bool supportSwiftError() const { 320 return false; 321 } 322 323 /// Load the returned value from the stack into virtual registers in \p VRegs. 324 /// It uses the frame index \p FI and the start offset from \p DemoteReg. 325 /// The loaded data size will be determined from \p RetTy. 326 void insertSRetLoads(MachineIRBuilder &MIRBuilder, Type *RetTy, 327 ArrayRef<Register> VRegs, Register DemoteReg, 328 int FI) const; 329 330 /// Store the return value given by \p VRegs into stack starting at the offset 331 /// specified in \p DemoteReg. 332 void insertSRetStores(MachineIRBuilder &MIRBuilder, Type *RetTy, 333 ArrayRef<Register> VRegs, Register DemoteReg) const; 334 335 /// Insert the hidden sret ArgInfo to the beginning of \p SplitArgs. 336 /// This function should be called from the target specific 337 /// lowerFormalArguments when \p F requires the sret demotion. 338 void insertSRetIncomingArgument(const Function &F, 339 SmallVectorImpl<ArgInfo> &SplitArgs, 340 Register &DemoteReg, MachineRegisterInfo &MRI, 341 const DataLayout &DL) const; 342 343 /// For the call-base described by \p CB, insert the hidden sret ArgInfo to 344 /// the OrigArgs field of \p Info. 345 void insertSRetOutgoingArgument(MachineIRBuilder &MIRBuilder, 346 const CallBase &CB, 347 CallLoweringInfo &Info) const; 348 349 /// \return True if the return type described by \p Outs can be returned 350 /// without performing sret demotion. 351 bool checkReturn(CCState &CCInfo, SmallVectorImpl<BaseArgInfo> &Outs, 352 CCAssignFn *Fn) const; 353 354 /// Get the type and the ArgFlags for the split components of \p RetTy as 355 /// returned by \c ComputeValueVTs. 356 void getReturnInfo(CallingConv::ID CallConv, Type *RetTy, AttributeList Attrs, 357 SmallVectorImpl<BaseArgInfo> &Outs, 358 const DataLayout &DL) const; 359 360 /// Toplevel function to check the return type based on the target calling 361 /// convention. \return True if the return value of \p MF can be returned 362 /// without performing sret demotion. 363 bool checkReturnTypeForCallConv(MachineFunction &MF) const; 364 365 /// This hook must be implemented to check whether the return values 366 /// described by \p Outs can fit into the return registers. If false 367 /// is returned, an sret-demotion is performed. canLowerReturn(MachineFunction & MF,CallingConv::ID CallConv,SmallVectorImpl<BaseArgInfo> & Outs,bool IsVarArg)368 virtual bool canLowerReturn(MachineFunction &MF, CallingConv::ID CallConv, 369 SmallVectorImpl<BaseArgInfo> &Outs, 370 bool IsVarArg) const { 371 return true; 372 } 373 374 /// This hook must be implemented to lower outgoing return values, described 375 /// by \p Val, into the specified virtual registers \p VRegs. 376 /// This hook is used by GlobalISel. 377 /// 378 /// \p FLI is required for sret demotion. 379 /// 380 /// \p SwiftErrorVReg is non-zero if the function has a swifterror parameter 381 /// that needs to be implicitly returned. 382 /// 383 /// \return True if the lowering succeeds, false otherwise. lowerReturn(MachineIRBuilder & MIRBuilder,const Value * Val,ArrayRef<Register> VRegs,FunctionLoweringInfo & FLI,Register SwiftErrorVReg)384 virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, 385 ArrayRef<Register> VRegs, FunctionLoweringInfo &FLI, 386 Register SwiftErrorVReg) const { 387 if (!supportSwiftError()) { 388 assert(SwiftErrorVReg == 0 && "attempt to use unsupported swifterror"); 389 return lowerReturn(MIRBuilder, Val, VRegs, FLI); 390 } 391 return false; 392 } 393 394 /// This hook behaves as the extended lowerReturn function, but for targets 395 /// that do not support swifterror value promotion. lowerReturn(MachineIRBuilder & MIRBuilder,const Value * Val,ArrayRef<Register> VRegs,FunctionLoweringInfo & FLI)396 virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, 397 ArrayRef<Register> VRegs, 398 FunctionLoweringInfo &FLI) const { 399 return false; 400 } 401 fallBackToDAGISel(const Function & F)402 virtual bool fallBackToDAGISel(const Function &F) const { return false; } 403 404 /// This hook must be implemented to lower the incoming (formal) 405 /// arguments, described by \p VRegs, for GlobalISel. Each argument 406 /// must end up in the related virtual registers described by \p VRegs. 407 /// In other words, the first argument should end up in \c VRegs[0], 408 /// the second in \c VRegs[1], and so on. For each argument, there will be one 409 /// register for each non-aggregate type, as returned by \c computeValueLLTs. 410 /// \p MIRBuilder is set to the proper insertion for the argument 411 /// lowering. \p FLI is required for sret demotion. 412 /// 413 /// \return True if the lowering succeeded, false otherwise. lowerFormalArguments(MachineIRBuilder & MIRBuilder,const Function & F,ArrayRef<ArrayRef<Register>> VRegs,FunctionLoweringInfo & FLI)414 virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, 415 const Function &F, 416 ArrayRef<ArrayRef<Register>> VRegs, 417 FunctionLoweringInfo &FLI) const { 418 return false; 419 } 420 421 /// This hook must be implemented to lower the given call instruction, 422 /// including argument and return value marshalling. 423 /// 424 /// 425 /// \return true if the lowering succeeded, false otherwise. lowerCall(MachineIRBuilder & MIRBuilder,CallLoweringInfo & Info)426 virtual bool lowerCall(MachineIRBuilder &MIRBuilder, 427 CallLoweringInfo &Info) const { 428 return false; 429 } 430 431 /// Lower the given call instruction, including argument and return value 432 /// marshalling. 433 /// 434 /// \p CI is the call/invoke instruction. 435 /// 436 /// \p ResRegs are the registers where the call's return value should be 437 /// stored (or 0 if there is no return value). There will be one register for 438 /// each non-aggregate type, as returned by \c computeValueLLTs. 439 /// 440 /// \p ArgRegs is a list of lists of virtual registers containing each 441 /// argument that needs to be passed (argument \c i should be placed in \c 442 /// ArgRegs[i]). For each argument, there will be one register for each 443 /// non-aggregate type, as returned by \c computeValueLLTs. 444 /// 445 /// \p SwiftErrorVReg is non-zero if the call has a swifterror inout 446 /// parameter, and contains the vreg that the swifterror should be copied into 447 /// after the call. 448 /// 449 /// \p GetCalleeReg is a callback to materialize a register for the callee if 450 /// the target determines it cannot jump to the destination based purely on \p 451 /// CI. This might be because \p CI is indirect, or because of the limited 452 /// range of an immediate jump. 453 /// 454 /// \return true if the lowering succeeded, false otherwise. 455 bool lowerCall(MachineIRBuilder &MIRBuilder, const CallBase &Call, 456 ArrayRef<Register> ResRegs, 457 ArrayRef<ArrayRef<Register>> ArgRegs, Register SwiftErrorVReg, 458 std::function<unsigned()> GetCalleeReg) const; 459 }; 460 461 } // end namespace llvm 462 463 #endif // LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H 464