Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
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 is used to ensure that functions have at most one return
10
// instruction in them.  Additionally, it keeps track of which node is the new
11
// exit node of the CFG.  If there are no exit nodes in the CFG, the getExitNode
12
// method will return a null pointer.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
17
#include "llvm/IR/BasicBlock.h"
18
#include "llvm/IR/Function.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/Type.h"
21
#include "llvm/Transforms/Utils.h"
22
using namespace llvm;
23
24
char UnifyFunctionExitNodes::ID = 0;
25
INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn",
26
                "Unify function exit nodes", false, false)
27
28
0
Pass *llvm::createUnifyFunctionExitNodesPass() {
29
0
  return new UnifyFunctionExitNodes();
30
0
}
31
32
0
void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
33
0
  // We preserve the non-critical-edgeness property
34
0
  AU.addPreservedID(BreakCriticalEdgesID);
35
0
  // This is a cluster of orthogonal Transforms
36
0
  AU.addPreservedID(LowerSwitchID);
37
0
}
38
39
// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
40
// BasicBlock, and converting all returns to unconditional branches to this
41
// new basic block.  The singular exit node is returned.
42
//
43
// If there are no return stmts in the Function, a null pointer is returned.
44
//
45
0
bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
46
0
  // Loop over all of the blocks in a function, tracking all of the blocks that
47
0
  // return.
48
0
  //
49
0
  std::vector<BasicBlock*> ReturningBlocks;
50
0
  std::vector<BasicBlock*> UnreachableBlocks;
51
0
  for (BasicBlock &I : F)
52
0
    if (isa<ReturnInst>(I.getTerminator()))
53
0
      ReturningBlocks.push_back(&I);
54
0
    else if (isa<UnreachableInst>(I.getTerminator()))
55
0
      UnreachableBlocks.push_back(&I);
56
0
57
0
  // Then unreachable blocks.
58
0
  if (UnreachableBlocks.empty()) {
59
0
    UnreachableBlock = nullptr;
60
0
  } else if (UnreachableBlocks.size() == 1) {
61
0
    UnreachableBlock = UnreachableBlocks.front();
62
0
  } else {
63
0
    UnreachableBlock = BasicBlock::Create(F.getContext(),
64
0
                                          "UnifiedUnreachableBlock", &F);
65
0
    new UnreachableInst(F.getContext(), UnreachableBlock);
66
0
67
0
    for (BasicBlock *BB : UnreachableBlocks) {
68
0
      BB->getInstList().pop_back();  // Remove the unreachable inst.
69
0
      BranchInst::Create(UnreachableBlock, BB);
70
0
    }
71
0
  }
72
0
73
0
  // Now handle return blocks.
74
0
  if (ReturningBlocks.empty()) {
75
0
    ReturnBlock = nullptr;
76
0
    return false;                          // No blocks return
77
0
  } else if (ReturningBlocks.size() == 1) {
78
0
    ReturnBlock = ReturningBlocks.front(); // Already has a single return block
79
0
    return false;
80
0
  }
81
0
82
0
  // Otherwise, we need to insert a new basic block into the function, add a PHI
83
0
  // nodes (if the function returns values), and convert all of the return
84
0
  // instructions into unconditional branches.
85
0
  //
86
0
  BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
87
0
                                               "UnifiedReturnBlock", &F);
88
0
89
0
  PHINode *PN = nullptr;
90
0
  if (F.getReturnType()->isVoidTy()) {
91
0
    ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
92
0
  } else {
93
0
    // If the function doesn't return void... add a PHI node to the block...
94
0
    PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
95
0
                         "UnifiedRetVal");
96
0
    NewRetBlock->getInstList().push_back(PN);
97
0
    ReturnInst::Create(F.getContext(), PN, NewRetBlock);
98
0
  }
99
0
100
0
  // Loop over all of the blocks, replacing the return instruction with an
101
0
  // unconditional branch.
102
0
  //
103
0
  for (BasicBlock *BB : ReturningBlocks) {
104
0
    // Add an incoming element to the PHI node for every return instruction that
105
0
    // is merging into this new block...
106
0
    if (PN)
107
0
      PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
108
0
109
0
    BB->getInstList().pop_back();  // Remove the return insn
110
0
    BranchInst::Create(NewRetBlock, BB);
111
0
  }
112
0
  ReturnBlock = NewRetBlock;
113
0
  return true;
114
0
}