Commit 39c39b54 authored by zhengxing.li's avatar zhengxing.li Committed by Commit bot

X87: [ia32] Byte and word memory operands in ia32 cmp/test.

  port 3dd3beb0 (r35199)

  original commit message:
  Currently, if the size of two cmp or test operands is a byte or a word, we sign-extend or zero-extend each of them into a 32-bit register before doing the comparison, even when the conditions
  for the use of a memory operand are met.

  This CL makes it possible to load only one of them into a register and address the other as a memory operand.

  The tricky bit is that, unlike as in the x64 counterpart http://crrev.com/1780193003, not all registers can be accessed as bytes.

BUG=

Review URL: https://codereview.chromium.org/1883373002

Cr-Commit-Position: refs/heads/master@{#35508}
parent fd936fac
......@@ -337,6 +337,33 @@ class OutOfLineRecordWrite final : public OutOfLineCode {
__ bind(&done); \
} while (false)
#define ASSEMBLE_COMPARE(asm_instr) \
do { \
if (AddressingModeField::decode(instr->opcode()) != kMode_None) { \
size_t index = 0; \
Operand left = i.MemoryOperand(&index); \
if (HasImmediateInput(instr, index)) { \
__ asm_instr(left, i.InputImmediate(index)); \
} else { \
__ asm_instr(left, i.InputRegister(index)); \
} \
} else { \
if (HasImmediateInput(instr, 1)) { \
if (instr->InputAt(0)->IsRegister()) { \
__ asm_instr(i.InputRegister(0), i.InputImmediate(1)); \
} else { \
__ asm_instr(i.InputOperand(0), i.InputImmediate(1)); \
} \
} else { \
if (instr->InputAt(1)->IsRegister()) { \
__ asm_instr(i.InputRegister(0), i.InputRegister(1)); \
} else { \
__ asm_instr(i.InputRegister(0), i.InputOperand(1)); \
} \
} \
} \
} while (0)
void CodeGenerator::AssembleDeconstructFrame() {
__ mov(esp, ebp);
__ pop(ebp);
......@@ -677,38 +704,22 @@ void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
}
break;
case kX87Cmp:
if (AddressingModeField::decode(instr->opcode()) != kMode_None) {
size_t index = 0;
Operand operand = i.MemoryOperand(&index);
if (HasImmediateInput(instr, index)) {
__ cmp(operand, i.InputImmediate(index));
} else {
__ cmp(operand, i.InputRegister(index));
}
} else {
if (HasImmediateInput(instr, 1)) {
__ cmp(i.InputOperand(0), i.InputImmediate(1));
} else {
__ cmp(i.InputRegister(0), i.InputOperand(1));
}
}
ASSEMBLE_COMPARE(cmp);
break;
case kX87Cmp16:
ASSEMBLE_COMPARE(cmpw);
break;
case kX87Cmp8:
ASSEMBLE_COMPARE(cmpb);
break;
case kX87Test:
if (AddressingModeField::decode(instr->opcode()) != kMode_None) {
size_t index = 0;
Operand operand = i.MemoryOperand(&index);
if (HasImmediateInput(instr, index)) {
__ test(operand, i.InputImmediate(index));
} else {
__ test(i.InputRegister(index), operand);
}
} else {
if (HasImmediateInput(instr, 1)) {
__ test(i.InputOperand(0), i.InputImmediate(1));
} else {
__ test(i.InputRegister(0), i.InputOperand(1));
}
}
ASSEMBLE_COMPARE(test);
break;
case kX87Test16:
ASSEMBLE_COMPARE(test_w);
break;
case kX87Test8:
ASSEMBLE_COMPARE(test_b);
break;
case kX87Imul:
if (HasImmediateInput(instr, 1)) {
......
......@@ -17,7 +17,11 @@ namespace compiler {
V(X87Add) \
V(X87And) \
V(X87Cmp) \
V(X87Cmp16) \
V(X87Cmp8) \
V(X87Test) \
V(X87Test16) \
V(X87Test8) \
V(X87Or) \
V(X87Xor) \
V(X87Sub) \
......
......@@ -27,6 +27,30 @@ class X87OperandGenerator final : public OperandGenerator {
return DefineAsRegister(node);
}
bool CanBeMemoryOperand(InstructionCode opcode, Node* node, Node* input) {
if (input->opcode() != IrOpcode::kLoad ||
!selector()->CanCover(node, input)) {
return false;
}
MachineRepresentation rep =
LoadRepresentationOf(input->op()).representation();
switch (opcode) {
case kX87Cmp:
case kX87Test:
return rep == MachineRepresentation::kWord32 ||
rep == MachineRepresentation::kTagged;
case kX87Cmp16:
case kX87Test16:
return rep == MachineRepresentation::kWord16;
case kX87Cmp8:
case kX87Test8:
return rep == MachineRepresentation::kWord8;
default:
break;
}
return false;
}
InstructionOperand CreateImmediate(int imm) {
return sequence()->AddImmediate(Constant(imm));
}
......@@ -1119,21 +1143,6 @@ void VisitCompareWithMemoryOperand(InstructionSelector* selector,
}
}
// Determines if {input} of {node} can be replaced by a memory operand.
bool CanUseMemoryOperand(InstructionSelector* selector, InstructionCode opcode,
Node* node, Node* input) {
if (input->opcode() != IrOpcode::kLoad || !selector->CanCover(node, input)) {
return false;
}
MachineRepresentation load_representation =
LoadRepresentationOf(input->op()).representation();
if (load_representation == MachineRepresentation::kWord32 ||
load_representation == MachineRepresentation::kTagged) {
return opcode == kX87Cmp || opcode == kX87Test;
}
return false;
}
// Shared routine for multiple compare operations.
void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
InstructionOperand left, InstructionOperand right,
......@@ -1164,6 +1173,36 @@ void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont);
}
// Tries to match the size of the given opcode to that of the operands, if
// possible.
InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left,
Node* right) {
if (opcode != kX87Cmp && opcode != kX87Test) {
return opcode;
}
// Currently, if one of the two operands is not a Load, we don't know what its
// machine representation is, so we bail out.
// TODO(epertoso): we can probably get some size information out of immediates
// and phi nodes.
if (left->opcode() != IrOpcode::kLoad || right->opcode() != IrOpcode::kLoad) {
return opcode;
}
// If the load representations don't match, both operands will be
// zero/sign-extended to 32bit.
LoadRepresentation left_representation = LoadRepresentationOf(left->op());
if (left_representation != LoadRepresentationOf(right->op())) {
return opcode;
}
switch (left_representation.representation()) {
case MachineRepresentation::kBit:
case MachineRepresentation::kWord8:
return opcode == kX87Cmp ? kX87Cmp8 : kX87Test8;
case MachineRepresentation::kWord16:
return opcode == kX87Cmp ? kX87Cmp16 : kX87Test16;
default:
return opcode;
}
}
// Shared routine for multiple float32 compare operations (inputs commuted).
void VisitFloat32Compare(InstructionSelector* selector, Node* node,
......@@ -1213,15 +1252,22 @@ void VisitWordCompare(InstructionSelector* selector, Node* node,
Node* left = node->InputAt(0);
Node* right = node->InputAt(1);
// If one of the two inputs is an immediate, make sure it's on the right.
if (!g.CanBeImmediate(right) && g.CanBeImmediate(left)) {
InstructionCode narrowed_opcode = TryNarrowOpcodeSize(opcode, left, right);
// If one of the two inputs is an immediate, make sure it's on the right, or
// if one of the two inputs is a memory operand, make sure it's on the left.
if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) ||
(g.CanBeMemoryOperand(narrowed_opcode, node, right) &&
!g.CanBeMemoryOperand(narrowed_opcode, node, left))) {
if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute();
std::swap(left, right);
}
// Match immediates on right side of comparison.
if (g.CanBeImmediate(right)) {
if (CanUseMemoryOperand(selector, opcode, node, left)) {
if (g.CanBeMemoryOperand(opcode, node, left)) {
// TODO(epertoso): we should use `narrowed_opcode' here once we match
// immediates too.
return VisitCompareWithMemoryOperand(selector, opcode, left,
g.UseImmediate(right), cont);
}
......@@ -1229,15 +1275,21 @@ void VisitWordCompare(InstructionSelector* selector, Node* node,
cont);
}
// Match memory operands on left side of comparison.
if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) {
bool needs_byte_register =
narrowed_opcode == kX87Test8 || narrowed_opcode == kX87Cmp8;
return VisitCompareWithMemoryOperand(
selector, narrowed_opcode, left,
needs_byte_register ? g.UseByteRegister(right) : g.UseRegister(right),
cont);
}
if (g.CanBeBetterLeftOperand(right)) {
if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute();
std::swap(left, right);
}
if (CanUseMemoryOperand(selector, opcode, node, left)) {
return VisitCompareWithMemoryOperand(selector, opcode, left,
g.UseRegister(right), cont);
}
return VisitCompare(selector, opcode, left, right, cont,
node->op()->HasProperty(Operator::kCommutative));
}
......
......@@ -1128,6 +1128,39 @@ void Assembler::test_b(const Operand& op, Immediate imm8) {
emit_b(imm8);
}
void Assembler::test_w(Register reg, Immediate imm16) {
DCHECK(imm16.is_int16() || imm16.is_uint16());
EnsureSpace ensure_space(this);
if (reg.is(eax)) {
EMIT(0xA9);
emit_w(imm16);
} else {
EMIT(0x66);
EMIT(0xF7);
EMIT(0xc0 | reg.code());
emit_w(imm16);
}
}
void Assembler::test_w(Register reg, const Operand& op) {
EnsureSpace ensure_space(this);
EMIT(0x66);
EMIT(0x85);
emit_operand(reg, op);
}
void Assembler::test_w(const Operand& op, Immediate imm16) {
DCHECK(imm16.is_int16() || imm16.is_uint16());
if (op.is_reg_only()) {
test_w(op.reg(), imm16);
return;
}
EnsureSpace ensure_space(this);
EMIT(0x66);
EMIT(0xF7);
emit_operand(eax, op);
emit_w(imm16);
}
void Assembler::xor_(Register dst, int32_t imm32) {
EnsureSpace ensure_space(this);
......
......@@ -288,6 +288,9 @@ class Immediate BASE_EMBEDDED {
bool is_int16() const {
return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_);
}
bool is_uint16() const {
return v8::internal::is_uint16(x_) && RelocInfo::IsNone(rmode_);
}
private:
inline explicit Immediate(Label* value);
......@@ -764,10 +767,18 @@ class Assembler : public AssemblerBase {
void test(Register reg, const Immediate& imm);
void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); }
void test(Register reg, const Operand& op);
void test_b(Register reg, const Operand& op);
void test(const Operand& op, const Immediate& imm);
void test(const Operand& op, Register reg) { test(reg, op); }
void test_b(Register reg, const Operand& op);
void test_b(Register reg, Immediate imm8);
void test_b(const Operand& op, Immediate imm8);
void test_b(const Operand& op, Register reg) { test_b(reg, op); }
void test_b(Register dst, Register src) { test_b(dst, Operand(src)); }
void test_w(Register reg, const Operand& op);
void test_w(Register reg, Immediate imm16);
void test_w(const Operand& op, Immediate imm16);
void test_w(const Operand& op, Register reg) { test_w(reg, op); }
void test_w(Register dst, Register src) { test_w(dst, Operand(src)); }
void xor_(Register dst, int32_t imm32);
void xor_(Register dst, Register src) { xor_(dst, Operand(src)); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment