Max Kazantsev | b9e65cb | 2018-12-07 14:39:46 +0000 | [diff] [blame] | 1 | //===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Max Kazantsev | b9e65cb | 2018-12-07 14:39:46 +0000 | [diff] [blame] | 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 | |
| 34 | #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h" |
| 35 | #include "llvm/Analysis/GuardUtils.h" |
| 36 | #include "llvm/IR/InstIterator.h" |
| 37 | #include "llvm/IR/IntrinsicInst.h" |
| 38 | #include "llvm/IR/Intrinsics.h" |
| 39 | #include "llvm/IR/IRBuilder.h" |
| 40 | #include "llvm/Pass.h" |
| 41 | #include "llvm/Transforms/Scalar.h" |
| 42 | #include "llvm/Transforms/Utils/GuardUtils.h" |
| 43 | |
| 44 | using namespace llvm; |
| 45 | |
| 46 | namespace { |
| 47 | struct MakeGuardsExplicitLegacyPass : public FunctionPass { |
| 48 | static char ID; |
| 49 | MakeGuardsExplicitLegacyPass() : FunctionPass(ID) { |
| 50 | initializeMakeGuardsExplicitLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 51 | } |
| 52 | |
| 53 | bool runOnFunction(Function &F) override; |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic) { |
| 58 | // Replace the guard with an explicit branch (just like in GuardWidening). |
| 59 | BasicBlock *BB = Guard->getParent(); |
| 60 | makeGuardControlFlowExplicit(DeoptIntrinsic, Guard); |
| 61 | BranchInst *ExplicitGuard = cast<BranchInst>(BB->getTerminator()); |
| 62 | assert(ExplicitGuard->isConditional() && "Must be!"); |
| 63 | |
| 64 | // We want the guard to be expressed as explicit control flow, but still be |
| 65 | // widenable. For that, we add Widenable Condition intrinsic call to the |
| 66 | // guard's condition. |
| 67 | IRBuilder<> B(ExplicitGuard); |
| 68 | auto *WidenableCondition = |
| 69 | B.CreateIntrinsic(Intrinsic::experimental_widenable_condition, |
| 70 | {}, {}, nullptr, "widenable_cond"); |
| 71 | WidenableCondition->setCallingConv(Guard->getCallingConv()); |
| 72 | auto *NewCond = |
| 73 | B.CreateAnd(ExplicitGuard->getCondition(), WidenableCondition); |
| 74 | NewCond->setName("exiplicit_guard_cond"); |
| 75 | ExplicitGuard->setCondition(NewCond); |
| 76 | Guard->eraseFromParent(); |
| 77 | } |
| 78 | |
| 79 | static bool explicifyGuards(Function &F) { |
| 80 | // Check if we can cheaply rule out the possibility of not having any work to |
| 81 | // do. |
| 82 | auto *GuardDecl = F.getParent()->getFunction( |
| 83 | Intrinsic::getName(Intrinsic::experimental_guard)); |
| 84 | if (!GuardDecl || GuardDecl->use_empty()) |
| 85 | return false; |
| 86 | |
| 87 | SmallVector<CallInst *, 8> GuardIntrinsics; |
| 88 | for (auto &I : instructions(F)) |
| 89 | if (isGuard(&I)) |
| 90 | GuardIntrinsics.push_back(cast<CallInst>(&I)); |
| 91 | |
| 92 | if (GuardIntrinsics.empty()) |
| 93 | return false; |
| 94 | |
| 95 | auto *DeoptIntrinsic = Intrinsic::getDeclaration( |
| 96 | F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()}); |
| 97 | DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv()); |
| 98 | |
| 99 | for (auto *Guard : GuardIntrinsics) |
| 100 | turnToExplicitForm(Guard, DeoptIntrinsic); |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool MakeGuardsExplicitLegacyPass::runOnFunction(Function &F) { |
| 106 | return explicifyGuards(F); |
| 107 | } |
| 108 | |
| 109 | char MakeGuardsExplicitLegacyPass::ID = 0; |
| 110 | INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit", |
| 111 | "Lower the guard intrinsic to explicit control flow form", |
| 112 | false, false) |
| 113 | |
| 114 | PreservedAnalyses MakeGuardsExplicitPass::run(Function &F, |
| 115 | FunctionAnalysisManager &) { |
| 116 | if (explicifyGuards(F)) |
| 117 | return PreservedAnalyses::none(); |
| 118 | return PreservedAnalyses::all(); |
| 119 | } |