Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
2
//
3
// 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
6
//
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
15
#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
16
#include "llvm/ADT/SmallVector.h"
17
#include "llvm/Analysis/GuardUtils.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/Module.h"
24
#include "llvm/Pass.h"
25
#include "llvm/Transforms/Scalar.h"
26
#include "llvm/Transforms/Utils/GuardUtils.h"
27
28
using namespace llvm;
29
30
namespace {
31
struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
32
  static char ID;
33
2
  LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
34
2
    initializeLowerGuardIntrinsicLegacyPassPass(
35
2
        *PassRegistry::getPassRegistry());
36
2
  }
37
38
  bool runOnFunction(Function &F) override;
39
};
40
}
41
42
11
static bool lowerGuardIntrinsic(Function &F) {
43
11
  // Check if we can cheaply rule out the possibility of not having any work to
44
11
  // do.
45
11
  auto *GuardDecl = F.getParent()->getFunction(
46
11
      Intrinsic::getName(Intrinsic::experimental_guard));
47
11
  if (!GuardDecl || GuardDecl->use_empty())
48
0
    return false;
49
11
50
11
  SmallVector<CallInst *, 8> ToLower;
51
11
  for (auto &I : instructions(F))
52
33
    if (isGuard(&I))
53
11
      ToLower.push_back(cast<CallInst>(&I));
54
11
55
11
  if (ToLower.empty())
56
0
    return false;
57
11
58
11
  auto *DeoptIntrinsic = Intrinsic::getDeclaration(
59
11
      F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
60
11
  DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
61
11
62
11
  for (auto *CI : ToLower) {
63
11
    makeGuardControlFlowExplicit(DeoptIntrinsic, CI);
64
11
    CI->eraseFromParent();
65
11
  }
66
11
67
11
  return true;
68
11
}
69
70
6
bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
71
6
  return lowerGuardIntrinsic(F);
72
6
}
73
74
char LowerGuardIntrinsicLegacyPass::ID = 0;
75
INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
76
                "Lower the guard intrinsic to normal control flow", false,
77
                false)
78
79
0
Pass *llvm::createLowerGuardIntrinsicPass() {
80
0
  return new LowerGuardIntrinsicLegacyPass();
81
0
}
82
83
PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
84
5
                                               FunctionAnalysisManager &AM) {
85
5
  if (lowerGuardIntrinsic(F))
86
5
    return PreservedAnalyses::none();
87
0
88
0
  return PreservedAnalyses::all();
89
0
}