LLVM  8.0.1
CallLowering.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/CallLowering.h - Call lowering ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file describes how to lower LLVM calls to machine code calls.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
16 #define LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
17 
18 #include "llvm/ADT/ArrayRef.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 DataLayout;
31 class Function;
32 class MachineIRBuilder;
33 class MachineOperand;
34 struct MachinePointerInfo;
35 class MachineRegisterInfo;
36 class TargetLowering;
37 class Type;
38 class Value;
39 
40 class CallLowering {
41  const TargetLowering *TLI;
42 
43  virtual void anchor();
44 public:
45  struct ArgInfo {
46  unsigned Reg;
47  Type *Ty;
49  bool IsFixed;
50 
51  ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{},
52  bool IsFixed = true)
53  : Reg(Reg), Ty(Ty), Flags(Flags), IsFixed(IsFixed) {}
54  };
55 
56  /// Argument handling is mostly uniform between the four places that
57  /// make these decisions: function formal arguments, call
58  /// instruction args, call instruction returns and function
59  /// returns. However, once a decision has been made on where an
60  /// arugment should go, exactly what happens can vary slightly. This
61  /// class abstracts the differences.
62  struct ValueHandler {
64  CCAssignFn *AssignFn)
65  : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn) {}
66 
67  virtual ~ValueHandler() = default;
68 
69  /// Materialize a VReg containing the address of the specified
70  /// stack-based object. This is either based on a FrameIndex or
71  /// direct SP manipulation, depending on the context. \p MPO
72  /// should be initialized to an appropriate description of the
73  /// address created.
74  virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
75  MachinePointerInfo &MPO) = 0;
76 
77  /// The specified value has been assigned to a physical register,
78  /// handle the appropriate COPY (either to or from) and mark any
79  /// relevant uses/defines as needed.
80  virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
81  CCValAssign &VA) = 0;
82 
83  /// The specified value has been assigned to a stack
84  /// location. Load or store it there, with appropriate extension
85  /// if necessary.
86  virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
87  uint64_t Size, MachinePointerInfo &MPO,
88  CCValAssign &VA) = 0;
89 
90  /// Handle custom values, which may be passed into one or more of \p VAs.
91  /// \return The number of \p VAs that have been assigned after the first
92  /// one, and which should therefore be skipped from further
93  /// processing.
94  virtual unsigned assignCustomValue(const ArgInfo &Arg,
96  // This is not a pure virtual method because not all targets need to worry
97  // about custom values.
98  llvm_unreachable("Custom values not supported");
99  }
100 
101  unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
102 
103  virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
104  CCValAssign::LocInfo LocInfo, const ArgInfo &Info,
105  CCState &State) {
106  return AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
107  }
108 
112 
113  private:
114  virtual void anchor();
115  };
116 
117 protected:
118  /// Getter for generic TargetLowering class.
119  const TargetLowering *getTLI() const {
120  return TLI;
121  }
122 
123  /// Getter for target specific TargetLowering class.
124  template <class XXXTargetLowering>
125  const XXXTargetLowering *getTLI() const {
126  return static_cast<const XXXTargetLowering *>(TLI);
127  }
128 
129  template <typename FuncInfoTy>
130  void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL,
131  const FuncInfoTy &FuncInfo) const;
132 
133  /// Invoke Handler::assignArg on each of the given \p Args and then use
134  /// \p Callback to move them to the assigned locations.
135  ///
136  /// \return True if everything has succeeded, false otherwise.
138  ValueHandler &Handler) const;
139 
140 public:
141  CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
142  virtual ~CallLowering() = default;
143 
144  /// This hook must be implemented to lower outgoing return values, described
145  /// by \p Val, into the specified virtual registers \p VRegs.
146  /// This hook is used by GlobalISel.
147  ///
148  /// \return True if the lowering succeeds, false otherwise.
149  virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
150  ArrayRef<unsigned> VRegs) const {
151  return false;
152  }
153 
154  /// This hook must be implemented to lower the incoming (formal)
155  /// arguments, described by \p Args, for GlobalISel. Each argument
156  /// must end up in the related virtual register described by VRegs.
157  /// In other words, the first argument should end up in VRegs[0],
158  /// the second in VRegs[1], and so on.
159  /// \p MIRBuilder is set to the proper insertion for the argument
160  /// lowering.
161  ///
162  /// \return True if the lowering succeeded, false otherwise.
163  virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
164  const Function &F,
165  ArrayRef<unsigned> VRegs) const {
166  return false;
167  }
168 
169  /// This hook must be implemented to lower the given call instruction,
170  /// including argument and return value marshalling.
171  ///
172  /// \p CallConv is the calling convention to be used for the call.
173  ///
174  /// \p Callee is the destination of the call. It should be either a register,
175  /// globaladdress, or externalsymbol.
176  ///
177  /// \p ResTy is the type returned by the function
178  ///
179  /// \p ResReg is the generic virtual register that the returned
180  /// value should be lowered into.
181  ///
182  /// \p ArgTys is a list of the types each member of \p ArgRegs has; used by
183  /// the target to decide which register/stack slot should be allocated.
184  ///
185  /// \p ArgRegs is a list of virtual registers containing each argument that
186  /// needs to be passed.
187  ///
188  /// \return true if the lowering succeeded, false otherwise.
189  virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
190  const MachineOperand &Callee, const ArgInfo &OrigRet,
191  ArrayRef<ArgInfo> OrigArgs) const {
192  return false;
193  }
194 
195  /// Lower the given call instruction, including argument and return value
196  /// marshalling.
197  ///
198  /// \p CI is the call/invoke instruction.
199  ///
200  /// \p ResReg is a register where the call's return value should be stored (or
201  /// 0 if there is no return value).
202  ///
203  /// \p ArgRegs is a list of virtual registers containing each argument that
204  /// needs to be passed.
205  ///
206  /// \p GetCalleeReg is a callback to materialize a register for the callee if
207  /// the target determines it cannot jump to the destination based purely on \p
208  /// CI. This might be because \p CI is indirect, or because of the limited
209  /// range of an immediate jump.
210  ///
211  /// \return true if the lowering succeeded, false otherwise.
212  bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
213  unsigned ResReg, ArrayRef<unsigned> ArgRegs,
214  std::function<unsigned()> GetCalleeReg) const;
215 };
216 
217 } // end namespace llvm
218 
219 #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:49
ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, CCAssignFn *AssignFn)
Definition: CallLowering.h:63
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MachineRegisterInfo & MRI
Definition: CallLowering.h:110
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:51
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:119
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:163
const XXXTargetLowering * getTLI() const
Getter for target specific TargetLowering class.
Definition: CallLowering.h:125
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:33
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
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:46
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, const MachineOperand &Callee, const ArgInfo &OrigRet, ArrayRef< ArgInfo > OrigArgs) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
Definition: CallLowering.h:189
Helper class to build MachineInstr.
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
CallLowering(const TargetLowering *TLI)
Definition: CallLowering.h:141
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:62
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:94
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.
amdgpu Simplify well known AMD library false Value Value * Arg
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef< unsigned > VRegs) const
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
Definition: CallLowering.h:149
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:711
virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, CCState &State)
Definition: CallLowering.h:103
uint32_t Size
Definition: Profile.cpp:47
LLVM Value Representation.
Definition: Value.h:73
print Print MemDeps of function
constexpr char Args[]
Key for Kernel::Metadata::mArgs.