GuardUtils.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===-- GuardUtils.cpp - Utils for work with guards -------------*- C++ -*-===//
  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. // Utils that are used to perform transformations related to guards and their
  9. // conditions.
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/Transforms/Utils/GuardUtils.h"
  12. #include "llvm/Analysis/GuardUtils.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/IRBuilder.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/MDBuilder.h"
  17. #include "llvm/IR/PatternMatch.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  20. using namespace llvm;
  21. using namespace llvm::PatternMatch;
  22. static cl::opt<uint32_t> PredicatePassBranchWeight(
  23. "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
  24. cl::desc("The probability of a guard failing is assumed to be the "
  25. "reciprocal of this value (default = 1 << 20)"));
  26. void llvm::makeGuardControlFlowExplicit(Function *DeoptIntrinsic,
  27. CallInst *Guard, bool UseWC) {
  28. OperandBundleDef DeoptOB(*Guard->getOperandBundle(LLVMContext::OB_deopt));
  29. SmallVector<Value *, 4> Args(drop_begin(Guard->args()));
  30. auto *CheckBB = Guard->getParent();
  31. auto *DeoptBlockTerm =
  32. SplitBlockAndInsertIfThen(Guard->getArgOperand(0), Guard, true);
  33. auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
  34. // SplitBlockAndInsertIfThen inserts control flow that branches to
  35. // DeoptBlockTerm if the condition is true. We want the opposite.
  36. CheckBI->swapSuccessors();
  37. CheckBI->getSuccessor(0)->setName("guarded");
  38. CheckBI->getSuccessor(1)->setName("deopt");
  39. if (auto *MD = Guard->getMetadata(LLVMContext::MD_make_implicit))
  40. CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
  41. MDBuilder MDB(Guard->getContext());
  42. CheckBI->setMetadata(LLVMContext::MD_prof,
  43. MDB.createBranchWeights(PredicatePassBranchWeight, 1));
  44. IRBuilder<> B(DeoptBlockTerm);
  45. auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
  46. if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
  47. B.CreateRetVoid();
  48. } else {
  49. DeoptCall->setName("deoptcall");
  50. B.CreateRet(DeoptCall);
  51. }
  52. DeoptCall->setCallingConv(Guard->getCallingConv());
  53. DeoptBlockTerm->eraseFromParent();
  54. if (UseWC) {
  55. // We want the guard to be expressed as explicit control flow, but still be
  56. // widenable. For that, we add Widenable Condition intrinsic call to the
  57. // guard's condition.
  58. IRBuilder<> B(CheckBI);
  59. auto *WC = B.CreateIntrinsic(Intrinsic::experimental_widenable_condition,
  60. {}, {}, nullptr, "widenable_cond");
  61. CheckBI->setCondition(B.CreateAnd(CheckBI->getCondition(), WC,
  62. "exiplicit_guard_cond"));
  63. assert(isWidenableBranch(CheckBI) && "Branch must be widenable.");
  64. }
  65. }
  66. void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {
  67. assert(isWidenableBranch(WidenableBR) && "precondition");
  68. // The tempting trivially option is to produce something like this:
  69. // br (and oldcond, newcond) where oldcond is assumed to contain a widenable
  70. // condition, but that doesn't match the pattern parseWidenableBranch expects
  71. // so we have to be more sophisticated.
  72. Use *C, *WC;
  73. BasicBlock *IfTrueBB, *IfFalseBB;
  74. parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);
  75. if (!C) {
  76. // br (wc()), ... form
  77. IRBuilder<> B(WidenableBR);
  78. WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));
  79. } else {
  80. // br (wc & C), ... form
  81. IRBuilder<> B(WidenableBR);
  82. C->set(B.CreateAnd(NewCond, C->get()));
  83. Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
  84. // Condition is only guaranteed to dominate branch
  85. WCAnd->moveBefore(WidenableBR);
  86. }
  87. assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
  88. }
  89. void llvm::setWidenableBranchCond(BranchInst *WidenableBR, Value *NewCond) {
  90. assert(isWidenableBranch(WidenableBR) && "precondition");
  91. Use *C, *WC;
  92. BasicBlock *IfTrueBB, *IfFalseBB;
  93. parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);
  94. if (!C) {
  95. // br (wc()), ... form
  96. IRBuilder<> B(WidenableBR);
  97. WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));
  98. } else {
  99. // br (wc & C), ... form
  100. Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
  101. // Condition is only guaranteed to dominate branch
  102. WCAnd->moveBefore(WidenableBR);
  103. C->set(NewCond);
  104. }
  105. assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
  106. }