[InstCombine] add tests to show pattern matching failures due to commutation

I was looking to fix a bug in getComplexity(), and these cases showed up as 
obvious failures. I'm not sure how to find these in general though.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281055 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjay Patel 2016-09-09 16:35:20 +00:00
parent bb15e0c82d
commit 6128d7f073
3 changed files with 148 additions and 0 deletions

View File

@ -140,6 +140,23 @@ define i32 @test12(i32 %x, i32 %y) {
ret i32 %and
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test12_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: @test12_commuted(
; CHECK-NEXT: [[NEG:%.*]] = xor i32 %x, -1
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[NEG]], %y
; CHECK-NEXT: [[OR:%.*]] = or i32 %y, %x
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[OR]]
; CHECK-NEXT: ret i32 [[AND]]
;
%neg = xor i32 %x, -1
%xor = xor i32 %neg, %y
%or = or i32 %y, %x
%and = and i32 %xor, %or
ret i32 %and
}
; ((x | y) ^ (x ^ y)) -> (x & y)
define i32 @test13(i32 %x, i32 %y) {
; CHECK-LABEL: @test13(
@ -166,6 +183,25 @@ define i32 @test14(i32 %x, i32 %y) {
ret i32 %xor
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test14_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: @test14_commuted(
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 %y, -1
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 %x, -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOTY]], %x
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOTX]], %y
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[OR1]], [[OR2]]
; CHECK-NEXT: ret i32 [[XOR]]
;
%noty = xor i32 %y, -1
%notx = xor i32 %x, -1
%or1 = or i32 %noty, %x
%or2 = or i32 %notx, %y
%xor = xor i32 %or1, %or2
ret i32 %xor
}
; ((x & ~y) ^ (~x & y)) -> x ^ y
define i32 @test15(i32 %x, i32 %y) {
; CHECK-LABEL: @test15(
@ -180,6 +216,25 @@ define i32 @test15(i32 %x, i32 %y) {
ret i32 %xor
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test15_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: @test15_commuted(
; CHECK-NEXT: [[NOTY:%.*]] = xor i32 %y, -1
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 %x, -1
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOTY]], %x
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[NOTX]], %y
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND1]], [[AND2]]
; CHECK-NEXT: ret i32 [[XOR]]
;
%noty = xor i32 %y, -1
%notx = xor i32 %x, -1
%and1 = and i32 %noty, %x
%and2 = and i32 %notx, %y
%xor = xor i32 %and1, %and2
ret i32 %xor
}
define i32 @test16(i32 %a, i32 %b) {
; CHECK-LABEL: @test16(
; CHECK-NEXT: [[TMP1:%.*]] = and i32 %a, 1

View File

@ -555,6 +555,8 @@ define i32 @test41(i32 %a, i32 %b) {
ret i32 %or
}
; (~A ^ B) | (A & B) -> (~A ^ B)
define i32 @test42(i32 %a, i32 %b) {
; CHECK-LABEL: @test42(
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 %a, -1
@ -568,6 +570,42 @@ define i32 @test42(i32 %a, i32 %b) {
ret i32 %or
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test42_commuted_and(i32 %a, i32 %b) {
; CHECK-LABEL: @test42_commuted_and(
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[NEGA]], %b
; CHECK-NEXT: [[AND:%.*]] = and i32 %b, %a
; CHECK-NEXT: [[OR:%.*]] = or i32 [[XOR]], [[AND]]
; CHECK-NEXT: ret i32 [[OR]]
;
%nega = xor i32 %a, -1
%xor = xor i32 %nega, %b
%and = and i32 %b, %a
%or = or i32 %xor, %and
ret i32 %or
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test42_commuted_xor(i32 %a, i32 %b) {
; CHECK-LABEL: @test42_commuted_xor(
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
; CHECK-NEXT: [[XOR:%.*]] = xor i32 %b, [[NEGA]]
; CHECK-NEXT: [[AND:%.*]] = and i32 %a, %b
; CHECK-NEXT: [[OR:%.*]] = or i32 [[XOR]], [[AND]]
; CHECK-NEXT: ret i32 [[OR]]
;
%nega = xor i32 %a, -1
%xor = xor i32 %b, %nega
%and = and i32 %a, %b
%or = or i32 %xor, %and
ret i32 %or
}
; Commute operands of the 'or'.
define i32 @test43(i32 %a, i32 %b) {
; CHECK-LABEL: @test43(
; CHECK-NEXT: [[OR:%.*]] = xor i32 %a, %b
@ -580,6 +618,23 @@ define i32 @test43(i32 %a, i32 %b) {
ret i32 %or
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test43_commuted_and(i32 %a, i32 %b) {
; CHECK-LABEL: @test43_commuted_and(
; CHECK-NEXT: [[NEG:%.*]] = xor i32 %b, -1
; CHECK-NEXT: [[AND:%.*]] = and i32 [[NEG]], %a
; CHECK-NEXT: [[XOR:%.*]] = xor i32 %a, %b
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[XOR]]
; CHECK-NEXT: ret i32 [[OR]]
;
%neg = xor i32 %b, -1
%and = and i32 %neg, %a
%xor = xor i32 %a, %b
%or = or i32 %and, %xor
ret i32 %or
}
define i32 @test44(i32 %a, i32 %b) {
; CHECK-LABEL: @test44(
; CHECK-NEXT: [[OR:%.*]] = xor i32 %a, %b

View File

@ -180,6 +180,27 @@ define i32 @test12(i32 %a, i32 %b) {
ret i32 %xor
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test12commuted(i32 %a, i32 %b) {
; CHECK-LABEL: @test12commuted(
; CHECK-NEXT: [[NEGB:%.*]] = xor i32 %b, -1
; CHECK-NEXT: [[AND:%.*]] = and i32 [[NEGB]], %a
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND]], [[NEGA]]
; CHECK-NEXT: ret i32 [[XOR]]
;
%negb = xor i32 %b, -1
%and = and i32 %negb, %a
%nega = xor i32 %a, -1
%xor = xor i32 %and, %nega
ret i32 %xor
}
; This is a test of canonicalization via operand complexity.
; The final xor has a binary operator and a (fake) unary operator,
; so binary (more complex) should come first.
define i32 @test13(i32 %a, i32 %b) {
; CHECK-LABEL: @test13(
; CHECK-NEXT: [[TMP1:%.*]] = and i32 %a, %b
@ -193,6 +214,23 @@ define i32 @test13(i32 %a, i32 %b) {
ret i32 %xor
}
; FIXME: We miss the fold because the pattern matching is inadequate.
define i32 @test13commuted(i32 %a, i32 %b) {
; CHECK-LABEL: @test13commuted(
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
; CHECK-NEXT: [[NEGB:%.*]] = xor i32 %b, -1
; CHECK-NEXT: [[AND:%.*]] = and i32 [[NEGB]], %a
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND]], [[NEGA]]
; CHECK-NEXT: ret i32 [[XOR]]
;
%nega = xor i32 %a, -1
%negb = xor i32 %b, -1
%and = and i32 %negb, %a
%xor = xor i32 %nega, %and
ret i32 %xor
}
; (A ^ C) ^ (A | B) -> ((~A) & B) ^ C
define i32 @test14(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @test14(