LLVM  10.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"
18 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/CallingConv.h"
25 #include <cstdint>
26 #include <functional>
27 
28 namespace llvm {
29 
30 class CCState;
31 class DataLayout;
32 class Function;
33 class MachineIRBuilder;
34 class MachineOperand;
35 struct MachinePointerInfo;
36 class MachineRegisterInfo;
37 class TargetLowering;
38 class Type;
39 class Value;
40 
41 class CallLowering {
42  const TargetLowering *TLI;
43 
44  virtual void anchor();
45 public:
46  struct ArgInfo {
48  Type *Ty;
50  bool IsFixed;
51 
53  ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{}, bool IsFixed = true)
54  : Regs(Regs.begin(), Regs.end()), Ty(Ty), Flags(Flags),
55  IsFixed(IsFixed) {
56  // FIXME: We should have just one way of saying "no register".
57  assert((Ty->isVoidTy() == (Regs.empty() || Regs[0] == 0)) &&
58  "only void types should have no register");
59  }
60 
61  ArgInfo() : Ty(nullptr), IsFixed(false) {}
62  };
63 
65  /// Calling convention to be used for the call.
67 
68  /// Destination of the call. It should be either a register, globaladdress,
69  /// or externalsymbol.
71 
72  /// Descriptor for the return type of the function.
74 
75  /// List of descriptors of the arguments passed to the function.
77 
78  /// Valid if the call has a swifterror inout parameter, and contains the
79  /// vreg that the swifterror should be copied into after the call.
80  Register SwiftErrorVReg = 0;
81 
82  MDNode *KnownCallees = nullptr;
83  };
84 
85  /// Argument handling is mostly uniform between the four places that
86  /// make these decisions: function formal arguments, call
87  /// instruction args, call instruction returns and function
88  /// returns. However, once a decision has been made on where an
89  /// arugment should go, exactly what happens can vary slightly. This
90  /// class abstracts the differences.
91  struct ValueHandler {
93  CCAssignFn *AssignFn)
94  : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn) {}
95 
96  virtual ~ValueHandler() = default;
97 
98  /// Returns true if the handler is dealing with incoming arguments,
99  /// i.e. those that move values from some physical location to vregs.
100  virtual bool isIncomingArgumentHandler() const { return false; }
101 
102  /// Materialize a VReg containing the address of the specified
103  /// stack-based object. This is either based on a FrameIndex or
104  /// direct SP manipulation, depending on the context. \p MPO
105  /// should be initialized to an appropriate description of the
106  /// address created.
107  virtual Register getStackAddress(uint64_t Size, int64_t Offset,
108  MachinePointerInfo &MPO) = 0;
109 
110  /// The specified value has been assigned to a physical register,
111  /// handle the appropriate COPY (either to or from) and mark any
112  /// relevant uses/defines as needed.
113  virtual void assignValueToReg(Register ValVReg, Register PhysReg,
114  CCValAssign &VA) = 0;
115 
116  /// The specified value has been assigned to a stack
117  /// location. Load or store it there, with appropriate extension
118  /// if necessary.
119  virtual void assignValueToAddress(Register ValVReg, Register Addr,
120  uint64_t Size, MachinePointerInfo &MPO,
121  CCValAssign &VA) = 0;
122 
123  /// Handle custom values, which may be passed into one or more of \p VAs.
124  /// \return The number of \p VAs that have been assigned after the first
125  /// one, and which should therefore be skipped from further
126  /// processing.
127  virtual unsigned assignCustomValue(const ArgInfo &Arg,
128  ArrayRef<CCValAssign> VAs) {
129  // This is not a pure virtual method because not all targets need to worry
130  // about custom values.
131  llvm_unreachable("Custom values not supported");
132  }
133 
134  Register extendRegister(Register ValReg, CCValAssign &VA);
135 
136  virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
137  CCValAssign::LocInfo LocInfo, const ArgInfo &Info,
138  CCState &State) {
139  return AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
140  }
141 
145 
146  private:
147  virtual void anchor();
148  };
149 
150 protected:
151  /// Getter for generic TargetLowering class.
152  const TargetLowering *getTLI() const {
153  return TLI;
154  }
155 
156  /// Getter for target specific TargetLowering class.
157  template <class XXXTargetLowering>
158  const XXXTargetLowering *getTLI() const {
159  return static_cast<const XXXTargetLowering *>(TLI);
160  }
161 
162  template <typename FuncInfoTy>
163  void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL,
164  const FuncInfoTy &FuncInfo) const;
165 
166  /// Generate instructions for packing \p SrcRegs into one big register
167  /// corresponding to the aggregate type \p PackedTy.
168  ///
169  /// \param SrcRegs should contain one virtual register for each base type in
170  /// \p PackedTy, as returned by computeValueLLTs.
171  ///
172  /// \return The packed register.
173  Register packRegs(ArrayRef<Register> SrcRegs, Type *PackedTy,
174  MachineIRBuilder &MIRBuilder) const;
175 
176  /// Generate instructions for unpacking \p SrcReg into the \p DstRegs
177  /// corresponding to the aggregate type \p PackedTy.
178  ///
179  /// \param DstRegs should contain one virtual register for each base type in
180  /// \p PackedTy, as returned by computeValueLLTs.
181  void unpackRegs(ArrayRef<Register> DstRegs, Register SrcReg, Type *PackedTy,
182  MachineIRBuilder &MIRBuilder) const;
183 
184  /// Invoke Handler::assignArg on each of the given \p Args and then use
185  /// \p Callback to move them to the assigned locations.
186  ///
187  /// \return True if everything has succeeded, false otherwise.
189  ValueHandler &Handler) const;
192  MachineIRBuilder &MIRBuilder, ArrayRef<ArgInfo> Args,
193  ValueHandler &Handler) const;
194 public:
195  CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
196  virtual ~CallLowering() = default;
197 
198  /// \return true if the target is capable of handling swifterror values that
199  /// have been promoted to a specified register. The extended versions of
200  /// lowerReturn and lowerCall should be implemented.
201  virtual bool supportSwiftError() const {
202  return false;
203  }
204 
205  /// This hook must be implemented to lower outgoing return values, described
206  /// by \p Val, into the specified virtual registers \p VRegs.
207  /// This hook is used by GlobalISel.
208  ///
209  /// \p SwiftErrorVReg is non-zero if the function has a swifterror parameter
210  /// that needs to be implicitly returned.
211  ///
212  /// \return True if the lowering succeeds, false otherwise.
213  virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
214  ArrayRef<Register> VRegs,
215  Register SwiftErrorVReg) const {
216  if (!supportSwiftError()) {
217  assert(SwiftErrorVReg == 0 && "attempt to use unsupported swifterror");
218  return lowerReturn(MIRBuilder, Val, VRegs);
219  }
220  return false;
221  }
222 
223  /// This hook behaves as the extended lowerReturn function, but for targets
224  /// that do not support swifterror value promotion.
225  virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
226  ArrayRef<Register> VRegs) const {
227  return false;
228  }
229 
230  /// This hook must be implemented to lower the incoming (formal)
231  /// arguments, described by \p VRegs, for GlobalISel. Each argument
232  /// must end up in the related virtual registers described by \p VRegs.
233  /// In other words, the first argument should end up in \c VRegs[0],
234  /// the second in \c VRegs[1], and so on. For each argument, there will be one
235  /// register for each non-aggregate type, as returned by \c computeValueLLTs.
236  /// \p MIRBuilder is set to the proper insertion for the argument
237  /// lowering.
238  ///
239  /// \return True if the lowering succeeded, false otherwise.
240  virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
241  const Function &F,
242  ArrayRef<ArrayRef<Register>> VRegs) const {
243  return false;
244  }
245 
246  /// This hook must be implemented to lower the given call instruction,
247  /// including argument and return value marshalling.
248  ///
249  ///
250  /// \return true if the lowering succeeded, false otherwise.
251  virtual bool lowerCall(MachineIRBuilder &MIRBuilder,
252  CallLoweringInfo &Info) const {
253  return false;
254  }
255 
256  /// Lower the given call instruction, including argument and return value
257  /// marshalling.
258  ///
259  /// \p CI is the call/invoke instruction.
260  ///
261  /// \p ResRegs are the registers where the call's return value should be
262  /// stored (or 0 if there is no return value). There will be one register for
263  /// each non-aggregate type, as returned by \c computeValueLLTs.
264  ///
265  /// \p ArgRegs is a list of lists of virtual registers containing each
266  /// argument that needs to be passed (argument \c i should be placed in \c
267  /// ArgRegs[i]). For each argument, there will be one register for each
268  /// non-aggregate type, as returned by \c computeValueLLTs.
269  ///
270  /// \p SwiftErrorVReg is non-zero if the call has a swifterror inout
271  /// parameter, and contains the vreg that the swifterror should be copied into
272  /// after the call.
273  ///
274  /// \p GetCalleeReg is a callback to materialize a register for the callee if
275  /// the target determines it cannot jump to the destination based purely on \p
276  /// CI. This might be because \p CI is indirect, or because of the limited
277  /// range of an immediate jump.
278  ///
279  /// \return true if the lowering succeeded, false otherwise.
280  bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
281  ArrayRef<Register> ResRegs,
282  ArrayRef<ArrayRef<Register>> ArgRegs, Register SwiftErrorVReg,
283  std::function<unsigned()> GetCalleeReg) const;
284 };
285 
286 } // end namespace llvm
287 
288 #endif // LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
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:92
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator begin() const
Definition: ArrayRef.h:136
MachineRegisterInfo & MRI
Definition: CallLowering.h:143
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...
Metadata node.
Definition: Metadata.h:863
F(f)
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< Register > VRegs) const
This hook behaves as the extended lowerReturn function, but for targets that do not support swifterro...
Definition: CallLowering.h:225
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:152
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
Definition: CallLowering.h:251
const XXXTargetLowering * getTLI() const
Getter for target specific TargetLowering class.
Definition: CallLowering.h:158
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool isIncomingArgumentHandler() const
Returns true if the handler is dealing with incoming arguments, i.e.
Definition: CallLowering.h:100
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 bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< ArrayRef< Register >> VRegs) const
This hook must be implemented to lower the incoming (formal) arguments, described by VRegs...
Definition: CallLowering.h:240
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
Helper class to build MachineInstr.
Register packRegs(ArrayRef< Register > SrcRegs, Type *PackedTy, MachineIRBuilder &MIRBuilder) const
Generate instructions for packing SrcRegs into one big register corresponding to the aggregate type P...
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:195
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:91
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:127
SmallVector< ArgInfo, 8 > OrigArgs
List of descriptors of the arguments passed to the function.
Definition: CallLowering.h:76
void unpackRegs(ArrayRef< Register > DstRegs, Register SrcReg, Type *PackedTy, MachineIRBuilder &MIRBuilder) const
Generate instructions for unpacking SrcReg into the DstRegs corresponding to the aggregate type Packe...
CCState - This class holds information needed while lowering arguments and return values...
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
CCValAssign - Represent assignment of one arg/retval to a location.
iterator end() const
Definition: ArrayRef.h:137
amdgpu Simplify well known AMD library false FunctionCallee Callee
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
static MachineOperand CreateImm(int64_t Val)
virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, CCState &State)
Definition: CallLowering.h:136
uint32_t Size
Definition: Profile.cpp:46
virtual bool supportSwiftError() const
Definition: CallLowering.h:201
ArgInfo(ArrayRef< Register > Regs, Type *Ty, ISD::ArgFlagsTy Flags=ISD::ArgFlagsTy{}, bool IsFixed=true)
Definition: CallLowering.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
print Print MemDeps of function
SmallVector< Register, 4 > Regs
Definition: CallLowering.h:47
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< Register > VRegs, Register SwiftErrorVReg) const
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
Definition: CallLowering.h:213
ArgInfo OrigRet
Descriptor for the return type of the function.
Definition: CallLowering.h:73
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143