2017-09-01 00:06:09 +02:00
|
|
|
//===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
|
2016-04-23 00:06:11 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-09-01 00:06:09 +02:00
|
|
|
//
|
2016-04-23 00:06:11 +02:00
|
|
|
/// \file
|
|
|
|
/// This file implements support for a bisecting optimizations based on a
|
|
|
|
/// command line option.
|
2017-09-01 00:06:09 +02:00
|
|
|
//
|
2016-04-23 00:06:11 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/IR/OptBisect.h"
|
2017-09-01 00:06:09 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2016-04-23 00:06:11 +02:00
|
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2017-06-01 23:22:26 +02:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
2017-09-01 00:06:09 +02:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2016-04-23 00:06:11 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-09-01 00:06:09 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
2016-04-23 00:06:11 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
|
2017-09-01 00:06:09 +02:00
|
|
|
cl::init(std::numeric_limits<int>::max()),
|
|
|
|
cl::Optional,
|
2016-04-23 00:06:11 +02:00
|
|
|
cl::desc("Maximum optimization to perform"));
|
|
|
|
|
|
|
|
OptBisect::OptBisect() {
|
2017-09-01 00:06:09 +02:00
|
|
|
BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
|
2016-04-23 00:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printPassMessage(const StringRef &Name, int PassNum,
|
|
|
|
StringRef TargetDesc, bool Running) {
|
|
|
|
StringRef Status = Running ? "" : "NOT ";
|
|
|
|
errs() << "BISECT: " << Status << "running pass "
|
|
|
|
<< "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getDescription(const Module &M) {
|
|
|
|
return "module (" + M.getName().str() + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getDescription(const Function &F) {
|
|
|
|
return "function (" + F.getName().str() + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getDescription(const BasicBlock &BB) {
|
|
|
|
return "basic block (" + BB.getName().str() + ") in function (" +
|
|
|
|
BB.getParent()->getName().str() + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getDescription(const Loop &L) {
|
2017-06-01 23:22:26 +02:00
|
|
|
// FIXME: Move into LoopInfo so we can get a better description
|
|
|
|
// (and avoid a circular dependency between IR and Analysis).
|
2016-04-23 00:06:11 +02:00
|
|
|
return "loop";
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:22:26 +02:00
|
|
|
static std::string getDescription(const Region &R) {
|
|
|
|
// FIXME: Move into RegionInfo so we can get a better description
|
|
|
|
// (and avoid a circular dependency between IR and Analysis).
|
|
|
|
return "region";
|
|
|
|
}
|
|
|
|
|
2016-04-23 00:06:11 +02:00
|
|
|
static std::string getDescription(const CallGraphSCC &SCC) {
|
2017-06-01 23:22:26 +02:00
|
|
|
// FIXME: Move into CallGraphSCCPass to avoid circular dependency between
|
|
|
|
// IR and Analysis.
|
2016-04-23 00:06:11 +02:00
|
|
|
std::string Desc = "SCC (";
|
|
|
|
bool First = true;
|
|
|
|
for (CallGraphNode *CGN : SCC) {
|
|
|
|
if (First)
|
|
|
|
First = false;
|
|
|
|
else
|
|
|
|
Desc += ", ";
|
|
|
|
Function *F = CGN->getFunction();
|
|
|
|
if (F)
|
|
|
|
Desc += F->getName();
|
|
|
|
else
|
|
|
|
Desc += "<<null function>>";
|
|
|
|
}
|
|
|
|
Desc += ")";
|
|
|
|
return Desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force instantiations.
|
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const Module &);
|
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const Function &);
|
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
|
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
|
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
|
2017-06-01 23:22:26 +02:00
|
|
|
template bool OptBisect::shouldRunPass(const Pass *, const Region &);
|
2016-04-23 00:06:11 +02:00
|
|
|
|
|
|
|
template <class UnitT>
|
|
|
|
bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
|
|
|
|
if (!BisectEnabled)
|
|
|
|
return true;
|
|
|
|
return checkPass(P->getPassName(), getDescription(U));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptBisect::checkPass(const StringRef PassName,
|
|
|
|
const StringRef TargetDesc) {
|
|
|
|
assert(BisectEnabled);
|
|
|
|
|
|
|
|
int CurBisectNum = ++LastBisectNum;
|
|
|
|
bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
|
|
|
|
printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
|
|
|
|
return ShouldRun;
|
|
|
|
}
|