1 | /* |
2 | * Copyright (C) 2016-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 | #include "config.h" |
27 | #include "B3PureCSE.h" |
28 | |
29 | #if ENABLE(B3_JIT) |
30 | |
31 | #include "B3Dominators.h" |
32 | #include "B3PhaseScope.h" |
33 | #include "B3Value.h" |
34 | #include "B3ValueInlines.h" |
35 | |
36 | namespace JSC { namespace B3 { |
37 | |
38 | PureCSE::PureCSE() |
39 | { |
40 | } |
41 | |
42 | PureCSE::~PureCSE() |
43 | { |
44 | } |
45 | |
46 | void PureCSE::clear() |
47 | { |
48 | m_map.clear(); |
49 | } |
50 | |
51 | Value* PureCSE::findMatch(const ValueKey& key, BasicBlock* block, Dominators& dominators) |
52 | { |
53 | if (!key) |
54 | return nullptr; |
55 | |
56 | auto iter = m_map.find(key); |
57 | if (iter == m_map.end()) |
58 | return nullptr; |
59 | |
60 | for (Value* match : iter->value) { |
61 | if (!match->owner) |
62 | continue; |
63 | if (dominators.dominates(match->owner, block)) |
64 | return match; |
65 | } |
66 | |
67 | return nullptr; |
68 | } |
69 | |
70 | bool PureCSE::process(Value* value, Dominators& dominators) |
71 | { |
72 | if (value->opcode() == Identity || value->isConstant()) |
73 | return false; |
74 | |
75 | ValueKey key = value->key(); |
76 | if (!key) |
77 | return false; |
78 | |
79 | Matches& matches = m_map.add(key, Matches()).iterator->value; |
80 | |
81 | for (Value* match : matches) { |
82 | if (!match->owner) |
83 | continue; |
84 | if (dominators.dominates(match->owner, value->owner)) { |
85 | value->replaceWithIdentity(match); |
86 | return true; |
87 | } |
88 | } |
89 | |
90 | matches.append(value); |
91 | return false; |
92 | } |
93 | |
94 | bool pureCSE(Procedure& proc) |
95 | { |
96 | PhaseScope phaseScope(proc, "pureCSE" ); |
97 | |
98 | Dominators& dominators = proc.dominators(); |
99 | PureCSE pureCSE; |
100 | bool result = false; |
101 | for (BasicBlock* block : proc.blocksInPreOrder()) { |
102 | for (Value* value : *block) { |
103 | result |= value->performSubstitution(); |
104 | result |= pureCSE.process(value, dominators); |
105 | } |
106 | } |
107 | |
108 | return result; |
109 | } |
110 | |
111 | } } // namespace JSC::B3 |
112 | |
113 | #endif // ENABLE(B3_JIT) |
114 | |
115 | |