ReduceArguments.cpp 4.6 KB

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