[llvm] [SCCP] Support constant structure in PhiNode (PR #163713)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 16 10:26:55 PDT 2025


================
@@ -2125,6 +2143,26 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
   }
 }
 
+bool SCCPInstVisitor::isInstUnderDefined(Instruction &Inst) {
+  // For structure Type, we handle each member seperately.
+  // A structure object won't be considered as overDefined when
+  // there is at least one member can become constant.
+  bool IsOverDefined = true;
+  if (StructType *STy = dyn_cast<StructType>(Inst.getType())) {
+    for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i) {
+      ValueLatticeElement PNState = getStructValueState(&Inst, i);
+      IsOverDefined &= PNState.isOverdefined();
+      if (!IsOverDefined)
+        break;
+    }
+  } else {
+    ValueLatticeElement PhiState = getValueState(&Inst);
+    IsOverDefined &= PhiState.isOverdefined();
+  }
+
+  return IsOverDefined;
----------------
dtcxzyw wrote:

```suggestion
  if (StructType *STy = dyn_cast<StructType>(Inst.getType())) {
    for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i) {
      if (!getStructValueState(&Inst, i).isOverdefined())
        return false;
    }
  }

    return getValueState(&Inst).isOverdefined();
```
Use early return; Avoid copying LatticeVal

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


More information about the llvm-commits mailing list