xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/GISel/BPFInstructionSelector.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- BPFInstructionSelector.cpp --------------------------------*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements the targeting of the InstructionSelector class for BPF.
10 //===----------------------------------------------------------------------===//
11 
12 #include "BPFInstrInfo.h"
13 #include "BPFRegisterBankInfo.h"
14 #include "BPFSubtarget.h"
15 #include "BPFTargetMachine.h"
16 #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
17 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
18 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
19 #include "llvm/IR/IntrinsicsBPF.h"
20 
21 #define DEBUG_TYPE "bpf-gisel"
22 
23 using namespace llvm;
24 
25 namespace {
26 
27 #define GET_GLOBALISEL_PREDICATE_BITSET
28 #include "BPFGenGlobalISel.inc"
29 #undef GET_GLOBALISEL_PREDICATE_BITSET
30 
31 class BPFInstructionSelector : public InstructionSelector {
32 public:
33   BPFInstructionSelector(const BPFTargetMachine &TM, const BPFSubtarget &STI,
34                          const BPFRegisterBankInfo &RBI);
35 
36   bool select(MachineInstr &I) override;
getName()37   static const char *getName() { return DEBUG_TYPE; }
38 
39 private:
40   /// tblgen generated 'select' implementation that is used as the initial
41   /// selector for the patterns that do not require complex C++.
42   bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
43 
44   const BPFInstrInfo &TII;
45   const BPFRegisterInfo &TRI;
46   const BPFRegisterBankInfo &RBI;
47 
48 #define GET_GLOBALISEL_PREDICATES_DECL
49 #include "BPFGenGlobalISel.inc"
50 #undef GET_GLOBALISEL_PREDICATES_DECL
51 
52 #define GET_GLOBALISEL_TEMPORARIES_DECL
53 #include "BPFGenGlobalISel.inc"
54 #undef GET_GLOBALISEL_TEMPORARIES_DECL
55 };
56 
57 } // namespace
58 
59 #define GET_GLOBALISEL_IMPL
60 #include "BPFGenGlobalISel.inc"
61 #undef GET_GLOBALISEL_IMPL
62 
BPFInstructionSelector(const BPFTargetMachine & TM,const BPFSubtarget & STI,const BPFRegisterBankInfo & RBI)63 BPFInstructionSelector::BPFInstructionSelector(const BPFTargetMachine &TM,
64                                                const BPFSubtarget &STI,
65                                                const BPFRegisterBankInfo &RBI)
66     : TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI),
67 #define GET_GLOBALISEL_PREDICATES_INIT
68 #include "BPFGenGlobalISel.inc"
69 #undef GET_GLOBALISEL_PREDICATES_INIT
70 #define GET_GLOBALISEL_TEMPORARIES_INIT
71 #include "BPFGenGlobalISel.inc"
72 #undef GET_GLOBALISEL_TEMPORARIES_INIT
73 {
74 }
75 
select(MachineInstr & I)76 bool BPFInstructionSelector::select(MachineInstr &I) {
77   if (!isPreISelGenericOpcode(I.getOpcode()))
78     return true;
79   if (selectImpl(I, *CoverageInfo))
80     return true;
81   return false;
82 }
83 
84 namespace llvm {
85 InstructionSelector *
createBPFInstructionSelector(const BPFTargetMachine & TM,const BPFSubtarget & Subtarget,const BPFRegisterBankInfo & RBI)86 createBPFInstructionSelector(const BPFTargetMachine &TM,
87                              const BPFSubtarget &Subtarget,
88                              const BPFRegisterBankInfo &RBI) {
89   return new BPFInstructionSelector(TM, Subtarget, RBI);
90 }
91 } // namespace llvm
92