This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Constant comparison involving "lshr exact"
ClosedPublic

Authored by rahul.jain on Jun 2 2014, 3:24 AM.

Details

Summary

Hi all,

This is a follow up patch for http://llvm.org/viewvc/llvm-project?view=revision&revision=209903.
Added support to optimize comparisons with "lshr exact" of a constant
similar to "ashr exact".

I could have merged the code for both, but didnt seem to make much of a difference,
so handled after the code to handle "ashr exact".

Please help in reviewing the same.

Thanks,
Rahul

Diff Detail

Event Timeline

rahul.jain updated this revision to Diff 10009.Jun 2 2014, 3:24 AM
rahul.jain retitled this revision from to [InstCombine] Constant comparison involving "lshr exact".
rahul.jain updated this object.
rahul.jain edited the test plan for this revision. (Show Details)
rahul.jain added a subscriber: Unknown Object (MLST).
rafael added inline comments.Jun 2 2014, 5:42 AM
lib/Transforms/InstCombine/InstCombineCompares.cpp
2473–2474

This is not necessary, is it? match will have initialized CI2, so there is no uninitialized access.

2482

Can this share code with the above case? Use a helper function if needed. It looks like it could look something like

APInt Quotient;
if (...)

Quotient = CI2->getValue().sdiv(CI->getValue());

else if (...)

Quotient = CI2->getValue().udiv(CI->getValue());

else

// no transform.
rahul.jain updated this revision to Diff 10014.Jun 2 2014, 6:12 AM
rahul.jain updated this object.

Hi Rafael,

Thanks for the review.
Refactored code accordingly! I dont feel the need to use
a helper function here.

Please help review the same.

Thanks,
Rahul

rafael edited edge metadata.Jun 2 2014, 6:36 AM

There is still quiet a bit of duplicated code. For example

shift = Quotient.logBase2();
return new ICmpInst(I.getPredicate(), A,

ConstantInt::get(A->getType(), shift));
 ConstantInt::get(A->getType(), shift));

With a helper function it looks like you can return a boolean for "found optimization opportunity" and set the Quotient variable. That way the shif and the construction of the ICmpInst are shared.

rahul.jain updated this revision to Diff 10019.Jun 2 2014, 8:33 AM
rahul.jain edited edge metadata.

Hi Rafael,

Thanks for your valuable comments.
As per your suggestion, I implemented a helper function.

Please help review the same.

Thanks,
Rahul

rafael accepted this revision.Jun 2 2014, 9:01 AM
rafael edited edge metadata.

LGTM with two nits.

lib/Transforms/InstCombine/InstCombineCompares.cpp
2327

Start function name with a lowercase letter.

2338

No else needed after a return.

This revision is now accepted and ready to land.Jun 2 2014, 9:01 AM
rahul.jain updated this revision to Diff 10023.Jun 2 2014, 10:53 AM
rahul.jain edited edge metadata.

Thanks Rafael, updated patch.

Please if you could commit the same for me.

Thanks a lot for your help!

Rahul

rahul.jain closed this revision.Jun 3 2014, 11:13 PM

Thanks,
Rahul