|
15 | 15 |
|
16 | 16 | #include "llvm/ADT/DenseMap.h"
|
17 | 17 | #include "swift/SIL/SILValue.h"
|
| 18 | +#include "swift/Basic/FixedBitSet.h" |
18 | 19 |
|
19 | 20 | namespace swift {
|
20 | 21 | class DominanceAnalysis;
|
| 22 | +class PostDominanceAnalysis; |
21 | 23 | class SILBasicBlock;
|
| 24 | +class CondBranchInst; |
22 | 25 |
|
23 | 26 | /// Cache a set of basic blocks that have been determined to be cold or hot.
|
24 | 27 | ///
|
25 | 28 | /// This does not inherit from SILAnalysis because it is not worth preserving
|
26 | 29 | /// across passes.
|
27 | 30 | class ColdBlockInfo {
|
| 31 | +public: |
| 32 | + // Represents the temperatures of edges flowing into a block. |
| 33 | + // |
| 34 | + // T = "top" -- both warm and cold edges |
| 35 | + // / \ |
| 36 | + // Warm Cold |
| 37 | + // \ / |
| 38 | + // B = "bottom" -- neither warm or cold edges |
| 39 | + struct State { |
| 40 | + // Each state kind, as an integer, is its position in any bit vectors. |
| 41 | + enum Temperature { |
| 42 | + Warm = 0, |
| 43 | + Cold = 1 |
| 44 | + }; |
| 45 | + |
| 46 | + // Number of states, excluding Top or Bottom, in this flow problem. |
| 47 | + static constexpr unsigned NumStates = 2; |
| 48 | + }; |
| 49 | + |
| 50 | + using Energy = FixedBitSet<State::NumStates, State::Temperature>; |
| 51 | + |
| 52 | +private: |
28 | 53 | DominanceAnalysis *DA;
|
| 54 | + PostDominanceAnalysis *PDA; |
29 | 55 |
|
30 |
| - /// Each block in this map has been determined to be either cold or hot. |
31 |
| - llvm::DenseMap<const SILBasicBlock*, bool> ColdBlockMap; |
| 56 | + /// Each block in this map has been determined to be cold and/or warm. |
| 57 | + /// Make sure to always use the set/resetToCold methods to update this! |
| 58 | + llvm::DenseMap<const SILBasicBlock*, Energy> EnergyMap; |
32 | 59 |
|
33 | 60 | // This is a cache and shouldn't be copied around.
|
34 | 61 | ColdBlockInfo(const ColdBlockInfo &) = delete;
|
35 | 62 | ColdBlockInfo &operator=(const ColdBlockInfo &) = delete;
|
| 63 | + LLVM_DUMP_METHOD void dump() const; |
36 | 64 |
|
37 |
| - /// Tri-value return code for checking branch hints. |
38 |
| - enum BranchHint : unsigned { |
39 |
| - None, |
40 |
| - LikelyTrue, |
41 |
| - LikelyFalse |
42 |
| - }; |
| 65 | + /// Tracks whether any information was changed in the energy map. |
| 66 | + bool changedMap = false; |
| 67 | + void resetToCold(const SILBasicBlock *BB); |
| 68 | + void set(const SILBasicBlock *BB, State::Temperature temp); |
43 | 69 |
|
44 |
| - enum { |
45 |
| - RecursionDepthLimit = 3 |
46 |
| - }; |
| 70 | + using ExpectedValue = std::optional<bool>; |
47 | 71 |
|
48 |
| - BranchHint getBranchHint(SILValue Cond, int recursionDepth); |
| 72 | + void setExpectedCondition(CondBranchInst *CondBranch, ExpectedValue value); |
49 | 73 |
|
50 |
| - bool isSlowPath(const SILBasicBlock *FromBB, const SILBasicBlock *ToBB, |
51 |
| - int recursionDepth); |
| 74 | + ExpectedValue searchForExpectedValue(SILValue Cond, |
| 75 | + unsigned recursionDepth = 0); |
| 76 | + void searchForExpectedValue(CondBranchInst *CondBranch); |
52 | 77 |
|
53 |
| - bool isCold(const SILBasicBlock *BB, |
54 |
| - int recursionDepth); |
| 78 | + bool inferFromEdgeProfile(SILBasicBlock *BB); |
55 | 79 |
|
56 | 80 | public:
|
57 |
| - ColdBlockInfo(DominanceAnalysis *DA): DA(DA) {} |
| 81 | + ColdBlockInfo(DominanceAnalysis *DA, PostDominanceAnalysis *PDA); |
58 | 82 |
|
59 |
| - bool isSlowPath(const SILBasicBlock *FromBB, const SILBasicBlock *ToBB) { |
60 |
| - return isSlowPath(FromBB, ToBB, 0); |
61 |
| - } |
| 83 | + void analyze(SILFunction *F); |
62 | 84 |
|
63 |
| - bool isCold(const SILBasicBlock *BB) { return isCold(BB, 0); } |
| 85 | + bool isCold(const SILBasicBlock *BB) const; |
64 | 86 | };
|
65 | 87 | } // end namespace swift
|
66 | 88 |
|
|
0 commit comments