HomePhabricator

[DAGCombine][X86][AArch64] 'A - (A & (B - 1))' -> 'A & (0 - B)' fold (PR44448)

Authored by lebedev.ri on Jan 3 2020, 1:50 AM.

Description

[DAGCombine][X86][AArch64] 'A - (A & (B - 1))' -> 'A & (0 - B)' fold (PR44448)

While we do manage to fold integer-typed IR in middle-end,
we can't do that for the main motivational case of pointers.

There is @llvm.ptrmask() intrinsic which may or may not be helpful,
but i'm not sure it is fully considered canonical yet,
not everything is fully aware of it likely.

https://rise4fun.com/Alive/ZVdp

Name: ptr - (ptr & (alignment-1)) -> ptr & (0 - alignment)

%mask = add i64 %alignment, -1
%bias = and i64 %ptr, %mask
%r = sub i64 %ptr, %bias

>

%highbitmask = sub i64 0, %alignment
%r = and i64 %ptr, %highbitmask

See

https://bugs.llvm.org/show_bug.cgi?id=44448
https://reviews.llvm.org/D71499

Details