Commit 423201ea authored by bmeurer@chromium.org's avatar bmeurer@chromium.org

[x86] Select better left operand for comparisons.

R=dcarney@chromium.org

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

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24764 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 018e3979
...@@ -648,11 +648,9 @@ void InstructionSelector::VisitCall(Node* node) { ...@@ -648,11 +648,9 @@ void InstructionSelector::VisitCall(Node* node) {
// Shared routine for multiple compare operations. // Shared routine for multiple compare operations.
static inline void VisitCompare(InstructionSelector* selector, static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
InstructionCode opcode, InstructionOperand* left, InstructionOperand* right,
InstructionOperand* left, FlagsContinuation* cont) {
InstructionOperand* right,
FlagsContinuation* cont) {
IA32OperandGenerator g(selector); IA32OperandGenerator g(selector);
if (cont->IsBranch()) { if (cont->IsBranch()) {
selector->Emit(cont->Encode(opcode), NULL, left, right, selector->Emit(cont->Encode(opcode), NULL, left, right,
...@@ -667,22 +665,34 @@ static inline void VisitCompare(InstructionSelector* selector, ...@@ -667,22 +665,34 @@ static inline void VisitCompare(InstructionSelector* selector,
} }
// Shared routine for multiple compare operations.
static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
Node* left, Node* right, FlagsContinuation* cont,
bool commutative) {
IA32OperandGenerator g(selector);
if (commutative && g.CanBeBetterLeftOperand(right)) {
std::swap(left, right);
}
VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont);
}
// Shared routine for multiple word compare operations. // Shared routine for multiple word compare operations.
static inline void VisitWordCompare(InstructionSelector* selector, Node* node, static void VisitWordCompare(InstructionSelector* selector, Node* node,
InstructionCode opcode, InstructionCode opcode, FlagsContinuation* cont) {
FlagsContinuation* cont, bool commutative) {
IA32OperandGenerator g(selector); IA32OperandGenerator g(selector);
Node* left = node->InputAt(0); Node* const left = node->InputAt(0);
Node* right = node->InputAt(1); Node* const right = node->InputAt(1);
// Match immediates on left or right side of comparison. // Match immediates on left or right side of comparison.
if (g.CanBeImmediate(right)) { if (g.CanBeImmediate(right)) {
VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont); VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont);
} else if (g.CanBeImmediate(left)) { } else if (g.CanBeImmediate(left)) {
if (!commutative) cont->Commute(); if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute();
VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont); VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont);
} else { } else {
VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); VisitCompare(selector, opcode, left, right, cont,
node->op()->HasProperty(Operator::kCommutative));
} }
} }
...@@ -690,11 +700,8 @@ static inline void VisitWordCompare(InstructionSelector* selector, Node* node, ...@@ -690,11 +700,8 @@ static inline void VisitWordCompare(InstructionSelector* selector, Node* node,
// Shared routine for multiple float compare operations. // Shared routine for multiple float compare operations.
static void VisitFloat64Compare(InstructionSelector* selector, Node* node, static void VisitFloat64Compare(InstructionSelector* selector, Node* node,
FlagsContinuation* cont) { FlagsContinuation* cont) {
IA32OperandGenerator g(selector); VisitCompare(selector, kSSEFloat64Cmp, node->InputAt(0), node->InputAt(1),
Node* left = node->InputAt(0); cont, node->op()->HasProperty(Operator::kCommutative));
Node* right = node->InputAt(1);
VisitCompare(selector, kSSEFloat64Cmp, g.UseRegister(left), g.Use(right),
cont);
} }
...@@ -729,19 +736,19 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, ...@@ -729,19 +736,19 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
switch (value->opcode()) { switch (value->opcode()) {
case IrOpcode::kWord32Equal: case IrOpcode::kWord32Equal:
cont.OverwriteAndNegateIfEqual(kEqual); cont.OverwriteAndNegateIfEqual(kEqual);
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kInt32LessThan: case IrOpcode::kInt32LessThan:
cont.OverwriteAndNegateIfEqual(kSignedLessThan); cont.OverwriteAndNegateIfEqual(kSignedLessThan);
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kInt32LessThanOrEqual: case IrOpcode::kInt32LessThanOrEqual:
cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kUint32LessThan: case IrOpcode::kUint32LessThan:
cont.OverwriteAndNegateIfEqual(kUnsignedLessThan); cont.OverwriteAndNegateIfEqual(kUnsignedLessThan);
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kUint32LessThanOrEqual: case IrOpcode::kUint32LessThanOrEqual:
cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kFloat64Equal: case IrOpcode::kFloat64Equal:
cont.OverwriteAndNegateIfEqual(kUnorderedEqual); cont.OverwriteAndNegateIfEqual(kUnorderedEqual);
return VisitFloat64Compare(this, value, &cont); return VisitFloat64Compare(this, value, &cont);
...@@ -777,9 +784,9 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, ...@@ -777,9 +784,9 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
} }
break; break;
case IrOpcode::kInt32Sub: case IrOpcode::kInt32Sub:
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kWord32And: case IrOpcode::kWord32And:
return VisitWordCompare(this, value, kIA32Test, &cont, true); return VisitWordCompare(this, value, kIA32Test, &cont);
default: default:
break; break;
} }
...@@ -799,39 +806,39 @@ void InstructionSelector::VisitWord32Equal(Node* const node) { ...@@ -799,39 +806,39 @@ void InstructionSelector::VisitWord32Equal(Node* const node) {
if (CanCover(user, value)) { if (CanCover(user, value)) {
switch (value->opcode()) { switch (value->opcode()) {
case IrOpcode::kInt32Sub: case IrOpcode::kInt32Sub:
return VisitWordCompare(this, value, kIA32Cmp, &cont, false); return VisitWordCompare(this, value, kIA32Cmp, &cont);
case IrOpcode::kWord32And: case IrOpcode::kWord32And:
return VisitWordCompare(this, value, kIA32Test, &cont, true); return VisitWordCompare(this, value, kIA32Test, &cont);
default: default:
break; break;
} }
} }
} }
return VisitWordCompare(this, node, kIA32Cmp, &cont, false); VisitWordCompare(this, node, kIA32Cmp, &cont);
} }
void InstructionSelector::VisitInt32LessThan(Node* node) { void InstructionSelector::VisitInt32LessThan(Node* node) {
FlagsContinuation cont(kSignedLessThan, node); FlagsContinuation cont(kSignedLessThan, node);
return VisitWordCompare(this, node, kIA32Cmp, &cont, false); VisitWordCompare(this, node, kIA32Cmp, &cont);
} }
void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) { void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) {
FlagsContinuation cont(kSignedLessThanOrEqual, node); FlagsContinuation cont(kSignedLessThanOrEqual, node);
return VisitWordCompare(this, node, kIA32Cmp, &cont, false); VisitWordCompare(this, node, kIA32Cmp, &cont);
} }
void InstructionSelector::VisitUint32LessThan(Node* node) { void InstructionSelector::VisitUint32LessThan(Node* node) {
FlagsContinuation cont(kUnsignedLessThan, node); FlagsContinuation cont(kUnsignedLessThan, node);
return VisitWordCompare(this, node, kIA32Cmp, &cont, false); VisitWordCompare(this, node, kIA32Cmp, &cont);
} }
void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) { void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) {
FlagsContinuation cont(kUnsignedLessThanOrEqual, node); FlagsContinuation cont(kUnsignedLessThanOrEqual, node);
return VisitWordCompare(this, node, kIA32Cmp, &cont, false); VisitWordCompare(this, node, kIA32Cmp, &cont);
} }
......
...@@ -789,33 +789,43 @@ static void VisitCompare(InstructionSelector* selector, InstructionCode opcode, ...@@ -789,33 +789,43 @@ static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
} }
// Shared routine for multiple compare operations.
static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
Node* left, Node* right, FlagsContinuation* cont,
bool commutative) {
X64OperandGenerator g(selector);
if (commutative && g.CanBeBetterLeftOperand(right)) {
std::swap(left, right);
}
VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont);
}
// Shared routine for multiple word compare operations. // Shared routine for multiple word compare operations.
static void VisitWordCompare(InstructionSelector* selector, Node* node, static void VisitWordCompare(InstructionSelector* selector, Node* node,
InstructionCode opcode, FlagsContinuation* cont, InstructionCode opcode, FlagsContinuation* cont) {
bool commutative) {
X64OperandGenerator g(selector); X64OperandGenerator g(selector);
Node* left = node->InputAt(0); Node* const left = node->InputAt(0);
Node* right = node->InputAt(1); Node* const right = node->InputAt(1);
// Match immediates on left or right side of comparison. // Match immediates on left or right side of comparison.
if (g.CanBeImmediate(right)) { if (g.CanBeImmediate(right)) {
VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont); VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont);
} else if (g.CanBeImmediate(left)) { } else if (g.CanBeImmediate(left)) {
if (!commutative) cont->Commute(); if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute();
VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont); VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont);
} else { } else {
VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); VisitCompare(selector, opcode, left, right, cont,
node->op()->HasProperty(Operator::kCommutative));
} }
} }
// Shared routine for multiple float64 compare operations.
static void VisitFloat64Compare(InstructionSelector* selector, Node* node, static void VisitFloat64Compare(InstructionSelector* selector, Node* node,
FlagsContinuation* cont) { FlagsContinuation* cont) {
X64OperandGenerator g(selector); VisitCompare(selector, kSSEFloat64Cmp, node->InputAt(0), node->InputAt(1),
Node* left = node->InputAt(0); cont, node->op()->HasProperty(Operator::kCommutative));
Node* right = node->InputAt(1);
VisitCompare(selector, kSSEFloat64Cmp, g.UseRegister(left), g.Use(right),
cont);
} }
...@@ -863,31 +873,31 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, ...@@ -863,31 +873,31 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
switch (value->opcode()) { switch (value->opcode()) {
case IrOpcode::kWord32Equal: case IrOpcode::kWord32Equal:
cont.OverwriteAndNegateIfEqual(kEqual); cont.OverwriteAndNegateIfEqual(kEqual);
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kInt32LessThan: case IrOpcode::kInt32LessThan:
cont.OverwriteAndNegateIfEqual(kSignedLessThan); cont.OverwriteAndNegateIfEqual(kSignedLessThan);
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kInt32LessThanOrEqual: case IrOpcode::kInt32LessThanOrEqual:
cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kUint32LessThan: case IrOpcode::kUint32LessThan:
cont.OverwriteAndNegateIfEqual(kUnsignedLessThan); cont.OverwriteAndNegateIfEqual(kUnsignedLessThan);
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kUint32LessThanOrEqual: case IrOpcode::kUint32LessThanOrEqual:
cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kWord64Equal: case IrOpcode::kWord64Equal:
cont.OverwriteAndNegateIfEqual(kEqual); cont.OverwriteAndNegateIfEqual(kEqual);
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kInt64LessThan: case IrOpcode::kInt64LessThan:
cont.OverwriteAndNegateIfEqual(kSignedLessThan); cont.OverwriteAndNegateIfEqual(kSignedLessThan);
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kInt64LessThanOrEqual: case IrOpcode::kInt64LessThanOrEqual:
cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kUint64LessThan: case IrOpcode::kUint64LessThan:
cont.OverwriteAndNegateIfEqual(kUnsignedLessThan); cont.OverwriteAndNegateIfEqual(kUnsignedLessThan);
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kFloat64Equal: case IrOpcode::kFloat64Equal:
cont.OverwriteAndNegateIfEqual(kUnorderedEqual); cont.OverwriteAndNegateIfEqual(kUnorderedEqual);
return VisitFloat64Compare(this, value, &cont); return VisitFloat64Compare(this, value, &cont);
...@@ -923,13 +933,13 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, ...@@ -923,13 +933,13 @@ void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
} }
break; break;
case IrOpcode::kInt32Sub: case IrOpcode::kInt32Sub:
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kInt64Sub: case IrOpcode::kInt64Sub:
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kWord32And: case IrOpcode::kWord32And:
return VisitWordCompare(this, value, kX64Test32, &cont, true); return VisitWordCompare(this, value, kX64Test32, &cont);
case IrOpcode::kWord64And: case IrOpcode::kWord64And:
return VisitWordCompare(this, value, kX64Test, &cont, true); return VisitWordCompare(this, value, kX64Test, &cont);
default: default:
break; break;
} }
...@@ -949,39 +959,39 @@ void InstructionSelector::VisitWord32Equal(Node* const node) { ...@@ -949,39 +959,39 @@ void InstructionSelector::VisitWord32Equal(Node* const node) {
if (CanCover(user, value)) { if (CanCover(user, value)) {
switch (value->opcode()) { switch (value->opcode()) {
case IrOpcode::kInt32Sub: case IrOpcode::kInt32Sub:
return VisitWordCompare(this, value, kX64Cmp32, &cont, false); return VisitWordCompare(this, value, kX64Cmp32, &cont);
case IrOpcode::kWord32And: case IrOpcode::kWord32And:
return VisitWordCompare(this, value, kX64Test32, &cont, true); return VisitWordCompare(this, value, kX64Test32, &cont);
default: default:
break; break;
} }
} }
} }
VisitWordCompare(this, node, kX64Cmp32, &cont, false); VisitWordCompare(this, node, kX64Cmp32, &cont);
} }
void InstructionSelector::VisitInt32LessThan(Node* node) { void InstructionSelector::VisitInt32LessThan(Node* node) {
FlagsContinuation cont(kSignedLessThan, node); FlagsContinuation cont(kSignedLessThan, node);
VisitWordCompare(this, node, kX64Cmp32, &cont, false); VisitWordCompare(this, node, kX64Cmp32, &cont);
} }
void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) { void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) {
FlagsContinuation cont(kSignedLessThanOrEqual, node); FlagsContinuation cont(kSignedLessThanOrEqual, node);
VisitWordCompare(this, node, kX64Cmp32, &cont, false); VisitWordCompare(this, node, kX64Cmp32, &cont);
} }
void InstructionSelector::VisitUint32LessThan(Node* node) { void InstructionSelector::VisitUint32LessThan(Node* node) {
FlagsContinuation cont(kUnsignedLessThan, node); FlagsContinuation cont(kUnsignedLessThan, node);
VisitWordCompare(this, node, kX64Cmp32, &cont, false); VisitWordCompare(this, node, kX64Cmp32, &cont);
} }
void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) { void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) {
FlagsContinuation cont(kUnsignedLessThanOrEqual, node); FlagsContinuation cont(kUnsignedLessThanOrEqual, node);
VisitWordCompare(this, node, kX64Cmp32, &cont, false); VisitWordCompare(this, node, kX64Cmp32, &cont);
} }
...@@ -994,15 +1004,15 @@ void InstructionSelector::VisitWord64Equal(Node* const node) { ...@@ -994,15 +1004,15 @@ void InstructionSelector::VisitWord64Equal(Node* const node) {
if (CanCover(user, value)) { if (CanCover(user, value)) {
switch (value->opcode()) { switch (value->opcode()) {
case IrOpcode::kInt64Sub: case IrOpcode::kInt64Sub:
return VisitWordCompare(this, value, kX64Cmp, &cont, false); return VisitWordCompare(this, value, kX64Cmp, &cont);
case IrOpcode::kWord64And: case IrOpcode::kWord64And:
return VisitWordCompare(this, value, kX64Test, &cont, true); return VisitWordCompare(this, value, kX64Test, &cont);
default: default:
break; break;
} }
} }
} }
VisitWordCompare(this, node, kX64Cmp, &cont, false); VisitWordCompare(this, node, kX64Cmp, &cont);
} }
...@@ -1028,19 +1038,19 @@ void InstructionSelector::VisitInt32SubWithOverflow(Node* node) { ...@@ -1028,19 +1038,19 @@ void InstructionSelector::VisitInt32SubWithOverflow(Node* node) {
void InstructionSelector::VisitInt64LessThan(Node* node) { void InstructionSelector::VisitInt64LessThan(Node* node) {
FlagsContinuation cont(kSignedLessThan, node); FlagsContinuation cont(kSignedLessThan, node);
VisitWordCompare(this, node, kX64Cmp, &cont, false); VisitWordCompare(this, node, kX64Cmp, &cont);
} }
void InstructionSelector::VisitInt64LessThanOrEqual(Node* node) { void InstructionSelector::VisitInt64LessThanOrEqual(Node* node) {
FlagsContinuation cont(kSignedLessThanOrEqual, node); FlagsContinuation cont(kSignedLessThanOrEqual, node);
VisitWordCompare(this, node, kX64Cmp, &cont, false); VisitWordCompare(this, node, kX64Cmp, &cont);
} }
void InstructionSelector::VisitUint64LessThan(Node* node) { void InstructionSelector::VisitUint64LessThan(Node* node) {
FlagsContinuation cont(kUnsignedLessThan, node); FlagsContinuation cont(kUnsignedLessThan, node);
VisitWordCompare(this, node, kX64Cmp, &cont, false); VisitWordCompare(this, node, kX64Cmp, &cont);
} }
......
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