Skip to content

Commit

Permalink
[feat] Removed unwanted calls pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Columpio authored and misonijnik committed Oct 25, 2023
1 parent b4fbe6d commit 6569119
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/Module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#===------------------------------------------------------------------------===#
set(KLEE_MODULE_COMPONENT_SRCS
CallSplitter.cpp
CallRemover.cpp
Checks.cpp
CodeGraphInfo.cpp
FunctionAlias.cpp
Expand Down
36 changes: 36 additions & 0 deletions lib/Module/CallRemover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===-- CallRemover.cpp----------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Passes.h"
#include <unordered_set>

namespace klee {

using namespace llvm;

char CallRemover::ID;

bool CallRemover::runOnModule(llvm::Module &M) {
std::vector<std::string> badFuncs = {"llvm.dbg.declare", "llvm.dbg.label"};

for (const auto &f : badFuncs) {
auto Declare = M.getFunction(f);
if (!Declare)
continue;
while (!Declare->use_empty()) {
auto CI = cast<CallInst>(Declare->user_back());
assert(CI->use_empty() && "deleted function must have void result");
CI->eraseFromParent();
}
Declare->eraseFromParent();
}

return true;
}
} // namespace klee
8 changes: 4 additions & 4 deletions lib/Module/CallSplitter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//===-- CallSplitter.cpp
//-------------------------------------------------------===//
//===-- CallSplitter.cpp --------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
Expand Down Expand Up @@ -46,8 +45,9 @@ bool CallSplitter::runOnFunction(Function &F) {
if (callInst != firstInst) {
fbb = fbb->splitBasicBlock(callInst);
}
if (isa<BranchInst>(afterCallInst) &&
cast<BranchInst>(afterCallInst)->isUnconditional()) {
if ((isa<BranchInst>(afterCallInst) &&
cast<BranchInst>(afterCallInst)->isUnconditional()) ||
isa<UnreachableInst>(afterCallInst)) {
break;
}
fbb = fbb->splitBasicBlock(afterCallInst);
Expand Down
7 changes: 7 additions & 0 deletions lib/Module/KModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ cl::opt<bool>
cl::desc("Split each call in own basic block (default=true)"),
cl::init(true), cl::cat(klee::ModuleCat));

static cl::opt<bool>
StripUnwantedCalls("strip-unwanted-calls",
cl::desc("Strip all unwanted calls (llvm.dbg.* stuff)"),
cl::init(false), cl::cat(klee::ModuleCat));

cl::opt<bool> SplitReturns(
"split-returns",
cl::desc("Split each return in own basic block (default=true)"),
Expand Down Expand Up @@ -332,6 +337,8 @@ void KModule::optimiseAndPrepare(
pm3.add(createScalarizerPass());
pm3.add(new PhiCleanerPass());
pm3.add(new FunctionAliasPass());
if (StripUnwantedCalls)
pm3.add(new CallRemover());
if (SplitCalls) {
pm3.add(new CallSplitter());
}
Expand Down
7 changes: 0 additions & 7 deletions lib/Module/Optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ static cl::opt<bool>
cl::desc("Strip debugger symbol info from executable"),
cl::init(false), cl::cat(klee::ModuleCat));

static cl::opt<bool>
StripDebugDeclare("strip-debug-declare",
cl::desc("Strip all llvm.dbg.declare intrinsics"),
cl::init(true), cl::cat(klee::ModuleCat));

static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
cl::aliasopt(StripDebug));

Expand All @@ -103,8 +98,6 @@ static void AddStandardCompilePasses(legacy::PassManager &PM) {
// If the -strip-debug command line option was specified, do it.
if (StripDebug)
addPass(PM, createStripSymbolsPass(true));
if (StripDebugDeclare)
addPass(PM, createStripDebugDeclarePass());

addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code
addPass(PM, createPromoteMemoryToRegisterPass()); // Kill useless allocas
Expand Down
8 changes: 8 additions & 0 deletions lib/Module/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ class CallSplitter : public llvm::FunctionPass {
bool runOnFunction(llvm::Function &F) override;
};

/// Remove unwanted calls
class CallRemover : public llvm::ModulePass {
public:
static char ID;
CallRemover() : llvm::ModulePass(ID) {}
bool runOnModule(llvm::Module &M) override;
};

class ReturnSplitter : public llvm::FunctionPass {
public:
static char ID;
Expand Down

0 comments on commit 6569119

Please sign in to comment.