Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/EscapeEnumerator.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- EscapeEnumerator.cpp -----------------------------------------------===//
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
// Defines a helper class that enumerates all possible exits from a function,
10
// including exception handling.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Utils/EscapeEnumerator.h"
15
#include "llvm/Analysis/EHPersonalities.h"
16
#include "llvm/Transforms/Utils/Local.h"
17
#include "llvm/IR/CallSite.h"
18
#include "llvm/IR/Module.h"
19
using namespace llvm;
20
21
5
static FunctionCallee getDefaultPersonalityFn(Module *M) {
22
5
  LLVMContext &C = M->getContext();
23
5
  Triple T(M->getTargetTriple());
24
5
  EHPersonality Pers = getDefaultEHPersonality(T);
25
5
  return M->getOrInsertFunction(getEHPersonalityName(Pers),
26
5
                                FunctionType::get(Type::getInt32Ty(C), true));
27
5
}
28
29
708
IRBuilder<> *EscapeEnumerator::Next() {
30
708
  if (Done)
31
5
    return nullptr;
32
703
33
703
  // Find all 'return', 'resume', and 'unwind' instructions.
34
718
  
while (703
StateBB != StateE) {
35
366
    BasicBlock *CurBB = &*StateBB++;
36
366
37
366
    // Branches and invokes do not escape, only unwind, resume, and return
38
366
    // do.
39
366
    Instruction *TI = CurBB->getTerminator();
40
366
    if (!isa<ReturnInst>(TI) && 
!isa<ResumeInst>(TI)15
)
41
15
      continue;
42
351
43
351
    Builder.SetInsertPoint(TI);
44
351
    return &Builder;
45
351
  }
46
703
47
703
  Done = true;
48
352
49
352
  if (!HandleExceptions)
50
4
    return nullptr;
51
348
52
348
  if (F.doesNotThrow())
53
313
    return nullptr;
54
35
55
35
  // Find all 'call' instructions that may throw.
56
35
  SmallVector<Instruction *, 16> Calls;
57
35
  for (BasicBlock &BB : F)
58
37
    for (Instruction &II : BB)
59
268
      if (CallInst *CI = dyn_cast<CallInst>(&II))
60
141
        if (!CI->doesNotThrow())
61
8
          Calls.push_back(CI);
62
35
63
35
  if (Calls.empty())
64
30
    return nullptr;
65
5
66
5
  // Create a cleanup block.
67
5
  LLVMContext &C = F.getContext();
68
5
  BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
69
5
  Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
70
5
  if (!F.hasPersonalityFn()) {
71
5
    FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
72
5
    F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
73
5
  }
74
5
75
5
  if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
76
0
    report_fatal_error("Scoped EH not supported");
77
0
  }
78
5
79
5
  LandingPadInst *LPad =
80
5
      LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
81
5
  LPad->setCleanup(true);
82
5
  ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
83
5
84
5
  // Transform the 'call' instructions into 'invoke's branching to the
85
5
  // cleanup block. Go in reverse order to make prettier BB names.
86
5
  SmallVector<Value *, 16> Args;
87
13
  for (unsigned I = Calls.size(); I != 0;) {
88
8
    CallInst *CI = cast<CallInst>(Calls[--I]);
89
8
    changeToInvokeAndSplitBasicBlock(CI, CleanupBB);
90
8
  }
91
5
92
5
  Builder.SetInsertPoint(RI);
93
5
  return &Builder;
94
5
}