OptBisect.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/ADT/StringRef.h"
  16. #include "llvm/Pass.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <cassert>
  20. #include <limits>
  21. #include <string>
  22. using namespace llvm;
  23. static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
  24. cl::init(std::numeric_limits<int>::max()),
  25. cl::Optional,
  26. cl::desc("Maximum optimization to perform"));
  27. OptBisect::OptBisect() : OptPassGate() {
  28. BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
  29. }
  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 Pass *P, StringRef IRDescription) {
  37. assert(BisectEnabled);
  38. return checkPass(P->getPassName(), IRDescription);
  39. }
  40. bool OptBisect::checkPass(const StringRef PassName,
  41. const StringRef TargetDesc) {
  42. assert(BisectEnabled);
  43. int CurBisectNum = ++LastBisectNum;
  44. bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
  45. printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
  46. return ShouldRun;
  47. }
  48. ManagedStatic<OptBisect> llvm::OptBisector;