[llvm] [InstSimplify] Fix Inconsistent PHI Simplification (PR #113037)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 3 16:45:01 PST 2024


================
@@ -5288,6 +5297,45 @@ Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx,
   return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
 }
 
+/// Try to fold the given arguments to a PHI function. If this is not
+/// possible, return nullptr.
+static Value *getCommonPHIValue(Value *PreviousCommon, Value *Current,
+                                const SimplifyQuery &Q) {
+  if (!PreviousCommon || PreviousCommon == Current)
+    return Current;
+
+  Constant *PreviousCommonC, *CurrentC;
+  if (match(PreviousCommon, m_Constant(PreviousCommonC)) &&
+      match(Current, m_Constant(CurrentC))) {
+    auto *Ty = PreviousCommonC->getType();
+    SmallVector<Constant *, 16> NewCommonC;
+    if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
+      if (getCommonValueForAggregateOrVector(VecTy->getNumElements(),
+                                             PreviousCommonC, CurrentC,
+                                             NewCommonC, Q))
+        return ConstantVector::get(NewCommonC);
+    } else if (auto *ArrayTy = dyn_cast<ArrayType>(Ty)) {
+      if (getCommonValueForAggregateOrVector(ArrayTy->getNumElements(),
+                                             PreviousCommonC, CurrentC,
+                                             NewCommonC, Q))
+        return ConstantArray::get(ArrayTy, NewCommonC);
+    } else if (auto *StructTy = dyn_cast<StructType>(Ty)) {
+      if (getCommonValueForAggregateOrVector(StructTy->getNumElements(),
----------------
arsenm wrote:

Can you test a case where the struct elements have different types? And more than 2 phi operands? 

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


More information about the llvm-commits mailing list