xref: /aosp_15_r20/external/llvm/test/Transforms/LoopSimplify/ashr-crash.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt -basicaa -loop-rotate -licm -instcombine -indvars -loop-unroll -S %s | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker;
3*9880d681SAndroid Build Coastguard Worker; PR18361: ScalarEvolution::getAddRecExpr():
4*9880d681SAndroid Build Coastguard Worker;          Assertion `isLoopInvariant(Operands[i],...
5*9880d681SAndroid Build Coastguard Worker;
6*9880d681SAndroid Build Coastguard Worker; After a series of loop optimizations, SCEV's LoopDispositions grow stale.
7*9880d681SAndroid Build Coastguard Worker; In particular, LoopSimplify hoists %cmp4, resulting in this SCEV for %add:
8*9880d681SAndroid Build Coastguard Worker; {(zext i1 %cmp4 to i32),+,1}<nw><%for.cond1.preheader>
9*9880d681SAndroid Build Coastguard Worker;
10*9880d681SAndroid Build Coastguard Worker; When recomputing the SCEV for %ashr, we truncate the operands to get:
11*9880d681SAndroid Build Coastguard Worker; (zext i1 %cmp4 to i16)
12*9880d681SAndroid Build Coastguard Worker;
13*9880d681SAndroid Build Coastguard Worker; This SCEV was never mapped to a value so never invalidated. It's
14*9880d681SAndroid Build Coastguard Worker; loop disposition is still marked as non-loop-invariant, which is
15*9880d681SAndroid Build Coastguard Worker; inconsistent with the AddRec.
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Workertarget datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
18*9880d681SAndroid Build Coastguard Workertarget triple = "x86_64-apple-macosx"
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker@d = common global i32 0, align 4
21*9880d681SAndroid Build Coastguard Worker@a = common global i32 0, align 4
22*9880d681SAndroid Build Coastguard Worker@c = common global i32 0, align 4
23*9880d681SAndroid Build Coastguard Worker@b = common global i32 0, align 4
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker; Check that the def-use chain that leads to the bad SCEV is still
26*9880d681SAndroid Build Coastguard Worker; there.
27*9880d681SAndroid Build Coastguard Worker;
28*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @foo
29*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: entry:
30*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: for.cond1.preheader:
31*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: for.body3:
32*9880d681SAndroid Build Coastguard Worker; CHECK: %cmp4.le.le
33*9880d681SAndroid Build Coastguard Worker; CHECK: %conv.le.le = zext i1 %cmp4.le.le to i32
34*9880d681SAndroid Build Coastguard Worker; CHECK: %xor.le.le = xor i32 %conv6.le.le, 1
35*9880d681SAndroid Build Coastguard Workerdefine void @foo() {
36*9880d681SAndroid Build Coastguard Workerentry:
37*9880d681SAndroid Build Coastguard Worker  br label %for.cond
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Workerfor.cond:                                         ; preds = %for.inc7, %entry
40*9880d681SAndroid Build Coastguard Worker  %storemerge = phi i32 [ 0, %entry ], [ %inc8, %for.inc7 ]
41*9880d681SAndroid Build Coastguard Worker  %f.0 = phi i32 [ undef, %entry ], [ %f.1, %for.inc7 ]
42*9880d681SAndroid Build Coastguard Worker  store i32 %storemerge, i32* @d, align 4
43*9880d681SAndroid Build Coastguard Worker  %cmp = icmp slt i32 %storemerge, 1
44*9880d681SAndroid Build Coastguard Worker  br i1 %cmp, label %for.cond1, label %for.end9
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workerfor.cond1:                                        ; preds = %for.cond, %for.body3
47*9880d681SAndroid Build Coastguard Worker  %storemerge1 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]
48*9880d681SAndroid Build Coastguard Worker  %f.1 = phi i32 [ %xor, %for.body3 ], [ %f.0, %for.cond ]
49*9880d681SAndroid Build Coastguard Worker  store i32 %storemerge1, i32* @a, align 4
50*9880d681SAndroid Build Coastguard Worker  %cmp2 = icmp slt i32 %storemerge1, 1
51*9880d681SAndroid Build Coastguard Worker  br i1 %cmp2, label %for.body3, label %for.inc7
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Workerfor.body3:                                        ; preds = %for.cond1
54*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* @c, align 4
55*9880d681SAndroid Build Coastguard Worker  %cmp4 = icmp sge i32 %storemerge1, %0
56*9880d681SAndroid Build Coastguard Worker  %conv = zext i1 %cmp4 to i32
57*9880d681SAndroid Build Coastguard Worker  %1 = load i32, i32* @d, align 4
58*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %conv, %1
59*9880d681SAndroid Build Coastguard Worker  %sext = shl i32 %add, 16
60*9880d681SAndroid Build Coastguard Worker  %conv6 = ashr exact i32 %sext, 16
61*9880d681SAndroid Build Coastguard Worker  %xor = xor i32 %conv6, 1
62*9880d681SAndroid Build Coastguard Worker  %inc = add nsw i32 %storemerge1, 1
63*9880d681SAndroid Build Coastguard Worker  br label %for.cond1
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Workerfor.inc7:                                         ; preds = %for.cond1
66*9880d681SAndroid Build Coastguard Worker  %2 = load i32, i32* @d, align 4
67*9880d681SAndroid Build Coastguard Worker  %inc8 = add nsw i32 %2, 1
68*9880d681SAndroid Build Coastguard Worker  br label %for.cond
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Workerfor.end9:                                         ; preds = %for.cond
71*9880d681SAndroid Build Coastguard Worker  %cmp10 = icmp sgt i32 %f.0, 0
72*9880d681SAndroid Build Coastguard Worker  br i1 %cmp10, label %if.then, label %if.end
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Workerif.then:                                          ; preds = %for.end9
75*9880d681SAndroid Build Coastguard Worker  store i32 0, i32* @b, align 4
76*9880d681SAndroid Build Coastguard Worker  br label %if.end
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Workerif.end:                                           ; preds = %if.then, %for.end9
79*9880d681SAndroid Build Coastguard Worker  ret void
80*9880d681SAndroid Build Coastguard Worker}
81