LLVM  4.0.0
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/SmallVector.h"
21 #include "llvm/IR/Function.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;
38 
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 
74  : MIRBuilder(MIRBuilder), MRI(MRI) {}
75 
76  virtual ~ValueHandler() {}
77 
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
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, unsigned VReg) const
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
Definition: CallLowering.h:115
virtual ~CallLowering()
Definition: CallLowering.h:108
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
MachineRegisterInfo & MRI
Definition: CallLowering.h:79
This class represents a function call, abstracting a target machine's calling convention.
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...
static const MCPhysReg VRegs[32]
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
#define F(x, y, z)
Definition: MD5.cpp:51
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg, CCValAssign &VA)=0
The specified value has been assigned to a physical register, handle the appropriate COPY (either to ...
const XXXTargetLowering * getTLI() const
Getter for target specific TargetLowering class.
Definition: CallLowering.h:90
Helper class to build MachineInstr.
uint32_t Offset
CallLowering(const TargetLowering *TLI)
Definition: CallLowering.h:107
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:49
This class contains a discriminated union of information about pointers in memory operands...
MachineOperand class - Representation of each machine instruction operand.
CCValAssign - Represent assignment of one arg/retval to a location.
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, 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:153
ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Definition: CallLowering.h:73
ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags=ISD::ArgFlagsTy{})
Definition: CallLowering.h:39
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
virtual unsigned getStackAddress(uint64_t Size, int64_t Offset, MachinePointerInfo &MPO)=0
Materialize a VReg containing the address of the specified stack-based object.
virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, MachinePointerInfo &MPO, CCValAssign &VA)=0
The specified value has been assigned to a stack location.
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:129
void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
LLVM Value Representation.
Definition: Value.h:71
print Print MemDeps of function
unsigned extendRegister(unsigned ValReg, CCValAssign &VA)
bool handleAssignments(MachineIRBuilder &MIRBuilder, CCAssignFn *AssignFn, ArrayRef< ArgInfo > Args, ValueHandler &Callback) const
Invoke the AssignFn on each of the given Args and then use Callback to move them to the assigned loca...
MachineIRBuilder & MIRBuilder
Definition: CallLowering.h:78
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
Definition: CallLowering.h:84