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" 15 #include "MCTargetDesc/BPFMCAsmInfo.h" 16 #include "TargetInfo/BPFTargetInfo.h" 17 #include "llvm/CodeGen/Passes.h" 18 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 19 #include "llvm/CodeGen/TargetPassConfig.h" 20 #include "llvm/IR/LegacyPassManager.h" 21 #include "llvm/Support/FormattedStream.h" 22 #include "llvm/Support/TargetRegistry.h" 23 #include "llvm/Target/TargetOptions.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" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() { 31 // Register the target. 32 RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget()); 33 RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget()); 34 RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget()); 35 36 PassRegistry &PR = *PassRegistry::getPassRegistry(); 37 initializeBPFAbstractMemberAccessPass(PR); 38 initializeBPFPreserveDITypePass(PR); 39 initializeBPFMIPeepholePass(PR); 40 initializeBPFMIPeepholeTruncElimPass(PR); 41 } 42 43 // DataLayout: little or big endian 44 static std::string computeDataLayout(const Triple &TT) { 45 if (TT.getArch() == Triple::bpfeb) 46 return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128"; 47 else 48 return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"; 49 } 50 51 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { 52 if (!RM.hasValue()) 53 return Reloc::PIC_; 54 return *RM; 55 } 56 57 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT, 58 StringRef CPU, StringRef FS, 59 const TargetOptions &Options, 60 Optional<Reloc::Model> RM, 61 Optional<CodeModel::Model> CM, 62 CodeGenOpt::Level OL, bool JIT) 63 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, 64 getEffectiveRelocModel(RM), 65 getEffectiveCodeModel(CM, CodeModel::Small), OL), 66 TLOF(std::make_unique<TargetLoweringObjectFileELF>()), 67 Subtarget(TT, std::string(CPU), std::string(FS), *this) { 68 initAsmInfo(); 69 70 BPFMCAsmInfo *MAI = 71 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get())); 72 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS()); 73 } 74 75 namespace { 76 // BPF Code Generator Pass Configuration Options. 77 class BPFPassConfig : public TargetPassConfig { 78 public: 79 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM) 80 : TargetPassConfig(TM, PM) {} 81 82 BPFTargetMachine &getBPFTargetMachine() const { 83 return getTM<BPFTargetMachine>(); 84 } 85 86 void addIRPasses() override; 87 bool addInstSelector() override; 88 void addMachineSSAOptimization() override; 89 void addPreEmitPass() override; 90 }; 91 } 92 93 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) { 94 return new BPFPassConfig(*this, PM); 95 } 96 97 void BPFPassConfig::addIRPasses() { 98 99 addPass(createBPFAbstractMemberAccess(&getBPFTargetMachine())); 100 addPass(createBPFPreserveDIType()); 101 102 TargetPassConfig::addIRPasses(); 103 } 104 105 // Install an instruction selector pass using 106 // the ISelDag to gen BPF code. 107 bool BPFPassConfig::addInstSelector() { 108 addPass(createBPFISelDag(getBPFTargetMachine())); 109 110 return false; 111 } 112 113 void BPFPassConfig::addMachineSSAOptimization() { 114 addPass(createBPFMISimplifyPatchablePass()); 115 116 // The default implementation must be called first as we want eBPF 117 // Peephole ran at last. 118 TargetPassConfig::addMachineSSAOptimization(); 119 120 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl(); 121 if (!DisableMIPeephole) { 122 if (Subtarget->getHasAlu32()) 123 addPass(createBPFMIPeepholePass()); 124 addPass(createBPFMIPeepholeTruncElimPass()); 125 } 126 } 127 128 void BPFPassConfig::addPreEmitPass() { 129 addPass(createBPFMIPreEmitCheckingPass()); 130 if (getOptLevel() != CodeGenOpt::None) 131 if (!DisableMIPeephole) 132 addPass(createBPFMIPreEmitPeepholePass()); 133 } 134