This is an archive of the discontinued LLVM Phabricator instance.

[IR] Clean up dead instructions after simplifying a conditional branch
AcceptedPublic

Authored by foad on May 19 2020, 6:15 AM.

Details

Summary

Change BasicBlock::removePredecessor to optionally return a vector of
instructions which might be dead. Use this in ConstantFoldTerminator to
delete them if they are dead.

Diff Detail

Event Timeline

foad created this revision.May 19 2020, 6:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2020, 6:15 AM
jdoerfert added inline comments.May 19 2020, 7:14 AM
llvm/lib/Transforms/Utils/Local.cpp
135

It seems unfortunate that we need to copy the vector. Do we believe there is another use case for MaybeDeadInstrs so we want it to take a Instruction vector?

foad updated this revision to Diff 264933.May 19 2020, 8:50 AM

Like this? I had to include ValueHandle.h from BasicBlock.h. I don't know if that's acceptable.

I find this better. It's opt-in and seems reasonable to me. @nhaehnle wdyt?

nhaehnle accepted this revision.Jun 10 2020, 12:58 PM

This looks very reasonable to me.

llvm/lib/IR/BasicBlock.cpp
346–348

Nitpick: I think it's better to put braces around multi-line if-guarded statements.

This revision is now accepted and ready to land.Jun 10 2020, 12:58 PM
fhahn added a subscriber: fhahn.Jun 11 2020, 3:37 AM
fhahn added inline comments.
llvm/lib/Transforms/Utils/Local.cpp
107

Might be good to mention the change in behavior in the doc-comment (not sure why there's one here, should also be updated in the headeR)

476–477

Why change this to a dyn_cast? Looks like only Instructions are added in removePredecessor?

foad marked an inline comment as done.Jun 11 2020, 5:14 AM
foad added inline comments.
llvm/lib/Transforms/Utils/Local.cpp
476–477

removePredecessor only adds Instructions, but then in some cases it will replaceAllUsesWith one of those Instructions with an arbitrary Value.

foad marked 3 inline comments as done.Jun 11 2020, 5:29 AM
This revision was automatically updated to reflect the committed changes.
fhahn added inline comments.Jun 11 2020, 6:28 AM
llvm/lib/Transforms/Utils/Local.cpp
132

IIUC this function only deletes dead values if DeleteDeadConditions is true. Might be good to guard the new simplifications in a similar fashion.

476–477

Hm, it seems a bit unfortunate that we would add instructions that we immediately RAUW afterwards.

IIUC the only case where this would happen is where the incoming value for the removed predecessor is the original PN itself. Couldn't we simply not add the incoming instruction to DeadInsts? That seems like a cleaner solution and should be straight-forward.

foad marked 2 inline comments as done.Jun 11 2020, 6:52 AM
foad added inline comments.
llvm/lib/Transforms/Utils/Local.cpp
132

Under the same flag or a different one? In any case my preference would be not to do this unless/until we really need it.

476–477

IIUC the only case where this would happen is where the incoming value for the removed predecessor is the original PN itself.

No, I think the problem occurs when we first visit PHI node A, one of whose inputs is PHI node B, which we add to MaybeDeadInstrs; and then we visit PHI node B and find that it hasConstantValue.

fhahn added inline comments.Jun 11 2020, 7:35 AM
llvm/lib/Transforms/Utils/Local.cpp
132

It would probably make sense to use the same flag. I assume there is a reason for making removing instructions conditional on an option (which defaults to false and some users set it to false). It seems like silently changing it to delete some other bits could trip over some existing users in obscure edge cases.

It might be that none of the users actually care of course. But in that case we should remove the flag altogether. In any case, I think the behavior should be documented.

476–477

Yeah that's another case, probably also possible to detect up-front. Anyways, it seems like we already allow non-instructions in DeadInsts in RecursivelyDeleteTriviallyDeadInstructions as well. Seems a bit odd to have something called DeadInsts containing non-instructions, but I guess that ship has sailed. Nevermind.

uabelho added a subscriber: uabelho.EditedJun 16 2020, 2:06 AM

Hi!

I wrote
https://bugs.llvm.org/show_bug.cgi?id=46343
about a crash that starts happening with this patch.

jdoerfert reopened this revision.Jun 16 2020, 10:49 AM

I wrote
https://bugs.llvm.org/show_bug.cgi?id=46343
about a crash that starts happening with this patch.

@foad, you look into this?

This revision is now accepted and ready to land.Jun 16 2020, 10:49 AM
foad added a comment.Jun 17 2020, 1:38 AM

I wrote
https://bugs.llvm.org/show_bug.cgi?id=46343
about a crash that starts happening with this patch.

@foad, you look into this?

I will. For the time being I reverted this commit in rG6fdd5a28b7840185d1f75d5702e7e2f3c4723b1c.