LLVM 17.0.0git
ElimAvailExtern.cpp
Go to the documentation of this file.
1//===- ElimAvailExtern.cpp - DCE unreachable internal functions -----------===//
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 transform is designed to eliminate available external global
10// definitions from the program, turning them into declarations.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/Statistic.h"
16#include "llvm/IR/Constant.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/GlobalValue.h"
20#include "llvm/IR/Module.h"
22#include "llvm/Pass.h"
23#include "llvm/Transforms/IPO.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "elim-avail-extern"
29
30STATISTIC(NumFunctions, "Number of functions removed");
31STATISTIC(NumVariables, "Number of global variables removed");
32
34 bool Changed = false;
35
36 // Drop initializers of available externally global variables.
37 for (GlobalVariable &GV : M.globals()) {
38 if (!GV.hasAvailableExternallyLinkage())
39 continue;
40 if (GV.hasInitializer()) {
41 Constant *Init = GV.getInitializer();
42 GV.setInitializer(nullptr);
44 Init->destroyConstant();
45 }
46 GV.removeDeadConstantUsers();
47 GV.setLinkage(GlobalValue::ExternalLinkage);
48 NumVariables++;
49 Changed = true;
50 }
51
52 // Drop the bodies of available externally functions.
53 for (Function &F : M) {
54 if (!F.hasAvailableExternallyLinkage())
55 continue;
56 if (!F.isDeclaration())
57 // This will set the linkage to external
58 F.deleteBody();
59 F.removeDeadConstantUsers();
60 NumFunctions++;
61 Changed = true;
62 }
63
64 return Changed;
65}
66
72}
static bool eliminateAvailableExternally(Module &M)
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
This is an important base class in LLVM.
Definition: Constant.h:41
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.