Files
revng-revng/lib/Dump/CollectCFG.cpp
T
Alessandro Di Federico 7a045d0c0d Drop revamb-dump in favor of plain passes
This commit does the following:

* It drops `revamb-dump` and transforms all the passes it featured in
  passes that can be used directly from `opt`.
* It rename `revamb` to `revng-lift`.
* It introduces a script called `revng` which acts as a driver for the
  whole rev.ng project. It replaces `translate`, `revcc`,
  `csv-to-ld-options` and `revamb-dump`, since it offers an `opt`
  subcommand which allows to easily invoke all the analysis passes.
* It makes the project a CMake package that can be easily used
  externally.
* It allows to easily create libraries of analysis to use through
  `revng-opt`.
2019-01-18 15:18:47 +01:00

94 lines
2.5 KiB
C++

/// \file collectcfg.cpp
/// \brief Implementation of the pass to collect the CFG in a readable format.
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// LLVM includes
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
// Local libraries includes
#include "revng/ADT/Queue.h"
#include "revng/Dump/CollectCFG.h"
#include "revng/Support/CommandLine.h"
#include "revng/Support/IRHelpers.h"
using namespace llvm;
using namespace llvm::cl;
char CollectCFG::ID = 0;
using RegisterCCFG = RegisterPass<CollectCFG>;
static RegisterCCFG X("collect-cfg", "Collect CFG Pass", true, true);
static opt<std::string> OutputPath("collect-cfg-output",
desc("Destination path for the Collect CFG "
"Pass"),
value_desc("path"),
cat(MainCategory));
void CollectCFG::serialize(std::ostream &Output) {
Output << "source,destination\n";
for (auto &P : Result) {
BasicBlock *Source = P.first;
std::sort(P.second.begin(), P.second.end(), CompareByName<BasicBlock>());
for (BasicBlock *Destination : P.second)
Output << Source->getName().data() << "," << Destination->getName().data()
<< "\n";
}
}
bool CollectCFG::isNewInstruction(BasicBlock *BB) {
if (BB->empty())
return false;
auto *Call = dyn_cast<CallInst>(&*BB->begin());
if (Call == nullptr || Call->getCalledFunction() == nullptr
|| Call->getCalledFunction()->getName() != "newpc")
return false;
return true;
}
bool CollectCFG::runOnModule(Module &M) {
Function &F = *M.getFunction("root");
Result.clear();
for (BasicBlock &BB : F) {
if (!isNewInstruction(&BB))
BlackList.insert(&BB);
else
break;
}
// For each basic block
for (BasicBlock &BB : F) {
if (!isNewInstruction(&BB))
continue;
OnceQueue<BasicBlock *> Queue;
Queue.insert(&BB);
while (!Queue.empty()) {
BasicBlock *ToExplore = Queue.pop();
for (BasicBlock *Successor : successors(ToExplore)) {
// If it's a new instruction register it, otherwise enqueue the basic
// block for further processing
if (isNewInstruction(Successor)) {
Result[&BB].push_back(Successor);
} else if (BlackList.count(Successor) == 0) {
Queue.insert(Successor);
}
}
}
}
if (OutputPath.getNumOccurrences() == 1) {
std::ofstream Output;
serialize(pathToStream(OutputPath, Output));
}
return false;
}