BPFTargetMachine.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "BPFTargetMachine.h"
  13. #include "BPF.h"
  14. #include "BPFTargetTransformInfo.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/IR/PassManager.h"
  22. #include "llvm/MC/TargetRegistry.h"
  23. #include "llvm/Passes/PassBuilder.h"
  24. #include "llvm/Support/FormattedStream.h"
  25. #include "llvm/Target/TargetOptions.h"
  26. #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  27. #include "llvm/Transforms/Scalar.h"
  28. #include "llvm/Transforms/Scalar/SimplifyCFG.h"
  29. #include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
  30. using namespace llvm;
  31. static cl::
  32. opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
  33. cl::desc("Disable machine peepholes for BPF"));
  34. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
  35. // Register the target.
  36. RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
  37. RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
  38. RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
  39. PassRegistry &PR = *PassRegistry::getPassRegistry();
  40. initializeBPFAbstractMemberAccessLegacyPassPass(PR);
  41. initializeBPFPreserveDITypePass(PR);
  42. initializeBPFIRPeepholePass(PR);
  43. initializeBPFAdjustOptPass(PR);
  44. initializeBPFCheckAndAdjustIRPass(PR);
  45. initializeBPFMIPeepholePass(PR);
  46. initializeBPFMIPeepholeTruncElimPass(PR);
  47. }
  48. // DataLayout: little or big endian
  49. static std::string computeDataLayout(const Triple &TT) {
  50. if (TT.getArch() == Triple::bpfeb)
  51. return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
  52. else
  53. return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
  54. }
  55. static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
  56. return RM.getValueOr(Reloc::PIC_);
  57. }
  58. BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
  59. StringRef CPU, StringRef FS,
  60. const TargetOptions &Options,
  61. Optional<Reloc::Model> RM,
  62. Optional<CodeModel::Model> CM,
  63. CodeGenOpt::Level OL, bool JIT)
  64. : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
  65. getEffectiveRelocModel(RM),
  66. getEffectiveCodeModel(CM, CodeModel::Small), OL),
  67. TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
  68. Subtarget(TT, std::string(CPU), std::string(FS), *this) {
  69. initAsmInfo();
  70. BPFMCAsmInfo *MAI =
  71. static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
  72. MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
  73. }
  74. namespace {
  75. // BPF Code Generator Pass Configuration Options.
  76. class BPFPassConfig : public TargetPassConfig {
  77. public:
  78. BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
  79. : TargetPassConfig(TM, PM) {}
  80. BPFTargetMachine &getBPFTargetMachine() const {
  81. return getTM<BPFTargetMachine>();
  82. }
  83. void addIRPasses() override;
  84. bool addInstSelector() override;
  85. void addMachineSSAOptimization() override;
  86. void addPreEmitPass() override;
  87. };
  88. }
  89. TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
  90. return new BPFPassConfig(*this, PM);
  91. }
  92. void BPFTargetMachine::adjustPassManager(PassManagerBuilder &Builder) {
  93. Builder.addExtension(
  94. PassManagerBuilder::EP_EarlyAsPossible,
  95. [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
  96. PM.add(createBPFAbstractMemberAccess(this));
  97. PM.add(createBPFPreserveDIType());
  98. PM.add(createBPFIRPeephole());
  99. });
  100. Builder.addExtension(
  101. PassManagerBuilder::EP_Peephole,
  102. [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
  103. PM.add(createCFGSimplificationPass(
  104. SimplifyCFGOptions().hoistCommonInsts(true)));
  105. });
  106. Builder.addExtension(
  107. PassManagerBuilder::EP_ModuleOptimizerEarly,
  108. [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
  109. PM.add(createBPFAdjustOpt());
  110. });
  111. }
  112. void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
  113. PB.registerPipelineStartEPCallback(
  114. [=](ModulePassManager &MPM, OptimizationLevel) {
  115. FunctionPassManager FPM;
  116. FPM.addPass(BPFAbstractMemberAccessPass(this));
  117. FPM.addPass(BPFPreserveDITypePass());
  118. FPM.addPass(BPFIRPeepholePass());
  119. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  120. });
  121. PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
  122. OptimizationLevel Level) {
  123. FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
  124. });
  125. PB.registerPipelineEarlySimplificationEPCallback(
  126. [=](ModulePassManager &MPM, OptimizationLevel) {
  127. MPM.addPass(BPFAdjustOptPass());
  128. });
  129. }
  130. void BPFPassConfig::addIRPasses() {
  131. addPass(createBPFCheckAndAdjustIR());
  132. TargetPassConfig::addIRPasses();
  133. }
  134. TargetTransformInfo
  135. BPFTargetMachine::getTargetTransformInfo(const Function &F) {
  136. return TargetTransformInfo(BPFTTIImpl(this, F));
  137. }
  138. // Install an instruction selector pass using
  139. // the ISelDag to gen BPF code.
  140. bool BPFPassConfig::addInstSelector() {
  141. addPass(createBPFISelDag(getBPFTargetMachine()));
  142. return false;
  143. }
  144. void BPFPassConfig::addMachineSSAOptimization() {
  145. addPass(createBPFMISimplifyPatchablePass());
  146. // The default implementation must be called first as we want eBPF
  147. // Peephole ran at last.
  148. TargetPassConfig::addMachineSSAOptimization();
  149. const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
  150. if (!DisableMIPeephole) {
  151. if (Subtarget->getHasAlu32())
  152. addPass(createBPFMIPeepholePass());
  153. addPass(createBPFMIPeepholeTruncElimPass());
  154. }
  155. }
  156. void BPFPassConfig::addPreEmitPass() {
  157. addPass(createBPFMIPreEmitCheckingPass());
  158. if (getOptLevel() != CodeGenOpt::None)
  159. if (!DisableMIPeephole)
  160. addPass(createBPFMIPreEmitPeepholePass());
  161. }