This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Fold (X || Y) ? false : X --> false
ClosedPublic

Authored by bcl5980 on Nov 25 2022, 2:21 AM.

Diff Detail

Event Timeline

bcl5980 created this revision.Nov 25 2022, 2:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 25 2022, 2:21 AM
bcl5980 requested review of this revision.Nov 25 2022, 2:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 25 2022, 2:21 AM
spatel edited the summary of this revision. (Show Details)Nov 25 2022, 6:16 AM
bcl5980 updated this revision to Diff 478042.Nov 25 2022, 8:35 PM
bcl5980 retitled this revision from [InstSimplify] Fold !(X || Y) && X to false to [InstSimplify] Fold two i1 select pattern.
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 retitled this revision from [InstSimplify] Fold two i1 select pattern to [InstSimplify] Fold two i1 select patterns.

These two patterns are really simple so I merge them into one patch. If any of you think split them is better I will send another patch for the new pattern.

bcl5980 added a comment.EditedNov 27 2022, 4:02 AM

Note:
!(X || Y) && Y --> false
can be trigged on SpecCPU2017\benchspec\CPU\502.gcc_r\src\insn-attrtab.c

There are quite a few variations within these patterns, so I prefer to split it up.
Please pre-commit baseline tests and see my comment in D138744. Also, should we prefer the code placement/structure that is proposed in that patch?

There are quite a few variations within these patterns, so I prefer to split it up.
Please pre-commit baseline tests and see my comment in D138744. Also, should we prefer the code placement/structure that is proposed in that patch?

I prefer to keep current position. Because

  1. These two patterns I add is very similar. Put them together looks better.
  2. They can share m_LogicalOr match. D138744 miss logical or also. We can't put the code on there because BO is not BinaryOperator when it is a logical or.
bcl5980 updated this revision to Diff 478230.Nov 28 2022, 7:01 AM
bcl5980 retitled this revision from [InstSimplify] Fold two i1 select patterns to [InstSimplify] Fold (X || Y) ? X : Y --> X.
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 retitled this revision from [InstSimplify] Fold (X || Y) ? X : Y --> X to [InstSimplify] Fold !(X || Y) && X --> false.

There's still a lot of possibilities even with these 2 patterns, so I'd prefer to split it up again. We are missing some necessary tests.

llvm/lib/Analysis/InstructionSimplify.cpp
4565

This comment is not the same as the pattern that is matched.

llvm/test/Transforms/InstSimplify/select-logical.ll
204–205

Shouldn't there be a test like this (or the first test) with a bitwise 'or'?

https://alive2.llvm.org/ce/z/yzcA-z

219–220

We need a test similar to this, but with poison elements.

329
bcl5980 updated this revision to Diff 478299.Nov 28 2022, 11:31 AM
bcl5980 retitled this revision from [InstSimplify] Fold !(X || Y) && X --> false to [InstSimplify] Fold (X || Y) ? false && X --> false.
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 added inline comments.
llvm/test/Transforms/InstSimplify/select-logical.ll
219–220

@logical_not_or_and_vector1_poison should be the test you mentioned.

bcl5980 updated this revision to Diff 478310.Nov 28 2022, 11:55 AM
bcl5980 edited the summary of this revision. (Show Details)
spatel added inline comments.Nov 28 2022, 1:16 PM
llvm/test/Transforms/InstSimplify/select-logical.ll
292

Here and below - the usual notation for a select op if we are writing it like C code would be:
(X | Y) ? false : X --> false

303

update comment to match test:
(X | Y) ? false : Y --> false

327

We can transform this, but it must not propagate the poison:
https://alive2.llvm.org/ce/z/MsJicP

So it should be ok to leave the match with m_ZeroInt(), but instead of returning the existing TrueVal, return a "full" false constant:

ConstantInt::getFalse(Cond->getType())
bcl5980 updated this revision to Diff 478425.Nov 28 2022, 5:28 PM
bcl5980 retitled this revision from [InstSimplify] Fold (X || Y) ? false && X --> false to [InstSimplify] Fold (X || Y) ? false : X --> false.
bcl5980 edited the summary of this revision. (Show Details)
spatel accepted this revision.Nov 29 2022, 5:43 AM

LGTM - see inline for code suggestion.

llvm/lib/Analysis/InstructionSimplify.cpp
4564

Use m_c_LogicalOr() and m_Specific() to make the code shorter. We do not need to capture X/Y:

if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
    match(TrueVal, m_ZeroInt()))
  return ConstantInt::getFalse(Cond->getType());
This revision is now accepted and ready to land.Nov 29 2022, 5:43 AM
bcl5980 updated this revision to Diff 478550.Nov 29 2022, 6:07 AM
bcl5980 edited the summary of this revision. (Show Details)
This revision was landed with ongoing or failed builds.Nov 29 2022, 6:09 AM
This revision was automatically updated to reflect the committed changes.

Thanks for the review.