[llvm] [InstCombine] Try to freely invert phi nodes (PR #80804)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 00:59:59 PST 2024


================
@@ -2375,6 +2375,33 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
     }
   }
 
+  if (PHINode *PN = dyn_cast<PHINode>(V)) {
+    SmallVector<std::pair<Value *, BasicBlock *>, 8> IncomingValues;
+    for (Use &U : PN->operands()) {
+      BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
+      if (isa<PHINode>(U.get()))
+        return nullptr;
+      Value *NewIncomingVal =
+          getFreelyInvertedImpl(U.get(), /*WillInvertAllUses=*/false,
+                                /*Builder=*/nullptr, DoesConsume, Depth);
+      if (NewIncomingVal == nullptr)
+        return nullptr;
+      // Make sure that we can safely erase the original PHI node.
+      if (NewIncomingVal == V)
+        return nullptr;
----------------
nikic wrote:

I think this is related to freelyInvertAllUsersOf() used for de-Morgan patterns. I think your check is fine.

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


More information about the llvm-commits mailing list