retdec
Functions | Variables
retdec::bin2llvmir::inst_opt Namespace Reference

Functions

bool optimize (llvm::Instruction *insn)
 
bool addZero (llvm::Instruction *insn)
 
bool subZero (llvm::Instruction *insn)
 
bool truncZext (llvm::Instruction *insn)
 
bool xorXX (llvm::Instruction *insn)
 
bool xorLoadXX (llvm::Instruction *insn)
 
bool orAndXX (llvm::Instruction *insn)
 
bool orAndLoadXX (llvm::Instruction *insn)
 
bool xor_i1 (llvm::Instruction *insn)
 
bool and_i1 (llvm::Instruction *insn)
 
bool addSequence (llvm::Instruction *insn)
 
llvm::Value * castSequence (llvm::CastInst *cast1, llvm::CastInst *cast2)
 
llvm::Value * castSequenceFinder (llvm::Value *insn)
 
bool castSequenceWrapper (llvm::Instruction *insn)
 
bool storeToBitcastPointer (llvm::Instruction *insn)
 
bool loadFromBitcastPointer (llvm::Instruction *insn)
 

Variables

std::vector< bool(*)(llvm::Instruction *)> optimizations
 

Function Documentation

◆ addSequence()

bool retdec::bin2llvmir::inst_opt::addSequence ( llvm::Instruction *  insn)

a = add x, c1 b = add a, c2 => b = add x, (c1 + c2)

◆ addZero()

bool retdec::bin2llvmir::inst_opt::addZero ( llvm::Instruction *  insn)

x = add y, 0 => x = y

x = add 0, y => x = y

◆ and_i1()

bool retdec::bin2llvmir::inst_opt::and_i1 ( llvm::Instruction *  insn)

a = and i1 x, y => a = icmp eq i1 x, y

◆ castSequence()

llvm::Value* retdec::bin2llvmir::inst_opt::castSequence ( llvm::CastInst *  cast1,
llvm::CastInst *  cast2 
)

cast1 fp/ptr to ... ... cast2 ... to fp/ptr => cast fp/ptr to fp/ptr

Do not do this for integers. It is not always safe. E.g. i32 -> i1 -> i32 is not the same as i32 -> i32. It may not be safe for pointers and floats as well, but we leave it for now.

◆ castSequenceFinder()

llvm::Value* retdec::bin2llvmir::inst_opt::castSequenceFinder ( llvm::Value *  insn)

Find cast sequnces to try to optimize.

◆ castSequenceWrapper()

bool retdec::bin2llvmir::inst_opt::castSequenceWrapper ( llvm::Instruction *  insn)

Apply cast optimization repeatedly until it can not be applied anymore.

◆ loadFromBitcastPointer()

bool retdec::bin2llvmir::inst_opt::loadFromBitcastPointer ( llvm::Instruction *  insn)
%1 = load float, float* bitcast (i32* @g to float*)
==>
%1 = load i32, i32* @g
%2 = bitcast i32 %1 to float
%1 = load i8*, i8** bitcast (i32* @g to i8**)
==>
%1 = load i32, i32* @g
%2 = inttoptr i32 %1 to i8*

◆ optimize()

bool retdec::bin2llvmir::inst_opt::optimize ( llvm::Instruction *  insn)

◆ orAndLoadXX()

bool retdec::bin2llvmir::inst_opt::orAndLoadXX ( llvm::Instruction *  insn)

a = load x b = load x c = or a, b => c = 0

a = load x b = load x c = and a, b => c = 0

◆ orAndXX()

bool retdec::bin2llvmir::inst_opt::orAndXX ( llvm::Instruction *  insn)

a = or x, x => a = x

a = and x, x => a = x

◆ storeToBitcastPointer()

bool retdec::bin2llvmir::inst_opt::storeToBitcastPointer ( llvm::Instruction *  insn)
store float %val, float* bitcast (i32* @gv to float*)
==>
%conv = bitcast float %val to i32
store i32 %conv, i32* @gv

This is countering an undesirable LLVM instrcombine optimization that is going the other way.

◆ subZero()

bool retdec::bin2llvmir::inst_opt::subZero ( llvm::Instruction *  insn)

x = sub y, 0 => x = use y

◆ truncZext()

bool retdec::bin2llvmir::inst_opt::truncZext ( llvm::Instruction *  insn)

a = trunc i32 val to i8 b = zext i8 a to i32 => b = and i32 val, 255

a = trunc i32 val to i16 b = zext i16 a to i32 => b = and i32 val, 65535

◆ xor_i1()

bool retdec::bin2llvmir::inst_opt::xor_i1 ( llvm::Instruction *  insn)

a = xor i1 x, y => a = icmp ne i1 x, y

◆ xorLoadXX()

bool retdec::bin2llvmir::inst_opt::xorLoadXX ( llvm::Instruction *  insn)

a = load x b = load x c = xor a, b => c = 0

◆ xorXX()

bool retdec::bin2llvmir::inst_opt::xorXX ( llvm::Instruction *  insn)

a = xor x, x => a = 0

Variable Documentation

◆ optimizations

std::vector<bool (*)(llvm::Instruction*)> retdec::bin2llvmir::inst_opt::optimizations
Initial value:
=
{
}
bool castSequenceWrapper(llvm::Instruction *insn)
Definition: inst_opt.cpp:426
bool xorXX(llvm::Instruction *insn)
Definition: inst_opt.cpp:138
bool and_i1(llvm::Instruction *insn)
Definition: inst_opt.cpp:301
bool addZero(llvm::Instruction *insn)
Definition: inst_opt.cpp:29
bool addSequence(llvm::Instruction *insn)
Definition: inst_opt.cpp:332
bool xor_i1(llvm::Instruction *insn)
Definition: inst_opt.cpp:271
bool storeToBitcastPointer(llvm::Instruction *insn)
Definition: inst_opt.cpp:449
bool truncZext(llvm::Instruction *insn)
Definition: inst_opt.cpp:83
bool loadFromBitcastPointer(llvm::Instruction *insn)
Definition: inst_opt.cpp:503
bool xorLoadXX(llvm::Instruction *insn)
Definition: inst_opt.cpp:162
bool orAndLoadXX(llvm::Instruction *insn)
Definition: inst_opt.cpp:237
bool subZero(llvm::Instruction *insn)
Definition: inst_opt.cpp:54
bool orAndXX(llvm::Instruction *insn)
Definition: inst_opt.cpp:203

Order here is important. More specific patterns must go first, more general later.