1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the info about BPF target spec.
12 //===----------------------------------------------------------------------===//
15 #include "BPFTargetMachine.h"
16 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17 #include "llvm/IR/LegacyPassManager.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/FormattedStream.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Target/TargetOptions.h"
24 extern "C" void LLVMInitializeBPFTarget() {
25 // Register the target.
26 RegisterTargetMachine<BPFTargetMachine> X(TheBPFleTarget);
27 RegisterTargetMachine<BPFTargetMachine> Y(TheBPFbeTarget);
28 RegisterTargetMachine<BPFTargetMachine> Z(TheBPFTarget);
31 // DataLayout: little or big endian
32 static std::string computeDataLayout(const Triple &TT) {
33 if (TT.getArch() == Triple::bpfeb)
34 return "E-m:e-p:64:64-i64:64-n32:64-S128";
36 return "e-m:e-p:64:64-i64:64-n32:64-S128";
39 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
40 StringRef CPU, StringRef FS,
41 const TargetOptions &Options,
42 Reloc::Model RM, CodeModel::Model CM,
44 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, RM, CM,
46 TLOF(make_unique<TargetLoweringObjectFileELF>()),
47 Subtarget(TT, CPU, FS, *this) {
51 // BPF Code Generator Pass Configuration Options.
52 class BPFPassConfig : public TargetPassConfig {
54 BPFPassConfig(BPFTargetMachine *TM, PassManagerBase &PM)
55 : TargetPassConfig(TM, PM) {}
57 BPFTargetMachine &getBPFTargetMachine() const {
58 return getTM<BPFTargetMachine>();
61 bool addInstSelector() override;
65 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
66 return new BPFPassConfig(this, PM);
69 // Install an instruction selector pass using
70 // the ISelDag to gen BPF code.
71 bool BPFPassConfig::addInstSelector() {
72 addPass(createBPFISelDag(getBPFTargetMachine()));