ReduceArguments.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 declared and defined functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ReduceArguments.h"
  14. #include "Delta.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/IR/Intrinsics.h"
  17. #include <set>
  18. #include <vector>
  19. using namespace llvm;
  20. /// Goes over OldF calls and replaces them with a call to NewF
  21. static void replaceFunctionCalls(Function &OldF, Function &NewF,
  22. const std::set<int> &ArgIndexesToKeep) {
  23. const auto &Users = OldF.users();
  24. for (auto I = Users.begin(), E = Users.end(); I != E; )
  25. if (auto *CI = dyn_cast<CallInst>(*I++)) {
  26. // Skip uses in call instructions where OldF isn't the called function
  27. // (e.g. if OldF is an argument of the call).
  28. if (CI->getCalledFunction() != &OldF)
  29. continue;
  30. SmallVector<Value *, 8> Args;
  31. for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
  32. if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
  33. Args.push_back(*ArgI);
  34. CallInst *NewCI = CallInst::Create(&NewF, Args);
  35. NewCI->setCallingConv(NewF.getCallingConv());
  36. if (!CI->use_empty())
  37. CI->replaceAllUsesWith(NewCI);
  38. ReplaceInstWithInst(CI, NewCI);
  39. }
  40. }
  41. /// Returns whether or not this function should be considered a candidate for
  42. /// argument removal. Currently, functions with no arguments and intrinsics are
  43. /// not considered. Intrinsics aren't considered because their signatures are
  44. /// fixed.
  45. static bool shouldRemoveArguments(const Function &F) {
  46. return !F.arg_empty() && !F.isIntrinsic();
  47. }
  48. /// Removes out-of-chunk arguments from functions, and modifies their calls
  49. /// accordingly. It also removes allocations of out-of-chunk arguments.
  50. static void extractArgumentsFromModule(Oracle &O, Module &Program) {
  51. std::vector<Argument *> InitArgsToKeep;
  52. std::vector<Function *> Funcs;
  53. // Get inside-chunk arguments, as well as their parent function
  54. for (auto &F : Program)
  55. if (shouldRemoveArguments(F)) {
  56. Funcs.push_back(&F);
  57. for (auto &A : F.args())
  58. if (O.shouldKeep())
  59. InitArgsToKeep.push_back(&A);
  60. }
  61. // We create a vector first, then convert it to a set, so that we don't have
  62. // to pay the cost of rebalancing the set frequently if the order we insert
  63. // the elements doesn't match the order they should appear inside the set.
  64. std::set<Argument *> ArgsToKeep(InitArgsToKeep.begin(), InitArgsToKeep.end());
  65. for (auto *F : Funcs) {
  66. ValueToValueMapTy VMap;
  67. std::vector<WeakVH> InstToDelete;
  68. for (auto &A : F->args())
  69. if (!ArgsToKeep.count(&A)) {
  70. // By adding undesired arguments to the VMap, CloneFunction will remove
  71. // them from the resulting Function
  72. VMap[&A] = UndefValue::get(A.getType());
  73. for (auto *U : A.users())
  74. if (auto *I = dyn_cast<Instruction>(*&U))
  75. InstToDelete.push_back(I);
  76. }
  77. // Delete any (unique) instruction that uses the argument
  78. for (Value *V : InstToDelete) {
  79. if (!V)
  80. continue;
  81. auto *I = cast<Instruction>(V);
  82. I->replaceAllUsesWith(UndefValue::get(I->getType()));
  83. if (!I->isTerminator())
  84. I->eraseFromParent();
  85. }
  86. // No arguments to reduce
  87. if (VMap.empty())
  88. continue;
  89. std::set<int> ArgIndexesToKeep;
  90. for (auto &Arg : enumerate(F->args()))
  91. if (ArgsToKeep.count(&Arg.value()))
  92. ArgIndexesToKeep.insert(Arg.index());
  93. auto *ClonedFunc = CloneFunction(F, VMap);
  94. // In order to preserve function order, we move Clone after old Function
  95. ClonedFunc->removeFromParent();
  96. Program.getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
  97. replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
  98. // Rename Cloned Function to Old's name
  99. std::string FName = std::string(F->getName());
  100. F->replaceAllUsesWith(ConstantExpr::getBitCast(ClonedFunc, F->getType()));
  101. F->eraseFromParent();
  102. ClonedFunc->setName(FName);
  103. }
  104. }
  105. void llvm::reduceArgumentsDeltaPass(TestRunner &Test) {
  106. outs() << "*** Reducing Arguments...\n";
  107. runDeltaPass(Test, extractArgumentsFromModule);
  108. }