LowerGuardIntrinsic.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/BasicBlock.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/InstIterator.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/Intrinsics.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Transforms/Scalar.h"
  26. #include "llvm/Transforms/Utils/GuardUtils.h"
  27. using namespace llvm;
  28. namespace {
  29. struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
  30. static char ID;
  31. LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
  32. initializeLowerGuardIntrinsicLegacyPassPass(
  33. *PassRegistry::getPassRegistry());
  34. }
  35. bool runOnFunction(Function &F) override;
  36. };
  37. }
  38. static bool lowerGuardIntrinsic(Function &F) {
  39. // Check if we can cheaply rule out the possibility of not having any work to
  40. // do.
  41. auto *GuardDecl = F.getParent()->getFunction(
  42. Intrinsic::getName(Intrinsic::experimental_guard));
  43. if (!GuardDecl || GuardDecl->use_empty())
  44. return false;
  45. SmallVector<CallInst *, 8> ToLower;
  46. for (auto &I : instructions(F))
  47. if (isGuard(&I))
  48. ToLower.push_back(cast<CallInst>(&I));
  49. if (ToLower.empty())
  50. return false;
  51. auto *DeoptIntrinsic = Intrinsic::getDeclaration(
  52. F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
  53. DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
  54. for (auto *CI : ToLower) {
  55. makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
  56. CI->eraseFromParent();
  57. }
  58. return true;
  59. }
  60. bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
  61. return lowerGuardIntrinsic(F);
  62. }
  63. char LowerGuardIntrinsicLegacyPass::ID = 0;
  64. INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
  65. "Lower the guard intrinsic to normal control flow", false,
  66. false)
  67. Pass *llvm::createLowerGuardIntrinsicPass() {
  68. return new LowerGuardIntrinsicLegacyPass();
  69. }
  70. PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
  71. FunctionAnalysisManager &AM) {
  72. if (lowerGuardIntrinsic(F))
  73. return PreservedAnalyses::none();
  74. return PreservedAnalyses::all();
  75. }