Files
revng-revng/lib/FunctionIsolation/RemoveExceptionalCalls.cpp
T
Alessandro Di Federico 0c671ea7ae Introduce RemoveExceptionalCalls
RemoveExceptionalCalls is a simple pass whose goal is to drop all the
calls to functions marked as `Exceptional` and replace them with an
`UnrechableInst`.

This is mainly useful in the decompilation pipeline.
2021-04-30 14:52:25 +02:00

62 lines
1.7 KiB
C++

/// \file RemoveExceptionalCalls.cpp
/// \brief
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "revng/FunctionIsolation/RemoveExceptionalCalls.h"
#include "revng/Support/FunctionTags.h"
#include "revng/Support/IRHelpers.h"
using namespace llvm;
char RemoveExceptionalCalls::ID = 0;
using Register = RegisterPass<RemoveExceptionalCalls>;
static Register
X("remove-exceptional-functions", "Remove Exceptional Functions");
bool RemoveExceptionalCalls::runOnModule(llvm::Module &M) {
LLVMContext &C = M.getContext();
// Collect all calls to exceptional functions in lifted functions
std::set<CallBase *> ToErase;
for (Function &F : FunctionTags::Exceptional.functions(&M))
for (CallBase *Call : callers(&F))
if (FunctionTags::Lifted.isTagOf(Call->getParent()->getParent()))
ToErase.insert(Call);
std::set<Function *> ToCleanup;
for (CallBase *Call : ToErase) {
BasicBlock *BB = Call->getParent();
// Register function for cleanup
ToCleanup.insert(BB->getParent());
// Split containing basic block
BB->splitBasicBlock(Call);
// Drop terminator of the old basic block
BB->getTerminator()->eraseFromParent();
// Terminate with an unreachable
new UnreachableInst(C, BB);
// Drop function call
auto *Undef = UndefValue::get(Call->getType());
Call->replaceAllUsesWith(Undef);
Call->eraseFromParent();
}
// Garbage collect dead blocks
for (Function *F : ToCleanup)
EliminateUnreachableBlocks(*F, nullptr, false);
return true;
}