ReduceUsingSimplifyCFG.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //===- ReduceUsingSimplifyCFG.h - 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 call SimplifyCFG on individual basic blocks.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ReduceUsingSimplifyCFG.h"
  14. #include "llvm/Analysis/TargetTransformInfo.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/Transforms/Utils/Local.h"
  18. using namespace llvm;
  19. static void reduceUsingSimplifyCFG(Oracle &O, ReducerWorkItem &WorkItem) {
  20. Module &Program = WorkItem.getModule();
  21. SmallVector<BasicBlock *, 16> ToSimplify;
  22. for (auto &F : Program)
  23. for (auto &BB : F)
  24. if (!O.shouldKeep())
  25. ToSimplify.push_back(&BB);
  26. TargetTransformInfo TTI(Program.getDataLayout());
  27. for (auto *BB : ToSimplify)
  28. simplifyCFG(BB, TTI);
  29. }
  30. void llvm::reduceUsingSimplifyCFGDeltaPass(TestRunner &Test) {
  31. runDeltaPass(Test, reduceUsingSimplifyCFG, "Reducing using SimplifyCFG");
  32. }
  33. static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem,
  34. bool Direction) {
  35. Module &M = WorkItem.getModule();
  36. SmallVector<BasicBlock *, 16> ToSimplify;
  37. for (auto &F : M) {
  38. for (auto &BB : F) {
  39. auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
  40. if (!BR || !BR->isConditional() || O.shouldKeep())
  41. continue;
  42. if (Direction)
  43. BR->setCondition(ConstantInt::getTrue(BR->getContext()));
  44. else
  45. BR->setCondition(ConstantInt::getFalse(BR->getContext()));
  46. ToSimplify.push_back(&BB);
  47. }
  48. }
  49. TargetTransformInfo TTI(M.getDataLayout());
  50. for (auto *BB : ToSimplify)
  51. simplifyCFG(BB, TTI);
  52. }
  53. void llvm::reduceConditionalsTrueDeltaPass(TestRunner &Test) {
  54. runDeltaPass(
  55. Test,
  56. [](Oracle &O, ReducerWorkItem &WorkItem) {
  57. reduceConditionals(O, WorkItem, true);
  58. },
  59. "Reducing conditional branches to true");
  60. }
  61. void llvm::reduceConditionalsFalseDeltaPass(TestRunner &Test) {
  62. runDeltaPass(
  63. Test,
  64. [](Oracle &O, ReducerWorkItem &WorkItem) {
  65. reduceConditionals(O, WorkItem, false);
  66. },
  67. "Reducing conditional branches to false");
  68. }