This is an archive of the discontinued LLVM Phabricator instance.

Optimize bitwise even/odd test (-x&1 -> x&1) to not use negation.
ClosedPublic

Authored by bmakam on Aug 19 2015, 9:44 AM.

Details

Summary

We know that -x & 1 is equivalent to x & 1, avoid using negation for testing if a negative integer is even or odd.

Diff Detail

Event Timeline

bmakam updated this revision to Diff 32565.Aug 19 2015, 9:44 AM
bmakam retitled this revision from to Optimize bitwise even/odd test (-x&1 -> x&1) to not use negation..
bmakam updated this object.
bmakam added a reviewer: majnemer.
bmakam added subscribers: llvm-commits, mcrosier, gberry and 2 others.
majnemer edited edge metadata.Aug 19 2015, 10:27 AM

Please add more context to the diff.

bmakam updated this revision to Diff 32580.Aug 19 2015, 10:59 AM
bmakam edited edge metadata.

adding more context to diff.

mcrosier added inline comments.Aug 19 2015, 11:12 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1277

The isa<> check removes the need for a dyn_cast. You can safely use

const APInt &Op0LHSMask = cast<ConstantInt>(Op0LHS)->getValue();

if I'm not mistaken.

1278

No need for the extra braces as the condition is guarding a single statement.

bmakam updated this revision to Diff 32582.Aug 19 2015, 11:16 AM
bmakam marked 2 inline comments as done.
majnemer added inline comments.Aug 19 2015, 11:17 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1276–1278

I think this can more concisely written as:

if (AndRHSMask == 1 && match(Op0LHS, m_Zero()))
  return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
bmakam updated this revision to Diff 32585.Aug 19 2015, 11:23 AM
bmakam marked an inline comment as done.
majnemer accepted this revision.Aug 19 2015, 11:34 AM
majnemer edited edge metadata.

LGTM

test/Transforms/InstCombine/and-with-one.ll
1–25

Instead of a new file, can you add these to and2.ll ?

This revision is now accepted and ready to land.Aug 19 2015, 11:34 AM
bmakam updated this revision to Diff 32589.Aug 19 2015, 11:48 AM
bmakam edited edge metadata.

Adding tests to and2.ll Thanks for the review.

Balaram,
Please let me know when you're ready for the patch to land and I'll be happy to commit it on your behalf.

This revision was automatically updated to reflect the committed changes.