]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h
MFC r316912: 7793 ztest fails assertion in dmu_tx_willuse_space
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / GlobalISel / CallLowering.h
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/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"
23
24 namespace llvm {
25 // Forward declarations.
26 class MachineIRBuilder;
27 class MachineOperand;
28 class TargetLowering;
29 class Value;
30
31 class CallLowering {
32   const TargetLowering *TLI;
33 public:
34   struct ArgInfo {
35     unsigned Reg;
36     Type *Ty;
37     ISD::ArgFlagsTy Flags;
38
39     ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{})
40         : Reg(Reg), Ty(Ty), Flags(Flags) {}
41   };
42
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.
49   struct ValueHandler {
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
54     /// address created.
55     virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
56                                      MachinePointerInfo &MPO) = 0;
57
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,
62                                   CCValAssign &VA) = 0;
63
64     /// The specified value has been assigned to a stack
65     /// location. Load or store it there, with appropriate extension
66     /// if necessary.
67     virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
68                                       uint64_t Size, MachinePointerInfo &MPO,
69                                       CCValAssign &VA) = 0;
70
71     unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
72
73     ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
74         : MIRBuilder(MIRBuilder), MRI(MRI) {}
75
76     virtual ~ValueHandler() {}
77
78     MachineIRBuilder &MIRBuilder;
79     MachineRegisterInfo &MRI;
80   };
81
82 protected:
83   /// Getter for generic TargetLowering class.
84   const TargetLowering *getTLI() const {
85     return TLI;
86   }
87
88   /// Getter for target specific TargetLowering class.
89   template <class XXXTargetLowering>
90     const XXXTargetLowering *getTLI() const {
91     return static_cast<const XXXTargetLowering *>(TLI);
92   }
93
94
95   template <typename FuncInfoTy>
96   void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL,
97                    const FuncInfoTy &FuncInfo) const;
98
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.
101   ///
102   /// \return True if everything has succeeded, false otherwise.
103   bool handleAssignments(MachineIRBuilder &MIRBuilder, CCAssignFn *AssignFn,
104                          ArrayRef<ArgInfo> Args, ValueHandler &Callback) const;
105
106 public:
107   CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
108   virtual ~CallLowering() {}
109
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.
113   ///
114   /// \return True if the lowering succeeds, false otherwise.
115   virtual bool lowerReturn(MachineIRBuilder &MIRBuilder,
116                            const Value *Val, unsigned VReg) const {
117     return false;
118   }
119
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
126   /// lowering.
127   ///
128   /// \return True if the lowering succeeded, false otherwise.
129   virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
130                                     const Function &F,
131                                     ArrayRef<unsigned> VRegs) const {
132     return false;
133   }
134
135   /// This hook must be implemented to lower the given call instruction,
136   /// including argument and return value marshalling.
137   ///
138   /// \p Callee is the destination of the call. It should be either a register,
139   /// globaladdress, or externalsymbol.
140   ///
141   /// \p ResTy is the type returned by the function
142   ///
143   /// \p ResReg is the generic virtual register that the returned
144   /// value should be lowered into.
145   ///
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.
148   ///
149   /// \p ArgRegs is a list of virtual registers containing each argument that
150   /// needs to be passed.
151   ///
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 {
156     return false;
157   }
158
159   /// This hook must be implemented to lower the given call instruction,
160   /// including argument and return value marshalling.
161   ///
162   /// \p ResReg is a register where the call's return value should be stored (or
163   /// 0 if there is no return value).
164   ///
165   /// \p ArgRegs is a list of virtual registers containing each argument that
166   /// needs to be passed.
167   ///
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.
172   ///
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;
177 };
178 } // End namespace llvm.
179
180 #endif