llvm.org GIT mirror llvm / 7996242
[PGO] Set edge weights for indirectbr instruction with profile counts Current PGO only annotates the edge weight for branch and switch instructions with profile counts. We should also annotate the indirectbr instruction as all the information is there. This patch enables the annotating for indirectbr instructions. Also uses this annotation in branch probability analysis. Differential Revision: https://reviews.llvm.org/D37074 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311604 91177308-0d34-0410-b5e6-96231b3b80d8 Rong Xu 2 years ago
4 changed file(s) with 64 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
236236 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
237237 const TerminatorInst *TI = BB->getTerminator();
238238 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
239 if (!isa(TI) && !isa(TI))
239 if (!(isa(TI) || isa(TI) || isa(TI)))
240240 return false;
241241
242242 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
10931093 TerminatorInst *TI = BB.getTerminator();
10941094 if (TI->getNumSuccessors() < 2)
10951095 continue;
1096 if (!isa(TI) && !isa(TI))
1096 if (!(isa(TI) || isa(TI) ||
1097 isa(TI)))
10971098 continue;
10981099 if (getBBInfo(&BB).CountValue == 0)
10991100 continue;
0 # IR level Instrumentation Flag
1 :ir
2 foo
3 # Func Hash:
4 40197883220
5 # Num Counters:
6 4
7 # Counter Values:
8 202
9 88
10 20
11 5
12
0 ; RUN: llvm-profdata merge %S/Inputs/indirectbr.proftext -o %t.profdata
1 ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
2 ; New PM
3 ; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
4 ; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | opt -S -analyze -branch-prob | FileCheck %s --check-prefix=BRANCHPROB
5
6 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 target triple = "x86_64-unknown-linux-gnu"
8
9 @foo.table = internal unnamed_addr constant [3 x i8*] [i8* blockaddress(@foo, %return), i8* blockaddress(@foo, %label2), i8* blockaddress(@foo, %label3)], align 16
10
11 define i32 @foo(i32 %i) {
12 entry:
13 %cmp = icmp ult i32 %i, 3
14 br i1 %cmp, label %if.then, label %return
15
16 if.then:
17 %idxprom = zext i32 %i to i64
18 %arrayidx = getelementptr inbounds [3 x i8*], [3 x i8*]* @foo.table, i64 0, i64 %idxprom
19 %0 = load i8*, i8** %arrayidx, align 8
20 indirectbr i8* %0, [label %return, label %label2, label %label3]
21 ; USE: indirectbr i8* %0, [label %return, label %label2, label %label3]
22 ; USE-SAME: !prof ![[BW_INDBR:[0-9]+]]
23 ; USE: ![[BW_INDBR]] = !{!"branch_weights", i32 63, i32 20, i32 5}
24
25 label2:
26 br label %return
27
28 label3:
29 br label %return
30
31 return:
32 %retval.0 = phi i32 [ 3, %label3 ], [ 2, %label2 ], [ 0, %entry ], [ 1, %if.then ]
33 ret i32 %retval.0
34 }
35
36 ; BRANCHPROB: Printing analysis 'Branch Probability Analysis' for function 'foo':
37 ; BRANCHPROB:---- Branch Probabilities ----
38 ; BRANCHPROB: edge entry -> if.then probability is 0x37c32b17 / 0x80000000 = 43.56%
39 ; BRANCHPROB: edge entry -> return probability is 0x483cd4e9 / 0x80000000 = 56.44%
40 ; BRANCHPROB: edge if.then -> return probability is 0x5ba2e8ba / 0x80000000 = 71.59%
41 ; BRANCHPROB: edge if.then -> label2 probability is 0x1d1745d1 / 0x80000000 = 22.73%
42 ; BRANCHPROB: edge if.then -> label3 probability is 0x0745d174 / 0x80000000 = 5.68%
43 ; BRANCHPROB: edge label2 -> return probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
44 ; BRANCHPROB: edge label3 -> return probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
45
46
47