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

39 lines
1.2 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; %cmp should not vanish
define i1 @f(i32 %i1) {
; CHECK-LABEL: @f(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[I1:%.*]], 0
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 7, [[I1]]
; CHECK-NEXT: [[CMP4:%.*]] = icmp sgt i32 [[I1]], [[SHR]]
; CHECK-NEXT: [[I2:%.*]] = select i1 [[CMP]], i1 true, i1 [[CMP4]]
; CHECK-NEXT: ret i1 [[I2]]
;
entry:
%cmp = icmp slt i32 %i1, 0
%shr = ashr i32 7, %i1
%cmp4 = icmp sgt i32 %i1, %shr
%i2 = select i1 %cmp, i1 true, i1 %cmp4
ret i1 %i2
}
; %cmp should not vanish
define i32 @f2(i32 signext %g, i32 zeroext %h) {
; CHECK-LABEL: @f2(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[G:%.*]], 0
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 7, [[H:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[G]], [[SHR]]
; CHECK-NEXT: [[DOT0:%.*]] = select i1 [[CMP]], i1 true, i1 [[CMP1]]
; CHECK-NEXT: [[LOR_EXT:%.*]] = zext i1 [[DOT0]] to i32
; CHECK-NEXT: ret i32 [[LOR_EXT]]
;
%cmp = icmp slt i32 %g, 0
%shr = ashr i32 7, %h
%cmp1 = icmp sgt i32 %g, %shr
%.0 = select i1 %cmp, i1 true, i1 %cmp1
%lor.ext = zext i1 %.0 to i32
ret i32 %lor.ext
}