Skip to content

Commit 8a9c70f

Browse files
committed
[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
1 parent a9aa14e commit 8a9c70f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,14 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
399399
reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
400400
return NewShift;
401401

402-
// (C1 shift (A add C2)) -> (C1 shift C2) shift A)
403-
// iff A and C2 are both positive.
402+
// C0 shift (A add C) -> (C0 shift C) shift A
403+
// iff A and C2 are both positive or the add has 'nuw'.
404404
Value *A;
405405
Constant *C;
406406
if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
407-
if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
408-
isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
407+
if (cast<BinaryOperator>(Op1)->hasNoUnsignedWrap() ||
408+
(isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
409+
isKnownNonNegative(C, DL, 0, &AC, &I, &DT)))
409410
return BinaryOperator::Create(
410411
I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
411412

llvm/test/Transforms/InstCombine/shift-add.ll

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,42 @@ define <4 x i32> @lshr_C1_add_A_C2_v4i32_splat(i16 %I) {
123123

124124
define i32 @shl_add_nuw(i32 %x) {
125125
; CHECK-LABEL: @shl_add_nuw(
126-
; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 5
127-
; CHECK-NEXT: [[R:%.*]] = shl i32 6, [[A]]
126+
; CHECK-NEXT: [[R:%.*]] = shl i32 192, [[X:%.*]]
128127
; CHECK-NEXT: ret i32 [[R]]
129128
;
130129
%a = add nuw i32 %x, 5
131130
%r = shl i32 6, %a
132131
ret i32 %r
133132
}
134133

134+
; vectors with arbitrary constants work too
135+
135136
define <2 x i12> @lshr_add_nuw(<2 x i12> %x) {
136137
; CHECK-LABEL: @lshr_add_nuw(
137-
; CHECK-NEXT: [[A:%.*]] = add nuw <2 x i12> [[X:%.*]], <i12 5, i12 1>
138-
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i12> <i12 6, i12 42>, [[A]]
138+
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i12> <i12 0, i12 21>, [[X:%.*]]
139139
; CHECK-NEXT: ret <2 x i12> [[R]]
140140
;
141141
%a = add nuw <2 x i12> %x, <i12 5, i12 1>
142142
%r = lshr <2 x i12> <i12 6, i12 42>, %a
143143
ret <2 x i12> %r
144144
}
145145

146+
; extra use is ok and in this case the result can be simplified to a constant
147+
146148
define i32 @ashr_add_nuw(i32 %x, i32* %p) {
147149
; CHECK-LABEL: @ashr_add_nuw(
148150
; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 5
149151
; CHECK-NEXT: store i32 [[A]], i32* [[P:%.*]], align 4
150-
; CHECK-NEXT: [[R:%.*]] = ashr i32 -6, [[A]]
151-
; CHECK-NEXT: ret i32 [[R]]
152+
; CHECK-NEXT: ret i32 -1
152153
;
153154
%a = add nuw i32 %x, 5
154155
store i32 %a, i32* %p
155156
%r = ashr i32 -6, %a
156157
ret i32 %r
157158
}
158159

160+
; negative test - must have 'nuw'
161+
159162
define i32 @shl_add_nsw(i32 %x) {
160163
; CHECK-LABEL: @shl_add_nsw(
161164
; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 5

0 commit comments

Comments
 (0)