This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] protect against an infinite loop between shl <--> mul (PR35579)
ClosedPublic

Authored by spatel on Dec 8 2017, 4:01 PM.

Details

Summary

At first, I tried to thread the x86 needle and use a target hook (isVectorShiftByScalarCheap()) to disable the transform only for non-splat pow-of-2 constants, but not AVX2, but only some element types, but...it's difficult.

Here we just avoid the loop with the x86 vector transform that conflicts with the general DAG combine and preserve all of the existing behavior AFAICT otherwise.

Some tests that will probably fail if someone does try to restrict this in a more targeted way for x86-only may be found in:

test/CodeGen/X86/combine-mul.ll
test/CodeGen/X86/vector-mul.ll
test/CodeGen/X86/widen_arith-5.ll

This should prevent the infinite looping seen with:
https://bugs.llvm.org/show_bug.cgi?id=35579

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.Dec 8 2017, 4:01 PM
RKSimon added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2678 ↗(On Diff #126226)

This looks OK, but would !LegalOperations || TLI.isOperationLegal(ISD::SHL, VT) be better?

spatel added inline comments.Dec 10 2017, 9:58 AM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2678 ↗(On Diff #126226)

That would be safer, but 'make check' shows we'll lose a fold for cases like this:

; fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
define <4 x i32> @combine_vec_urem_by_shl_pow2a(<4 x i32> %x, <4 x i32> %y) {
  %1 = shl <4 x i32> <i32 4, i32 4, i32 4, i32 4>, %y
  %2 = urem <4 x i32> %x, %1
  ret <4 x i32> %2
}

-; SSE-NEXT: pslld $2, %xmm1
+; SSE-NEXT: pmulld {{.*}}(%rip), %xmm1

We can't ask 'isOperationLegalOrCustom()' because then we'd still hit the infinite loop.

RKSimon accepted this revision.Dec 10 2017, 10:56 AM

OK, let's go with this then. LGTM

This revision is now accepted and ready to land.Dec 10 2017, 10:56 AM
This revision was automatically updated to reflect the committed changes.