This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by bcl5980 on Nov 28 2022, 7:03 AM.

Diff Detail

Event Timeline

bcl5980 created this revision.Nov 28 2022, 7:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 7:03 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Nov 28 2022, 7:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 7:03 AM
bcl5980 updated this revision to Diff 478428.Nov 28 2022, 5:42 PM

rebase code

bcl5980 updated this revision to Diff 478574.Nov 29 2022, 6:52 AM

rebase code

bcl5980 added inline comments.Nov 29 2022, 6:55 AM
llvm/lib/Analysis/InstructionSimplify.cpp
4558

Hi @spatel , which way do you think is better here?

// (X || Y) ? X : Y --> X (commuted 2 ways)
if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
  return TrueVal;

// (X || Y) ? false : X --> false (commuted 2 ways)
if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
    match(TrueVal, m_ZeroInt()))
  return ConstantInt::getFalse(Cond->getType());

or

if (match(Cond, m_LogicalOr(m_Value(X), m_Value(Y)))) {
  // (X || Y) ? X : Y --> X
  if ((X == TrueVal && Y == FalseVal) || (X == FalseVal && Y == TrueVal))
    return TrueVal;

  // (X || Y) ? false : X --> false (commuted 2 ways)
  if (match(TrueVal, m_ZeroInt()) && (X == FalseVal || Y == FalseVal))
    return ConstantInt::getFalse(Cond->getType());
}
spatel accepted this revision.Nov 29 2022, 9:03 AM

LGTM

llvm/lib/Analysis/InstructionSimplify.cpp
4558

The current code (using m_Specific) is a little easier to read to me.

llvm/test/Transforms/InstSimplify/select-logical.ll
445

This could fold, but we don't match it currently, right? Add a TODO comment to be consistent with the previous patches.

This revision is now accepted and ready to land.Nov 29 2022, 9:03 AM
bcl5980 updated this revision to Diff 478775.Nov 29 2022, 6:11 PM

add TODO for test

This revision was landed with ongoing or failed builds.Nov 29 2022, 6:14 PM
This revision was automatically updated to reflect the committed changes.