1 //===- ReduceArguments.cpp - Specialized Delta Pass -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a function which calls the Generic Delta pass in order
10 // to reduce uninteresting Arguments from declared and defined functions.
12 //===----------------------------------------------------------------------===//
14 #include "ReduceArguments.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Intrinsics.h"
26 /// Goes over OldF calls and replaces them with a call to NewF
27 static void replaceFunctionCalls(Function
&OldF
, Function
&NewF
,
28 const std::set
<int> &ArgIndexesToKeep
) {
29 const auto &Users
= OldF
.users();
30 for (auto I
= Users
.begin(), E
= Users
.end(); I
!= E
; )
31 if (auto *CI
= dyn_cast
<CallInst
>(*I
++)) {
32 // Skip uses in call instructions where OldF isn't the called function
33 // (e.g. if OldF is an argument of the call).
34 if (CI
->getCalledFunction() != &OldF
)
36 SmallVector
<Value
*, 8> Args
;
37 for (auto ArgI
= CI
->arg_begin(), E
= CI
->arg_end(); ArgI
!= E
; ++ArgI
)
38 if (ArgIndexesToKeep
.count(ArgI
- CI
->arg_begin()))
39 Args
.push_back(*ArgI
);
41 CallInst
*NewCI
= CallInst::Create(&NewF
, Args
);
42 NewCI
->setCallingConv(NewF
.getCallingConv());
44 CI
->replaceAllUsesWith(NewCI
);
45 ReplaceInstWithInst(CI
, NewCI
);
49 /// Returns whether or not this function should be considered a candidate for
50 /// argument removal. Currently, functions with no arguments and intrinsics are
51 /// not considered. Intrinsics aren't considered because their signatures are
53 static bool shouldRemoveArguments(const Function
&F
) {
54 return !F
.arg_empty() && !F
.isIntrinsic();
57 /// Removes out-of-chunk arguments from functions, and modifies their calls
58 /// accordingly. It also removes allocations of out-of-chunk arguments.
59 static void extractArgumentsFromModule(Oracle
&O
, ReducerWorkItem
&WorkItem
) {
60 Module
&Program
= WorkItem
.getModule();
61 std::vector
<Argument
*> InitArgsToKeep
;
62 std::vector
<Function
*> Funcs
;
63 // Get inside-chunk arguments, as well as their parent function
64 for (auto &F
: Program
)
65 if (shouldRemoveArguments(F
)) {
67 for (auto &A
: F
.args())
69 InitArgsToKeep
.push_back(&A
);
72 // We create a vector first, then convert it to a set, so that we don't have
73 // to pay the cost of rebalancing the set frequently if the order we insert
74 // the elements doesn't match the order they should appear inside the set.
75 std::set
<Argument
*> ArgsToKeep(InitArgsToKeep
.begin(), InitArgsToKeep
.end());
77 for (auto *F
: Funcs
) {
78 ValueToValueMapTy VMap
;
79 std::vector
<WeakVH
> InstToDelete
;
80 for (auto &A
: F
->args())
81 if (!ArgsToKeep
.count(&A
)) {
82 // By adding undesired arguments to the VMap, CloneFunction will remove
83 // them from the resulting Function
84 VMap
[&A
] = getDefaultValue(A
.getType());
85 for (auto *U
: A
.users())
86 if (auto *I
= dyn_cast
<Instruction
>(*&U
))
87 InstToDelete
.push_back(I
);
89 // Delete any (unique) instruction that uses the argument
90 for (Value
*V
: InstToDelete
) {
93 auto *I
= cast
<Instruction
>(V
);
94 I
->replaceAllUsesWith(getDefaultValue(I
->getType()));
95 if (!I
->isTerminator())
99 // No arguments to reduce
103 std::set
<int> ArgIndexesToKeep
;
104 for (const auto &[Index
, Arg
] : enumerate(F
->args()))
105 if (ArgsToKeep
.count(&Arg
))
106 ArgIndexesToKeep
.insert(Index
);
108 auto *ClonedFunc
= CloneFunction(F
, VMap
);
109 // In order to preserve function order, we move Clone after old Function
110 ClonedFunc
->removeFromParent();
111 Program
.getFunctionList().insertAfter(F
->getIterator(), ClonedFunc
);
113 replaceFunctionCalls(*F
, *ClonedFunc
, ArgIndexesToKeep
);
114 // Rename Cloned Function to Old's name
115 std::string FName
= std::string(F
->getName());
116 F
->replaceAllUsesWith(ConstantExpr::getBitCast(ClonedFunc
, F
->getType()));
117 F
->eraseFromParent();
118 ClonedFunc
->setName(FName
);
122 void llvm::reduceArgumentsDeltaPass(TestRunner
&Test
) {
123 runDeltaPass(Test
, extractArgumentsFromModule
, "Reducing Arguments");