llvm.org GIT mirror llvm / 3b89f11
Merging r142493: ------------------------------------------------------------------------ r142493 | chandlerc | 2011-10-19 03:32:19 -0700 (Wed, 19 Oct 2011) | 5 lines Generalize the reading of probability metadata to work for both branches and switches, with arbitrary numbers of successors. Still optimized for the common case of 2 successors for a conditional branch. Add a test case for switch metadata showing up in the BlockFrequencyInfo pass. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_30@142548 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 9 years ago
2 changed file(s) with 73 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
142142
143143 // Propagate existing explicit probabilities from either profile data or
144144 // 'expect' intrinsic processing.
145 // FIXME: This doesn't correctly extract probabilities for switches.
146145 bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
147 BranchInst *BI = dyn_cast(BB->getTerminator());
148 if (!BI || !BI->isConditional())
149 return false;
150
151 MDNode *WeightsNode = BI->getMetadata(LLVMContext::MD_prof);
152 if (!WeightsNode || WeightsNode->getNumOperands() < 3)
153 return false;
154
155 // Pull the weights out of the metadata. Note that the zero operand is the
156 // name.
157 ConstantInt *Weights[] = {
158 dyn_cast(WeightsNode->getOperand(1)),
159 dyn_cast(WeightsNode->getOperand(2))
160 };
161 if (!Weights[0] || !Weights[1])
162 return false;
163
146 TerminatorInst *TI = BB->getTerminator();
147 if (TI->getNumSuccessors() == 1)
148 return false;
149 if (!isa(TI) && !isa(TI))
150 return false;
151
152 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
153 if (!WeightsNode)
154 return false;
155
156 // Ensure there are weights for all of the successors. Note that the first
157 // operand to the metadata node is a name, not a weight.
158 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
159 return false;
160
161 // Build up the final weights that will be used in a temporary buffer, but
162 // don't add them until all weihts are present. Each weight value is clamped
163 // to [1, getMaxWeightFor(BB)].
164164 uint32_t WeightLimit = getMaxWeightFor(BB);
165 BP->setEdgeWeight(BB, BI->getSuccessor(0),
166 Weights[0]->getLimitedValue(WeightLimit));
167 BP->setEdgeWeight(BB, BI->getSuccessor(1),
168 Weights[1]->getLimitedValue(WeightLimit));
165 SmallVector Weights;
166 Weights.reserve(TI->getNumSuccessors());
167 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
168 ConstantInt *Weight = dyn_cast(WeightsNode->getOperand(i));
169 if (!Weight)
170 return false;
171 Weights.push_back(
172 std::max(1, Weight->getLimitedValue(WeightLimit)));
173 }
174 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
175 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
176 BP->setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
169177
170178 return true;
171179 }
4646 }
4747
4848 !0 = metadata !{metadata !"branch_weights", i32 64, i32 4}
49
50 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
51 ; CHECK: Printing analysis {{.*}} for function 'test3'
52 ; CHECK: entry = 1024
53 entry:
54 switch i32 %i, label %case_a [ i32 1, label %case_b
55 i32 2, label %case_c
56 i32 3, label %case_d
57 i32 4, label %case_e ], !prof !1
58
59 ; CHECK: case_a = 51
60 case_a:
61 br label %exit
62
63 ; CHECK: case_b = 51
64 case_b:
65 br label %exit
66
67 ; The 'case_c' branch is predicted more likely via branch weight metadata.
68 ; CHECK: case_c = 819
69 case_c:
70 br label %exit
71
72 ; CHECK: case_d = 51
73 case_d:
74 br label %exit
75
76 ; CHECK: case_e = 51
77 case_e:
78 br label %exit
79
80 ; FIXME: It may be a bug that we don't sum back to 1024.
81 ; CHECK: exit = 1023
82 exit:
83 %result = phi i32 [ %a, %case_a ],
84 [ %b, %case_b ],
85 [ %c, %case_c ],
86 [ %d, %case_d ],
87 [ %e, %case_e ]
88 ret i32 %result
89 }
90
91 !1 = metadata !{metadata !"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}