LLVM  9.0.0svn
CallLowering.h
Go to the documentation of this file.
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"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/CallingConv.h"
24 #include <cstdint>
25 #include <functional>
26 
27 namespace llvm {
28 
29 class DataLayout;
30 class Function;
31 class MachineIRBuilder;
32 class MachineOperand;
33 struct MachinePointerInfo;
34 class MachineRegisterInfo;
35 class TargetLowering;
36 class Type;
37 class Value;
38 
39 class CallLowering {
40  const TargetLowering *TLI;
41 
42  virtual void anchor();
43 public:
44  struct ArgInfo {
45  unsigned Reg;
46  Type *Ty;
48  bool IsFixed;
49 
50  ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{},
51  bool IsFixed = true)
52  : Reg(Reg), Ty(Ty), Flags(Flags), IsFixed(IsFixed) {
53  assert((Ty->isVoidTy() == (Reg == 0)) &&
54  "only void types should have no register");
55  }
56  };
57 
58  /// Argument handling is mostly uniform between the four places that
59  /// make these decisions: function formal arguments, call
60  /// instruction args, call instruction returns and function
61  /// returns. However, once a decision has been made on where an
62  /// arugment should go, exactly what happens can vary slightly. This
63  /// class abstracts the differences.
64  struct ValueHandler {
66  CCAssignFn *AssignFn)
67  : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn) {}
68 
69  virtual ~ValueHandler() = default;
70 
71  /// Returns true if the handler is dealing with formal arguments,
72  /// not with return values etc.
73  virtual bool isArgumentHandler() const { return false; }
74 
75  /// Materialize a VReg containing the address of the specified
76  /// stack-based object. This is either based on a FrameIndex or
77  /// direct SP manipulation, depending on the context. \p MPO
78  /// should be initialized to an appropriate description of the
79  /// address created.
80  virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
81  MachinePointerInfo &MPO) = 0;
82 
83  /// The specified value has been assigned to a physical register,
84  /// handle the appropriate COPY (either to or from) and mark any
85  /// relevant uses/defines as needed.
86  virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
87  CCValAssign &VA) = 0;
88 
89  /// The specified value has been assigned to a stack
90  /// location. Load or store it there, with appropriate extension
91  /// if necessary.
92  virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
93  uint64_t Size, MachinePointerInfo &MPO,
94  CCValAssign &VA) = 0;
95 
96  /// Handle custom values, which may be passed into one or more of \p VAs.
97  /// \return The number of \p VAs that have been assigned after the first
98  /// one, and which should therefore be skipped from further
99  /// processing.
100  virtual unsigned assignCustomValue(const ArgInfo &Arg,
101  ArrayRef<CCValAssign> VAs) {
102  // This is not a pure virtual method because not all targets need to worry
103  // about custom values.
104  llvm_unreachable("Custom values not supported");
105  }
106 
107  unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
108 
109  virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
110  CCValAssign::LocInfo LocInfo, const ArgInfo &Info,
111  CCState &State) {
112  return AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
113  }
114 
118 
119  private:
120  virtual void anchor();
121  };
122 
123 protected:
124  /// Getter for generic TargetLowering class.
125  const TargetLowering *getTLI() const {
126  return TLI;
127  }
128 
129  /// Getter for target specific TargetLowering class.
130  template <class XXXTargetLowering>
131  const XXXTargetLowering *getTLI() const {
132  return static_cast<const XXXTargetLowering *>(TLI);
133  }
134 
135  template <typename FuncInfoTy>
136  void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL,
137  const FuncInfoTy &FuncInfo) const;
138 
139  /// Invoke Handler::assignArg on each of the given \p Args and then use
140  /// \p Callback to move them to the assigned locations.
141  ///
142  /// \return True if everything has succeeded, false otherwise.
144  ValueHandler &Handler) const;
145 
146 public:
147  CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
148  virtual ~CallLowering() = default;
149 
150  /// \return true if the target is capable of handling swifterror values that
151  /// have been promoted to a specified register. The extended versions of
152  /// lowerReturn and lowerCall should be implemented.
153  virtual bool supportSwiftError() const {
154  return false;
155  }
156 
157  /// This hook must be implemented to lower outgoing return values, described
158  /// by \p Val, into the specified virtual registers \p VRegs.
159  /// This hook is used by GlobalISel.
160  ///
161  /// \p SwiftErrorVReg is non-zero if the function has a swifterror parameter
162  /// that needs to be implicitly returned.
163  ///
164  /// \return True if the lowering succeeds, false otherwise.
165  virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
166  ArrayRef<unsigned> VRegs,
167  unsigned SwiftErrorVReg) const {
168  if (!supportSwiftError()) {
169  assert(SwiftErrorVReg == 0 && "attempt to use unsupported swifterror");
170  return lowerReturn(MIRBuilder, Val, VRegs);
171  }
172  return false;
173  }
174 
175  /// This hook behaves as the extended lowerReturn function, but for targets
176  /// that do not support swifterror value promotion.
177  virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
178  ArrayRef<unsigned> VRegs) const {
179  return false;
180  }
181 
182 
183  /// This hook must be implemented to lower the incoming (formal)
184  /// arguments, described by \p Args, for GlobalISel. Each argument
185  /// must end up in the related virtual register described by VRegs.
186  /// In other words, the first argument should end up in VRegs[0],
187  /// the second in VRegs[1], and so on.
188  /// \p MIRBuilder is set to the proper insertion for the argument
189  /// lowering.
190  ///
191  /// \return True if the lowering succeeded, false otherwise.
192  virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
193  const Function &F,
194  ArrayRef<unsigned> VRegs) const {
195  return false;
196  }
197 
198  /// This hook must be implemented to lower the given call instruction,
199  /// including argument and return value marshalling.
200  ///
201  /// \p CallConv is the calling convention to be used for the call.
202  ///
203  /// \p Callee is the destination of the call. It should be either a register,
204  /// globaladdress, or externalsymbol.
205  ///
206  /// \p OrigRet is a descriptor for the return type of the function.
207  ///
208  /// \p OrigArgs is a list of descriptors of the arguments passed to the
209  /// function.
210  ///
211  /// \p SwiftErrorVReg is non-zero if the call has a swifterror inout
212  /// parameter, and contains the vreg that the swifterror should be copied into
213  /// after the call.
214  ///
215  /// \return true if the lowering succeeded, false otherwise.
216  virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
217  const MachineOperand &Callee, const ArgInfo &OrigRet,
218  ArrayRef<ArgInfo> OrigArgs,
219  unsigned SwiftErrorVReg) const {
220  if (!supportSwiftError()) {
221  assert(SwiftErrorVReg == 0 && "trying to use unsupported swifterror");
222  return lowerCall(MIRBuilder, CallConv, Callee, OrigRet, OrigArgs);
223  }
224  return false;
225  }
226 
227  /// This hook behaves as the extended lowerCall function, but for targets that
228  /// do not support swifterror value promotion.
229  virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
230  const MachineOperand &Callee, const ArgInfo &OrigRet,
231  ArrayRef<ArgInfo> OrigArgs) const {
232  return false;
233  }
234 
235  /// Lower the given call instruction, including argument and return value
236  /// marshalling.
237  ///
238  /// \p CI is the call/invoke instruction.
239  ///
240  /// \p ResReg is a register where the call's return value should be stored (or
241  /// 0 if there is no return value).
242  ///
243  /// \p ArgRegs is a list of virtual registers containing each argument that
244  /// needs to be passed.
245  ///
246  /// \p SwiftErrorVReg is non-zero if the call has a swifterror inout
247  /// parameter, and contains the vreg that the swifterror should be copied into
248  /// after the call.
249  ///
250  /// \p GetCalleeReg is a callback to materialize a register for the callee if
251  /// the target determines it cannot jump to the destination based purely on \p
252  /// CI. This might be because \p CI is indirect, or because of the limited
253  /// range of an immediate jump.
254  ///
255  /// \return true if the lowering succeeded, false otherwise.
256  bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
257  unsigned ResReg, ArrayRef<unsigned> ArgRegs,
258  unsigned SwiftErrorVReg,
259  std::function<unsigned()> GetCalleeReg) const;
260 
261 };
262 
263 } // end namespace llvm
264 
265 #endif // LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, CCAssignFn *AssignFn)
Definition: CallLowering.h:65
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MachineRegisterInfo & MRI
Definition: CallLowering.h:116
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...
F(f)
ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags=ISD::ArgFlagsTy{}, bool IsFixed=true)
Definition: CallLowering.h:50
bool handleAssignments(MachineIRBuilder &MIRBuilder, ArrayRef< ArgInfo > Args, ValueHandler &Handler) const
Invoke Handler::assignArg on each of the given Args and then use Callback to move them to the assigne...
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
Definition: CallLowering.h:125
virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< unsigned > VRegs) const
This hook must be implemented to lower the incoming (formal) arguments, described by Args...
Definition: CallLowering.h:192
const XXXTargetLowering * getTLI() const
Getter for target specific TargetLowering class.
Definition: CallLowering.h:131
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, const MachineOperand &Callee, const ArgInfo &OrigRet, ArrayRef< ArgInfo > OrigArgs, unsigned SwiftErrorVReg) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
Definition: CallLowering.h:216
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:140
virtual ~CallLowering()=default
unsigned const MachineRegisterInfo * MRI
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, const MachineOperand &Callee, const ArgInfo &OrigRet, ArrayRef< ArgInfo > OrigArgs) const
This hook behaves as the extended lowerCall function, but for targets that do not support swifterror ...
Definition: CallLowering.h:229
Helper class to build MachineInstr.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
CallLowering(const TargetLowering *TLI)
Definition: CallLowering.h:147
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:64
This class contains a discriminated union of information about pointers in memory operands...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual unsigned assignCustomValue(const ArgInfo &Arg, ArrayRef< CCValAssign > VAs)
Handle custom values, which may be passed into one or more of VAs.
Definition: CallLowering.h:100
CCState - This class holds information needed while lowering arguments and return values...
MachineOperand class - Representation of each machine instruction operand.
CCValAssign - Represent assignment of one arg/retval to a location.
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< unsigned > VRegs) const
This hook behaves as the extended lowerReturn function, but for targets that do not support swifterro...
Definition: CallLowering.h:177
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Establish a view to a call site for examination.
Definition: CallSite.h:897
virtual bool isArgumentHandler() const
Returns true if the handler is dealing with formal arguments, not with return values etc...
Definition: CallLowering.h:73
virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, CCState &State)
Definition: CallLowering.h:109
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< unsigned > VRegs, unsigned SwiftErrorVReg) const
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
Definition: CallLowering.h:165
uint32_t Size
Definition: Profile.cpp:46
virtual bool supportSwiftError() const
Definition: CallLowering.h:153
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
print Print MemDeps of function
constexpr char Args[]
Key for Kernel::Metadata::mArgs.