| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(B3_JIT) |
| 29 | |
| 30 | #include "B3BasicBlock.h" |
| 31 | #include "B3CFG.h" |
| 32 | #include "B3Procedure.h" |
| 33 | #include "B3ValueInlines.h" |
| 34 | #include "B3Variable.h" |
| 35 | #include "B3VariableValue.h" |
| 36 | #include <wtf/Liveness.h> |
| 37 | |
| 38 | namespace JSC { namespace B3 { |
| 39 | |
| 40 | struct VariableLivenessAdapter { |
| 41 | static constexpr const char* name = "VariableLiveness" ; |
| 42 | typedef B3::CFG CFG; |
| 43 | typedef Variable* Thing; |
| 44 | |
| 45 | VariableLivenessAdapter(Procedure& proc) |
| 46 | : proc(proc) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void prepareToCompute() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | unsigned numIndices() |
| 55 | { |
| 56 | return proc.variables().size(); |
| 57 | } |
| 58 | |
| 59 | static unsigned valueToIndex(Variable* var) { return var->index(); } |
| 60 | Variable* indexToValue(unsigned index) { return proc.variables()[index]; } |
| 61 | |
| 62 | unsigned blockSize(BasicBlock* block) |
| 63 | { |
| 64 | return block->size(); |
| 65 | } |
| 66 | |
| 67 | template<typename Func> |
| 68 | void forEachUse(BasicBlock* block, unsigned valueBoundaryIndex, const Func& func) |
| 69 | { |
| 70 | // We want all of the uses that happen between valueBoundaryIndex-1 and |
| 71 | // valueBoundaryIndex. Since the Get opcode is the only value that has a use and since |
| 72 | // this is an early use, we only care about block[valueBoundaryIndex]. |
| 73 | Value* value = block->get(valueBoundaryIndex); |
| 74 | if (!value) |
| 75 | return; |
| 76 | if (value->opcode() != Get) |
| 77 | return; |
| 78 | func(value->as<VariableValue>()->variable()->index()); |
| 79 | } |
| 80 | |
| 81 | template<typename Func> |
| 82 | void forEachDef(BasicBlock* block, unsigned valueBoundaryIndex, const Func& func) |
| 83 | { |
| 84 | // We want all of the defs that happen between valueBoundaryIndex-1 and |
| 85 | // valueBoundaryIndex. Since the Set opcode is the only value that has a def and since |
| 86 | // this is an late def, we only care about block[valueBoundaryIndex - 1]. |
| 87 | Value* value = block->get(valueBoundaryIndex - 1); |
| 88 | if (!value) |
| 89 | return; |
| 90 | if (value->opcode() != Set) |
| 91 | return; |
| 92 | func(value->as<VariableValue>()->variable()->index()); |
| 93 | } |
| 94 | |
| 95 | Procedure& proc; |
| 96 | }; |
| 97 | |
| 98 | class VariableLiveness : public WTF::Liveness<VariableLivenessAdapter> { |
| 99 | public: |
| 100 | VariableLiveness(Procedure&); |
| 101 | ~VariableLiveness(); |
| 102 | }; |
| 103 | |
| 104 | } } // namespace JSC::B3 |
| 105 | |
| 106 | #endif // ENABLE(B3_JIT) |
| 107 | |
| 108 | |