LLVM  7.0.0
BPFTargetMachine.cpp
Go to the documentation of this file.
1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
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 // Implements the info about BPF target spec.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BPFTargetMachine.h"
15 #include "BPF.h"
17 #include "llvm/CodeGen/Passes.h"
24 using namespace llvm;
25 
26 static cl::
27 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
28  cl::desc("Disable machine peepholes for BPF"));
29 
30 extern "C" void LLVMInitializeBPFTarget() {
31  // Register the target.
35 
38 }
39 
40 // DataLayout: little or big endian
41 static std::string computeDataLayout(const Triple &TT) {
42  if (TT.getArch() == Triple::bpfeb)
43  return "E-m:e-p:64:64-i64:64-n32:64-S128";
44  else
45  return "e-m:e-p:64:64-i64:64-n32:64-S128";
46 }
47 
49  if (!RM.hasValue())
50  return Reloc::PIC_;
51  return *RM;
52 }
53 
55  if (CM)
56  return *CM;
57  return CodeModel::Small;
58 }
59 
61  StringRef CPU, StringRef FS,
62  const TargetOptions &Options,
65  CodeGenOpt::Level OL, bool JIT)
66  : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
68  OL),
70  Subtarget(TT, CPU, FS, *this) {
71  initAsmInfo();
72 
73  BPFMCAsmInfo *MAI = static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo));
75 }
76 namespace {
77 // BPF Code Generator Pass Configuration Options.
78 class BPFPassConfig : public TargetPassConfig {
79 public:
80  BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
81  : TargetPassConfig(TM, PM) {}
82 
83  BPFTargetMachine &getBPFTargetMachine() const {
84  return getTM<BPFTargetMachine>();
85  }
86 
87  bool addInstSelector() override;
88  void addMachineSSAOptimization() override;
89  void addPreEmitPass() override;
90 };
91 }
92 
94  return new BPFPassConfig(*this, PM);
95 }
96 
97 // Install an instruction selector pass using
98 // the ISelDag to gen BPF code.
99 bool BPFPassConfig::addInstSelector() {
100  addPass(createBPFISelDag(getBPFTargetMachine()));
101 
102  return false;
103 }
104 
105 void BPFPassConfig::addMachineSSAOptimization() {
106  // The default implementation must be called first as we want eBPF
107  // Peephole ran at last.
109 
110  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
111  if (Subtarget->getHasAlu32() && !DisableMIPeephole)
112  addPass(createBPFMIPeepholePass());
113 }
114 
115 void BPFPassConfig::addPreEmitPass() {
116  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
117 
118  if (getOptLevel() != CodeGenOpt::None)
119  if (Subtarget->getHasAlu32() && !DisableMIPeephole)
121 }
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializeBPFMIPeepholePass(PassRegistry &)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
FunctionPass * createBPFMIPreEmitPeepholePass()
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:1069
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:48
Target-Independent Code Generator Pass Configuration Options.
const MCAsmInfo * AsmInfo
Contains target specific asm information.
Definition: TargetMachine.h:87
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:285
static cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
static CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool getUseDwarfRIS() const
Definition: BPFSubtarget.h:70
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
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:44
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:183
bool getHasAlu32() const
Definition: BPFSubtarget.h:69
void LLVMInitializeBPFTarget()
static std::string computeDataLayout(const Triple &TT)
Target & getTheBPFleTarget()
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39
Target & getTheBPFbeTarget()
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
Target & getTheBPFTarget()