[PATCH] D93888: [GVN] If zext X == N or sext X == N, then X == trunc N.

Chenguang Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 28 23:26:59 PST 2020


wecing created this revision.
wecing added a reviewer: lebedev.ri.
Herald added a subscriber: hiraditya.
wecing requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

As discussed in https://reviews.llvm.org/D93850, this seems to be a bug
of GVN.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93888

Files:
  llvm/lib/Transforms/Scalar/GVN.cpp
  llvm/test/Transforms/GVN/zext-eq-const-int.ll


Index: llvm/test/Transforms/GVN/zext-eq-const-int.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GVN/zext-eq-const-int.ll
@@ -0,0 +1,45 @@
+; RUN: opt -gvn -S < %s | FileCheck %s
+
+define i32 @zext-eq-const-int(i32 %x) {
+; CHECK-LABEL: @zext-eq-const-int(
+; CHECK:       bb9:
+; CHECK-NEXT:    br i1 true, label %t
+;
+entry:
+  %_x = zext i32 %x to i64
+  switch i64 %_x, label %bb.end [
+    i64 3, label %bb9
+  ]
+
+bb9:
+  %cond = icmp eq i32 %x, 3
+  br i1 %cond, label %t, label %bb.end
+
+t:
+  ret i32 42
+
+bb.end:
+  ret i32 9999
+}
+
+define i32 @sext-eq-const-int(i32 %x) {
+; CHECK-LABEL: @sext-eq-const-int(
+; CHECK:       bb9:
+; CHECK-NEXT:    br i1 true, label %t
+;
+entry:
+  %_x = sext i32 %x to i64
+  switch i64 %_x, label %bb.end [
+    i64 4294967299, label %bb9 ; 0x1_0000_0003
+  ]
+
+bb9:
+  %cond = icmp eq i32 %x, 3
+  br i1 %cond, label %t, label %bb.end
+
+t:
+  ret i32 42
+
+bb.end:
+  ret i32 9999
+}
Index: llvm/lib/Transforms/Scalar/GVN.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/GVN.cpp
+++ llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2069,6 +2069,20 @@
         MD->invalidateCachedPointerInfo(LHS);
     }
 
+    // If (zext X) == N or (sext X) == N, then X == (trunc N).
+    Value *X;
+    if (match(LHS, m_ZExtOrSExt(m_Value(X)))) {
+      unsigned Width = X->getType()->getIntegerBitWidth();
+      ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
+      if (CI) {
+        Value *NewRHS =
+            ConstantInt::get(IntegerType::get(X->getContext(), Width),
+                             CI->getValue().trunc(Width));
+        Worklist.push_back(std::make_pair(X, NewRHS));
+        continue;
+      }
+    }
+
     // Now try to deduce additional equalities from this one. For example, if
     // the known equality was "(A != B)" == "false" then it follows that A and B
     // are equal in the scope. Only boolean equalities with an explicit true or


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93888.313936.patch
Type: text/x-patch
Size: 2020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201229/0faa5b7d/attachment.bin>


More information about the llvm-commits mailing list