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 "Utils.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/Transforms/Utils/ModuleUtils.h"
20 
21 using namespace llvm;
22 
23 /// Removes all aliases aren't inside any of the
24 /// desired Chunks.
extractAliasesFromModule(Oracle & O,ReducerWorkItem & Program)25 static void extractAliasesFromModule(Oracle &O, ReducerWorkItem &Program) {
26   for (auto &GA : make_early_inc_range(Program.getModule().aliases())) {
27     if (!O.shouldKeep()) {
28       GA.replaceAllUsesWith(GA.getAliasee());
29       GA.eraseFromParent();
30     }
31   }
32 }
33 
extractIFuncsFromModule(Oracle & O,Module & Program)34 static void extractIFuncsFromModule(Oracle &O, Module &Program) {
35   std::vector<GlobalIFunc *> IFuncs;
36   for (GlobalIFunc &GI : Program.ifuncs()) {
37     if (!O.shouldKeep())
38       IFuncs.push_back(&GI);
39   }
40 
41   if (!IFuncs.empty())
42     lowerGlobalIFuncUsersAsGlobalCtor(Program, IFuncs);
43 }
44 
reduceAliasesDeltaPass(TestRunner & Test)45 void llvm::reduceAliasesDeltaPass(TestRunner &Test) {
46   runDeltaPass(Test, extractAliasesFromModule, "Reducing Aliases");
47 }
48 
reduceIFuncsDeltaPass(TestRunner & Test)49 void llvm::reduceIFuncsDeltaPass(TestRunner &Test) {
50   runDeltaPass(Test, extractIFuncsFromModule, "Reducing Ifuncs");
51 }
52