Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce static data #787

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/retdec/bin2llvmir/analyses/reaching_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Use
class BasicBlockEntry
{
public:
BasicBlockEntry(const llvm::BasicBlock* b = nullptr);
BasicBlockEntry(const llvm::BasicBlock* b = nullptr, std::size_t _id = 0);

std::string getName() const;
friend std::ostream& operator<<(
Expand Down Expand Up @@ -115,7 +115,6 @@ class BasicBlockEntry

private:
unsigned id;
static int newUID;
};

class ReachingDefinitionsAnalysis
Expand Down
1 change: 1 addition & 0 deletions include/retdec/bin2llvmir/analyses/symbolic_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class SymbolicTree
// Global SymbolicTree configuration methods and data.
//
public:
static void clear();
static bool isVal2ValMapUsed();
static void setAbi(Abi* abi);
static void setConfig(Config* config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class Collector
bool extractFormatString(CallEntry* ce) const;

bool storesString(llvm::StoreInst* si, std::string& str) const;
llvm::Value* getRoot(llvm::Value* i, bool first = true) const;
llvm::Value* getRoot(llvm::Value* i) const;
llvm::Value* _getRoot(llvm::Value* i, std::set<llvm::Value*>& seen) const;

protected:
const Abi* _abi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class RegisterLocalization : public llvm::ModulePass
llvm::Module* _module = nullptr;
Abi* _abi = nullptr;
Config* _config = nullptr;
static std::map<llvm::Type*, llvm::Function*> _type2fnc;
};

} // namespace bin2llvmir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ using EquationEntrySet = std::unordered_set<EquationEntry, EquationEntryHash>;
class EqSet
{
public:
EqSet();
EqSet(std::size_t id);
void insert(Config* config, llvm::Value* v, eSourcePriority p = eSourcePriority::PRIORITY_NONE);
void insert(llvm::Type* t, eSourcePriority p = eSourcePriority::PRIORITY_NONE);
void propagate(llvm::Module* module);
Expand All @@ -157,8 +157,7 @@ class EqSet

public:
/// Each instance gets its own unique ID for debug print purposes.
static unsigned newUID;
const unsigned id;
const std::size_t id;

/// Type of an entire equivalence set.
TypeEntry masterType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class CallingConventionProvider
// Destructor, singleton method.
public:
static CallingConventionProvider* getProvider();
static void clear();

// Factory methods.
public:
Expand Down
8 changes: 3 additions & 5 deletions src/bin2llvmir/analyses/reaching_definitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void ReachingDefinitionsAnalysis::initializeBasicBlocks(llvm::Function& F)
{
for (BasicBlock& B : F)
{
BasicBlockEntry bbe(&B);
BasicBlockEntry bbe(&B, bbMap.size());

int insnPos = -1;
for (Instruction& I : B)
Expand Down Expand Up @@ -385,11 +385,9 @@ std::ostream& operator<<(std::ostream& out, const ReachingDefinitionsAnalysis& r
//=============================================================================
//

int BasicBlockEntry::newUID = 0;

BasicBlockEntry::BasicBlockEntry(const llvm::BasicBlock* b) :
BasicBlockEntry::BasicBlockEntry(const llvm::BasicBlock* b, std::size_t _id) :
bb(b),
id(newUID++)
id(_id)
{

}
Expand Down
8 changes: 8 additions & 0 deletions src/bin2llvmir/analyses/symbolic_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,16 @@ bool SymbolicTree::_trackOnlyFlagRegisters = false;
bool SymbolicTree::_simplifyAtCreation = true;
unsigned SymbolicTree::_naryLimit = 3;

void SymbolicTree::clear()
{
_abi = nullptr;
_config = nullptr;
setToDefaultConfiguration();
}

void SymbolicTree::setToDefaultConfiguration()
{
_val2valUsed = false;
_trackThroughAllocaLoads = true;
_trackThroughGeneralRegisterLoads = true;
_trackOnlyFlagRegisters = false;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ std::size_t Decoder::decodeJumpTargetDryRun_arm(
auto basicMode = _c2l->getBasicMode();
if (mode != basicMode) _c2l->modifyBasicMode(mode);

static csh ce = _c2l->getCapstoneEngine();
csh ce = _c2l->getCapstoneEngine();

decodedSz = 0;
uint64_t addr = jt.getAddress();
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/arm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::size_t Decoder::decodeJumpTargetDryRun_arm64(
return true;
}

static csh ce = _c2l->getCapstoneEngine();
csh ce = _c2l->getCapstoneEngine();

uint64_t addr = jt.getAddress();
std::size_t nops = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/decoder_ranges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void RangesToDecode::removeZeroSequences(
FileImage* image,
common::AddressRangeContainer& rs)
{
static unsigned minSequence = 0x50; // TODO: Maybe should be smaller.
unsigned minSequence = 0x50; // TODO: Maybe should be smaller.
retdec::common::AddressRangeContainer toRemove;

for (auto& range : rs)
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/jump_targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const JumpTarget* JumpTargets::push(
retdec::common::Address f,
std::optional<std::size_t> sz)
{
static auto& arch = config->getConfig().architecture;
auto& arch = config->getConfig().architecture;

if (arch.isArm64() && m == CS_MODE_THUMB)
{
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::size_t Decoder::decodeJumpTargetDryRun_mips(
return true;
}

static csh ce = _c2l->getCapstoneEngine();
csh ce = _c2l->getCapstoneEngine();

uint64_t addr = jt.getAddress();
std::size_t nops = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/powerpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ std::size_t Decoder::decodeJumpTargetDryRun_ppc(
return true;
}

static csh ce = _c2l->getCapstoneEngine();
csh ce = _c2l->getCapstoneEngine();

uint64_t addr = jt.getAddress();
std::size_t nops = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/decoder/x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ std::size_t Decoder::decodeJumpTargetDryRun_x86(
return true;
}

static csh ce = _c2l->getCapstoneEngine();
csh ce = _c2l->getCapstoneEngine();

uint64_t addr = jt.getAddress();
std::size_t nops = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/inst_opt_rda/inst_opt_rda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace inst_opt_rda {
*/
bool unusedStores(llvm::Instruction* insn, ReachingDefinitionsAnalysis& RDA)
{
static auto* abi = AbiProvider::getAbi(insn->getModule());
auto* abi = AbiProvider::getAbi(insn->getModule());
auto* store = llvm::dyn_cast<llvm::StoreInst>(insn);
if (store == nullptr
|| (llvm::isa<llvm::GlobalVariable>(store->getPointerOperand())
Expand Down
19 changes: 10 additions & 9 deletions src/bin2llvmir/optimizations/param_return/collector/collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,14 @@ bool Collector::storesString(StoreInst* si, std::string& str) const
return true;
}

llvm::Value* Collector::getRoot(llvm::Value* i, bool first) const
llvm::Value* Collector::getRoot(llvm::Value* i) const
{
std::set<llvm::Value*> seen;
return _getRoot(i, seen);
}

llvm::Value* Collector::_getRoot(llvm::Value* i, std::set<llvm::Value*>& seen) const
{
static std::set<llvm::Value*> seen;
if (first)
{
seen.clear();
}
if (seen.count(i))
{
return i;
Expand All @@ -568,7 +569,7 @@ llvm::Value* Collector::getRoot(llvm::Value* i, bool first) const
auto* d = (*u->defs.begin())->def;
if (auto* s = dyn_cast<StoreInst>(d))
{
return getRoot(s->getValueOperand(), false);
return _getRoot(s->getValueOperand(), seen);
}
else
{
Expand All @@ -577,7 +578,7 @@ llvm::Value* Collector::getRoot(llvm::Value* i, bool first) const
}
else if (auto* l = dyn_cast<LoadInst>(ii))
{
return getRoot(l->getPointerOperand(), false);
return _getRoot(l->getPointerOperand(), seen);
}
else
{
Expand All @@ -586,7 +587,7 @@ llvm::Value* Collector::getRoot(llvm::Value* i, bool first) const
}
else if (auto* l = dyn_cast<LoadInst>(ii))
{
return getRoot(l->getPointerOperand(), false);
return _getRoot(l->getPointerOperand(), seen);
}
else
{
Expand Down
18 changes: 11 additions & 7 deletions src/bin2llvmir/optimizations/provider_init/provider_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "retdec/bin2llvmir/optimizations/provider_init/provider_init.h"
#include "retdec/bin2llvmir/providers/abi/abi.h"
#include "retdec/bin2llvmir/providers/asm_instruction.h"
#include "retdec/bin2llvmir/providers/calling_convention/calling_convention.h"
#include "retdec/bin2llvmir/providers/config.h"
#include "retdec/bin2llvmir/providers/debugformat.h"
#include "retdec/bin2llvmir/providers/demangler.h"
Expand Down Expand Up @@ -170,11 +171,16 @@ void ProviderInitialization::setConfig(retdec::config::Config* c)
*/
bool ProviderInitialization::runOnModule(Module& m)
{
static bool firstRun = true;
if (!firstRun)
{
return false;
}
AbiProvider::clear();
AsmInstruction::clear();
ConfigProvider::clear();
DebugFormatProvider::clear();
DemanglerProvider::clear();
FileImageProvider::clear();
LtiProvider::clear();
NamesProvider::clear();
SymbolicTree::clear();
CallingConventionProvider::clear();

// Config.
//
Expand Down Expand Up @@ -397,8 +403,6 @@ bool ProviderInitialization::runOnModule(Module& m)

AsmInstruction::clear();

firstRun = false;

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace bin2llvmir {

char RegisterLocalization::ID = 0;

std::map<llvm::Type*, llvm::Function*> RegisterLocalization::_type2fnc;

static RegisterPass<RegisterLocalization> X(
"retdec-register-localization",
"Make all registers local",
Expand Down
15 changes: 8 additions & 7 deletions src/bin2llvmir/optimizations/simple_types/simple_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,21 @@ bool SimpleTypesAnalysis::runOnModule(Module& M)

if (first)
{
first = false;

RDA.runOnModule(M, AbiProvider::getAbi(&M));
buildEqSets(M);
buildEquations();
eqSets.propagate(module);
eqSets.apply(module, config, objf, instToErase);
eraseObsoleteInstructions();
setGlobalConstants();
first = false;
RDA.clear();
}
else
{
first = true;

instToErase.clear();

IrModifier irModif(module, config);
Expand Down Expand Up @@ -721,7 +724,7 @@ void SimpleTypesAnalysis::eraseObsoleteInstructions()

EqSet& EqSetContainer::createEmptySet()
{
eqSets.push_back( EqSet() );
eqSets.push_back(EqSet(eqSets.size()));
return eqSets.back();
}

Expand Down Expand Up @@ -762,10 +765,8 @@ std::ostream& operator<<(std::ostream& out, const EqSetContainer& eqs)
//=============================================================================
//

unsigned EqSet::newUID = 0;

EqSet::EqSet() :
id(newUID++)
EqSet::EqSet(std::size_t id) :
id(id)
{

}
Expand Down Expand Up @@ -1228,7 +1229,7 @@ void EqSet::apply(

LOG << "\napply BEGIN " << id << " =============================\n";

static auto &conf = config->getConfig();
auto &conf = config->getConfig();

IrModifier irModif(module, config);
for (auto& vs : valSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if (fnc.getName() != "_func") continue;

bool TypesPropagator::skipRootProcessing(llvm::Value* val)
{
static auto* special = AsmInstruction::getLlvmToAsmGlobalVariable(_module);
auto* special = AsmInstruction::getLlvmToAsmGlobalVariable(_module);
return val == special
|| _abi->isRegister(val)
// || (llvm::isa<llvm::Instruction>(val)
Expand Down
5 changes: 5 additions & 0 deletions src/bin2llvmir/optimizations/value_protect/value_protect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ bool ValueProtect::run()

bool changed = false;

if (!_type2fnc.empty() && _type2fnc.begin()->second->getParent() != _module)
{
_type2fnc.clear();
}

changed = _type2fnc.empty() ? protect() : unprotect();

return changed;
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/optimizations/writer_dsm/writer_dsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void DsmWriter::generateFunction(

void DsmWriter::getAsmInstructionHex(AsmInstruction& ai, std::ostream& ret)
{
static std::size_t longestHexa = _longestInst * 3 - 1;
std::size_t longestHexa = _longestInst * 3 - 1;
const std::size_t aiHexa = ai.getByteSize() * 3 - 1;

std::vector<std::uint64_t> bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ CallingConventionProvider* CallingConventionProvider::getProvider()
return &instance;
}

void CallingConventionProvider::clear()
{
// TODO: probably not needed. and definitely not working.
// auto* ccp = getProvider();
// ccp->_id2cc.clear();
}

void CallingConventionProvider::registerCC(
const CallingConvention::ID& cc,
const CallingConvention::ConstructorMethod& con)
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/providers/fileimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ llvm::Constant* FileImage::getConstant(
refGvs.push_back(newGv);
addr += Abi::getTypeByteSize(_module, Abi::getDefaultType(_module));

static auto& conf = config->getConfig();
auto& conf = config->getConfig();
if (conf.globals.getObjectByAddress(addr))
{
break;
Expand Down
Loading