This is an archive of the discontinued LLVM Phabricator instance.

[TargetLowering] Use HandleSDNodes to prevent nodes from being deleted by recursive calls in getNegatedExpression.
ClosedPublic

Authored by craig.topper on Mar 3 2021, 11:19 PM.

Details

Summary

For binary or ternary ops we call getNegatedExpression multiple
times and then compare costs. While we're doing this we need to
hold a node from the first call across the second call, but its
not yet attached to the DAG. Its possible the second call creates
an identical node and then decides it didn't need it so will try
to delete it if it has no uses. This can cause a reference to the
node we're holding further up the call stack to become invalidated.

To prevent this, we can use a HandleSDNode to artifically give
the node a use without connecting it to the DAG.

I've used a std::list of HandleSDNodes so we can create handles
only when we have a node to hold. HandleSDNode does not have
default constructor and cannot be copied or moved.

Fixes PR49393.

Diff Detail

Event Timeline

craig.topper created this revision.Mar 3 2021, 11:19 PM
craig.topper requested review of this revision.Mar 3 2021, 11:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2021, 11:19 PM
uabelho added a subscriber: uabelho.Mar 4 2021, 1:22 AM
spatel added a comment.Mar 4 2021, 6:10 AM

This keeps getting more complicated, but I don't have any better suggestions.
I am still wondering if we can/should assert within getNode() that operand values are not already deleted nodes. Ie, passing those nodes in means the caller has definitely gone out-of-bounds in some way.

spatel accepted this revision.Mar 4 2021, 1:27 PM

LGTM

This revision is now accepted and ready to land.Mar 4 2021, 1:27 PM
This revision was landed with ongoing or failed builds.Mar 4 2021, 10:51 PM
This revision was automatically updated to reflect the committed changes.