1; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s 2; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml 3; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s 4; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml 5 6; Original test case. 7; 8; 1 #include <stdlib.h> 9; 2 10; 3 long long foo() { 11; 4 long long int sum = 0; 12; 5 for (int i = 0; i < 500000000; i++) 13; 6 if (i < 1000) 14; 7 sum -= i; 15; 8 else 16; 9 sum += -i * rand(); 17; 10 return sum; 18; 11 } 19; 12 20; 13 int main() { return foo() > 0; } 21 22; We are expecting foo() to be inlined in main() (almost all the cycles are 23; spent inside foo). 24; CHECK: remark: remarks.cc:13:21: _Z3foov inlined into main to match profiling context with (cost=130, threshold=2147483647) at callsite main:0:21; 25; CHECK: remark: remarks.cc:9:19: rand inlined into main to match profiling context with (cost=always): always inline attribute at callsite _Z3foov:6:19 @ main:0:21; 26 27; The back edge for the loop is the hottest edge in the loop subgraph. 28; CHECK: remark: remarks.cc:6:9: most popular destination for conditional branches at remarks.cc:5:3 29 30; The predicate almost always chooses the 'else' branch. 31; CHECK: remark: remarks.cc:9:15: most popular destination for conditional branches at remarks.cc:6:9 32 33; Checking to see if YAML file is generated and contains remarks 34;YAML: --- !Passed 35;YAML-NEXT: Pass: sample-profile-inline 36;YAML-NEXT: Name: Inlined 37;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 21 } 38;YAML-NEXT: Function: main 39;YAML-NEXT: Args: 40;YAML-NEXT: - Callee: _Z3foov 41;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 3, Column: 0 } 42;YAML-NEXT: - String: ' inlined into ' 43;YAML-NEXT: - Caller: main 44;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 0 } 45;YAML-NEXT: - String: ' to match profiling context' 46;YAML-NEXT: - String: ' with ' 47;YAML-NEXT: - String: '(cost=' 48;YAML-NEXT: - Cost: '130' 49;YAML-NEXT: - String: ', threshold=' 50;YAML-NEXT: - Threshold: '2147483647' 51;YAML-NEXT: - String: ')' 52;YAML-NEXT: - String: ' at callsite ' 53;YAML-NEXT: - String: main 54;YAML-NEXT: - String: ':' 55;YAML-NEXT: - Line: '0' 56;YAML-NEXT: - String: ':' 57;YAML-NEXT: - Column: '21' 58;YAML-NEXT: - String: ';' 59;YAML-NEXT: ... 60;YAML: --- !Passed 61;YAML-NEXT: Pass: sample-profile-inline 62;YAML-NEXT: Name: AlwaysInline 63;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 9, Column: 19 } 64;YAML-NEXT: Function: main 65;YAML-NEXT: Args: 66;YAML-NEXT: - Callee: rand 67;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 90, Column: 0 } 68;YAML-NEXT: - String: ' inlined into ' 69;YAML-NEXT: - Caller: main 70;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 0 } 71;YAML-NEXT: - String: ' to match profiling context' 72;YAML-NEXT: - String: ' with ' 73;YAML-NEXT: - String: '(cost=always)' 74;YAML-NEXT: - String: ': ' 75;YAML-NEXT: - Reason: always inline attribute 76;YAML-NEXT: - String: ' at callsite ' 77;YAML-NEXT: - String: _Z3foov 78;YAML-NEXT: - String: ':' 79;YAML-NEXT: - Line: '6' 80;YAML-NEXT: - String: ':' 81;YAML-NEXT: - Column: '19' 82;YAML-NEXT: - String: ' @ ' 83;YAML-NEXT: - String: main 84;YAML-NEXT: - String: ':' 85;YAML-NEXT: - Line: '0' 86;YAML-NEXT: - String: ':' 87;YAML-NEXT: - Column: '21' 88;YAML-NEXT: - String: ';' 89;YAML: --- !Analysis 90;YAML-NEXT: Pass: sample-profile 91;YAML-NEXT: Name: AppliedSamples 92;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 5, Column: 8 } 93;YAML-NEXT: Function: main 94;YAML-NEXT: Args: 95;YAML-NEXT: - String: 'Applied ' 96;YAML-NEXT: - NumSamples: '18305' 97;YAML-NEXT: - String: ' samples from profile (offset: ' 98;YAML-NEXT: - LineOffset: '2' 99;YAML-NEXT: - String: ')' 100;YAML-NEXT: ... 101;YAML: --- !Passed 102;YAML-NEXT: Pass: sample-profile 103;YAML-NEXT: Name: PopularDest 104;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 6, Column: 9 } 105;YAML-NEXT: Function: main 106;YAML-NEXT: Args: 107;YAML-NEXT: - String: 'most popular destination for conditional branches at ' 108;YAML-NEXT: - CondBranchesLoc: 'remarks.cc:5:3' 109;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 5, Column: 3 } 110;YAML-NEXT: ... 111 112; Function Attrs: nounwind uwtable 113define i64 @_Z3foov() #0 !dbg !4 { 114entry: 115 %sum = alloca i64, align 8 116 %i = alloca i32, align 4 117 %0 = bitcast i64* %sum to i8*, !dbg !19 118 call void @llvm.lifetime.start.p0i8(i64 8, i8* %0) #4, !dbg !19 119 call void @llvm.dbg.declare(metadata i64* %sum, metadata !9, metadata !20), !dbg !21 120 store i64 0, i64* %sum, align 8, !dbg !21, !tbaa !22 121 %1 = bitcast i32* %i to i8*, !dbg !26 122 call void @llvm.lifetime.start.p0i8(i64 4, i8* %1) #4, !dbg !26 123 call void @llvm.dbg.declare(metadata i32* %i, metadata !10, metadata !20), !dbg !27 124 store i32 0, i32* %i, align 4, !dbg !27, !tbaa !28 125 br label %for.cond, !dbg !26 126 127for.cond: ; preds = %for.inc, %entry 128 %2 = load i32, i32* %i, align 4, !dbg !30, !tbaa !28 129 %cmp = icmp slt i32 %2, 500000000, !dbg !34 130 br i1 %cmp, label %for.body, label %for.cond.cleanup, !dbg !35 131 132for.cond.cleanup: ; preds = %for.cond 133 %3 = bitcast i32* %i to i8*, !dbg !36 134 call void @llvm.lifetime.end.p0i8(i64 4, i8* %3) #4, !dbg !36 135 br label %for.end 136 137for.body: ; preds = %for.cond 138 %4 = load i32, i32* %i, align 4, !dbg !38, !tbaa !28 139 %cmp1 = icmp slt i32 %4, 1000, !dbg !40 140 br i1 %cmp1, label %if.then, label %if.else, !dbg !41 141 142if.then: ; preds = %for.body 143 %5 = load i32, i32* %i, align 4, !dbg !42, !tbaa !28 144 %conv = sext i32 %5 to i64, !dbg !42 145 %6 = load i64, i64* %sum, align 8, !dbg !43, !tbaa !22 146 %sub = sub nsw i64 %6, %conv, !dbg !43 147 store i64 %sub, i64* %sum, align 8, !dbg !43, !tbaa !22 148 br label %if.end, !dbg !44 149 150if.else: ; preds = %for.body 151 %7 = load i32, i32* %i, align 4, !dbg !45, !tbaa !28 152 %sub2 = sub nsw i32 0, %7, !dbg !46 153 %call = call i32 @rand() #4, !dbg !47 154 %mul = mul nsw i32 %sub2, %call, !dbg !48 155 %conv3 = sext i32 %mul to i64, !dbg !46 156 %8 = load i64, i64* %sum, align 8, !dbg !49, !tbaa !22 157 %add = add nsw i64 %8, %conv3, !dbg !49 158 store i64 %add, i64* %sum, align 8, !dbg !49, !tbaa !22 159 br label %if.end 160 161if.end: ; preds = %if.else, %if.then 162 br label %for.inc, !dbg !50 163 164for.inc: ; preds = %if.end 165 %9 = load i32, i32* %i, align 4, !dbg !51, !tbaa !28 166 %inc = add nsw i32 %9, 1, !dbg !51 167 store i32 %inc, i32* %i, align 4, !dbg !51, !tbaa !28 168 br label %for.cond, !dbg !52 169 170for.end: ; preds = %for.cond.cleanup 171 %10 = load i64, i64* %sum, align 8, !dbg !53, !tbaa !22 172 %11 = bitcast i64* %sum to i8*, !dbg !54 173 call void @llvm.lifetime.end.p0i8(i64 8, i8* %11) #4, !dbg !54 174 ret i64 %10, !dbg !55 175} 176 177; Function Attrs: nounwind argmemonly 178declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1 179 180; Function Attrs: nounwind readnone 181declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 182 183; Function Attrs: nounwind 184define i32 @rand() #3 !dbg !59 { 185 ret i32 1 186} 187 188; Function Attrs: nounwind argmemonly 189declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1 190 191; Function Attrs: nounwind uwtable 192define i32 @main() #0 !dbg !13 { 193entry: 194 %retval = alloca i32, align 4 195 store i32 0, i32* %retval, align 4 196 %call = call i64 @_Z3foov(), !dbg !56 197 %cmp = icmp sgt i64 %call, 0, !dbg !57 198 %conv = zext i1 %cmp to i32, !dbg !56 199 ret i32 %conv, !dbg !58 200} 201 202attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" "use-sample-profile" } 203attributes #1 = { nounwind argmemonly } 204attributes #2 = { nounwind readnone } 205attributes #3 = { nounwind alwaysinline "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 206attributes #4 = { nounwind } 207 208!llvm.dbg.cu = !{!0} 209!llvm.module.flags = !{!16, !17} 210!llvm.ident = !{!18} 211 212!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 251041) (llvm/trunk 251053)", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2) 213!1 = !DIFile(filename: "remarks.cc", directory: ".") 214!2 = !{} 215!4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8) 216!5 = !DISubroutineType(types: !6) 217!6 = !{!7} 218!7 = !DIBasicType(name: "long long int", size: 64, align: 64, encoding: DW_ATE_signed) 219!8 = !{!9, !10} 220!9 = !DILocalVariable(name: "sum", scope: !4, file: !1, line: 4, type: !7) 221!10 = !DILocalVariable(name: "i", scope: !11, file: !1, line: 5, type: !12) 222!11 = distinct !DILexicalBlock(scope: !4, file: !1, line: 5, column: 3) 223!12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) 224!13 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 13, type: !14, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2) 225!14 = !DISubroutineType(types: !15) 226!15 = !{!12} 227!16 = !{i32 2, !"Dwarf Version", i32 4} 228!17 = !{i32 2, !"Debug Info Version", i32 3} 229!18 = !{!"clang version 3.8.0 (trunk 251041) (llvm/trunk 251053)"} 230!19 = !DILocation(line: 4, column: 3, scope: !4) 231!20 = !DIExpression() 232!21 = !DILocation(line: 4, column: 17, scope: !4) 233!22 = !{!23, !23, i64 0} 234!23 = !{!"long long", !24, i64 0} 235!24 = !{!"omnipotent char", !25, i64 0} 236!25 = !{!"Simple C/C++ TBAA"} 237!26 = !DILocation(line: 5, column: 8, scope: !11) 238!27 = !DILocation(line: 5, column: 12, scope: !11) 239!28 = !{!29, !29, i64 0} 240!29 = !{!"int", !24, i64 0} 241!30 = !DILocation(line: 5, column: 19, scope: !31) 242!31 = !DILexicalBlockFile(scope: !32, file: !1, discriminator: 3) 243!32 = !DILexicalBlockFile(scope: !33, file: !1, discriminator: 1) 244!33 = distinct !DILexicalBlock(scope: !11, file: !1, line: 5, column: 3) 245!34 = !DILocation(line: 5, column: 21, scope: !33) 246!35 = !DILocation(line: 5, column: 3, scope: !11) 247!36 = !DILocation(line: 5, column: 3, scope: !37) 248!37 = !DILexicalBlockFile(scope: !33, file: !1, discriminator: 2) 249!38 = !DILocation(line: 6, column: 9, scope: !39) 250!39 = distinct !DILexicalBlock(scope: !33, file: !1, line: 6, column: 9) 251!40 = !DILocation(line: 6, column: 11, scope: !39) 252!41 = !DILocation(line: 6, column: 9, scope: !33) 253!42 = !DILocation(line: 7, column: 14, scope: !39) 254!43 = !DILocation(line: 7, column: 11, scope: !39) 255!44 = !DILocation(line: 7, column: 7, scope: !39) 256!45 = !DILocation(line: 9, column: 15, scope: !39) 257!46 = !DILocation(line: 9, column: 14, scope: !39) 258!47 = !DILocation(line: 9, column: 19, scope: !39) 259!48 = !DILocation(line: 9, column: 17, scope: !39) 260!49 = !DILocation(line: 9, column: 11, scope: !39) 261!50 = !DILocation(line: 6, column: 13, scope: !39) 262!51 = !DILocation(line: 5, column: 35, scope: !33) 263!52 = !DILocation(line: 5, column: 3, scope: !33) 264!53 = !DILocation(line: 10, column: 10, scope: !4) 265!54 = !DILocation(line: 11, column: 1, scope: !4) 266!55 = !DILocation(line: 10, column: 3, scope: !4) 267!56 = !DILocation(line: 13, column: 21, scope: !13) 268!57 = !DILocation(line: 13, column: 27, scope: !13) 269!58 = !DILocation(line: 13, column: 14, scope: !13) 270!59 = distinct !DISubprogram(name: "rand", linkageName: "rand", scope: !1, file: !1, line: 90, type: !5, isLocal: false, isDefinition: true, scopeLine: 90, flags: DIFlagPrototyped, isOptimized: true, unit: !0) 271