Files
revng-revng/lib/FunctionCallIdentification/PruneRetSuccessors.cpp
Antonio Frighetto 99274f1a41 GCBI: create wrappers to support old and new PM
The GeneratedCodeBasicInfo class has been disentangled in order to
create wrappers to support both the legacy and the new pass manager.
2021-01-28 17:03:31 +01:00

48 lines
1.4 KiB
C++

/// \file PruneRetSuccessors.cpp
/// \brief Remove successors from return instructions
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Local libraries includes
#include "revng/FunctionCallIdentification/PruneRetSuccessors.h"
#include "revng/Support/Debug.h"
using namespace llvm;
char PruneRetSuccessors::ID = 0;
using Register = RegisterPass<PruneRetSuccessors>;
static Register X("prs", "Prune Ret Successors", true, true);
bool PruneRetSuccessors::runOnModule(llvm::Module &M) {
auto &GCBI = getAnalysis<GeneratedCodeBasicInfoWrapperPass>().getGCBI();
auto &FCI = getAnalysis<FunctionCallIdentification>();
for (BasicBlock &BB : *GCBI.root()) {
if (BB.getTerminator()->getNumSuccessors() < 2)
continue;
auto Successors = GCBI.getSuccessors(&BB);
if (not Successors.UnexpectedPC or Successors.Other)
continue;
revng_assert(not Successors.AnyPC);
bool AllFallthrough = true;
for (MetaAddress SuccessorMA : Successors.Addresses) {
if (not FCI.isFallthrough(SuccessorMA)) {
AllFallthrough = false;
}
}
if (AllFallthrough) {
Instruction *OldTerminator = BB.getTerminator();
auto *NewTerminator = BranchInst::Create(GCBI.dispatcher(), &BB);
NewTerminator->copyMetadata(*OldTerminator);
OldTerminator->eraseFromParent();
}
}
return true;
}