LLVM  5.0.0
InstructionSelectorImpl.h
Go to the documentation of this file.
1 //==-- llvm/CodeGen/GlobalISel/InstructionSelectorImpl.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_INSTRUCTIONSELECTORIMPL_H
17 #define LLVM_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTORIMPL_H
18 
19 namespace llvm {
20 template <class TgtInstructionSelector, class PredicateBitset,
21  class ComplexMatcherMemFn>
23  TgtInstructionSelector &ISel, NewMIVector &OutMIs, MatcherState &State,
25  const int64_t *MatchTable, const TargetInstrInfo &TII,
27  const RegisterBankInfo &RBI,
28  const PredicateBitset &AvailableFeatures) const {
29  const int64_t *Command = MatchTable;
30  while (true) {
31  switch (*Command++) {
32  case GIM_RecordInsn: {
33  int64_t NewInsnID = *Command++;
34  int64_t InsnID = *Command++;
35  int64_t OpIdx = *Command++;
36 
37  // As an optimisation we require that MIs[0] is always the root. Refuse
38  // any attempt to modify it.
39  assert(NewInsnID != 0 && "Refusing to modify MIs[0]");
40  (void)NewInsnID;
41 
42  MachineOperand &MO = State.MIs[InsnID]->getOperand(OpIdx);
43  if (!MO.isReg()) {
44  DEBUG(dbgs() << "Rejected (not a register)\n");
45  return false;
46  }
47  if (TRI.isPhysicalRegister(MO.getReg())) {
48  DEBUG(dbgs() << "Rejected (is a physical register)\n");
49  return false;
50  }
51 
52  assert((size_t)NewInsnID == State.MIs.size() &&
53  "Expected to store MIs in order");
54  State.MIs.push_back(MRI.getVRegDef(MO.getReg()));
55  DEBUG(dbgs() << "MIs[" << NewInsnID << "] = GIM_RecordInsn(" << InsnID
56  << ", " << OpIdx << ")\n");
57  break;
58  }
59 
60  case GIM_CheckFeatures: {
61  int64_t ExpectedBitsetID = *Command++;
62  DEBUG(dbgs() << "GIM_CheckFeatures(ExpectedBitsetID=" << ExpectedBitsetID
63  << ")\n");
64  if ((AvailableFeatures & MatcherInfo.FeatureBitsets[ExpectedBitsetID]) !=
65  MatcherInfo.FeatureBitsets[ExpectedBitsetID]) {
66  DEBUG(dbgs() << "Rejected\n");
67  return false;
68  }
69  break;
70  }
71 
72  case GIM_CheckOpcode: {
73  int64_t InsnID = *Command++;
74  int64_t Expected = *Command++;
75 
76  unsigned Opcode = State.MIs[InsnID]->getOpcode();
77  DEBUG(dbgs() << "GIM_CheckOpcode(MIs[" << InsnID << "], ExpectedOpcode="
78  << Expected << ") // Got=" << Opcode << "\n");
79  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
80  if (Opcode != Expected)
81  return false;
82  break;
83  }
84  case GIM_CheckNumOperands: {
85  int64_t InsnID = *Command++;
86  int64_t Expected = *Command++;
87  DEBUG(dbgs() << "GIM_CheckNumOperands(MIs[" << InsnID
88  << "], Expected=" << Expected << ")\n");
89  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
90  if (State.MIs[InsnID]->getNumOperands() != Expected)
91  return false;
92  break;
93  }
94 
95  case GIM_CheckType: {
96  int64_t InsnID = *Command++;
97  int64_t OpIdx = *Command++;
98  int64_t TypeID = *Command++;
99  DEBUG(dbgs() << "GIM_CheckType(MIs[" << InsnID << "]->getOperand("
100  << OpIdx << "), TypeID=" << TypeID << ")\n");
101  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
102  if (MRI.getType(State.MIs[InsnID]->getOperand(OpIdx).getReg()) !=
103  MatcherInfo.TypeObjects[TypeID])
104  return false;
105  break;
106  }
108  int64_t InsnID = *Command++;
109  int64_t OpIdx = *Command++;
110  int64_t RCEnum = *Command++;
111  DEBUG(dbgs() << "GIM_CheckRegBankForClass(MIs[" << InsnID
112  << "]->getOperand(" << OpIdx << "), RCEnum=" << RCEnum
113  << ")\n");
114  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
115  if (&RBI.getRegBankFromRegClass(*TRI.getRegClass(RCEnum)) !=
116  RBI.getRegBank(State.MIs[InsnID]->getOperand(OpIdx).getReg(), MRI, TRI))
117  return false;
118  break;
119  }
121  int64_t InsnID = *Command++;
122  int64_t OpIdx = *Command++;
123  int64_t RendererID = *Command++;
124  int64_t ComplexPredicateID = *Command++;
125  DEBUG(dbgs() << "State.Renderers[" << RendererID
126  << "] = GIM_CheckComplexPattern(MIs[" << InsnID
127  << "]->getOperand(" << OpIdx
128  << "), ComplexPredicateID=" << ComplexPredicateID << ")\n");
129  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
130  // FIXME: Use std::invoke() when it's available.
131  if (!(State.Renderers[RendererID] =
132  (ISel.*MatcherInfo.ComplexPredicates[ComplexPredicateID])(
133  State.MIs[InsnID]->getOperand(OpIdx))))
134  return false;
135  break;
136  }
137  case GIM_CheckConstantInt: {
138  int64_t InsnID = *Command++;
139  int64_t OpIdx = *Command++;
140  int64_t Value = *Command++;
141  DEBUG(dbgs() << "GIM_CheckConstantInt(MIs[" << InsnID << "]->getOperand("
142  << OpIdx << "), Value=" << Value << ")\n");
143  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
144  if (!isOperandImmEqual(State.MIs[InsnID]->getOperand(OpIdx), Value, MRI))
145  return false;
146  break;
147  }
148  case GIM_CheckLiteralInt: {
149  int64_t InsnID = *Command++;
150  int64_t OpIdx = *Command++;
151  int64_t Value = *Command++;
152  DEBUG(dbgs() << "GIM_CheckLiteralInt(MIs[" << InsnID << "]->getOperand(" << OpIdx
153  << "), Value=" << Value << ")\n");
154  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
155  MachineOperand &OM = State.MIs[InsnID]->getOperand(OpIdx);
156  if (!OM.isCImm() || !OM.getCImm()->equalsInt(Value))
157  return false;
158  break;
159  }
160  case GIM_CheckIntrinsicID: {
161  int64_t InsnID = *Command++;
162  int64_t OpIdx = *Command++;
163  int64_t Value = *Command++;
164  DEBUG(dbgs() << "GIM_CheckIntrinsicID(MIs[" << InsnID << "]->getOperand(" << OpIdx
165  << "), Value=" << Value << ")\n");
166  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
167  MachineOperand &OM = State.MIs[InsnID]->getOperand(OpIdx);
168  if (!OM.isIntrinsicID() || OM.getIntrinsicID() != Value)
169  return false;
170  break;
171  }
172  case GIM_CheckIsMBB: {
173  int64_t InsnID = *Command++;
174  int64_t OpIdx = *Command++;
175  DEBUG(dbgs() << "GIM_CheckIsMBB(MIs[" << InsnID << "]->getOperand("
176  << OpIdx << "))\n");
177  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
178  if (!State.MIs[InsnID]->getOperand(OpIdx).isMBB())
179  return false;
180  break;
181  }
182 
183  case GIM_CheckIsSafeToFold: {
184  int64_t InsnID = *Command++;
185  DEBUG(dbgs() << "GIM_CheckIsSafeToFold(MIs[" << InsnID << "])\n");
186  assert(State.MIs[InsnID] != nullptr && "Used insn before defined");
187  if (!isObviouslySafeToFold(*State.MIs[InsnID]))
188  return false;
189  break;
190  }
191 
192  case GIR_MutateOpcode: {
193  int64_t OldInsnID = *Command++;
194  int64_t NewInsnID = *Command++;
195  int64_t NewOpcode = *Command++;
196  assert((size_t)NewInsnID == OutMIs.size() &&
197  "Expected to store MIs in order");
198  OutMIs.push_back(
199  MachineInstrBuilder(*State.MIs[OldInsnID]->getParent()->getParent(),
200  State.MIs[OldInsnID]));
201  OutMIs[NewInsnID]->setDesc(TII.get(NewOpcode));
202  DEBUG(dbgs() << "GIR_MutateOpcode(OutMIs[" << NewInsnID << "], MIs["
203  << OldInsnID << "], " << NewOpcode << ")\n");
204  break;
205  }
206  case GIR_BuildMI: {
207  int64_t InsnID = *Command++;
208  int64_t Opcode = *Command++;
209  assert((size_t)InsnID == OutMIs.size() &&
210  "Expected to store MIs in order");
211  (void)InsnID;
212  OutMIs.push_back(BuildMI(*State.MIs[0]->getParent(), State.MIs[0],
213  State.MIs[0]->getDebugLoc(), TII.get(Opcode)));
214  DEBUG(dbgs() << "GIR_BuildMI(OutMIs[" << InsnID << "], " << Opcode
215  << ")\n");
216  break;
217  }
218 
219  case GIR_Copy: {
220  int64_t NewInsnID = *Command++;
221  int64_t OldInsnID = *Command++;
222  int64_t OpIdx = *Command++;
223  assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction");
224  OutMIs[NewInsnID].add(State.MIs[OldInsnID]->getOperand(OpIdx));
225  DEBUG(dbgs() << "GIR_Copy(OutMIs[" << NewInsnID << "], MIs[" << OldInsnID
226  << "], " << OpIdx << ")\n");
227  break;
228  }
229  case GIR_CopySubReg: {
230  int64_t NewInsnID = *Command++;
231  int64_t OldInsnID = *Command++;
232  int64_t OpIdx = *Command++;
233  int64_t SubRegIdx = *Command++;
234  assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction");
235  OutMIs[NewInsnID].addReg(State.MIs[OldInsnID]->getOperand(OpIdx).getReg(),
236  0, SubRegIdx);
237  DEBUG(dbgs() << "GIR_CopySubReg(OutMIs[" << NewInsnID << "], MIs["
238  << OldInsnID << "], " << OpIdx << ", " << SubRegIdx
239  << ")\n");
240  break;
241  }
242  case GIR_AddImplicitDef: {
243  int64_t InsnID = *Command++;
244  int64_t RegNum = *Command++;
245  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
246  OutMIs[InsnID].addDef(RegNum, RegState::Implicit);
247  DEBUG(dbgs() << "GIR_AddImplicitDef(OutMIs[" << InsnID << "], " << RegNum
248  << ")\n");
249  break;
250  }
251  case GIR_AddImplicitUse: {
252  int64_t InsnID = *Command++;
253  int64_t RegNum = *Command++;
254  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
255  OutMIs[InsnID].addUse(RegNum, RegState::Implicit);
256  DEBUG(dbgs() << "GIR_AddImplicitUse(OutMIs[" << InsnID << "], " << RegNum
257  << ")\n");
258  break;
259  }
260  case GIR_AddRegister: {
261  int64_t InsnID = *Command++;
262  int64_t RegNum = *Command++;
263  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
264  OutMIs[InsnID].addReg(RegNum);
265  DEBUG(dbgs() << "GIR_AddRegister(OutMIs[" << InsnID << "], " << RegNum
266  << ")\n");
267  break;
268  }
269  case GIR_AddImm: {
270  int64_t InsnID = *Command++;
271  int64_t Imm = *Command++;
272  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
273  OutMIs[InsnID].addImm(Imm);
274  DEBUG(dbgs() << "GIR_AddImm(OutMIs[" << InsnID << "], " << Imm << ")\n");
275  break;
276  }
277  case GIR_ComplexRenderer: {
278  int64_t InsnID = *Command++;
279  int64_t RendererID = *Command++;
280  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
281  State.Renderers[RendererID](OutMIs[InsnID]);
282  DEBUG(dbgs() << "GIR_ComplexRenderer(OutMIs[" << InsnID << "], "
283  << RendererID << ")\n");
284  break;
285  }
286 
287  case GIR_ConstrainOperandRC: {
288  int64_t InsnID = *Command++;
289  int64_t OpIdx = *Command++;
290  int64_t RCEnum = *Command++;
291  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
292  constrainOperandRegToRegClass(*OutMIs[InsnID].getInstr(), OpIdx,
293  *TRI.getRegClass(RCEnum), TII, TRI, RBI);
294  DEBUG(dbgs() << "GIR_ConstrainOperandRC(OutMIs[" << InsnID << "], "
295  << OpIdx << ", " << RCEnum << ")\n");
296  break;
297  }
299  int64_t InsnID = *Command++;
300  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
301  constrainSelectedInstRegOperands(*OutMIs[InsnID].getInstr(), TII, TRI,
302  RBI);
303  DEBUG(dbgs() << "GIR_ConstrainSelectedInstOperands(OutMIs[" << InsnID
304  << "])\n");
305  break;
306  }
307  case GIR_MergeMemOperands: {
308  int64_t InsnID = *Command++;
309  assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
310  for (const auto *FromMI : State.MIs)
311  for (const auto &MMO : FromMI->memoperands())
312  OutMIs[InsnID].addMemOperand(MMO);
313  DEBUG(dbgs() << "GIR_MergeMemOperands(OutMIs[" << InsnID << "])\n");
314  break;
315  }
316  case GIR_EraseFromParent: {
317  int64_t InsnID = *Command++;
318  assert(State.MIs[InsnID] &&
319  "Attempted to erase an undefined instruction");
320  State.MIs[InsnID]->eraseFromParent();
321  DEBUG(dbgs() << "GIR_EraseFromParent(MIs[" << InsnID << "])\n");
322  break;
323  }
324 
325  case GIR_Done:
326  DEBUG(dbgs() << "GIR_Done");
327  return true;
328 
329  default:
330  llvm_unreachable("Unexpected command");
331  }
332  }
333 }
334 
335 } // end namespace llvm
336 
337 #endif // LLVM_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTORIMPL_H
void push_back(const T &Elt)
Definition: SmallVector.h:211
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC) const
Get a register bank that covers RC.
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
Check the register bank for the specified operand.
const std::vector< ComplexMatcherMemFn > ComplexPredicates
Type::TypeID TypeID
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.
Holds all the information related to register banks.
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Check the operand is a specific intrinsic ID.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
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.
Mutate an instruction.
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool isIntrinsicID() const
Intrinsic::ID getIntrinsicID() const
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 isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
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...
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Check the specified operand is an MBB.
bool equalsInt(uint64_t V) const
A helper method that can be used to determine if the constant contained within is equal to a constant...
Definition: Constants.h:165
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const ConstantInt * getCImm() const
Render complex operands to the specified instruction.
bool isObviouslySafeToFold(MachineInstr &MI) const
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Check the operand is a specific integer.
Build a new instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
Constrain an instruction operand to a register class.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
unsigned getReg() const
getReg - Returns the register number.
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
#define DEBUG(X)
Definition: Debug.h:118
A successful emission.
Copy an operand to the specified instruction.
Add an register to the specified instruction.
Check the type for the specified operand.