
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.
29 lines
769 B
LLVM
29 lines
769 B
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
|
|
|
|
define i32 @foo(i32 %x, i32 %y) {
|
|
; CHECK-LABEL: @foo(
|
|
; CHECK-NEXT: [[RES:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
|
|
; CHECK-NEXT: ret i32 [[RES]]
|
|
;
|
|
%add = add nsw i32 %y, %x
|
|
%mul = mul nsw i32 %add, %y
|
|
%square = mul nsw i32 %y, %y
|
|
%res = sub i32 %mul, %square
|
|
ret i32 %res
|
|
}
|
|
|
|
define i1 @bar(i64 %x, i64 %y) {
|
|
; CHECK-LABEL: @bar(
|
|
; CHECK-NEXT: [[Y1:%.*]] = xor i64 [[X:%.*]], -1
|
|
; CHECK-NEXT: [[B:%.*]] = and i64 [[Y:%.*]], [[Y1]]
|
|
; CHECK-NEXT: [[R:%.*]] = icmp eq i64 [[B]], 0
|
|
; CHECK-NEXT: ret i1 [[R]]
|
|
;
|
|
%a = and i64 %y, %x
|
|
%not = xor i64 %a, -1
|
|
%b = and i64 %y, %not
|
|
%r = icmp eq i64 %b, 0
|
|
ret i1 %r
|
|
}
|