Skip to content

Commit

Permalink
idiom_analysis: Disable idiom AShr(X,shift) <-> X / shift (#724)
Browse files Browse the repository at this point in the history
Disables idiom that exchanged Pattern AShr(X,shift) into divistion
SDiv(X, shift). This is because signed division is not equal to the
arithmetic shift right all the time. Only when X is signed integer.

Example:

* AShr(0xA5A5A5A5A5A50020, 8) = 0xFFA5A5A5A5A5A500
* SDiv(0xA5A5A5A5A5A50020, 256) = 0xFFA5A5A5A5A5A501
  • Loading branch information
Peter Kubov committed Aug 2, 2020
1 parent 127dbe2 commit 4927332
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class IdiomsCommon: virtual public IdiomsAbstract {
llvm::Instruction * exchangeLessThanZero(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeGreaterEqualZero(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeBitShiftSDiv1(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeBitShiftSDiv2(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeBitShiftUDiv(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeBitShiftMul(llvm::BasicBlock::iterator iter) const;
llvm::Instruction * exchangeSignedModulo2n(llvm::BasicBlock::iterator iter) const;
Expand Down
23 changes: 0 additions & 23 deletions src/bin2llvmir/optimizations/idioms/idioms_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,6 @@ Instruction * IdiomsCommon::exchangeBitShiftSDiv1(BasicBlock::iterator iter) con
return res;
}

/**
* Exchange shift right with a division.
*
* @param iter value to visit
* @return replaced Instruction, otherwise nullptr
*/
Instruction * IdiomsCommon::exchangeBitShiftSDiv2(BasicBlock::iterator iter) const {
Instruction & val = (*iter);
Value * op0 = nullptr;
ConstantInt * cnst = nullptr;

// X s>> C --> X / 2^C
if (match(&val, m_AShr(m_Value(op0), m_ConstantInt(cnst))) &&
isPowerOfTwoRepresentable(cnst)) {
Constant *NewCst = ConstantInt::get(cnst->getType(),
pow(2, *cnst->getValue().getRawData()));
BinaryOperator *div = BinaryOperator::CreateSDiv(op0, NewCst);
return div;
}

return nullptr;
}

/**
* Exchange shift left by with a multiplication
*
Expand Down

0 comments on commit 4927332

Please sign in to comment.