1*da58b97aSjoerg //===- ReduceGlobalVars.cpp - Specialized Delta Pass ----------------------===//
2*da58b97aSjoerg //
3*da58b97aSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*da58b97aSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*da58b97aSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*da58b97aSjoerg //
7*da58b97aSjoerg //===----------------------------------------------------------------------===//
8*da58b97aSjoerg //
9*da58b97aSjoerg // This file implements a function which calls the Generic Delta pass in order
10*da58b97aSjoerg // to reduce initializers of Global Variables in the provided Module.
11*da58b97aSjoerg //
12*da58b97aSjoerg //===----------------------------------------------------------------------===//
13*da58b97aSjoerg 
14*da58b97aSjoerg #include "ReduceGlobalVarInitializers.h"
15*da58b97aSjoerg #include "llvm/IR/Constants.h"
16*da58b97aSjoerg #include "llvm/IR/GlobalValue.h"
17*da58b97aSjoerg 
18*da58b97aSjoerg using namespace llvm;
19*da58b97aSjoerg 
20*da58b97aSjoerg /// Removes all the Initialized GVs that aren't inside the desired Chunks.
extractGVsFromModule(std::vector<Chunk> ChunksToKeep,Module * Program)21*da58b97aSjoerg static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
22*da58b97aSjoerg                                  Module *Program) {
23*da58b97aSjoerg   Oracle O(ChunksToKeep);
24*da58b97aSjoerg 
25*da58b97aSjoerg   // Drop initializers of out-of-chunk GVs
26*da58b97aSjoerg   for (auto &GV : Program->globals())
27*da58b97aSjoerg     if (GV.hasInitializer() && !O.shouldKeep()) {
28*da58b97aSjoerg       GV.setInitializer(nullptr);
29*da58b97aSjoerg       GV.setLinkage(GlobalValue::LinkageTypes::ExternalLinkage);
30*da58b97aSjoerg       GV.setComdat(nullptr);
31*da58b97aSjoerg     }
32*da58b97aSjoerg }
33*da58b97aSjoerg 
34*da58b97aSjoerg /// Counts the amount of initialized GVs and displays their
35*da58b97aSjoerg /// respective name & index
countGVs(Module * Program)36*da58b97aSjoerg static int countGVs(Module *Program) {
37*da58b97aSjoerg   // TODO: Silence index with --quiet flag
38*da58b97aSjoerg   outs() << "----------------------------\n";
39*da58b97aSjoerg   outs() << "GlobalVariable Index Reference:\n";
40*da58b97aSjoerg   int GVCount = 0;
41*da58b97aSjoerg   for (auto &GV : Program->globals())
42*da58b97aSjoerg     if (GV.hasInitializer())
43*da58b97aSjoerg       outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
44*da58b97aSjoerg   outs() << "----------------------------\n";
45*da58b97aSjoerg   return GVCount;
46*da58b97aSjoerg }
47*da58b97aSjoerg 
reduceGlobalsInitializersDeltaPass(TestRunner & Test)48*da58b97aSjoerg void llvm::reduceGlobalsInitializersDeltaPass(TestRunner &Test) {
49*da58b97aSjoerg   outs() << "*** Reducing GVs initializers...\n";
50*da58b97aSjoerg   int GVCount = countGVs(Test.getProgram());
51*da58b97aSjoerg   runDeltaPass(Test, GVCount, extractGVsFromModule);
52*da58b97aSjoerg }
53