This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyLibCalls] Strip dead code in printf() transformations
ClosedPublic

Authored by davide on Mar 31 2016, 10:18 AM.

Details

Summary

If the return value of printf() is used, we don't do any transformation in SimplifyLibCalls.
Yet, in each transformation we check again if Ci->use_empty(). I think this check is redundant, because it's always true. This also allows to strip some code. IT passes the testsuite, but given I wasn't entirely sure of what are the effects, I put an assert() instead.

Diff Detail

Repository
rL LLVM

Event Timeline

davide updated this revision to Diff 52234.Mar 31 2016, 10:18 AM
davide retitled this revision from to [SimplifyLibCalls] Strip dead code in printf() transformations.
davide updated this object.
davide added reviewers: majnemer, spatel.
davide added a subscriber: llvm-commits.
spatel added inline comments.Mar 31 2016, 11:03 AM
lib/Transforms/Utils/SimplifyLibCalls.cpp
1828–1832 ↗(On Diff #52234)

IMO, the asserts are unnecessary since the check for empty is right here and has a nice explanatory comment too.

If others like the added safety of the asserts, I don't object. There is a proposal to refactor this whole file, so maybe asserts will be handy.

But instead of repeating the assert 5x, add a one-line helper function or lambda?

davide added inline comments.Mar 31 2016, 11:07 AM
lib/Transforms/Utils/SimplifyLibCalls.cpp
1828–1832 ↗(On Diff #52234)

My vote is to remove the asserts alltogheter. David?

spatel accepted this revision.Apr 1 2016, 3:30 PM
spatel edited edge metadata.

My vote is to remove the assertions; if you do that, then LGTM.
Up to you if you'd like to wait for David to review.

This revision is now accepted and ready to land.Apr 1 2016, 3:30 PM
majnemer accepted this revision.Apr 1 2016, 4:32 PM
majnemer edited edge metadata.

LGTM

lib/Transforms/Utils/SimplifyLibCalls.cpp
1828–1832 ↗(On Diff #52234)

SGTM

This revision was automatically updated to reflect the committed changes.
filcab added a subscriber: filcab.Apr 3 2016, 9:03 PM
filcab added inline comments.
lib/Transforms/Utils/SimplifyLibCalls.cpp
1837 ↗(On Diff #52234)

What about the !Res case? If the target has printf, but not putchar, for some reason? Or do we simply not care about those (which I think is valid :) )?

davide added a comment.Apr 3 2016, 9:09 PM

I assume that might be a problem.
This change is NFC because the first expression always evaluate to true so we never care about the !Res case even prior to this change. In case we hit a case you describe I'll be more than happy to fix but I think the possibility of an environment defining printf() and not putchar() is very remote anyway.

filcab added a comment.Apr 3 2016, 9:55 PM

Do you want to add an assert somewhere?
Just in case :-)

Thank you,

Filipe

Do you want to add an assert somewhere?
Just in case :-)

I think we're already safe here. On failure (eg, putchar is not available), emitPutChar() returns nullptr, so optimizePrintFString() will also return nullptr to signal to its caller that the transform didn't work.

filcab added a comment.Apr 5 2016, 1:41 AM

Right.

Thank you,

Filipe