[InstCombine] C0 shift (X add nuw C) --> (C0 shift C) shift X

With 'nuw' we can convert the increment of the shift amount
into a pre-shift (constant fold) of the shifted constant:
https://alive2.llvm.org/ce/z/FkTyR2

Fixes issue #41976
This commit is contained in:
Sanjay Patel 2022-04-19 15:09:45 -04:00
parent a9aa14e0cb
commit 8a9c70fc01
2 changed files with 14 additions and 10 deletions

View file

@ -399,13 +399,14 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
return NewShift;
// (C1 shift (A add C2)) -> (C1 shift C2) shift A)
// iff A and C2 are both positive.
// C0 shift (A add C) -> (C0 shift C) shift A
// iff A and C2 are both positive or the add has 'nuw'.
Value *A;
Constant *C;
if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
if (cast<BinaryOperator>(Op1)->hasNoUnsignedWrap() ||
(isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
isKnownNonNegative(C, DL, 0, &AC, &I, &DT)))
return BinaryOperator::Create(
I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);

View file

@ -123,8 +123,7 @@ define <4 x i32> @lshr_C1_add_A_C2_v4i32_splat(i16 %I) {
define i32 @shl_add_nuw(i32 %x) {
; CHECK-LABEL: @shl_add_nuw(
; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 5
; CHECK-NEXT: [[R:%.*]] = shl i32 6, [[A]]
; CHECK-NEXT: [[R:%.*]] = shl i32 192, [[X:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = add nuw i32 %x, 5
@ -132,10 +131,11 @@ define i32 @shl_add_nuw(i32 %x) {
ret i32 %r
}
; vectors with arbitrary constants work too
define <2 x i12> @lshr_add_nuw(<2 x i12> %x) {
; CHECK-LABEL: @lshr_add_nuw(
; CHECK-NEXT: [[A:%.*]] = add nuw <2 x i12> [[X:%.*]], <i12 5, i12 1>
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i12> <i12 6, i12 42>, [[A]]
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i12> <i12 0, i12 21>, [[X:%.*]]
; CHECK-NEXT: ret <2 x i12> [[R]]
;
%a = add nuw <2 x i12> %x, <i12 5, i12 1>
@ -143,12 +143,13 @@ define <2 x i12> @lshr_add_nuw(<2 x i12> %x) {
ret <2 x i12> %r
}
; extra use is ok and in this case the result can be simplified to a constant
define i32 @ashr_add_nuw(i32 %x, i32* %p) {
; CHECK-LABEL: @ashr_add_nuw(
; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 5
; CHECK-NEXT: store i32 [[A]], i32* [[P:%.*]], align 4
; CHECK-NEXT: [[R:%.*]] = ashr i32 -6, [[A]]
; CHECK-NEXT: ret i32 [[R]]
; CHECK-NEXT: ret i32 -1
;
%a = add nuw i32 %x, 5
store i32 %a, i32* %p
@ -156,6 +157,8 @@ define i32 @ashr_add_nuw(i32 %x, i32* %p) {
ret i32 %r
}
; negative test - must have 'nuw'
define i32 @shl_add_nsw(i32 %x) {
; CHECK-LABEL: @shl_add_nsw(
; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 5