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.
183 lines
5.8 KiB
LLVM
183 lines
5.8 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
|
|
|
|
define i32 @uaddo_commute1(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute1(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[Z:%.*]], i32 [[A]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp ugt i32 %x, %noty
|
|
%r = select i1 %c, i32 %z, i32 %a
|
|
ret i32 %r
|
|
}
|
|
|
|
define <2 x i32> @uaddo_commute2(<2 x i32> %x, <2 x i32> %y, <2 x i32> %z) {
|
|
; CHECK-LABEL: @uaddo_commute2(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor <2 x i32> [[Y:%.*]], <i32 -1, i32 -1>
|
|
; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[Y]], [[X:%.*]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> [[Z:%.*]], <2 x i32> [[A]]
|
|
; CHECK-NEXT: ret <2 x i32> [[R]]
|
|
;
|
|
%noty = xor <2 x i32> %y, <i32 -1, i32 -1>
|
|
%a = add <2 x i32> %y, %x
|
|
%c = icmp ugt <2 x i32> %x, %noty
|
|
%r = select <2 x i1> %c, <2 x i32> %z, <2 x i32> %a
|
|
ret <2 x i32> %r
|
|
}
|
|
|
|
define i32 @uaddo_commute3(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute3(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[Z:%.*]], i32 [[A]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp ult i32 %noty, %x
|
|
%r = select i1 %c, i32 %z, i32 %a
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_commute4(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute4(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[Y]], [[X:%.*]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[Z:%.*]], i32 [[A]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %y, %x
|
|
%c = icmp ult i32 %noty, %x
|
|
%r = select i1 %c, i32 %z, i32 %a
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_commute5(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute5(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 [[Z:%.*]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp ugt i32 %x, %noty
|
|
%r = select i1 %c, i32 %a, i32 %z
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_commute6(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute6(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[Y]], [[X:%.*]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 [[Z:%.*]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %y, %x
|
|
%c = icmp ugt i32 %x, %noty
|
|
%r = select i1 %c, i32 %a, i32 %z
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_commute7(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute7(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 [[Z:%.*]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp ult i32 %noty, %x
|
|
%r = select i1 %c, i32 %a, i32 %z
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_commute8(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_commute8(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[Y]], [[X:%.*]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 [[Z:%.*]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %y, %x
|
|
%c = icmp ult i32 %noty, %x
|
|
%r = select i1 %c, i32 %a, i32 %z
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_wrong_pred1(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_wrong_pred1(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[Z:%.*]], i32 [[A]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp ult i32 %x, %noty
|
|
%r = select i1 %c, i32 %z, i32 %a
|
|
ret i32 %r
|
|
}
|
|
|
|
define i32 @uaddo_wrong_pred2(i32 %x, i32 %y, i32 %z) {
|
|
; CHECK-LABEL: @uaddo_wrong_pred2(
|
|
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y:%.*]], -1
|
|
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y]]
|
|
; CHECK-NEXT: [[C_NOT:%.*]] = icmp ult i32 [[X]], [[NOTY]]
|
|
; CHECK-NEXT: [[R:%.*]] = select i1 [[C_NOT]], i32 [[A]], i32 [[Z:%.*]]
|
|
; CHECK-NEXT: ret i32 [[R]]
|
|
;
|
|
%noty = xor i32 %y, -1
|
|
%a = add i32 %x, %y
|
|
%c = icmp uge i32 %x, %noty
|
|
%r = select i1 %c, i32 %z, i32 %a
|
|
ret i32 %r
|
|
}
|
|
|
|
; icmp canonicalization should be consistent for these cases.
|
|
; Either the compare depends on the sum or not.
|
|
|
|
define i1 @uaddo_1(i8 %x, ptr %p) {
|
|
; CHECK-LABEL: @uaddo_1(
|
|
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], 1
|
|
; CHECK-NEXT: store i8 [[A]], ptr [[P:%.*]], align 1
|
|
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[A]], 0
|
|
; CHECK-NEXT: ret i1 [[C]]
|
|
;
|
|
%a = add i8 %x, 1
|
|
store i8 %a, ptr %p
|
|
%c = icmp ult i8 %a, 1
|
|
ret i1 %c
|
|
}
|
|
|
|
define i1 @uaddo_neg1(i8 %x, ptr %p) {
|
|
; CHECK-LABEL: @uaddo_neg1(
|
|
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -1
|
|
; CHECK-NEXT: store i8 [[A]], ptr [[P:%.*]], align 1
|
|
; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[X]], 0
|
|
; CHECK-NEXT: ret i1 [[C]]
|
|
;
|
|
%a = add i8 %x, -1
|
|
store i8 %a, ptr %p
|
|
%c = icmp ne i8 %a, -1
|
|
ret i1 %c
|
|
}
|
|
|