Files
revng-revng/lib/Support/BasicBlockID.cpp
T
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

60 lines
1.5 KiB
C++

/// \file BasicBlockID.cpp
/// \brief Implementation of BasicBlockID.
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "revng/Support/BasicBlockID.h"
#include "revng/Support/IRHelpers.h"
static const char *BlockIDUniqueStringID = "block-id";
BasicBlockID BasicBlockID::fromString(llvm::StringRef Text) {
using namespace llvm;
SmallVector<StringRef, 2> Splitted;
Text.split(Splitted, "-");
if (not(Splitted.size() == 1 or Splitted.size() == 2))
return BasicBlockID::invalid();
auto Start = MetaAddress::fromString(Splitted[0]);
if (not Start.isValid())
return BasicBlockID::invalid();
uint64_t InliningIndex = 0;
if (Splitted.size() == 2) {
APInt APIndex;
bool Failure = Splitted[1].getAsInteger(10, APIndex);
if (Failure or APIndex.getBitWidth() > 64)
return BasicBlockID::invalid();
InliningIndex = APIndex.getLimitedValue();
}
return BasicBlockID(Start, InliningIndex);
}
std::string BasicBlockID::toString() const {
std::string Result = Start.toString();
if (isInlined()) {
Result += "-" + llvm::Twine(InliningIndex).str();
}
return Result;
}
BasicBlockID BasicBlockID::fromValue(llvm::Value *V) {
return BasicBlockID::fromString(extractFromConstantStringPtr(V));
}
llvm::Constant *BasicBlockID::toValue(llvm::Module *M) const {
return getUniqueString(M, BlockIDUniqueStringID, toString());
}