From 706c7da4c837dfb487ede162bf6dbc84a2fd3ed2 Mon Sep 17 00:00:00 2001 From: Francesco Bertolaccini Date: Fri, 26 Aug 2022 11:27:02 +0200 Subject: [PATCH] Remove temporary variables for `load`s --- include/rellic/BC/Util.h | 2 -- lib/AST/IRToASTVisitor.cpp | 4 --- lib/BC/Util.cpp | 57 -------------------------------------- lib/Decompiler.cpp | 1 - tools/xref/Xref.cpp | 1 - 5 files changed, 65 deletions(-) diff --git a/include/rellic/BC/Util.h b/include/rellic/BC/Util.h index 2435395a..a08e5aa2 100644 --- a/include/rellic/BC/Util.h +++ b/include/rellic/BC/Util.h @@ -67,6 +67,4 @@ void RemoveInsertValues(llvm::Module &module); // Converts by value array arguments and wraps them into a struct, so that // semantics are preserved in C void ConvertArrayArguments(llvm::Module &module); - -void FindRedundantLoads(llvm::Module &module); } // namespace rellic \ No newline at end of file diff --git a/lib/AST/IRToASTVisitor.cpp b/lib/AST/IRToASTVisitor.cpp index 1bc58662..19c872f7 100755 --- a/lib/AST/IRToASTVisitor.cpp +++ b/lib/AST/IRToASTVisitor.cpp @@ -1313,11 +1313,7 @@ void IRToASTVisitor::VisitFunctionDecl(llvm::Function &func) { fdecl->addDecl(var); } else if (inst.hasNUsesOrMore(2) || (inst.hasNUsesOrMore(1) && llvm::isa(inst)) || - llvm::isa(inst) || llvm::isa(inst)) { - if (inst.getMetadata("rellic.notemp")) { - continue; - } if (!inst.getType()->isVoidTy()) { auto GetPrefix{[&](llvm::Instruction *inst) { if (llvm::isa(inst)) { diff --git a/lib/BC/Util.cpp b/lib/BC/Util.cpp index 76052e4c..63f7dd0f 100644 --- a/lib/BC/Util.cpp +++ b/lib/BC/Util.cpp @@ -437,61 +437,4 @@ void ConvertArrayArguments(llvm::Module &m) { CHECK(VerifyModule(&m)) << "Transformation broke module correctness"; } -static void FindRedundantLoads(llvm::Function &func) { - auto HasStoreBeforeUse = [&](llvm::Value *ptr, llvm::User *user, - llvm::LoadInst *load) { - std::unordered_set visited_blocks; - std::vector work_list; - auto inst{llvm::cast(user)}; - work_list.push_back(inst); - while (!work_list.empty()) { - inst = work_list.back(); - work_list.pop_back(); - auto bb{inst->getParent()}; - visited_blocks.insert(bb); - - while (inst) { - if (auto store = llvm::dyn_cast(inst)) { - if (store->getPointerOperand() == ptr) { - return true; - } - } - if (inst == load) { - return false; - } - inst = inst->getPrevNode(); - } - for (auto pred : llvm::predecessors(bb)) { - if (!visited_blocks.count(pred)) { - work_list.push_back(pred->getTerminator()); - } - } - } - - return false; - }; - - for (auto &inst : llvm::instructions(func)) { - if (auto load = llvm::dyn_cast(&inst)) { - for (auto &use : load->uses()) { - auto ptr{load->getPointerOperand()}; - if (!llvm::isa(ptr)) { - continue; - } - if (HasStoreBeforeUse(ptr, use.getUser(), load)) { - continue; - } - load->setMetadata("rellic.notemp", - llvm::MDNode::get(func.getContext(), {})); - } - } - } -} - -void FindRedundantLoads(llvm::Module &module) { - for (auto &func : module.functions()) { - FindRedundantLoads(func); - } -} - } // namespace rellic \ No newline at end of file diff --git a/lib/Decompiler.cpp b/lib/Decompiler.cpp index 5bae24a0..001d8748 100644 --- a/lib/Decompiler.cpp +++ b/lib/Decompiler.cpp @@ -73,7 +73,6 @@ Result Decompile( ConvertArrayArguments(*module); RemoveInsertValues(*module); - FindRedundantLoads(*module); InitOptPasses(); rellic::DebugInfoCollector dic; diff --git a/tools/xref/Xref.cpp b/tools/xref/Xref.cpp index 315de3ef..c5200006 100644 --- a/tools/xref/Xref.cpp +++ b/tools/xref/Xref.cpp @@ -248,7 +248,6 @@ static void LoadModule(const httplib::Request& req, httplib::Response& res) { return; } session.Module = std::unique_ptr(mod); - rellic::FindRedundantLoads(*session.Module); llvm::json::Object msg{{"message", "Ok."}}; SendJSON(res, msg); res.status = 200;