BPFIRPeephole.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===------------ BPFIRPeephole.cpp - IR Peephole Transformation ----------===//
  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. // IR level peephole optimization, specifically removing @llvm.stacksave() and
  10. // @llvm.stackrestore().
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "BPF.h"
  14. #include "llvm/IR/Instruction.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/PassManager.h"
  18. #include "llvm/IR/Type.h"
  19. #include "llvm/IR/User.h"
  20. #include "llvm/IR/Value.h"
  21. #include "llvm/Pass.h"
  22. #define DEBUG_TYPE "bpf-ir-peephole"
  23. using namespace llvm;
  24. namespace {
  25. static bool BPFIRPeepholeImpl(Function &F) {
  26. LLVM_DEBUG(dbgs() << "******** BPF IR Peephole ********\n");
  27. bool Changed = false;
  28. Instruction *ToErase = nullptr;
  29. for (auto &BB : F) {
  30. for (auto &I : BB) {
  31. // The following code pattern is handled:
  32. // %3 = call i8* @llvm.stacksave()
  33. // store i8* %3, i8** %saved_stack, align 8
  34. // ...
  35. // %4 = load i8*, i8** %saved_stack, align 8
  36. // call void @llvm.stackrestore(i8* %4)
  37. // ...
  38. // The goal is to remove the above four instructions,
  39. // so we won't have instructions with r11 (stack pointer)
  40. // if eventually there is no variable length stack allocation.
  41. // InstrCombine also tries to remove the above instructions,
  42. // if it is proven safe (constant alloca etc.), but depending
  43. // on code pattern, it may still miss some.
  44. //
  45. // With unconditionally removing these instructions, if alloca is
  46. // constant, we are okay then. Otherwise, SelectionDag will complain
  47. // since BPF does not support dynamic allocation yet.
  48. if (ToErase) {
  49. ToErase->eraseFromParent();
  50. ToErase = nullptr;
  51. }
  52. if (auto *Call = dyn_cast<CallInst>(&I)) {
  53. if (auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand())) {
  54. if (!GV->getName().equals("llvm.stacksave"))
  55. continue;
  56. if (!Call->hasOneUser())
  57. continue;
  58. auto *Inst = cast<Instruction>(*Call->user_begin());
  59. LLVM_DEBUG(dbgs() << "Remove:"; I.dump());
  60. LLVM_DEBUG(dbgs() << "Remove:"; Inst->dump(); dbgs() << '\n');
  61. Changed = true;
  62. Inst->eraseFromParent();
  63. ToErase = &I;
  64. }
  65. continue;
  66. }
  67. if (auto *LD = dyn_cast<LoadInst>(&I)) {
  68. if (!LD->hasOneUser())
  69. continue;
  70. auto *Call = dyn_cast<CallInst>(*LD->user_begin());
  71. if (!Call)
  72. continue;
  73. auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
  74. if (!GV)
  75. continue;
  76. if (!GV->getName().equals("llvm.stackrestore"))
  77. continue;
  78. LLVM_DEBUG(dbgs() << "Remove:"; I.dump());
  79. LLVM_DEBUG(dbgs() << "Remove:"; Call->dump(); dbgs() << '\n');
  80. Changed = true;
  81. Call->eraseFromParent();
  82. ToErase = &I;
  83. }
  84. }
  85. }
  86. return Changed;
  87. }
  88. class BPFIRPeephole final : public FunctionPass {
  89. bool runOnFunction(Function &F) override;
  90. public:
  91. static char ID;
  92. BPFIRPeephole() : FunctionPass(ID) {}
  93. };
  94. } // End anonymous namespace
  95. char BPFIRPeephole::ID = 0;
  96. INITIALIZE_PASS(BPFIRPeephole, DEBUG_TYPE, "BPF IR Peephole", false, false)
  97. FunctionPass *llvm::createBPFIRPeephole() { return new BPFIRPeephole(); }
  98. bool BPFIRPeephole::runOnFunction(Function &F) { return BPFIRPeepholeImpl(F); }
  99. PreservedAnalyses BPFIRPeepholePass::run(Function &F,
  100. FunctionAnalysisManager &AM) {
  101. return BPFIRPeepholeImpl(F) ? PreservedAnalyses::none()
  102. : PreservedAnalyses::all();
  103. }