Skip to content

Commit

Permalink
idiom_analysis: Disable idiom AShr(X,shift) <-> SDiv(X,shift) (#724)
Browse files Browse the repository at this point in the history
Closes #724

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 positive integer.

Example:

* AShr(0xA5A5A5A5A5A50020, 8) = 0xFFA5A5A5A5A5A500
* SDiv(0xA5A5A5A5A5A50020, 256) = 0xFFA5A5A5A5A5A501
  • Loading branch information
Peter Kubov committed Aug 4, 2020
1 parent a46f47b commit 9a9c1d3
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 29 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
5 changes: 0 additions & 5 deletions src/bin2llvmir/optimizations/idioms/idioms_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ bool IdiomsAnalysis::doAnalysis(Function & f, Pass * p) {
change_made |= analyse(bb, &IdiomsCommon::exchangeBitShiftSDiv1,
"IdiomsCommon::exchangeBitShiftSDiv1");

// all arch
// all compilers
change_made |= analyse(bb, &IdiomsCommon::exchangeBitShiftSDiv2,
"IdiomsCommon::exchangeBitShiftSDiv2");

// all arch
// all compilers
change_made |= analyse(bb, &IdiomsCommon::exchangeBitShiftUDiv,
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 9a9c1d3

Please sign in to comment.