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 "B3OpaqueByproducts.h" |
31 | #include "B3Origin.h" |
32 | #include "B3PCToOriginMap.h" |
33 | #include "B3SparseCollection.h" |
34 | #include "B3Type.h" |
35 | #include "B3ValueKey.h" |
36 | #include "CCallHelpers.h" |
37 | #include "PureNaN.h" |
38 | #include "RegisterAtOffsetList.h" |
39 | #include <wtf/Bag.h> |
40 | #include <wtf/FastMalloc.h> |
41 | #include <wtf/HashSet.h> |
42 | #include <wtf/IndexedContainerIterator.h> |
43 | #include <wtf/Noncopyable.h> |
44 | #include <wtf/PrintStream.h> |
45 | #include <wtf/SharedTask.h> |
46 | #include <wtf/TriState.h> |
47 | #include <wtf/Vector.h> |
48 | |
49 | namespace JSC { namespace B3 { |
50 | |
51 | class BackwardsCFG; |
52 | class BackwardsDominators; |
53 | class BasicBlock; |
54 | class BlockInsertionSet; |
55 | class CFG; |
56 | class Dominators; |
57 | class NaturalLoops; |
58 | class StackSlot; |
59 | class Value; |
60 | class Variable; |
61 | |
62 | namespace Air { class Code; } |
63 | |
64 | typedef void WasmBoundsCheckGeneratorFunction(CCallHelpers&, GPRReg); |
65 | typedef SharedTask<WasmBoundsCheckGeneratorFunction> WasmBoundsCheckGenerator; |
66 | |
67 | // This represents B3's view of a piece of code. Note that this object must exist in a 1:1 |
68 | // relationship with Air::Code. B3::Procedure and Air::Code are just different facades of the B3 |
69 | // compiler's knowledge about a piece of code. Some kinds of state aren't perfect fits for either |
70 | // Procedure or Code, and are placed in one or the other based on convenience. Procedure always |
71 | // allocates a Code, and a Code cannot be allocated without an owning Procedure and they always |
72 | // have references to each other. |
73 | |
74 | class Procedure { |
75 | WTF_MAKE_NONCOPYABLE(Procedure); |
76 | WTF_MAKE_FAST_ALLOCATED; |
77 | public: |
78 | |
79 | JS_EXPORT_PRIVATE Procedure(); |
80 | JS_EXPORT_PRIVATE ~Procedure(); |
81 | |
82 | template<typename Callback> |
83 | void setOriginPrinter(Callback&& callback) |
84 | { |
85 | m_originPrinter = createSharedTask<void(PrintStream&, Origin)>( |
86 | std::forward<Callback>(callback)); |
87 | } |
88 | |
89 | // Usually you use this via OriginDump, though it's cool to use it directly. |
90 | void printOrigin(PrintStream& out, Origin origin) const; |
91 | |
92 | // This is a debugging hack. Sometimes while debugging B3 you need to break the abstraction |
93 | // and get at the DFG Graph, or whatever data structure the frontend used to describe the |
94 | // program. The FTL passes the DFG Graph. |
95 | void setFrontendData(const void* value) { m_frontendData = value; } |
96 | const void* frontendData() const { return m_frontendData; } |
97 | |
98 | JS_EXPORT_PRIVATE BasicBlock* addBlock(double frequency = 1); |
99 | |
100 | // Changes the order of basic blocks to be as in the supplied vector. The vector does not |
101 | // need to mention every block in the procedure. Blocks not mentioned will be placed after |
102 | // these blocks in the same order as they were in originally. |
103 | template<typename BlockIterable> |
104 | void setBlockOrder(const BlockIterable& iterable) |
105 | { |
106 | Vector<BasicBlock*> blocks; |
107 | for (BasicBlock* block : iterable) |
108 | blocks.append(block); |
109 | setBlockOrderImpl(blocks); |
110 | } |
111 | |
112 | JS_EXPORT_PRIVATE StackSlot* addStackSlot(unsigned byteSize); |
113 | JS_EXPORT_PRIVATE Variable* addVariable(Type); |
114 | |
115 | template<typename ValueType, typename... Arguments> |
116 | ValueType* add(Arguments...); |
117 | |
118 | Value* clone(Value*); |
119 | |
120 | Value* addIntConstant(Origin, Type, int64_t value); |
121 | Value* addIntConstant(Value*, int64_t value); |
122 | |
123 | // bits is a bitwise_cast of the constant you want. |
124 | Value* addConstant(Origin, Type, uint64_t bits); |
125 | |
126 | // You're guaranteed that bottom is zero. |
127 | Value* addBottom(Origin, Type); |
128 | Value* addBottom(Value*); |
129 | |
130 | // Returns null for MixedTriState. |
131 | Value* addBoolConstant(Origin, TriState); |
132 | |
133 | void resetValueOwners(); |
134 | JS_EXPORT_PRIVATE void resetReachability(); |
135 | |
136 | // This destroys CFG analyses. If we ask for them again, we will recompute them. Usually you |
137 | // should call this anytime you call resetReachability(). |
138 | void invalidateCFG(); |
139 | |
140 | JS_EXPORT_PRIVATE void dump(PrintStream&) const; |
141 | |
142 | unsigned size() const { return m_blocks.size(); } |
143 | BasicBlock* at(unsigned index) const { return m_blocks[index].get(); } |
144 | BasicBlock* operator[](unsigned index) const { return at(index); } |
145 | |
146 | typedef WTF::IndexedContainerIterator<Procedure> iterator; |
147 | |
148 | iterator begin() const { return iterator(*this, 0); } |
149 | iterator end() const { return iterator(*this, size()); } |
150 | |
151 | Vector<BasicBlock*> blocksInPreOrder(); |
152 | Vector<BasicBlock*> blocksInPostOrder(); |
153 | |
154 | SparseCollection<StackSlot>& stackSlots() { return m_stackSlots; } |
155 | const SparseCollection<StackSlot>& stackSlots() const { return m_stackSlots; } |
156 | |
157 | // Short for stackSlots().remove(). It's better to call this method since it's out of line. |
158 | void deleteStackSlot(StackSlot*); |
159 | |
160 | SparseCollection<Variable>& variables() { return m_variables; } |
161 | const SparseCollection<Variable>& variables() const { return m_variables; } |
162 | |
163 | // Short for variables().remove(). It's better to call this method since it's out of line. |
164 | void deleteVariable(Variable*); |
165 | |
166 | SparseCollection<Value>& values() { return m_values; } |
167 | const SparseCollection<Value>& values() const { return m_values; } |
168 | |
169 | // Short for values().remove(). It's better to call this method since it's out of line. |
170 | void deleteValue(Value*); |
171 | |
172 | // A valid procedure cannot contain any orphan values. An orphan is a value that is not in |
173 | // any basic block. It is possible to create an orphan value during code generation or during |
174 | // transformation. If you know that you may have created some, you can call this method to |
175 | // delete them, making the procedure valid again. |
176 | void deleteOrphans(); |
177 | |
178 | CFG& cfg() const { return *m_cfg; } |
179 | |
180 | Dominators& dominators(); |
181 | NaturalLoops& naturalLoops(); |
182 | BackwardsCFG& backwardsCFG(); |
183 | BackwardsDominators& backwardsDominators(); |
184 | |
185 | void addFastConstant(const ValueKey&); |
186 | bool isFastConstant(const ValueKey&); |
187 | |
188 | unsigned numEntrypoints() const { return m_numEntrypoints; } |
189 | JS_EXPORT_PRIVATE void setNumEntrypoints(unsigned); |
190 | |
191 | // Only call this after code generation is complete. Note that the label for the 0th entrypoint |
192 | // should point to exactly where the code generation cursor was before you started generating |
193 | // code. |
194 | JS_EXPORT_PRIVATE CCallHelpers::Label entrypointLabel(unsigned entrypointIndex) const; |
195 | |
196 | // The name has to be a string literal, since we don't do any memory management for the string. |
197 | void setLastPhaseName(const char* name) |
198 | { |
199 | m_lastPhaseName = name; |
200 | } |
201 | |
202 | const char* lastPhaseName() const { return m_lastPhaseName; } |
203 | |
204 | // Allocates a slab of memory that will be kept alive by anyone who keeps the resulting code |
205 | // alive. Great for compiler-generated data sections, like switch jump tables and constant pools. |
206 | // This returns memory that has been zero-initialized. |
207 | JS_EXPORT_PRIVATE void* addDataSection(size_t); |
208 | |
209 | // Some operations are specified in B3 IR to behave one way but on this given CPU they behave a |
210 | // different way. When true, those B3 IR ops switch to behaving the CPU way, and the optimizer may |
211 | // start taking advantage of it. |
212 | // |
213 | // One way to think of it is like this. Imagine that you find that the cleanest way of lowering |
214 | // something in lowerMacros is to unconditionally replace one opcode with another. This is a shortcut |
215 | // where you instead keep the same opcode, but rely on the opcode's meaning changes once lowerMacros |
216 | // sets hasQuirks. |
217 | bool hasQuirks() const { return m_hasQuirks; } |
218 | void setHasQuirks(bool value) { m_hasQuirks = value; } |
219 | |
220 | OpaqueByproducts& byproducts() { return *m_byproducts; } |
221 | |
222 | // Below are methods that make sense to call after you have generated code for the procedure. |
223 | |
224 | // You have to call this method after calling generate(). The code generated by B3::generate() |
225 | // will require you to keep this object alive for as long as that code is runnable. Usually, this |
226 | // just keeps alive things like the double constant pool and switch lookup tables. If this sounds |
227 | // confusing, you should probably be using the B3::Compilation API to compile code. If you use |
228 | // that API, then you don't have to worry about this. |
229 | std::unique_ptr<OpaqueByproducts> releaseByproducts() { return WTFMove(m_byproducts); } |
230 | |
231 | // This gives you direct access to Code. However, the idea is that clients of B3 shouldn't have to |
232 | // call this. So, Procedure has some methods (below) that expose some Air::Code functionality. |
233 | const Air::Code& code() const { return *m_code; } |
234 | Air::Code& code() { return *m_code; } |
235 | |
236 | unsigned callArgAreaSizeInBytes() const; |
237 | void requestCallArgAreaSizeInBytes(unsigned size); |
238 | |
239 | // This tells the register allocators to stay away from this register. |
240 | JS_EXPORT_PRIVATE void pinRegister(Reg); |
241 | |
242 | JS_EXPORT_PRIVATE void setOptLevel(unsigned value); |
243 | unsigned optLevel() const { return m_optLevel; } |
244 | |
245 | // You can turn off used registers calculation. This may speed up compilation a bit. But if |
246 | // you turn it off then you cannot use StackmapGenerationParams::usedRegisters() or |
247 | // StackmapGenerationParams::unavailableRegisters(). |
248 | void setNeedsUsedRegisters(bool value) { m_needsUsedRegisters = value; } |
249 | bool needsUsedRegisters() const { return m_needsUsedRegisters; } |
250 | |
251 | JS_EXPORT_PRIVATE unsigned frameSize() const; |
252 | JS_EXPORT_PRIVATE RegisterAtOffsetList calleeSaveRegisterAtOffsetList() const; |
253 | |
254 | PCToOriginMap& pcToOriginMap() { return m_pcToOriginMap; } |
255 | PCToOriginMap releasePCToOriginMap() { return WTFMove(m_pcToOriginMap); } |
256 | |
257 | JS_EXPORT_PRIVATE void setWasmBoundsCheckGenerator(RefPtr<WasmBoundsCheckGenerator>); |
258 | |
259 | template<typename Functor> |
260 | void setWasmBoundsCheckGenerator(const Functor& functor) |
261 | { |
262 | setWasmBoundsCheckGenerator(RefPtr<WasmBoundsCheckGenerator>(createSharedTask<WasmBoundsCheckGeneratorFunction>(functor))); |
263 | } |
264 | |
265 | JS_EXPORT_PRIVATE RegisterSet mutableGPRs(); |
266 | JS_EXPORT_PRIVATE RegisterSet mutableFPRs(); |
267 | |
268 | private: |
269 | friend class BlockInsertionSet; |
270 | |
271 | JS_EXPORT_PRIVATE Value* addValueImpl(Value*); |
272 | void setBlockOrderImpl(Vector<BasicBlock*>&); |
273 | |
274 | SparseCollection<StackSlot> m_stackSlots; |
275 | SparseCollection<Variable> m_variables; |
276 | Vector<std::unique_ptr<BasicBlock>> m_blocks; |
277 | SparseCollection<Value> m_values; |
278 | std::unique_ptr<CFG> m_cfg; |
279 | std::unique_ptr<Dominators> m_dominators; |
280 | std::unique_ptr<NaturalLoops> m_naturalLoops; |
281 | std::unique_ptr<BackwardsCFG> m_backwardsCFG; |
282 | std::unique_ptr<BackwardsDominators> m_backwardsDominators; |
283 | HashSet<ValueKey> m_fastConstants; |
284 | unsigned m_numEntrypoints { 1 }; |
285 | const char* m_lastPhaseName; |
286 | std::unique_ptr<OpaqueByproducts> m_byproducts; |
287 | std::unique_ptr<Air::Code> m_code; |
288 | RefPtr<SharedTask<void(PrintStream&, Origin)>> m_originPrinter; |
289 | const void* m_frontendData; |
290 | PCToOriginMap m_pcToOriginMap; |
291 | unsigned m_optLevel { defaultOptLevel() }; |
292 | bool m_needsUsedRegisters { true }; |
293 | bool m_hasQuirks { false }; |
294 | }; |
295 | |
296 | } } // namespace JSC::B3 |
297 | |
298 | #endif // ENABLE(B3_JIT) |
299 | |