blob: 9182cb6c8bb7a94207ac1010404afb8e824db2e3 [file] [log] [blame]
fpizlo@apple.comd1d46742015-12-01 07:03:55 +00001/*
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +00002 * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
fpizlo@apple.comd1d46742015-12-01 07:03:55 +00003 *
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.com22104f52016-09-28 17:08:17 +000026#pragma once
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000027
28#if ENABLE(B3_JIT)
29
fpizlo@apple.com65e043c2016-01-19 20:55:34 +000030#include "AirArgInlines.h"
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000031#include "AirBlockWorklist.h"
32#include "AirCode.h"
33#include "AirInstInlines.h"
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000034#include "AirTmpInlines.h"
35#include <wtf/CommaPrinter.h>
36#include <wtf/Vector.h>
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000037
38namespace JSC { namespace B3 { namespace Air {
39
40class Code;
41
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000042// Computes the number of uses of a tmp based on frequency of execution. The frequency of blocks
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000043// that are only reachable by rare edges is scaled by Options::rareBlockPenalty().
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000044class UseCounts {
45public:
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000046 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.com2fe588a2021-05-19 03:24:14 +000057
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.com04cc3372021-05-19 20:49:25 +000063 m_fpConstDefs.ensureSize(fpArraySize);
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000064
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000065 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.com2fe588a2021-05-19 03:24:14 +000070 inst.forEach<Tmp>(
71 [&] (Tmp& tmp, Arg::Role role, Bank bank, Width) {
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000072
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000073 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.comd1d46742015-12-01 07:03:55 +000079 });
fpizlo@apple.com65e043c2016-01-19 20:55:34 +000080
fpizlo@apple.com1e42bb42016-09-30 16:59:24 +000081 if ((inst.kind.opcode == Move || inst.kind.opcode == Move32)
fpizlo@apple.com65e043c2016-01-19 20:55:34 +000082 && inst.args[0].isSomeImm()
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000083 && 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.comd1d46742015-12-01 07:03:55 +000090 }
91 }
92 }
93
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000094 template<Bank bank>
95 bool isConstDef(unsigned absoluteIndex) const
benjamin@webkit.orge69ffdf2016-01-20 23:11:32 +000096 {
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +000097 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.orge69ffdf2016-01-20 23:11:32 +0000108 }
fpizlo@apple.comd1d46742015-12-01 07:03:55 +0000109
110 void dump(PrintStream& out) const
111 {
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +0000112 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.comd1d46742015-12-01 07:03:55 +0000117 }
118
119private:
rmorisset@apple.com2fe588a2021-05-19 03:24:14 +0000120 Vector<float> m_gpNumWarmUsesAndDefs;
121 Vector<float> m_fpNumWarmUsesAndDefs;
122 BitVector m_gpConstDefs;
123 BitVector m_fpConstDefs;
fpizlo@apple.comd1d46742015-12-01 07:03:55 +0000124};
125
126} } } // namespace JSC::B3::Air
127
128#endif // ENABLE(B3_JIT)