xref: /llvm-project/llvm/test/CodeGen/BPF/loop-exit-cond.ll (revision 29441e4f5fa5f5c7709f7cf180815ba97f611297)
1f6f76920SPhilip Reames; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2f6f76920SPhilip Reames; RUN: opt -O2 -S < %s | FileCheck %s
3e52946b9SYonghong Song;
4e52946b9SYonghong Song; Source code:
5e52946b9SYonghong Song;   typedef unsigned long u64;
6e52946b9SYonghong Song;   void foo(char *data, int idx, u64 *);
7e52946b9SYonghong Song;   int test(int len, char *data) {
8e52946b9SYonghong Song;     if (len < 100) {
9e52946b9SYonghong Song;       for (int i = 1; i < len; i++) {
10e52946b9SYonghong Song;         u64 d[1];
11e52946b9SYonghong Song;         d[0] = data[0] ?: '0';
12e52946b9SYonghong Song;         foo("%c", i, d);
13e52946b9SYonghong Song;       }
14e52946b9SYonghong Song;     }
15e52946b9SYonghong Song;   return 0;
16e52946b9SYonghong Song; }
17e52946b9SYonghong Song; Compilation flag:
18e52946b9SYonghong Song;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes test.c
19e52946b9SYonghong Song
20e52946b9SYonghong Song; ModuleID = 'test.c'
21e52946b9SYonghong Songsource_filename = "test.c"
22e52946b9SYonghong Songtarget datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
23e52946b9SYonghong Songtarget triple = "bpf"
24e52946b9SYonghong Song
25e52946b9SYonghong Song@.str = private unnamed_addr constant [3 x i8] c"%c\00", align 1
26e52946b9SYonghong Song
27e52946b9SYonghong Song; Function Attrs: nounwind
2860228733SNikita Popovdefine dso_local i32 @test(i32 %len, ptr %data) #0 {
291228becfSYingwei Zheng; CHECK-LABEL: define dso_local noundef i32 @test(
30*29441e4fSNikita Popov; CHECK-SAME: i32 [[LEN:%.*]], ptr readonly captures(none) [[DATA:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
31f6f76920SPhilip Reames; CHECK-NEXT:  entry:
32f6f76920SPhilip Reames; CHECK-NEXT:    [[D:%.*]] = alloca [1 x i64], align 8
33f6f76920SPhilip Reames; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[LEN]], -2
34f6f76920SPhilip Reames; CHECK-NEXT:    [[OR_COND:%.*]] = icmp ult i32 [[TMP0]], 98
35f6f76920SPhilip Reames; CHECK-NEXT:    br i1 [[OR_COND]], label [[FOR_BODY:%.*]], label [[IF_END:%.*]]
36f6f76920SPhilip Reames; CHECK:       for.body:
37f6f76920SPhilip Reames; CHECK-NEXT:    [[I_05:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 1, [[ENTRY:%.*]] ]
38f6f76920SPhilip Reames; CHECK-NEXT:    call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[D]]) #[[ATTR3:[0-9]+]]
39f6f76920SPhilip Reames; CHECK-NEXT:    [[TMP1:%.*]] = load i8, ptr [[DATA]], align 1, !tbaa [[TBAA3:![0-9]+]]
40f6f76920SPhilip Reames; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP1]], 0
41f6f76920SPhilip Reames; CHECK-NEXT:    [[NARROW:%.*]] = select i1 [[TOBOOL_NOT]], i8 48, i8 [[TMP1]]
42f6f76920SPhilip Reames; CHECK-NEXT:    [[CONV2:%.*]] = sext i8 [[NARROW]] to i64
43f6f76920SPhilip Reames; CHECK-NEXT:    store i64 [[CONV2]], ptr [[D]], align 8, !tbaa [[TBAA6:![0-9]+]]
44f6f76920SPhilip Reames; CHECK-NEXT:    call void @foo(ptr nonnull @.str, i32 [[I_05]], ptr nonnull [[D]]) #[[ATTR3]]
45f6f76920SPhilip Reames; CHECK-NEXT:    call void @llvm.lifetime.end.p0(i64 8, ptr nonnull [[D]]) #[[ATTR3]]
46f6f76920SPhilip Reames; CHECK-NEXT:    [[INC]] = add nuw nsw i32 [[I_05]], 1
47a7f35d54SPhilip Reames; CHECK-NEXT:    [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[LEN]]
48a7f35d54SPhilip Reames; CHECK-NEXT:    br i1 [[EXITCOND_NOT]], label [[IF_END]], label [[FOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
49f6f76920SPhilip Reames; CHECK:       if.end:
50f6f76920SPhilip Reames; CHECK-NEXT:    ret i32 0
51f6f76920SPhilip Reames;
52e52946b9SYonghong Songentry:
53e52946b9SYonghong Song  %len.addr = alloca i32, align 4
5460228733SNikita Popov  %data.addr = alloca ptr, align 8
55e52946b9SYonghong Song  %i = alloca i32, align 4
56e52946b9SYonghong Song  %d = alloca [1 x i64], align 8
5760228733SNikita Popov  store i32 %len, ptr %len.addr, align 4, !tbaa !3
5860228733SNikita Popov  store ptr %data, ptr %data.addr, align 8, !tbaa !7
5960228733SNikita Popov  %0 = load i32, ptr %len.addr, align 4, !tbaa !3
60e52946b9SYonghong Song  %cmp = icmp slt i32 %0, 100
61e52946b9SYonghong Song  br i1 %cmp, label %if.then, label %if.end
62e52946b9SYonghong Song
63e52946b9SYonghong Songif.then:                                          ; preds = %entry
6460228733SNikita Popov  call void @llvm.lifetime.start.p0(i64 4, ptr %i) #3
6560228733SNikita Popov  store i32 1, ptr %i, align 4, !tbaa !3
66e52946b9SYonghong Song  br label %for.cond
67e52946b9SYonghong Song
68e52946b9SYonghong Songfor.cond:                                         ; preds = %for.inc, %if.then
6960228733SNikita Popov  %1 = load i32, ptr %i, align 4, !tbaa !3
7060228733SNikita Popov  %2 = load i32, ptr %len.addr, align 4, !tbaa !3
7160228733SNikita Popov  %cmp1 = icmp slt i32 %1, %2
72e52946b9SYonghong Song  br i1 %cmp1, label %for.body, label %for.cond.cleanup
73e52946b9SYonghong Song
74e52946b9SYonghong Song
75e52946b9SYonghong Songfor.cond.cleanup:                                 ; preds = %for.cond
7660228733SNikita Popov  call void @llvm.lifetime.end.p0(i64 4, ptr %i) #3
77e52946b9SYonghong Song  br label %for.end
78e52946b9SYonghong Song
79e52946b9SYonghong Songfor.body:                                         ; preds = %for.cond
8060228733SNikita Popov  call void @llvm.lifetime.start.p0(i64 8, ptr %d) #3
8160228733SNikita Popov  %3 = load ptr, ptr %data.addr, align 8, !tbaa !7
8260228733SNikita Popov  %4 = load i8, ptr %3, align 1, !tbaa !9
8360228733SNikita Popov  %conv = sext i8 %4 to i32
84e52946b9SYonghong Song  %tobool = icmp ne i32 %conv, 0
85e52946b9SYonghong Song  br i1 %tobool, label %cond.true, label %cond.false
86e52946b9SYonghong Song
87e52946b9SYonghong Songcond.true:                                        ; preds = %for.body
88e52946b9SYonghong Song  br label %cond.end
89e52946b9SYonghong Song
90e52946b9SYonghong Songcond.false:                                       ; preds = %for.body
91e52946b9SYonghong Song  br label %cond.end
92e52946b9SYonghong Song
93e52946b9SYonghong Songcond.end:                                         ; preds = %cond.false, %cond.true
94e52946b9SYonghong Song  %cond = phi i32 [ %conv, %cond.true ], [ 48, %cond.false ]
95e52946b9SYonghong Song  %conv2 = sext i32 %cond to i64
9660228733SNikita Popov  store i64 %conv2, ptr %d, align 8, !tbaa !10
9760228733SNikita Popov  %5 = load i32, ptr %i, align 4, !tbaa !3
9860228733SNikita Popov  call void @foo(ptr @.str, i32 %5, ptr %d)
9960228733SNikita Popov  call void @llvm.lifetime.end.p0(i64 8, ptr %d) #3
100e52946b9SYonghong Song  br label %for.inc
101e52946b9SYonghong Song
102e52946b9SYonghong Songfor.inc:                                          ; preds = %cond.end
10360228733SNikita Popov  %6 = load i32, ptr %i, align 4, !tbaa !3
10460228733SNikita Popov  %inc = add nsw i32 %6, 1
10560228733SNikita Popov  store i32 %inc, ptr %i, align 4, !tbaa !3
106e52946b9SYonghong Song  br label %for.cond, !llvm.loop !12
107e52946b9SYonghong Song
108e52946b9SYonghong Songfor.end:                                          ; preds = %for.cond.cleanup
109e52946b9SYonghong Song  br label %if.end
110e52946b9SYonghong Song
111e52946b9SYonghong Songif.end:                                           ; preds = %for.end, %entry
112e52946b9SYonghong Song  ret i32 0
113e52946b9SYonghong Song}
114e52946b9SYonghong Song
115e52946b9SYonghong Song; Function Attrs: argmemonly nofree nosync nounwind willreturn
11660228733SNikita Popovdeclare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
117e52946b9SYonghong Song
11860228733SNikita Popovdeclare dso_local void @foo(ptr, i32, ptr) #2
119e52946b9SYonghong Song
120e52946b9SYonghong Song; Function Attrs: argmemonly nofree nosync nounwind willreturn
12160228733SNikita Popovdeclare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
122e52946b9SYonghong Song
123e52946b9SYonghong Songattributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
124e52946b9SYonghong Songattributes #1 = { argmemonly nofree nosync nounwind willreturn }
125e52946b9SYonghong Songattributes #2 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
126e52946b9SYonghong Songattributes #3 = { nounwind }
127e52946b9SYonghong Song
128e52946b9SYonghong Song!llvm.module.flags = !{!0, !1}
129e52946b9SYonghong Song!llvm.ident = !{!2}
130e52946b9SYonghong Song
131e52946b9SYonghong Song!0 = !{i32 1, !"wchar_size", i32 4}
132e52946b9SYonghong Song!1 = !{i32 7, !"frame-pointer", i32 2}
133e52946b9SYonghong Song!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 8385de118443144518c9fba8b3d831d9076e746b)"}
134e52946b9SYonghong Song!3 = !{!4, !4, i64 0}
135e52946b9SYonghong Song!4 = !{!"int", !5, i64 0}
136e52946b9SYonghong Song!5 = !{!"omnipotent char", !6, i64 0}
137e52946b9SYonghong Song!6 = !{!"Simple C/C++ TBAA"}
138e52946b9SYonghong Song!7 = !{!8, !8, i64 0}
139e52946b9SYonghong Song!8 = !{!"any pointer", !5, i64 0}
140e52946b9SYonghong Song!9 = !{!5, !5, i64 0}
141e52946b9SYonghong Song!10 = !{!11, !11, i64 0}
142e52946b9SYonghong Song!11 = !{!"long", !5, i64 0}
143e52946b9SYonghong Song!12 = distinct !{!12, !13}
144e52946b9SYonghong Song!13 = !{!"llvm.loop.mustprogress"}
145