Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ad1d29c

Browse files
askeksacommit-bot@chromium.org
authored andcommitted
Reland "[vm] Share some comparison and condition code across platforms."
This is a reland of 87b4848 Original change's description: > [vm] Share some comparison and condition code across platforms. > > Change-Id: Idf03f86a46d65d53b497add41873d38efe99ba84 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136637 > Reviewed-by: Daco Harkes <[email protected]> Change-Id: If2d53728e4397f038acc18fff0add72fa03da414 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142374 Reviewed-by: Daco Harkes <[email protected]>
1 parent 9f80082 commit ad1d29c

File tree

9 files changed

+124
-263
lines changed

9 files changed

+124
-263
lines changed

runtime/vm/compiler/backend/il.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,6 +4319,27 @@ StrictCompareInstr::StrictCompareInstr(TokenPosition token_pos,
43194319
SetInputAt(1, right);
43204320
}
43214321

4322+
Condition StrictCompareInstr::EmitComparisonCode(FlowGraphCompiler* compiler,
4323+
BranchLabels labels) {
4324+
Location left = locs()->in(0);
4325+
Location right = locs()->in(1);
4326+
ASSERT(!left.IsConstant() || !right.IsConstant());
4327+
Condition true_condition;
4328+
if (left.IsConstant()) {
4329+
true_condition = EmitComparisonCodeRegConstant(
4330+
compiler, labels, right.reg(), left.constant());
4331+
} else if (right.IsConstant()) {
4332+
true_condition = EmitComparisonCodeRegConstant(compiler, labels, left.reg(),
4333+
right.constant());
4334+
} else {
4335+
true_condition = compiler->EmitEqualityRegRegCompare(
4336+
left.reg(), right.reg(), needs_number_check(), token_pos(), deopt_id());
4337+
}
4338+
return true_condition != kInvalidCondition && (kind() != Token::kEQ_STRICT)
4339+
? InvertCondition(true_condition)
4340+
: true_condition;
4341+
}
4342+
43224343
LocationSummary* LoadClassIdInstr::MakeLocationSummary(Zone* zone,
43234344
bool opt) const {
43244345
const intptr_t kNumInputs = 1;

runtime/vm/compiler/backend/il.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,6 +4226,11 @@ class StrictCompareInstr : public TemplateComparison<2, NoThrow, Pure> {
42264226
ADD_EXTRA_INFO_TO_S_EXPRESSION_SUPPORT;
42274227

42284228
private:
4229+
Condition EmitComparisonCodeRegConstant(FlowGraphCompiler* compiler,
4230+
BranchLabels labels,
4231+
Register reg,
4232+
const Object& obj);
4233+
42294234
// True if the comparison must check for double or Mint and
42304235
// use value comparison instead.
42314236
bool needs_number_check_;

runtime/vm/compiler/backend/il_arm.cc

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -395,38 +395,6 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
395395
__ set_constant_pool_allowed(true);
396396
}
397397

398-
static Condition NegateCondition(Condition condition) {
399-
switch (condition) {
400-
case EQ:
401-
return NE;
402-
case NE:
403-
return EQ;
404-
case LT:
405-
return GE;
406-
case LE:
407-
return GT;
408-
case GT:
409-
return LE;
410-
case GE:
411-
return LT;
412-
case CC:
413-
return CS;
414-
case LS:
415-
return HI;
416-
case HI:
417-
return LS;
418-
case CS:
419-
return CC;
420-
case VC:
421-
return VS;
422-
case VS:
423-
return VC;
424-
default:
425-
UNREACHABLE();
426-
return EQ;
427-
}
428-
}
429-
430398
// Detect pattern when one value is zero and another is a power of 2.
431399
static bool IsPowerOfTwoKind(intptr_t v1, intptr_t v2) {
432400
return (Utils::IsPowerOfTwo(v1) && (v2 == 0)) ||
@@ -464,7 +432,7 @@ void IfThenElseInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
464432
if (is_power_of_two_kind) {
465433
if (true_value == 0) {
466434
// We need to have zero in result on true_condition.
467-
true_condition = NegateCondition(true_condition);
435+
true_condition = InvertCondition(true_condition);
468436
}
469437
} else {
470438
if (true_value == 0) {
@@ -473,7 +441,7 @@ void IfThenElseInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
473441
true_value = false_value;
474442
false_value = temp;
475443
} else {
476-
true_condition = NegateCondition(true_condition);
444+
true_condition = InvertCondition(true_condition);
477445
}
478446
}
479447

@@ -895,7 +863,7 @@ static void EmitBranchOnCondition(FlowGraphCompiler* compiler,
895863
__ b(labels.true_label, true_condition);
896864
} else {
897865
// If the next block is not the false successor we will branch to it.
898-
Condition false_condition = NegateCondition(true_condition);
866+
Condition false_condition = InvertCondition(true_condition);
899867
__ b(labels.false_label, false_condition);
900868

901869
// Fall through or jump to the true successor.
@@ -4060,7 +4028,7 @@ void CheckedSmiComparisonInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
40604028
ASSERT(true_condition != kInvalidCondition);
40614029
Register result = locs()->out(0).reg();
40624030
__ LoadObject(result, Bool::True(), true_condition);
4063-
__ LoadObject(result, Bool::False(), NegateCondition(true_condition));
4031+
__ LoadObject(result, Bool::False(), InvertCondition(true_condition));
40644032
__ Bind(slow_path->exit_label());
40654033
}
40664034
#undef EMIT_SMI_CHECK
@@ -7509,29 +7477,13 @@ LocationSummary* StrictCompareInstr::MakeLocationSummary(Zone* zone,
75097477
return locs;
75107478
}
75117479

7512-
Condition StrictCompareInstr::EmitComparisonCode(FlowGraphCompiler* compiler,
7513-
BranchLabels labels) {
7514-
Location left = locs()->in(0);
7515-
Location right = locs()->in(1);
7516-
ASSERT(!left.IsConstant() || !right.IsConstant());
7517-
Condition true_condition;
7518-
if (left.IsConstant()) {
7519-
true_condition = compiler->EmitEqualityRegConstCompare(
7520-
right.reg(), left.constant(), needs_number_check(), token_pos(),
7521-
deopt_id_);
7522-
} else if (right.IsConstant()) {
7523-
true_condition = compiler->EmitEqualityRegConstCompare(
7524-
left.reg(), right.constant(), needs_number_check(), token_pos(),
7525-
deopt_id_);
7526-
} else {
7527-
true_condition = compiler->EmitEqualityRegRegCompare(
7528-
left.reg(), right.reg(), needs_number_check(), token_pos(), deopt_id_);
7529-
}
7530-
if (kind() != Token::kEQ_STRICT) {
7531-
ASSERT(kind() == Token::kNE_STRICT);
7532-
true_condition = NegateCondition(true_condition);
7533-
}
7534-
return true_condition;
7480+
Condition StrictCompareInstr::EmitComparisonCodeRegConstant(
7481+
FlowGraphCompiler* compiler,
7482+
BranchLabels labels,
7483+
Register reg,
7484+
const Object& obj) {
7485+
return compiler->EmitEqualityRegConstCompare(reg, obj, needs_number_check(),
7486+
token_pos(), deopt_id());
75357487
}
75367488

75377489
void ComparisonInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
@@ -7556,7 +7508,7 @@ void ComparisonInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
75567508
// a condition we can avoid the branch and use conditional loads.
75577509
ASSERT(true_condition != kInvalidCondition);
75587510
__ LoadObject(result, Bool::True(), true_condition);
7559-
__ LoadObject(result, Bool::False(), NegateCondition(true_condition));
7511+
__ LoadObject(result, Bool::False(), InvertCondition(true_condition));
75607512
}
75617513
}
75627514

runtime/vm/compiler/backend/il_arm64.cc

Lines changed: 13 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -296,38 +296,6 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
296296
__ set_constant_pool_allowed(true);
297297
}
298298

