[llvm] [InstCombine] Optimistically allow multiple shufflevector uses in foldOpPhi (PR #114278)

David Peixotto via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 17:20:03 PST 2024


================
@@ -1773,17 +1773,31 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
   if (NumPHIValues == 0)
     return nullptr;
 
-  // We normally only transform phis with a single use.  However, if a PHI has
-  // multiple uses and they are all the same operation, we can fold *all* of the
-  // uses into the PHI.
+  // We normally only transform phis with a single use.
+  bool AllUsesIdentical = false;
+  bool MultipleShuffleVectorUses = false;
   if (!PN->hasOneUse()) {
-    // Walk the use list for the instruction, comparing them to I.
+    // Exceptions:
+    //   - All uses are identical.
+    //   - All uses are shufflevector instructions that fully simplify; this
+    //     helps interleave -> phi -> 2x de-interleave+de patterns.
+    MultipleShuffleVectorUses = isa<ShuffleVectorInst>(I);
+    AllUsesIdentical = true;
+    unsigned NumUses = 0;
     for (User *U : PN->users()) {
+      ++NumUses;
       Instruction *UI = cast<Instruction>(U);
-      if (UI != &I && !I.isIdenticalTo(UI))
+      if (UI == &I)
+        continue;
+
+      if (!I.isIdenticalTo(UI))
+        AllUsesIdentical = false;
+      // Only inspect first 4 uses to avoid quadratic complexity.
+      if (!isa<ShuffleVectorInst>(UI) || NumUses > 4)
----------------
dmpots wrote:

I see. I thought the check [below](https://github.com/llvm/llvm-project/pull/114278/files#diff-0ffb024c6c026c4b93507d08e6d24dccf98980023130781164b71586bc747f2dR1851) was to ensure that the shuffle vector uses were simplified.

```
    // Be conservative in cases with multiple uses and require all inputs to
    // simplify.
    if (MultipleShuffleVectorUses)
      return nullptr;
```

But it sounds like even with this check we may end up with some shufflevectors that are not simplified?

https://github.com/llvm/llvm-project/pull/114278


More information about the llvm-commits mailing list