Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
Line
Count
Source
1
//===-- ElimAvailExtern.cpp - DCE unreachable internal functions
2
//----------------===//
3
//
4
//                     The LLVM Compiler Infrastructure
5
//
6
// This file is distributed under the University of Illinois Open Source
7
// License. See LICENSE.TXT for details.
8
//
9
//===----------------------------------------------------------------------===//
10
//
11
// This transform is designed to eliminate available external global
12
// definitions from the program, turning them into declarations.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Transforms/IPO/ElimAvailExtern.h"
17
#include "llvm/ADT/Statistic.h"
18
#include "llvm/IR/Constants.h"
19
#include "llvm/IR/Module.h"
20
#include "llvm/Pass.h"
21
#include "llvm/Transforms/IPO.h"
22
#include "llvm/Transforms/Utils/GlobalStatus.h"
23
using namespace llvm;
24
25
#define DEBUG_TYPE "elim-avail-extern"
26
27
STATISTIC(NumFunctions, "Number of functions removed");
28
STATISTIC(NumVariables, "Number of global variables removed");
29
30
17.3k
static bool eliminateAvailableExternally(Module &M) {
31
17.3k
  bool Changed = false;
32
17.3k
33
17.3k
  // Drop initializers of available externally global variables.
34
773k
  for (GlobalVariable &GV : M.globals()) {
35
773k
    if (!GV.hasAvailableExternallyLinkage())
36
773k
      continue;
37
117
    
if (117
GV.hasInitializer()117
) {
38
117
      Constant *Init = GV.getInitializer();
39
117
      GV.setInitializer(nullptr);
40
117
      if (isSafeToDestroyConstant(Init))
41
116
        Init->destroyConstant();
42
117
    }
43
773k
    GV.removeDeadConstantUsers();
44
773k
    GV.setLinkage(GlobalValue::ExternalLinkage);
45
773k
    NumVariables++;
46
773k
    Changed = true;
47
773k
  }
48
17.3k
49
17.3k
  // Drop the bodies of available externally functions.
50
673k
  for (Function &F : M) {
51
673k
    if (!F.hasAvailableExternallyLinkage())
52
673k
      continue;
53
79
    
if (79
!F.isDeclaration()79
)
54
79
      // This will set the linkage to external
55
79
      F.deleteBody();
56
673k
    F.removeDeadConstantUsers();
57
673k
    NumFunctions++;
58
673k
    Changed = true;
59
673k
  }
60
17.3k
61
17.3k
  return Changed;
62
17.3k
}
63
64
PreservedAnalyses
65
48
EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &) {
66
48
  if (!eliminateAvailableExternally(M))
67
47
    return PreservedAnalyses::all();
68
1
  return PreservedAnalyses::none();
69
1
}
70
71
namespace {
72
struct EliminateAvailableExternallyLegacyPass : public ModulePass {
73
  static char ID; // Pass identification, replacement for typeid
74
17.3k
  EliminateAvailableExternallyLegacyPass() : ModulePass(ID) {
75
17.3k
    initializeEliminateAvailableExternallyLegacyPassPass(
76
17.3k
        *PassRegistry::getPassRegistry());
77
17.3k
  }
78
79
  // run - Do the EliminateAvailableExternally pass on the specified module,
80
  // optionally updating the specified callgraph to reflect the changes.
81
  //
82
17.3k
  bool runOnModule(Module &M) {
83
17.3k
    if (skipModule(M))
84
10
      return false;
85
17.3k
    return eliminateAvailableExternally(M);
86
17.3k
  }
87
};
88
}
89
90
char EliminateAvailableExternallyLegacyPass::ID = 0;
91
INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern",
92
                "Eliminate Available Externally Globals", false, false)
93
94
17.3k
ModulePass *llvm::createEliminateAvailableExternallyPass() {
95
17.3k
  return new EliminateAvailableExternallyLegacyPass();
96
17.3k
}