[llvm] [SCCP] Support structure in freeze (PR #226537)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 26 07:12:37 PDT 2026


================
@@ -1724,27 +1724,28 @@ void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
 }
 
 void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
-  // If this freeze returns a struct, just mark the result overdefined.
-  // TODO: We could do a lot better than this.
-  if (I.getType()->isStructTy())
-    return (void)markOverdefined(&I);
-
-  ValueLatticeElement V0State = getValueState(I.getOperand(0));
-  ValueLatticeElement &IV = ValueState[&I];
   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
   // discover a concrete value later.
-  if (IV.isOverdefined())
+  if (isInstFullyOverDefined(I))
     return (void)markOverdefined(&I);
 
-  // If something is unknown/undef, wait for it to resolve.
-  if (V0State.isUnknownOrUndef())
-    return;
+  forEachLatticeElement(&I, [&](auto GetValueState) {
+    ValueLatticeElement V0State = GetValueState(I.getOperand(0));
+    ValueLatticeElement &IV = GetValueState(&I);
+    // If something is unknown/undef, wait for it to resolve.
+    if (V0State.isUnknownOrUndef())
+      return;
 
-  if (SCCPSolver::isConstant(V0State) &&
-      isGuaranteedNotToBeUndefOrPoison(getConstant(V0State, I.getType())))
-    return (void)markConstant(IV, &I, getConstant(V0State, I.getType()));
+    // Freeze is a no-op on a constant that is guaranteed not to be undef or
+    // poison. Integer constants are represented as single-element ranges and
+    // can never be undef or poison.
----------------
nikic wrote:

This is not true, due to the existence of constantrange_including_undef. I think this was fine in the original code, because we would just refine the undef value to the non-undef value -- which is okay, as long as all uses of the freeze get replaced. But I don't think we can guarantee all uses being replaced if the freeze is of an aggregate.

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


More information about the llvm-commits mailing list