LLVM  5.0.0
InstructionSelector.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/InstructionSelector.h ------------*- 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 This file declares the API for the instruction selector.
11 /// This class is responsible for selecting machine instructions.
12 /// It's implemented by the target. It's used by the InstructionSelect pass.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H
17 #define LLVM_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H
18 
19 #include "llvm/ADT/SmallVector.h"
20 #include <bitset>
21 #include <cstddef>
22 #include <cstdint>
23 #include <functional>
24 #include <initializer_list>
25 #include <vector>
26 
27 namespace llvm {
28 
29 class LLT;
30 class MachineInstr;
31 class MachineInstrBuilder;
32 class MachineOperand;
33 class MachineRegisterInfo;
34 class RegisterBankInfo;
35 class TargetInstrInfo;
36 class TargetRegisterClass;
37 class TargetRegisterInfo;
38 
39 /// Container class for CodeGen predicate results.
40 /// This is convenient because std::bitset does not have a constructor
41 /// with an initializer list of set bits.
42 ///
43 /// Each InstructionSelector subclass should define a PredicateBitset class
44 /// with:
45 /// const unsigned MAX_SUBTARGET_PREDICATES = 192;
46 /// using PredicateBitset = PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;
47 /// and updating the constant to suit the target. Tablegen provides a suitable
48 /// definition for the predicates in use in <Target>GenGlobalISel.inc when
49 /// GET_GLOBALISEL_PREDICATE_BITSET is defined.
50 template <std::size_t MaxPredicates>
51 class PredicateBitsetImpl : public std::bitset<MaxPredicates> {
52 public:
53  // Cannot inherit constructors because it's not supported by VC++..
54  PredicateBitsetImpl() = default;
55 
56  PredicateBitsetImpl(const std::bitset<MaxPredicates> &B)
57  : std::bitset<MaxPredicates>(B) {}
58 
59  PredicateBitsetImpl(std::initializer_list<unsigned> Init) {
60  for (auto I : Init)
61  std::bitset<MaxPredicates>::set(I);
62  }
63 };
64 
65 enum {
66  /// Record the specified instruction
67  /// - NewInsnID - Instruction ID to define
68  /// - InsnID - Instruction ID
69  /// - OpIdx - Operand index
71 
72  /// Check the feature bits
73  /// - Expected features
75 
76  /// Check the opcode on the specified instruction
77  /// - InsnID - Instruction ID
78  /// - Expected opcode
80  /// Check the instruction has the right number of operands
81  /// - InsnID - Instruction ID
82  /// - Expected number of operands
84 
85  /// Check the type for the specified operand
86  /// - InsnID - Instruction ID
87  /// - OpIdx - Operand index
88  /// - Expected type
90  /// Check the register bank for the specified operand
91  /// - InsnID - Instruction ID
92  /// - OpIdx - Operand index
93  /// - Expected register bank (specified as a register class)
95  /// Check the operand matches a complex predicate
96  /// - InsnID - Instruction ID
97  /// - OpIdx - Operand index
98  /// - RendererID - The renderer to hold the result
99  /// - Complex predicate ID
101  /// Check the operand is a specific integer
102  /// - InsnID - Instruction ID
103  /// - OpIdx - Operand index
104  /// - Expected integer
106  /// Check the operand is a specific literal integer (i.e. MO.isImm() or
107  /// MO.isCImm() is true).
108  /// - InsnID - Instruction ID
109  /// - OpIdx - Operand index
110  /// - Expected integer
112  /// Check the operand is a specific intrinsic ID
113  /// - InsnID - Instruction ID
114  /// - OpIdx - Operand index
115  /// - Expected Intrinsic ID
117  /// Check the specified operand is an MBB
118  /// - InsnID - Instruction ID
119  /// - OpIdx - Operand index
121 
122  /// Check if the specified operand is safe to fold into the current
123  /// instruction.
124  /// - InsnID - Instruction ID
126 
127  //=== Renderers ===
128 
129  /// Mutate an instruction
130  /// - NewInsnID - Instruction ID to define
131  /// - OldInsnID - Instruction ID to mutate
132  /// - NewOpcode - The new opcode to use
134  /// Build a new instruction
135  /// - InsnID - Instruction ID to define
136  /// - Opcode - The new opcode to use
138 
139  /// Copy an operand to the specified instruction
140  /// - NewInsnID - Instruction ID to modify
141  /// - OldInsnID - Instruction ID to copy from
142  /// - OpIdx - The operand to copy
144  /// Copy an operand to the specified instruction
145  /// - NewInsnID - Instruction ID to modify
146  /// - OldInsnID - Instruction ID to copy from
147  /// - OpIdx - The operand to copy
148  /// - SubRegIdx - The subregister to copy
150  /// Add an implicit register def to the specified instruction
151  /// - InsnID - Instruction ID to modify
152  /// - RegNum - The register to add
154  /// Add an implicit register use to the specified instruction
155  /// - InsnID - Instruction ID to modify
156  /// - RegNum - The register to add
158  /// Add an register to the specified instruction
159  /// - InsnID - Instruction ID to modify
160  /// - RegNum - The register to add
162  /// Add an immediate to the specified instruction
163  /// - InsnID - Instruction ID to modify
164  /// - Imm - The immediate to add
166  /// Render complex operands to the specified instruction
167  /// - InsnID - Instruction ID to modify
168  /// - RendererID - The renderer to call
170 
171  /// Constrain an instruction operand to a register class.
172  /// - InsnID - Instruction ID to modify
173  /// - OpIdx - Operand index
174  /// - RCEnum - Register class enumeration value
176  /// Constrain an instructions operands according to the instruction
177  /// description.
178  /// - InsnID - Instruction ID to modify
180  /// Merge all memory operands into instruction.
181  /// - InsnID - Instruction ID to modify
183  /// Erase from parent.
184  /// - InsnID - Instruction ID to erase
186 
187  /// A successful emission
189 };
190 
191 /// Provides the logic to select generic machine instructions.
193 public:
194  virtual ~InstructionSelector() = default;
195 
196  /// Select the (possibly generic) instruction \p I to only use target-specific
197  /// opcodes. It is OK to insert multiple instructions, but they cannot be
198  /// generic pre-isel instructions.
199  ///
200  /// \returns whether selection succeeded.
201  /// \pre I.getParent() && I.getParent()->getParent()
202  /// \post
203  /// if returns true:
204  /// for I in all mutated/inserted instructions:
205  /// !isPreISelGenericOpcode(I.getOpcode())
206  ///
207  virtual bool select(MachineInstr &I) const = 0;
208 
209 protected:
210  using ComplexRendererFn = std::function<void(MachineInstrBuilder &)>;
213 
214  struct MatcherState {
215  std::vector<ComplexRendererFn> Renderers;
217 
218  MatcherState(unsigned MaxRenderers);
219  };
220 
221 public:
222  template <class PredicateBitset, class ComplexMatcherMemFn>
223  struct MatcherInfoTy {
224  const LLT *TypeObjects;
225  const PredicateBitset *FeatureBitsets;
226  const std::vector<ComplexMatcherMemFn> ComplexPredicates;
227  };
228 
229 protected:
231 
232  /// Execute a given matcher table and return true if the match was successful
233  /// and false otherwise.
234  template <class TgtInstructionSelector, class PredicateBitset,
235  class ComplexMatcherMemFn>
236  bool executeMatchTable(
237  TgtInstructionSelector &ISel, NewMIVector &OutMIs, MatcherState &State,
239  const int64_t *MatchTable, const TargetInstrInfo &TII,
241  const RegisterBankInfo &RBI,
242  const PredicateBitset &AvailableFeatures) const;
243 
244  /// Constrain a register operand of an instruction \p I to a specified
245  /// register class. This could involve inserting COPYs before (for uses) or
246  /// after (for defs) and may replace the operand of \p I.
247  /// \returns whether operand regclass constraining succeeded.
248  bool constrainOperandRegToRegClass(MachineInstr &I, unsigned OpIdx,
249  const TargetRegisterClass &RC,
250  const TargetInstrInfo &TII,
251  const TargetRegisterInfo &TRI,
252  const RegisterBankInfo &RBI) const;
253 
254  /// Mutate the newly-selected instruction \p I to constrain its (possibly
255  /// generic) virtual register operands to the instruction's register class.
256  /// This could involve inserting COPYs before (for uses) or after (for defs).
257  /// This requires the number of operands to match the instruction description.
258  /// \returns whether operand regclass constraining succeeded.
259  ///
260  // FIXME: Not all instructions have the same number of operands. We should
261  // probably expose a constrain helper per operand and let the target selector
262  // constrain individual registers, like fast-isel.
264  const TargetInstrInfo &TII,
265  const TargetRegisterInfo &TRI,
266  const RegisterBankInfo &RBI) const;
267 
268  bool isOperandImmEqual(const MachineOperand &MO, int64_t Value,
269  const MachineRegisterInfo &MRI) const;
270 
272 };
273 
274 } // end namespace llvm
275 
276 #endif // LLVM_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H
virtual ~InstructionSelector()=default
Check the register bank for the specified operand.
const std::vector< ComplexMatcherMemFn > ComplexPredicates
Merge all memory operands into instruction.
bool constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Add an immediate to the specified instruction.
Copy an operand to the specified instruction.
Add an implicit register def to the specified instruction.
PredicateBitsetImpl(std::initializer_list< unsigned > Init)
Holds all the information related to register banks.
const HexagonInstrInfo * TII
Check the operand is a specific intrinsic ID.
Constrain an instructions operands according to the instruction description.
Check the instruction has the right number of operands.
Check the operand matches a complex predicate.
std::function< void(MachineInstrBuilder &)> ComplexRendererFn
Mutate an instruction.
TargetInstrInfo - Interface to description of machine instruction set.
Container class for CodeGen predicate results.
unsigned const MachineRegisterInfo * MRI
Record the specified instruction.
bool executeMatchTable(TgtInstructionSelector &ISel, NewMIVector &OutMIs, MatcherState &State, const MatcherInfoTy< PredicateBitset, ComplexMatcherMemFn > &MatcherInfo, const int64_t *MatchTable, const TargetInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI, const PredicateBitset &AvailableFeatures) const
Execute a given matcher table and return true if the match was successful and false otherwise...
std::vector< ComplexRendererFn > Renderers
bool isOperandImmEqual(const MachineOperand &MO, int64_t Value, const MachineRegisterInfo &MRI) const
Check the feature bits.
Check if the specified operand is safe to fold into the current instruction.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define B
Definition: LargeTest.cpp:24
virtual bool select(MachineInstr &I) const =0
Select the (possibly generic) instruction I to only use target-specific opcodes.
MachineOperand class - Representation of each machine instruction operand.
Check the specified operand is an MBB.
RegisterBankInfo(RegisterBank **RegBanks, unsigned NumRegBanks)
Create a RegisterBankInfo that can accommodate up to NumRegBanks RegisterBank instances.
Render complex operands to the specified instruction.
bool isObviouslySafeToFold(MachineInstr &MI) const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides the logic to select generic machine instructions.
Representation of each machine instruction.
Definition: MachineInstr.h:59
Check the operand is a specific integer.
Build a new instruction.
#define I(x, y, z)
Definition: MD5.cpp:58
Constrain an instruction operand to a register class.
Add an implicit register use to the specified instruction.
Check the opcode on the specified instruction.
Check the operand is a specific literal integer (i.e.
LLVM Value Representation.
Definition: Value.h:73
bool constrainOperandRegToRegClass(MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
Constrain a register operand of an instruction I to a specified register class.
IRTranslator LLVM IR MI
PredicateBitsetImpl(const std::bitset< MaxPredicates > &B)
A successful emission.
Copy an operand to the specified instruction.
Add an register to the specified instruction.
Check the type for the specified operand.