Debian Codegraph @

js/src/jit/x86-shared/MoveEmitter-x86-shared.cpp

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 #include "jit/x86-shared/MoveEmitter-x86-shared.h"
8
9 #include "jit/MacroAssembler-inl.h"
10
11 using namespace js;
12 using namespace js::jit;
13
14 using mozilla::Maybe;
15
16 MoveEmitterX86::MoveEmitterX86(MacroAssembler& masm)
17 : inCycle_(false), masm(masm), pushedAtCycle_(-1) {
18 pushedAtStart_ = masm.framePushed();
19 }
20
21 // Examine the cycle in moves starting at position i. Determine if it's a
22 // simple cycle consisting of all register-to-register moves in a single class,
23 // and whether it can be implemented entirely by swaps.
24 size_t MoveEmitterX86::characterizeCycle(const MoveResolver& moves, size_t i,
25 bool* allGeneralRegs,
26 bool* allFloatRegs) {
27 size_t swapCount = 0;
28
29 for (size_t j = i;; j++) {
30 const MoveOp& move = moves.getMove(j);
31
32 // If it isn't a cycle of registers of the same kind, we won't be able
33 // to optimize it.
34 if (!move.to().isGeneralReg()) {
35 *allGeneralRegs = false;
36 }
37 if (!move.to().isFloatReg()) {
38 *allFloatRegs = false;
39 }
40 if (!*allGeneralRegs && !*allFloatRegs) {
41 return -1;
42 }
43
44 // Stop iterating when we see the last one.
45 if (j != i && move.isCycleEnd()) {
46 break;
47 }
48
49 // Check that this move is actually part of the cycle. This is
50 // over-conservative when there are multiple reads from the same source,
51 // but that's expected to be rare.
52 if (move.from() != moves.getMove(j + 1).to()) {
53 *allGeneralRegs = false;
54 *allFloatRegs = false;
55 return -1;
56 }
57
58 swapCount++;
59 }
60
61 // Check that the last move cycles back to the first move.
62 const MoveOp& move = moves.getMove(i + swapCount);
63 if (move.from() != moves.getMove(i).to()) {
64 *allGeneralRegs = false;
65 *allFloatRegs = false;
66 return -1;
67 }
68
69 return swapCount;
70 }
71
72 // If we can emit optimized code for the cycle in moves starting at position i,
73 // do so, and return true.
74 bool MoveEmitterX86::maybeEmitOptimizedCycle(const MoveResolver& moves,
75 size_t i, bool allGeneralRegs,
76 bool allFloatRegs,
77 size_t swapCount) {
78 if (allGeneralRegs && swapCount <= 2) {
79 // Use x86's swap-integer-registers instruction if we only have a few
80 // swaps. (x86 also has a swap between registers and memory but it's
81 // slow.)
82 for (size_t k = 0; k < swapCount; k++) {
83 masm.xchg(moves.getMove(i + k).to().reg(),
84 moves.getMove(i + k + 1).to().reg());
85 }
86 return true;
87 }
88
89 if (allFloatRegs && swapCount == 1) {
90 // There's no xchg for xmm registers, but if we only need a single swap,
91 // it's cheap to do an XOR swap.
92 FloatRegister a = moves.getMove(i).to().floatReg();
93 FloatRegister b = moves.getMove(i + 1).to().floatReg();
94 masm.vxorpd(a, b, b);
95 masm.vxorpd(b, a, a);
96 masm.vxorpd(a, b, b);
97 return true;
98 }
99
100 return false;
101 }
102
103 void MoveEmitterX86::emit(const MoveResolver& moves) {
104 #if defined(JS_CODEGEN_X86) && defined(DEBUG)
105 // Clobber any scratch register we have, to make regalloc bugs more visible.
106 if (scratchRegister_.isSome()) {
107 masm.mov(ImmWord(0xdeadbeef), scratchRegister_.value());
108 }
109 #endif
110
111 for (size_t i = 0; i < moves.numMoves(); i++) {
112 #if defined(JS_CODEGEN_X86) && defined(DEBUG)
113 if (!scratchRegister_.isSome()) {
114 Maybe<Register> reg = findScratchRegister(moves, i);
115 if (reg.isSome()) {
116 masm.mov(ImmWord(0xdeadbeef), reg.value());
117 }
118 }
119 #endif
120
121 const MoveOp& move = moves.getMove(i);
122 const MoveOperand& from = move.from();
123 const MoveOperand& to = move.to();
124
125 if (move.isCycleEnd()) {
126 MOZ_ASSERT(inCycle_);
127 completeCycle(to, move.type());
128 inCycle_ = false;
129 continue;
130 }
131
132 if (move.isCycleBegin()) {
133 MOZ_ASSERT(!inCycle_);
134
135 // Characterize the cycle.
136 bool allGeneralRegs = true, allFloatRegs = true;
137 size_t swapCount =
138 characterizeCycle(moves, i, &allGeneralRegs, &allFloatRegs);
139
140 // Attempt to optimize it to avoid using the stack.
141 if (maybeEmitOptimizedCycle(moves, i, allGeneralRegs, allFloatRegs,
142 swapCount)) {
143 i += swapCount;
144 continue;
145 }
146
147 // Otherwise use the stack.
148 breakCycle(to, move.endCycleType());
149 inCycle_ = true;
150 }
151
152 // A normal move which is not part of a cycle.
153 switch (move.type()) {
154 case MoveOp::FLOAT32:
155 emitFloat32Move(from, to);
156 break;
157 case MoveOp::DOUBLE:
158 emitDoubleMove(from, to);
159 break;
160 case MoveOp::INT32:
161 emitInt32Move(from, to, moves, i);
162 break;
163 case MoveOp::GENERAL:
164 emitGeneralMove(from, to, moves, i);
165 break;
166 case MoveOp::SIMD128:
167 emitSimd128Move(from, to);
168 break;
169 default:
170 MOZ_CRASH("Unexpected move type");
171 }
172 }
173 }
174
175 MoveEmitterX86::~MoveEmitterX86() { assertDone(); }
176
177 Address MoveEmitterX86::cycleSlot() {
178 if (pushedAtCycle_ == -1) {
179 // Reserve stack for cycle resolution
180 static_assert(SpillSlotSize == 16);
181 masm.reserveStack(SpillSlotSize);
182 pushedAtCycle_ = masm.framePushed();
183 }
184
185 return Address(StackPointer, masm.framePushed() - pushedAtCycle_);
186 }
187
188 Address MoveEmitterX86::toAddress(const MoveOperand& operand) const {
189 if (operand.base() != StackPointer) {
190 return Address(operand.base(), operand.disp());
191 }
192
193 MOZ_ASSERT(operand.disp() >= 0);
194
195 // Otherwise, the stack offset may need to be adjusted.
196 return Address(StackPointer,
197 operand.disp() + (masm.framePushed() - pushedAtStart_));
198 }
199
200 // Warning, do not use the resulting operand with pop instructions, since they
201 // compute the effective destination address after altering the stack pointer.
202 // Use toPopOperand if an Operand is needed for a pop.
203 Operand MoveEmitterX86::toOperand(const MoveOperand& operand) const {
204 if (operand.isMemoryOrEffectiveAddress()) {
205 return Operand(toAddress(operand));
206 }
207 if (operand.isGeneralReg()) {
208 return Operand(operand.reg());
209 }
210
211 MOZ_ASSERT(operand.isFloatReg());
212 return Operand(operand.floatReg());
213 }
214
215 // This is the same as toOperand except that it computes an Operand suitable for
216 // use in a pop.
217 Operand MoveEmitterX86::toPopOperand(const MoveOperand& operand) const {
218 if (operand.isMemory()) {
219 if (operand.base() != StackPointer) {
220 return Operand(operand.base(), operand.disp());
221 }
222
223 MOZ_ASSERT(operand.disp() >= 0);
224
225 // Otherwise, the stack offset may need to be adjusted.
226 // Note the adjustment by the stack slot here, to offset for the fact that
227 // pop computes its effective address after incrementing the stack pointer.
228 return Operand(
229 StackPointer,
230 operand.disp() + (masm.framePushed() - sizeof(void*) - pushedAtStart_));
231 }
232 if (operand.isGeneralReg()) {
233 return Operand(operand.reg());
234 }
235
236 MOZ_ASSERT(operand.isFloatReg());
237 return Operand(operand.floatReg());
238 }
239
240 void MoveEmitterX86::breakCycle(const MoveOperand& to, MoveOp::Type type) {
241 // There is some pattern:
242 // (A -> B)
243 // (B -> A)
244 //
245 // This case handles (A -> B), which we reach first. We save B, then allow
246 // the original move to continue.
247 switch (type) {
248 case MoveOp::SIMD128:
249 if (to.isMemory()) {
250 ScratchSimd128Scope scratch(masm);
251 masm.loadUnalignedSimd128(toAddress(to), scratch);
252 masm.storeUnalignedSimd128(scratch, cycleSlot());
253 } else {
254 masm.storeUnalignedSimd128(to.floatReg(), cycleSlot());
255 }
256 break;
257 case MoveOp::FLOAT32:
258 if (to.isMemory()) {
259 ScratchFloat32Scope scratch(masm);
260 masm.loadFloat32(toAddress(to), scratch);
261 masm.storeFloat32(scratch, cycleSlot());
262 } else {
263 masm.storeFloat32(to.floatReg(), cycleSlot());
264 }
265 break;
266 case MoveOp::DOUBLE:
267 if (to.isMemory()) {
268 ScratchDoubleScope scratch(masm);
269 masm.loadDouble(toAddress(to), scratch);
270 masm.storeDouble(scratch, cycleSlot());
271 } else {
272 masm.storeDouble(to.floatReg(), cycleSlot());
273 }
274 break;
275 case MoveOp::INT32:
276 #ifdef JS_CODEGEN_X64
277 // x64 can't pop to a 32-bit destination, so don't push.
278 if (to.isMemory()) {
279 masm.load32(toAddress(to), ScratchReg);
280 masm.store32(ScratchReg, cycleSlot());
281 } else {
282 masm.store32(to.reg(), cycleSlot());
283 }
284 break;
285 #endif
286 case MoveOp::GENERAL:
287 masm.Push(toOperand(to));
288 break;
289 default:
290 MOZ_CRASH("Unexpected move type");
291 }
292 }
293
294 void MoveEmitterX86::completeCycle(const MoveOperand& to, MoveOp::Type type) {
295 // There is some pattern:
296 // (A -> B)
297 // (B -> A)
298 //
299 // This case handles (B -> A), which we reach last. We emit a move from the
300 // saved value of B, to A.
301 switch (type) {
302 case MoveOp::SIMD128:
303 MOZ_ASSERT(pushedAtCycle_ != -1);
304 MOZ_ASSERT(pushedAtCycle_ - pushedAtStart_ >= Simd128DataSize);
305 if (to.isMemory()) {
306 ScratchSimd128Scope scratch(masm);
307 masm.loadUnalignedSimd128(cycleSlot(), scratch);
308 masm.storeUnalignedSimd128(scratch, toAddress(to));
309 } else {
310 masm.loadUnalignedSimd128(cycleSlot(), to.floatReg());
311 }
312 break;
313 case MoveOp::FLOAT32:
314 MOZ_ASSERT(pushedAtCycle_ != -1);
315 MOZ_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(float));
316 if (to.isMemory()) {
317 ScratchFloat32Scope scratch(masm);
318 masm.loadFloat32(cycleSlot(), scratch);
319 masm.storeFloat32(scratch, toAddress(to));
320 } else {
321 masm.loadFloat32(cycleSlot(), to.floatReg());
322 }
323 break;
324 case MoveOp::DOUBLE:
325 MOZ_ASSERT(pushedAtCycle_ != -1);
326 MOZ_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(double));
327 if (to.isMemory()) {
328 ScratchDoubleScope scratch(masm);
329 masm.loadDouble(cycleSlot(), scratch);
330 masm.storeDouble(scratch, toAddress(to));
331 } else {
332 masm.loadDouble(cycleSlot(), to.floatReg());
333 }
334 break;
335 case MoveOp::INT32:
336 #ifdef JS_CODEGEN_X64
337 MOZ_ASSERT(pushedAtCycle_ != -1);
338 MOZ_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(int32_t));
339 // x64 can't pop to a 32-bit destination.
340 if (to.isMemory()) {
341 masm.load32(cycleSlot(), ScratchReg);
342 masm.store32(ScratchReg, toAddress(to));
343 } else {
344 masm.load32(cycleSlot(), to.reg());
345 }
346 break;
347 #endif
348 case MoveOp::GENERAL:
349 MOZ_ASSERT(masm.framePushed() - pushedAtStart_ >= sizeof(intptr_t));
350 masm.Pop(toPopOperand(to));
351 break;
352 default:
353 MOZ_CRASH("Unexpected move type");
354 }
355 }
356
357 void MoveEmitterX86::emitInt32Move(const MoveOperand& from,
358 const MoveOperand& to,
359 const MoveResolver& moves, size_t i) {
360 if (from.isGeneralReg()) {
361 masm.move32(from.reg(), toOperand(to));
362 } else if (to.isGeneralReg()) {
363 MOZ_ASSERT(from.isMemory());
364 masm.load32(toAddress(from), to.reg());
365 } else {
366 // Memory to memory gpr move.
367 MOZ_ASSERT(from.isMemory());
368 Maybe<Register> reg = findScratchRegister(moves, i);
369 if (reg.isSome()) {
370 masm.load32(toAddress(from), reg.value());
371 masm.move32(reg.value(), toOperand(to));
372 } else {
373 // No scratch register available; bounce it off the stack.
374 masm.Push(toOperand(from));
375 masm.Pop(toPopOperand(to));
376 }
377 }
378 }
379
380 void MoveEmitterX86::emitGeneralMove(const MoveOperand& from,
381 const MoveOperand& to,
382 const MoveResolver& moves, size_t i) {
383 if (from.isGeneralReg()) {
384 masm.mov(from.reg(), toOperand(to));
385 } else if (to.isGeneralReg()) {
386 MOZ_ASSERT(from.isMemoryOrEffectiveAddress());
387 if (from.isMemory()) {
388 masm.loadPtr(toAddress(from), to.reg());
389 } else {
390 masm.lea(toOperand(from), to.reg());
391 }
392 } else if (from.isMemory()) {
393 // Memory to memory gpr move.
394 Maybe<Register> reg = findScratchRegister(moves, i);
395 if (reg.isSome()) {
396 masm.loadPtr(toAddress(from), reg.value());
397 masm.mov(reg.value(), toOperand(to));
398 } else {
399 // No scratch register available; bounce it off the stack.
400 masm.Push(toOperand(from));
401 masm.Pop(toPopOperand(to));
402 }
403 } else {
404 // Effective address to memory move.
405 MOZ_ASSERT(from.isEffectiveAddress());
406 Maybe<Register> reg = findScratchRegister(moves, i);
407 if (reg.isSome()) {
408 masm.lea(toOperand(from), reg.value());
409 masm.mov(reg.value(), toOperand(to));
410 } else {
411 // This is tricky without a scratch reg. We can't do an lea. Bounce the
412 // base register off the stack, then add the offset in place. Note that
413 // this clobbers FLAGS!
414 masm.Push(from.base());
415 masm.Pop(toPopOperand(to));
416 MOZ_ASSERT(to.isMemoryOrEffectiveAddress());
417 masm.addPtr(Imm32(from.disp()), toAddress(to));
418 }
419 }
420 }
421
422 void MoveEmitterX86::emitFloat32Move(const MoveOperand& from,
423 const MoveOperand& to) {
424 MOZ_ASSERT_IF(from.isFloatReg(), from.floatReg().isSingle());
425 MOZ_ASSERT_IF(to.isFloatReg(), to.floatReg().isSingle());
426
427 if (from.isFloatReg()) {
428 if (to.isFloatReg()) {
429 masm.moveFloat32(from.floatReg(), to.floatReg());
430 } else {
431 masm.storeFloat32(from.floatReg(), toAddress(to));
432 }
433 } else if (to.isFloatReg()) {
434 masm.loadFloat32(toAddress(from), to.floatReg());
435 } else {
436 // Memory to memory move.
437 MOZ_ASSERT(from.isMemory());
438 ScratchFloat32Scope scratch(masm);
439 masm.loadFloat32(toAddress(from), scratch);
440 masm.storeFloat32(scratch, toAddress(to));
441 }
442 }
443
444 void MoveEmitterX86::emitDoubleMove(const MoveOperand& from,
445 const MoveOperand& to) {
446 MOZ_ASSERT_IF(from.isFloatReg(), from.floatReg().isDouble());
447 MOZ_ASSERT_IF(to.isFloatReg(), to.floatReg().isDouble());
448
449 if (from.isFloatReg()) {
450 if (to.isFloatReg()) {
451 masm.moveDouble(from.floatReg(), to.floatReg());
452 } else {
453 masm.storeDouble(from.floatReg(), toAddress(to));
454 }
455 } else if (to.isFloatReg()) {
456 masm.loadDouble(toAddress(from), to.floatReg());
457 } else {
458 // Memory to memory move.
459 MOZ_ASSERT(from.isMemory());
460 ScratchDoubleScope scratch(masm);
461 masm.loadDouble(toAddress(from), scratch);
462 masm.storeDouble(scratch, toAddress(to));
463 }
464 }
465
466 void MoveEmitterX86::emitSimd128Move(const MoveOperand& from,
467 const MoveOperand& to) {
468 MOZ_ASSERT_IF(from.isFloatReg(), from.floatReg().isSimd128());
469 MOZ_ASSERT_IF(to.isFloatReg(), to.floatReg().isSimd128());
470
471 if (from.isFloatReg()) {
472 if (to.isFloatReg()) {
473 masm.moveSimd128(from.floatReg(), to.floatReg());
474 } else {
475 masm.storeUnalignedSimd128(from.floatReg(), toAddress(to));
476 }
477 } else if (to.isFloatReg()) {
478 masm.loadUnalignedSimd128(toAddress(from), to.floatReg());
479 } else {
480 // Memory to memory move.
481 MOZ_ASSERT(from.isMemory());
482 ScratchSimd128Scope scratch(masm);
483 masm.loadUnalignedSimd128(toAddress(from), scratch);
484 masm.storeUnalignedSimd128(scratch, toAddress(to));
485 }
486 }
487
488 void MoveEmitterX86::assertDone() { MOZ_ASSERT(!inCycle_); }
489
490 void MoveEmitterX86::finish() {
491 assertDone();
492
493 masm.freeStack(masm.framePushed() - pushedAtStart_);
494 }
495
496 Maybe<Register> MoveEmitterX86::findScratchRegister(const MoveResolver& moves,
497 size_t initial) {
498 #ifdef JS_CODEGEN_X86
499 if (scratchRegister_.isSome()) {
500 return scratchRegister_;
501 }
502
503 // All registers are either in use by this move group or are live
504 // afterwards. Look through the remaining moves for a register which is
505 // clobbered before it is used, and is thus dead at this point.
506 AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
507 for (size_t i = initial; i < moves.numMoves(); i++) {
508 const MoveOp& move = moves.getMove(i);
509 if (move.from().isGeneralReg()) {
510 regs.takeUnchecked(move.from().reg());
511 } else if (move.from().isMemoryOrEffectiveAddress()) {
512 regs.takeUnchecked(move.from().base());
513 }
514 if (move.to().isGeneralReg()) {
515 if (i != initial && !move.isCycleBegin() && regs.has(move.to().reg())) {
516 return mozilla::Some(move.to().reg());
517 }
518 regs.takeUnchecked(move.to().reg());
519 } else if (move.to().isMemoryOrEffectiveAddress()) {
520 regs.takeUnchecked(move.to().base());
521 }
522 }
523
524 return mozilla::Nothing();
525 #else
526 return mozilla::Some(ScratchReg);
527 #endif
528 }