BPFCheckAndAdjustIR.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===------------ BPFCheckAndAdjustIR.cpp - Check and Adjust IR -----------===//
  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. // Check IR and adjust IR for verifier friendly codes.
  10. // The following are done for IR checking:
  11. // - no relocation globals in PHI node.
  12. // The following are done for IR adjustment:
  13. // - remove __builtin_bpf_passthrough builtins. Target independent IR
  14. // optimizations are done and those builtins can be removed.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "BPF.h"
  18. #include "BPFCORE.h"
  19. #include "BPFTargetMachine.h"
  20. #include "llvm/IR/DebugInfoMetadata.h"
  21. #include "llvm/IR/GlobalVariable.h"
  22. #include "llvm/IR/Instruction.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/IR/Module.h"
  25. #include "llvm/IR/Type.h"
  26. #include "llvm/IR/User.h"
  27. #include "llvm/IR/Value.h"
  28. #include "llvm/Pass.h"
  29. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  30. #define DEBUG_TYPE "bpf-check-and-opt-ir"
  31. using namespace llvm;
  32. namespace {
  33. class BPFCheckAndAdjustIR final : public ModulePass {
  34. bool runOnModule(Module &F) override;
  35. public:
  36. static char ID;
  37. BPFCheckAndAdjustIR() : ModulePass(ID) {}
  38. private:
  39. void checkIR(Module &M);
  40. bool adjustIR(Module &M);
  41. bool removePassThroughBuiltin(Module &M);
  42. };
  43. } // End anonymous namespace
  44. char BPFCheckAndAdjustIR::ID = 0;
  45. INITIALIZE_PASS(BPFCheckAndAdjustIR, DEBUG_TYPE, "BPF Check And Adjust IR",
  46. false, false)
  47. ModulePass *llvm::createBPFCheckAndAdjustIR() {
  48. return new BPFCheckAndAdjustIR();
  49. }
  50. void BPFCheckAndAdjustIR::checkIR(Module &M) {
  51. // Ensure relocation global won't appear in PHI node
  52. // This may happen if the compiler generated the following code:
  53. // B1:
  54. // g1 = @llvm.skb_buff:0:1...
  55. // ...
  56. // goto B_COMMON
  57. // B2:
  58. // g2 = @llvm.skb_buff:0:2...
  59. // ...
  60. // goto B_COMMON
  61. // B_COMMON:
  62. // g = PHI(g1, g2)
  63. // x = load g
  64. // ...
  65. // If anything likes the above "g = PHI(g1, g2)", issue a fatal error.
  66. for (Function &F : M)
  67. for (auto &BB : F)
  68. for (auto &I : BB) {
  69. PHINode *PN = dyn_cast<PHINode>(&I);
  70. if (!PN || PN->use_empty())
  71. continue;
  72. for (int i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
  73. auto *GV = dyn_cast<GlobalVariable>(PN->getIncomingValue(i));
  74. if (!GV)
  75. continue;
  76. if (GV->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
  77. GV->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
  78. report_fatal_error("relocation global in PHI node");
  79. }
  80. }
  81. }
  82. bool BPFCheckAndAdjustIR::removePassThroughBuiltin(Module &M) {
  83. // Remove __builtin_bpf_passthrough()'s which are used to prevent
  84. // certain IR optimizations. Now major IR optimizations are done,
  85. // remove them.
  86. bool Changed = false;
  87. CallInst *ToBeDeleted = nullptr;
  88. for (Function &F : M)
  89. for (auto &BB : F)
  90. for (auto &I : BB) {
  91. if (ToBeDeleted) {
  92. ToBeDeleted->eraseFromParent();
  93. ToBeDeleted = nullptr;
  94. }
  95. auto *Call = dyn_cast<CallInst>(&I);
  96. if (!Call)
  97. continue;
  98. auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
  99. if (!GV)
  100. continue;
  101. if (!GV->getName().startswith("llvm.bpf.passthrough"))
  102. continue;
  103. Changed = true;
  104. Value *Arg = Call->getArgOperand(1);
  105. Call->replaceAllUsesWith(Arg);
  106. ToBeDeleted = Call;
  107. }
  108. return Changed;
  109. }
  110. bool BPFCheckAndAdjustIR::adjustIR(Module &M) {
  111. return removePassThroughBuiltin(M);
  112. }
  113. bool BPFCheckAndAdjustIR::runOnModule(Module &M) {
  114. checkIR(M);
  115. return adjustIR(M);
  116. }