99 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9/// \file10/// This file implements support for a bisecting optimizations based on a11/// command line option.12//13//===----------------------------------------------------------------------===//14 15#include "llvm/IR/OptBisect.h"16#include "llvm/Pass.h"17#include "llvm/Support/CommandLine.h"18#include "llvm/Support/raw_ostream.h"19#include <cassert>20 21using namespace llvm;22 23static OptBisect &getOptBisector() {24 static OptBisect OptBisector;25 return OptBisector;26}27 28static OptDisable &getOptDisabler() {29 static OptDisable OptDisabler;30 return OptDisabler;31}32 33static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,34 cl::init(OptBisect::Disabled), cl::Optional,35 cl::cb<void, int>([](int Limit) {36 getOptBisector().setLimit(Limit);37 }),38 cl::desc("Maximum optimization to perform"));39 40static cl::opt<bool> OptBisectVerbose(41 "opt-bisect-verbose",42 cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden,43 cl::init(true), cl::Optional);44 45static cl::list<std::string> OptDisablePasses(46 "opt-disable", cl::Hidden, cl::CommaSeparated, cl::Optional,47 cl::cb<void, std::string>([](const std::string &Pass) {48 getOptDisabler().setDisabled(Pass);49 }),50 cl::desc("Optimization pass(es) to disable (comma-separated list)"));51 52static cl::opt<bool>53 OptDisableVerbose("opt-disable-enable-verbosity",54 cl::desc("Show verbose output when opt-disable is set"),55 cl::Hidden, cl::init(false), cl::Optional);56 57static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc,58 bool Running) {59 StringRef Status = Running ? "" : "NOT ";60 errs() << "BISECT: " << Status << "running pass (" << PassNum << ") " << Name61 << " on " << TargetDesc << '\n';62}63 64bool OptBisect::shouldRunPass(StringRef PassName,65 StringRef IRDescription) const {66 assert(isEnabled());67 68 int CurBisectNum = ++LastBisectNum;69 bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);70 if (OptBisectVerbose)71 printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);72 return ShouldRun;73}74 75static void printDisablePassMessage(const StringRef &Name, StringRef TargetDesc,76 bool Running) {77 StringRef Status = Running ? "" : "NOT ";78 dbgs() << "OptDisable: " << Status << "running pass " << Name << " on "79 << TargetDesc << "\n";80}81 82void OptDisable::setDisabled(StringRef Pass) { DisabledPasses.insert(Pass); }83 84bool OptDisable::shouldRunPass(StringRef PassName,85 StringRef IRDescription) const {86 assert(isEnabled());87 88 const bool ShouldRun = !DisabledPasses.contains(PassName);89 if (OptDisableVerbose)90 printDisablePassMessage(PassName, IRDescription, ShouldRun);91 return ShouldRun;92}93 94OptPassGate &llvm::getGlobalPassGate() {95 if (getOptDisabler().isEnabled())96 return getOptDisabler();97 return getOptBisector();98}99