[llvm] [InstCombine] Fold (sub (xor X, (sext C)), (sext C)) => (select C (neg X), X) (PR #79417)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 31 04:01:39 PST 2024
================
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i64 @foo(i64 %a, i1 %b) {
+; CHECK-LABEL: define i64 @foo(
+; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[A]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i64 [[TMP1]], i64 [[A]]
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %c = sext i1 %b to i64
+ %d = xor i64 %a, %c
+ %r = sub i64 %d, %c
+ ret i64 %r
+}
+
+define i64 @bar(i64 %a, i1 %b) {
+; CHECK-LABEL: define i64 @bar(
+; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[A]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i64 [[A]], i64 [[TMP1]]
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %c = sext i1 %b to i64
+ %d = xor i64 %a, %c
+ %r = sub i64 %c, %d
+ ret i64 %r
+}
+
+define i64 @absdiff(i64 %0, i64 %1) {
+; CHECK-LABEL: define i64 @absdiff(
+; CHECK-SAME: i64 [[TMP0:%.*]], i64 [[TMP1:%.*]]) {
+; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i64 [[TMP0]], [[TMP1]]
+; CHECK-NEXT: [[TMP4:%.*]] = sub i64 [[TMP0]], [[TMP1]]
+; CHECK-NEXT: [[TMP5:%.*]] = sub i64 0, [[TMP4]]
+; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i64 [[TMP5]], i64 [[TMP4]]
+; CHECK-NEXT: ret i64 [[TMP6]]
+;
+ %3 = icmp ult i64 %0, %1
+ %4 = sext i1 %3 to i64
+ %5 = sub i64 %0, %1
+ %6 = xor i64 %5, %4
+ %7 = sub i64 %6, %4
+ ret i64 %7
----------------
nikic wrote:
Don't use unnamed values.
https://github.com/llvm/llvm-project/pull/79417
More information about the llvm-commits
mailing list