[llvm] [InstCombine] Extend Phi-Icmp use to include or (PR #67682)

David Green via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 1 11:30:44 PDT 2023


================
@@ -1441,36 +1441,56 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
         PHIUser->user_back() == &PN) {
       return replaceInstUsesWith(PN, PoisonValue::get(PN.getType()));
     }
-    // When a PHI is used only to be compared with zero, it is safe to replace
-    // an incoming value proved as known nonzero with any non-zero constant.
-    // For example, in the code below, the incoming value %v can be replaced
-    // with any non-zero constant based on the fact that the PHI is only used to
-    // be compared with zero and %v is a known non-zero value:
-    // %v = select %cond, 1, 2
-    // %p = phi [%v, BB] ...
-    //      icmp eq, %p, 0
-    auto *CmpInst = dyn_cast<ICmpInst>(PHIUser);
-    // FIXME: To be simple, handle only integer type for now.
-    if (CmpInst && isa<IntegerType>(PN.getType()) && CmpInst->isEquality() &&
-        match(CmpInst->getOperand(1), m_Zero())) {
-      ConstantInt *NonZeroConst = nullptr;
-      bool MadeChange = false;
-      for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
-        Instruction *CtxI = PN.getIncomingBlock(I)->getTerminator();
-        Value *VA = PN.getIncomingValue(I);
-        if (isKnownNonZero(VA, DL, 0, &AC, CtxI, &DT)) {
-          if (!NonZeroConst)
-            NonZeroConst = getAnyNonZeroConstInt(PN);
+  }
 
-          if (NonZeroConst != VA) {
-            replaceOperand(PN, I, NonZeroConst);
-            MadeChange = true;
-          }
+  // When a PHI is used only to be compared with zero, it is safe to replace
+  // an incoming value proved as known nonzero with any non-zero constant.
+  // For example, in the code below, the incoming value %v can be replaced
+  // with any non-zero constant based on the fact that the PHI is only used to
+  // be compared with zero and %v is a known non-zero value:
+  // %v = select %cond, 1, 2
+  // %p = phi [%v, BB] ...
+  //      icmp eq, %p, 0
+  // FIXME: To be simple, handle only integer type for now.
+  // Extend to 2 use of phi -> icmp and or(icmp)
----------------
davemgreen wrote:

Can this comment be expanded to more fully explain the use checks, why it's limited to 2 and whatnot

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


More information about the llvm-commits mailing list