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

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 09:51:34 PST 2024


================
@@ -1895,17 +1916,21 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
   for (unsigned i = 0; i != NumPHIValues; ++i)
     NewPN->addIncoming(NewPhiValues[i], PN->getIncomingBlock(i));
 
-  for (User *U : make_early_inc_range(PN->users())) {
-    Instruction *User = cast<Instruction>(U);
-    if (User == &I)
-      continue;
-    replaceInstUsesWith(*User, NewPN);
-    eraseInstFromFunction(*User);
+  if (AllUsesIdentical) {
----------------
MatzeB wrote:

When there is multipluses operands using the PHI, i.e. `op0(Phi(...))` and `op1(Phi(...))` then this loop will replace all uses of the `Phi` with the new `Phi(op0(...)...)` pattern, this includes replacing `op1` so this is only a correct transformation when op0 and op1 are identical.

The shufflevector code I add here `op0` and `op1` may be shufflevectors with different masks so we have can only optimize one use at a time; but instcombine will invoke this rule a 2nd for the other use later so it will get optimized as well.

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


More information about the llvm-commits mailing list