1 //===- ReduceAliases.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 aliases in the provided Module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceAliases.h"
15 #include "Delta.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/GlobalValue.h"
18 
19 using namespace llvm;
20 
21 /// Removes all aliases aren't inside any of the
22 /// desired Chunks.
extractAliasesFromModule(const std::vector<Chunk> & ChunksToKeep,Module * Program)23 static void extractAliasesFromModule(const std::vector<Chunk> &ChunksToKeep,
24                                      Module *Program) {
25   Oracle O(ChunksToKeep);
26 
27   for (auto &GA : make_early_inc_range(Program->aliases())) {
28     if (!O.shouldKeep()) {
29       GA.replaceAllUsesWith(GA.getAliasee());
30       GA.eraseFromParent();
31     }
32   }
33 }
34 
35 /// Counts the amount of aliases and prints their respective name & index.
countAliases(Module * Program)36 static int countAliases(Module *Program) {
37   // TODO: Silence index with --quiet flag
38   errs() << "----------------------------\n";
39   errs() << "Aliases Index Reference:\n";
40   int Count = 0;
41   for (auto &GA : Program->aliases())
42     errs() << "\t" << ++Count << ": " << GA.getName() << "\n";
43 
44   errs() << "----------------------------\n";
45   return Count;
46 }
47 
reduceAliasesDeltaPass(TestRunner & Test)48 void llvm::reduceAliasesDeltaPass(TestRunner &Test) {
49   errs() << "*** Reducing Aliases ...\n";
50   int Functions = countAliases(Test.getProgram());
51   runDeltaPass(Test, Functions, extractAliasesFromModule);
52   errs() << "----------------------------\n";
53 }
54