This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Always remove null check before free
ClosedPublic

Authored by xbolva00 on Apr 5 2018, 5:31 PM.

Details

Summary

Null check before free is currently removed only when we are optimizing for size.

This patch removes that limitation since free already handles null pointer passed as argument so null check before free can be easily removed. One more useless branching at the end of functions to be removed.

Diff Detail

Event Timeline

xbolva00 created this revision.Apr 5 2018, 5:31 PM
  1. Test?
  2. Please always upload diffs with full context (-U99999)
xbolva00 updated this revision to Diff 141324.Apr 6 2018, 6:00 AM

Added test

xbolva00 retitled this revision from Always remove null check before free to [InstCombine] Always remove null check before free.Apr 6 2018, 6:00 AM
xbolva00 updated this revision to Diff 141325.Apr 6 2018, 6:04 AM

I don't think you intended to add mypatch.patch

Also, i suspect ideally there should be an explanation of why "null check before free is currently removed only when we are optimizing for size." and then why "This patch removes that limitation."

test/Transforms/InstCombine/null-check-free.ll
1 ↗(On Diff #141324)

Please use llvm/utils/update_test_checks.py.
Also, the test is incomplete, @nullcheckfree is only listed in the check line, but it's not defined anywhere,
(You need to upload the diff as compared to the git master/svn trunk, not as compared to the previous diff)

xbolva00 updated this revision to Diff 141330.Apr 6 2018, 6:37 AM
xbolva00 edited the summary of this revision. (Show Details)Apr 6 2018, 6:39 AM
xbolva00 updated this revision to Diff 141334.Apr 6 2018, 6:50 AM
xbolva00 marked an inline comment as done.
lebedev.ri added inline comments.Apr 6 2018, 6:56 AM
test/Transforms/InstCombine/null-check-free.ll
1 ↗(On Diff #141324)

The usual comment at the top is missing though:

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
xbolva00 updated this revision to Diff 141400.Apr 6 2018, 12:09 PM
xbolva00 updated this revision to Diff 141417.Apr 6 2018, 1:53 PM
xbolva00 marked an inline comment as done.
xbolva00 updated this revision to Diff 141487.Apr 7 2018, 5:13 AM
xbolva00 added a reviewer: lebedev.ri.

Null check before free is currently removed only when we are optimizing for size.
This patch removes that limitation since free already handles null pointer passed as argument so null check before free can be easily removed.

I'm seeing "what", but not seeing "why"...
The original rL171762 talked about only doing that when it is known to be profitable.
It might be helpful to explain in the differential's description why it is now
preferable to always do this, not only when optimizing for size.

test/Transforms/InstCombine/null-check-free.ll
21 ↗(On Diff #141487)

So what exactly does that diff change here in the output?
https://godbolt.org/g/CJaEsg i don't see any difference.

This is why it is best to have two separate differentials,
one with just the original testcase, and the other one with the code change + regenerated test output.

33 ↗(On Diff #141487)

Hmm, i don't like the test. Does it not end up returning dangling pointer?

xbolva00 updated this revision to Diff 142111.Apr 11 2018, 9:19 PM

Patch updated with suggested changes by @lebedev.ri

xbolva00 abandoned this revision.Apr 12 2018, 6:49 AM
xbolva00 reclaimed this revision.
xbolva00 edited the summary of this revision. (Show Details)
xbolva00 accepted this revision.Apr 16 2018, 9:28 AM
This revision is now accepted and ready to land.Apr 16 2018, 9:28 AM
spatel added a subscriber: qcolombet.

I don't understand what's going on here. Why is this patch marked 'Accepted'? In general, you shouldn't approve your own patches that you've posted for review.

Beyond that, I don't understand why this is desirable. I think the reason this was coded with size optimization as a predicate is because the null check cheaply guards against a potentially expensive library call (cc @qcolombet as author of the original patch). This might be the correct transform for instcombine, but then there should be a backend transform that would add the null check back if it's profitable?

xbolva00 closed this revision.Apr 18 2018, 7:43 AM

I don't understand what's going on here. Why is this patch marked 'Accepted'? In general, you shouldn't approve your own patches that you've posted for review.

Beyond that, I don't understand why this is desirable. I think the reason this was coded with size optimization as a predicate is because the null check cheaply guards against a potentially expensive library call (cc @qcolombet as author of the original patch). This might be the correct transform for instcombine, but then there should be a backend transform that would add the null check back if it's profitable?

True. I will close this patch.