[PATCH] D46974: [NewGVN] Fix handling of assumes

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 16 12:45:51 PDT 2018


nikic created this revision.
nikic added a reviewer: dberlin.
Herald added subscribers: llvm-commits, Prazek.

This patch fixes two bugs:

- test1: Previously assume(a >= 5) concluded that a == 5. That's only valid for assume(a == 5)...
- test2: If operands were swapped, additional users were added to the wrong cmp operand. This resulted in an "unsettled iteration" assertion failure.


Repository:
  rL LLVM

https://reviews.llvm.org/D46974

Files:
  lib/Transforms/Scalar/NewGVN.cpp
  test/Transforms/NewGVN/assumes.ll


Index: test/Transforms/NewGVN/assumes.ll
===================================================================
--- /dev/null
+++ test/Transforms/NewGVN/assumes.ll
@@ -0,0 +1,26 @@
+; RUN: opt < %s -newgvn -S | FileCheck %s
+
+; CHECK-LABEL: @test1
+; CHECK: ret i32 %arg
+define i32 @test1(i32 %arg) {
+  %cmp = icmp sge i32 %arg, 5
+  call void @llvm.assume(i1 %cmp)
+  ret i32 %arg
+}
+
+; CHECK-LABEL: @test2
+; CHECK: ret i32 %arg
+define i32 @test2(i32 %arg, i1 %b) {
+  br label %bb
+
+bb:
+  %a = phi i32 [ 1, %0 ], [ 2, %bb ]
+  %cmp = icmp eq i32 %arg, %a
+  call void @llvm.assume(i1 %cmp)
+  br i1 %b, label %bb, label %end
+
+end:
+  ret i32 %arg
+}
+
+declare void @llvm.assume(i1 %cond)
Index: lib/Transforms/Scalar/NewGVN.cpp
===================================================================
--- lib/Transforms/Scalar/NewGVN.cpp
+++ lib/Transforms/Scalar/NewGVN.cpp
@@ -1585,11 +1585,11 @@
       SwappedOps ? Cmp->getSwappedPredicate() : Cmp->getPredicate();
 
   if (isa<PredicateAssume>(PI)) {
-    // If the comparison is true when the operands are equal, then we know the
-    // operands are equal, because assumes must always be true.
-    if (CmpInst::isTrueWhenEqual(Predicate)) {
+    // If we assume the operands are equal, then they are equal.
+    if (Predicate == CmpInst::ICMP_EQ) {
       addPredicateUsers(PI, I);
-      addAdditionalUsers(Cmp->getOperand(0), I);
+      addAdditionalUsers(SwappedOps ? Cmp->getOperand(1) : Cmp->getOperand(0),
+                         I);
       return createVariableOrConstant(FirstOp);
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46974.147160.patch
Type: text/x-patch
Size: 1567 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180516/7a0f7816/attachment.bin>


More information about the llvm-commits mailing list