1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
;RUN: opt -S -reassociate < %s | FileCheck %s
; ==========================================================================
;
; Xor reassociation general cases
;
; ==========================================================================
; (x | c1) ^ (x | c2) => (x & c3) ^ c3, where c3 = c1^c2
;
define i32 @xor1(i32 %x) {
%or = or i32 %x, 123
%or1 = or i32 %x, 456
%xor = xor i32 %or, %or1
ret i32 %xor
;CHECK-LABEL: @xor1(
;CHECK: %and.ra = and i32 %x, 435
;CHECK: %xor = xor i32 %and.ra, 435
}
; (x | c1) ^ (x | c2) => (x & c3) ^ c3, where c3 = c1^c2
;
define <2 x i32> @xor1_vec(<2 x i32> %x) {
%or = or <2 x i32> %x, <i32 123, i32 123>
%or1 = or <2 x i32> %x, <i32 456, i32 456>
%xor = xor <2 x i32> %or, %or1
ret <2 x i32> %xor
;CHECK-LABEL: @xor1_vec(
;CHECK: %and.ra = and <2 x i32> %x, <i32 435, i32 435>
;CHECK: %xor = xor <2 x i32> %and.ra, <i32 435, i32 435>
}
; Test rule : (x & c1) ^ (x & c2) = (x & (c1^c2))
; Real testing case : (x & 123) ^ y ^ (x & 345) => (x & 435) ^ y
define i32 @xor2(i32 %x, i32 %y) {
%and = and i32 %x, 123
%xor = xor i32 %and, %y
%and1 = and i32 %x, 456
%xor2 = xor i32 %xor, %and1
ret i32 %xor2
;CHECK-LABEL: @xor2(
;CHECK: %and.ra = and i32 %x, 435
;CHECK: %xor2 = xor i32 %and.ra, %y
}
; Test rule : (x & c1) ^ (x & c2) = (x & (c1^c2))
; Real testing case : (x & 123) ^ y ^ (x & 345) => (x & 435) ^ y
define <2 x i32> @xor2_vec(<2 x i32> %x, <2 x i32> %y) {
%and = and <2 x i32> %x, <i32 123, i32 123>
%xor = xor <2 x i32> %and, %y
%and1 = and <2 x i32> %x, <i32 456, i32 456>
%xor2 = xor <2 x i32> %xor, %and1
ret <2 x i32> %xor2
;CHECK-LABEL: @xor2_vec(
;CHECK: %and.ra = and <2 x i32> %x, <i32 435, i32 435>
;CHECK: %xor2 = xor <2 x i32> %and.ra, %y
}
; Test rule: (x | c1) ^ (x & c2) = (x & c3) ^ c1, where c3 = ~c1 ^ c2
; c3 = ~c1 ^ c2
define i32 @xor3(i32 %x, i32 %y) {
%or = or i32 %x, 123
%xor = xor i32 %or, %y
%and = and i32 %x, 456
%xor1 = xor i32 %xor, %and
ret i32 %xor1
;CHECK-LABEL: @xor3(
;CHECK: %and.ra = and i32 %x, -436
;CHECK: %xor = xor i32 %y, 123
;CHECK: %xor1 = xor i32 %xor, %and.ra
}
; Test rule: (x | c1) ^ (x & c2) = (x & c3) ^ c1, where c3 = ~c1 ^ c2
; c3 = ~c1 ^ c2
define <2 x i32> @xor3_vec(<2 x i32> %x, <2 x i32> %y) {
%or = or <2 x i32> %x, <i32 123, i32 123>
%xor = xor <2 x i32> %or, %y
%and = and <2 x i32> %x, <i32 456, i32 456>
%xor1 = xor <2 x i32> %xor, %and
ret <2 x i32> %xor1
;CHECK-LABEL: @xor3_vec(
;CHECK: %and.ra = and <2 x i32> %x, <i32 -436, i32 -436>
;CHECK: %xor = xor <2 x i32> %y, <i32 123, i32 123>
;CHECK: %xor1 = xor <2 x i32> %xor, %and.ra
}
; Test rule: (x | c1) ^ c2 = (x & ~c1) ^ (c1 ^ c2)
define i32 @xor4(i32 %x, i32 %y) {
%and = and i32 %x, -124
%xor = xor i32 %y, 435
%xor1 = xor i32 %xor, %and
ret i32 %xor1
; CHECK-LABEL: @xor4(
; CHECK: %and = and i32 %x, -124
; CHECK: %xor = xor i32 %y, 435
; CHECK: %xor1 = xor i32 %xor, %and
}
; Test rule: (x | c1) ^ c2 = (x & ~c1) ^ (c1 ^ c2)
define <2 x i32> @xor4_vec(<2 x i32> %x, <2 x i32> %y) {
%and = and <2 x i32> %x, <i32 -124, i32 -124>
%xor = xor <2 x i32> %y, <i32 435, i32 435>
%xor1 = xor <2 x i32> %xor, %and
ret <2 x i32> %xor1
; CHECK-LABEL: @xor4_vec(
; CHECK: %and = and <2 x i32> %x, <i32 -124, i32 -124>
; CHECK: %xor = xor <2 x i32> %y, <i32 435, i32 435>
; CHECK: %xor1 = xor <2 x i32> %xor, %and
}
; ==========================================================================
;
; Xor reassociation special cases
;
; ==========================================================================
; Special case1:
; (x | c1) ^ (x & ~c1) = c1
define i32 @xor_special1(i32 %x, i32 %y) {
%or = or i32 %x, 123
%xor = xor i32 %or, %y
%and = and i32 %x, -124
%xor1 = xor i32 %xor, %and
ret i32 %xor1
; CHECK-LABEL: @xor_special1(
; CHECK: %xor1 = xor i32 %y, 123
; CHECK: ret i32 %xor1
}
; Special case1:
; (x | c1) ^ (x & ~c1) = c1
define <2 x i32> @xor_special1_vec(<2 x i32> %x, <2 x i32> %y) {
%or = or <2 x i32> %x, <i32 123, i32 123>
%xor = xor <2 x i32> %or, %y
%and = and <2 x i32> %x, <i32 -124, i32 -124>
%xor1 = xor <2 x i32> %xor, %and
ret <2 x i32> %xor1
; CHECK-LABEL: @xor_special1_vec(
; CHECK: %xor1 = xor <2 x i32> %y, <i32 123, i32 123>
; CHECK: ret <2 x i32> %xor1
}
; Special case1:
; (x | c1) ^ (x & c1) = x ^ c1
define i32 @xor_special2(i32 %x, i32 %y) {
%or = or i32 %x, 123
%xor = xor i32 %or, %y
%and = and i32 %x, 123
%xor1 = xor i32 %xor, %and
ret i32 %xor1
; CHECK-LABEL: @xor_special2(
; CHECK: %xor = xor i32 %x, 123
; CHECK: %xor1 = xor i32 %xor, %y
; CHECK: ret i32 %xor1
}
; Special case1:
; (x | c1) ^ (x & c1) = x ^ c1
define <2 x i32> @xor_special2_vec(<2 x i32> %x, <2 x i32> %y) {
%or = or <2 x i32> %x, <i32 123, i32 123>
%xor = xor <2 x i32> %or, %y
%and = and <2 x i32> %x, <i32 123, i32 123>
%xor1 = xor <2 x i32> %xor, %and
ret <2 x i32> %xor1
; CHECK-LABEL: @xor_special2_vec(
; CHECK: %xor = xor <2 x i32> %x, <i32 123, i32 123>
; CHECK: %xor1 = xor <2 x i32> %xor, %y
; CHECK: ret <2 x i32> %xor1
}
; (x | c1) ^ (x | c1) => 0
define i32 @xor_special3(i32 %x) {
%or = or i32 %x, 123
%or1 = or i32 %x, 123
%xor = xor i32 %or, %or1
ret i32 %xor
;CHECK-LABEL: @xor_special3(
;CHECK: ret i32 0
}
; (x | c1) ^ (x | c1) => 0
define <2 x i32> @xor_special3_vec(<2 x i32> %x) {
%or = or <2 x i32> %x, <i32 123, i32 123>
%or1 = or <2 x i32> %x, <i32 123, i32 123>
%xor = xor <2 x i32> %or, %or1
ret <2 x i32> %xor
;CHECK-LABEL: @xor_special3_vec(
;CHECK: ret <2 x i32> zeroinitializer
}
; (x & c1) ^ (x & c1) => 0
define i32 @xor_special4(i32 %x) {
%or = and i32 %x, 123
%or1 = and i32 123, %x
%xor = xor i32 %or, %or1
ret i32 %xor
;CHECK-LABEL: @xor_special4(
;CHECK: ret i32 0
}
; (x & c1) ^ (x & c1) => 0
define <2 x i32> @xor_special4_vec(<2 x i32> %x) {
%or = and <2 x i32> %x, <i32 123, i32 123>
%or1 = and <2 x i32> <i32 123, i32 123>, %x
%xor = xor <2 x i32> %or, %or1
ret <2 x i32> %xor
;CHECK-LABEL: @xor_special4_vec(
;CHECK: ret <2 x i32> zeroinitializer
}
; ==========================================================================
;
; Xor reassociation curtail code size
;
; ==========================================================================
; (x | c1) ^ (x | c2) => (x & c3) ^ c3
; is enabled if one of operands has multiple uses
;
define i32 @xor_ra_size1(i32 %x) {
%or = or i32 %x, 123
%or1 = or i32 %x, 456
%xor = xor i32 %or, %or1
%add = add i32 %xor, %or
ret i32 %add
;CHECK-LABEL: @xor_ra_size1(
;CHECK: %xor = xor i32 %and.ra, 435
}
; (x | c1) ^ (x | c2) => (x & c3) ^ c3
; is disenabled if bothf operands has multiple uses.
;
define i32 @xor_ra_size2(i32 %x) {
%or = or i32 %x, 123
%or1 = or i32 %x, 456
%xor = xor i32 %or, %or1
%add = add i32 %xor, %or
%add2 = add i32 %add, %or1
ret i32 %add2
;CHECK-LABEL: @xor_ra_size2(
;CHECK: %or1 = or i32 %x, 456
;CHECK: %xor = xor i32 %or, %or1
}
; ==========================================================================
;
; Xor reassociation bugs
;
; ==========================================================================
@xor_bug1_data = external global <{}>, align 4
define void @xor_bug1() {
%1 = ptrtoint i32* undef to i64
%2 = xor i64 %1, ptrtoint (<{}>* @xor_bug1_data to i64)
%3 = and i64 undef, %2
ret void
}
; The bug was that when the compiler optimize "(x | c1)" ^ "(x & c2)", it may
; swap the two xor-subexpressions if they are not in canoninical order; however,
; when optimizer swaps two sub-expressions, if forgot to swap the cached value
; of c1 and c2 accordingly, hence cause the problem.
;
define i32 @xor_bug2(i32, i32, i32, i32) {
%5 = mul i32 %0, 123
%6 = add i32 %2, 24
%7 = add i32 %1, 8
%8 = and i32 %1, 3456789
%9 = or i32 %8, 4567890
%10 = and i32 %1, 543210987
%11 = or i32 %1, 891034567
%12 = and i32 %2, 255
%13 = xor i32 %9, %10
%14 = xor i32 %11, %13
%15 = xor i32 %5, %14
%16 = and i32 %3, 255
%17 = xor i32 %16, 42
%18 = add i32 %6, %7
%19 = add i32 %18, %12
%20 = add i32 %19, %15
ret i32 %20
;CHECK-LABEL: @xor_bug2(
;CHECK: xor i32 %5, 891034567
}
|