blob: 3104b90f30701b530d7ffa9842893977a8c06761 [file] [log] [blame]
Eugene Zelenko3e561692017-08-31 22:06:09 +00001//===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
Andrew Kayloraa641a52016-04-22 22:06:11 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Andrew Kayloraa641a52016-04-22 22:06:11 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenko3e561692017-08-31 22:06:09 +00008//
Andrew Kayloraa641a52016-04-22 22:06:11 +00009/// \file
10/// This file implements support for a bisecting optimizations based on a
11/// command line option.
Eugene Zelenko3e561692017-08-31 22:06:09 +000012//
Andrew Kayloraa641a52016-04-22 22:06:11 +000013//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/OptBisect.h"
Eugene Zelenko3e561692017-08-31 22:06:09 +000016#include "llvm/ADT/StringRef.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000017#include "llvm/Pass.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3e561692017-08-31 22:06:09 +000020#include <cassert>
21#include <limits>
22#include <string>
Andrew Kayloraa641a52016-04-22 22:06:11 +000023
24using namespace llvm;
25
26static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
Eugene Zelenko3e561692017-08-31 22:06:09 +000027 cl::init(std::numeric_limits<int>::max()),
28 cl::Optional,
Andrew Kayloraa641a52016-04-22 22:06:11 +000029 cl::desc("Maximum optimization to perform"));
30
Fedor Sergeev98014e42018-03-27 16:57:20 +000031OptBisect::OptBisect() : OptPassGate() {
Eugene Zelenko3e561692017-08-31 22:06:09 +000032 BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
Andrew Kayloraa641a52016-04-22 22:06:11 +000033}
34
35static void printPassMessage(const StringRef &Name, int PassNum,
36 StringRef TargetDesc, bool Running) {
37 StringRef Status = Running ? "" : "NOT ";
38 errs() << "BISECT: " << Status << "running pass "
39 << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
40}
41
Richard Trieub37a70f2019-02-28 04:00:55 +000042bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
43 assert(BisectEnabled);
Andrew Kayloraa641a52016-04-22 22:06:11 +000044
Richard Trieub37a70f2019-02-28 04:00:55 +000045 return checkPass(P->getPassName(), IRDescription);
Andrew Kayloraa641a52016-04-22 22:06:11 +000046}
47
48bool OptBisect::checkPass(const StringRef PassName,
49 const StringRef TargetDesc) {
50 assert(BisectEnabled);
51
52 int CurBisectNum = ++LastBisectNum;
53 bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
54 printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
55 return ShouldRun;
56}