Skip to content

Commit c27ad80

Browse files
LebedevRItstellar
authored andcommitted
[InstCombine] dropRedundantMaskingOfLeftShiftInput(): check that adding shift amounts doesn't overflow (PR49778)
This is identical to 781d077, but for the other function. For certain shift amount bit widths, we must first ensure that adding shift amounts is safe, that the sum won't have an unsigned overflow. Fixes https://bugs.llvm.org/show_bug.cgi?id=49778 (cherry picked from commit 2760a80)
1 parent 4a4b1c7 commit c27ad80

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
226226
// Peek through an optional zext of the shift amount.
227227
match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
228228

229-
// We have two shift amounts from two different shifts. The types of those
230-
// shift amounts may not match. If that's the case let's bailout now.
231-
if (MaskShAmt->getType() != ShiftShAmt->getType())
229+
// Verify that it would be safe to try to add those two shift amounts.
230+
if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
231+
MaskShAmt))
232232
return nullptr;
233233

234234
// Can we simplify (MaskShAmt+ShiftShAmt) ?
@@ -258,9 +258,9 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
258258
// Peek through an optional zext of the shift amount.
259259
match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
260260

261-
// We have two shift amounts from two different shifts. The types of those
262-
// shift amounts may not match. If that's the case let's bailout now.
263-
if (MaskShAmt->getType() != ShiftShAmt->getType())
261+
// Verify that it would be safe to try to add those two shift amounts.
262+
if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
263+
MaskShAmt))
264264
return nullptr;
265265

266266
// Can we simplify (ShiftShAmt-MaskShAmt) ?

llvm/test/Transforms/InstCombine/redundant-left-shift-input-masking-pr49778.ll

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
; PR49778: this should not be folded to 0.
55
define i32 @src(i1 %x2) {
66
; CHECK-LABEL: @src(
7-
; CHECK-NEXT: ret i32 0
7+
; CHECK-NEXT: [[X13:%.*]] = zext i1 [[X2:%.*]] to i32
8+
; CHECK-NEXT: [[_7:%.*]] = shl i32 -1, [[X13]]
9+
; CHECK-NEXT: [[MASK:%.*]] = xor i32 [[_7]], -1
10+
; CHECK-NEXT: [[_8:%.*]] = and i32 [[MASK]], [[X13]]
11+
; CHECK-NEXT: [[_9:%.*]] = shl i32 [[_8]], [[X13]]
12+
; CHECK-NEXT: ret i32 [[_9]]
813
;
914
%x13 = zext i1 %x2 to i32
1015
%_7 = shl i32 4294967295, %x13

0 commit comments

Comments
 (0)