1/*
2 * Copyright (C) 2015-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 "AirArgInlines.h"
31#include "AirBlockWorklist.h"
32#include "AirCode.h"
33#include "AirInstInlines.h"
34#include <wtf/HashMap.h>
35#include <wtf/ListDump.h>
36
37namespace JSC { namespace B3 { namespace Air {
38
39class Code;
40
41// Computes the number of uses of a variable based on frequency of execution. The frequency of blocks
42// that are only reachable by rare edges is scaled by Options::rareBlockPenalty().
43
44// Thing can be either Tmp or Arg.
45template<typename Thing>
46class UseCounts {
47public:
48 struct Counts {
49 void dump(PrintStream& out) const
50 {
51 out.print(
52 "{numWarmUses = ", numWarmUses, ", numColdUses = ", numColdUses, ", numDefs = ",
53 numDefs, "}");
54 }
55
56 double numWarmUses { 0 };
57 double numColdUses { 0 };
58 double numDefs { 0 };
59 double numConstDefs { 0 };
60 };
61
62 UseCounts(Code& code)
63 {
64 // Find non-rare blocks.
65 BlockWorklist fastWorklist;
66 fastWorklist.push(code[0]);
67 while (BasicBlock* block = fastWorklist.pop()) {
68 for (FrequentedBlock& successor : block->successors()) {
69 if (!successor.isRare())
70 fastWorklist.push(successor.block());
71 }
72 }
73
74 for (BasicBlock* block : code) {
75 double frequency = block->frequency();
76 if (!fastWorklist.saw(block))
77 frequency *= Options::rareBlockPenalty();
78 for (Inst& inst : *block) {
79 inst.forEach<Thing>(
80 [&] (Thing& arg, Arg::Role role, Bank, Width) {
81 Counts& counts = m_counts.add(arg, Counts()).iterator->value;
82
83 if (Arg::isWarmUse(role))
84 counts.numWarmUses += frequency;
85 if (Arg::isColdUse(role))
86 counts.numColdUses += frequency;
87 if (Arg::isAnyDef(role))
88 counts.numDefs += frequency;
89 });
90
91 if ((inst.kind.opcode == Move || inst.kind.opcode == Move32)
92 && inst.args[0].isSomeImm()
93 && inst.args[1].is<Thing>())
94 m_counts.add(inst.args[1].as<Thing>(), Counts()).iterator->value.numConstDefs++;
95 }
96 }
97 }
98
99 const Counts* operator[](const Thing& arg) const
100 {
101 auto iter = m_counts.find(arg);
102 if (iter == m_counts.end())
103 return nullptr;
104 return &iter->value;
105 }
106
107 void dump(PrintStream& out) const
108 {
109 out.print(mapDump(m_counts));
110 }
111
112private:
113 HashMap<Thing, Counts> m_counts;
114};
115
116} } } // namespace JSC::B3::Air
117
118#endif // ENABLE(B3_JIT)
119