1 ; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
3 ; Check that debug locations are preserved. For more info see:
4 ; https://llvm.org/docs/SourceLevelDebugging.html#fixing-errors
5 ; RUN: opt < %s -enable-debugify -passes=correlated-propagation -S 2>&1 | \
6 ; RUN: FileCheck %s -check-prefix=DEBUG
7 ; DEBUG: CheckModuleDebugify: PASS
10 define void @test1(i32 %n) {
14 for.cond: ; preds = %for.body, %entry
15 %a = phi i32 [ %n, %entry ], [ %shr, %for.body ]
16 %cmp = icmp sgt i32 %a, 1
17 br i1 %cmp, label %for.body, label %for.end
19 for.body: ; preds = %for.cond
20 ; CHECK: lshr i32 %a, 5
24 for.end: ; preds = %for.cond
28 ;; Negative test to show transform doesn't happen unless n > 0.
30 define void @test2(i32 %n) {
34 for.cond: ; preds = %for.body, %entry
35 %a = phi i32 [ %n, %entry ], [ %shr, %for.body ]
36 %cmp = icmp sgt i32 %a, -2
37 br i1 %cmp, label %for.body, label %for.end
39 for.body: ; preds = %for.cond
40 ; CHECK: ashr i32 %a, 2
44 for.end: ; preds = %for.cond
48 ;; Non looping test case.
50 define void @test3(i32 %n) {
52 %cmp = icmp sgt i32 %n, 0
53 br i1 %cmp, label %bb, label %exit
56 ; CHECK: lshr exact i32 %n, 4
57 %shr = ashr exact i32 %n, 4
64 ; looping case where loop has exactly one block
65 ; at the point of ashr, we know that the operand is always greater than 0,
66 ; because of the guard before it, so we can transform it to lshr.
67 declare void @llvm.experimental.guard(i1,...)
69 define void @test4(i32 %n) {
71 %cmp = icmp sgt i32 %n, 0
72 br i1 %cmp, label %loop, label %exit
75 ; CHECK: lshr i32 %a, 1
76 %a = phi i32 [ %n, %entry ], [ %shr, %loop ]
77 %cond = icmp sgt i32 %a, 2
78 call void(i1,...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
80 br i1 %cond, label %loop, label %exit
86 ; same test as above with assume instead of guard.
87 declare void @llvm.assume(i1)
89 define void @test5(i32 %n) {
91 %cmp = icmp sgt i32 %n, 0
92 br i1 %cmp, label %loop, label %exit
95 ; CHECK: lshr i32 %a, 1
96 %a = phi i32 [ %n, %entry ], [ %shr, %loop ]
97 %cond = icmp sgt i32 %a, 4
98 call void @llvm.assume(i1 %cond)
100 %loopcond = icmp sgt i32 %shr, 8
101 br i1 %loopcond, label %loop, label %exit
107 ; check that ashr of -1 or 0 is optimized away
108 ; CHECK-LABEL: @test6
109 define i32 @test6(i32 %f, i32 %g) {
112 %1 = icmp ult i32 %0, 2
113 tail call void @llvm.assume(i1 %1)
115 %shr = ashr i32 %f, %g
119 ; same test as above with different numbers
120 ; CHECK-LABEL: @test7
121 define i32 @test7(i32 %f, i32 %g) {
124 %1 = icmp eq i32 %0, 6
125 tail call void @llvm.assume(i1 %1)
126 %sub = add nsw i32 %f, -7
127 ; CHECK: ret i32 %sub
128 %shr = ashr i32 %sub, %g
132 ; check that ashr of -2 or 1 is not optimized away
133 ; CHECK-LABEL: @test8
134 define i32 @test8(i32 %f, i32 %g, i1 %s) {
136 ; CHECK: ashr i32 -2, %f
138 ; CHECK: lshr i32 1, %g
140 %2 = select i1 %s, i32 %0, i32 %1
144 define i32 @may_including_undef(i1 %c.1, i1 %c.2) {
145 ; CHECK-LABEL: define i32 @may_including_undef
146 ; CHECK-SAME: (i1 [[C_1:%.*]], i1 [[C_2:%.*]]) {
147 ; CHECK-NEXT: br i1 [[C_1]], label [[TRUE_1:%.*]], label [[FALSE:%.*]]
149 ; CHECK-NEXT: br i1 [[C_2]], label [[TRUE_2:%.*]], label [[EXIT:%.*]]
151 ; CHECK-NEXT: br label [[EXIT]]
153 ; CHECK-NEXT: br label [[EXIT]]
155 ; CHECK-NEXT: [[P:%.*]] = phi i32 [ 2, [[TRUE_1]] ], [ 4, [[TRUE_2]] ], [ undef, [[FALSE]] ]
156 ; CHECK-NEXT: [[R:%.*]] = ashr i32 [[P]], 1
157 ; CHECK-NEXT: ret i32 [[R]]
159 br i1 %c.1, label %true.1, label %false
162 br i1 %c.2, label %true.2, label %exit
171 %p = phi i32 [ 2, %true.1 ], [ 4, %true.2], [ undef, %false ]