[llvm] r318817 - [SCCP] Pick the right lattice value for constants.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 19:04:55 PST 2017


Author: davide
Date: Tue Nov 21 19:04:55 2017
New Revision: 318817

URL: http://llvm.org/viewvc/llvm-project?rev=318817&view=rev
Log:
[SCCP] Pick the right lattice value for constants.

After the dataflow algorithm proves that an argument is constant,
it replaces it value with the integer constant and drops the lattice
value associated to the DEF.

e.g. in the example we have @f() that's called twice:
call @f(undef, ...)
call @f(2, ...)

`undef` MEET 2 = 2 so we replace the argument and all its uses with
the constant 2.

Shortly after, tryToReplaceWithConstantRange() tries to get the lattice
value for the argument we just replaced, causing an assertion.
This function is a little peculiar as it runs when we're doing replacement
and not as part of the solver but still queries the solver.

The fix is that of checking whether we replaced the value already and
get a temporary lattice value for the constant.

Thanks to Zhendong Su for the report!

Fixes PR35357.

Added:
    llvm/trunk/test/Transforms/SCCP/pr35357.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/SCCP.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=318817&r1=318816&r2=318817&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp Tue Nov 21 19:04:55 2017
@@ -1615,8 +1615,15 @@ static bool tryToReplaceWithConstantRang
     if (!Icmp || !Solver.isBlockExecutable(Icmp->getParent()))
       continue;
 
-    auto A = Solver.getLatticeValueFor(Icmp->getOperand(0));
-    auto B = Solver.getLatticeValueFor(Icmp->getOperand(1));
+    auto getIcmpLatticeValue = [&](Value *Op) {
+      if (auto *C = dyn_cast<Constant>(Op))
+        return ValueLatticeElement::get(C);
+      return Solver.getLatticeValueFor(Op);
+    };
+
+    ValueLatticeElement A = getIcmpLatticeValue(Icmp->getOperand(0));
+    ValueLatticeElement B = getIcmpLatticeValue(Icmp->getOperand(1));
+
     Constant *C = nullptr;
     if (A.satisfiesPredicate(Icmp->getPredicate(), B))
       C = ConstantInt::getTrue(Icmp->getType());

Added: llvm/trunk/test/Transforms/SCCP/pr35357.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SCCP/pr35357.ll?rev=318817&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/SCCP/pr35357.ll (added)
+++ llvm/trunk/test/Transforms/SCCP/pr35357.ll Tue Nov 21 19:04:55 2017
@@ -0,0 +1,24 @@
+; RUN: opt -S %s -ipsccp | FileCheck %s
+
+ at a = internal global i32 2
+
+define i32 @patatino() {
+; CHECK: @patatino(
+; CHECK: call void @f(i32 undef, i32 1)
+; CHECK-NEXT: call void @f(i32 2, i32 0)
+; CHECK-NEXT: ret i32 0
+entry:
+  call void @f(i32 undef, i32 1)
+  %0 = load i32, i32* @a
+  call void @f(i32 %0, i32 0)
+  ret i32 0
+}
+
+define internal void @f(i32 %c, i32 %d) {
+; CHECK: @f(
+; CHECK:    ret void
+;
+entry:
+  %cmp = icmp ne i32 %c, %d
+  ret void
+}




More information about the llvm-commits mailing list