Skip to content

Commit 40a87a9

Browse files
committed
[InstCombine] remove fold that swaps xor/or with constants; NFCI
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) This canonicalization was added at: https://reviews.llvm.org/rL7264 By moving xors out/down, we can more easily combine constants. I'm adding tests that do not change with this patch, so we can verify that those kinds of transforms are still happening. This is no-functional-change-intended because there's a later fold: // (X^C)|Y -> (X|Y)^C iff Y&C == 0 ...and demanded-bits appears to guarantee that any fold that would have hit the fold we're removing here would be caught by that 2nd fold. Similar reasoning was used in: https://reviews.llvm.org/rL299384 The larger motivation for removing this code is that it could interfere with the fix for PR32706: https://bugs.llvm.org/show_bug.cgi?id=32706 Ie, we're not checking if the 'xor' is actually a 'not', so we could reverse a 'not' optimization and cause an infinite loop by altering an 'xor X, -1'. Differential Revision: https://reviews.llvm.org/D33050 llvm-svn: 302733
1 parent 52f8d19 commit 40a87a9

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,18 +1986,6 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
19861986
if (Value *V = SimplifyBSwap(I))
19871987
return replaceInstUsesWith(I, V);
19881988

1989-
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1990-
ConstantInt *C1 = nullptr; Value *X = nullptr;
1991-
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1992-
if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1993-
Op0->hasOneUse()) {
1994-
Value *Or = Builder->CreateOr(X, RHS);
1995-
Or->takeName(Op0);
1996-
return BinaryOperator::CreateXor(Or,
1997-
Builder->getInt(C1->getValue() & ~RHS->getValue()));
1998-
}
1999-
}
2000-
20011989
if (isa<Constant>(Op1))
20021990
if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
20031991
return FoldedLogic;

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,73 @@ define i32 @test16(i32 %a, i32 %b) {
230230
%xor = or i32 %and1, %and2
231231
ret i32 %xor
232232
}
233+
234+
define i8 @not_or(i8 %x) {
235+
; CHECK-LABEL: @not_or(
236+
; CHECK-NEXT: [[NOTX:%.*]] = or i8 %x, 7
237+
; CHECK-NEXT: [[OR:%.*]] = xor i8 [[NOTX]], -8
238+
; CHECK-NEXT: ret i8 [[OR]]
239+
;
240+
%notx = xor i8 %x, -1
241+
%or = or i8 %notx, 7
242+
ret i8 %or
243+
}
244+
245+
define i8 @not_or_xor(i8 %x) {
246+
; CHECK-LABEL: @not_or_xor(
247+
; CHECK-NEXT: [[NOTX:%.*]] = or i8 %x, 7
248+
; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[NOTX]], -12
249+
; CHECK-NEXT: ret i8 [[XOR]]
250+
;
251+
%notx = xor i8 %x, -1
252+
%or = or i8 %notx, 7
253+
%xor = xor i8 %or, 12
254+
ret i8 %xor
255+
}
256+
257+
define i8 @xor_or(i8 %x) {
258+
; CHECK-LABEL: @xor_or(
259+
; CHECK-NEXT: [[XOR:%.*]] = or i8 %x, 7
260+
; CHECK-NEXT: [[OR:%.*]] = xor i8 [[XOR]], 32
261+
; CHECK-NEXT: ret i8 [[OR]]
262+
;
263+
%xor = xor i8 %x, 32
264+
%or = or i8 %xor, 7
265+
ret i8 %or
266+
}
267+
268+
define i8 @xor_or2(i8 %x) {
269+
; CHECK-LABEL: @xor_or2(
270+
; CHECK-NEXT: [[XOR:%.*]] = or i8 %x, 7
271+
; CHECK-NEXT: [[OR:%.*]] = xor i8 [[XOR]], 32
272+
; CHECK-NEXT: ret i8 [[OR]]
273+
;
274+
%xor = xor i8 %x, 33
275+
%or = or i8 %xor, 7
276+
ret i8 %or
277+
}
278+
279+
define i8 @xor_or_xor(i8 %x) {
280+
; CHECK-LABEL: @xor_or_xor(
281+
; CHECK-NEXT: [[XOR1:%.*]] = or i8 %x, 7
282+
; CHECK-NEXT: [[XOR2:%.*]] = xor i8 [[XOR1]], 44
283+
; CHECK-NEXT: ret i8 [[XOR2]]
284+
;
285+
%xor1 = xor i8 %x, 33
286+
%or = or i8 %xor1, 7
287+
%xor2 = xor i8 %or, 12
288+
ret i8 %xor2
289+
}
290+
291+
define i8 @or_xor_or(i8 %x) {
292+
; CHECK-LABEL: @or_xor_or(
293+
; CHECK-NEXT: [[XOR:%.*]] = or i8 %x, 39
294+
; CHECK-NEXT: [[OR2:%.*]] = xor i8 [[XOR]], 8
295+
; CHECK-NEXT: ret i8 [[OR2]]
296+
;
297+
%or1 = or i8 %x, 33
298+
%xor = xor i8 %or1, 12
299+
%or2 = or i8 %xor, 7
300+
ret i8 %or2
301+
}
302+

0 commit comments

Comments
 (0)