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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "jit/shared/MoveEmitter-x86-shared.h"
michael@0 8
michael@0 9 using namespace js;
michael@0 10 using namespace js::jit;
michael@0 11
michael@0 12 MoveEmitterX86::MoveEmitterX86(MacroAssemblerSpecific &masm)
michael@0 13 : inCycle_(false),
michael@0 14 masm(masm),
michael@0 15 pushedAtCycle_(-1)
michael@0 16 {
michael@0 17 pushedAtStart_ = masm.framePushed();
michael@0 18 }
michael@0 19
michael@0 20 // Examine the cycle in moves starting at position i. Determine if it's a
michael@0 21 // simple cycle consisting of all register-to-register moves in a single class,
michael@0 22 // and whether it can be implemented entirely by swaps.
michael@0 23 size_t
michael@0 24 MoveEmitterX86::characterizeCycle(const MoveResolver &moves, size_t i,
michael@0 25 bool *allGeneralRegs, bool *allFloatRegs)
michael@0 26 {
michael@0 27 size_t swapCount = 0;
michael@0 28
michael@0 29 for (size_t j = i; ; j++) {
michael@0 30 const MoveOp &move = moves.getMove(j);
michael@0 31
michael@0 32 // If it isn't a cycle of registers of the same kind, we won't be able
michael@0 33 // to optimize it.
michael@0 34 if (!move.to().isGeneralReg())
michael@0 35 *allGeneralRegs = false;
michael@0 36 if (!move.to().isFloatReg())
michael@0 37 *allFloatRegs = false;
michael@0 38 if (!*allGeneralRegs && !*allFloatRegs)
michael@0 39 return -1;
michael@0 40
michael@0 41 // Stop iterating when we see the last one.
michael@0 42 if (j != i && move.isCycleEnd())
michael@0 43 break;
michael@0 44
michael@0 45 // Check that this move is actually part of the cycle. This is
michael@0 46 // over-conservative when there are multiple reads from the same source,
michael@0 47 // but that's expected to be rare.
michael@0 48 if (move.from() != moves.getMove(j + 1).to()) {
michael@0 49 *allGeneralRegs = false;
michael@0 50 *allFloatRegs = false;
michael@0 51 return -1;
michael@0 52 }
michael@0 53
michael@0 54 swapCount++;
michael@0 55 }
michael@0 56
michael@0 57 // Check that the last move cycles back to the first move.
michael@0 58 const MoveOp &move = moves.getMove(i + swapCount);
michael@0 59 if (move.from() != moves.getMove(i).to()) {
michael@0 60 *allGeneralRegs = false;
michael@0 61 *allFloatRegs = false;
michael@0 62 return -1;
michael@0 63 }
michael@0 64
michael@0 65 return swapCount;
michael@0 66 }
michael@0 67
michael@0 68 // If we can emit optimized code for the cycle in moves starting at position i,
michael@0 69 // do so, and return true.
michael@0 70 bool
michael@0 71 MoveEmitterX86::maybeEmitOptimizedCycle(const MoveResolver &moves, size_t i,
michael@0 72 bool allGeneralRegs, bool allFloatRegs, size_t swapCount)
michael@0 73 {
michael@0 74 if (allGeneralRegs && swapCount <= 2) {
michael@0 75 // Use x86's swap-integer-registers instruction if we only have a few
michael@0 76 // swaps. (x86 also has a swap between registers and memory but it's
michael@0 77 // slow.)
michael@0 78 for (size_t k = 0; k < swapCount; k++)
michael@0 79 masm.xchg(moves.getMove(i + k).to().reg(), moves.getMove(i + k + 1).to().reg());
michael@0 80 return true;
michael@0 81 }
michael@0 82
michael@0 83 if (allFloatRegs && swapCount == 1) {
michael@0 84 // There's no xchg for xmm registers, but if we only need a single swap,
michael@0 85 // it's cheap to do an XOR swap.
michael@0 86 FloatRegister a = moves.getMove(i).to().floatReg();
michael@0 87 FloatRegister b = moves.getMove(i + 1).to().floatReg();
michael@0 88 masm.xorpd(a, b);
michael@0 89 masm.xorpd(b, a);
michael@0 90 masm.xorpd(a, b);
michael@0 91 return true;
michael@0 92 }
michael@0 93
michael@0 94 return false;
michael@0 95 }
michael@0 96
michael@0 97 void
michael@0 98 MoveEmitterX86::emit(const MoveResolver &moves)
michael@0 99 {
michael@0 100 for (size_t i = 0; i < moves.numMoves(); i++) {
michael@0 101 const MoveOp &move = moves.getMove(i);
michael@0 102 const MoveOperand &from = move.from();
michael@0 103 const MoveOperand &to = move.to();
michael@0 104
michael@0 105 if (move.isCycleEnd()) {
michael@0 106 JS_ASSERT(inCycle_);
michael@0 107 completeCycle(to, move.type());
michael@0 108 inCycle_ = false;
michael@0 109 continue;
michael@0 110 }
michael@0 111
michael@0 112 if (move.isCycleBegin()) {
michael@0 113 JS_ASSERT(!inCycle_);
michael@0 114
michael@0 115 // Characterize the cycle.
michael@0 116 bool allGeneralRegs = true, allFloatRegs = true;
michael@0 117 size_t swapCount = characterizeCycle(moves, i, &allGeneralRegs, &allFloatRegs);
michael@0 118
michael@0 119 // Attempt to optimize it to avoid using the stack.
michael@0 120 if (maybeEmitOptimizedCycle(moves, i, allGeneralRegs, allFloatRegs, swapCount)) {
michael@0 121 i += swapCount;
michael@0 122 continue;
michael@0 123 }
michael@0 124
michael@0 125 // Otherwise use the stack.
michael@0 126 breakCycle(to, move.endCycleType());
michael@0 127 inCycle_ = true;
michael@0 128 }
michael@0 129
michael@0 130 // A normal move which is not part of a cycle.
michael@0 131 switch (move.type()) {
michael@0 132 case MoveOp::FLOAT32:
michael@0 133 emitFloat32Move(from, to);
michael@0 134 break;
michael@0 135 case MoveOp::DOUBLE:
michael@0 136 emitDoubleMove(from, to);
michael@0 137 break;
michael@0 138 case MoveOp::INT32:
michael@0 139 emitInt32Move(from, to);
michael@0 140 break;
michael@0 141 case MoveOp::GENERAL:
michael@0 142 emitGeneralMove(from, to);
michael@0 143 break;
michael@0 144 default:
michael@0 145 MOZ_ASSUME_UNREACHABLE("Unexpected move type");
michael@0 146 }
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 MoveEmitterX86::~MoveEmitterX86()
michael@0 151 {
michael@0 152 assertDone();
michael@0 153 }
michael@0 154
michael@0 155 Address
michael@0 156 MoveEmitterX86::cycleSlot()
michael@0 157 {
michael@0 158 if (pushedAtCycle_ == -1) {
michael@0 159 // Reserve stack for cycle resolution
michael@0 160 masm.reserveStack(sizeof(double));
michael@0 161 pushedAtCycle_ = masm.framePushed();
michael@0 162 }
michael@0 163
michael@0 164 return Address(StackPointer, masm.framePushed() - pushedAtCycle_);
michael@0 165 }
michael@0 166
michael@0 167 Address
michael@0 168 MoveEmitterX86::toAddress(const MoveOperand &operand) const
michael@0 169 {
michael@0 170 if (operand.base() != StackPointer)
michael@0 171 return Address(operand.base(), operand.disp());
michael@0 172
michael@0 173 JS_ASSERT(operand.disp() >= 0);
michael@0 174
michael@0 175 // Otherwise, the stack offset may need to be adjusted.
michael@0 176 return Address(StackPointer, operand.disp() + (masm.framePushed() - pushedAtStart_));
michael@0 177 }
michael@0 178
michael@0 179 // Warning, do not use the resulting operand with pop instructions, since they
michael@0 180 // compute the effective destination address after altering the stack pointer.
michael@0 181 // Use toPopOperand if an Operand is needed for a pop.
michael@0 182 Operand
michael@0 183 MoveEmitterX86::toOperand(const MoveOperand &operand) const
michael@0 184 {
michael@0 185 if (operand.isMemoryOrEffectiveAddress())
michael@0 186 return Operand(toAddress(operand));
michael@0 187 if (operand.isGeneralReg())
michael@0 188 return Operand(operand.reg());
michael@0 189
michael@0 190 JS_ASSERT(operand.isFloatReg());
michael@0 191 return Operand(operand.floatReg());
michael@0 192 }
michael@0 193
michael@0 194 // This is the same as toOperand except that it computes an Operand suitable for
michael@0 195 // use in a pop.
michael@0 196 Operand
michael@0 197 MoveEmitterX86::toPopOperand(const MoveOperand &operand) const
michael@0 198 {
michael@0 199 if (operand.isMemory()) {
michael@0 200 if (operand.base() != StackPointer)
michael@0 201 return Operand(operand.base(), operand.disp());
michael@0 202
michael@0 203 JS_ASSERT(operand.disp() >= 0);
michael@0 204
michael@0 205 // Otherwise, the stack offset may need to be adjusted.
michael@0 206 // Note the adjustment by the stack slot here, to offset for the fact that pop
michael@0 207 // computes its effective address after incrementing the stack pointer.
michael@0 208 return Operand(StackPointer,
michael@0 209 operand.disp() + (masm.framePushed() - sizeof(void *) - pushedAtStart_));
michael@0 210 }
michael@0 211 if (operand.isGeneralReg())
michael@0 212 return Operand(operand.reg());
michael@0 213
michael@0 214 JS_ASSERT(operand.isFloatReg());
michael@0 215 return Operand(operand.floatReg());
michael@0 216 }
michael@0 217
michael@0 218 void
michael@0 219 MoveEmitterX86::breakCycle(const MoveOperand &to, MoveOp::Type type)
michael@0 220 {
michael@0 221 // There is some pattern:
michael@0 222 // (A -> B)
michael@0 223 // (B -> A)
michael@0 224 //
michael@0 225 // This case handles (A -> B), which we reach first. We save B, then allow
michael@0 226 // the original move to continue.
michael@0 227 switch (type) {
michael@0 228 case MoveOp::FLOAT32:
michael@0 229 if (to.isMemory()) {
michael@0 230 masm.loadFloat32(toAddress(to), ScratchFloatReg);
michael@0 231 masm.storeFloat32(ScratchFloatReg, cycleSlot());
michael@0 232 } else {
michael@0 233 masm.storeFloat32(to.floatReg(), cycleSlot());
michael@0 234 }
michael@0 235 break;
michael@0 236 case MoveOp::DOUBLE:
michael@0 237 if (to.isMemory()) {
michael@0 238 masm.loadDouble(toAddress(to), ScratchFloatReg);
michael@0 239 masm.storeDouble(ScratchFloatReg, cycleSlot());
michael@0 240 } else {
michael@0 241 masm.storeDouble(to.floatReg(), cycleSlot());
michael@0 242 }
michael@0 243 break;
michael@0 244 #ifdef JS_CODEGEN_X64
michael@0 245 case MoveOp::INT32:
michael@0 246 // x64 can't pop to a 32-bit destination, so don't push.
michael@0 247 if (to.isMemory()) {
michael@0 248 masm.load32(toAddress(to), ScratchReg);
michael@0 249 masm.store32(ScratchReg, cycleSlot());
michael@0 250 } else {
michael@0 251 masm.store32(to.reg(), cycleSlot());
michael@0 252 }
michael@0 253 break;
michael@0 254 #endif
michael@0 255 #ifndef JS_CODEGEN_X64
michael@0 256 case MoveOp::INT32:
michael@0 257 #endif
michael@0 258 case MoveOp::GENERAL:
michael@0 259 masm.Push(toOperand(to));
michael@0 260 break;
michael@0 261 default:
michael@0 262 MOZ_ASSUME_UNREACHABLE("Unexpected move type");
michael@0 263 }
michael@0 264 }
michael@0 265
michael@0 266 void
michael@0 267 MoveEmitterX86::completeCycle(const MoveOperand &to, MoveOp::Type type)
michael@0 268 {
michael@0 269 // There is some pattern:
michael@0 270 // (A -> B)
michael@0 271 // (B -> A)
michael@0 272 //
michael@0 273 // This case handles (B -> A), which we reach last. We emit a move from the
michael@0 274 // saved value of B, to A.
michael@0 275 switch (type) {
michael@0 276 case MoveOp::FLOAT32:
michael@0 277 JS_ASSERT(pushedAtCycle_ != -1);
michael@0 278 JS_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(float));
michael@0 279 if (to.isMemory()) {
michael@0 280 masm.loadFloat32(cycleSlot(), ScratchFloatReg);
michael@0 281 masm.storeFloat32(ScratchFloatReg, toAddress(to));
michael@0 282 } else {
michael@0 283 masm.loadFloat32(cycleSlot(), to.floatReg());
michael@0 284 }
michael@0 285 break;
michael@0 286 case MoveOp::DOUBLE:
michael@0 287 JS_ASSERT(pushedAtCycle_ != -1);
michael@0 288 JS_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(double));
michael@0 289 if (to.isMemory()) {
michael@0 290 masm.loadDouble(cycleSlot(), ScratchFloatReg);
michael@0 291 masm.storeDouble(ScratchFloatReg, toAddress(to));
michael@0 292 } else {
michael@0 293 masm.loadDouble(cycleSlot(), to.floatReg());
michael@0 294 }
michael@0 295 break;
michael@0 296 #ifdef JS_CODEGEN_X64
michael@0 297 case MoveOp::INT32:
michael@0 298 JS_ASSERT(pushedAtCycle_ != -1);
michael@0 299 JS_ASSERT(pushedAtCycle_ - pushedAtStart_ >= sizeof(int32_t));
michael@0 300 // x64 can't pop to a 32-bit destination.
michael@0 301 if (to.isMemory()) {
michael@0 302 masm.load32(cycleSlot(), ScratchReg);
michael@0 303 masm.store32(ScratchReg, toAddress(to));
michael@0 304 } else {
michael@0 305 masm.load32(cycleSlot(), to.reg());
michael@0 306 }
michael@0 307 break;
michael@0 308 #endif
michael@0 309 #ifndef JS_CODEGEN_X64
michael@0 310 case MoveOp::INT32:
michael@0 311 #endif
michael@0 312 case MoveOp::GENERAL:
michael@0 313 JS_ASSERT(masm.framePushed() - pushedAtStart_ >= sizeof(intptr_t));
michael@0 314 masm.Pop(toPopOperand(to));
michael@0 315 break;
michael@0 316 default:
michael@0 317 MOZ_ASSUME_UNREACHABLE("Unexpected move type");
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 void
michael@0 322 MoveEmitterX86::emitInt32Move(const MoveOperand &from, const MoveOperand &to)
michael@0 323 {
michael@0 324 if (from.isGeneralReg()) {
michael@0 325 masm.move32(from.reg(), toOperand(to));
michael@0 326 } else if (to.isGeneralReg()) {
michael@0 327 JS_ASSERT(from.isMemory());
michael@0 328 masm.load32(toAddress(from), to.reg());
michael@0 329 } else {
michael@0 330 // Memory to memory gpr move.
michael@0 331 JS_ASSERT(from.isMemory());
michael@0 332 #ifdef JS_CODEGEN_X64
michael@0 333 // x64 has a ScratchReg. Use it.
michael@0 334 masm.load32(toAddress(from), ScratchReg);
michael@0 335 masm.move32(ScratchReg, toOperand(to));
michael@0 336 #else
michael@0 337 // No ScratchReg; bounce it off the stack.
michael@0 338 masm.Push(toOperand(from));
michael@0 339 masm.Pop(toPopOperand(to));
michael@0 340 #endif
michael@0 341 }
michael@0 342 }
michael@0 343
michael@0 344 void
michael@0 345 MoveEmitterX86::emitGeneralMove(const MoveOperand &from, const MoveOperand &to)
michael@0 346 {
michael@0 347 if (from.isGeneralReg()) {
michael@0 348 masm.mov(from.reg(), toOperand(to));
michael@0 349 } else if (to.isGeneralReg()) {
michael@0 350 JS_ASSERT(from.isMemoryOrEffectiveAddress());
michael@0 351 if (from.isMemory())
michael@0 352 masm.loadPtr(toAddress(from), to.reg());
michael@0 353 else
michael@0 354 masm.lea(toOperand(from), to.reg());
michael@0 355 } else if (from.isMemory()) {
michael@0 356 // Memory to memory gpr move.
michael@0 357 #ifdef JS_CODEGEN_X64
michael@0 358 // x64 has a ScratchReg. Use it.
michael@0 359 masm.loadPtr(toAddress(from), ScratchReg);
michael@0 360 masm.mov(ScratchReg, toOperand(to));
michael@0 361 #else
michael@0 362 // No ScratchReg; bounce it off the stack.
michael@0 363 masm.Push(toOperand(from));
michael@0 364 masm.Pop(toPopOperand(to));
michael@0 365 #endif
michael@0 366 } else {
michael@0 367 // Effective address to memory move.
michael@0 368 JS_ASSERT(from.isEffectiveAddress());
michael@0 369 #ifdef JS_CODEGEN_X64
michael@0 370 // x64 has a ScratchReg. Use it.
michael@0 371 masm.lea(toOperand(from), ScratchReg);
michael@0 372 masm.mov(ScratchReg, toOperand(to));
michael@0 373 #else
michael@0 374 // This is tricky without a ScratchReg. We can't do an lea. Bounce the
michael@0 375 // base register off the stack, then add the offset in place. Note that
michael@0 376 // this clobbers FLAGS!
michael@0 377 masm.Push(from.base());
michael@0 378 masm.Pop(toPopOperand(to));
michael@0 379 masm.addPtr(Imm32(from.disp()), toOperand(to));
michael@0 380 #endif
michael@0 381 }
michael@0 382 }
michael@0 383
michael@0 384 void
michael@0 385 MoveEmitterX86::emitFloat32Move(const MoveOperand &from, const MoveOperand &to)
michael@0 386 {
michael@0 387 if (from.isFloatReg()) {
michael@0 388 if (to.isFloatReg())
michael@0 389 masm.moveFloat32(from.floatReg(), to.floatReg());
michael@0 390 else
michael@0 391 masm.storeFloat32(from.floatReg(), toAddress(to));
michael@0 392 } else if (to.isFloatReg()) {
michael@0 393 masm.loadFloat32(toAddress(from), to.floatReg());
michael@0 394 } else {
michael@0 395 // Memory to memory move.
michael@0 396 JS_ASSERT(from.isMemory());
michael@0 397 masm.loadFloat32(toAddress(from), ScratchFloatReg);
michael@0 398 masm.storeFloat32(ScratchFloatReg, toAddress(to));
michael@0 399 }
michael@0 400 }
michael@0 401
michael@0 402 void
michael@0 403 MoveEmitterX86::emitDoubleMove(const MoveOperand &from, const MoveOperand &to)
michael@0 404 {
michael@0 405 if (from.isFloatReg()) {
michael@0 406 if (to.isFloatReg())
michael@0 407 masm.moveDouble(from.floatReg(), to.floatReg());
michael@0 408 else
michael@0 409 masm.storeDouble(from.floatReg(), toAddress(to));
michael@0 410 } else if (to.isFloatReg()) {
michael@0 411 masm.loadDouble(toAddress(from), to.floatReg());
michael@0 412 } else {
michael@0 413 // Memory to memory move.
michael@0 414 JS_ASSERT(from.isMemory());
michael@0 415 masm.loadDouble(toAddress(from), ScratchFloatReg);
michael@0 416 masm.storeDouble(ScratchFloatReg, toAddress(to));
michael@0 417 }
michael@0 418 }
michael@0 419
michael@0 420 void
michael@0 421 MoveEmitterX86::assertDone()
michael@0 422 {
michael@0 423 JS_ASSERT(!inCycle_);
michael@0 424 }
michael@0 425
michael@0 426 void
michael@0 427 MoveEmitterX86::finish()
michael@0 428 {
michael@0 429 assertDone();
michael@0 430
michael@0 431 masm.freeStack(masm.framePushed() - pushedAtStart_);
michael@0 432 }
michael@0 433

mercurial