[llvm] [InstCombine] Simplify phi using KnownBits of condition (PR #134712)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 11 00:47:20 PDT 2025


================
@@ -1625,6 +1625,44 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
     return replaceInstUsesWith(PN, &IdenticalPN);
   }
 
+  if (PN.getType()->isIntegerTy()) {
+    BasicBlock *PNBB = PN.getParent();
+    KnownBits Known(PN.getType()->getScalarSizeInBits());
+    bool MadeChange = false;
+    for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
+      Value *V = PN.getIncomingValue(I);
+      if (isa<ConstantInt>(V))
+        continue;
+
+      ArrayRef<BranchInst *> BRs = SQ.DC->conditionsFor(V);
+      if (BRs.empty())
+        continue;
+
+      Known.resetAll();
+      Instruction *CtxI = PN.getIncomingBlock(I)->getTerminator();
+      SimplifyQuery Q = SQ.getWithInstruction(CtxI);
+      BranchInst *BI = dyn_cast<BranchInst>(CtxI);
+      if (BI && BI->isConditional() && llvm::is_contained(BRs, BI)) {
+        CondContext CC(BI->getCondition());
+        CC.AffectedValues.insert(V);
+        if (BI->getSuccessor(1) == PNBB)
----------------
nikic wrote:

https://godbolt.org/z/vacTYPoha is the way to spell it.

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


More information about the llvm-commits mailing list