llvm.org GIT mirror llvm / 9462243
Merging r142492: ------------------------------------------------------------------------ r142492 | chandlerc | 2011-10-19 03:30:30 -0700 (Wed, 19 Oct 2011) | 7 lines Teach the BranchProbabilityInfo analysis pass to read any metadata encoding of probabilities. In the absense of metadata, it continues to fall back on static heuristics. This allows __builtin_expect, after lowering through llvm.expect a branch instruction's metadata, to actually enter the branch probability model. This is one component of resolving PR2577. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_30@142547 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 9 years ago
2 changed file(s) with 63 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1212
1313 #include "llvm/Constants.h"
1414 #include "llvm/Instructions.h"
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Metadata.h"
1517 #include "llvm/Analysis/BranchProbabilityInfo.h"
1618 #include "llvm/Analysis/LoopInfo.h"
1719 #include "llvm/Support/Debug.h"
119121 : Weights(W), BP(BP), LI(LI) {
120122 }
121123
124 // Metadata Weights
125 bool calcMetadataWeights(BasicBlock *BB);
126
122127 // Return Heuristics
123128 bool calcReturnHeuristics(BasicBlock *BB);
124129
134139 bool runOnFunction(Function &F);
135140 };
136141 } // end anonymous namespace
142
143 // Propagate existing explicit probabilities from either profile data or
144 // 'expect' intrinsic processing.
145 // FIXME: This doesn't correctly extract probabilities for switches.
146 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
164 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));
169
170 return true;
171 }
137172
138173 // Calculate Edge Weights using "Return Heuristics". Predict a successor which
139174 // leads directly to Return Instruction will not be taken.
343378 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
344379 BasicBlock *BB = I++;
345380
381 if (calcMetadataWeights(BB))
382 continue;
383
346384 if (calcLoopBranchHeuristics(BB))
347385 continue;
348386
2121 exit:
2222 ret i32 %sum
2323 }
24
25 define i32 @test2(i32 %i, i32 %a, i32 %b) {
26 ; CHECK: Printing analysis {{.*}} for function 'test2'
27 ; CHECK: entry = 1024
28 entry:
29 %cond = icmp ult i32 %i, 42
30 br i1 %cond, label %then, label %else, !prof !0
31
32 ; The 'then' branch is predicted more likely via branch weight metadata.
33 ; CHECK: then = 963
34 then:
35 br label %exit
36
37 ; CHECK: else = 60
38 else:
39 br label %exit
40
41 ; FIXME: It may be a bug that we don't sum back to 1024.
42 ; CHECK: exit = 1023
43 exit:
44 %result = phi i32 [ %a, %then ], [ %b, %else ]
45 ret i32 %result
46 }
47
48 !0 = metadata !{metadata !"branch_weights", i32 64, i32 4}