299-
static Condition NegateCondition(Condition condition) {
300-
switch (condition) {
301-
case EQ:
302-
return NE;
303-
case NE:
304-
return EQ;
305-
case LT:
306-
return GE;
307-
case LE:
308-
return GT;
309-
case GT:
310-
return LE;
311-
case GE:
312-
return LT;
313-
case CC:
314-
return CS;
315-
case LS:
316-
return HI;
317-
case HI:
318-
return LS;
319-
case CS:
320-
return CC;
321-
case VS:
322-
return VC;
323-
case VC:
324-
return VS;
325-
default:
326-
UNREACHABLE();
327-
return EQ;
328-
}
329-
}
330-
331299
// Detect pattern when one value is zero and another is a power of 2.
332300
static bool IsPowerOfTwoKind(intptr_t v1, intptr_t v2) {
333301
return (Utils::IsPowerOfTwo(v1) && (v2 == 0)) ||
@@ -362,7 +330,7 @@ void IfThenElseInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
362330
if (is_power_of_two_kind) {
363331
if (true_value == 0) {
364332
// We need to have zero in result on true_condition.
365-
true_condition = NegateCondition(true_condition);
333+
true_condition = InvertCondition(true_condition);
366334
}
367335
} else {
368336
if (true_value == 0) {
@@ -371,7 +339,7 @@ void IfThenElseInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
371339
true_value = false_value;
372340
false_value = temp;
373341
} else {
374-
true_condition = NegateCondition(true_condition);
342+
true_condition = InvertCondition(true_condition);
375343
}
376344
}
377345

@@ -730,7 +698,7 @@ static void EmitBranchOnCondition(FlowGraphCompiler* compiler,
730698
__ b(labels.true_label, true_condition);
731699
} else {
732700
// If the next block is not the false successor we will branch to it.
733-
Condition false_condition = NegateCondition(true_condition);
701+
Condition false_condition = InvertCondition(true_condition);
734702
__ b(labels.false_label, false_condition);
735703

736704
// Fall through or jump to the true successor.
@@ -762,7 +730,7 @@ static void EmitCbzTbz(Register reg,
762730
__ GenerateXCbzTbz(reg, true_condition, labels.true_label);
763731
} else {
764732
// If the next block is not the false successor we will branch to it.
765-
Condition false_condition = NegateCondition(true_condition);
733+
Condition false_condition = InvertCondition(true_condition);
766734
__ GenerateXCbzTbz(reg, false_condition, labels.false_label);
767735

768736
// Fall through or jump to the true successor.
@@ -6481,47 +6449,20 @@ LocationSummary* StrictCompareInstr::MakeLocationSummary(Zone* zone,
64816449
return locs;
64826450
}
64836451

6484-
static Condition EmitComparisonCodeRegConstant(FlowGraphCompiler* compiler,
6485-
Token::Kind kind,
6486-
BranchLabels labels,
6487-
Register reg,
6488-
const Object& obj,
6489-
bool needs_number_check,
6490-
TokenPosition token_pos,
6491-
intptr_t deopt_id) {
6492-
Condition orig_cond = (kind == Token::kEQ_STRICT) ? EQ : NE;
6493-
if (!needs_number_check && compiler::target::IsSmi(obj) &&
6452+
Condition StrictCompareInstr::EmitComparisonCodeRegConstant(
6453+
FlowGraphCompiler* compiler,
6454+
BranchLabels labels,
6455+
Register reg,
6456+
const Object& obj) {
6457+
Condition orig_cond = (kind() == Token::kEQ_STRICT) ? EQ : NE;
6458+
if (!needs_number_check() && compiler::target::IsSmi(obj) &&
64946459
compiler::target::ToRawSmi(obj) == 0 &&
64956460
CanUseCbzTbzForComparison(compiler, reg, orig_cond, labels)) {
64966461
EmitCbzTbz(reg, compiler, orig_cond, labels);
64976462
return kInvalidCondition;
64986463
} else {
6499-
Condition true_condition = compiler->EmitEqualityRegConstCompare(
6500-
reg, obj, needs_number_check, token_pos, deopt_id);
6501-
return (kind != Token::kEQ_STRICT) ? NegateCondition(true_condition)
6502-
: true_condition;
6503-
}
6504-
}
6505-
6506-
Condition StrictCompareInstr::EmitComparisonCode(FlowGraphCompiler* compiler,
6507-
BranchLabels labels) {
6508-
Location left = locs()->in(0);
6509-
Location right = locs()->in(1);
6510-
ASSERT(!left.IsConstant() || !right.IsConstant());
6511-
Condition true_condition;
6512-
if (left.IsConstant()) {
6513-
return EmitComparisonCodeRegConstant(compiler, kind(), labels, right.reg(),
6514-
left.constant(), needs_number_check(),
6515-
token_pos(), deopt_id_);
6516-
} else if (right.IsConstant()) {
6517-
return EmitComparisonCodeRegConstant(compiler, kind(), labels, left.reg(),
6518-
right.constant(), needs_number_check(),
6519-
token_pos(), deopt_id_);
6520-
} else {
6521-
true_condition = compiler->EmitEqualityRegRegCompare(
6522-
left.reg(), right.reg(), needs_number_check(), token_pos(), deopt_id_);
6523-
return (kind() != Token::kEQ_STRICT) ? NegateCondition(true_condition)
6524-
: true_condition;
6464+
return compiler->EmitEqualityRegConstCompare(reg, obj, needs_number_check(),
6465+
token_pos(), deopt_id());
65256466
}
65266467
}
65276468

0 commit comments

Comments
 (0)