Files
revng-revng/lib/Yield/CrossRelations.cpp
Alessandro Di Federico f68b7866b3 Introduce BasicBlockID
This commit introduces `BasicBlockID` as the unique identifier for a
`efa::BasicBlock` into the CFG. A `BasicBlockID` is defined by a
`MetaAddress` plus an incremental integer. This enables us to have
multiple instances of the same block in a single function, which is
particularly useful when inlining multiple times the same function.

Apart from this, the commit also does the following:

* It drops representing `MetaAddress`es a `structs` in the IR. This created
  several issues related to ABI. We now represent them as strings.

* It defines more functions in `support.h`, instead of defining prototypes
  by hand in `CodeGenerator.cpp` and the like. Specifically, `unknownPC`
  and `raise_exception_helper`. We also introduce a C "constructor" for
  `PlainMetaAddress`.

* It significantly reduces the API of `GeneratedCodeBasicInfo`, which
  was supposed to be put on a diet since a long time.  Specifically,
  many jump target related methods have been moved to free functions in
  `IRHelpers.h`.  Also `GCBI::getSuccessors` has been pushed into its
  only user, `PruneRetSuccessors`, to prevent further usage of a
  deprecated API. In the future, it would be nice to drop it entirely.

* It introduces `efa::BasicBlock::InlinedFrom`.

* Introduce an enum to represent named argument indices for `newpc`.
  This enables us to more effectively manipulate its argument list.

* It improves the verification and error reporting for
  `efa::FunctionMetadata`.

* Update tests.

This commit is preliminary to another piece of work to improve the
generality of inlining beyond the simple "fake function" scenario, for
which the feature was originally conceived.
2023-02-23 14:51:10 +01:00

189 lines
7.4 KiB
C++

