blob: 9489e01774d64147d84e63f9f28e27223c92eba9 [file] [log] [blame]
Sanjoy Das021de052016-03-31 00:18:46 +00001//===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sanjoy Das021de052016-03-31 00:18:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers the llvm.experimental.guard intrinsic to a conditional call
10// to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
11// be widened.
12//
13//===----------------------------------------------------------------------===//
14
Michael Kupersteine45d4d92016-07-28 22:08:41 +000015#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sanjoy Das021de052016-03-31 00:18:46 +000016#include "llvm/ADT/SmallVector.h"
Max Kazantsev3c284bd2018-08-30 03:39:16 +000017#include "llvm/Analysis/GuardUtils.h"
Sanjoy Das021de052016-03-31 00:18:46 +000018#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))
Max Kazantsev3c284bd2018-08-30 03:39:16 +000052 if (isGuard(&I))
53 ToLower.push_back(cast<CallInst>(&I));
Sanjoy Das021de052016-03-31 00:18:46 +000054
55 if (ToLower.empty())
56 return false;
57
58 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
59 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
Sanjoy Das52c68bb2016-04-30 00:17:47 +000060 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +000061
62 for (auto *CI : ToLower) {
Max Kazantsev8b4ffe62018-08-29 10:51:59 +000063 makeGuardControlFlowExplicit(DeoptIntrinsic, CI);
Sanjoy Das021de052016-03-31 00:18:46 +000064 CI->eraseFromParent();
65 }
66
67 return true;
68}
69
Michael Kupersteine45d4d92016-07-28 22:08:41 +000070bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
71 return lowerGuardIntrinsic(F);
72}
73
74char LowerGuardIntrinsicLegacyPass::ID = 0;
75INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
Sanjoy Das021de052016-03-31 00:18:46 +000076 "Lower the guard intrinsic to normal control flow", false,
77 false)
78
79Pass *llvm::createLowerGuardIntrinsicPass() {
Michael Kupersteine45d4d92016-07-28 22:08:41 +000080 return new LowerGuardIntrinsicLegacyPass();
81}
82
83PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
84 FunctionAnalysisManager &AM) {
85 if (lowerGuardIntrinsic(F))
86 return PreservedAnalyses::none();
87
88 return PreservedAnalyses::all();
Sanjoy Das021de052016-03-31 00:18:46 +000089}