[llvm] [InstCombine] fold icmp of select with invertible shl (PR #147182)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 22 08:18:11 PDT 2025


================
@@ -835,3 +835,41 @@ define i1 @discr_eq_constantexpr(ptr %p) {
   %cmp = icmp eq i64 %sub, -1
   ret i1 %cmp
 }
+
+define i1 @shl_nsw_eq(i8 %a, i1 %cond) {
+; CHECK-LABEL: @shl_nsw_eq(
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i8 [[A1:%.*]], 3
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i8 8, i8 0
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl = shl nsw i8 %a, 3
+  %sel = select i1 %cond, i8 8, i8 0
----------------
nikic wrote:

Should also test this with constants that show the lshr / ashr difference (negative number).

https://github.com/llvm/llvm-project/pull/147182


More information about the llvm-commits mailing list