OptBisect.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
  21. cl::init(OptBisect::Disabled), cl::Optional,
  22. cl::cb<void, int>([](int Limit) {
  23. llvm::OptBisector->setLimit(Limit);
  24. }),
  25. cl::desc("Maximum optimization to perform"));
  26. static void printPassMessage(const StringRef &Name, int PassNum,
  27. StringRef TargetDesc, bool Running) {
  28. StringRef Status = Running ? "" : "NOT ";
  29. errs() << "BISECT: " << Status << "running pass "
  30. << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
  31. }
  32. bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
  33. assert(isEnabled());
  34. return checkPass(P->getPassName(), IRDescription);
  35. }
  36. bool OptBisect::checkPass(const StringRef PassName,
  37. const StringRef TargetDesc) {
  38. assert(isEnabled());
  39. int CurBisectNum = ++LastBisectNum;
  40. bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
  41. printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
  42. return ShouldRun;
  43. }
  44. const int OptBisect::Disabled;
  45. ManagedStatic<OptBisect> llvm::OptBisector;