xref: /llvm-project/llvm/test/Transforms/LoopIdiom/unsafe.ll (revision 055fb7795aa219a3d274d280ec9129784f169f56)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S < %s -passes=loop-idiom | FileCheck %s
3; CHECK-NOT: memset
4; check that memset is not generated (for stores) because that will result
5; in udiv hoisted out of the loop by the SCEV Expander
6; TODO: ideally we should be able to generate memset
7; if SCEV expander is taught to generate the dependencies
8; at the right point.
9
10@a = global i32 0, align 4
11@b = global i32 0, align 4
12@c = external local_unnamed_addr global [1 x i8], align 1
13
14define void @e() local_unnamed_addr {
15; CHECK-LABEL: @e(
16; CHECK-NEXT:  entry:
17; CHECK-NEXT:    [[D0:%.*]] = load i32, ptr @a, align 4
18; CHECK-NEXT:    [[D1:%.*]] = load i32, ptr @b, align 4
19; CHECK-NEXT:    br label [[FOR_COND1THREAD_PRE_SPLIT:%.*]]
20; CHECK:       for.cond1thread-pre-split.loopexit:
21; CHECK-NEXT:    br label [[FOR_COND1THREAD_PRE_SPLIT]]
22; CHECK:       for.cond1thread-pre-split:
23; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[D0]], [[D1]]
24; CHECK-NEXT:    br label [[FOR_BODY5:%.*]]
25; CHECK:       for.body5:
26; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_COND1THREAD_PRE_SPLIT]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY5]] ]
27; CHECK-NEXT:    [[DIVX:%.*]] = sext i32 [[DIV]] to i64
28; CHECK-NEXT:    [[TMP0:%.*]] = add nsw i64 [[DIVX]], [[INDVARS_IV]]
29; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [1 x i8], ptr @c, i64 0, i64 [[TMP0]]
30; CHECK-NEXT:    store i8 0, ptr [[ARRAYIDX]], align 1
31; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], 1
32; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
33; CHECK-NEXT:    [[TOBOOL4:%.*]] = icmp eq i32 [[TMP1]], 0
34; CHECK-NEXT:    br i1 [[TOBOOL4]], label [[FOR_COND1THREAD_PRE_SPLIT_LOOPEXIT:%.*]], label [[FOR_BODY5]]
35;
36entry:
37  %d0 = load i32, ptr @a, align 4
38  %d1 = load i32, ptr @b, align 4
39  br label %for.cond1thread-pre-split
40
41for.cond1thread-pre-split:                        ; preds = %for.body5, %entry
42  %div = udiv i32 %d0, %d1
43  br label %for.body5
44
45for.body5:                                        ; preds = %for.body5, %for.cond1thread-pre-split
46  %indvars.iv = phi i64 [ 0, %for.cond1thread-pre-split ], [ %indvars.iv.next, %for.body5 ]
47  %divx = sext i32 %div to i64
48  %0 = add nsw i64 %divx, %indvars.iv
49  %arrayidx = getelementptr inbounds [1 x i8], ptr @c, i64 0, i64 %0
50  store i8 0, ptr %arrayidx, align 1
51  %indvars.iv.next = add nsw i64 %indvars.iv, 1
52  %1 = trunc i64 %indvars.iv.next to i32
53  %tobool4 = icmp eq i32 %1, 0
54  br i1 %tobool4, label %for.cond1thread-pre-split, label %for.body5
55}
56
57; The loop's trip count is depending on an unsafe operation
58; udiv. SCEV expander hoists it out of the loop, so loop-idiom
59; should check that the memset is not generated in this case.
60define void @f(i32 %a, i32 %b, ptr nocapture %x) local_unnamed_addr {
61; CHECK-LABEL: @f(
62; CHECK-NEXT:  entry:
63; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
64; CHECK:       for.body.loopexit:
65; CHECK-NEXT:    br label [[FOR_BODY]]
66; CHECK:       for.body:
67; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
68; CHECK-NEXT:    [[CONV:%.*]] = zext i32 [[DIV]] to i64
69; CHECK-NEXT:    br label [[FOR_BODY6:%.*]]
70; CHECK:       for.body6:
71; CHECK-NEXT:    [[I_09:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_BODY6]] ], [ 0, [[FOR_BODY]] ]
72; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[X:%.*]], i64 [[I_09]]
73; CHECK-NEXT:    store i8 0, ptr [[ARRAYIDX]], align 1
74; CHECK-NEXT:    [[INC]] = add nuw nsw i64 [[I_09]], 1
75; CHECK-NEXT:    [[CMP3:%.*]] = icmp slt i64 [[INC]], [[CONV]]
76; CHECK-NEXT:    br i1 [[CMP3]], label [[FOR_BODY6]], label [[FOR_BODY_LOOPEXIT:%.*]]
77;
78entry:
79  br label %for.body
80
81for.body:                                         ; preds = %for.body6, %entry
82  %div = udiv i32 %a, %b
83  %conv = zext i32 %div to i64
84  br label %for.body6
85
86for.body6:                                        ; preds = %for.body6, %for.body
87  %i.09 = phi i64 [ %inc, %for.body6 ], [ 0, %for.body ]
88  %arrayidx = getelementptr inbounds i8, ptr %x, i64 %i.09
89  store i8 0, ptr %arrayidx, align 1
90  %inc = add nuw nsw i64 %i.09, 1
91  %cmp3 = icmp slt i64 %inc, %conv
92  br i1 %cmp3, label %for.body6, label %for.body
93}
94
95