Files
clang-p2996/llvm/test/Transforms/InstCombine/sub-xor-cmp.ll
Nikita Popov a105877646 [InstCombine] Remove some of the complexity-based canonicalization (#91185)
The idea behind this canonicalization is that it allows us to handle less
patterns, because we know that some will be canonicalized away. This is
indeed very useful to e.g. know that constants are always on the right.

However, this is only useful if the canonicalization is actually
reliable. This is the case for constants, but not for arguments: Moving
these to the right makes it look like the "more complex" expression is
guaranteed to be on the left, but this is not actually the case in
practice. It fails as soon as you replace the argument with another
instruction.

The end result is that it looks like things correctly work in tests,
while they actually don't. We use the "thwart complexity-based
canonicalization" trick to handle this in tests, but it's often a
challenge for new contributors to get this right, and based on the
regressions this PR originally exposed, we clearly don't get this right
in many cases.

For this reason, I think that it's better to remove this complexity
canonicalization. It will make it much easier to write tests for
commuted cases and make sure that they are handled.
2024-08-21 12:02:54 +02:00

204 lines
6.1 KiB
LLVM

; 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 @sext_xor_sub(i64 %a, i1 %b) {
; CHECK-LABEL: define i64 @sext_xor_sub(
; 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 @sext_xor_sub_1(i64 %a, i1 %b) {
; CHECK-LABEL: define i64 @sext_xor_sub_1(
; 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 %c, %a
%r = sub i64 %d, %c
ret i64 %r
}
define i64 @sext_xor_sub_2(i64 %a, i1 %b) {
; CHECK-LABEL: define i64 @sext_xor_sub_2(
; 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 @sext_xor_sub_3(i64 %a, i1 %b) {
; CHECK-LABEL: define i64 @sext_xor_sub_3(
; 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 %c, %a
%r = sub i64 %c, %d
ret i64 %r
}
; Sext non boolean type.
define i64 @sext_non_bool_xor_sub(i64 %a, i8 %b) {
; CHECK-LABEL: define i64 @sext_non_bool_xor_sub(
; CHECK-SAME: i64 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = sext i8 [[B]] to i64
; CHECK-NEXT: [[D:%.*]] = xor i64 [[A]], [[C]]
; CHECK-NEXT: [[R:%.*]] = sub i64 [[D]], [[C]]
; CHECK-NEXT: ret i64 [[R]]
;
%c = sext i8 %b to i64
%d = xor i64 %a, %c
%r = sub i64 %d, %c
ret i64 %r
}
define i64 @sext_non_bool_xor_sub_1(i64 %a, i8 %b) {
; CHECK-LABEL: define i64 @sext_non_bool_xor_sub_1(
; CHECK-SAME: i64 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = sext i8 [[B]] to i64
; CHECK-NEXT: [[D:%.*]] = xor i64 [[A]], [[C]]
; CHECK-NEXT: [[R:%.*]] = sub i64 [[D]], [[C]]
; CHECK-NEXT: ret i64 [[R]]
;
%c = sext i8 %b to i64
%d = xor i64 %c, %a
%r = sub i64 %d, %c
ret i64 %r
}
; Different boolean values.
define i64 @sext_diff_i1_xor_sub(i64 %a, i1 %b, i1 %c) {
; CHECK-LABEL: define i64 @sext_diff_i1_xor_sub(
; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]], i1 [[C:%.*]]) {
; CHECK-NEXT: [[D:%.*]] = sext i1 [[B]] to i64
; CHECK-NEXT: [[E_NEG:%.*]] = zext i1 [[C]] to i64
; CHECK-NEXT: [[R:%.*]] = add nsw i64 [[E_NEG]], [[D]]
; CHECK-NEXT: ret i64 [[R]]
;
%d = sext i1 %b to i64
%e = sext i1 %c to i64
%f = xor i64 %a, %d
%r = sub i64 %d, %e
ret i64 %r
}
define i64 @sext_diff_i1_xor_sub_1(i64 %a, i1 %b, i1 %c) {
; CHECK-LABEL: define i64 @sext_diff_i1_xor_sub_1(
; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]], i1 [[C:%.*]]) {
; CHECK-NEXT: [[D:%.*]] = sext i1 [[B]] to i64
; CHECK-NEXT: [[E_NEG:%.*]] = zext i1 [[C]] to i64
; CHECK-NEXT: [[R:%.*]] = add nsw i64 [[E_NEG]], [[D]]
; CHECK-NEXT: ret i64 [[R]]
;
%d = sext i1 %b to i64
%e = sext i1 %c to i64
%f = xor i64 %d, %a
%r = sub i64 %d, %e
ret i64 %r
}
; (sext C) has multiple uses.
define i64 @sext_multi_uses(i64 %a, i1 %b, i64 %x) {
; CHECK-LABEL: define i64 @sext_multi_uses(
; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[X]], [[A]]
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 0, [[TMP1]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i64 [[TMP2]], i64 [[A]]
; CHECK-NEXT: ret i64 [[R]]
;
%c = sext i1 %b to i64
%d = xor i64 %a, %c
%e = sub i64 %d, %c
%f = mul i64 %x, %c
%r = add i64 %f, %e
ret i64 %r
}
; (xor X, (sext C)) has multiple uses.
define i64 @xor_multi_uses(i64 %a, i1 %b, i64 %x) {
; CHECK-LABEL: define i64 @xor_multi_uses(
; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = sext i1 [[B]] to i64
; CHECK-NEXT: [[D:%.*]] = xor i64 [[A]], [[C]]
; CHECK-NEXT: [[E:%.*]] = sub i64 [[D]], [[C]]
; CHECK-NEXT: [[F:%.*]] = mul i64 [[X]], [[D]]
; CHECK-NEXT: [[R:%.*]] = add i64 [[F]], [[E]]
; CHECK-NEXT: ret i64 [[R]]
;
%c = sext i1 %b to i64
%d = xor i64 %a, %c
%e = sub i64 %d, %c
%f = mul i64 %x, %d
%r = add i64 %f, %e
ret i64 %r
}
define i64 @absdiff(i64 %a, i64 %b) {
; CHECK-LABEL: define i64 @absdiff(
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = icmp ult i64 [[A]], [[B]]
; CHECK-NEXT: [[D:%.*]] = sub i64 [[A]], [[B]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[D]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 [[TMP1]], i64 [[D]]
; CHECK-NEXT: ret i64 [[RES]]
;
%c = icmp ult i64 %a, %b
%c.ext = sext i1 %c to i64
%d = sub i64 %a, %b
%may.rev = xor i64 %c.ext, %d
%res = sub i64 %may.rev, %c.ext
ret i64 %res
}
; Commuted xor operands compared to absdiff.
define i64 @absdiff1(i64 %a, i64 %b) {
; CHECK-LABEL: define i64 @absdiff1(
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = icmp ult i64 [[A]], [[B]]
; CHECK-NEXT: [[D:%.*]] = sub i64 [[A]], [[B]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[D]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 [[TMP1]], i64 [[D]]
; CHECK-NEXT: ret i64 [[RES]]
;
%c = icmp ult i64 %a, %b
%c.ext = sext i1 %c to i64
%d = sub i64 %a, %b
%may.rev = xor i64 %d, %c.ext
%res = sub i64 %may.rev, %c.ext
ret i64 %res
}
; Use ugt as compare cond.
define i64 @absdiff2(i64 %a, i64 %b) {
; CHECK-LABEL: define i64 @absdiff2(
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = icmp ugt i64 [[A]], [[B]]
; CHECK-NEXT: [[D:%.*]] = sub i64 [[B]], [[A]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[D]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 [[TMP1]], i64 [[D]]
; CHECK-NEXT: ret i64 [[RES]]
;
%c = icmp ugt i64 %a, %b
%c.ext = sext i1 %c to i64
%d = sub i64 %b, %a
%may.rev = xor i64 %d, %c.ext
%res = sub i64 %may.rev, %c.ext
ret i64 %res
}