|
| 1 | +#ifndef wasm_analysis_monotone_analyzer_impl_h |
| 2 | +#define wasm_analysis_monotone_analyzer_impl_h |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +#include "monotone-analyzer.h" |
| 8 | + |
| 9 | +namespace wasm::analysis { |
| 10 | +template<size_t N> |
| 11 | +inline BlockState<N>::BlockState(const BasicBlock* underlyingBlock) |
| 12 | + : index(underlyingBlock->getIndex()), cfgBlock(underlyingBlock), |
| 13 | + beginningState(BitsetPowersetLattice<N>::getBottom()), |
| 14 | + endState(BitsetPowersetLattice<N>::getBottom()), |
| 15 | + currState(BitsetPowersetLattice<N>::getBottom()) {} |
| 16 | + |
| 17 | +template<size_t N> inline void BlockState<N>::addPredecessor(BlockState* pred) { |
| 18 | + predecessors.push_back(pred); |
| 19 | +} |
| 20 | + |
| 21 | +template<size_t N> inline void BlockState<N>::addSuccessor(BlockState* succ) { |
| 22 | + successors.push_back(succ); |
| 23 | +} |
| 24 | + |
| 25 | +template<size_t N> |
| 26 | +inline BitsetPowersetLattice<N>& BlockState<N>::getFirstState() { |
| 27 | + return beginningState; |
| 28 | +} |
| 29 | + |
| 30 | +template<size_t N> |
| 31 | +inline BitsetPowersetLattice<N>& BlockState<N>::getLastState() { |
| 32 | + return endState; |
| 33 | +} |
| 34 | + |
| 35 | +// In our current limited implementation, we just update a new live variable |
| 36 | +// when it it is used in a get or set. |
| 37 | +template<size_t N> inline void BlockState<N>::visitLocalSet(LocalSet* curr) { |
| 38 | + currState.value[curr->index] = false; |
| 39 | +} |
| 40 | + |
| 41 | +template<size_t N> inline void BlockState<N>::visitLocalGet(LocalGet* curr) { |
| 42 | + currState.value[curr->index] = true; |
| 43 | +} |
| 44 | + |
| 45 | +template<size_t N> inline void BlockState<N>::transfer() { |
| 46 | + // If the block is empty, we propagate the state by endState = currState, then |
| 47 | + // currState = beginningState |
| 48 | + |
| 49 | + // compute transfer function for all expressions in the CFG block |
| 50 | + auto cfgIter = cfgBlock->rbegin(); |
| 51 | + currState = endState; |
| 52 | + |
| 53 | + while (cfgIter != cfgBlock->rend()) { |
| 54 | + // run transfer function. |
| 55 | + BlockState<N>::visit(*cfgIter); |
| 56 | + ++cfgIter; |
| 57 | + } |
| 58 | + beginningState = currState; |
| 59 | +} |
| 60 | + |
| 61 | +template<size_t N> inline void BlockState<N>::print(std::ostream& os) { |
| 62 | + os << "State Block: " << index << std::endl; |
| 63 | + os << "State at beginning: "; |
| 64 | + beginningState.print(os); |
| 65 | + os << std::endl << "State at end: "; |
| 66 | + endState.print(os); |
| 67 | + os << std::endl << "Intermediate States (reverse order): " << std::endl; |
| 68 | + |
| 69 | + currState = endState; |
| 70 | + currState.print(os); |
| 71 | + os << std::endl; |
| 72 | + auto cfgIter = cfgBlock->rbegin(); |
| 73 | + |
| 74 | + while (cfgIter != cfgBlock->rend()) { |
| 75 | + // run transfer function. |
| 76 | + os << ShallowExpression{*cfgIter} << std::endl; |
| 77 | + BlockState<N>::visit(*cfgIter); |
| 78 | + currState.print(os); |
| 79 | + os << std::endl; |
| 80 | + ++cfgIter; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +template<size_t N> |
| 85 | +MonotoneCFGAnalyzer<N> inline MonotoneCFGAnalyzer<N>::fromCFG(CFG* cfg) { |
| 86 | + MonotoneCFGAnalyzer<N> result; |
| 87 | + |
| 88 | + // Map BasicBlocks to each BlockState's index |
| 89 | + std::unordered_map<const BasicBlock*, size_t> basicBlockToState; |
| 90 | + size_t index = 0; |
| 91 | + for (auto it = cfg->begin(); it != cfg->end(); it++) { |
| 92 | + result.stateBlocks.emplace_back(&(*it)); |
| 93 | + basicBlockToState[&(*it)] = index++; |
| 94 | + } |
| 95 | + |
| 96 | + // Update predecessors and successors of each BlockState object |
| 97 | + // according to the BasicBlock's predecessors and successors. |
| 98 | + for (index = 0; index < result.stateBlocks.size(); ++index) { |
| 99 | + BlockState<N>& currBlock = result.stateBlocks.at(index); |
| 100 | + BasicBlock::Predecessors preds = currBlock.cfgBlock->preds(); |
| 101 | + BasicBlock::Successors succs = currBlock.cfgBlock->succs(); |
| 102 | + for (auto pred : preds) { |
| 103 | + currBlock.addPredecessor(&result.stateBlocks[basicBlockToState[&pred]]); |
| 104 | + } |
| 105 | + |
| 106 | + for (auto succ : succs) { |
| 107 | + currBlock.addSuccessor(&result.stateBlocks[basicBlockToState[&succ]]); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | +} |
| 113 | + |
| 114 | +template<size_t N> inline void MonotoneCFGAnalyzer<N>::evaluate() { |
| 115 | + std::queue<Index> worklist; |
| 116 | + |
| 117 | + for (auto it = stateBlocks.rbegin(); it != stateBlocks.rend(); ++it) { |
| 118 | + worklist.push(it->index); |
| 119 | + } |
| 120 | + |
| 121 | + while (!worklist.empty()) { |
| 122 | + BlockState<N>& currBlockState = stateBlocks[worklist.front()]; |
| 123 | + worklist.pop(); |
| 124 | + currBlockState.transfer(); |
| 125 | + |
| 126 | + // Propagate state to dependents |
| 127 | + for (size_t j = 0; j < currBlockState.predecessors.size(); ++j) { |
| 128 | + BitsetPowersetLattice<N>& predLast = |
| 129 | + currBlockState.predecessors[j]->getLastState(); |
| 130 | + |
| 131 | + LatticeComparison cmp = BitsetPowersetLattice<N>::compare( |
| 132 | + predLast, currBlockState.getFirstState()); |
| 133 | + |
| 134 | + if (cmp == LatticeComparison::NO_RELATION || |
| 135 | + cmp == LatticeComparison::LESS) { |
| 136 | + predLast.getLeastUpperBound(currBlockState.getFirstState()); |
| 137 | + worklist.push(currBlockState.predecessors[j]->index); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +template<size_t N> inline void MonotoneCFGAnalyzer<N>::print(std::ostream& os) { |
| 144 | + os << "CFG Analyzer" << std::endl; |
| 145 | + for (auto state : stateBlocks) { |
| 146 | + state.print(os); |
| 147 | + } |
| 148 | + os << "End" << std::endl; |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace wasm::analysis |
| 152 | + |
| 153 | +#endif // wasm_analysis_monotone_analyzer_impl_h |
0 commit comments