1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; Tests for Integer BitWidth <= 64 && BitWidth % 8 != 0.
6 ;; Flip sign bit then add INT_MIN -> nop.
7 define i1 @test1(i1 %x) {
9 ; CHECK-NEXT: ret i1 %x
12 %tmp.4 = add i1 %tmp.2, 1
16 ;; Flip sign bit then add INT_MIN -> nop.
17 define i47 @test2(i47 %x) {
18 ; CHECK-LABEL: @test2(
19 ; CHECK-NEXT: ret i47 %x
21 %tmp.2 = xor i47 %x, 70368744177664
22 %tmp.4 = add i47 %tmp.2, 70368744177664
26 ;; Flip sign bit then add INT_MIN -> nop.
27 define i15 @test3(i15 %x) {
28 ; CHECK-LABEL: @test3(
29 ; CHECK-NEXT: ret i15 %x
31 %tmp.2 = xor i15 %x, 16384
32 %tmp.4 = add i15 %tmp.2, 16384
36 ; X + signbit --> X ^ signbit
37 define <2 x i5> @test3vec(<2 x i5> %x) {
38 ; CHECK-LABEL: @test3vec(
39 ; CHECK-NEXT: [[Y:%.*]] = xor <2 x i5> %x, <i5 -16, i5 -16>
40 ; CHECK-NEXT: ret <2 x i5> [[Y]]
42 %y = add <2 x i5> %x, <i5 16, i5 16>
46 ;; (x & 0b1111..0) + 1 -> x | 1
47 define i49 @test4(i49 %x) {
48 ; CHECK-LABEL: @test4(
49 ; CHECK-NEXT: [[TMP_4:%.*]] = or i49 %x, 1
50 ; CHECK-NEXT: ret i49 [[TMP_4]]
52 %tmp.2 = and i49 %x, 562949953421310
53 %tmp.4 = add i49 %tmp.2, 1
57 define i7 @sext(i4 %x) {
59 ; CHECK-NEXT: [[ADD:%.*]] = sext i4 %x to i7
60 ; CHECK-NEXT: ret i7 [[ADD]]
63 %zext = zext i4 %xor to i7
64 %add = add nsw i7 %zext, -8
68 define <2 x i10> @sext_vec(<2 x i3> %x) {
69 ; CHECK-LABEL: @sext_vec(
70 ; CHECK-NEXT: [[ADD:%.*]] = sext <2 x i3> %x to <2 x i10>
71 ; CHECK-NEXT: ret <2 x i10> [[ADD]]
73 %xor = xor <2 x i3> %x, <i3 -4, i3 -4>
74 %zext = zext <2 x i3> %xor to <2 x i10>
75 %add = add nsw <2 x i10> %zext, <i10 -4, i10 -4>
79 ; Multiple uses of the operands don't prevent the fold.
81 define i4 @sext_multiuse(i4 %x) {
82 ; CHECK-LABEL: @sext_multiuse(
83 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 %x, -8
84 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i4 [[XOR]] to i7
85 ; CHECK-NEXT: [[ADD:%.*]] = sext i4 %x to i7
86 ; CHECK-NEXT: [[MUL:%.*]] = sdiv i7 [[ZEXT]], [[ADD]]
87 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i7 [[MUL]] to i4
88 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[TRUNC]], [[XOR]]
89 ; CHECK-NEXT: ret i4 [[DIV]]
92 %zext = zext i4 %xor to i7
93 %add = add nsw i7 %zext, -8
94 %mul = sdiv i7 %zext, %add
95 %trunc = trunc i7 %mul to i4
96 %div = sdiv i4 %trunc, %xor
100 ; Tests for Integer BitWidth > 64 && BitWidth <= 1024.
102 ;; Flip sign bit then add INT_MIN -> nop.
103 define i111 @test5(i111 %x) {
104 ; CHECK-LABEL: @test5(
105 ; CHECK-NEXT: ret i111 %x
107 %tmp.2 = shl i111 1, 110
108 %tmp.4 = xor i111 %x, %tmp.2
109 %tmp.6 = add i111 %tmp.4, %tmp.2
113 ;; Flip sign bit then add INT_MIN -> nop.
114 define i65 @test6(i65 %x) {
115 ; CHECK-LABEL: @test6(
116 ; CHECK-NEXT: ret i65 %x
118 %tmp.0 = shl i65 1, 64
119 %tmp.2 = xor i65 %x, %tmp.0
120 %tmp.4 = add i65 %tmp.2, %tmp.0
124 ;; Flip sign bit then add INT_MIN -> nop.
125 define i1024 @test7(i1024 %x) {
126 ; CHECK-LABEL: @test7(
127 ; CHECK-NEXT: ret i1024 %x
129 %tmp.0 = shl i1024 1, 1023
130 %tmp.2 = xor i1024 %x, %tmp.0
131 %tmp.4 = add i1024 %tmp.2, %tmp.0
135 ;; If we have add(xor(X, 0xF..F80..), 0x80..), it's an xor.
136 define i128 @test8(i128 %x) {
137 ; CHECK-LABEL: @test8(
138 ; CHECK-NEXT: [[TMP_4:%.*]] = xor i128 %x, 170141183460469231731687303715884105600
139 ; CHECK-NEXT: ret i128 [[TMP_4]]
141 %tmp.5 = shl i128 1, 127
142 %tmp.1 = ashr i128 %tmp.5, 120
143 %tmp.2 = xor i128 %x, %tmp.1
144 %tmp.4 = add i128 %tmp.2, %tmp.5
148 ;; (x & 254)+1 -> (x & 254)|1
149 define i77 @test9(i77 %x) {
150 ; CHECK-LABEL: @test9(
151 ; CHECK-NEXT: [[TMP_2:%.*]] = and i77 %x, 562949953421310
152 ; CHECK-NEXT: [[TMP_4:%.*]] = or i77 [[TMP_2]], 1
153 ; CHECK-NEXT: ret i77 [[TMP_4]]
155 %tmp.2 = and i77 %x, 562949953421310
156 %tmp.4 = add i77 %tmp.2, 1