blob: fac57014740f99e2b5e565a12530f987a738f3a6 [file] [log] [blame]
Sanjoy Das021de052016-03-31 00:18:46 +00001//===- 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 Kupersteine45d4d92016-07-28 22:08:41 +000016#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sanjoy Das021de052016-03-31 00:18:46 +000017#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 Das021de052016-03-31 00:18:46 +000023#include "llvm/IR/Module.h"
24#include "llvm/Pass.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +000025#include "llvm/Transforms/Scalar.h"
Max Kazantsev8b4ffe62018-08-29 10:51:59 +000026#include "llvm/Transforms/Utils/GuardUtils.h"
Sanjoy Das021de052016-03-31 00:18:46 +000027
28using namespace llvm;
29
30namespace {
Michael Kupersteine45d4d92016-07-28 22:08:41 +000031struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
Sanjoy Das021de052016-03-31 00:18:46 +000032 static char ID;
Michael Kupersteine45d4d92016-07-28 22:08:41 +000033 LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
34 initializeLowerGuardIntrinsicLegacyPassPass(
35 *PassRegistry::getPassRegistry());
Sanjoy Das021de052016-03-31 00:18:46 +000036 }
37
38 bool runOnFunction(Function &F) override;
39};
40}
41
Michael Kupersteine45d4d92016-07-28 22:08:41 +000042static bool lowerGuardIntrinsic(Function &F) {
Sanjoy Das021de052016-03-31 00:18:46 +000043 // 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 Das52c68bb2016-04-30 00:17:47 +000062 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +000063
64 for (auto *CI : ToLower) {
Max Kazantsev8b4ffe62018-08-29 10:51:59 +000065 makeGuardControlFlowExplicit(DeoptIntrinsic, CI);
Sanjoy Das021de052016-03-31 00:18:46 +000066 CI->eraseFromParent();
67 }
68
69 return true;
70}
71
Michael Kupersteine45d4d92016-07-28 22:08:41 +000072bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
73 return lowerGuardIntrinsic(F);
74}
75
76char LowerGuardIntrinsicLegacyPass::ID = 0;
77INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
Sanjoy Das021de052016-03-31 00:18:46 +000078 "Lower the guard intrinsic to normal control flow", false,
79 false)
80
81Pass *llvm::createLowerGuardIntrinsicPass() {
Michael Kupersteine45d4d92016-07-28 22:08:41 +000082 return new LowerGuardIntrinsicLegacyPass();
83}
84
85PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
86 FunctionAnalysisManager &AM) {
87 if (lowerGuardIntrinsic(F))
88 return PreservedAnalyses::none();
89
90 return PreservedAnalyses::all();
Sanjoy Das021de052016-03-31 00:18:46 +000091}