Skip to content

Commit 23de287

Browse files
committed
[InstCombine] Fold (sub nuw X, (Y << nuw Z)) >>u exact Z --> (X >>u exact Z) sub nuw Y
This is the same fold as ((X << nuw Z) sub nuw Y) >>u exact Z --> X sub nuw (Y >>u exact Z), but with the sub operands swapped. Alive2 Proof: https://alive2.llvm.org/ce/z/2cRcdx
1 parent 75dd3f0 commit 23de287

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,17 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
12751275
return NewSub;
12761276
}
12771277

1278+
// (sub nuw X, (Y << nuw Z)) >>u exact Z --> (X >>u exact Z) sub nuw Y
1279+
if (I.isExact() &&
1280+
match(Op0, m_OneUse(m_NUWSub(m_Value(X),
1281+
m_NUWShl(m_Value(Y), m_Specific(Op1)))))) {
1282+
Value *NewLshr = Builder.CreateLShr(X, Op1, "", /*isExact=*/true);
1283+
auto *NewSub = BinaryOperator::CreateNUWSub(NewLshr, Y);
1284+
NewSub->setHasNoSignedWrap(
1285+
cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1286+
return NewSub;
1287+
}
1288+
12781289
auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
12791290
switch (BinOpcode) {
12801291
default:

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,8 @@ define i32 @shl_sub_lshr(i32 %x, i32 %c, i32 %y) {
466466

467467
define i32 @shl_sub_lshr_reverse(i32 %x, i32 %c, i32 %y) {
468468
; CHECK-LABEL: @shl_sub_lshr_reverse(
469-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
470-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw i32 [[Y:%.*]], [[SHL]]
471-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
469+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[Y:%.*]], [[C:%.*]]
470+
; CHECK-NEXT: [[LSHR:%.*]] = sub nuw nsw i32 [[TMP1]], [[X:%.*]]
472471
; CHECK-NEXT: ret i32 [[LSHR]]
473472
;
474473
%shl = shl nuw i32 %x, %c
@@ -479,9 +478,8 @@ define i32 @shl_sub_lshr_reverse(i32 %x, i32 %c, i32 %y) {
479478

480479
define i32 @shl_sub_lshr_reverse_no_nsw(i32 %x, i32 %c, i32 %y) {
481480
; CHECK-LABEL: @shl_sub_lshr_reverse_no_nsw(
482-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
483-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
484-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
481+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[Y:%.*]], [[C:%.*]]
482+
; CHECK-NEXT: [[LSHR:%.*]] = sub nuw i32 [[TMP1]], [[X:%.*]]
485483
; CHECK-NEXT: ret i32 [[LSHR]]
486484
;
487485
%shl = shl nuw i32 %x, %c
@@ -492,9 +490,8 @@ define i32 @shl_sub_lshr_reverse_no_nsw(i32 %x, i32 %c, i32 %y) {
492490

493491
define i32 @shl_sub_lshr_reverse_nsw_on_op1(i32 %x, i32 %c, i32 %y) {
494492
; CHECK-LABEL: @shl_sub_lshr_reverse_nsw_on_op1(
495-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 [[X:%.*]], [[C:%.*]]
496-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
497-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
493+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[Y:%.*]], [[C:%.*]]
494+
; CHECK-NEXT: [[LSHR:%.*]] = sub nuw i32 [[TMP1]], [[X:%.*]]
498495
; CHECK-NEXT: ret i32 [[LSHR]]
499496
;
500497
%shl = shl nuw nsw i32 %x, %c
@@ -539,8 +536,8 @@ define i32 @shl_sub_lshr_reverse_multiuse2(i32 %x, i32 %c, i32 %y) {
539536
; CHECK-LABEL: @shl_sub_lshr_reverse_multiuse2(
540537
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
541538
; CHECK-NEXT: call void @use(i32 [[SHL]])
542-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
543-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
539+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[Y:%.*]], [[C]]
540+
; CHECK-NEXT: [[LSHR:%.*]] = sub nuw i32 [[TMP1]], [[X]]
544541
; CHECK-NEXT: ret i32 [[LSHR]]
545542
;
546543
%shl = shl nuw i32 %x, %c

0 commit comments

Comments
 (0)