|
9 | 9 |
|
10 | 10 | #include "fgprofilesynthesis.h"
|
11 | 11 |
|
| 12 | +//------------------------------------------------------------------------ |
| 13 | +// AdjustThrowEdgeLikelihoods: Find throw blocks in the flowgraph, and propagate |
| 14 | +// their throwable state through their predecessors in postorder. |
| 15 | +// Then, adjust heuristic-based likelihoods of edges into paths known to throw. |
| 16 | +// |
| 17 | +// Arguments: |
| 18 | +// compiler - the Compiler object |
| 19 | +// |
| 20 | +// Returns: |
| 21 | +// Suitable phase status |
| 22 | +// |
| 23 | +/* static */ PhaseStatus ProfileSynthesis::AdjustThrowEdgeLikelihoods(Compiler* compiler) |
| 24 | +{ |
| 25 | + const FlowGraphDfsTree* dfsTree = compiler->m_dfsTree; |
| 26 | + assert(dfsTree != nullptr); |
| 27 | + BitVecTraits traits = dfsTree->PostOrderTraits(); |
| 28 | + BitVec willThrow(BitVecOps::MakeEmpty(&traits)); |
| 29 | + |
| 30 | + // Adjusts the likelihoods out of a block that conditionally flows into a path that throws |
| 31 | + auto tweakLikelihoods = [&](BasicBlock* block) { |
| 32 | + assert(block->KindIs(BBJ_COND)); |
| 33 | + FlowEdge *throwEdge, *normalEdge; |
| 34 | + |
| 35 | + if (BitVecOps::IsMember(&traits, willThrow, block->GetTrueTarget()->bbPostorderNum)) |
| 36 | + { |
| 37 | + throwEdge = block->GetTrueEdge(); |
| 38 | + normalEdge = block->GetFalseEdge(); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + throwEdge = block->GetFalseEdge(); |
| 43 | + normalEdge = block->GetTrueEdge(); |
| 44 | + } |
| 45 | + |
| 46 | + throwEdge->setLikelihood(throwLikelihood); |
| 47 | + normalEdge->setLikelihood(1.0 - throwLikelihood); |
| 48 | + }; |
| 49 | + |
| 50 | + bool modified = false; |
| 51 | + |
| 52 | + // Walk the flowgraph in postorder, propagating throw state backwards |
| 53 | + for (unsigned i = 0; i < dfsTree->GetPostOrderCount(); i++) |
| 54 | + { |
| 55 | + BasicBlock* const block = dfsTree->GetPostOrder(i); |
| 56 | + if (block->KindIs(BBJ_THROW)) |
| 57 | + { |
| 58 | + JITDUMP(FMT_BB " will throw.\n", block->bbNum); |
| 59 | + BitVecOps::AddElemD(&traits, willThrow, i); |
| 60 | + } |
| 61 | + // Avoid slightly more expensive successor iteration for blocks with one outgoing edge |
| 62 | + else if ((block->GetUniqueSucc() != nullptr) && |
| 63 | + BitVecOps::IsMember(&traits, willThrow, block->GetUniqueSucc()->bbPostorderNum)) |
| 64 | + { |
| 65 | + JITDUMP(FMT_BB " flows into a throw block.\n", block->bbNum); |
| 66 | + BitVecOps::AddElemD(&traits, willThrow, i); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + bool anyPathThrows = false; |
| 71 | + bool allPathsThrow = true; |
| 72 | + |
| 73 | + for (BasicBlock* const succBlock : block->Succs(compiler)) |
| 74 | + { |
| 75 | + if (BitVecOps::IsMember(&traits, willThrow, succBlock->bbPostorderNum)) |
| 76 | + { |
| 77 | + anyPathThrows = true; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + allPathsThrow = false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (anyPathThrows) |
| 86 | + { |
| 87 | + if (allPathsThrow) |
| 88 | + { |
| 89 | + JITDUMP(FMT_BB " flows into a throw block.\n", block->bbNum); |
| 90 | + BitVecOps::AddElemD(&traits, willThrow, i); |
| 91 | + } |
| 92 | + else if (block->KindIs(BBJ_COND) && block->GetTrueEdge()->isHeuristicBased()) |
| 93 | + { |
| 94 | + JITDUMP(FMT_BB " can flow into a throw block.\n", block->bbNum); |
| 95 | + assert(block->GetFalseEdge()->isHeuristicBased()); |
| 96 | + tweakLikelihoods(block); |
| 97 | + modified = true; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (modified && compiler->fgIsUsingProfileWeights()) |
| 104 | + { |
| 105 | + JITDUMP("Modified edge likelihoods. Data %s inconsistent.\n", |
| 106 | + compiler->fgPgoConsistent ? "is now" : "was already"); |
| 107 | + compiler->fgPgoConsistent = false; |
| 108 | + } |
| 109 | + |
| 110 | + return modified ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING; |
| 111 | +} |
| 112 | + |
12 | 113 | // TODO
|
13 | 114 | //
|
14 | 115 | // * vet against some real data
|
@@ -252,6 +353,8 @@ void ProfileSynthesis::AssignLikelihoods()
|
252 | 353 |
|
253 | 354 | case BBJ_COND:
|
254 | 355 | // Two successor cases
|
| 356 | + block->GetTrueEdge()->setHeuristicBased(true); |
| 357 | + block->GetFalseEdge()->setHeuristicBased(true); |
255 | 358 | AssignLikelihoodCond(block);
|
256 | 359 | break;
|
257 | 360 |
|
|
0 commit comments