llvm.org GIT mirror llvm / 922426e
[DAGCombine] Fix issue with out of bound constant rotation (PR33828) Take the modulo of rotations by a constant greater than or equal to the bit-width git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308302 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Pilgrim 3 years ago
2 changed file(s) with 58 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
52785278 SDValue N0 = N->getOperand(0);
52795279 SDValue N1 = N->getOperand(1);
52805280 EVT VT = N->getValueType(0);
5281 unsigned Bitsize = VT.getScalarSizeInBits();
52815282
52825283 // fold (rot x, 0) -> x
52835284 if (isNullConstantOrNullSplatConstant(N1))
52845285 return N0;
5286
5287 // fold (rot x, c) -> (rot x, c % BitSize)
5288 if (ConstantSDNode *Cst = isConstOrConstSplat(N1)) {
5289 if (Cst->getAPIntValue().uge(Bitsize)) {
5290 uint64_t RotAmt = Cst->getAPIntValue().urem(Bitsize);
5291 return DAG.getNode(N->getOpcode(), dl, VT, N0,
5292 DAG.getConstant(RotAmt, dl, N1.getValueType()));
5293 }
5294 }
52855295
52865296 // fold (rot* x, (trunc (and y, c))) -> (rot* x, (and (trunc y), (trunc c))).
52875297 if (N1.getOpcode() == ISD::TRUNCATE &&
53015311 unsigned CombineOp = SameSide ? ISD::ADD : ISD::SUB;
53025312 if (SDValue CombinedShift =
53035313 DAG.FoldConstantArithmetic(CombineOp, dl, ShiftVT, C1, C2)) {
5304 unsigned Bitsize = VT.getScalarSizeInBits();
53055314 SDValue BitsizeC = DAG.getConstant(Bitsize, dl, ShiftVT);
53065315 SDValue CombinedShiftNorm = DAG.FoldConstantArithmetic(
53075316 ISD::SREM, dl, ShiftVT, CombinedShift.getNode(),
0 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
1 ; RUN: llc < %s -mtriple=i686-unknown-unknown -mcpu=haswell | FileCheck %s --check-prefix=X86
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=haswell | FileCheck %s --check-prefix=X64
3
4 @var_580 = external local_unnamed_addr global i8, align 1
5
6 define void @foo() {
7 ; X86-LABEL: foo:
8 ; X86: # BB#0: # %entry
9 ; X86-NEXT: movsbl var_580, %eax
10 ; X86-NEXT: testl $-536870913, %eax # imm = 0xDFFFFFFF
11 ; X86-NEXT: jne .LBB0_1
12 ; X86-NEXT: # BB#2: # %if.end13
13 ; X86-NEXT: retl
14 ; X86-NEXT: .LBB0_1: # %if.then11
15 ;
16 ; X64-LABEL: foo:
17 ; X64: # BB#0: # %entry
18 ; X64-NEXT: movsbl {{.*}}(%rip), %eax
19 ; X64-NEXT: testl $-536870913, %eax # imm = 0xDFFFFFFF
20 ; X64-NEXT: jne .LBB0_1
21 ; X64-NEXT: # BB#2: # %if.end13
22 ; X64-NEXT: retq
23 ; X64-NEXT: .LBB0_1: # %if.then11
24 entry:
25 %tmp = icmp ugt i8 undef, 60
26 %phitmp = zext i1 %tmp to i16
27 br label %if.end
28
29 if.end:
30 %tmp1 = load i8, i8* @var_580, align 1
31 %conv7 = sext i8 %tmp1 to i32
32 %conv8 = zext i16 %phitmp to i32
33 %mul = shl nuw nsw i32 %conv8, 1
34 %div9 = udiv i32 %mul, 71
35 %sub = add nsw i32 %div9, -3
36 %shl = shl i32 1, %sub
37 %neg = xor i32 %shl, -1
38 %and = and i32 %neg, %conv7
39 %tobool10 = icmp eq i32 %and, 0
40 br i1 %tobool10, label %if.end13, label %if.then11
41
42 if.then11:
43 unreachable
44
45 if.end13:
46 ret void
47 }