1 | /* |
2 | * Copyright (C) 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 "PureNaN.h" |
31 | #include <climits> |
32 | #include <wtf/BubbleSort.h> |
33 | #include <wtf/Insertion.h> |
34 | #include <wtf/Vector.h> |
35 | |
36 | namespace JSC { namespace B3 { |
37 | |
38 | class InsertionSet; |
39 | |
40 | template<typename BasicBlock> |
41 | class GenericBlockInsertionSet { |
42 | public: |
43 | typedef WTF::Insertion<std::unique_ptr<BasicBlock>> BlockInsertion; |
44 | |
45 | GenericBlockInsertionSet(Vector<std::unique_ptr<BasicBlock>>& blocks) |
46 | : m_blocks(blocks) |
47 | { |
48 | } |
49 | |
50 | void insert(BlockInsertion&& insertion) |
51 | { |
52 | m_insertions.append(WTFMove(insertion)); |
53 | } |
54 | |
55 | // Insert a new block at a given index. |
56 | BasicBlock* insert(unsigned index, double frequency = PNaN) |
57 | { |
58 | std::unique_ptr<BasicBlock> block(new BasicBlock(UINT_MAX, frequency)); |
59 | BasicBlock* result = block.get(); |
60 | insert(BlockInsertion(index, WTFMove(block))); |
61 | return result; |
62 | } |
63 | |
64 | // Inserts a new block before the given block. Usually you will not pass the frequency |
65 | // argument. Passing PNaN causes us to just use the frequency of the 'before' block. That's |
66 | // usually what you want. |
67 | BasicBlock* insertBefore(BasicBlock* before, double frequency = PNaN) |
68 | { |
69 | return insert(before->index(), frequency == frequency ? frequency : before->frequency()); |
70 | } |
71 | |
72 | // Inserts a new block after the given block. |
73 | BasicBlock* insertAfter(BasicBlock* after, double frequency = PNaN) |
74 | { |
75 | return insert(after->index() + 1, frequency == frequency ? frequency : after->frequency()); |
76 | } |
77 | |
78 | bool execute() |
79 | { |
80 | if (m_insertions.isEmpty()) |
81 | return false; |
82 | |
83 | // We allow insertions to be given to us in any order. So, we need to sort them before |
84 | // running WTF::executeInsertions. We strongly prefer a stable sort and we want it to be |
85 | // fast, so we use bubble sort. |
86 | bubbleSort(m_insertions.begin(), m_insertions.end()); |
87 | |
88 | executeInsertions(m_blocks, m_insertions); |
89 | |
90 | // Prune out empty entries. This isn't strictly necessary but it's |
91 | // healthy to keep the block list from growing. |
92 | m_blocks.removeAllMatching( |
93 | [&] (std::unique_ptr<BasicBlock>& blockPtr) -> bool { |
94 | return !blockPtr; |
95 | }); |
96 | |
97 | // Make sure that the blocks know their new indices. |
98 | for (unsigned i = 0; i < m_blocks.size(); ++i) |
99 | m_blocks[i]->m_index = i; |
100 | |
101 | return true; |
102 | } |
103 | |
104 | private: |
105 | Vector<std::unique_ptr<BasicBlock>>& m_blocks; |
106 | Vector<BlockInsertion, 8> m_insertions; |
107 | }; |
108 | |
109 | } } // namespace JSC::B3 |
110 | |
111 | #endif // ENABLE(B3_JIT) |
112 | |