xref: /llvm-project/llvm/test/Transforms/LoopIdiom/unsafe.ll (revision 055fb7795aa219a3d274d280ec9129784f169f56)
1260679b0SDávid Bolvanský; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
248c6b272SRoman Lebedev; RUN: opt -S < %s -passes=loop-idiom | FileCheck %s
3cee313d2SEric Christopher; CHECK-NOT: memset
4cee313d2SEric Christopher; check that memset is not generated (for stores) because that will result
5cee313d2SEric Christopher; in udiv hoisted out of the loop by the SCEV Expander
6cee313d2SEric Christopher; TODO: ideally we should be able to generate memset
7cee313d2SEric Christopher; if SCEV expander is taught to generate the dependencies
8cee313d2SEric Christopher; at the right point.
9cee313d2SEric Christopher
10cee313d2SEric Christopher@a = global i32 0, align 4
11cee313d2SEric Christopher@b = global i32 0, align 4
12cee313d2SEric Christopher@c = external local_unnamed_addr global [1 x i8], align 1
13cee313d2SEric Christopher
14cee313d2SEric Christopherdefine void @e() local_unnamed_addr {
15260679b0SDávid Bolvanský; CHECK-LABEL: @e(
16260679b0SDávid Bolvanský; CHECK-NEXT:  entry:
17*055fb779SNikita Popov; CHECK-NEXT:    [[D0:%.*]] = load i32, ptr @a, align 4
18*055fb779SNikita Popov; CHECK-NEXT:    [[D1:%.*]] = load i32, ptr @b, align 4
19260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_COND1THREAD_PRE_SPLIT:%.*]]
20260679b0SDávid Bolvanský; CHECK:       for.cond1thread-pre-split.loopexit:
21260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_COND1THREAD_PRE_SPLIT]]
22260679b0SDávid Bolvanský; CHECK:       for.cond1thread-pre-split:
23260679b0SDávid Bolvanský; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[D0]], [[D1]]
24260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_BODY5:%.*]]
25260679b0SDávid Bolvanský; CHECK:       for.body5:
26260679b0SDávid Bolvanský; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_COND1THREAD_PRE_SPLIT]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY5]] ]
27260679b0SDávid Bolvanský; CHECK-NEXT:    [[DIVX:%.*]] = sext i32 [[DIV]] to i64
28260679b0SDávid Bolvanský; CHECK-NEXT:    [[TMP0:%.*]] = add nsw i64 [[DIVX]], [[INDVARS_IV]]
29*055fb779SNikita Popov; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [1 x i8], ptr @c, i64 0, i64 [[TMP0]]
30*055fb779SNikita Popov; CHECK-NEXT:    store i8 0, ptr [[ARRAYIDX]], align 1
31260679b0SDávid Bolvanský; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], 1
32260679b0SDávid Bolvanský; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
33260679b0SDávid Bolvanský; CHECK-NEXT:    [[TOBOOL4:%.*]] = icmp eq i32 [[TMP1]], 0
34260679b0SDávid Bolvanský; CHECK-NEXT:    br i1 [[TOBOOL4]], label [[FOR_COND1THREAD_PRE_SPLIT_LOOPEXIT:%.*]], label [[FOR_BODY5]]
35260679b0SDávid Bolvanský;
36cee313d2SEric Christopherentry:
37*055fb779SNikita Popov  %d0 = load i32, ptr @a, align 4
38*055fb779SNikita Popov  %d1 = load i32, ptr @b, align 4
39cee313d2SEric Christopher  br label %for.cond1thread-pre-split
40cee313d2SEric Christopher
41cee313d2SEric Christopherfor.cond1thread-pre-split:                        ; preds = %for.body5, %entry
42cee313d2SEric Christopher  %div = udiv i32 %d0, %d1
43cee313d2SEric Christopher  br label %for.body5
44cee313d2SEric Christopher
45cee313d2SEric Christopherfor.body5:                                        ; preds = %for.body5, %for.cond1thread-pre-split
46cee313d2SEric Christopher  %indvars.iv = phi i64 [ 0, %for.cond1thread-pre-split ], [ %indvars.iv.next, %for.body5 ]
47cee313d2SEric Christopher  %divx = sext i32 %div to i64
48cee313d2SEric Christopher  %0 = add nsw i64 %divx, %indvars.iv
49*055fb779SNikita Popov  %arrayidx = getelementptr inbounds [1 x i8], ptr @c, i64 0, i64 %0
50*055fb779SNikita Popov  store i8 0, ptr %arrayidx, align 1
51cee313d2SEric Christopher  %indvars.iv.next = add nsw i64 %indvars.iv, 1
52cee313d2SEric Christopher  %1 = trunc i64 %indvars.iv.next to i32
53cee313d2SEric Christopher  %tobool4 = icmp eq i32 %1, 0
54cee313d2SEric Christopher  br i1 %tobool4, label %for.cond1thread-pre-split, label %for.body5
55cee313d2SEric Christopher}
56cee313d2SEric Christopher
57cee313d2SEric Christopher; The loop's trip count is depending on an unsafe operation
58cee313d2SEric Christopher; udiv. SCEV expander hoists it out of the loop, so loop-idiom
59cee313d2SEric Christopher; should check that the memset is not generated in this case.
60*055fb779SNikita Popovdefine void @f(i32 %a, i32 %b, ptr nocapture %x) local_unnamed_addr {
61260679b0SDávid Bolvanský; CHECK-LABEL: @f(
62260679b0SDávid Bolvanský; CHECK-NEXT:  entry:
63260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
64260679b0SDávid Bolvanský; CHECK:       for.body.loopexit:
65260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_BODY]]
66260679b0SDávid Bolvanský; CHECK:       for.body:
67260679b0SDávid Bolvanský; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
68260679b0SDávid Bolvanský; CHECK-NEXT:    [[CONV:%.*]] = zext i32 [[DIV]] to i64
69260679b0SDávid Bolvanský; CHECK-NEXT:    br label [[FOR_BODY6:%.*]]
70260679b0SDávid Bolvanský; CHECK:       for.body6:
71260679b0SDávid Bolvanský; CHECK-NEXT:    [[I_09:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_BODY6]] ], [ 0, [[FOR_BODY]] ]
72*055fb779SNikita Popov; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[X:%.*]], i64 [[I_09]]
73*055fb779SNikita Popov; CHECK-NEXT:    store i8 0, ptr [[ARRAYIDX]], align 1
74260679b0SDávid Bolvanský; CHECK-NEXT:    [[INC]] = add nuw nsw i64 [[I_09]], 1
75260679b0SDávid Bolvanský; CHECK-NEXT:    [[CMP3:%.*]] = icmp slt i64 [[INC]], [[CONV]]
76260679b0SDávid Bolvanský; CHECK-NEXT:    br i1 [[CMP3]], label [[FOR_BODY6]], label [[FOR_BODY_LOOPEXIT:%.*]]
77260679b0SDávid Bolvanský;
78cee313d2SEric Christopherentry:
79cee313d2SEric Christopher  br label %for.body
80cee313d2SEric Christopher
81cee313d2SEric Christopherfor.body:                                         ; preds = %for.body6, %entry
82cee313d2SEric Christopher  %div = udiv i32 %a, %b
83cee313d2SEric Christopher  %conv = zext i32 %div to i64
84cee313d2SEric Christopher  br label %for.body6
85cee313d2SEric Christopher
86cee313d2SEric Christopherfor.body6:                                        ; preds = %for.body6, %for.body
87cee313d2SEric Christopher  %i.09 = phi i64 [ %inc, %for.body6 ], [ 0, %for.body ]
88*055fb779SNikita Popov  %arrayidx = getelementptr inbounds i8, ptr %x, i64 %i.09
89*055fb779SNikita Popov  store i8 0, ptr %arrayidx, align 1
90cee313d2SEric Christopher  %inc = add nuw nsw i64 %i.09, 1
91cee313d2SEric Christopher  %cmp3 = icmp slt i64 %inc, %conv
92cee313d2SEric Christopher  br i1 %cmp3, label %for.body6, label %for.body
93cee313d2SEric Christopher}
94cee313d2SEric Christopher
95