Skip to content

Commit 906aa22

Browse files
committed
[InstCombine] Fold adds + shifts with nsw and nuw flags
I also added mul nsw/nuw 3, div 2 since this was the canonical version of ((x << 1) + x) / 2, which is a specific expression which canonicalization causes the InstCombine to miss it.
1 parent 71d21ff commit 906aa22

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,18 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
12671267
match(Op1, m_SpecificIntAllowUndef(BitWidth - 1)))
12681268
return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
12691269

1270+
// If both the add and the shift are nuw, then:
1271+
// ((X << Y) + Z) nuw >>u Z --> X + (Y nuw >>u Z) nuw
1272+
Value *Y;
1273+
if (match(Op0, m_OneUse(m_c_NUWAdd((m_NUWShl(m_Value(X), m_Specific(Op1))),
1274+
m_Value(Y))))) {
1275+
Value *NewLshr = Builder.CreateLShr(Y, Op1, "", I.isExact());
1276+
auto *newAdd = BinaryOperator::CreateNUWAdd(NewLshr, X);
1277+
if (auto *Op0Bin = cast<OverflowingBinaryOperator>(Op0))
1278+
newAdd->setHasNoSignedWrap(Op0Bin->hasNoSignedWrap());
1279+
return newAdd;
1280+
}
1281+
12701282
if (match(Op1, m_APInt(C))) {
12711283
unsigned ShAmtC = C->getZExtValue();
12721284
auto *II = dyn_cast<IntrinsicInst>(Op0);
@@ -1283,7 +1295,6 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
12831295
return new ZExtInst(Cmp, Ty);
12841296
}
12851297

1286-
Value *X;
12871298
const APInt *C1;
12881299
if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
12891300
if (C1->ult(ShAmtC)) {
@@ -1328,7 +1339,6 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
13281339
// ((X << C) + Y) >>u C --> (X + (Y >>u C)) & (-1 >>u C)
13291340
// TODO: Consolidate with the more general transform that starts from shl
13301341
// (the shifts are in the opposite order).
1331-
Value *Y;
13321342
if (match(Op0,
13331343
m_OneUse(m_c_Add(m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))),
13341344
m_Value(Y))))) {
@@ -1450,9 +1460,24 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14501460
NewMul->setHasNoSignedWrap(true);
14511461
return NewMul;
14521462
}
1463+
1464+
// Special case: lshr nuw (mul (X, 3), 1) -> add nuw nsw (X, lshr(X, 1)
1465+
if (ShAmtC == 1 && MulC->getZExtValue() == 3) {
1466+
auto *NewAdd = BinaryOperator::CreateNUWAdd(
1467+
X,
1468+
Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact()));
1469+
NewAdd->setHasNoSignedWrap(true);
1470+
return NewAdd;
1471+
}
14531472
}
14541473
}
14551474

1475+
// // lshr nsw (mul (X, 3), 1) -> add nsw (X, lshr(X, 1)
1476+
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_SpecificInt(3)))) &&
1477+
ShAmtC == 1)
1478+
return BinaryOperator::CreateNSWAdd(
1479+
X, Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact()));
1480+
14561481
// Try to narrow bswap.
14571482
// In the case where the shift amount equals the bitwidth difference, the
14581483
// shift is eliminated.
@@ -1656,6 +1681,26 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
16561681
if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
16571682
return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
16581683
}
1684+
1685+
// Special case: ashr nuw (mul (X, 3), 1) -> add nuw nsw (X, lshr(X, 1)
1686+
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_SpecificInt(3)))) &&
1687+
ShAmt == 1) {
1688+
Value *Shift;
1689+
if (auto *Op0Bin = cast<OverflowingBinaryOperator>(Op0)) {
1690+
if (Op0Bin->hasNoUnsignedWrap())
1691+
// We can use lshr if the mul is nuw and nsw
1692+
Shift =
1693+
Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact());
1694+
else
1695+
Shift =
1696+
Builder.CreateAShr(X, ConstantInt::get(Ty, 1), "", I.isExact());
1697+
1698+
auto *NewAdd = BinaryOperator::CreateNSWAdd(X, Shift);
1699+
NewAdd->setHasNoUnsignedWrap(Op0Bin->hasNoUnsignedWrap());
1700+
1701+
return NewAdd;
1702+
}
1703+
}
16591704
}
16601705

