This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] recognize obfuscated variants of umin/umax
ClosedPublic

Authored by spatel on Oct 28 2016, 12:45 PM.

Details

Summary

The smallest tests that I could find to expose this are codegen tests (because SelectionDAGBuilder::visitSelect() uses matchSelectPattern to create UMAX/UMIN nodes), so I've semi-arbitrarily chosen x86 as the target for the regression tests.

If these were written as unsigned compares in IR, InstCombine canonicalizes the unsigned compares to signed compares. Ie, running the optimizer pessimizes the codegen for this case on trunk:

define <4 x i32> @umax_vec(<4 x i32> %x) {
  %cmp = icmp ugt <4 x i32> %x, <i32 2147483647, i32 2147483647, i32 2147483647, i32 2147483647>
  %sel = select <4 x i1> %cmp, <4 x i32> %x, <4 x i32> <i32 2147483647, i32 2147483647, i32 2147483647, i32 2147483647>
  ret <4 x i32> %sel
}

$ ./opt umax.ll -S | ./llc -o - -mattr=avx

vpmaxud	LCPI0_0(%rip), %xmm0, %xmm0

$ ./opt -instcombine umax.ll -S | ./llc -o - -mattr=avx

vpxor	%xmm1, %xmm1, %xmm1
vpcmpgtd	%xmm0, %xmm1, %xmm1
vmovaps	LCPI0_0(%rip), %xmm2    ## xmm2 = [2147483647,2147483647,2147483647,2147483647]
vblendvps	%xmm1, %xmm0, %xmm2, %xmm0

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 76236.Oct 28 2016, 12:45 PM
spatel retitled this revision from to [ValueTracking] recognize obfuscated variants of umin/umax.
spatel updated this object.
spatel added reviewers: efriedma, majnemer, sanjoy.
spatel added a subscriber: llvm-commits.
spatel updated this revision to Diff 76312.Oct 29 2016, 9:53 AM

Patch updated - rebase after rL285499 , but no difference in intent/functionality:

  1. Insert the unsigned min/max pattern matching block next to the updated signed min/max block.
  2. Changed name of test file since we have signed and unsigned tests now.
efriedma accepted this revision.Nov 8 2016, 2:12 PM
efriedma edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Nov 8 2016, 2:12 PM
This revision was automatically updated to reflect the committed changes.