[llvm] Fix Failure to fold (and %x, (sext i1 %m)) -> (select %m, %x, 0) with multiple uses of %m Resolve #81288 (PR #81409)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 15 03:40:04 PST 2024
================
@@ -2768,3 +2768,54 @@ define i32 @add_constant_equal_with_the_top_bit_of_demandedbits_insertpt(i32 %x,
%and = and i32 %or, 24
ret i32 %and
}
+
+define i32 @and_sext_multiuse(i32 %x, i32 %y, i32 %a, i32 %b) {
+; CHECK-LABEL: @and_sext_multiuse(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[ADD:%.*]] = select i1 [[CMP]], i32 [[TMP1]], i32 0
+; CHECK-NEXT: ret i32 [[ADD]]
+;
+ %cmp = icmp sgt i32 %x, %y
+ %sext = sext i1 %cmp to i32
+ %and1 = and i32 %sext, %a
+ %and2 = and i32 %sext, %b
+ %add = add i32 %and1, %and2
+ ret i32 %add
+}
+
+define <2 x i32> @and_sext_multiuse_with_vector(<2 x i32> %x, <2 x i32> %y, <2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: @and_sext_multiuse_with_vector(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[ADD:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[TMP1]], <2 x i32> zeroinitializer
+; CHECK-NEXT: ret <2 x i32> [[ADD]]
+;
+ %cmp = icmp sgt <2 x i32> %x, %y
+ %sext = sext <2 x i1> %cmp to <2 x i32>
+ %and1 = and <2 x i32> %a, %sext
+ %and2 = and <2 x i32> %b, %sext
+ %add = add <2 x i32> %and1, %and2
+ ret <2 x i32> %add
+}
+
+define <2 x i32> @and_sext_multiuse_commuted(<2 x i32> %x, <2 x i32> %y, <2 x i32> %a, <2 x i32> %b, <2 x i32> %c, <2 x i32> %d) {
+; CHECK-LABEL: @and_sext_multiuse_commuted(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = add <2 x i32> [[C:%.*]], [[D:%.*]]
+; CHECK-NEXT: [[TMP3:%.*]] = add <2 x i32> [[TMP1]], [[TMP2]]
+; CHECK-NEXT: [[ADD3:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[TMP3]], <2 x i32> zeroinitializer
+; CHECK-NEXT: ret <2 x i32> [[ADD3]]
+;
+ %cmp = icmp sgt <2 x i32> %x, %y
+ %sext = sext <2 x i1> %cmp to <2 x i32>
+ %and1 = and <2 x i32> %sext, %a
+ %and2 = and <2 x i32> %b, %sext
+ %and3 = and <2 x i32> %c, %sext
+ %and4 = and <2 x i32> %sext, %d
+ %add1 = add <2 x i32> %and1, %and2
+ %add2 = add <2 x i32> %and3, %and4
+ %add3 = add <2 x i32> %add1, %add2
----------------
nikic wrote:
Why does this test need so many instructions?
https://github.com/llvm/llvm-project/pull/81409
More information about the llvm-commits
mailing list