1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ;RUN: opt -loop-unswitch -enable-new-pm=0 -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s
4 define i32 @foo(i32 %a, i32 %b) {
7 ; CHECK-NEXT: [[CMP0:%.*]] = icmp sgt i32 [[B:%.*]], 0
8 ; CHECK-NEXT: br i1 [[CMP0]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]]
9 ; CHECK: for.body.preheader:
10 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], 12345
11 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_BODY_US:%.*]], label [[FOR_BODY:%.*]], !prof [[PROF0:![0-9]+]]
13 ; CHECK-NEXT: [[INC_I_US:%.*]] = phi i32 [ [[INC_US:%.*]], [[FOR_BODY_US]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
14 ; CHECK-NEXT: [[MUL_I_US:%.*]] = phi i32 [ [[B]], [[FOR_BODY_US]] ], [ 3, [[FOR_BODY_PREHEADER]] ]
15 ; CHECK-NEXT: [[ADD_I_US:%.*]] = phi i32 [ [[ADD_US:%.*]], [[FOR_BODY_US]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ]
16 ; CHECK-NEXT: [[ADD_US]] = add nsw i32 [[ADD_I_US]], 123
17 ; CHECK-NEXT: [[INC_US]] = add nuw nsw i32 [[INC_I_US]], 1
18 ; CHECK-NEXT: [[EXITCOND_US:%.*]] = icmp eq i32 [[INC_US]], [[B]]
19 ; CHECK-NEXT: br i1 [[EXITCOND_US]], label [[FOR_COND_CLEANUP]], label [[FOR_BODY_US]]
21 ; CHECK-NEXT: [[INC_I:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
22 ; CHECK-NEXT: [[MUL_I:%.*]] = phi i32 [ [[MUL:%.*]], [[FOR_BODY]] ], [ 3, [[FOR_BODY_PREHEADER]] ]
23 ; CHECK-NEXT: [[ADD_I:%.*]] = phi i32 [ [[A]], [[FOR_BODY]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ]
24 ; CHECK-NEXT: [[MUL]] = mul nsw i32 [[MUL_I]], [[B]]
25 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[INC_I]], 1
26 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[B]]
27 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP]], label [[FOR_BODY]]
28 ; CHECK: for.cond.cleanup:
29 ; CHECK-NEXT: [[T2:%.*]] = phi i32 [ [[B]], [[ENTRY:%.*]] ], [ [[B]], [[FOR_BODY_US]] ], [ [[MUL]], [[FOR_BODY]] ]
30 ; CHECK-NEXT: [[T1:%.*]] = phi i32 [ [[A]], [[ENTRY]] ], [ [[ADD_US]], [[FOR_BODY_US]] ], [ [[A]], [[FOR_BODY]] ]
31 ; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[T2]], [[T1]]
32 ; CHECK-NEXT: ret i32 [[ADD3]]
35 br label %for.body.lr.ph
37 for.body.lr.ph: ; preds = %entry
38 %cmp0 = icmp sgt i32 %b, 0
39 br i1 %cmp0, label %for.body, label %for.cond.cleanup
41 for.body: ; preds = %for.inc, %for.body.lr.ph
42 %inc.i = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.inc ]
43 %mul.i = phi i32 [ 3, %for.body.lr.ph ], [ %mul.p, %for.inc ]
44 %add.i = phi i32 [ %a, %for.body.lr.ph ], [ %add.p, %for.inc ]
45 %cmp1 = icmp eq i32 %a, 12345
46 br i1 %cmp1, label %if.then, label %if.else, !prof !0
47 if.then: ; preds = %for.body
48 %add = add nsw i32 %add.i, 123
51 if.else: ; preds = %for.body
52 %mul = mul nsw i32 %mul.i, %b
54 for.inc: ; preds = %if.then, %if.else
55 %mul.p = phi i32 [ %b, %if.then ], [ %mul, %if.else ]
56 %add.p = phi i32 [ %add, %if.then ], [ %a, %if.else ]
57 %inc = add nuw nsw i32 %inc.i, 1
58 %exitcond = icmp eq i32 %inc, %b
59 br i1 %exitcond, label %for.cond.cleanup, label %for.body
61 for.cond.cleanup: ; preds = %for.inc, %for.body.lr.ph
62 %t2 = phi i32 [ %b, %for.body.lr.ph ], [ %mul.p, %for.inc ]
63 %t1 = phi i32 [ %a, %for.body.lr.ph ], [ %add.p, %for.inc ]
64 %add3 = add nsw i32 %t2, %t1
68 define void @foo_swapped(i32 %a, i32 %b) {
69 ; CHECK-LABEL: @foo_swapped(
71 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 1, 2
72 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_BODY:%.*]], label [[FOR_COND_CLEANUP_SPLIT:%.*]], !prof [[PROF1:![0-9]+]]
74 ; CHECK-NEXT: [[INC_I:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
75 ; CHECK-NEXT: [[ADD_I:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ], [ 100, [[ENTRY]] ]
76 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[INC_I]], 1
77 ; CHECK-NEXT: [[ADD]] = add nsw i32 [[A:%.*]], [[ADD_I]]
78 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[B:%.*]]
79 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP_SPLIT]], label [[FOR_BODY]]
80 ; CHECK: for.cond.cleanup.split:
81 ; CHECK-NEXT: ret void
85 for.body: ; preds = %for.inc, %entry
86 %inc.i = phi i32 [ 0, %entry ], [ %inc, %if.then ]
87 %add.i = phi i32 [ 100, %entry ], [ %add, %if.then ]
88 %inc = add nuw nsw i32 %inc.i, 1
89 %cmp1 = icmp eq i32 1, 2
90 br i1 %cmp1, label %if.then, label %for.cond.cleanup, !prof !0
92 if.then: ; preds = %for.body
93 %add = add nsw i32 %a, %add.i
95 %exitcond = icmp eq i32 %inc, %b
96 br i1 %exitcond, label %for.cond.cleanup, label %for.body
98 for.cond.cleanup: ; preds = %for.inc, %for.body.lr.ph, %for.body
101 !0 = !{!"branch_weights", i32 64, i32 4}
103 ;CHECK: !0 = !{!"branch_weights", i32 64, i32 4}
104 ;CHECK: !1 = !{!"branch_weights", i32 4, i32 64}