[llvm] [InstCombine] Fold icmp (zext X), (and (trunc Y), Mask) -> icmp X, trunc Y (PR #221189)
armandeep singh via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 12:13:37 PDT 2026
================
@@ -0,0 +1,98 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Exact shape from issue #115466 — ugt, not eq.
+define i1 @test_zext_trunc_and_ugt(i16 %x, i64 %y) {
+; CHECK-LABEL: define i1 @test_zext_trunc_and_ugt(
+; CHECK-SAME: i16 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[Y]] to i16
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i16 [[X]], [[TMP1]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = zext i16 %x to i32
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 %trunc, 65535
+ %cmp = icmp ugt i32 %ext, %mask
+ ret i1 %cmp
+}
+
+; AND operands reversed (mask first) — needs m_c_And to catch this.
+define i1 @test_zext_trunc_and_ugt_commuted(i16 %x, i64 %y) {
+; CHECK-LABEL: define i1 @test_zext_trunc_and_ugt_commuted(
+; CHECK-SAME: i16 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[Y]] to i16
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i16 [[X]], [[TMP1]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = zext i16 %x to i32
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 65535, %trunc
+ %cmp = icmp ugt i32 %ext, %mask
+ ret i1 %cmp
+}
+
+; Signed predicate on a value that cannot be proven non-negative — must NOT fold.
+define i1 @test_signed_cmp_not_folded(i32 %ext, i64 %y) {
+; CHECK-LABEL: define i1 @test_signed_cmp_not_folded(
+; CHECK-SAME: i32 [[EXT:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[Y]] to i32
+; CHECK-NEXT: [[MASK:%.*]] = and i32 [[TRUNC]], 65535
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[EXT]], [[MASK]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 %trunc, 65535
+ %cmp = icmp sgt i32 %ext, %mask
+ ret i1 %cmp
+}
+
+; AND has another use — must NOT fold.
+define i1 @test_and_multiuse_not_folded(i16 %x, i64 %y, ptr %p) {
+; CHECK-LABEL: define i1 @test_and_multiuse_not_folded(
+; CHECK-SAME: i16 [[X:%.*]], i64 [[Y:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[X]] to i32
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[Y]] to i32
+; CHECK-NEXT: [[MASK:%.*]] = and i32 [[TRUNC]], 65535
+; CHECK-NEXT: store i32 [[MASK]], ptr [[P]], align 4
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i32 [[MASK]], [[EXT]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = zext i16 %x to i32
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 %trunc, 65535
+ store i32 %mask, ptr %p
+ %cmp = icmp ugt i32 %ext, %mask
+ ret i1 %cmp
+}
+
+; Mask too large for X's width — must NOT fold.
+define i1 @negative_mask_too_big(i16 %x, i64 %y) {
+; CHECK-LABEL: define i1 @negative_mask_too_big(
+; CHECK-SAME: i16 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[X]] to i32
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[Y]] to i32
+; CHECK-NEXT: [[MASK:%.*]] = and i32 [[TRUNC]], 131071
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[MASK]], [[EXT]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = zext i16 %x to i32
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 %trunc, 131071
+ %cmp = icmp eq i32 %ext, %mask
+ ret i1 %cmp
+}
+; Direct ult form — zext stays at operand(0), no ugt-swap canonicalization needed.
+; Exercises the original (non-mirrored) fold path in foldICmpWithZextOrSext.
+define i1 @test_zext_trunc_and_ult_direct(i16 %x, i64 %y) {
+; CHECK-LABEL: define i1 @test_zext_trunc_and_ult_direct(
+; CHECK-SAME: i16 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[Y]] to i16
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i16 [[X]], [[TMP1]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = zext i16 %x to i32
+ %trunc = trunc i64 %y to i32
+ %mask = and i32 %trunc, 65535
+ %cmp = icmp ult i32 %mask, %ext
+ ret i1 %cmp
+}
----------------
armandeep23947 wrote:
checked, this was redundant with the existing ugt test in foldICmpBinOp, so I've removed it.
https://github.com/llvm/llvm-project/pull/221189
More information about the llvm-commits
mailing list