[InstCombine] prevent infinite loop from conflicting shuffle mask transforms
authorSanjay Patel <spatel@rotateright.com>
Mon, 25 Nov 2019 16:55:57 +0000 (11:55 -0500)
committerSanjay Patel <spatel@rotateright.com>
Mon, 25 Nov 2019 17:00:41 +0000 (12:00 -0500)
The pattern in question is currently not possible because we
aggressively (wrongly) transform mask elements to undef values
if they choose from an undef operand. That, however, would
change if we tighten our semantics for shuffles as discussed
in D70641. Adding this check gives us the flexibility to make
that change with minimal overhead for current definitions.

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

index fd31f52..1dba330 100644 (file)
@@ -1553,9 +1553,11 @@ static Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf,
   if (!Shuf.isSelect())
     return nullptr;
 
-  // Canonicalize to choose from operand 0 first.
+  // Canonicalize to choose from operand 0 first unless operand 1 is undefined.
+  // Commuting undef to operand 0 conflicts with another canonicalization.
   unsigned NumElts = Shuf.getType()->getVectorNumElements();
-  if (Shuf.getMaskValue(0) >= (int)NumElts) {
+  if (!isa<UndefValue>(Shuf.getOperand(1)) &&
+      Shuf.getMaskValue(0) >= (int)NumElts) {
     // TODO: Can we assert that both operands of a shuffle-select are not undef
     // (otherwise, it would have been folded by instsimplify?
     Shuf.commute();