[llvm] [InstCombine] fold commutative binop with const into select (PR #183692)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 03:56:08 PST 2026


================
@@ -557,6 +557,18 @@ define i32 @OrSelectIcmpZero(i32 %a, i32 %b) {
   ret i32 %or
 }
 
+define i32 @OrSelectIcmpZeroCommuted(i32 %a, i32 %b) {
+; CHECK-LABEL: @OrSelectIcmpZeroCommuted(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[A]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %cmp = icmp eq i32 %a, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or = or i32 %a, %sel
+  ret i32 %or
+}
----------------
nikic wrote:

It looks like the new test here is identical to swap_operand_order in or-select-zero-icmp.ll.

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


More information about the llvm-commits mailing list