retdec
Protected Member Functions | Friends | List of all members
retdec::bin2llvmir::IdiomsGCC Class Reference

GNU/GCC instruction idioms. More...

#include <idioms_gcc.h>

Inheritance diagram for retdec::bin2llvmir::IdiomsGCC:
Inheritance graph
[legend]
Collaboration diagram for retdec::bin2llvmir::IdiomsGCC:
Collaboration graph
[legend]

Protected Member Functions

llvm::Instruction * exchangeFloatNeg (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeXorMinusOne (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeSignedModuloByTwo (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeCondBitShiftDiv1 (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeCondBitShiftDiv2 (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeCondBitShiftDiv3 (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeCopysign (llvm::BasicBlock::iterator iter) const
 
llvm::Instruction * exchangeFloatAbs (llvm::BasicBlock::iterator iter) const
 
int exchangeCondBitShiftDivMultiBB (llvm::Function &f, llvm::Pass *pass) const
 
- Protected Member Functions inherited from retdec::bin2llvmir::IdiomsAbstract
 IdiomsAbstract ()
 
void init (llvm::Module *M, CC_compiler cc, CC_arch arch)
 
CC_compiler getCompiler () const
 
CC_arch getArch () const
 
llvm::Module * getModule () const
 
virtual bool doAnalysis (llvm::Function &, llvm::Pass *)=0
 
virtual ~IdiomsAbstract ()=default
 
bool findBranchDependingOn (llvm::BranchInst **br, llvm::BasicBlock &bb, const llvm::Value *val) const
 

Friends

class IdiomsAnalysis
 

Additional Inherited Members

- Static Protected Member Functions inherited from retdec::bin2llvmir::IdiomsAbstract
static void eraseInstFromBasicBlock (llvm::Value *val, llvm::BasicBlock *bb)
 
static bool isPowerOfTwo (unsigned x)
 
static bool isPowerOfTwoRepresentable (const llvm::ConstantInt *cnst)
 

Detailed Description

GNU/GCC instruction idioms.

Member Function Documentation

◆ exchangeCondBitShiftDiv1()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeCondBitShiftDiv1 ( llvm::BasicBlock::iterator  iter) const
protected

Exchange (X < 0 ? X + (N-1) : X) a>> log2(N) with X s/ N

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeCondBitShiftDiv2()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeCondBitShiftDiv2 ( llvm::BasicBlock::iterator  iter) const
protected

Division by negative power of two. Found on PowerPC, e.g. div -4. Exchange 0 - ((X >> shift) | ((X >> 31) & mask))

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeCondBitShiftDiv3()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeCondBitShiftDiv3 ( llvm::BasicBlock::iterator  iter) const
protected

Division by negative power of two. Found on PowerPC, e.g. div -2. Exchange 0 - ((X >> shift) | (X & mask)) – in case of div -2.

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeCondBitShiftDivMultiBB()

int retdec::bin2llvmir::IdiomsGCC::exchangeCondBitShiftDivMultiBB ( llvm::Function &  f,
llvm::Pass *  pass 
) const
protected

Exchange multi BB idiom, found only when compiling for ARM with -Os.

Parameters
ffunction to visit
passactual pass
Returns
idioms replaced

res = n % 2: dec_label: u12 = and i32 n, -2147483647 u13 = icmp slt i32 u12, 0 u11 = sext i1 u13 to i32 u11.u12 = add i32 u11, u12 br i1 u13, label if_true, label after_if

if_true: fold = add i32 u11, n u5 = or i32 fold, -2 u11 = add i32 u5, 1 br label after_if

after_if_8230_3: res = phi i32 [ u11, if_true ], [ u11.u12, dec_label ]

This idiom was found when compiling for ARM with -Os (GCC)

Multi BB idiom. Before branch:

                                icmp slt
                                   /\
                            -------  -------
                           /                \
                         and                 0
                         /\
                  -------  -------
                 /                \
               <n>           -2147483647

if icmp is true (if_true block)

                                   add
                                   /\
                            -------  -------
                           /                \
                         or                  1
                         /\
                  -------  -------
                 /                \
               add                 -2
                /\
         -------  -------
        /                \
      <n>                 1

◆ exchangeCopysign()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeCopysign ( llvm::BasicBlock::iterator  iter) const
protected

Exchange X = ((A & 0x7FFFFFFF) | (B & 0x80000000)) with copysignf(A, B) or X = ((fabsf(A)) | (B & 0x80000000)) with copysignf(A, B)

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeFloatAbs()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeFloatAbs ( llvm::BasicBlock::iterator  iter) const
protected

Exchange X & 0x7FFFFFFF with fabsf(X)

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeFloatNeg()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeFloatNeg ( llvm::BasicBlock::iterator  iter) const
protected

Exchange (x ^ -2147483648) with -x

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeSignedModuloByTwo()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeSignedModuloByTwo ( llvm::BasicBlock::iterator  iter) const
protected

Exchange (((lshr(X, 31) + X) & 1) - lshr(X, 31))) with X % 2

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

◆ exchangeXorMinusOne()

Instruction * retdec::bin2llvmir::IdiomsGCC::exchangeXorMinusOne ( llvm::BasicBlock::iterator  iter) const
protected

Exchange x ^ -1 with -x - 1

Parameters
itervalue to visit
Returns
replaced Instruction, otherwise nullptr

Friends And Related Function Documentation

◆ IdiomsAnalysis

friend class IdiomsAnalysis
friend

The documentation for this class was generated from the following files: