Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 1 | //===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass lowers the llvm.experimental.guard intrinsic to a conditional call |
| 11 | // to @llvm.experimental.deoptimize. Once this happens, the guard can no longer |
| 12 | // be widened. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h" |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/IR/BasicBlock.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/InstIterator.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Intrinsics.h" |
| 23 | #include "llvm/IR/IRBuilder.h" |
Sanjoy Das | fd67038 | 2016-05-17 17:51:19 +0000 | [diff] [blame] | 24 | #include "llvm/IR/MDBuilder.h" |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/Pass.h" |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Scalar.h" |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Sanjoy Das | 52bbde2 | 2016-05-18 23:16:27 +0000 | [diff] [blame] | 32 | static cl::opt<uint32_t> PredicatePassBranchWeight( |
| 33 | "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20), |
Sanjoy Das | fd67038 | 2016-05-17 17:51:19 +0000 | [diff] [blame] | 34 | cl::desc("The probability of a guard failing is assumed to be the " |
| 35 | "reciprocal of this value (default = 1 << 20)")); |
| 36 | |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 37 | namespace { |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 38 | struct LowerGuardIntrinsicLegacyPass : public FunctionPass { |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 39 | static char ID; |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 40 | LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) { |
| 41 | initializeLowerGuardIntrinsicLegacyPassPass( |
| 42 | *PassRegistry::getPassRegistry()); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool runOnFunction(Function &F) override; |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | static void MakeGuardControlFlowExplicit(Function *DeoptIntrinsic, |
| 50 | CallInst *CI) { |
| 51 | OperandBundleDef DeoptOB(*CI->getOperandBundle(LLVMContext::OB_deopt)); |
| 52 | SmallVector<Value *, 4> Args(std::next(CI->arg_begin()), CI->arg_end()); |
| 53 | |
| 54 | auto *CheckBB = CI->getParent(); |
| 55 | auto *DeoptBlockTerm = |
| 56 | SplitBlockAndInsertIfThen(CI->getArgOperand(0), CI, true); |
| 57 | |
| 58 | auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator()); |
| 59 | |
| 60 | // SplitBlockAndInsertIfThen inserts control flow that branches to |
| 61 | // DeoptBlockTerm if the condition is true. We want the opposite. |
| 62 | CheckBI->swapSuccessors(); |
| 63 | |
| 64 | CheckBI->getSuccessor(0)->setName("guarded"); |
| 65 | CheckBI->getSuccessor(1)->setName("deopt"); |
| 66 | |
Sanjoy Das | 47cf2af | 2016-04-30 00:55:59 +0000 | [diff] [blame] | 67 | if (auto *MD = CI->getMetadata(LLVMContext::MD_make_implicit)) |
| 68 | CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD); |
| 69 | |
Sanjoy Das | fd67038 | 2016-05-17 17:51:19 +0000 | [diff] [blame] | 70 | MDBuilder MDB(CI->getContext()); |
| 71 | CheckBI->setMetadata(LLVMContext::MD_prof, |
Sanjoy Das | 52bbde2 | 2016-05-18 23:16:27 +0000 | [diff] [blame] | 72 | MDB.createBranchWeights(PredicatePassBranchWeight, 1)); |
Sanjoy Das | fd67038 | 2016-05-17 17:51:19 +0000 | [diff] [blame] | 73 | |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 74 | IRBuilder<> B(DeoptBlockTerm); |
| 75 | auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, ""); |
| 76 | |
| 77 | if (DeoptIntrinsic->getReturnType()->isVoidTy()) { |
| 78 | B.CreateRetVoid(); |
| 79 | } else { |
| 80 | DeoptCall->setName("deoptcall"); |
| 81 | B.CreateRet(DeoptCall); |
| 82 | } |
| 83 | |
Sanjoy Das | 52c68bb | 2016-04-30 00:17:47 +0000 | [diff] [blame] | 84 | DeoptCall->setCallingConv(CI->getCallingConv()); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 85 | DeoptBlockTerm->eraseFromParent(); |
| 86 | } |
| 87 | |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 88 | static bool lowerGuardIntrinsic(Function &F) { |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 89 | // Check if we can cheaply rule out the possibility of not having any work to |
| 90 | // do. |
| 91 | auto *GuardDecl = F.getParent()->getFunction( |
| 92 | Intrinsic::getName(Intrinsic::experimental_guard)); |
| 93 | if (!GuardDecl || GuardDecl->use_empty()) |
| 94 | return false; |
| 95 | |
| 96 | SmallVector<CallInst *, 8> ToLower; |
| 97 | for (auto &I : instructions(F)) |
| 98 | if (auto *CI = dyn_cast<CallInst>(&I)) |
| 99 | if (auto *F = CI->getCalledFunction()) |
| 100 | if (F->getIntrinsicID() == Intrinsic::experimental_guard) |
| 101 | ToLower.push_back(CI); |
| 102 | |
| 103 | if (ToLower.empty()) |
| 104 | return false; |
| 105 | |
| 106 | auto *DeoptIntrinsic = Intrinsic::getDeclaration( |
| 107 | F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()}); |
Sanjoy Das | 52c68bb | 2016-04-30 00:17:47 +0000 | [diff] [blame] | 108 | DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv()); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 109 | |
| 110 | for (auto *CI : ToLower) { |
| 111 | MakeGuardControlFlowExplicit(DeoptIntrinsic, CI); |
| 112 | CI->eraseFromParent(); |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 118 | bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) { |
| 119 | return lowerGuardIntrinsic(F); |
| 120 | } |
| 121 | |
| 122 | char LowerGuardIntrinsicLegacyPass::ID = 0; |
| 123 | INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic", |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 124 | "Lower the guard intrinsic to normal control flow", false, |
| 125 | false) |
| 126 | |
| 127 | Pass *llvm::createLowerGuardIntrinsicPass() { |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 128 | return new LowerGuardIntrinsicLegacyPass(); |
| 129 | } |
| 130 | |
| 131 | PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F, |
| 132 | FunctionAnalysisManager &AM) { |
| 133 | if (lowerGuardIntrinsic(F)) |
| 134 | return PreservedAnalyses::none(); |
| 135 | |
| 136 | return PreservedAnalyses::all(); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 137 | } |