Skip to content

Commit 6474d7f

Browse files
committed
[Test] Add test showing missed branch elimination due to loop predication transform
1 parent 040805d commit 6474d7f

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -passes='loop-mssa(loop-predication),gvn,simplifycfg' < %s 2>&1 | FileCheck %s
3+
4+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
define i32 @test0(i32* %p1, i8* %p2, i32* %p3, i8* %p4, i8* %p5, i1 %c, i32 %x) {
8+
; CHECK-LABEL: @test0(
9+
; CHECK-NEXT: entry:
10+
; CHECK-NEXT: [[P1_1:%.*]] = getelementptr i32, i32* [[P1:%.*]], i64 1
11+
; CHECK-NEXT: [[P1_2:%.*]] = getelementptr i32, i32* [[P1]], i64 2
12+
; CHECK-NEXT: [[P1_3:%.*]] = getelementptr i32, i32* [[P1]], i64 3
13+
; CHECK-NEXT: [[IV_1_START:%.*]] = load i32, i32* [[P1_1]], align 4, !range [[RNG0:![0-9]+]]
14+
; CHECK-NEXT: [[IV_1_END:%.*]] = load i32, i32* [[P1_2]], align 4, !range [[RNG0]]
15+
; CHECK-NEXT: [[IV_2_END:%.*]] = load i32, i32* [[P1_3]], align 4, !range [[RNG0]]
16+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_2_END]], [[IV_1_END]]
17+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP_PREHEADER:%.*]], label [[EXIT:%.*]]
18+
; CHECK: loop.preheader:
19+
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[IV_1_END]], -1
20+
; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[TMP0]], [[IV_1_START]]
21+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ule i32 [[IV_2_END]], [[TMP1]]
22+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i32 [[IV_1_START]], [[IV_1_END]]
23+
; CHECK-NEXT: [[TMP4:%.*]] = and i1 [[TMP3]], [[TMP2]]
24+
; CHECK-NEXT: br label [[LOOP:%.*]]
25+
; CHECK: loop:
26+
; CHECK-NEXT: [[IV_1:%.*]] = phi i32 [ [[IV_1_NEXT:%.*]], [[LATCH:%.*]] ], [ [[IV_1_START]], [[LOOP_PREHEADER]] ]
27+
; CHECK-NEXT: [[IV_2:%.*]] = phi i32 [ [[IV_2_NEXT:%.*]], [[LATCH]] ], [ 0, [[LOOP_PREHEADER]] ]
28+
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, i8* [[P2:%.*]], i32 [[IV_1]]
29+
; CHECK-NEXT: [[VALUE:%.*]] = load i8, i8* [[GEP_1]], align 1
30+
; CHECK-NEXT: [[COND_1:%.*]] = icmp ult i32 [[IV_1]], [[IV_1_END]]
31+
; CHECK-NEXT: [[WC:%.*]] = call i1 @llvm.experimental.widenable.condition()
32+
; CHECK-NEXT: [[TMP5:%.*]] = and i1 [[TMP4]], [[WC]]
33+
; CHECK-NEXT: br i1 [[TMP5]], label [[LOOP_NEXT:%.*]], label [[DEOPT:%.*]]
34+
; CHECK: loop.next:
35+
; CHECK-NEXT: br i1 [[COND_1]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
36+
; CHECK: if.true:
37+
; CHECK-NEXT: [[GEP_3:%.*]] = getelementptr i8, i8* [[P4:%.*]], i32 [[IV_1]]
38+
; CHECK-NEXT: store i8 [[VALUE]], i8* [[GEP_3]], align 1
39+
; CHECK-NEXT: br label [[LATCH]]
40+
; CHECK: if.false:
41+
; CHECK-NEXT: [[GEP_4:%.*]] = getelementptr i8, i8* [[P4]], i32 [[IV_2]]
42+
; CHECK-NEXT: store i8 [[VALUE]], i8* [[GEP_4]], align 1
43+
; CHECK-NEXT: br label [[LATCH]]
44+
; CHECK: latch:
45+
; CHECK-NEXT: [[IV_1_NEXT]] = add nuw nsw i32 [[IV_1]], 1
46+
; CHECK-NEXT: [[IV_2_NEXT]] = add nuw nsw i32 [[IV_2]], 1
47+
; CHECK-NEXT: [[LATCH_COND:%.*]] = icmp ult i32 [[IV_2]], [[IV_2_END]]
48+
; CHECK-NEXT: br i1 [[LATCH_COND]], label [[LOOP]], label [[EXIT]]
49+
; CHECK: deopt:
50+
; CHECK-NEXT: [[DEOPT_RES:%.*]] = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"() ]
51+
; CHECK-NEXT: ret i32 [[DEOPT_RES]]
52+
; CHECK: exit:
53+
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_2]], [[LATCH]] ]
54+
; CHECK-NEXT: ret i32 [[RES]]
55+
;
56+
entry:
57+
%p1.1 = getelementptr i32, i32* %p1, i64 1
58+
%p1.2 = getelementptr i32, i32* %p1, i64 2
59+
%p1.3 = getelementptr i32, i32* %p1, i64 3
60+
%iv.1.start = load i32, i32* %p1.1, !range !0
61+
%iv.1.end = load i32, i32* %p1.2, !range !0
62+
%iv.2.end = load i32, i32* %p1.3, !range !0
63+
%loop.cond = icmp ult i32 %iv.2.end, %iv.1.end
64+
br i1 %loop.cond, label %loop, label %exit
65+
66+
loop:
67+
%iv.1 = phi i32 [ %iv.1.start, %entry ], [ %iv.1.next, %latch ]
68+
%iv.2 = phi i32 [ 0, %entry ], [ %iv.2.next, %latch ]
69+
%gep.1 = getelementptr i8, i8* %p2, i32 %iv.1
70+
%value = load i8, i8* %gep.1
71+
%cond.1 = icmp ult i32 %iv.1, %iv.1.end
72+
%wc = call i1 @llvm.experimental.widenable.condition()
73+
%explicit_guard_cond = and i1 %cond.1, %wc
74+
br i1 %explicit_guard_cond, label %loop.next, label %deopt
75+
76+
loop.next:
77+
br i1 %cond.1, label %if.true, label %if.false
78+
79+
if.true:
80+
%gep.3 = getelementptr i8, i8* %p4, i32 %iv.1
81+
store i8 %value, i8* %gep.3
82+
br label %latch
83+
84+
if.false:
85+
%gep.4 = getelementptr i8, i8* %p4, i32 %iv.2
86+
store i8 %value, i8* %gep.4
87+
br label %latch
88+
89+
latch:
90+
%iv.1.next = add nuw nsw i32 %iv.1, 1
91+
%iv.2.next = add nuw nsw i32 %iv.2, 1
92+
%latch.cond = icmp ult i32 %iv.2, %iv.2.end
93+
br i1 %latch.cond, label %loop, label %exit
94+
95+
deopt:
96+
%deopt_res = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"() ]
97+
ret i32 %deopt_res
98+
99+
exit:
100+
%res = phi i32 [ 0, %entry ], [ %iv.2, %latch ]
101+
ret i32 %res
102+
}
103+
104+
; Function Attrs: inaccessiblememonly nocallback nofree nosync nounwind willreturn
105+
declare void @llvm.assume(i1) #0
106+
107+
; Function Attrs: inaccessiblememonly nocallback nofree nosync nounwind speculatable willreturn
108+
declare i1 @llvm.experimental.widenable.condition() #1
109+
110+
declare i32 @llvm.experimental.deoptimize.i32(...)
111+
112+
attributes #0 = { inaccessiblememonly nocallback nofree nosync nounwind willreturn }
113+
attributes #1 = { inaccessiblememonly nocallback nofree nosync nounwind speculatable willreturn }
114+
115+
!0 = !{i32 0, i32 2147483646}

0 commit comments

Comments
 (0)