This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Expand combine for (x & mask) -> x when (x & mask) == x
ClosedPublic

Authored by mbrkusanin on Nov 3 2020, 2:51 AM.

Details

Summary

We can use KnownBitsAnalysis to cover cases when mask is not trivial. It can
also help with cases when mask is not constant but can still be folded into
one. Since 'and' is commutative we should treat both operands as possible
replacements.

Diff Detail

Event Timeline

mbrkusanin created this revision.Nov 3 2020, 2:51 AM
mbrkusanin requested review of this revision.Nov 3 2020, 2:51 AM
arsenm added inline comments.Nov 3 2020, 6:40 AM
llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
426

Unnecessary whitespace change

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2881–2889

Constants are canonicalized to the RHS, so you don't need KnownBits and can just directly get the constant.

2894

MaskedValueIsZero helper like the DAG?

foad added inline comments.Nov 3 2020, 7:05 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2881–2889

Or you can implement it generally and symmetrically, so that it works for non-constants as well as constants:

X & Y -> X // if (XBits.Zero | YBits.One).isAllOnesValue()
X & Y -> Y // if (XBits.One | YBits.Zero).isAllOnesValue()
mbrkusanin updated this revision to Diff 303469.Nov 6 2020, 8:50 AM
  • Refactored match function
  • Updated tests
mbrkusanin added inline comments.Nov 6 2020, 8:54 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2881–2889

@arsenm With KnownBits we can also catch cases like in test_and_non_const. Or the ones in combine-shift-of-shifted-logic.ll generated by D90223

foad accepted this revision.Nov 9 2020, 2:02 AM

LGTM modulo minor comments inline.

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
432

Maybe just matchRedundantAnd?

llvm/include/llvm/Target/GlobalISel/Combine.td
384–387

I think we should define a standard Register matchdata that we can use here and elsewhere. Doesn't have to be part of this patch though.

This revision is now accepted and ready to land.Nov 9 2020, 2:02 AM
paquette added inline comments.Nov 9 2020, 9:02 AM
llvm/include/llvm/Target/GlobalISel/Combine.td
384–387

+1

  • Renamed
  • Updated comments