/// \file CallGraph.cpp
/// \brief
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include <unordered_map>
#include "revng/ADT/STLExtras.h"
#include "revng/Model/Binary.h"
#include "revng/Pipeline/Location.h"
#include "revng/Pipes/Ranks.h"
#include "revng/Yield/CallEdge.h"
#include "revng/Yield/CrossRelations/CrossRelations.h"
namespace CR = yield::crossrelations;
using MetadataContainer = SortedVector<efa::FunctionMetadata>;
CR::CrossRelations::CrossRelations(const MetadataContainer &Metadata,
const model::Binary &Binary) {
revng_assert(Metadata.size() == Binary.Functions().size());
namespace ranks = revng::ranks;
// Make sure all the functions are present.
for (auto Inserter = Relations().batch_insert();
const auto &Function : Binary.Functions()) {
const auto Location = pipeline::location(ranks::Function, Function.Entry());
Inserter.insert(CR::RelationDescription(Location.toString(), {}));
}
// Make sure all the dynamic functions are also present
for (auto Inserter = Relations().batch_insert();
const auto &Function : Binary.ImportedDynamicFunctions()) {
const auto Location = pipeline::location(ranks::DynamicFunction,
Function.OriginalName());
Inserter.insert(CR::RelationDescription(Location.toString(), {}));
}
for (const auto &[EntryAddress, ControlFlowGraph] : Metadata) {
for (const auto &BasicBlock : ControlFlowGraph) {
auto CallLocation = pipeline::location(ranks::BasicBlock,
EntryAddress,
BasicBlock.ID());
for (const auto &Edge : BasicBlock.Successors()) {
if (auto *CallEdge = llvm::dyn_cast<efa::CallEdge>(Edge.get())) {
if (efa::FunctionEdgeType::isCall(Edge->Type())) {
if (const auto &Callee = Edge->Destination(); Callee.isValid()) {
// TODO: embed information about the call instruction into
// `CallLocation` after yield starts providing it.
auto CalleeAddress = Callee.notInlinedAddress();
auto L = pipeline::location(ranks::Function, CalleeAddress)
.toString();
if (auto It = Relations().find(L); It != Relations().end()) {
CR::RelationTarget T(CR::RelationType::IsCalledFrom,
CallLocation.toString());
It->Related().insert(std::move(T));
}
} else if (!CallEdge->DynamicFunction().empty()) {
auto L = pipeline::location(ranks::DynamicFunction,
CallEdge->DynamicFunction())
.toString();
if (auto It = Relations().find(L); It != Relations().end()) {
CR::RelationTarget T(CR::RelationType::IsDynamicallyCalledFrom,
CallLocation.toString());
It->Related().insert(std::move(T));
}
} else {
// Ignore indirect calls.
}
} else {
// Ignore non-call edges.
}
}
}
}
}
}
template<typename AddNodeCallable, typename AddEdgeCallable>
static void conversionHelper(const CR::CrossRelations &Input,
const AddNodeCallable &AddNode,
const AddEdgeCallable &AddEdge) {
for (const auto &[LocationString, Related] : Input.Relations())
AddNode(LocationString);
for (const auto &[LocationString, Related] : Input.Relations()) {
for (const auto &[RelationKind, TargetString] : Related) {
switch (RelationKind) {
case CR::RelationType::IsCalledFrom:
case CR::RelationType::IsDynamicallyCalledFrom:
AddEdge(LocationString, TargetString, RelationKind);
break;
case CR::RelationType::Invalid:
case CR::RelationType::Count:
default:
revng_abort("Unknown enum value");
}
}
}
}
GenericGraph<CR::CrossRelations::Node, 16, true>
CR::CrossRelations::toCallGraph() const {
GenericGraph<CR::CrossRelations::Node, 16, true> Result;
using NodeView = decltype(Result)::Node *;
std::unordered_map<std::string_view, NodeView> LookupHelper;
auto AddNode = [&Result, &LookupHelper](std::string_view Location) {
auto *Node = Result.addNode(Location);
auto [Iterator, Success] = LookupHelper.try_emplace(Location, Node);
revng_assert(Success);
};
auto AddEdge = [&LookupHelper](std::string_view Callee,
std::string_view Caller,
RelationType::Values Kind) {
if (Kind == CR::RelationType::IsCalledFrom
|| Kind == CR::RelationType::IsDynamicallyCalledFrom) {
// This assumes all the call sites are represented as basic block
// locations for all the relations covered by these two kinds.
using namespace pipeline;
namespace ranks = revng::ranks;
auto CallerLocation = *locationFromString(ranks::BasicBlock, Caller);
auto CallerFunction = convertLocation(ranks::Function, CallerLocation);
auto *CallerNode = LookupHelper.at(CallerFunction.toString());
auto *CalleeNode = LookupHelper.at(Callee);
using EdgeLabel = CR::CrossRelations::EdgeLabel;
if (!llvm::is_contained(CallerNode->successors(), CalleeNode))
CallerNode->addSuccessor(CalleeNode, EdgeLabel{ Kind });
} else {
revng_abort("Unsupported relation type.");
}
};
conversionHelper(*this, AddNode, AddEdge);
return Result;
}
yield::Graph CR::CrossRelations::toYieldGraph() const {
yield::Graph Result;
std::map<BasicBlockID, yield::Graph::Node *> LookupHelper;
namespace ranks = revng::ranks;
using namespace pipeline;
auto AddNode = [&Result, &LookupHelper](std::string_view Location) {
auto MaybeKey = genericLocationFromString<0>(Location,
ranks::Function,
ranks::BasicBlock,
ranks::Instruction);
// TODO: extend to support dynamic functions
if (MaybeKey.has_value()) {
auto Address = BasicBlockID(std::get<0>(MaybeKey.value()));
auto [_, Success] = LookupHelper.try_emplace(Address,
Result.addNode(Address));
revng_assert(Success);
}
};
auto AddEdge = [&LookupHelper](std::string_view FromLocation,
std::string_view ToLocation,
CR::RelationType::Values Kind) {
if (Kind == CR::RelationType::IsCalledFrom) {
using namespace pipeline;
auto GetBlockID = [](llvm::StringRef Location) -> BasicBlockID {
auto MaybeAddress = genericLocationFromString<0>(Location,
ranks::Function,
ranks::BasicBlock,
ranks::Instruction);
revng_assert(MaybeAddress);
return BasicBlockID(std::get<0>(*MaybeAddress));
};
auto *FromNode = LookupHelper.at(GetBlockID(FromLocation));
auto *ToNode = LookupHelper.at(GetBlockID(ToLocation));
if (!llvm::is_contained(FromNode->successors(), ToNode))
FromNode->addSuccessor(ToNode);
}
};
conversionHelper(*this, AddNode, AddEdge);
return Result;
}