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

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 11 12:44:13 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)
----------------
andjo403 wrote:

Thanks for the help with how to make the test.
I do not think that it needs to be handled here as the condition in the branch will be replaced by a constant so it will not be in the DomConditionCache.
see https://github.com/llvm/llvm-project/blob/72436b37bf4203ee43395c65cc179dc573f79251/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp#L3787-L3791

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


More information about the llvm-commits mailing list