This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] mark ADD with nuw if no unsigned overflow
ClosedPublic

Authored by jingyue on Jun 13 2014, 9:52 PM.

Details

Summary

As a starting step, we only use one simple heuristic: if the sign bits
of both a and b are zero, we can prove "add a, b" do not unsigned
overflow, and thus convert it to "add nuw a, b".

Updated all affected tests and added two new tests (@zero_sign_bit and
@zero_sign_bit2) in AddOverflow.ll

Diff Detail

Event Timeline

jingyue updated this revision to Diff 10417.Jun 13 2014, 9:52 PM
jingyue retitled this revision from to [InstCombine] mark ADD with nuw if no unsigned overflow.
jingyue updated this object.
jingyue edited the test plan for this revision. (Show Details)
jingyue added reviewers: eliben, meheff, rafael.
jingyue added a subscriber: Unknown Object (MLST).
chandlerc accepted this revision.Jun 14 2014, 11:28 AM
chandlerc added a reviewer: chandlerc.
chandlerc added a subscriber: chandlerc.

Minor comment, LGTM with the obvious adjustment.

I worry a little about how many times we compute the known sign bits here, but that's nothing new in this patch.

lib/Transforms/InstCombine/InstCombineAddSub.cpp
1264

This should be an else if, no? If we succeed at adding NSW, why would we check for NUW?

This revision is now accepted and ready to land.Jun 14 2014, 11:28 AM

Thanks for your review! I agree with your concerns. computeKnownBits is a little expensive. I'm considering merging WillNotOverflowSignedAdd and WillNotOverflowUnsignedAdd in a separate patch.

lib/Transforms/InstCombine/InstCombineAddSub.cpp
1264

Even with nsw, there's still value of checking for NUW. e.g., "add i16 16383, 16383" can be marked with both nsw and nuw, while "add i16 65535, 65535" can be marked with only nsw but not nuw.

meheff edited edge metadata.Jun 16 2014, 12:50 PM

LGTM

lib/Transforms/InstCombine/InstCombineAddSub.cpp
972

nit: The comment suggests more than one technique for ruling out unsigned overflow, but only one is actually used.

jingyue updated this revision to Diff 10475.Jun 16 2014, 5:42 PM
jingyue edited edge metadata.

Fixed some comments. Thanks Chandler and Mark!

jingyue closed this revision.Jun 16 2014, 5:50 PM