xref: /llvm-project/llvm/test/Transforms/PGOProfile/misexpect-branch-overflow.ll (revision 9ff36df5a4a7d52c51e950522870bb64912688d2)
1; Ensure that profile counters that overflow are handled correctly.
2; This can happen when the sum of all counters exceeds the max size of uint32_t
3
4; RUN: llvm-profdata merge %S/Inputs/misexpect-branch-overflow.proftext -o %t.profdata
5; RUN: opt < %s -passes="function(lower-expect),pgo-instr-use" -pgo-test-profile-file=%t.profdata -pgo-warn-misexpect -pass-remarks=misexpect -unlikely-branch-weight=2147483648 -likely-branch-weight=2147483648 -S 2>&1 | FileCheck %s --check-prefix=OVERFLOW
6
7; OVERFLOW-NOT: warning: misexpect-branch.c:22:0: 50.00%
8; OVERFLOW-NOT: remark: misexpect-branch.c:22:0: Potential performance regression from use of the llvm.expect intrinsic: Annotation was correct on 50.00% (2147483648 / 4294967296) of profiled executions.
9
10; ModuleID = 'misexpect-branch.c'
11source_filename = "misexpect-branch.c"
12target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
13target triple = "x86_64-unknown-linux-gnu"
14
15@inner_loop = constant i32 100, align 4
16@outer_loop = constant i32 2000, align 4
17
18; Function Attrs: nounwind
19define i32 @bar() #0 !dbg !6 {
20entry:
21  %rando = alloca i32, align 4
22  %x = alloca i32, align 4
23  call void @llvm.lifetime.start.p0(i64 4, ptr %rando) #4, !dbg !9
24  %call = call i32 (...) @buzz(), !dbg !9
25  store i32 %call, ptr %rando, align 4, !dbg !9, !tbaa !10
26  call void @llvm.lifetime.start.p0(i64 4, ptr %x) #4, !dbg !14
27  store i32 0, ptr %x, align 4, !dbg !14, !tbaa !10
28  %0 = load i32, ptr %rando, align 4, !dbg !15, !tbaa !10
29  %rem = srem i32 %0, 200000, !dbg !15
30  %cmp = icmp eq i32 %rem, 0, !dbg !15
31  %lnot = xor i1 %cmp, true, !dbg !15
32  %lnot1 = xor i1 %lnot, true, !dbg !15
33  %lnot.ext = zext i1 %lnot1 to i32, !dbg !15
34  %conv = sext i32 %lnot.ext to i64, !dbg !15
35  %expval = call i64 @llvm.expect.i64(i64 %conv, i64 1), !dbg !15
36  %tobool = icmp ne i64 %expval, 0, !dbg !15
37  br i1 %tobool, label %if.then, label %if.else, !dbg !15
38
39if.then:                                          ; preds = %entry
40  %1 = load i32, ptr %rando, align 4, !dbg !16, !tbaa !10
41  %call2 = call i32 @baz(i32 %1), !dbg !16
42  store i32 %call2, ptr %x, align 4, !dbg !16, !tbaa !10
43  br label %if.end, !dbg !17
44
45if.else:                                          ; preds = %entry
46  %call3 = call i32 @foo(i32 50), !dbg !18
47  store i32 %call3, ptr %x, align 4, !dbg !18, !tbaa !10
48  br label %if.end
49
50if.end:                                           ; preds = %if.else, %if.then
51  %2 = load i32, ptr %x, align 4, !dbg !19, !tbaa !10
52  call void @llvm.lifetime.end.p0(i64 4, ptr %x) #4, !dbg !20
53  call void @llvm.lifetime.end.p0(i64 4, ptr %rando) #4, !dbg !20
54  ret i32 %2, !dbg !19
55}
56
57; Function Attrs: argmemonly nounwind willreturn
58declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
59
60declare i32 @buzz(...) #2
61
62; Function Attrs: nounwind readnone willreturn
63declare i64 @llvm.expect.i64(i64, i64) #3
64
65declare i32 @baz(i32) #2
66
67declare i32 @foo(i32) #2
68
69; Function Attrs: argmemonly nounwind willreturn
70declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
71
72attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
73attributes #1 = { argmemonly nounwind willreturn }
74attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
75attributes #3 = { nounwind readnone willreturn }
76attributes #4 = { nounwind }
77
78!llvm.dbg.cu = !{!0}
79!llvm.module.flags = !{!3, !4}
80!llvm.ident = !{!5}
81
82!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0 (trunk c20270bfffc9d6965219de339d66c61e9fe7d82d)", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2, nameTableKind: None)
83!1 = !DIFile(filename: "<stdin>", directory: ".")
84!2 = !{}
85!3 = !{i32 2, !"Debug Info Version", i32 3}
86!4 = !{i32 1, !"wchar_size", i32 4}
87!5 = !{!"clang version 10.0.0 (trunk c20270bfffc9d6965219de339d66c61e9fe7d82d)"}
88!6 = distinct !DISubprogram(name: "bar", scope: !7, file: !7, line: 19, type: !8, scopeLine: 19, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
89!7 = !DIFile(filename: "misexpect-branch.c", directory: ".")
90!8 = !DISubroutineType(types: !2)
91!9 = !DILocation(line: 20, scope: !6)
92!10 = !{!11, !11, i64 0}
93!11 = !{!"int", !12, i64 0}
94!12 = !{!"omnipotent char", !13, i64 0}
95!13 = !{!"Simple C/C++ TBAA"}
96!14 = !DILocation(line: 21, scope: !6)
97!15 = !DILocation(line: 22, scope: !6)
98!16 = !DILocation(line: 23, scope: !6)
99!17 = !DILocation(line: 24, scope: !6)
100!18 = !DILocation(line: 25, scope: !6)
101!19 = !DILocation(line: 27, scope: !6)
102!20 = !DILocation(line: 28, scope: !6)
103