-
Notifications
You must be signed in to change notification settings - Fork 945
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bin2llvmir: add register localization pass * bin2llvmir/value_protect: better protectRegisters() implementation. * bin2llvmir: implement register localization. * scripts/retdec-config.py: remove some LLVM passes. * CHANGELOG.md: fix #652, add entry for #652.
- Loading branch information
1 parent
dee7018
commit 5659ad0
Showing
7 changed files
with
290 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
include/retdec/bin2llvmir/optimizations/register_localization/register_localization.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @file include/retdec/bin2llvmir/optimizations/register_localization/register_localization.h | ||
* @brief Make all registers local. | ||
* @copyright (c) 2019 Avast Software, licensed under the MIT license | ||
*/ | ||
|
||
#ifndef RETDEC_BIN2LLVMIR_OPTIMIZATIONS_REGISTER_LOCALIZATION_REGISTER_LOCALIZATION_H | ||
#define RETDEC_BIN2LLVMIR_OPTIMIZATIONS_REGISTER_LOCALIZATION_REGISTER_LOCALIZATION_H | ||
|
||
#include <llvm/IR/Function.h> | ||
#include <llvm/IR/Module.h> | ||
#include <llvm/Pass.h> | ||
|
||
#include "retdec/bin2llvmir/providers/abi/abi.h" | ||
#include "retdec/bin2llvmir/providers/config.h" | ||
|
||
namespace retdec { | ||
namespace bin2llvmir { | ||
|
||
class RegisterLocalization : public llvm::ModulePass | ||
{ | ||
public: | ||
static char ID; | ||
RegisterLocalization(); | ||
virtual bool runOnModule(llvm::Module& M) override; | ||
bool runOnModuleCustom(llvm::Module& M, Abi* a, Config* c); | ||
|
||
private: | ||
bool run(); | ||
llvm::AllocaInst* getLocalized( | ||
llvm::GlobalVariable* reg, | ||
llvm::Function* fnc, | ||
std::map<llvm::Function*, llvm::AllocaInst*>& fnc2alloca); | ||
bool localize( | ||
llvm::GlobalVariable* reg, | ||
std::map<llvm::Function*, llvm::AllocaInst*>& fnc2alloca, | ||
llvm::Instruction* insn); | ||
|
||
private: | ||
llvm::Module* _module = nullptr; | ||
Abi* _abi = nullptr; | ||
Config* _config = nullptr; | ||
static std::map<llvm::Type*, llvm::Function*> _type2fnc; | ||
}; | ||
|
||
} // namespace bin2llvmir | ||
} // namespace retdec | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/bin2llvmir/optimizations/register_localization/register_localization.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/** | ||
* @file src/bin2llvmir/optimizations/register_localization/register_localization.cpp | ||
* @brief Make all registers local. | ||
* @copyright (c) 2019 Avast Software, licensed under the MIT license | ||
*/ | ||
|
||
#include <cassert> | ||
|
||
#include <llvm/IR/Instruction.h> | ||
#include <llvm/IR/Instructions.h> | ||
#include <llvm/IR/InstIterator.h> | ||
|
||
#include "retdec/bin2llvmir/optimizations/register_localization/register_localization.h" | ||
#include "retdec/bin2llvmir/providers/names.h" | ||
#include "retdec/bin2llvmir/utils/debug.h" | ||
#include "retdec/bin2llvmir/utils/ir_modifier.h" | ||
#include "retdec/bin2llvmir/utils/llvm.h" | ||
|
||
using namespace retdec::utils; | ||
using namespace llvm; | ||
|
||
namespace retdec { | ||
namespace bin2llvmir { | ||
|
||
char RegisterLocalization::ID = 0; | ||
|
||
std::map<llvm::Type*, llvm::Function*> RegisterLocalization::_type2fnc; | ||
|
||
static RegisterPass<RegisterLocalization> X( | ||
"register-localization", | ||
"Make all registers local", | ||
false, // Only looks at CFG | ||
false // Analysis Pass | ||
); | ||
|
||
RegisterLocalization::RegisterLocalization() : | ||
ModulePass(ID) | ||
{ | ||
|
||
} | ||
|
||
bool RegisterLocalization::runOnModule(Module& M) | ||
{ | ||
_module = &M; | ||
_abi = AbiProvider::getAbi(_module); | ||
_config = ConfigProvider::getConfig(_module); | ||
return run(); | ||
} | ||
|
||
bool RegisterLocalization::runOnModuleCustom(llvm::Module& M, Abi* a, Config* c) | ||
{ | ||
_module = &M; | ||
_abi = a; | ||
_config = c; | ||
return run(); | ||
} | ||
|
||
/** | ||
* @return @c True if module @a _module was modified in any way, | ||
* @c false otherwise. | ||
*/ | ||
bool RegisterLocalization::run() | ||
{ | ||
if (_abi == nullptr || _config == nullptr) | ||
{ | ||
return false; | ||
} | ||
const auto& regs = _abi->getRegisters(); | ||
|
||
bool changed = false; | ||
|
||
for (GlobalVariable* reg : regs) | ||
{ | ||
std::map<Function*, AllocaInst*> fnc2alloca; | ||
|
||
for (auto uIt = reg->user_begin(); uIt != reg->user_end(); ) | ||
{ | ||
User* user = *uIt; | ||
++uIt; | ||
|
||
if (auto* insn = dyn_cast<Instruction>(user)) | ||
{ | ||
changed = localize(reg, fnc2alloca, insn); | ||
} | ||
else if (auto* expr = dyn_cast<ConstantExpr>(user)) | ||
{ | ||
for (auto euIt = expr->user_begin(); euIt != expr->user_end(); ) | ||
{ | ||
User* euser = *euIt; | ||
++euIt; | ||
|
||
if (auto* insn = dyn_cast<Instruction>(euser)) | ||
{ | ||
auto* einsn = expr->getAsInstruction(); | ||
einsn->insertBefore(insn); | ||
|
||
if (localize(reg, fnc2alloca, einsn)) | ||
{ | ||
insn->replaceUsesOfWith(expr, einsn); | ||
changed = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return changed; | ||
} | ||
|
||
llvm::AllocaInst* RegisterLocalization::getLocalized( | ||
llvm::GlobalVariable* reg, | ||
llvm::Function* fnc, | ||
std::map<llvm::Function*, llvm::AllocaInst*>& fnc2alloca) | ||
{ | ||
auto fIt = fnc2alloca.find(fnc); | ||
if (fIt != fnc2alloca.end()) | ||
{ | ||
return fIt->second; | ||
} | ||
else if (!fnc->empty() && !fnc->front().empty()) | ||
{ | ||
auto* a = new AllocaInst( | ||
reg->getValueType(), | ||
reg->getAddressSpace(), | ||
nullptr, | ||
reg->getName(), | ||
&fnc->front().front()); | ||
fnc2alloca.emplace(fnc, a); | ||
return a; | ||
} | ||
else | ||
{ | ||
// Should not really happen. | ||
return nullptr; | ||
} | ||
} | ||
|
||
bool RegisterLocalization::localize( | ||
llvm::GlobalVariable* reg, | ||
std::map<llvm::Function*, llvm::AllocaInst*>& fnc2alloca, | ||
llvm::Instruction* insn) | ||
{ | ||
AllocaInst* localized = getLocalized( | ||
reg, | ||
insn->getFunction(), | ||
fnc2alloca); | ||
|
||
if (localized == nullptr) | ||
{ | ||
return false; | ||
} | ||
|
||
insn->replaceUsesOfWith(reg, localized); | ||
return true; | ||
} | ||
|
||
} // namespace bin2llvmir | ||
} // namespace retdec |
Oops, something went wrong.