[llvm] 6de41e6 - [ValueLattice] Fix getCompare() for undef values

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 08:41:32 PDT 2022


Author: Nikita Popov
Date: 2022-11-01T16:41:24+01:00
New Revision: 6de41e6d7652b74a5aa2bd78b0597cc53a8c3c2a

URL: https://github.com/llvm/llvm-project/commit/6de41e6d7652b74a5aa2bd78b0597cc53a8c3c2a
DIFF: https://github.com/llvm/llvm-project/commit/6de41e6d7652b74a5aa2bd78b0597cc53a8c3c2a.diff

LOG: [ValueLattice] Fix getCompare() for undef values

Resolve the TODO about incorrect getCompare() behavior. This can
be made more precise (e.g. by materializing the undef value and
performing constant folding on it), but for now just return an
unknown result.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/ValueLattice.h
    llvm/lib/Transforms/Utils/SCCPSolver.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ValueLattice.h b/llvm/include/llvm/Analysis/ValueLattice.h
index bc6b279e9ed52..7fe45d9f4dc96 100644
--- a/llvm/include/llvm/Analysis/ValueLattice.h
+++ b/llvm/include/llvm/Analysis/ValueLattice.h
@@ -453,8 +453,14 @@ class ValueLatticeElement {
   /// evaluated.
   Constant *getCompare(CmpInst::Predicate Pred, Type *Ty,
                        const ValueLatticeElement &Other) const {
-    if (isUnknownOrUndef() || Other.isUnknownOrUndef())
-      return UndefValue::get(Ty);
+    // Not yet resolved.
+    if (isUnknown() || Other.isUnknown())
+      return nullptr;
+
+    // TODO: Can be made more precise, but always returning undef would be
+    // incorrect.
+    if (isUndef() || isUndef())
+      return nullptr;
 
     if (isConstant() && Other.isConstant())
       return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());

diff  --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index a3455577a35c5..3d467f2fd68f9 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -1044,9 +1044,6 @@ void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
 
   Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State);
   if (C) {
-    // TODO: getCompare() currently has incorrect handling for unknown/undef.
-    if (isa<UndefValue>(C))
-      return;
     ValueLatticeElement CV;
     CV.markConstant(C);
     mergeInValue(&I, CV);


        


More information about the llvm-commits mailing list