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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 6 16:11:48 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;
----------------
dtcxzyw wrote:

See the test `@test_inv_free_loop`. TBH I don't know why it happens. If the PHI node is used twice, `WillInvertAllUses` should be false.


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


More information about the llvm-commits mailing list