[llvm] [InstCombine] Enable select freeze poison folding when storing value (PR #129776)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 19 06:09:08 PDT 2025


================
@@ -4840,10 +4845,27 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
     return replaceInstUsesWith(I, getUndefReplacement(I.getType()));
   }
 
+  auto getFreezeVectorReplacement = [](Constant *C) -> Constant * {
+    Type *Ty = C->getType();
+    auto *VTy = dyn_cast<FixedVectorType>(Ty);
+    if (!VTy)
+      return nullptr;
+    unsigned NumElts = VTy->getNumElements();
+    Constant *BestValue = Constant::getNullValue(VTy->getScalarType());
+    for (unsigned i = 0; i != NumElts; ++i) {
+      Constant *EltC = C->getAggregateElement(i);
+      if (EltC && !match(EltC, m_Undef())) {
+        BestValue = EltC;
+        break;
+      }
+    }
+    return Constant::replaceUndefsWith(C, BestValue);
+  };
+
   Constant *C;
   if (match(Op0, m_Constant(C)) && C->containsUndefOrPoisonElement()) {
-    Constant *ReplaceC = getUndefReplacement(I.getType()->getScalarType());
-    return replaceInstUsesWith(I, Constant::replaceUndefsWith(C, ReplaceC));
+    if (auto Repl = getFreezeVectorReplacement(C))
----------------
dtcxzyw wrote:

```suggestion
    if (Constant *Repl = getFreezeVectorReplacement(C))
```

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


More information about the llvm-commits mailing list