llvm.org GIT mirror llvm / ee99c7f
PGO: preserve branch-weight metadata when removing a case which jumps to the default target. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@163724 91177308-0d34-0410-b5e6-96231b3b80d8 Manman Ren 7 years ago
2 changed file(s) with 55 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
2222 #include "llvm/Instructions.h"
2323 #include "llvm/IntrinsicInst.h"
2424 #include "llvm/Intrinsics.h"
25 #include "llvm/MDBuilder.h"
2526 #include "llvm/Metadata.h"
2627 #include "llvm/Operator.h"
2728 #include "llvm/ADT/DenseMap.h"
121122 // Check to see if this branch is going to the same place as the default
122123 // dest. If so, eliminate it as an explicit compare.
123124 if (i.getCaseSuccessor() == DefaultDest) {
125 MDNode* MD = SI->getMetadata(LLVMContext::MD_prof);
126 // MD should have 2 + NumCases operands.
127 if (MD && MD->getNumOperands() == 2 + SI->getNumCases()) {
128 // Collect branch weights into a vector.
129 SmallVector Weights;
130 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
131 ++MD_i) {
132 ConstantInt* CI = dyn_cast(MD->getOperand(MD_i));
133 assert(CI);
134 Weights.push_back(CI->getValue().getZExtValue());
135 }
136 // Merge weight of this case to the default weight.
137 unsigned idx = i.getCaseIndex();
138 Weights[0] += Weights[idx+1];
139 // Remove weight for this case.
140 std::swap(Weights[idx+1], Weights.back());
141 Weights.pop_back();
142 SI->setMetadata(LLVMContext::MD_prof,
143 MDBuilder(BB->getContext()).
144 createBranchWeights(Weights));
145 }
124146 // Remove this entry.
125147 DefaultDest->removePredecessor(SI->getParent());
126148 SI->removeCase(i);
7878 ret void
7979 }
8080
81 ;; test5 - The case where it jumps to the default target will be removed.
82 define void @test5(i32 %M, i32 %N) nounwind uwtable {
83 entry:
84 switch i32 %N, label %sw2 [
85 i32 1, label %sw2
86 i32 2, label %sw.bb
87 i32 3, label %sw.bb1
88 ], !prof !3
89 ; CHECK: test5
90 ; CHECK: switch i32 %N, label %sw2 [
91 ; CHECK: i32 3, label %sw.bb1
92 ; CHECK: i32 2, label %sw.bb
93 ; CHECK: ], !prof !2
94
95 sw.bb:
96 call void @helper(i32 0)
97 br label %sw.epilog
98
99 sw.bb1:
100 call void @helper(i32 1)
101 br label %sw.epilog
102
103 sw2:
104 call void @helper(i32 2)
105 br label %sw.epilog
106
107 sw.epilog:
108 ret void
109 }
110
81111 !0 = metadata !{metadata !"branch_weights", i32 3, i32 5}
82112 !1 = metadata !{metadata !"branch_weights", i32 1, i32 1}
83113 !2 = metadata !{metadata !"branch_weights", i32 1, i32 2}
114 !3 = metadata !{metadata !"branch_weights", i32 4, i32 3, i32 2, i32 1}
84115
85116 ; CHECK: !0 = metadata !{metadata !"branch_weights", i32 5, i32 11}
86117 ; CHECK: !1 = metadata !{metadata !"branch_weights", i32 1, i32 5}
87 ; CHECK-NOT: !2
118 ; CHECK: !2 = metadata !{metadata !"branch_weights", i32 7, i32 1, i32 2}
119 ; CHECK-NOT: !3