Debian Codegraph @

js/src/jit/shared/Lowering-shared.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef jit_shared_Lowering_shared_h
8 #define jit_shared_Lowering_shared_h
9
10 // This file declares the structures that are used for attaching LIR to a
11 // MIRGraph.
12
13 #include "jit/LIR.h"
14 #include "jit/MIRGenerator.h"
15
16 namespace js {
17 namespace jit {
18
19 class MIRGenerator;
20 class MIRGraph;
21 class MDefinition;
22 class MInstruction;
23 class LOsiPoint;
24
25 class LIRGeneratorShared {
26 protected:
27 MIRGenerator* gen;
28 MIRGraph& graph;
29 LIRGraph& lirGraph_;
30 LBlock* current;
31 MResumePoint* lastResumePoint_;
32 LRecoverInfo* cachedRecoverInfo_;
33 LOsiPoint* osiPoint_;
34
35 LIRGeneratorShared(MIRGenerator* gen, MIRGraph& graph, LIRGraph& lirGraph)
36 : gen(gen),
37 graph(graph),
38 lirGraph_(lirGraph),
39 current(nullptr),
40 lastResumePoint_(nullptr),
41 cachedRecoverInfo_(nullptr),
42 osiPoint_(nullptr) {}
43
44 MIRGenerator* mir() { return gen; }
45
46 // Abort errors are caught at end of visitInstruction. It is possible for
47 // multiple errors to be detected before the end of visitInstruction. In
48 // this case, we only report the first back to the MIRGenerator.
49 bool errored() { return gen->getOffThreadStatus().isErr(); }
50 void abort(AbortReason r, const char* message, ...) MOZ_FORMAT_PRINTF(3, 4) {
51 if (errored()) {
52 return;
53 }
54
55 va_list ap;
56 va_start(ap, message);
57 auto reason_ = gen->abortFmt(r, message, ap);
58 va_end(ap);
59 gen->setOffThreadStatus(reason_);
60 }
61 void abort(AbortReason r) {
62 if (errored()) {
63 return;
64 }
65
66 auto reason_ = gen->abort(r);
67 gen->setOffThreadStatus(reason_);
68 }
69
70 static void ReorderCommutative(MDefinition** lhsp, MDefinition** rhsp,
71 MInstruction* ins);
72 static bool ShouldReorderCommutative(MDefinition* lhs, MDefinition* rhs,
73 MInstruction* ins);
74
75 // A backend can decide that an instruction should be emitted at its uses,
76 // rather than at its definition. To communicate this, set the
77 // instruction's virtual register set to 0. When using the instruction,
78 // its virtual register is temporarily reassigned. To know to clear it
79 // after constructing the use information, the worklist bit is temporarily
80 // unset.
81 //
82 // The backend can use the worklist bit to determine whether or not a
83 // definition should be created.
84 inline void emitAtUses(MInstruction* mir);
85
86 // The lowest-level calls to use, those that do not wrap another call to
87 // use(), must prefix grabbing virtual register IDs by these calls.
88 inline void ensureDefined(MDefinition* mir);
89
90 void visitEmittedAtUses(MInstruction* ins);
91
92 // These all create a use of a virtual register, with an optional
93 // allocation policy.
94 //
95 // Some of these use functions have atStart variants.
96 // - non-atStart variants will tell the register allocator that the input
97 // allocation must be different from any Temp or Definition also needed for
98 // this LInstruction.
99 // - atStart variants relax that restriction and allow the input to be in
100 // the same register as any output Definition (but not Temps) used by the
101 // LInstruction. Note that it doesn't *imply* this will actually happen,
102 // but gives a hint to the register allocator that it can do it.
103 //
104 // TL;DR: Use non-atStart variants only if you need the input value after
105 // writing to any definitions (excluding temps), during code generation of
106 // this LInstruction. Otherwise, use atStart variants, which will lower
107 // register pressure.
108 //
109 // There is an additional constraint. Consider a MIR node with two
110 // MDefinition* operands, op1 and op2. If the node reuses the register of op1
111 // for its output then op1 must be used as atStart. Then, if op1 and op2
112 // represent the same LIR node then op2 must be an atStart use too; otherwise
113 // op2 must be a non-atStart use. There is however not always a 1-1 mapping
114 // from MDefinition* to LNode*, so to determine whether two MDefinition* map
115 // to the same LNode*, ALWAYS go via the willHaveDifferentLIRNodes()
116 // predicate. Do not use pointer equality on the MIR nodes.
117 //
118 // Do not add other conditions when using willHaveDifferentLIRNodes(). The
119 // predicate is the source of truth about whether to use atStart or not, no
120 // other conditions may apply in contexts when it is appropriate to use it.
121 inline LUse use(MDefinition* mir, LUse policy);
122 inline LUse use(MDefinition* mir);
123 inline LUse useAtStart(MDefinition* mir);
124 inline LUse useRegister(MDefinition* mir);
125 inline LUse useRegisterAtStart(MDefinition* mir);
126 inline LUse useFixed(MDefinition* mir, Register reg);
127 inline LUse useFixed(MDefinition* mir, FloatRegister reg);
128 inline LUse useFixed(MDefinition* mir, AnyRegister reg);
129 inline LUse useFixedAtStart(MDefinition* mir, Register reg);
130 inline LUse useFixedAtStart(MDefinition* mir, AnyRegister reg);
131 inline LAllocation useOrConstant(MDefinition* mir);
132 inline LAllocation useOrConstantAtStart(MDefinition* mir);
133 // "Any" is architecture dependent, and will include registers and stack
134 // slots on X86, and only registers on ARM.
135 inline LAllocation useAny(MDefinition* mir);
136 inline LAllocation useAnyAtStart(MDefinition* mir);
137 inline LAllocation useAnyOrConstant(MDefinition* mir);
138 // "Storable" is architecture dependend, and will include registers and
139 // constants on X86 and only registers on ARM. This is a generic "things
140 // we can expect to write into memory in 1 instruction".
141 inline LAllocation useStorable(MDefinition* mir);
142 inline LAllocation useStorableAtStart(MDefinition* mir);
143 inline LAllocation useKeepalive(MDefinition* mir);
144 inline LAllocation useKeepaliveOrConstant(MDefinition* mir);
145 inline LAllocation useRegisterOrConstant(MDefinition* mir);
146 inline LAllocation useRegisterOrConstantAtStart(MDefinition* mir);
147 inline LAllocation useRegisterOrZeroAtStart(MDefinition* mir);
148 inline LAllocation useRegisterOrZero(MDefinition* mir);
149 inline LAllocation useRegisterOrNonDoubleConstant(MDefinition* mir);
150
151 // These methods accept either an Int32 or IntPtr value. A constant is used if
152 // the value fits in an int32.
153 inline LAllocation useRegisterOrInt32Constant(MDefinition* mir);
154 inline LAllocation useAnyOrInt32Constant(MDefinition* mir);
155
156 // Like useRegisterOrInt32Constant, but uses a constant only if
157 // |int32val * Scalar::byteSize(type) + offsetAdjustment| doesn't overflow
158 // int32.
159 LAllocation useRegisterOrIndexConstant(MDefinition* mir, Scalar::Type type,
160 int32_t offsetAdjustment = 0);
161
162 inline LUse useRegisterForTypedLoad(MDefinition* mir, MIRType type);
163
164 #ifdef JS_NUNBOX32
165 inline LUse useType(MDefinition* mir, LUse::Policy policy);
166 inline LUse usePayload(MDefinition* mir, LUse::Policy policy);
167 inline LUse usePayloadAtStart(MDefinition* mir, LUse::Policy policy);
168 inline LUse usePayloadInRegisterAtStart(MDefinition* mir);
169
170 // Adds a box input to an instruction, setting operand |n| to the type and
171 // |n+1| to the payload. Does not modify the operands, instead expecting a
172 // policy to already be set.
173 inline void fillBoxUses(LInstruction* lir, size_t n, MDefinition* mir);
174 #endif
175
176 // Test whether mir1 and mir2 may give rise to different LIR nodes even if
177 // mir1 == mir2; use it to guide the selection of the use directive for one of
178 // the nodes in the context of a reused input. See comments above about why
179 // it's important to use this predicate and not pointer equality.
180 //
181 // This predicate may be called before or after the application of a use
182 // directive to the first of the nodes, but it is meaningless to call it after
183 // the application of a directive to the second node.
184 inline bool willHaveDifferentLIRNodes(MDefinition* mir1, MDefinition* mir2);
185
186 // These create temporary register requests.
187 inline LDefinition temp(LDefinition::Type type = LDefinition::GENERAL,
188 LDefinition::Policy policy = LDefinition::REGISTER);
189 inline LInt64Definition tempInt64(
190 LDefinition::Policy policy = LDefinition::REGISTER);
191 inline LDefinition tempFloat32();
192 inline LDefinition tempDouble();
193 #ifdef ENABLE_WASM_SIMD
194 inline LDefinition tempSimd128();
195 #endif
196 inline LDefinition tempCopy(MDefinition* input, uint32_t reusedInput);
197
198 // Note that the fixed register has a GENERAL type,
199 // unless the arg is of FloatRegister type
200 inline LDefinition tempFixed(Register reg);
201 inline LDefinition tempFixed(FloatRegister reg);
202 inline LInt64Definition tempInt64Fixed(Register64 reg);
203
204 template <size_t Ops, size_t Temps>
205 inline void defineFixed(LInstructionHelper<1, Ops, Temps>* lir,
206 MDefinition* mir, const LAllocation& output);
207
208 template <size_t Temps>
209 inline void defineBox(
210 details::LInstructionFixedDefsTempsHelper<BOX_PIECES, Temps>* lir,
211 MDefinition* mir, LDefinition::Policy policy = LDefinition::REGISTER);
212
213 template <size_t Ops, size_t Temps>
214 inline void defineInt64(LInstructionHelper<INT64_PIECES, Ops, Temps>* lir,
215 MDefinition* mir,
216 LDefinition::Policy policy = LDefinition::REGISTER);
217
218 template <size_t Ops, size_t Temps>
219 inline void defineInt64Fixed(
220 LInstructionHelper<INT64_PIECES, Ops, Temps>* lir, MDefinition* mir,
221 const LInt64Allocation& output);
222
223 inline void defineReturn(LInstruction* lir, MDefinition* mir);
224
225 template <size_t X>
226 inline void define(details::LInstructionFixedDefsTempsHelper<1, X>* lir,
227 MDefinition* mir,
228 LDefinition::Policy policy = LDefinition::REGISTER);
229 template <size_t X>
230 inline void define(details::LInstructionFixedDefsTempsHelper<1, X>* lir,
231 MDefinition* mir, const LDefinition& def);
232
233 template <size_t Ops, size_t Temps>
234 inline void defineReuseInput(LInstructionHelper<1, Ops, Temps>* lir,
235 MDefinition* mir, uint32_t operand);
236
237 template <size_t Ops, size_t Temps>
238 inline void defineBoxReuseInput(
239 LInstructionHelper<BOX_PIECES, Ops, Temps>* lir, MDefinition* mir,
240 uint32_t operand);
241
242 template <size_t Ops, size_t Temps>
243 inline void defineInt64ReuseInput(
244 LInstructionHelper<INT64_PIECES, Ops, Temps>* lir, MDefinition* mir,
245 uint32_t operand);
246
247 // Returns a box allocation for a Value-typed instruction.
248 inline LBoxAllocation useBox(MDefinition* mir,
249 LUse::Policy policy = LUse::REGISTER,
250 bool useAtStart = false);
251
252 // Returns a box allocation. The use is either typed, a Value, or
253 // a constant (if useConstant is true).
254 inline LBoxAllocation useBoxOrTypedOrConstant(MDefinition* mir,
255 bool useConstant,
256 bool useAtStart = false);
257 inline LBoxAllocation useBoxOrTyped(MDefinition* mir,
258 bool useAtStart = false);
259
260 // Returns an int64 allocation for an Int64-typed instruction.
261 inline LInt64Allocation useInt64(MDefinition* mir, LUse::Policy policy,
262 bool useAtStart = false);
263 inline LInt64Allocation useInt64(MDefinition* mir, bool useAtStart = false);
264 inline LInt64Allocation useInt64AtStart(MDefinition* mir);
265 inline LInt64Allocation useInt64OrConstant(MDefinition* mir,
266 bool useAtStart = false);
267 inline LInt64Allocation useInt64Register(MDefinition* mir,
268 bool useAtStart = false);
269 inline LInt64Allocation useInt64RegisterOrConstant(MDefinition* mir,
270 bool useAtStart = false);
271 inline LInt64Allocation useInt64Fixed(MDefinition* mir, Register64 regs,
272 bool useAtStart = false);
273 inline LInt64Allocation useInt64FixedAtStart(MDefinition* mir,
274 Register64 regs);
275
276 inline LInt64Allocation useInt64RegisterAtStart(MDefinition* mir);
277 inline LInt64Allocation useInt64RegisterOrConstantAtStart(MDefinition* mir);
278 inline LInt64Allocation useInt64OrConstantAtStart(MDefinition* mir);
279
280 #ifdef JS_NUNBOX32
281 // Returns a non-int64 allocation for an Int64-typed instruction.
282 inline LUse useLowWord(MDefinition* mir, LUse policy);
283 inline LUse useLowWordRegister(MDefinition* mir);
284 inline LUse useLowWordRegisterAtStart(MDefinition* mir);
285 inline LUse useLowWordFixed(MDefinition* mir, Register reg);
286 #endif
287
288 // Rather than defining a new virtual register, sets |ins| to have the same
289 // virtual register as |as|.
290 inline void redefine(MDefinition* ins, MDefinition* as);
291
292 template <typename LClass, typename... Args>
293 inline LClass* allocateVariadic(uint32_t numOperands, Args&&... args);
294
295 TempAllocator& alloc() const { return graph.alloc(); }
296
297 uint32_t getVirtualRegister() {
298 uint32_t vreg = lirGraph_.getVirtualRegister();
299
300 // If we run out of virtual registers, mark code generation as having
301 // failed and return a dummy vreg. Include a + 1 here for NUNBOX32
302 // platforms that expect Value vregs to be adjacent.
303 if (vreg + 1 >= MAX_VIRTUAL_REGISTERS) {
304 abort(AbortReason::Alloc, "max virtual registers");
305 return 1;
306 }
307 return vreg;
308 }
309
310 template <typename T>
311 void annotate(T* ins);
312 template <typename T>
313 void add(T* ins, MInstruction* mir = nullptr);
314
315 void lowerTypedPhiInput(MPhi* phi, uint32_t inputPosition, LBlock* block,
316 size_t lirIndex);
317
318 void definePhiOneRegister(MPhi* phi, size_t lirIndex);
319 #ifdef JS_NUNBOX32
320 void definePhiTwoRegisters(MPhi* phi, size_t lirIndex);
321 #endif
322
323 void defineTypedPhi(MPhi* phi, size_t lirIndex) {
324 // One register containing the payload.
325 definePhiOneRegister(phi, lirIndex);
326 }
327 void defineUntypedPhi(MPhi* phi, size_t lirIndex) {
328 #ifdef JS_NUNBOX32
329 // Two registers: one for the type, one for the payload.
330 definePhiTwoRegisters(phi, lirIndex);
331 #else
332 // One register containing the full Value.
333 definePhiOneRegister(phi, lirIndex);
334 #endif
335 }
336
337 LOsiPoint* popOsiPoint() {
338 LOsiPoint* tmp = osiPoint_;
339 osiPoint_ = nullptr;
340 return tmp;
341 }
342
343 LRecoverInfo* getRecoverInfo(MResumePoint* rp);
344 LSnapshot* buildSnapshot(MResumePoint* rp, BailoutKind kind);
345 bool assignPostSnapshot(MInstruction* mir, LInstruction* ins);
346
347 // Marks this instruction as fallible, meaning that before it performs
348 // effects (if any), it may check pre-conditions and bailout if they do not
349 // hold. This function informs the register allocator that it will need to
350 // capture appropriate state.
351 void assignSnapshot(LInstruction* ins, BailoutKind kind);
352
353 // Marks this instruction as needing to call into either the VM or GC. This
354 // function may build a snapshot that captures the result of its own
355 // instruction, and as such, should generally be called after define*().
356 void assignSafepoint(LInstruction* ins, MInstruction* mir,
357 BailoutKind kind = BailoutKind::DuringVMCall);
358
359 // Marks this instruction as needing a wasm safepoint.
360 void assignWasmSafepoint(LInstruction* ins);
361
362 inline void lowerConstantDouble(double d, MInstruction* mir);
363 inline void lowerConstantFloat32(float f, MInstruction* mir);
364
365 bool canSpecializeWasmCompareAndSelect(MCompare::CompareType compTy,
366 MIRType insTy);
367 void lowerWasmCompareAndSelect(MWasmSelect* ins, MDefinition* lhs,
368 MDefinition* rhs, MCompare::CompareType compTy,
369 JSOp jsop);
370
371 public:
372 // Whether to generate typed reads for element accesses with hole checks.
373 static bool allowTypedElementHoleCheck() { return false; }
374 };
375
376 } // namespace jit
377 } // namespace js
378
379 #endif /* jit_shared_Lowering_shared_h */