1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=loop-idiom < %s -S | FileCheck %s
4 target datalayout = "e-p:32:32:32-p1:64:64:64-p2:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32-n8:16:32:64"
5 target triple = "x86_64-apple-darwin10.0.0"
7 ; Two dimensional nested loop should be promoted to one big memset.
8 define void @test10(ptr addrspace(2) %X) nounwind ssp {
9 ; CHECK-LABEL: @test10(
11 ; CHECK-NEXT: call void @llvm.memset.p2.i16(ptr addrspace(2) align 1 [[X:%.*]], i8 0, i16 10000, i1 false)
12 ; CHECK-NEXT: br label [[BB_NPH:%.*]]
14 ; CHECK-NEXT: [[I_04:%.*]] = phi i16 [ 0, [[ENTRY:%.*]] ], [ [[INC12:%.*]], [[FOR_INC10:%.*]] ]
15 ; CHECK-NEXT: [[TMP0:%.*]] = mul nuw nsw i16 [[I_04]], 100
16 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr addrspace(2) [[X]], i16 [[TMP0]]
17 ; CHECK-NEXT: br label [[FOR_BODY5:%.*]]
19 ; CHECK-NEXT: [[J_02:%.*]] = phi i16 [ 0, [[BB_NPH]] ], [ [[INC:%.*]], [[FOR_BODY5]] ]
20 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i16 [[I_04]], 100
21 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i16 [[J_02]], [[MUL]]
22 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr addrspace(2) [[X]], i16 [[ADD]]
23 ; CHECK-NEXT: [[INC]] = add nsw i16 [[J_02]], 1
24 ; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i16 [[INC]], 100
25 ; CHECK-NEXT: br i1 [[CMP4]], label [[FOR_INC10]], label [[FOR_BODY5]]
27 ; CHECK-NEXT: [[INC12]] = add nsw i16 [[I_04]], 1
28 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[INC12]], 100
29 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_END13:%.*]], label [[BB_NPH]]
31 ; CHECK-NEXT: ret void
37 bb.nph: ; preds = %entry, %for.inc10
38 %i.04 = phi i16 [ 0, %entry ], [ %inc12, %for.inc10 ]
41 for.body5: ; preds = %for.body5, %bb.nph
42 %j.02 = phi i16 [ 0, %bb.nph ], [ %inc, %for.body5 ]
43 %mul = mul nsw i16 %i.04, 100
44 %add = add nsw i16 %j.02, %mul
45 %arrayidx = getelementptr inbounds i8, ptr addrspace(2) %X, i16 %add
46 store i8 0, ptr addrspace(2) %arrayidx, align 1
47 %inc = add nsw i16 %j.02, 1
48 %cmp4 = icmp eq i16 %inc, 100
49 br i1 %cmp4, label %for.inc10, label %for.body5
51 for.inc10: ; preds = %for.body5
52 %inc12 = add nsw i16 %i.04, 1
53 %cmp = icmp eq i16 %inc12, 100
54 br i1 %cmp, label %for.end13, label %bb.nph
56 for.end13: ; preds = %for.inc10
60 define void @test11_pattern(ptr addrspace(2) nocapture %P) nounwind ssp {
61 ; CHECK-LABEL: @test11_pattern(
63 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
65 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ]
66 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, ptr addrspace(2) [[P:%.*]], i64 [[INDVAR]]
67 ; CHECK-NEXT: store i32 1, ptr addrspace(2) [[ARRAYIDX]], align 4
68 ; CHECK-NEXT: [[INDVAR_NEXT]] = add i64 [[INDVAR]], 1
69 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[INDVAR_NEXT]], 10000
70 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
72 ; CHECK-NEXT: ret void
77 for.body: ; preds = %entry, %for.body
78 %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.body ]
79 %arrayidx = getelementptr i32, ptr addrspace(2) %P, i64 %indvar
80 store i32 1, ptr addrspace(2) %arrayidx, align 4
81 %indvar.next = add i64 %indvar, 1
82 %exitcond = icmp eq i64 %indvar.next, 10000
83 br i1 %exitcond, label %for.end, label %for.body
85 for.end: ; preds = %for.body
89 ; PR9815 - This is a partial overlap case that cannot be safely transformed
91 @g_50 = addrspace(2) global [7 x i32] [i32 0, i32 0, i32 0, i32 0, i32 1, i32 0, i32 0], align 16
94 define i32 @test14() nounwind {
95 ; CHECK-LABEL: @test14(
97 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
99 ; CHECK-NEXT: [[TMP5:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
100 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP5]], 4
101 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[ADD]] to i64
102 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [7 x i32], ptr addrspace(2) @g_50, i32 0, i64 [[IDXPROM]]
103 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(2) [[ARRAYIDX]], align 4
104 ; CHECK-NEXT: [[ADD4:%.*]] = add nsw i32 [[TMP5]], 5
105 ; CHECK-NEXT: [[IDXPROM5:%.*]] = sext i32 [[ADD4]] to i64
106 ; CHECK-NEXT: [[ARRAYIDX6:%.*]] = getelementptr inbounds [7 x i32], ptr addrspace(2) @g_50, i32 0, i64 [[IDXPROM5]]
107 ; CHECK-NEXT: store i32 [[TMP2]], ptr addrspace(2) [[ARRAYIDX6]], align 4
108 ; CHECK-NEXT: [[INC]] = add nsw i32 [[TMP5]], 1
109 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[INC]], 2
110 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END:%.*]]
112 ; CHECK-NEXT: [[TMP8:%.*]] = load i32, ptr addrspace(2) getelementptr inbounds ([7 x i32], ptr addrspace(2) @g_50, i32 0, i64 6), align 4
113 ; CHECK-NEXT: ret i32 [[TMP8]]
119 for.body: ; preds = %for.inc, %for.body.lr.ph
120 %tmp5 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
121 %add = add nsw i32 %tmp5, 4
122 %idxprom = sext i32 %add to i64
123 %arrayidx = getelementptr inbounds [7 x i32], ptr addrspace(2) @g_50, i32 0, i64 %idxprom
124 %tmp2 = load i32, ptr addrspace(2) %arrayidx, align 4
125 %add4 = add nsw i32 %tmp5, 5
126 %idxprom5 = sext i32 %add4 to i64
127 %arrayidx6 = getelementptr inbounds [7 x i32], ptr addrspace(2) @g_50, i32 0, i64 %idxprom5
128 store i32 %tmp2, ptr addrspace(2) %arrayidx6, align 4
129 %inc = add nsw i32 %tmp5, 1
130 %cmp = icmp slt i32 %inc, 2
131 br i1 %cmp, label %for.body, label %for.end
133 for.end: ; preds = %for.inc
134 %tmp8 = load i32, ptr addrspace(2) getelementptr inbounds ([7 x i32], ptr addrspace(2) @g_50, i32 0, i64 6), align 4