MakeGuardsExplicit.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===//
  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 the new form of
  10. // guard represented as widenable explicit branch to the deopt block. The
  11. // difference between this pass and LowerGuardIntrinsic is that after this pass
  12. // the guard represented as intrinsic:
  13. //
  14. // call void(i1, ...) @llvm.experimental.guard(i1 %old_cond) [ "deopt"() ]
  15. //
  16. // transforms to a guard represented as widenable explicit branch:
  17. //
  18. // %widenable_cond = call i1 @llvm.experimental.widenable.condition()
  19. // br i1 (%old_cond & %widenable_cond), label %guarded, label %deopt
  20. //
  21. // Here:
  22. // - The semantics of @llvm.experimental.widenable.condition allows to replace
  23. // %widenable_cond with the construction (%widenable_cond & %any_other_cond)
  24. // without loss of correctness;
  25. // - %guarded is the lower part of old guard intrinsic's parent block split by
  26. // the intrinsic call;
  27. // - %deopt is a block containing a sole call to @llvm.experimental.deoptimize
  28. // intrinsic.
  29. //
  30. // Therefore, this branch preserves the property of widenability.
  31. //
  32. //===----------------------------------------------------------------------===//
  33. #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
  34. #include "llvm/Analysis/GuardUtils.h"
  35. #include "llvm/IR/InstIterator.h"
  36. #include "llvm/IR/Instructions.h"
  37. #include "llvm/IR/Intrinsics.h"
  38. #include "llvm/InitializePasses.h"
  39. #include "llvm/Pass.h"
  40. #include "llvm/Transforms/Utils/GuardUtils.h"
  41. using namespace llvm;
  42. namespace {
  43. struct MakeGuardsExplicitLegacyPass : public FunctionPass {
  44. static char ID;
  45. MakeGuardsExplicitLegacyPass() : FunctionPass(ID) {
  46. initializeMakeGuardsExplicitLegacyPassPass(*PassRegistry::getPassRegistry());
  47. }
  48. bool runOnFunction(Function &F) override;
  49. };
  50. }
  51. static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic) {
  52. // Replace the guard with an explicit branch (just like in GuardWidening).
  53. BasicBlock *OriginalBB = Guard->getParent();
  54. (void)OriginalBB;
  55. makeGuardControlFlowExplicit(DeoptIntrinsic, Guard, true);
  56. assert(isWidenableBranch(OriginalBB->getTerminator()) && "should hold");
  57. Guard->eraseFromParent();
  58. }
  59. static bool explicifyGuards(Function &F) {
  60. // Check if we can cheaply rule out the possibility of not having any work to
  61. // do.
  62. auto *GuardDecl = F.getParent()->getFunction(
  63. Intrinsic::getName(Intrinsic::experimental_guard));
  64. if (!GuardDecl || GuardDecl->use_empty())
  65. return false;
  66. SmallVector<CallInst *, 8> GuardIntrinsics;
  67. for (auto &I : instructions(F))
  68. if (isGuard(&I))
  69. GuardIntrinsics.push_back(cast<CallInst>(&I));
  70. if (GuardIntrinsics.empty())
  71. return false;
  72. auto *DeoptIntrinsic = Intrinsic::getDeclaration(
  73. F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
  74. DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
  75. for (auto *Guard : GuardIntrinsics)
  76. turnToExplicitForm(Guard, DeoptIntrinsic);
  77. return true;
  78. }
  79. bool MakeGuardsExplicitLegacyPass::runOnFunction(Function &F) {
  80. return explicifyGuards(F);
  81. }
  82. char MakeGuardsExplicitLegacyPass::ID = 0;
  83. INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
  84. "Lower the guard intrinsic to explicit control flow form",
  85. false, false)
  86. PreservedAnalyses MakeGuardsExplicitPass::run(Function &F,
  87. FunctionAnalysisManager &) {
  88. if (explicifyGuards(F))
  89. return PreservedAnalyses::none();
  90. return PreservedAnalyses::all();
  91. }