MIPS: Improve compare and branch combining.

TEST=
BUG=
R=paul.lind@imgtec.com

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

Cr-Commit-Position: refs/heads/master@{#25120}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@25120 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent dbc6c86b
...@@ -240,11 +240,10 @@ void CodeGenerator::AssembleArchInstruction(Instruction* instr) { ...@@ -240,11 +240,10 @@ void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
__ Ror(i.OutputRegister(), i.InputRegister(0), i.InputOperand(1)); __ Ror(i.OutputRegister(), i.InputRegister(0), i.InputOperand(1));
break; break;
case kMipsTst: case kMipsTst:
// Psuedo-instruction used for tst/branch. // Pseudo-instruction used for tst/branch. No opcode emitted here.
__ And(kCompareReg, i.InputRegister(0), i.InputOperand(1));
break; break;
case kMipsCmp: case kMipsCmp:
// Psuedo-instruction used for cmp/branch. No opcode emitted here. // Pseudo-instruction used for cmp/branch. No opcode emitted here.
break; break;
case kMipsMov: case kMipsMov:
// TODO(plind): Should we combine mov/li like this, or use separate instr? // TODO(plind): Should we combine mov/li like this, or use separate instr?
...@@ -418,7 +417,6 @@ void CodeGenerator::AssembleArchBranch(Instruction* instr, ...@@ -418,7 +417,6 @@ void CodeGenerator::AssembleArchBranch(Instruction* instr,
// not separated by other instructions. // not separated by other instructions.
if (instr->arch_opcode() == kMipsTst) { if (instr->arch_opcode() == kMipsTst) {
// The kMipsTst psuedo-instruction emits And to 'kCompareReg' register.
switch (condition) { switch (condition) {
case kNotEqual: case kNotEqual:
cc = ne; cc = ne;
...@@ -430,7 +428,8 @@ void CodeGenerator::AssembleArchBranch(Instruction* instr, ...@@ -430,7 +428,8 @@ void CodeGenerator::AssembleArchBranch(Instruction* instr,
UNSUPPORTED_COND(kMipsTst, condition); UNSUPPORTED_COND(kMipsTst, condition);
break; break;
} }
__ Branch(tlabel, cc, kCompareReg, Operand(zero_reg)); __ And(at, i.InputRegister(0), i.InputOperand(1));
__ Branch(tlabel, cc, at, Operand(zero_reg));
} else if (instr->arch_opcode() == kMipsAddOvf || } else if (instr->arch_opcode() == kMipsAddOvf ||
instr->arch_opcode() == kMipsSubOvf) { instr->arch_opcode() == kMipsSubOvf) {
...@@ -557,7 +556,6 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr, ...@@ -557,7 +556,6 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr,
// TODO(plind): Add CHECK() to ensure that test/cmp and this branch were // TODO(plind): Add CHECK() to ensure that test/cmp and this branch were
// not separated by other instructions. // not separated by other instructions.
if (instr->arch_opcode() == kMipsTst) { if (instr->arch_opcode() == kMipsTst) {
// The kMipsTst psuedo-instruction emits And to 'kCompareReg' register.
switch (condition) { switch (condition) {
case kNotEqual: case kNotEqual:
cc = ne; cc = ne;
...@@ -569,7 +567,8 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr, ...@@ -569,7 +567,8 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr,
UNSUPPORTED_COND(kMipsTst, condition); UNSUPPORTED_COND(kMipsTst, condition);
break; break;
} }
__ Branch(USE_DELAY_SLOT, &done, cc, kCompareReg, Operand(zero_reg)); __ And(at, i.InputRegister(0), i.InputOperand(1));
__ Branch(USE_DELAY_SLOT, &done, cc, at, Operand(zero_reg));
__ li(result, Operand(1)); // In delay slot. __ li(result, Operand(1)); // In delay slot.
} else if (instr->arch_opcode() == kMipsAddOvf || } else if (instr->arch_opcode() == kMipsAddOvf ||
......
...@@ -541,70 +541,114 @@ void VisitWordCompare(InstructionSelector* selector, Node* node, ...@@ -541,70 +541,114 @@ void VisitWordCompare(InstructionSelector* selector, Node* node,
VisitWordCompare(selector, node, kMipsCmp, cont, false); VisitWordCompare(selector, node, kMipsCmp, cont, false);
} }
} // namespace
void VisitWordTest(InstructionSelector* selector, Node* node, // Shared routine for word comparisons against zero.
FlagsContinuation* cont) { void VisitWordCompareZero(InstructionSelector* selector, Node* user,
Node* value, FlagsContinuation* cont) {
while (selector->CanCover(user, value)) {
switch (value->opcode()) {
case IrOpcode::kWord32Equal: {
// Combine with comparisons against 0 by simply inverting the
// continuation.
Int32BinopMatcher m(value);
if (m.right().Is(0)) {
user = value;
value = m.left().node();
cont->Negate();
continue;
}
cont->OverwriteAndNegateIfEqual(kEqual);
return VisitWordCompare(selector, value, cont);
}
case IrOpcode::kInt32LessThan:
cont->OverwriteAndNegateIfEqual(kSignedLessThan);
return VisitWordCompare(selector, value, cont);
case IrOpcode::kInt32LessThanOrEqual:
cont->OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
return VisitWordCompare(selector, value, cont);
case IrOpcode::kUint32LessThan:
cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
return VisitWordCompare(selector, value, cont);
case IrOpcode::kUint32LessThanOrEqual:
cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
return VisitWordCompare(selector, value, cont);
case IrOpcode::kFloat64Equal:
cont->OverwriteAndNegateIfEqual(kUnorderedEqual);
return VisitFloat64Compare(selector, value, cont);
case IrOpcode::kFloat64LessThan:
cont->OverwriteAndNegateIfEqual(kUnorderedLessThan);
return VisitFloat64Compare(selector, value, cont);
case IrOpcode::kFloat64LessThanOrEqual:
cont->OverwriteAndNegateIfEqual(kUnorderedLessThanOrEqual);
return VisitFloat64Compare(selector, value, cont);
case IrOpcode::kProjection:
// Check if this is the overflow output projection of an
// <Operation>WithOverflow node.
if (OpParameter<size_t>(value) == 1u) {
// We cannot combine the <Operation>WithOverflow with this branch
// unless the 0th projection (the use of the actual value of the
// <Operation> is either NULL, which means there's no use of the
// actual value, or was already defined, which means it is scheduled
// *AFTER* this branch).
Node* const node = value->InputAt(0);
Node* const result = node->FindProjection(0);
if (!result || selector->IsDefined(result)) {
switch (node->opcode()) {
case IrOpcode::kInt32AddWithOverflow:
cont->OverwriteAndNegateIfEqual(kOverflow);
return VisitBinop(selector, node, kMipsAddOvf, cont);
case IrOpcode::kInt32SubWithOverflow:
cont->OverwriteAndNegateIfEqual(kOverflow);
return VisitBinop(selector, node, kMipsSubOvf, cont);
default:
break;
}
}
}
break;
case IrOpcode::kWord32And:
return VisitWordCompare(selector, value, kMipsTst, cont, true);
default:
break;
}
break;
}
// Continuation could not be combined with a compare, emit compare against 0.
MipsOperandGenerator g(selector); MipsOperandGenerator g(selector);
// kMipsTst is a pseudo-instruction to do logical 'and' and leave the result InstructionCode const opcode = cont->Encode(kMipsCmp);
// in a dedicated tmp register. InstructionOperand* const value_operand = g.UseRegister(value);
VisitCompare(selector, kMipsTst, g.UseRegister(node), g.UseRegister(node), if (cont->IsBranch()) {
cont); selector->Emit(opcode, nullptr, value_operand, g.TempImmediate(0),
g.Label(cont->true_block()),
g.Label(cont->false_block()))->MarkAsControl();
} else {
selector->Emit(opcode, g.DefineAsRegister(cont->result()), value_operand,
g.TempImmediate(0));
}
} }
} // namespace
void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
BasicBlock* fbranch) { BasicBlock* fbranch) {
MipsOperandGenerator g(this);
Node* user = branch;
Node* value = branch->InputAt(0);
FlagsContinuation cont(kNotEqual, tbranch, fbranch); FlagsContinuation cont(kNotEqual, tbranch, fbranch);
// If we can fall through to the true block, invert the branch. // If we can fall through to the true block, invert the branch.
if (IsNextInAssemblyOrder(tbranch)) { if (IsNextInAssemblyOrder(tbranch)) {
cont.Negate(); cont.Negate();
cont.SwapBlocks(); cont.SwapBlocks();
} }
VisitWordCompareZero(this, branch, branch->InputAt(0), &cont);
// Try to combine with comparisons against 0 by simply inverting the branch.
while (CanCover(user, value) && value->opcode() == IrOpcode::kWord32Equal) {
Int32BinopMatcher m(value);
if (m.right().Is(0)) {
user = value;
value = m.left().node();
cont.Negate();
} else {
break;
}
}
// Try to combine the branch with a comparison.
if (CanCover(user, value)) {
switch (value->opcode()) {
case IrOpcode::kWord32And:
// TODO(plind): understand the significance of 'IR and' special case.
return VisitWordCompare(this, value, kMipsTst, &cont, true);
default:
break;
}
}
// Branch could not be combined with a compare, emit compare against 0.
return VisitWordTest(this, value, &cont);
} }
void InstructionSelector::VisitWord32Equal(Node* const node) { void InstructionSelector::VisitWord32Equal(Node* const node) {
Node* const user = node;
FlagsContinuation cont(kEqual, node); FlagsContinuation cont(kEqual, node);
Int32BinopMatcher m(user); Int32BinopMatcher m(node);
if (m.right().Is(0)) { if (m.right().Is(0)) {
Node* const value = m.left().node(); return VisitWordCompareZero(this, m.node(), m.left().node(), &cont);
return VisitWordTest(this, value, &cont);
} }
VisitWordCompare(this, node, &cont); VisitWordCompare(this, node, &cont);
} }
......
...@@ -2037,18 +2037,26 @@ void MacroAssembler::BranchShort(int16_t offset, Condition cond, Register rs, ...@@ -2037,18 +2037,26 @@ void MacroAssembler::BranchShort(int16_t offset, Condition cond, Register rs,
b(offset); b(offset);
break; break;
case eq: case eq:
// We don't want any other register but scratch clobbered. if (rt.imm32_ == 0) {
DCHECK(!scratch.is(rs)); beq(rs, zero_reg, offset);
r2 = scratch; } else {
li(r2, rt); // We don't want any other register but scratch clobbered.
beq(rs, r2, offset); DCHECK(!scratch.is(rs));
r2 = scratch;
li(r2, rt);
beq(rs, r2, offset);
}
break; break;
case ne: case ne:
// We don't want any other register but scratch clobbered. if (rt.imm32_ == 0) {
DCHECK(!scratch.is(rs)); bne(rs, zero_reg, offset);
r2 = scratch; } else {
li(r2, rt); // We don't want any other register but scratch clobbered.
bne(rs, r2, offset); DCHECK(!scratch.is(rs));
r2 = scratch;
li(r2, rt);
bne(rs, r2, offset);
}
break; break;
// Signed comparison. // Signed comparison.
case greater: case greater:
...@@ -2290,18 +2298,28 @@ void MacroAssembler::BranchShort(Label* L, Condition cond, Register rs, ...@@ -2290,18 +2298,28 @@ void MacroAssembler::BranchShort(Label* L, Condition cond, Register rs,
b(offset); b(offset);
break; break;
case eq: case eq:
DCHECK(!scratch.is(rs)); if (rt.imm32_ == 0) {
r2 = scratch; offset = shifted_branch_offset(L, false);
li(r2, rt); beq(rs, zero_reg, offset);
offset = shifted_branch_offset(L, false); } else {
beq(rs, r2, offset); DCHECK(!scratch.is(rs));
r2 = scratch;
li(r2, rt);
offset = shifted_branch_offset(L, false);
beq(rs, r2, offset);
}
break; break;
case ne: case ne:
DCHECK(!scratch.is(rs)); if (rt.imm32_ == 0) {
r2 = scratch; offset = shifted_branch_offset(L, false);
li(r2, rt); bne(rs, zero_reg, offset);
offset = shifted_branch_offset(L, false); } else {
bne(rs, r2, offset); DCHECK(!scratch.is(rs));
r2 = scratch;
li(r2, rt);
offset = shifted_branch_offset(L, false);
bne(rs, r2, offset);
}
break; break;
// Signed comparison. // Signed comparison.
case greater: case greater:
......
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