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" 16 #include "llvm/CodeGen/Passes.h" 17 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 18 #include "llvm/CodeGen/TargetPassConfig.h" 19 #include "llvm/IR/LegacyPassManager.h" 20 #include "llvm/Support/FormattedStream.h" 21 #include "llvm/Support/TargetRegistry.h" 22 #include "llvm/Target/TargetOptions.h" 23 using namespace llvm; 24 25 extern "C" void LLVMInitializeBPFTarget() { 26 // Register the target. 27 RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget()); 28 RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget()); 29 RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget()); 30 } 31 32 // DataLayout: little or big endian 33 static std::string computeDataLayout(const Triple &TT) { 34 if (TT.getArch() == Triple::bpfeb) 35 return "E-m:e-p:64:64-i64:64-n32:64-S128"; 36 else 37 return "e-m:e-p:64:64-i64:64-n32:64-S128"; 38 } 39 40 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { 41 if (!RM.hasValue()) 42 return Reloc::PIC_; 43 return *RM; 44 } 45 46 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) { 47 if (CM) 48 return *CM; 49 return CodeModel::Small; 50 } 51 52 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT, 53 StringRef CPU, StringRef FS, 54 const TargetOptions &Options, 55 Optional<Reloc::Model> RM, 56 Optional<CodeModel::Model> CM, 57 CodeGenOpt::Level OL, bool JIT) 58 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, 59 getEffectiveRelocModel(RM), getEffectiveCodeModel(CM), 60 OL), 61 TLOF(make_unique<TargetLoweringObjectFileELF>()), 62 Subtarget(TT, CPU, FS, *this) { 63 initAsmInfo(); 64 } 65 namespace { 66 // BPF Code Generator Pass Configuration Options. 67 class BPFPassConfig : public TargetPassConfig { 68 public: 69 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM) 70 : TargetPassConfig(TM, PM) {} 71 72 BPFTargetMachine &getBPFTargetMachine() const { 73 return getTM<BPFTargetMachine>(); 74 } 75 76 bool addInstSelector() override; 77 }; 78 } 79 80 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) { 81 return new BPFPassConfig(*this, PM); 82 } 83 84 // Install an instruction selector pass using 85 // the ISelDag to gen BPF code. 86 bool BPFPassConfig::addInstSelector() { 87 addPass(createBPFISelDag(getBPFTargetMachine())); 88 89 return false; 90 } 91