Skip to content

Commit

Permalink
[ValueTracking] peek through extends in haveNoCommonBitsSet
Browse files Browse the repository at this point in the history
In cases with matching extends, this allows changing an 'add'
into an 'or' and narrowing the 'or' which then simplifies to
a constant.

In cases with opposite extends, we just convert to an 'or'
currently, but that could be reduced too.

https://alive2.llvm.org/ce/z/fTHzdb
  • Loading branch information
rotateright committed Dec 13, 2022
1 parent c8cba0b commit 7520d18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
9 changes: 6 additions & 3 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -270,10 +270,13 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y))))
return true;

// Peek through extends:
// Peek through extends to find a 'not' of the other side:
// (ext Y) op ext(~Y)
if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))))
// (ext ~Y) op ext(Y)
if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) ||
(match(RHS, m_ZExtOrSExt(m_Value(Y))) &&
match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))))
return true;

// Look for: (A & B) op ~(A | B)
Expand Down
31 changes: 8 additions & 23 deletions llvm/test/Transforms/InstCombine/add.ll
Expand Up @@ -2348,11 +2348,7 @@ define <vscale x 1 x i32> @add_to_or_scalable(<vscale x 1 x i32> %in) {

define i5 @zext_zext_not(i3 %x) {
; CHECK-LABEL: @zext_zext_not(
; CHECK-NEXT: [[ZX:%.*]] = zext i3 [[X:%.*]] to i5
; CHECK-NEXT: [[NOTX:%.*]] = xor i3 [[X]], -1
; CHECK-NEXT: [[ZNOTX:%.*]] = zext i3 [[NOTX]] to i5
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i5 [[ZX]], [[ZNOTX]]
; CHECK-NEXT: ret i5 [[R]]
; CHECK-NEXT: ret i5 7
;
%zx = zext i3 %x to i5
%notx = xor i3 %x, -1
Expand All @@ -2363,11 +2359,7 @@ define i5 @zext_zext_not(i3 %x) {

define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {
; CHECK-LABEL: @zext_zext_not_commute(
; CHECK-NEXT: [[ZX:%.*]] = zext <2 x i3> [[X:%.*]] to <2 x i5>
; CHECK-NEXT: [[NOTX:%.*]] = xor <2 x i3> [[X]], <i3 -1, i3 poison>
; CHECK-NEXT: [[ZNOTX:%.*]] = zext <2 x i3> [[NOTX]] to <2 x i5>
; CHECK-NEXT: [[R:%.*]] = add nuw nsw <2 x i5> [[ZNOTX]], [[ZX]]
; CHECK-NEXT: ret <2 x i5> [[R]]
; CHECK-NEXT: ret <2 x i5> <i5 7, i5 7>
;
%zx = zext <2 x i3> %x to <2 x i5>
%notx = xor <2 x i3> %x, <i3 -1, i3 poison>
Expand All @@ -2378,11 +2370,7 @@ define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {

define i9 @sext_sext_not(i3 %x) {
; CHECK-LABEL: @sext_sext_not(
; CHECK-NEXT: [[SX:%.*]] = sext i3 [[X:%.*]] to i9
; CHECK-NEXT: [[NOTX:%.*]] = xor i3 [[X]], -1
; CHECK-NEXT: [[SNOTX:%.*]] = sext i3 [[NOTX]] to i9
; CHECK-NEXT: [[R:%.*]] = add nsw i9 [[SX]], [[SNOTX]]
; CHECK-NEXT: ret i9 [[R]]
; CHECK-NEXT: ret i9 -1
;
%sx = sext i3 %x to i9
%notx = xor i3 %x, -1
Expand All @@ -2395,10 +2383,7 @@ define i8 @sext_sext_not_commute(i3 %x) {
; CHECK-LABEL: @sext_sext_not_commute(
; CHECK-NEXT: [[SX:%.*]] = sext i3 [[X:%.*]] to i8
; CHECK-NEXT: call void @use(i8 [[SX]])
; CHECK-NEXT: [[NOTX:%.*]] = xor i3 [[X]], -1
; CHECK-NEXT: [[SNOTX:%.*]] = sext i3 [[NOTX]] to i8
; CHECK-NEXT: [[R:%.*]] = add nsw i8 [[SNOTX]], [[SX]]
; CHECK-NEXT: ret i8 [[R]]
; CHECK-NEXT: ret i8 -1
;
%sx = sext i3 %x to i8
call void @use(i8 %sx)
Expand All @@ -2413,7 +2398,7 @@ define i5 @zext_sext_not(i4 %x) {
; CHECK-NEXT: [[ZX:%.*]] = zext i4 [[X:%.*]] to i5
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
; CHECK-NEXT: [[SNOTX:%.*]] = sext i4 [[NOTX]] to i5
; CHECK-NEXT: [[R:%.*]] = add i5 [[ZX]], [[SNOTX]]
; CHECK-NEXT: [[R:%.*]] = or i5 [[ZX]], [[SNOTX]]
; CHECK-NEXT: ret i5 [[R]]
;
%zx = zext i4 %x to i5
Expand All @@ -2430,7 +2415,7 @@ define i8 @zext_sext_not_commute(i4 %x) {
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
; CHECK-NEXT: [[SNOTX:%.*]] = sext i4 [[NOTX]] to i8
; CHECK-NEXT: call void @use(i8 [[SNOTX]])
; CHECK-NEXT: [[R:%.*]] = add nsw i8 [[SNOTX]], [[ZX]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[SNOTX]], [[ZX]]
; CHECK-NEXT: ret i8 [[R]]
;
%zx = zext i4 %x to i8
Expand All @@ -2447,7 +2432,7 @@ define i9 @sext_zext_not(i4 %x) {
; CHECK-NEXT: [[SX:%.*]] = sext i4 [[X:%.*]] to i9
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
; CHECK-NEXT: [[ZNOTX:%.*]] = zext i4 [[NOTX]] to i9
; CHECK-NEXT: [[R:%.*]] = add nsw i9 [[SX]], [[ZNOTX]]
; CHECK-NEXT: [[R:%.*]] = or i9 [[SX]], [[ZNOTX]]
; CHECK-NEXT: ret i9 [[R]]
;
%sx = sext i4 %x to i9
Expand All @@ -2462,7 +2447,7 @@ define i9 @sext_zext_not_commute(i4 %x) {
; CHECK-NEXT: [[SX:%.*]] = sext i4 [[X:%.*]] to i9
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
; CHECK-NEXT: [[ZNOTX:%.*]] = zext i4 [[NOTX]] to i9
; CHECK-NEXT: [[R:%.*]] = add nsw i9 [[ZNOTX]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = or i9 [[ZNOTX]], [[SX]]
; CHECK-NEXT: ret i9 [[R]]
;
%sx = sext i4 %x to i9
Expand Down

0 comments on commit 7520d18

Please sign in to comment.