LLVM  9.0.0svn
BPFTargetMachine.cpp
Go to the documentation of this file.
1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
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 //
9 // Implements the info about BPF target spec.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "BPFTargetMachine.h"
14 #include "BPF.h"
16 #include "llvm/CodeGen/Passes.h"
23 using namespace llvm;
24 
25 static cl::
26 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
27  cl::desc("Disable machine peepholes for BPF"));
28 
29 extern "C" void LLVMInitializeBPFTarget() {
30  // Register the target.
34 
37 }
38 
39 // DataLayout: little or big endian
40 static std::string computeDataLayout(const Triple &TT) {
41  if (TT.getArch() == Triple::bpfeb)
42  return "E-m:e-p:64:64-i64:64-n32:64-S128";
43  else
44  return "e-m:e-p:64:64-i64:64-n32:64-S128";
45 }
46 
48  if (!RM.hasValue())
49  return Reloc::PIC_;
50  return *RM;
51 }
52 
54  StringRef CPU, StringRef FS,
55  const TargetOptions &Options,
58  CodeGenOpt::Level OL, bool JIT)
59  : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
61  getEffectiveCodeModel(CM, CodeModel::Small), OL),
63  Subtarget(TT, CPU, FS, *this) {
64  initAsmInfo();
65 
66  BPFMCAsmInfo *MAI =
67  static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
69 }
70 namespace {
71 // BPF Code Generator Pass Configuration Options.
72 class BPFPassConfig : public TargetPassConfig {
73 public:
74  BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
75  : TargetPassConfig(TM, PM) {}
76 
77  BPFTargetMachine &getBPFTargetMachine() const {
78  return getTM<BPFTargetMachine>();
79  }
80 
81  bool addInstSelector() override;
82  void addMachineSSAOptimization() override;
83  void addPreEmitPass() override;
84 };
85 }
86 
88  return new BPFPassConfig(*this, PM);
89 }
90 
91 // Install an instruction selector pass using
92 // the ISelDag to gen BPF code.
93 bool BPFPassConfig::addInstSelector() {
94  addPass(createBPFISelDag(getBPFTargetMachine()));
95 
96  return false;
97 }
98 
99 void BPFPassConfig::addMachineSSAOptimization() {
100  // The default implementation must be called first as we want eBPF
101  // Peephole ran at last.
103 
104  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
105  if (Subtarget->getHasAlu32() && !DisableMIPeephole)
106  addPass(createBPFMIPeepholePass());
107 }
108 
109 void BPFPassConfig::addPreEmitPass() {
110  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
111 
113  if (getOptLevel() != CodeGenOpt::None)
114  if (Subtarget->getHasAlu32() && !DisableMIPeephole)
116 }
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializeBPFMIPeepholePass(PassRegistry &)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FunctionPass * createBPFMIPreEmitPeepholePass()
FunctionPass * createBPFMIPreEmitCheckingPass()
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1348
virtual void addMachineSSAOptimization()
addMachineSSAOptimization - Add standard passes that optimize machine instructions in SSA form...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
void setDwarfUsesRelocationsAcrossSections(bool enable)
Definition: BPFMCAsmInfo.h:47
Target-Independent Code Generator Pass Configuration Options.
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:289
static cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:55
bool getUseDwarfRIS() const
Definition: BPFSubtarget.h:74
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
std::unique_ptr< const MCAsmInfo > AsmInfo
Contains target specific asm information.
Definition: TargetMachine.h:86
This class describes a target machine that is implemented with the LLVM target-independent code gener...
FunctionPass * createBPFMIPeepholePass()
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
BPFTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Target - Wrapper for Target specific information.
bool hasValue() const
Definition: Optional.h:166
bool getHasAlu32() const
Definition: BPFSubtarget.h:73
void LLVMInitializeBPFTarget()
static std::string computeDataLayout(const Triple &TT)
Target & getTheBPFleTarget()
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
Target & getTheBPFbeTarget()
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
Target & getTheBPFTarget()