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" |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/Pass.h" |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Max Kazantsev | 8b4ffe6 | 2018-08-29 10:51:59 +0000 | [diff] [blame^] | 26 | #include "llvm/Transforms/Utils/GuardUtils.h" |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 31 | struct LowerGuardIntrinsicLegacyPass : public FunctionPass { |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 32 | static char ID; |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 33 | LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) { |
| 34 | initializeLowerGuardIntrinsicLegacyPassPass( |
| 35 | *PassRegistry::getPassRegistry()); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool runOnFunction(Function &F) override; |
| 39 | }; |
| 40 | } |
| 41 | |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 42 | static bool lowerGuardIntrinsic(Function &F) { |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 43 | // Check if we can cheaply rule out the possibility of not having any work to |
| 44 | // do. |
| 45 | auto *GuardDecl = F.getParent()->getFunction( |
| 46 | Intrinsic::getName(Intrinsic::experimental_guard)); |
| 47 | if (!GuardDecl || GuardDecl->use_empty()) |
| 48 | return false; |
| 49 | |
| 50 | SmallVector<CallInst *, 8> ToLower; |
| 51 | for (auto &I : instructions(F)) |
| 52 | if (auto *CI = dyn_cast<CallInst>(&I)) |
| 53 | if (auto *F = CI->getCalledFunction()) |
| 54 | if (F->getIntrinsicID() == Intrinsic::experimental_guard) |
| 55 | ToLower.push_back(CI); |
| 56 | |
| 57 | if (ToLower.empty()) |
| 58 | return false; |
| 59 | |
| 60 | auto *DeoptIntrinsic = Intrinsic::getDeclaration( |
| 61 | F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()}); |
Sanjoy Das | 52c68bb | 2016-04-30 00:17:47 +0000 | [diff] [blame] | 62 | DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv()); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 63 | |
| 64 | for (auto *CI : ToLower) { |
Max Kazantsev | 8b4ffe6 | 2018-08-29 10:51:59 +0000 | [diff] [blame^] | 65 | makeGuardControlFlowExplicit(DeoptIntrinsic, CI); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 66 | CI->eraseFromParent(); |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 72 | bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) { |
| 73 | return lowerGuardIntrinsic(F); |
| 74 | } |
| 75 | |
| 76 | char LowerGuardIntrinsicLegacyPass::ID = 0; |
| 77 | INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic", |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 78 | "Lower the guard intrinsic to normal control flow", false, |
| 79 | false) |
| 80 | |
| 81 | Pass *llvm::createLowerGuardIntrinsicPass() { |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 82 | return new LowerGuardIntrinsicLegacyPass(); |
| 83 | } |
| 84 | |
| 85 | PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F, |
| 86 | FunctionAnalysisManager &AM) { |
| 87 | if (lowerGuardIntrinsic(F)) |
| 88 | return PreservedAnalyses::none(); |
| 89 | |
| 90 | return PreservedAnalyses::all(); |
Sanjoy Das | 021de05 | 2016-03-31 00:18:46 +0000 | [diff] [blame] | 91 | } |