• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ReduceArguments.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 uninteresting Arguments from defined functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceArguments.h"
15 #include "Delta.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include <set>
18 #include <vector>
19 
20 using namespace llvm;
21 
22 /// Goes over OldF calls and replaces them with a call to NewF
replaceFunctionCalls(Function & OldF,Function & NewF,const std::set<int> & ArgIndexesToKeep)23 static void replaceFunctionCalls(Function &OldF, Function &NewF,
24                                  const std::set<int> &ArgIndexesToKeep) {
25   const auto &Users = OldF.users();
26   for (auto I = Users.begin(), E = Users.end(); I != E; )
27     if (auto *CI = dyn_cast<CallInst>(*I++)) {
28       SmallVector<Value *, 8> Args;
29       for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
30         if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
31           Args.push_back(*ArgI);
32 
33       CallInst *NewCI = CallInst::Create(&NewF, Args);
34       NewCI->setCallingConv(NewF.getCallingConv());
35       if (!CI->use_empty())
36         CI->replaceAllUsesWith(NewCI);
37       ReplaceInstWithInst(CI, NewCI);
38     }
39 }
40 
41 /// Removes out-of-chunk arguments from functions, and modifies their calls
42 /// accordingly. It also removes allocations of out-of-chunk arguments.
extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,Module * Program)43 static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
44                                        Module *Program) {
45   Oracle O(ChunksToKeep);
46 
47   std::set<Argument *> ArgsToKeep;
48   std::vector<Function *> Funcs;
49   // Get inside-chunk arguments, as well as their parent function
50   for (auto &F : *Program)
51     if (!F.arg_empty()) {
52       Funcs.push_back(&F);
53       for (auto &A : F.args())
54         if (O.shouldKeep())
55           ArgsToKeep.insert(&A);
56     }
57 
58   for (auto *F : Funcs) {
59     ValueToValueMapTy VMap;
60     std::vector<WeakVH> InstToDelete;
61     for (auto &A : F->args())
62       if (!ArgsToKeep.count(&A)) {
63         // By adding undesired arguments to the VMap, CloneFunction will remove
64         // them from the resulting Function
65         VMap[&A] = UndefValue::get(A.getType());
66         for (auto *U : A.users())
67           if (auto *I = dyn_cast<Instruction>(*&U))
68             InstToDelete.push_back(I);
69       }
70     // Delete any (unique) instruction that uses the argument
71     for (Value *V : InstToDelete) {
72       if (!V)
73         continue;
74       auto *I = cast<Instruction>(V);
75       I->replaceAllUsesWith(UndefValue::get(I->getType()));
76       if (!I->isTerminator())
77         I->eraseFromParent();
78     }
79 
80     // No arguments to reduce
81     if (VMap.empty())
82       continue;
83 
84     std::set<int> ArgIndexesToKeep;
85     for (auto &Arg : enumerate(F->args()))
86       if (ArgsToKeep.count(&Arg.value()))
87         ArgIndexesToKeep.insert(Arg.index());
88 
89     auto *ClonedFunc = CloneFunction(F, VMap);
90     // In order to preserve function order, we move Clone after old Function
91     ClonedFunc->removeFromParent();
92     Program->getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
93 
94     replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
95     // Rename Cloned Function to Old's name
96     std::string FName = std::string(F->getName());
97     F->replaceAllUsesWith(ConstantExpr::getBitCast(ClonedFunc, F->getType()));
98     F->eraseFromParent();
99     ClonedFunc->setName(FName);
100   }
101 }
102 
103 /// Counts the amount of arguments in non-declaration functions and prints their
104 /// respective name, index, and parent function name
countArguments(Module * Program)105 static int countArguments(Module *Program) {
106   // TODO: Silence index with --quiet flag
107   outs() << "----------------------------\n";
108   outs() << "Param Index Reference:\n";
109   int ArgsCount = 0;
110   for (auto &F : *Program)
111     if (!F.arg_empty()) {
112       outs() << "  " << F.getName() << "\n";
113       for (auto &A : F.args())
114         outs() << "\t" << ++ArgsCount << ": " << A.getName() << "\n";
115 
116       outs() << "----------------------------\n";
117     }
118 
119   return ArgsCount;
120 }
121 
reduceArgumentsDeltaPass(TestRunner & Test)122 void llvm::reduceArgumentsDeltaPass(TestRunner &Test) {
123   outs() << "*** Reducing Arguments...\n";
124   int ArgCount = countArguments(Test.getProgram());
125   runDeltaPass(Test, ArgCount, extractArgumentsFromModule);
126 }
127