fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 1 | /* |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2021 Apple Inc. All rights reserved. |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 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 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 27 | |
| 28 | #if ENABLE(B3_JIT) |
| 29 | |
fpizlo@apple.com | 65e043c | 2016-01-19 20:55:34 +0000 | [diff] [blame] | 30 | #include "AirArgInlines.h" |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 31 | #include "AirBlockWorklist.h" |
| 32 | #include "AirCode.h" |
| 33 | #include "AirInstInlines.h" |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 34 | #include "AirTmpInlines.h" |
| 35 | #include <wtf/CommaPrinter.h> |
| 36 | #include <wtf/Vector.h> |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 37 | |
| 38 | namespace JSC { namespace B3 { namespace Air { |
| 39 | |
| 40 | class Code; |
| 41 | |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 42 | // Computes the number of uses of a tmp based on frequency of execution. The frequency of blocks |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 43 | // that are only reachable by rare edges is scaled by Options::rareBlockPenalty(). |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 44 | class UseCounts { |
| 45 | public: |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 46 | UseCounts(Code& code) |
| 47 | { |
| 48 | // Find non-rare blocks. |
| 49 | BlockWorklist fastWorklist; |
| 50 | fastWorklist.push(code[0]); |
| 51 | while (BasicBlock* block = fastWorklist.pop()) { |
| 52 | for (FrequentedBlock& successor : block->successors()) { |
| 53 | if (!successor.isRare()) |
| 54 | fastWorklist.push(successor.block()); |
| 55 | } |
| 56 | } |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 57 | |
| 58 | unsigned gpArraySize = AbsoluteTmpMapper<GP>::absoluteIndex(code.numTmps(GP)); |
| 59 | m_gpNumWarmUsesAndDefs.resize(gpArraySize); |
| 60 | m_gpConstDefs.ensureSize(gpArraySize); |
| 61 | unsigned fpArraySize = AbsoluteTmpMapper<FP>::absoluteIndex(code.numTmps(FP)); |
| 62 | m_fpNumWarmUsesAndDefs.resize(fpArraySize); |
rmorisset@apple.com | 04cc337 | 2021-05-19 20:49:25 +0000 | [diff] [blame] | 63 | m_fpConstDefs.ensureSize(fpArraySize); |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 64 | |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 65 | for (BasicBlock* block : code) { |
| 66 | double frequency = block->frequency(); |
| 67 | if (!fastWorklist.saw(block)) |
| 68 | frequency *= Options::rareBlockPenalty(); |
| 69 | for (Inst& inst : *block) { |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 70 | inst.forEach<Tmp>( |
| 71 | [&] (Tmp& tmp, Arg::Role role, Bank bank, Width) { |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 72 | |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 73 | if (Arg::isWarmUse(role) || Arg::isAnyDef(role)) { |
| 74 | if (bank == GP) |
| 75 | m_gpNumWarmUsesAndDefs[AbsoluteTmpMapper<GP>::absoluteIndex(tmp)] += frequency; |
| 76 | else |
| 77 | m_fpNumWarmUsesAndDefs[AbsoluteTmpMapper<FP>::absoluteIndex(tmp)] += frequency; |
| 78 | } |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 79 | }); |
fpizlo@apple.com | 65e043c | 2016-01-19 20:55:34 +0000 | [diff] [blame] | 80 | |
fpizlo@apple.com | 1e42bb4 | 2016-09-30 16:59:24 +0000 | [diff] [blame] | 81 | if ((inst.kind.opcode == Move || inst.kind.opcode == Move32) |
fpizlo@apple.com | 65e043c | 2016-01-19 20:55:34 +0000 | [diff] [blame] | 82 | && inst.args[0].isSomeImm() |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 83 | && inst.args[1].is<Tmp>()) { |
| 84 | Tmp tmp = inst.args[1].as<Tmp>(); |
| 85 | if (tmp.bank() == GP) |
| 86 | m_gpConstDefs.quickSet(AbsoluteTmpMapper<GP>::absoluteIndex(tmp)); |
| 87 | else |
| 88 | m_fpConstDefs.quickSet(AbsoluteTmpMapper<FP>::absoluteIndex(tmp)); |
| 89 | } |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 94 | template<Bank bank> |
| 95 | bool isConstDef(unsigned absoluteIndex) const |
benjamin@webkit.org | e69ffdf | 2016-01-20 23:11:32 +0000 | [diff] [blame] | 96 | { |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 97 | if (bank == GP) |
| 98 | return m_gpConstDefs.quickGet(absoluteIndex); |
| 99 | return m_fpConstDefs.quickGet(absoluteIndex); |
| 100 | } |
| 101 | |
| 102 | template<Bank bank> |
| 103 | float numWarmUsesAndDefs(unsigned absoluteIndex) const |
| 104 | { |
| 105 | if (bank == GP) |
| 106 | return m_gpNumWarmUsesAndDefs[absoluteIndex]; |
| 107 | return m_fpNumWarmUsesAndDefs[absoluteIndex]; |
benjamin@webkit.org | e69ffdf | 2016-01-20 23:11:32 +0000 | [diff] [blame] | 108 | } |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 109 | |
| 110 | void dump(PrintStream& out) const |
| 111 | { |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 112 | CommaPrinter comma(", "); |
| 113 | for (unsigned i = 0; i < m_gpNumWarmUsesAndDefs.size(); ++i) |
| 114 | out.print(comma, AbsoluteTmpMapper<GP>::tmpFromAbsoluteIndex(i), "=> {numWarmUsesAndDefs=", m_gpNumWarmUsesAndDefs[i], ", isConstDef=", m_gpConstDefs.quickGet(i), "}"); |
| 115 | for (unsigned i = 0; i < m_fpNumWarmUsesAndDefs.size(); ++i) |
| 116 | out.print(comma, AbsoluteTmpMapper<FP>::tmpFromAbsoluteIndex(i), "=> {numWarmUsesAndDefs=", m_fpNumWarmUsesAndDefs[i], ", isConstDef=", m_fpConstDefs.quickGet(i), "}"); |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
rmorisset@apple.com | 2fe588a | 2021-05-19 03:24:14 +0000 | [diff] [blame] | 120 | Vector<float> m_gpNumWarmUsesAndDefs; |
| 121 | Vector<float> m_fpNumWarmUsesAndDefs; |
| 122 | BitVector m_gpConstDefs; |
| 123 | BitVector m_fpConstDefs; |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } } } // namespace JSC::B3::Air |
| 127 | |
| 128 | #endif // ENABLE(B3_JIT) |