Skip to content

Commit

Permalink
[SCEV] Recognize binary xor as bit-wise add
Browse files Browse the repository at this point in the history
https://alive2.llvm.org/ce/z/ULuZxB

We could transparently handle wider bitwidths,
by effectively casting iN to <N x i1> and performing the `add`
bit/element -wise, the expression will be rather large,
so let's not do that for now.
  • Loading branch information
LebedevRI committed Feb 10, 2022
1 parent 503541f commit 73990ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Expand Up @@ -5067,6 +5067,9 @@ static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
// Instcombine turns add of signmask into xor as a strength reduction step.
if (RHSC->getValue().isSignMask())
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
// Binary `xor` is a bit-wise `add`.
if (V->getType()->isIntegerTy(1))
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
return BinaryOp(Op);

case Instruction::LShr:
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Analysis/ScalarEvolution/logical-operations.ll
Expand Up @@ -86,7 +86,7 @@ define i1 @binary_xor.i1(i1 %x, i1 %y) {
; CHECK-LABEL: 'binary_xor.i1'
; CHECK-NEXT: Classifying expressions for: @binary_xor.i1
; CHECK-NEXT: %r = xor i1 %x, %y
; CHECK-NEXT: --> %r U: full-set S: full-set
; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @binary_xor.i1
;
%r = xor i1 %x, %y
Expand All @@ -108,11 +108,11 @@ define i1 @binary_xor.4ops.i1(i1 %x, i1 %y, i1 %z, i1 %a) {
; CHECK-LABEL: 'binary_xor.4ops.i1'
; CHECK-NEXT: Classifying expressions for: @binary_xor.4ops.i1
; CHECK-NEXT: %t0 = xor i1 %x, %y
; CHECK-NEXT: --> %t0 U: full-set S: full-set
; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
; CHECK-NEXT: %t1 = xor i1 %z, %a
; CHECK-NEXT: --> %t1 U: full-set S: full-set
; CHECK-NEXT: --> (%z + %a) U: full-set S: full-set
; CHECK-NEXT: %r = xor i1 %t0, %t1
; CHECK-NEXT: --> %r U: full-set S: full-set
; CHECK-NEXT: --> (%x + %y + %z + %a) U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @binary_xor.4ops.i1
;
%t0 = xor i1 %x, %y
Expand Down

0 comments on commit 73990ff

Please sign in to comment.