BPFCheckAndAdjustIR.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. bool removeCompareBuiltin(Module &M);
  43. };
  44. } // End anonymous namespace
  45. char BPFCheckAndAdjustIR::ID = 0;
  46. INITIALIZE_PASS(BPFCheckAndAdjustIR, DEBUG_TYPE, "BPF Check And Adjust IR",
  47. false, false)
  48. ModulePass *llvm::createBPFCheckAndAdjustIR() {
  49. return new BPFCheckAndAdjustIR();
  50. }
  51. void BPFCheckAndAdjustIR::checkIR(Module &M) {
  52. // Ensure relocation global won't appear in PHI node
  53. // This may happen if the compiler generated the following code:
  54. // B1:
  55. // g1 = @llvm.skb_buff:0:1...
  56. // ...
  57. // goto B_COMMON
  58. // B2:
  59. // g2 = @llvm.skb_buff:0:2...
  60. // ...
  61. // goto B_COMMON
  62. // B_COMMON:
  63. // g = PHI(g1, g2)
  64. // x = load g
  65. // ...
  66. // If anything likes the above "g = PHI(g1, g2)", issue a fatal error.
  67. for (Function &F : M)
  68. for (auto &BB : F)
  69. for (auto &I : BB) {
  70. PHINode *PN = dyn_cast<PHINode>(&I);
  71. if (!PN || PN->use_empty())
  72. continue;
  73. for (int i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
  74. auto *GV = dyn_cast<GlobalVariable>(PN->getIncomingValue(i));
  75. if (!GV)
  76. continue;
  77. if (GV->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
  78. GV->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
  79. report_fatal_error("relocation global in PHI node");
  80. }
  81. }
  82. }
  83. bool BPFCheckAndAdjustIR::removePassThroughBuiltin(Module &M) {
  84. // Remove __builtin_bpf_passthrough()'s which are used to prevent
  85. // certain IR optimizations. Now major IR optimizations are done,
  86. // remove them.
  87. bool Changed = false;
  88. CallInst *ToBeDeleted = nullptr;
  89. for (Function &F : M)
  90. for (auto &BB : F)
  91. for (auto &I : BB) {
  92. if (ToBeDeleted) {
  93. ToBeDeleted->eraseFromParent();
  94. ToBeDeleted = nullptr;
  95. }
  96. auto *Call = dyn_cast<CallInst>(&I);
  97. if (!Call)
  98. continue;
  99. auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
  100. if (!GV)
  101. continue;
  102. if (!GV->getName().startswith("llvm.bpf.passthrough"))
  103. continue;
  104. Changed = true;
  105. Value *Arg = Call->getArgOperand(1);
  106. Call->replaceAllUsesWith(Arg);
  107. ToBeDeleted = Call;
  108. }
  109. return Changed;
  110. }
  111. bool BPFCheckAndAdjustIR::removeCompareBuiltin(Module &M) {
  112. // Remove __builtin_bpf_compare()'s which are used to prevent
  113. // certain IR optimizations. Now major IR optimizations are done,
  114. // remove them.
  115. bool Changed = false;
  116. CallInst *ToBeDeleted = nullptr;
  117. for (Function &F : M)
  118. for (auto &BB : F)
  119. for (auto &I : BB) {
  120. if (ToBeDeleted) {
  121. ToBeDeleted->eraseFromParent();
  122. ToBeDeleted = nullptr;
  123. }
  124. auto *Call = dyn_cast<CallInst>(&I);
  125. if (!Call)
  126. continue;
  127. auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
  128. if (!GV)
  129. continue;
  130. if (!GV->getName().startswith("llvm.bpf.compare"))
  131. continue;
  132. Changed = true;
  133. Value *Arg0 = Call->getArgOperand(0);
  134. Value *Arg1 = Call->getArgOperand(1);
  135. Value *Arg2 = Call->getArgOperand(2);
  136. auto OpVal = cast<ConstantInt>(Arg0)->getValue().getZExtValue();
  137. CmpInst::Predicate Opcode = (CmpInst::Predicate)OpVal;
  138. auto *ICmp = new ICmpInst(Opcode, Arg1, Arg2);
  139. ICmp->insertBefore(Call);
  140. Call->replaceAllUsesWith(ICmp);
  141. ToBeDeleted = Call;
  142. }
  143. return Changed;
  144. }
  145. bool BPFCheckAndAdjustIR::adjustIR(Module &M) {
  146. bool Changed = removePassThroughBuiltin(M);
  147. Changed = removeCompareBuiltin(M) || Changed;
  148. return Changed;
  149. }
  150. bool BPFCheckAndAdjustIR::runOnModule(Module &M) {
  151. checkIR(M);
  152. return adjustIR(M);
  153. }