LowerGuardIntrinsic.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
  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. // This pass lowers the llvm.experimental.guard intrinsic to a conditional call
  10. // to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
  11. // be widened.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/Analysis/GuardUtils.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/InstIterator.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Intrinsics.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/InitializePasses.h"
  23. #include "llvm/Pass.h"
  24. #include "llvm/Transforms/Scalar.h"
  25. #include "llvm/Transforms/Utils/GuardUtils.h"
  26. using namespace llvm;
  27. namespace {
  28. struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
  29. static char ID;
  30. LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
  31. initializeLowerGuardIntrinsicLegacyPassPass(
  32. *PassRegistry::getPassRegistry());
  33. }
  34. bool runOnFunction(Function &F) override;
  35. };
  36. }
  37. static bool lowerGuardIntrinsic(Function &F) {
  38. // Check if we can cheaply rule out the possibility of not having any work to
  39. // do.
  40. auto *GuardDecl = F.getParent()->getFunction(
  41. Intrinsic::getName(Intrinsic::experimental_guard));
  42. if (!GuardDecl || GuardDecl->use_empty())
  43. return false;
  44. SmallVector<CallInst *, 8> ToLower;
  45. // Traverse through the users of GuardDecl.
  46. // This is presumably cheaper than traversing all instructions in the
  47. // function.
  48. for (auto *U : GuardDecl->users())
  49. if (auto *CI = dyn_cast<CallInst>(U))
  50. if (CI->getFunction() == &F)
  51. ToLower.push_back(CI);
  52. if (ToLower.empty())
  53. return false;
  54. auto *DeoptIntrinsic = Intrinsic::getDeclaration(
  55. F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
  56. DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
  57. for (auto *CI : ToLower) {
  58. makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
  59. CI->eraseFromParent();
  60. }
  61. return true;
  62. }
  63. bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
  64. return lowerGuardIntrinsic(F);
  65. }
  66. char LowerGuardIntrinsicLegacyPass::ID = 0;
  67. INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
  68. "Lower the guard intrinsic to normal control flow", false,
  69. false)
  70. Pass *llvm::createLowerGuardIntrinsicPass() {
  71. return new LowerGuardIntrinsicLegacyPass();
  72. }
  73. PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
  74. FunctionAnalysisManager &AM) {
  75. if (lowerGuardIntrinsic(F))
  76. return PreservedAnalyses::none();
  77. return PreservedAnalyses::all();
  78. }