PPCCallLowering.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===-- PPCCallLowering.h - Call lowering for GlobalISel -------*- 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 implements the lowering of LLVM calls to machine code calls for
  11. /// GlobalISel.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "PPCCallLowering.h"
  15. #include "PPCISelLowering.h"
  16. #include "PPCSubtarget.h"
  17. #include "PPCTargetMachine.h"
  18. #include "llvm/CodeGen/CallingConvLower.h"
  19. #include "llvm/CodeGen/GlobalISel/CallLowering.h"
  20. #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
  21. #include "llvm/CodeGen/TargetCallingConv.h"
  22. #include "llvm/Support/Debug.h"
  23. #define DEBUG_TYPE "ppc-call-lowering"
  24. using namespace llvm;
  25. PPCCallLowering::PPCCallLowering(const PPCTargetLowering &TLI)
  26. : CallLowering(&TLI) {}
  27. bool PPCCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
  28. const Value *Val, ArrayRef<Register> VRegs,
  29. FunctionLoweringInfo &FLI,
  30. Register SwiftErrorVReg) const {
  31. assert(((Val && !VRegs.empty()) || (!Val && VRegs.empty())) &&
  32. "Return value without a vreg");
  33. if (VRegs.size() > 0)
  34. return false;
  35. MIRBuilder.buildInstr(PPC::BLR8);
  36. return true;
  37. }
  38. bool PPCCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
  39. CallLoweringInfo &Info) const {
  40. return false;
  41. }
  42. bool PPCCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
  43. const Function &F,
  44. ArrayRef<ArrayRef<Register>> VRegs,
  45. FunctionLoweringInfo &FLI) const {
  46. MachineFunction &MF = MIRBuilder.getMF();
  47. MachineRegisterInfo &MRI = MF.getRegInfo();
  48. const auto &DL = F.getParent()->getDataLayout();
  49. auto &TLI = *getTLI<PPCTargetLowering>();
  50. // Loop over each arg, set flags and split to single value types
  51. SmallVector<ArgInfo, 8> SplitArgs;
  52. unsigned I = 0;
  53. for (const auto &Arg : F.args()) {
  54. if (DL.getTypeStoreSize(Arg.getType()).isZero())
  55. continue;
  56. ArgInfo OrigArg{VRegs[I], Arg, I};
  57. setArgFlags(OrigArg, I + AttributeList::FirstArgIndex, DL, F);
  58. splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv());
  59. ++I;
  60. }
  61. CCAssignFn *AssignFn =
  62. TLI.ccAssignFnForCall(F.getCallingConv(), false, F.isVarArg());
  63. IncomingValueAssigner ArgAssigner(AssignFn);
  64. FormalArgHandler ArgHandler(MIRBuilder, MRI);
  65. return determineAndHandleAssignments(ArgHandler, ArgAssigner, SplitArgs,
  66. MIRBuilder, F.getCallingConv(),
  67. F.isVarArg());
  68. }
  69. void PPCIncomingValueHandler::assignValueToReg(Register ValVReg,
  70. Register PhysReg,
  71. CCValAssign VA) {
  72. markPhysRegUsed(PhysReg);
  73. IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA);
  74. }
  75. void PPCIncomingValueHandler::assignValueToAddress(Register ValVReg,
  76. Register Addr, LLT MemTy,
  77. MachinePointerInfo &MPO,
  78. CCValAssign &VA) {
  79. // define a lambda expression to load value
  80. auto BuildLoad = [](MachineIRBuilder &MIRBuilder, MachinePointerInfo &MPO,
  81. LLT MemTy, const DstOp &Res, Register Addr) {
  82. MachineFunction &MF = MIRBuilder.getMF();
  83. auto *MMO = MF.getMachineMemOperand(MPO, MachineMemOperand::MOLoad, MemTy,
  84. inferAlignFromPtrInfo(MF, MPO));
  85. return MIRBuilder.buildLoad(Res, Addr, *MMO);
  86. };
  87. BuildLoad(MIRBuilder, MPO, MemTy, ValVReg, Addr);
  88. }
  89. Register PPCIncomingValueHandler::getStackAddress(uint64_t Size, int64_t Offset,
  90. MachinePointerInfo &MPO,
  91. ISD::ArgFlagsTy Flags) {
  92. auto &MFI = MIRBuilder.getMF().getFrameInfo();
  93. const bool IsImmutable = !Flags.isByVal();
  94. int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable);
  95. MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
  96. // Build Frame Index based on whether the machine is 32-bit or 64-bit
  97. llvm::LLT FramePtr = LLT::pointer(
  98. 0, MIRBuilder.getMF().getDataLayout().getPointerSizeInBits());
  99. MachineInstrBuilder AddrReg = MIRBuilder.buildFrameIndex(FramePtr, FI);
  100. StackUsed = std::max(StackUsed, Size + Offset);
  101. return AddrReg.getReg(0);
  102. }
  103. void FormalArgHandler::markPhysRegUsed(unsigned PhysReg) {
  104. MIRBuilder.getMRI()->addLiveIn(PhysReg);
  105. MIRBuilder.getMBB().addLiveIn(PhysReg);
  106. }