16611706
const SimplifyQuery Q = SQ.getWithInstruction(&I);

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ define <3 x i14> @mul_splat_fold_vec(<3 x i14> %x) {
362362

363363
define i32 @mul_times_3_div_2 (i32 %x) {
364364
; CHECK-LABEL: @mul_times_3_div_2(
365-
; CHECK-NEXT: [[TMP1:%.*]] = mul nuw nsw i32 [[X:%.*]], 3
366-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], 1
367-
; CHECK-NEXT: ret i32 [[TMP2]]
365+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1:%.*]], 1
366+
; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], [[TMP1]]
367+
; CHECK-NEXT: ret i32 [[TMP3]]
368368
;
369369
%2 = mul nsw nuw i32 %x, 3
370370
%3 = lshr i32 %2, 1
@@ -373,21 +373,20 @@ define i32 @mul_times_3_div_2 (i32 %x) {
373373

374374
define i32 @shl_add_lshr (i32 %x, i32 %c, i32 %y) {
375375
; CHECK-LABEL: @shl_add_lshr(
376-
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
377-
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP1]], [[Y:%.*]]
378-
; CHECK-NEXT: [[TMP3:%.*]] = lshr exact i32 [[TMP2]], [[C]]
379-
; CHECK-NEXT: ret i32 [[TMP3]]
376+
; CHECK-NEXT: [[TMP3:%.*]] = lshr exact i32 [[TMP2:%.*]], [[C:%.*]]
377+
; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i32 [[TMP3]], [[X:%.*]]
378+
; CHECK-NEXT: ret i32 [[TMP4]]
380379
;
381380
%2 = shl nuw i32 %x, %c
382-
%3 = add nsw nuw i32 %2, %y
381+
%3 = add nuw nsw i32 %2, %y
383382
%4 = lshr exact i32 %3, %c
384383
ret i32 %4
385384
}
386385

387386
define i32 @ashr_mul_times_3_div_2 (i32 %0) {
388387
; CHECK-LABEL: @ashr_mul_times_3_div_2(
389-
; CHECK-NEXT: [[TMP2:%.*]] = mul nuw nsw i32 [[TMP0:%.*]], 3
390-
; CHECK-NEXT: [[TMP3:%.*]] = ashr i32 [[TMP2]], 1
388+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP0:%.*]], 1
389+
; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], [[TMP0]]
391390
; CHECK-NEXT: ret i32 [[TMP3]]
392391
;
393392
%2 = mul nsw nuw i32 %0, 3
@@ -397,9 +396,9 @@ define i32 @mul_times_3_div_2 (i32 %x) {
397396

398397
define i32 @ashr_mul_times_3_div_2_exact (i32 %0) {
399398
; CHECK-LABEL: @ashr_mul_times_3_div_2_exact(
400-
; CHECK-NEXT: [[TMP2:%.*]] = mul nsw i32 [[TMP0:%.*]], 3
401-
; CHECK-NEXT: [[TMP3:%.*]] = ashr exact i32 [[TMP2]], 1
402-
; CHECK-NEXT: ret i32 [[TMP3]]
399+
; CHECK-NEXT: [[TMP3:%.*]] = ashr exact i32 [[TMP2:%.*]], 1
400+
; CHECK-NEXT: [[TMP4:%.*]] = add nsw i32 [[TMP3]], [[TMP2]]
401+
; CHECK-NEXT: ret i32 [[TMP4]]
403402
;
404403
%2 = mul nsw i32 %0, 3
405404
%3 = ashr exact i32 %2, 1

0 commit comments

Comments
 (0)