Skip to content

Commit 4baf29e

Browse files
committed
[DAG] Handle cases where a shift amount is larger than the pre-extended value bitwidth
In the (zext (shl (zext x), cst)) -> (shl (zext x), cst) fold, don't use a bitmask / MaskedValueIsZero as we can't guarantee that the shift amount is in bounds. Fixes #106202
1 parent 900cd62 commit 4baf29e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14114,13 +14114,10 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1411414114
if (ShAmtC->getAPIntValue().ugt(KnownZeroBits)) {
1411514115
// If the shift is too large, then see if we can deduce that the
1411614116
// shift is safe anyway.
14117-
// Create a mask that has ones for the bits being shifted out.
14118-
APInt ShiftOutMask =
14119-
APInt::getHighBitsSet(ShVal.getValueSizeInBits(),
14120-
ShAmtC->getAPIntValue().getZExtValue());
1412114117

1412214118
// Check if the bits being shifted out are known to be zero.
14123-
if (!DAG.MaskedValueIsZero(ShVal, ShiftOutMask))
14119+
KnownBits KnownShVal = DAG.computeKnownBits(ShVal);
14120+
if (ShAmtC->getAPIntValue().ugt(KnownShVal.countMinLeadingZeros()))
1412414121
return SDValue();
1412514122
}
1412614123
}

llvm/test/CodeGen/SystemZ/pr106202.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=s390x-ibm-linux -mcpu=z10 | FileCheck %s
3+
4+
@g_0 = external dso_local local_unnamed_addr global i16, align 2
5+
@g_1 = external dso_local local_unnamed_addr global i32, align 4
6+
@g_2 = external dso_local local_unnamed_addr global i32, align 4
7+
8+
define void @func() {
9+
; CHECK-LABEL: func:
10+
; CHECK: # %bb.0:
11+
; CHECK-NEXT: lhi %r0, 41
12+
; CHECK-NEXT: strl %r0, g_1
13+
; CHECK-NEXT: lhi %r0, 0
14+
; CHECK-NEXT: strl %r0, g_2
15+
; CHECK-NEXT: br %r14
16+
store i32 41, ptr @g_1, align 4
17+
%1 = load i32, ptr @g_1, align 4
18+
%2 = load i16, ptr @g_0, align 2
19+
%3 = zext i16 %2 to i32
20+
%4 = shl i32 %3, %1
21+
%5 = zext i32 %4 to i64
22+
%6 = shl i64 %5, 48
23+
%7 = ashr exact i64 %6, 48
24+
%8 = or i64 %7, 0
25+
%9 = sext i32 %1 to i64
26+
%10 = icmp sge i64 %8, %9
27+
%11 = zext i1 %10 to i32
28+
%12 = or i32 0, %11
29+
store i32 %12, ptr @g_2, align 4
30+
ret void
31+
}

0 commit comments

Comments
 (0)