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/CodeGen/MachineFunction.h"
20 #include "llvm/IR/IntrinsicsBPF.h"
21 #include "llvm/Support/Debug.h"
22 
23 #define DEBUG_TYPE "bpf-gisel"
24 
25 using namespace llvm;
26 
27 namespace {
28 
29 #define GET_GLOBALISEL_PREDICATE_BITSET
30 #include "BPFGenGlobalISel.inc"
31 #undef GET_GLOBALISEL_PREDICATE_BITSET
32 
33 class BPFInstructionSelector : public InstructionSelector {
34 public:
35   BPFInstructionSelector(const BPFTargetMachine &TM, const BPFSubtarget &STI,
36                          const BPFRegisterBankInfo &RBI);
37 
38   bool select(MachineInstr &I) override;
getName()39   static const char *getName() { return DEBUG_TYPE; }
40 
41 private:
42   /// tblgen generated 'select' implementation that is used as the initial
43   /// selector for the patterns that do not require complex C++.
44   bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
45 
46   const BPFInstrInfo &TII;
47   const BPFRegisterInfo &TRI;
48   const BPFRegisterBankInfo &RBI;
49 
50 #define GET_GLOBALISEL_PREDICATES_DECL
51 #include "BPFGenGlobalISel.inc"
52 #undef GET_GLOBALISEL_PREDICATES_DECL
53 
54 #define GET_GLOBALISEL_TEMPORARIES_DECL
55 #include "BPFGenGlobalISel.inc"
56 #undef GET_GLOBALISEL_TEMPORARIES_DECL
57 };
58 
59 } // namespace
60 
61 #define GET_GLOBALISEL_IMPL
62 #include "BPFGenGlobalISel.inc"
63 #undef GET_GLOBALISEL_IMPL
64 
BPFInstructionSelector(const BPFTargetMachine & TM,const BPFSubtarget & STI,const BPFRegisterBankInfo & RBI)65 BPFInstructionSelector::BPFInstructionSelector(const BPFTargetMachine &TM,
66                                                const BPFSubtarget &STI,
67                                                const BPFRegisterBankInfo &RBI)
68     : TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI),
69 #define GET_GLOBALISEL_PREDICATES_INIT
70 #include "BPFGenGlobalISel.inc"
71 #undef GET_GLOBALISEL_PREDICATES_INIT
72 #define GET_GLOBALISEL_TEMPORARIES_INIT
73 #include "BPFGenGlobalISel.inc"
74 #undef GET_GLOBALISEL_TEMPORARIES_INIT
75 {
76 }
77 
select(MachineInstr & I)78 bool BPFInstructionSelector::select(MachineInstr &I) {
79   if (!isPreISelGenericOpcode(I.getOpcode()))
80     return true;
81   if (selectImpl(I, *CoverageInfo))
82     return true;
83   return false;
84 }
85 
86 namespace llvm {
87 InstructionSelector *
createBPFInstructionSelector(const BPFTargetMachine & TM,const BPFSubtarget & Subtarget,const BPFRegisterBankInfo & RBI)88 createBPFInstructionSelector(const BPFTargetMachine &TM,
89                              const BPFSubtarget &Subtarget,
90                              const BPFRegisterBankInfo &RBI) {
91   return new BPFInstructionSelector(TM, Subtarget, RBI);
92 }
93 } // namespace llvm
94