OptBisect.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
  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. /// \file
  10. /// This file implements support for a bisecting optimizations based on a
  11. /// command line option.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/OptBisect.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <cassert>
  19. using namespace llvm;
  20. static OptBisect &getOptBisector() {
  21. static OptBisect OptBisector;
  22. return OptBisector;
  23. }
  24. static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
  25. cl::init(OptBisect::Disabled), cl::Optional,
  26. cl::cb<void, int>([](int Limit) {
  27. getOptBisector().setLimit(Limit);
  28. }),
  29. cl::desc("Maximum optimization to perform"));
  30. static void printPassMessage(const StringRef &Name, int PassNum,
  31. StringRef TargetDesc, bool Running) {
  32. StringRef Status = Running ? "" : "NOT ";
  33. errs() << "BISECT: " << Status << "running pass "
  34. << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
  35. }
  36. bool OptBisect::shouldRunPass(const StringRef PassName,
  37. StringRef IRDescription) {
  38. assert(isEnabled());
  39. int CurBisectNum = ++LastBisectNum;
  40. bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
  41. printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
  42. return ShouldRun;
  43. }
  44. const int OptBisect::Disabled;
  45. OptPassGate &llvm::getGlobalPassGate() { return getOptBisector(); }