Skip to content

Commit c37fae1

Browse files
committed
[InstCombine] Add pre-commit tests for PR76554. NFC.
1 parent e5054fb commit c37fae1

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

llvm/test/Transforms/InstCombine/and-xor-or.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
declare void @use(i32)
55
declare void @use_i8(i8)
6+
declare void @use_i1(i1)
67

78
; a & (a ^ b) --> a & ~b
89

@@ -4780,3 +4781,32 @@ define i32 @canonicalize_logic_first_constexpr_nuw(i32 %x) {
47804781
%r = and i32 %a, -10
47814782
ret i32 %r
47824783
}
4784+
4785+
define i1 @test_and_xor_freely_invertable(i32 %x, i32 %y, i1 %z) {
4786+
; CHECK-LABEL: define {{[^@]+}}@test_and_xor_freely_invertable
4787+
; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[Z:%.*]]) {
4788+
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[X]], [[Y]]
4789+
; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP]], [[Z]]
4790+
; CHECK-NEXT: ret i1 [[AND]]
4791+
;
4792+
%cmp = icmp sgt i32 %x, %y
4793+
%xor = xor i1 %cmp, %z
4794+
%and = and i1 %xor, %z
4795+
ret i1 %and
4796+
}
4797+
4798+
define i1 @test_and_xor_freely_invertable_multiuse(i32 %x, i32 %y, i1 %z) {
4799+
; CHECK-LABEL: define {{[^@]+}}@test_and_xor_freely_invertable_multiuse
4800+
; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[Z:%.*]]) {
4801+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], [[Y]]
4802+
; CHECK-NEXT: call void @use_i1(i1 [[CMP]])
4803+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[CMP]], true
4804+
; CHECK-NEXT: [[AND:%.*]] = and i1 [[TMP1]], [[Z]]
4805+
; CHECK-NEXT: ret i1 [[AND]]
4806+
;
4807+
%cmp = icmp sgt i32 %x, %y
4808+
call void @use_i1(i1 %cmp)
4809+
%xor = xor i1 %cmp, %z
4810+
%and = and i1 %xor, %z
4811+
ret i1 %and
4812+
}

llvm/test/Transforms/InstCombine/or.ll

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,3 +1777,120 @@ if.then:
17771777
if.else:
17781778
ret i32 0
17791779
}
1780+
1781+
; Tests from PR76554
1782+
define i32 @test_or_and_xor_constant(i32 %x, i32 %y) {
1783+
; CHECK-LABEL: @test_or_and_xor_constant(
1784+
; CHECK-NEXT: [[A:%.*]] = and i32 [[X:%.*]], -2147483648
1785+
; CHECK-NEXT: [[B:%.*]] = xor i32 [[A]], -2147483648
1786+
; CHECK-NEXT: [[C:%.*]] = and i32 [[B]], [[Y:%.*]]
1787+
; CHECK-NEXT: [[D:%.*]] = or i32 [[C]], [[A]]
1788+
; CHECK-NEXT: ret i32 [[D]]
1789+
;
1790+
%a = and i32 %x, -2147483648
1791+
%b = xor i32 %a, -2147483648
1792+
%c = and i32 %b, %y
1793+
%d = or i32 %c, %a
1794+
ret i32 %d
1795+
}
1796+
1797+
define i32 @test_or_and_xor(i32 %a, i32 %b, i32 %c) {
1798+
; CHECK-LABEL: @test_or_and_xor(
1799+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
1800+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1801+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[A]]
1802+
; CHECK-NEXT: ret i32 [[OR]]
1803+
;
1804+
%xor = xor i32 %a, %b
1805+
%and = and i32 %xor, %c
1806+
%or = or i32 %and, %a
1807+
ret i32 %or
1808+
}
1809+
1810+
define i32 @test_or_and_xor_commuted1(i32 %a, i32 %b, i32 %c) {
1811+
; CHECK-LABEL: @test_or_and_xor_commuted1(
1812+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[B:%.*]], [[A:%.*]]
1813+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1814+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[A]]
1815+
; CHECK-NEXT: ret i32 [[OR]]
1816+
;
1817+
%xor = xor i32 %b, %a
1818+
%and = and i32 %xor, %c
1819+
%or = or i32 %and, %a
1820+
ret i32 %or
1821+
}
1822+
1823+
define i32 @test_or_and_xor_commuted2(i32 %a, i32 %b, i32 %c) {
1824+
; CHECK-LABEL: @test_or_and_xor_commuted2(
1825+
; CHECK-NEXT: [[CC:%.*]] = mul i32 [[C:%.*]], [[C]]
1826+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
1827+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[CC]], [[XOR]]
1828+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[A]]
1829+
; CHECK-NEXT: ret i32 [[OR]]
1830+
;
1831+
%cc = mul i32 %c, %c
1832+
%xor = xor i32 %a, %b
1833+
%and = and i32 %cc, %xor
1834+
%or = or i32 %and, %a
1835+
ret i32 %or
1836+
}
1837+
1838+
define i32 @test_or_and_xor_commuted3(i32 %a, i32 %b, i32 %c) {
1839+
; CHECK-LABEL: @test_or_and_xor_commuted3(
1840+
; CHECK-NEXT: [[AA:%.*]] = mul i32 [[A:%.*]], [[A]]
1841+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AA]], [[B:%.*]]
1842+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1843+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AA]], [[AND]]
1844+
; CHECK-NEXT: ret i32 [[OR]]
1845+
;
1846+
%aa = mul i32 %a, %a
1847+
%xor = xor i32 %aa, %b
1848+
%and = and i32 %xor, %c
1849+
%or = or i32 %aa, %and
1850+
ret i32 %or
1851+
}
1852+
1853+
define i32 @test_or_and_xor_multiuse1(i32 %a, i32 %b, i32 %c) {
1854+
; CHECK-LABEL: @test_or_and_xor_multiuse1(
1855+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
1856+
; CHECK-NEXT: call void @use(i32 [[XOR]])
1857+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1858+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[A]]
1859+
; CHECK-NEXT: ret i32 [[OR]]
1860+
;
1861+
%xor = xor i32 %a, %b
1862+
call void @use(i32 %xor)
1863+
%and = and i32 %xor, %c
1864+
%or = or i32 %and, %a
1865+
ret i32 %or
1866+
}
1867+
1868+
; Negative tests
1869+
1870+
define i32 @test_or_and_xor_mismatched_op(i32 %a, i32 %b, i32 %c, i32 %d) {
1871+
; CHECK-LABEL: @test_or_and_xor_mismatched_op(
1872+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
1873+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1874+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[D:%.*]]
1875+
; CHECK-NEXT: ret i32 [[OR]]
1876+
;
1877+
%xor = xor i32 %a, %b
1878+
%and = and i32 %xor, %c
1879+
%or = or i32 %and, %d
1880+
ret i32 %or
1881+
}
1882+
1883+
define i32 @test_or_and_xor_multiuse2(i32 %a, i32 %b, i32 %c) {
1884+
; CHECK-LABEL: @test_or_and_xor_multiuse2(
1885+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
1886+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[C:%.*]]
1887+
; CHECK-NEXT: call void @use(i32 [[AND]])
1888+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[AND]], [[A]]
1889+
; CHECK-NEXT: ret i32 [[OR]]
1890+
;
1891+
%xor = xor i32 %a, %b
1892+
%and = and i32 %xor, %c
1893+
call void @use(i32 %and)
1894+
%or = or i32 %and, %a
1895+
ret i32 %or
1896+
}

0 commit comments

Comments
 (0)