1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2 ; RUN: opt < %s -S -passes=loop-unroll,simplifycfg,instcombine -unroll-force-peel-count=3 -verify-dom-info -simplifycfg-require-and-preserve-domtree=1 | FileCheck %s
3 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll,simplifycfg,instcombine' -unroll-force-peel-count=3 -verify-dom-info | FileCheck %s
4 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll<peeling;no-runtime>,simplifycfg,instcombine' -unroll-force-peel-count=3 -verify-dom-info | FileCheck %s
6 ; Basic loop peeling - check that we can peel-off the first 3 loop iterations
7 ; when explicitly requested.
8 define void @basic(ptr %p, i32 %k) #0 {
9 ; CHECK-LABEL: define void @basic(
10 ; CHECK-SAME: ptr [[P:%.*]], i32 [[K:%.*]]) {
12 ; CHECK-NEXT: [[CMP3:%.*]] = icmp sgt i32 [[K]], 0
13 ; CHECK-NEXT: br i1 [[CMP3]], label [[FOR_BODY_PEEL:%.*]], label [[FOR_END:%.*]]
14 ; CHECK: for.body.peel:
15 ; CHECK-NEXT: store i32 0, ptr [[P]], align 4
16 ; CHECK-NEXT: [[CMP_PEEL_NOT:%.*]] = icmp eq i32 [[K]], 1
17 ; CHECK-NEXT: br i1 [[CMP_PEEL_NOT]], label [[FOR_END]], label [[FOR_BODY_PEEL2:%.*]]
18 ; CHECK: for.body.peel2:
19 ; CHECK-NEXT: [[INCDEC_PTR_PEEL:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 4
20 ; CHECK-NEXT: store i32 1, ptr [[INCDEC_PTR_PEEL]], align 4
21 ; CHECK-NEXT: [[CMP_PEEL5:%.*]] = icmp sgt i32 [[K]], 2
22 ; CHECK-NEXT: br i1 [[CMP_PEEL5]], label [[FOR_BODY_PEEL7:%.*]], label [[FOR_END]]
23 ; CHECK: for.body.peel7:
24 ; CHECK-NEXT: [[INCDEC_PTR_PEEL3:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8
25 ; CHECK-NEXT: [[INCDEC_PTR_PEEL8:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 12
26 ; CHECK-NEXT: store i32 2, ptr [[INCDEC_PTR_PEEL3]], align 4
27 ; CHECK-NEXT: [[CMP_PEEL10_NOT:%.*]] = icmp eq i32 [[K]], 3
28 ; CHECK-NEXT: br i1 [[CMP_PEEL10_NOT]], label [[FOR_END]], label [[FOR_BODY:%.*]]
30 ; CHECK-NEXT: [[I_05:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 3, [[FOR_BODY_PEEL7]] ]
31 ; CHECK-NEXT: [[P_ADDR_04:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ], [ [[INCDEC_PTR_PEEL8]], [[FOR_BODY_PEEL7]] ]
32 ; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds i8, ptr [[P_ADDR_04]], i64 4
33 ; CHECK-NEXT: store i32 [[I_05]], ptr [[P_ADDR_04]], align 4
34 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I_05]], 1
35 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[INC]], [[K]]
36 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END]], !llvm.loop [[LOOP0:![0-9]+]]
38 ; CHECK-NEXT: ret void
41 %cmp3 = icmp slt i32 0, %k
42 br i1 %cmp3, label %for.body.lr.ph, label %for.end
44 for.body.lr.ph: ; preds = %entry
47 for.body: ; preds = %for.body.lr.ph, %for.body
48 %i.05 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
49 %p.addr.04 = phi ptr [ %p, %for.body.lr.ph ], [ %incdec.ptr, %for.body ]
50 %incdec.ptr = getelementptr inbounds i32, ptr %p.addr.04, i32 1
51 store i32 %i.05, ptr %p.addr.04, align 4
52 %inc = add nsw i32 %i.05, 1
53 %cmp = icmp slt i32 %inc, %k
54 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge, !llvm.loop !1
56 for.cond.for.end_crit_edge: ; preds = %for.body
59 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
65 ; Make sure peeling works correctly when a value defined in a loop is used
66 ; in later code - we need to correctly plumb the phi depending on which
67 ; iteration is actually used.
68 define i32 @output(ptr %p, i32 %k) #0 {
69 ; CHECK-LABEL: define i32 @output(
70 ; CHECK-SAME: ptr [[P:%.*]], i32 [[K:%.*]]) {
72 ; CHECK-NEXT: [[CMP3:%.*]] = icmp sgt i32 [[K]], 0
73 ; CHECK-NEXT: br i1 [[CMP3]], label [[FOR_BODY_PEEL:%.*]], label [[FOR_END:%.*]]
74 ; CHECK: for.body.peel:
75 ; CHECK-NEXT: store i32 0, ptr [[P]], align 4
76 ; CHECK-NEXT: [[CMP_PEEL_NOT:%.*]] = icmp eq i32 [[K]], 1
77 ; CHECK-NEXT: br i1 [[CMP_PEEL_NOT]], label [[FOR_END]], label [[FOR_BODY_PEEL2:%.*]]
78 ; CHECK: for.body.peel2:
79 ; CHECK-NEXT: [[INCDEC_PTR_PEEL:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 4
80 ; CHECK-NEXT: store i32 1, ptr [[INCDEC_PTR_PEEL]], align 4
81 ; CHECK-NEXT: [[CMP_PEEL5:%.*]] = icmp sgt i32 [[K]], 2
82 ; CHECK-NEXT: br i1 [[CMP_PEEL5]], label [[FOR_BODY_PEEL7:%.*]], label [[FOR_END]]
83 ; CHECK: for.body.peel7:
84 ; CHECK-NEXT: [[INCDEC_PTR_PEEL3:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8
85 ; CHECK-NEXT: [[INCDEC_PTR_PEEL8:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 12
86 ; CHECK-NEXT: store i32 2, ptr [[INCDEC_PTR_PEEL3]], align 4
87 ; CHECK-NEXT: [[CMP_PEEL10_NOT:%.*]] = icmp eq i32 [[K]], 3
88 ; CHECK-NEXT: br i1 [[CMP_PEEL10_NOT]], label [[FOR_END]], label [[FOR_BODY:%.*]]
90 ; CHECK-NEXT: [[I_05:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 3, [[FOR_BODY_PEEL7]] ]
91 ; CHECK-NEXT: [[P_ADDR_04:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ], [ [[INCDEC_PTR_PEEL8]], [[FOR_BODY_PEEL7]] ]
92 ; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds i8, ptr [[P_ADDR_04]], i64 4
93 ; CHECK-NEXT: store i32 [[I_05]], ptr [[P_ADDR_04]], align 4
94 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I_05]], 1
95 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[INC]], [[K]]
96 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END]], !llvm.loop [[LOOP3:![0-9]+]]
98 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 1, [[FOR_BODY_PEEL]] ], [ 2, [[FOR_BODY_PEEL2]] ], [ 3, [[FOR_BODY_PEEL7]] ], [ [[INC]], [[FOR_BODY]] ]
99 ; CHECK-NEXT: ret i32 [[RET]]
102 %cmp3 = icmp slt i32 0, %k
103 br i1 %cmp3, label %for.body.lr.ph, label %for.end
105 for.body.lr.ph: ; preds = %entry
108 for.body: ; preds = %for.body.lr.ph, %for.body
109 %i.05 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
110 %p.addr.04 = phi ptr [ %p, %for.body.lr.ph ], [ %incdec.ptr, %for.body ]
111 %incdec.ptr = getelementptr inbounds i32, ptr %p.addr.04, i32 1
112 store i32 %i.05, ptr %p.addr.04, align 4
113 %inc = add nsw i32 %i.05, 1
114 %cmp = icmp slt i32 %inc, %k
115 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge, !llvm.loop !2
117 for.cond.for.end_crit_edge: ; preds = %for.body
120 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
121 %ret = phi i32 [ 0, %entry], [ %inc, %for.cond.for.end_crit_edge ]
127 ; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
128 ; CHECK: [[META1]] = !{!"llvm.loop.peeled.count", i32 3}
129 ; CHECK: [[META2]] = !{!"llvm.loop.unroll.disable"}
130 ; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META1]], [[META2]]}