Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,62 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
return Res;

// (sub[ nsw][ nuw] (sext (add nsw (X, Y)), sext (X))) --> (sext (Y))
{
Value *Add0;
if (match(Op0, m_SExt(m_Value(Add0))) &&
match(Add0, m_Add(m_Value(X), m_Value(Y))) &&
match(Op1, m_SExt(m_Specific(X)))) {
auto *OBO0 = cast<OverflowingBinaryOperator>(Add0);
if (OBO0->hasNoSignedWrap()) {
// Non-constant Y requires new SExt.
unsigned numOfNewInstrs = !isa<Constant>(Y) ? 1 : 0;
// Check if we can trade some of the old instructions for the new ones.
unsigned numOfDeadInstrs = 0;
numOfDeadInstrs += Op0->hasOneUse() ? 1 : 0;
numOfDeadInstrs += Op1->hasOneUse() ? 1 : 0;
numOfDeadInstrs += Add0->hasOneUse() ? 1 : 0;
if (numOfDeadInstrs >= numOfNewInstrs) {
Value *SExtY = Builder.CreateSExt(Y, I.getType());
return replaceInstUsesWith(I, SExtY);
}
}
}
}

// (sub[ nsw] (sext (add nsw (X, Y)), sext (add nsw (X, Z)))) -->
// --> (sub[ nsw] (sext (Y), sext(Z)))
{
Value *Z, *Add0, *Add1;
if (match(Op0, m_SExt(m_Value(Add0))) &&
match(Add0, m_Add(m_Value(X), m_Value(Y))) &&
match(Op1, m_SExt(m_Value(Add1))) &&
match(Add1, m_Add(m_Specific(X), m_Value(Z)))) {
auto *OBO0 = cast<OverflowingBinaryOperator>(Add0);
auto *OBO1 = cast<OverflowingBinaryOperator>(Add1);
if (OBO0->hasNoSignedWrap() && OBO1->hasNoSignedWrap()) {
unsigned numOfNewInstrs = 0;
// Non-constant Y, Z require new SExt.
numOfNewInstrs += !isa<Constant>(Y) ? 1 : 0;
numOfNewInstrs += !isa<Constant>(Z) ? 1 : 0;
// Check if we can trade some of the old instructions for the new ones.
unsigned numOfDeadInstrs = 0;
numOfDeadInstrs += Op0->hasOneUse() ? 1 : 0;
numOfDeadInstrs += Op1->hasOneUse() ? 1 : 0;
numOfDeadInstrs += Add0->hasOneUse() ? 1 : 0;
numOfDeadInstrs += Add1->hasOneUse() ? 1 : 0;
if (numOfDeadInstrs >= numOfNewInstrs) {
Value *SExtY = Builder.CreateSExt(Y, I.getType());
Value *SExtZ = Builder.CreateSExt(Z, I.getType());
Value *Sub = Builder.CreateSub(SExtY, SExtZ, "",
/* HasNUW */ false,
/* HasNSW */ I.hasNoSignedWrap());
return replaceInstUsesWith(I, Sub);
}
}
}
}

return TryToNarrowDeduceFlags();
}

Expand Down
89 changes: 89 additions & 0 deletions llvm/test/Transforms/InstCombine/sub-sext-add.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i64 @src_2add_2sext_sub(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: define i64 @src_2add_2sext_sub(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
; CHECK-NEXT: [[SEXT1:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: [[SEXT2:%.*]] = sext i32 [[Z]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i64 [[SEXT1]], [[SEXT2]]
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%add2 = add nsw i32 %x, %z
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %add2 to i64
%sub = sub i64 %sext1, %sext2
ret i64 %sub
}

define i64 @src_2add_2sext_sub_nsw(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: define i64 @src_2add_2sext_sub_nsw(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
; CHECK-NEXT: [[SEXT1:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: [[SEXT2:%.*]] = sext i32 [[Z]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i64 [[SEXT1]], [[SEXT2]]
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%add2 = add nsw i32 %x, %z
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %add2 to i64
%sub = sub nsw i64 %sext1, %sext2
ret i64 %sub
}

define i64 @src_2add_2sext_sub_nuw(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: define i64 @src_2add_2sext_sub_nuw(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[Z]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i64 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%add2 = add nsw i32 %x, %z
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %add2 to i64
%sub = sub nuw i64 %sext1, %sext2
ret i64 %sub
}

define i64 @src_x_add_2sext_sub(i32 %x, i32 %y) {
; CHECK-LABEL: define i64 @src_x_add_2sext_sub(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %x to i64
%sub = sub i64 %sext1, %sext2
ret i64 %sub
}

define i64 @src_x_add_2sext_sub_nsw(i32 %x, i32 %y) {
; CHECK-LABEL: define i64 @src_x_add_2sext_sub_nsw(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %x to i64
%sub = sub nsw i64 %sext1, %sext2
ret i64 %sub
}

define i64 @src_x_add_2sext_sub_nuw(i32 %x, i32 %y) {
; CHECK-LABEL: define i64 @src_x_add_2sext_sub_nuw(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sext i32 [[Y]] to i64
; CHECK-NEXT: ret i64 [[SUB]]
;
%add1 = add nsw i32 %x, %y
%sext1 = sext i32 %add1 to i64
%sext2 = sext i32 %x to i64
%sub = sub nuw i64 %sext1, %sext2
ret i64 %sub
}
Loading