LLVM 20.0.0git
PPCCallLowering.cpp
Go to the documentation of this file.
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
15#include "PPCCallLowering.h"
16#include "PPCCallingConv.h"
17#include "PPCISelLowering.h"
23
24#define DEBUG_TYPE "ppc-call-lowering"
25
26using namespace llvm;
27
28namespace {
29
30struct OutgoingArgHandler : public CallLowering::OutgoingValueHandler {
31 OutgoingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
33 : OutgoingValueHandler(MIRBuilder, MRI), MIB(MIB) {}
34
35 void assignValueToReg(Register ValVReg, Register PhysReg,
36 const CCValAssign &VA) override;
37 void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
38 const MachinePointerInfo &MPO,
39 const CCValAssign &VA) override;
42 ISD::ArgFlagsTy Flags) override;
43
45};
46} // namespace
47
48void OutgoingArgHandler::assignValueToReg(Register ValVReg, Register PhysReg,
49 const CCValAssign &VA) {
50 MIB.addUse(PhysReg, RegState::Implicit);
51 Register ExtReg = extendRegister(ValVReg, VA);
52 MIRBuilder.buildCopy(PhysReg, ExtReg);
53}
54
55void OutgoingArgHandler::assignValueToAddress(Register ValVReg, Register Addr,
56 LLT MemTy,
57 const MachinePointerInfo &MPO,
58 const CCValAssign &VA) {
59 llvm_unreachable("unimplemented");
60}
61
62Register OutgoingArgHandler::getStackAddress(uint64_t Size, int64_t Offset,
64 ISD::ArgFlagsTy Flags) {
65 llvm_unreachable("unimplemented");
66}
67
69 : CallLowering(&TLI) {}
70
72 const Value *Val, ArrayRef<Register> VRegs,
74 Register SwiftErrorVReg) const {
75 auto MIB = MIRBuilder.buildInstrNoInsert(PPC::BLR8);
76 bool Success = true;
77 MachineFunction &MF = MIRBuilder.getMF();
78 const Function &F = MF.getFunction();
80 auto &DL = F.getDataLayout();
81 if (!VRegs.empty()) {
82 // Setup the information about the return value.
83 ArgInfo OrigArg{VRegs, Val->getType(), 0};
85
86 // Split the return value into consecutive registers if needed.
88 splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv());
89
90 // Use the calling convention callback to determine type and location of
91 // return value.
93
94 // Handler to move the return value into the correct location.
95 OutgoingArgHandler ArgHandler(MIRBuilder, MRI, MIB);
96
97 // Iterate over all return values, and move them to the assigned location.
98 Success = determineAndHandleAssignments(ArgHandler, ArgAssigner, SplitArgs,
99 MIRBuilder, F.getCallingConv(),
100 F.isVarArg());
101 }
102 MIRBuilder.insertInstr(MIB);
103 return Success;
104}
105
107 CallLoweringInfo &Info) const {
108 return false;
109}
110
112 const Function &F,
114 FunctionLoweringInfo &FLI) const {
115 MachineFunction &MF = MIRBuilder.getMF();
117 const auto &DL = F.getDataLayout();
118 auto &TLI = *getTLI<PPCTargetLowering>();
119
120 // Loop over each arg, set flags and split to single value types
121 SmallVector<ArgInfo, 8> SplitArgs;
122 unsigned I = 0;
123 for (const auto &Arg : F.args()) {
124 if (DL.getTypeStoreSize(Arg.getType()).isZero())
125 continue;
126
127 ArgInfo OrigArg{VRegs[I], Arg, I};
129 splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv());
130 ++I;
131 }
132
133 CCAssignFn *AssignFn =
134 TLI.ccAssignFnForCall(F.getCallingConv(), false, F.isVarArg());
135 IncomingValueAssigner ArgAssigner(AssignFn);
136 FormalArgHandler ArgHandler(MIRBuilder, MRI);
137 return determineAndHandleAssignments(ArgHandler, ArgAssigner, SplitArgs,
138 MIRBuilder, F.getCallingConv(),
139 F.isVarArg());
140}
141
142void PPCIncomingValueHandler::assignValueToReg(Register ValVReg,
143 Register PhysReg,
144 const CCValAssign &VA) {
145 markPhysRegUsed(PhysReg);
146 IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA);
147}
148
149void PPCIncomingValueHandler::assignValueToAddress(
150 Register ValVReg, Register Addr, LLT MemTy, const MachinePointerInfo &MPO,
151 const CCValAssign &VA) {
152 // define a lambda expression to load value
153 auto BuildLoad = [](MachineIRBuilder &MIRBuilder,
154 const MachinePointerInfo &MPO, LLT MemTy,
155 const DstOp &Res, Register Addr) {
157 auto *MMO = MF.getMachineMemOperand(MPO, MachineMemOperand::MOLoad, MemTy,
158 inferAlignFromPtrInfo(MF, MPO));
159 return MIRBuilder.buildLoad(Res, Addr, *MMO);
160 };
161
162 BuildLoad(MIRBuilder, MPO, MemTy, ValVReg, Addr);
163}
164
165Register PPCIncomingValueHandler::getStackAddress(uint64_t Size, int64_t Offset,
167 ISD::ArgFlagsTy Flags) {
168 auto &MFI = MIRBuilder.getMF().getFrameInfo();
169 const bool IsImmutable = !Flags.isByVal();
170 int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable);
172
173 // Build Frame Index based on whether the machine is 32-bit or 64-bit
177 StackUsed = std::max(StackUsed, Size + Offset);
178 return AddrReg.getReg(0);
179}
180
181void FormalArgHandler::markPhysRegUsed(unsigned PhysReg) {
182 MIRBuilder.getMRI()->addLiveIn(PhysReg);
183 MIRBuilder.getMBB().addLiveIn(PhysReg);
184}
unsigned const MachineRegisterInfo * MRI
#define Success
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file describes how to lower LLVM calls to machine code calls.
uint64_t Addr
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares the MachineIRBuilder class.
This file describes how to lower LLVM calls to machine code calls.
static const unsigned FramePtr
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
CCValAssign - Represent assignment of one arg/retval to a location.
void splitToValueTypes(const ArgInfo &OrigArgInfo, SmallVectorImpl< ArgInfo > &SplitArgs, const DataLayout &DL, CallingConv::ID CallConv, SmallVectorImpl< uint64_t > *Offsets=nullptr) const
Break OrigArgInfo into one or more pieces the calling convention can process, returned in SplitArgs.
bool determineAndHandleAssignments(ValueHandler &Handler, ValueAssigner &Assigner, SmallVectorImpl< ArgInfo > &Args, MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, bool IsVarArg, ArrayRef< Register > ThisReturnRegs={}) const
Invoke ValueAssigner::assignArg on each of the given Args and then use Handler to move them to the as...
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:364
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
Definition: LowLevelType.h:57
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
MachineInstrBuilder insertInstr(MachineInstrBuilder MIB)
Insert an existing instruction at the insertion point.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineFunction & getMF()
Getter for the function we currently build.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
@ MOLoad
The memory access reads data.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< Register > VRegs, FunctionLoweringInfo &FLI, Register SwiftErrorVReg) const override
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
bool lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const override
This hook must be implemented to lower the given call instruction, including argument and return valu...
PPCCallLowering(const PPCTargetLowering &TLI)
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< ArrayRef< Register > > VRegs, FunctionLoweringInfo &FLI) const override
This hook must be implemented to lower the incoming (formal) arguments, described by VRegs,...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
bool RetCC_PPC(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition: Utils.cpp:878
Base class for ValueHandlers used for arguments passed to a function call, or for return values.
Definition: CallLowering.h:347
virtual Register getStackAddress(uint64_t MemSize, int64_t Offset, MachinePointerInfo &MPO, ISD::ArgFlagsTy Flags)=0
Materialize a VReg containing the address of the specified stack-based object.
virtual void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy, const MachinePointerInfo &MPO, const CCValAssign &VA)=0
The specified value has been assigned to a stack location.
virtual void assignValueToReg(Register ValVReg, Register PhysReg, const CCValAssign &VA)=0
The specified value has been assigned to a physical register, handle the appropriate COPY (either to ...
This class contains a discriminated union of information about pointers in memory operands,...
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.