1/*
2 * Copyright (C) 2016-2019 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#include "config.h"
27#include "B3DuplicateTails.h"
28
29#if ENABLE(B3_JIT)
30
31#include "B3BasicBlockInlines.h"
32#include "B3BreakCriticalEdges.h"
33#include "B3Dominators.h"
34#include "B3FixSSA.h"
35#include "B3InsertionSetInlines.h"
36#include "B3PhaseScope.h"
37#include "B3ProcedureInlines.h"
38#include "B3SwitchValue.h"
39#include "B3UpsilonValue.h"
40#include "B3ValueInlines.h"
41#include <wtf/IndexSet.h>
42
43namespace JSC { namespace B3 {
44
45namespace {
46
47namespace B3DuplicateTailsInternal {
48static constexpr bool verbose = false;
49}
50
51class DuplicateTails {
52public:
53 DuplicateTails(Procedure& proc)
54 : m_proc(proc)
55 , m_insertionSet(proc)
56 , m_maxSize(Options::maxB3TailDupBlockSize())
57 , m_maxSuccessors(Options::maxB3TailDupBlockSuccessors())
58 {
59 }
60
61 void run()
62 {
63 // Breaking critical edges introduces blocks that jump to things. Those Jumps' successors
64 // become candidates for tail duplication. Prior to critical edge breaking, some of those
65 // Jumps would have been Branches, and so no tail duplication would have happened.
66 breakCriticalEdges(m_proc);
67
68 // Find blocks that would be candidates for tail duplication. They must be small enough
69 // and they much not have too many successors.
70
71 m_proc.resetValueOwners();
72
73 IndexSet<BasicBlock*> candidates;
74
75 for (BasicBlock* block : m_proc) {
76 if (block->size() > m_maxSize)
77 continue;
78 if (block->numSuccessors() > m_maxSuccessors)
79 continue;
80 if (block->last()->type() != Void) // Demoting doesn't handle terminals with values.
81 continue;
82
83 candidates.add(block);
84 }
85
86 // Collect the set of values that must be de-SSA'd.
87 IndexSet<Value*> valuesToDemote;
88 for (BasicBlock* block : m_proc) {
89 for (Value* value : *block) {
90 if (value->opcode() == Phi && candidates.contains(block))
91 valuesToDemote.add(value);
92 for (Value* child : value->children()) {
93 if (child->owner != block && candidates.contains(child->owner))
94 valuesToDemote.add(child);
95 }
96 }
97 }
98 demoteValues(m_proc, valuesToDemote);
99 if (B3DuplicateTailsInternal::verbose) {
100 dataLog("Procedure after value demotion:\n");
101 dataLog(m_proc);
102 }
103
104 for (BasicBlock* block : m_proc) {
105 if (block->last()->opcode() != Jump)
106 continue;
107
108 BasicBlock* tail = block->successorBlock(0);
109 if (!candidates.contains(tail))
110 continue;
111
112 // Don't tail duplicate a trivial self-loop, because the code below can't handle block and
113 // tail being the same block.
114 if (block == tail)
115 continue;
116
117 // We're about to change 'block'. Make sure that nobody duplicates block after this
118 // point.
119 candidates.remove(block);
120
121 if (B3DuplicateTailsInternal::verbose)
122 dataLog("Duplicating ", *tail, " into ", *block, "\n");
123
124 block->removeLast(m_proc);
125
126 HashMap<Value*, Value*> map;
127 for (Value* value : *tail) {
128 Value* clone = m_proc.clone(value);
129 for (Value*& child : clone->children()) {
130 if (Value* replacement = map.get(child))
131 child = replacement;
132 }
133 if (value->type() != Void)
134 map.add(value, clone);
135 block->append(clone);
136 }
137 block->successors() = tail->successors();
138 }
139
140 m_proc.resetReachability();
141 m_proc.invalidateCFG();
142 }
143
144private:
145
146 Procedure& m_proc;
147 InsertionSet m_insertionSet;
148 unsigned m_maxSize;
149 unsigned m_maxSuccessors;
150};
151
152} // anonymous namespace
153
154void duplicateTails(Procedure& proc)
155{
156 PhaseScope phaseScope(proc, "duplicateTails");
157 DuplicateTails duplicateTails(proc);
158 duplicateTails.run();
159}
160
161} } // namespace JSC::B3
162
163#endif // ENABLE(B3_JIT)
164
165