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