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;
39 ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{})
40 : Reg(Reg), Ty(Ty), Flags(Flags) {}
43 /// Argument handling is mostly uniform between the four places that
44 /// make these decisions: function formal arguments, call
45 /// instruction args, call instruction returns and function
46 /// returns. However, once a decision has been made on where an
47 /// arugment should go, exactly what happens can vary slightly. This
48 /// class abstracts the differences.
50 /// Materialize a VReg containing the address of the specified
51 /// stack-based object. This is either based on a FrameIndex or
52 /// direct SP manipulation, depending on the context. \p MPO
53 /// should be initialized to an appropriate description of the
55 virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
56 MachinePointerInfo &MPO) = 0;
58 /// The specified value has been assigned to a physical register,
59 /// handle the appropriate COPY (either to or from) and mark any
60 /// relevant uses/defines as needed.
61 virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
64 /// The specified value has been assigned to a stack
65 /// location. Load or store it there, with appropriate extension
67 virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
68 uint64_t Size, MachinePointerInfo &MPO,
71 unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
73 ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
74 : MIRBuilder(MIRBuilder), MRI(MRI) {}
76 virtual ~ValueHandler() {}
78 MachineIRBuilder &MIRBuilder;
79 MachineRegisterInfo &MRI;
83 /// Getter for generic TargetLowering class.
84 const TargetLowering *getTLI() const {
88 /// Getter for target specific TargetLowering class.
89 template <class XXXTargetLowering>
90 const XXXTargetLowering *getTLI() const {
91 return static_cast<const XXXTargetLowering *>(TLI);
95 template <typename FuncInfoTy>
96 void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL,
97 const FuncInfoTy &FuncInfo) const;
99 /// Invoke the \p AssignFn on each of the given \p Args and then use
100 /// \p Callback to move them to the assigned locations.
102 /// \return True if everything has succeeded, false otherwise.
103 bool handleAssignments(MachineIRBuilder &MIRBuilder, CCAssignFn *AssignFn,
104 ArrayRef<ArgInfo> Args, ValueHandler &Callback) const;
107 CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
108 virtual ~CallLowering() {}
110 /// This hook must be implemented to lower outgoing return values, described
111 /// by \p Val, into the specified virtual register \p VReg.
112 /// This hook is used by GlobalISel.
114 /// \return True if the lowering succeeds, false otherwise.
115 virtual bool lowerReturn(MachineIRBuilder &MIRBuilder,
116 const Value *Val, unsigned VReg) const {
120 /// This hook must be implemented to lower the incoming (formal)
121 /// arguments, described by \p Args, for GlobalISel. Each argument
122 /// must end up in the related virtual register described by VRegs.
123 /// In other words, the first argument should end up in VRegs[0],
124 /// the second in VRegs[1], and so on.
125 /// \p MIRBuilder is set to the proper insertion for the argument
128 /// \return True if the lowering succeeded, false otherwise.
129 virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
131 ArrayRef<unsigned> VRegs) const {
135 /// This hook must be implemented to lower the given call instruction,
136 /// including argument and return value marshalling.
138 /// \p Callee is the destination of the call. It should be either a register,
139 /// globaladdress, or externalsymbol.
141 /// \p ResTy is the type returned by the function
143 /// \p ResReg is the generic virtual register that the returned
144 /// value should be lowered into.
146 /// \p ArgTys is a list of the types each member of \p ArgRegs has; used by
147 /// the target to decide which register/stack slot should be allocated.
149 /// \p ArgRegs is a list of virtual registers containing each argument that
150 /// needs to be passed.
152 /// \return true if the lowering succeeded, false otherwise.
153 virtual bool lowerCall(MachineIRBuilder &MIRBuilder,
154 const MachineOperand &Callee, const ArgInfo &OrigRet,
155 ArrayRef<ArgInfo> OrigArgs) const {
159 /// This hook must be implemented to lower the given call instruction,
160 /// including argument and return value marshalling.
162 /// \p ResReg is a register where the call's return value should be stored (or
163 /// 0 if there is no return value).
165 /// \p ArgRegs is a list of virtual registers containing each argument that
166 /// needs to be passed.
168 /// \p GetCalleeReg is a callback to materialize a register for the callee if
169 /// the target determines it cannot jump to the destination based purely on \p
170 /// CI. This might be because \p CI is indirect, or because of the limited
171 /// range of an immediate jump.
173 /// \return true if the lowering succeeded, false otherwise.
174 virtual bool lowerCall(MachineIRBuilder &MIRBuilder, const CallInst &CI,
175 unsigned ResReg, ArrayRef<unsigned> ArgRegs,
176 std::function<unsigned()> GetCalleeReg) const;
178 } // End namespace llvm.