1 //===-- llvm/CodeGen/GlobalISel/CallLowering.h - Call lowering --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file describes how to lower LLVM calls to machine code calls.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
16 #define LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Target/TargetCallingConv.h"
25 // Forward declarations.
26 class MachineIRBuilder;
32 const TargetLowering *TLI;
37 ISD::ArgFlagsTy Flags;
40 ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{},
42 : Reg(Reg), Ty(Ty), Flags(Flags), IsFixed(IsFixed) {}
45 /// Argument handling is mostly uniform between the four places that
46 /// make these decisions: function formal arguments, call
47 /// instruction args, call instruction returns and function
48 /// returns. However, once a decision has been made on where an
49 /// arugment should go, exactly what happens can vary slightly. This
50 /// class abstracts the differences.
52 /// Materialize a VReg containing the address of the specified
53 /// stack-based object. This is either based on a FrameIndex or
54 /// direct SP manipulation, depending on the context. \p MPO
55 /// should be initialized to an appropriate description of the
57 virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
58 MachinePointerInfo &MPO) = 0;
60 /// The specified value has been assigned to a physical register,
61 /// handle the appropriate COPY (either to or from) and mark any
62 /// relevant uses/defines as needed.
63 virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
66 /// The specified value has been assigned to a stack
67 /// location. Load or store it there, with appropriate extension
69 virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
70 uint64_t Size, MachinePointerInfo &MPO,
73 /// Handle custom values, which may be passed into one or more of \p VAs.
74 /// \return The number of \p VAs that have been assigned after the first
75 /// one, and which should therefore be skipped from further
77 virtual unsigned assignCustomValue(const ArgInfo &Arg,
78 ArrayRef<CCValAssign> VAs) {
79 // This is not a pure virtual method because not all targets need to worry
80 // about custom values.
81 llvm_unreachable("Custom values not supported");
84 unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
86 virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
87 CCValAssign::LocInfo LocInfo, const ArgInfo &Info,
89 return AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
92 ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
94 : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn) {}
96 virtual ~ValueHandler() {}
98 MachineIRBuilder &MIRBuilder;
99 MachineRegisterInfo &MRI;
100 CCAssignFn *AssignFn;
104 /// Getter for generic TargetLowering class.
105 const TargetLowering *getTLI() const {
109 /// Getter for target specific TargetLowering class.
110 template <class XXXTargetLowering>
111 const XXXTargetLowering *getTLI() const {
112 return static_cast<const XXXTargetLowering *>(TLI);
116 template <typename FuncInfoTy>
117 void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL,
118 const FuncInfoTy &FuncInfo) const;
120 /// Invoke Handler::assignArg on each of the given \p Args and then use
121 /// \p Callback to move them to the assigned locations.
123 /// \return True if everything has succeeded, false otherwise.
124 bool handleAssignments(MachineIRBuilder &MIRBuilder, ArrayRef<ArgInfo> Args,
125 ValueHandler &Callback) const;
128 CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
129 virtual ~CallLowering() {}
131 /// This hook must be implemented to lower outgoing return values, described
132 /// by \p Val, into the specified virtual register \p VReg.
133 /// This hook is used by GlobalISel.
135 /// \return True if the lowering succeeds, false otherwise.
136 virtual bool lowerReturn(MachineIRBuilder &MIRBuilder,
137 const Value *Val, unsigned VReg) const {
141 /// This hook must be implemented to lower the incoming (formal)
142 /// arguments, described by \p Args, for GlobalISel. Each argument
143 /// must end up in the related virtual register described by VRegs.
144 /// In other words, the first argument should end up in VRegs[0],
145 /// the second in VRegs[1], and so on.
146 /// \p MIRBuilder is set to the proper insertion for the argument
149 /// \return True if the lowering succeeded, false otherwise.
150 virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
152 ArrayRef<unsigned> VRegs) const {
156 /// This hook must be implemented to lower the given call instruction,
157 /// including argument and return value marshalling.
159 /// \p CallConv is the calling convention to be used for the call.
161 /// \p Callee is the destination of the call. It should be either a register,
162 /// globaladdress, or externalsymbol.
164 /// \p ResTy is the type returned by the function
166 /// \p ResReg is the generic virtual register that the returned
167 /// value should be lowered into.
169 /// \p ArgTys is a list of the types each member of \p ArgRegs has; used by
170 /// the target to decide which register/stack slot should be allocated.
172 /// \p ArgRegs is a list of virtual registers containing each argument that
173 /// needs to be passed.
175 /// \return true if the lowering succeeded, false otherwise.
176 virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
177 const MachineOperand &Callee, const ArgInfo &OrigRet,
178 ArrayRef<ArgInfo> OrigArgs) const {
182 /// Lower the given call instruction, including argument and return value
185 /// \p CI is the call/invoke instruction.
187 /// \p ResReg is a register where the call's return value should be stored (or
188 /// 0 if there is no return value).
190 /// \p ArgRegs is a list of virtual registers containing each argument that
191 /// needs to be passed.
193 /// \p GetCalleeReg is a callback to materialize a register for the callee if
194 /// the target determines it cannot jump to the destination based purely on \p
195 /// CI. This might be because \p CI is indirect, or because of the limited
196 /// range of an immediate jump.
198 /// \return true if the lowering succeeded, false otherwise.
199 bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
200 unsigned ResReg, ArrayRef<unsigned> ArgRegs,
201 std::function<unsigned()> GetCalleeReg) const;
203 } // End namespace llvm.