diff --git a/llvm/test/Transforms/InstSimplify/select_or_and.ll b/llvm/test/Transforms/InstSimplify/select_or_and.ll --- a/llvm/test/Transforms/InstSimplify/select_or_and.ll +++ b/llvm/test/Transforms/InstSimplify/select_or_and.ll @@ -200,6 +200,61 @@ ret i32 %cond } +define i32 @select_icmp_and_eq_commuted(i32 %a, i32 %b) { +; CHECK-LABEL: @select_icmp_and_eq_commuted( +; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[AND]], -1 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[TOBOOL]], i32 [[B]], i32 -1 +; CHECK-NEXT: ret i32 [[COND]] +; + %and = and i32 %a, %b + %tobool = icmp eq i32 %and, -1 + %cond = select i1 %tobool, i32 %b, i32 -1 + ret i32 %cond +} + +; https://alive2.llvm.org/ce/z/HfYXvx +define <2 x i16> @select_icmp_and_eq_vec(<2 x i16> %a, <2 x i16> %b) { +; CHECK-LABEL: @select_icmp_and_eq_vec( +; CHECK-NEXT: [[AND:%.*]] = and <2 x i16> [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq <2 x i16> [[AND]], +; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[TOBOOL]], <2 x i16> [[A]], <2 x i16> +; CHECK-NEXT: ret <2 x i16> [[COND]] +; + %and = and <2 x i16> %a, %b + %tobool = icmp eq <2 x i16> %and, + %cond = select <2 x i1> %tobool, <2 x i16> %a, <2 x i16> + ret <2 x i16> %cond +} + +; The ne should also be macthed +define i32 @select_icmp_and_ne(i32 %a, i32 %b) { +; CHECK-LABEL: @select_icmp_and_ne( +; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[AND]], -1 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[TOBOOL]], i32 -1, i32 [[A]] +; CHECK-NEXT: ret i32 [[COND]] +; + %and = and i32 %a, %b + %tobool = icmp ne i32 %and, -1 + %cond = select i1 %tobool, i32 -1, i32 %a + ret i32 %cond +} + +; Negative test: Incorrect const value for icmp +define i32 @select_icmp_and_eq_incorrect_const(i32 %a, i32 %b) { +; CHECK-LABEL: @select_icmp_and_eq_incorrect_const( +; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[AND]], 0 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[TOBOOL]], i32 [[A]], i32 -1 +; CHECK-NEXT: ret i32 [[COND]] +; + %and = and i32 %a, %b + %tobool = icmp eq i32 %and, 0 ; Incorrect const value + %cond = select i1 %tobool, i32 %a, i32 -1 + ret i32 %cond +} + ; https://alive2.llvm.org/ce/z/hSyCuR define i32 @select_icmp_or_eq(i32 %a, i32 %b) { ; CHECK-LABEL: @select_icmp_or_eq(