[llvm] Fix Failure to fold (and %x, (sext i1 %m)) -> (select %m, %x, 0) with multiple uses of %m Resolve #81288 (PR #81409)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 11 04:11:46 PST 2024


================
@@ -2768,3 +2768,18 @@ 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(i32 %x, i32 %y, i32 %a, i32 %b) {
+; CHECK-LABEL: @and_sext(
+; 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
+}
----------------
RKSimon wrote:

You need a better name - and_sext_multiuse at the very least. You also need to add some variants of the test with commuted operands / different types of use of the sext - maybe see what happens if you temporarily disable the and(sext(A), B) fold entirely and use the update script to see what other tests are dependent on this fold and what form they take.

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


More information about the llvm-commits mailing list