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