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