blob: 0e138cbbc7a10bdf28eb065e8aabbb26ed46b5d4 [file] [log] [blame]
Teresa Johnsonf72278f2015-11-02 18:02:11 +00001//===-- ElimAvailExtern.cpp - DCE unreachable internal functions
2//----------------===//
Teresa Johnsond3a33a12015-07-06 16:22:42 +00003//
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.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/IR/Constants.h"
Teresa Johnsond3a33a12015-07-06 16:22:42 +000019#include "llvm/IR/Module.h"
Teresa Johnsond3a33a12015-07-06 16:22:42 +000020#include "llvm/Transforms/Utils/GlobalStatus.h"
21#include "llvm/Pass.h"
22using namespace llvm;
23
24#define DEBUG_TYPE "elim-avail-extern"
25
Teresa Johnsond3a33a12015-07-06 16:22:42 +000026STATISTIC(NumFunctions, "Number of functions removed");
27STATISTIC(NumVariables, "Number of global variables removed");
28
29namespace {
Teresa Johnsonf72278f2015-11-02 18:02:11 +000030struct EliminateAvailableExternally : public ModulePass {
31 static char ID; // Pass identification, replacement for typeid
32 EliminateAvailableExternally() : ModulePass(ID) {
33 initializeEliminateAvailableExternallyPass(
34 *PassRegistry::getPassRegistry());
35 }
Teresa Johnsond3a33a12015-07-06 16:22:42 +000036
Teresa Johnsonf72278f2015-11-02 18:02:11 +000037 // run - Do the EliminateAvailableExternally pass on the specified module,
38 // optionally updating the specified callgraph to reflect the changes.
39 //
40 bool runOnModule(Module &M) override;
41};
Teresa Johnsond3a33a12015-07-06 16:22:42 +000042}
43
44char EliminateAvailableExternally::ID = 0;
45INITIALIZE_PASS(EliminateAvailableExternally, "elim-avail-extern",
46 "Eliminate Available Externally Globals", false, false)
47
48ModulePass *llvm::createEliminateAvailableExternallyPass() {
49 return new EliminateAvailableExternally();
50}
51
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000052static void convertAliasToDeclaration(GlobalAlias &GA, Module &M) {
53 GlobalValue *GVal = GA.getBaseObject();
54 GlobalValue *NewGV;
55 if (auto *GVar = dyn_cast<GlobalVariable>(GVal)) {
56 GlobalVariable *NewGVar = new GlobalVariable(
57 M, GVar->getType()->getElementType(), GVar->isConstant(),
58 GVar->getLinkage(), /*init*/ nullptr, GA.getName(), GVar,
59 GVar->getThreadLocalMode(), GVar->getType()->getAddressSpace());
60 NewGV = NewGVar;
61 NewGV->copyAttributesFrom(GVar);
62 } else {
63 auto *F = dyn_cast<Function>(GVal);
64 assert(F);
65 Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
66 GA.getName(), &M);
67 NewGV = NewF;
68 NewGV->copyAttributesFrom(F);
69 }
70 GA.replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, GA.getType()));
71 GA.eraseFromParent();
72}
73
Teresa Johnsond3a33a12015-07-06 16:22:42 +000074bool EliminateAvailableExternally::runOnModule(Module &M) {
75 bool Changed = false;
76
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000077 // Convert any aliases that alias with an available externally
78 // value (which will be turned into declarations later on in this routine)
79 // into declarations themselves. All aliases must be definitions, and
80 // must alias with a definition. So this involves creating a declaration
81 // equivalent to the alias's base object.
82 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;) {
83 // Increment the iterator first since we may delete the current alias.
84 GlobalAlias &GA = *(I++);
85 GlobalValue *GVal = GA.getBaseObject();
86 if (!GVal->hasAvailableExternallyLinkage())
87 continue;
88 convertAliasToDeclaration(GA, M);
89 Changed = true;
90 }
91
Teresa Johnsond3a33a12015-07-06 16:22:42 +000092 // Drop initializers of available externally global variables.
Teresa Johnsonf72278f2015-11-02 18:02:11 +000093 for (GlobalVariable &GV : M.globals()) {
Yaron Keren771e3192015-09-04 20:24:24 +000094 if (!GV.hasAvailableExternallyLinkage())
Teresa Johnsond3a33a12015-07-06 16:22:42 +000095 continue;
Yaron Keren771e3192015-09-04 20:24:24 +000096 if (GV.hasInitializer()) {
97 Constant *Init = GV.getInitializer();
98 GV.setInitializer(nullptr);
Teresa Johnsond3a33a12015-07-06 16:22:42 +000099 if (isSafeToDestroyConstant(Init))
100 Init->destroyConstant();
101 }
Yaron Keren771e3192015-09-04 20:24:24 +0000102 GV.removeDeadConstantUsers();
103 GV.setLinkage(GlobalValue::ExternalLinkage);
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000104 NumVariables++;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000105 Changed = true;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000106 }
107
108 // Drop the bodies of available externally functions.
Yaron Keren771e3192015-09-04 20:24:24 +0000109 for (Function &F : M) {
110 if (!F.hasAvailableExternallyLinkage())
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000111 continue;
Yaron Keren771e3192015-09-04 20:24:24 +0000112 if (!F.isDeclaration())
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000113 // This will set the linkage to external
Yaron Keren771e3192015-09-04 20:24:24 +0000114 F.deleteBody();
115 F.removeDeadConstantUsers();
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000116 NumFunctions++;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000117 Changed = true;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000118 }
119
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000120 return Changed;
121}