Skip to content

Commit 52cb60b

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm/compiler] Rename Comparison to Condition, introduce proper Comparison
Previously, ComparisonInstr base class represented arbitrary conditions used in Branch, IfThenElse and CheckCondition instructions and included subclasses TestInt, TestCids, TestRange and unary DoubleTestOp which are not comparisons. So this refactoring renames ComparisonInstr to ConditionInstr. In addition, a new Comparison instruction is added as a base class for StrictCompare, EqualityCompare and RelationalOp. TEST=ci (pure refactoring) Change-Id: Ic8756ee5913ff2bc974c95cea8004370c9f5527f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393420 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent af2bea8 commit 52cb60b

26 files changed

+507
-492
lines changed

runtime/vm/compiler/aot/aot_call_specializer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ bool AotCallSpecializer::TryReplaceInstanceOfWithRangeCheck(
11271127
new (Z) LoadClassIdInstr(new (Z) Value(left), kUnboxedUword);
11281128
InsertBefore(call, load_cid, nullptr, FlowGraph::kValue);
11291129

1130-
ComparisonInstr* check_range;
1130+
ConditionInstr* check_range;
11311131
if (lower_limit == upper_limit) {
11321132
ConstantInstr* cid_constant = flow_graph()->GetConstant(
11331133
Smi::Handle(Z, Smi::New(lower_limit)), kUnboxedUword);

runtime/vm/compiler/backend/block_builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ class BlockBuilder : public ValueObject {
132132
return AddUnboxInstr(rep, new Value(boxed), value_mode);
133133
}
134134

135-
BranchInstr* AddBranch(ComparisonInstr* comp,
135+
BranchInstr* AddBranch(ConditionInstr* cond,
136136
TargetEntryInstr* true_successor,
137137
TargetEntryInstr* false_successor) {
138138
auto branch =
139-
new BranchInstr(comp, CompilerState::Current().GetNextDeoptId());
139+
new BranchInstr(cond, CompilerState::Current().GetNextDeoptId());
140140
// Some graph transformations use environments from branches.
141141
branch->SetEnvironment(dummy_env_);
142142
current_->AppendInstruction(branch);

runtime/vm/compiler/backend/branch_optimizer.cc

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ static bool PhiHasSingleUse(PhiInstr* phi, Value* use) {
3030
}
3131

3232
bool BranchSimplifier::Match(JoinEntryInstr* block) {
33-
// Match the pattern of a branch on a comparison whose left operand is a
33+
// Match the pattern of a branch on a condition whose left operand is a
3434
// phi from the same block, and whose right operand is a constant.
3535
//
36-
// Branch(Comparison(kind, Phi, Constant))
36+
// Branch(Condition(kind, Phi, Constant))
3737
//
3838
// These are the branches produced by inlining in a test context. Also,
3939
// the phi has no other uses so they can simply be eliminated. The block
4040
// has no other phis and no instructions intervening between the phi and
4141
// branch so the block can simply be eliminated.
4242
BranchInstr* branch = block->last_instruction()->AsBranch();
4343
ASSERT(branch != nullptr);
44-
ComparisonInstr* comparison = branch->comparison();
45-
if (comparison->InputCount() != 2) {
44+
ConditionInstr* condition = branch->condition();
45+
if (condition->InputCount() != 2) {
4646
return false;
4747
}
48-
if (comparison->CanDeoptimize() || comparison->MayThrow()) {
48+
if (condition->CanDeoptimize() || condition->MayThrow()) {
4949
return false;
5050
}
51-
Value* left = comparison->left();
51+
Value* left = condition->InputAt(0);
5252
PhiInstr* phi = left->definition()->AsPhi();
53-
Value* right = comparison->right();
53+
Value* right = condition->InputAt(1);
5454
ConstantInstr* constant =
5555
(right == nullptr) ? nullptr : right->definition()->AsConstant();
5656
return (phi != nullptr) && (constant != nullptr) &&
@@ -87,11 +87,11 @@ BranchInstr* BranchSimplifier::CloneBranch(Zone* zone,
8787
BranchInstr* branch,
8888
Value* new_left,
8989
Value* new_right) {
90-
ComparisonInstr* comparison = branch->comparison();
91-
ComparisonInstr* new_comparison =
92-
comparison->CopyWithNewOperands(new_left, new_right);
90+
ConditionInstr* condition = branch->condition();
91+
ConditionInstr* new_condition =
92+
condition->CopyWithNewOperands(new_left, new_right);
9393
BranchInstr* new_branch =
94-
new (zone) BranchInstr(new_comparison, DeoptId::kNone);
94+
new (zone) BranchInstr(new_condition, DeoptId::kNone);
9595
return new_branch;
9696
}
9797

@@ -140,9 +140,10 @@ void BranchSimplifier::Simplify(FlowGraph* flow_graph) {
140140
JoinEntryInstr* join_true = ToJoinEntry(zone, branch->true_successor());
141141
JoinEntryInstr* join_false = ToJoinEntry(zone, branch->false_successor());
142142

143-
ComparisonInstr* comparison = branch->comparison();
144-
PhiInstr* phi = comparison->left()->definition()->AsPhi();
145-
ConstantInstr* constant = comparison->right()->definition()->AsConstant();
143+
ConditionInstr* condition = branch->condition();
144+
PhiInstr* phi = condition->InputAt(0)->definition()->AsPhi();
145+
ConstantInstr* constant =
146+
condition->InputAt(1)->definition()->AsConstant();
146147
ASSERT(constant != nullptr);
147148
// Copy the constant and branch and push it to all the predecessors.
148149
for (intptr_t i = 0, count = block->PredecessorCount(); i < count; ++i) {
@@ -161,10 +162,10 @@ void BranchSimplifier::Simplify(FlowGraph* flow_graph) {
161162
} else {
162163
// Take the environment from the branch if it has one.
163164
new_branch->InheritDeoptTarget(zone, branch);
164-
// InheritDeoptTarget gave the new branch's comparison the same
165+
// InheritDeoptTarget gave the new branch's condition the same
165166
// deopt id that it gave the new branch. The id should be the
166-
// deopt id of the original comparison.
167-
new_branch->comparison()->SetDeoptId(*comparison);
167+
// deopt id of the original condition.
168+
new_branch->condition()->SetDeoptId(*condition);
168169
// The phi can be used in the branch's environment. Rename such
169170
// uses.
170171
Definition* replacement = phi->InputAt(i)->definition();
@@ -249,11 +250,11 @@ void IfConverter::Simplify(FlowGraph* flow_graph) {
249250
JoinEntryInstr* join = block->AsJoinEntry();
250251

251252
// Detect diamond control flow pattern which materializes a value depending
252-
// on the result of the comparison:
253+
// on the result of the condition:
253254
//
254255
// B_pred:
255256
// ...
256-
// Branch if COMP goto (B_pred1, B_pred2)
257+
// Branch if COND goto (B_pred1, B_pred2)
257258
// B_pred1: -- trivial block that contains at most one definition
258259
// v1 = Constant(...)
259260
// goto B_block
@@ -266,7 +267,7 @@ void IfConverter::Simplify(FlowGraph* flow_graph) {
266267
// and replace it with
267268
//
268269
// Ba:
269-
// v3 = IfThenElse(COMP ? v1 : v2)
270+
// v3 = IfThenElse(COND ? v1 : v2)
270271
//
271272
if ((join != nullptr) && (join->phis() != nullptr) &&
272273
(join->phis()->length() == 1) && (block->PredecessorCount() == 2)) {
@@ -291,19 +292,20 @@ void IfConverter::Simplify(FlowGraph* flow_graph) {
291292
continue;
292293
}
293294

294-
ComparisonInstr* comparison = branch->comparison();
295+
ConditionInstr* condition = branch->condition();
295296

296297
// Check if the platform supports efficient branchless IfThenElseInstr
297-
// for the given combination of comparison and values flowing from
298+
// for the given combination of condition and values flowing from
298299
// false and true paths.
299-
if (IfThenElseInstr::Supports(comparison, v1, v2)) {
300+
if (IfThenElseInstr::Supports(condition, v1, v2)) {
300301
Value* if_true = (pred1 == branch->true_successor()) ? v1 : v2;
301302
Value* if_false = (pred2 == branch->true_successor()) ? v1 : v2;
302303

303-
ComparisonInstr* new_comparison = comparison->CopyWithNewOperands(
304-
comparison->left()->Copy(zone), comparison->right()->Copy(zone));
304+
ConditionInstr* new_condition =
305+
condition->CopyWithNewOperands(condition->InputAt(0)->Copy(zone),
306+
condition->InputAt(1)->Copy(zone));
305307
IfThenElseInstr* if_then_else =
306-
new (zone) IfThenElseInstr(new_comparison, if_true->Copy(zone),
308+
new (zone) IfThenElseInstr(new_condition, if_true->Copy(zone),
307309
if_false->Copy(zone), DeoptId::kNone);
308310
flow_graph->InsertBefore(branch, if_then_else, nullptr,
309311
FlowGraph::kValue);

runtime/vm/compiler/backend/constant_propagator.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void ConstantPropagator::VisitIndirectGoto(IndirectGotoInstr* instr) {
239239
}
240240

241241
void ConstantPropagator::VisitBranch(BranchInstr* instr) {
242-
instr->comparison()->Accept(this);
242+
instr->condition()->Accept(this);
243243

244244
// The successors may be reachable, but only if this instruction is. (We
245245
// might be analyzing it because the constant value of one of its inputs
@@ -250,7 +250,7 @@ void ConstantPropagator::VisitBranch(BranchInstr* instr) {
250250
(instr->constant_target() == instr->false_successor()));
251251
SetReachable(instr->constant_target());
252252
} else {
253-
const Object& value = instr->comparison()->constant_value();
253+
const Object& value = instr->condition()->constant_value();
254254
if (IsNonConstant(value)) {
255255
SetReachable(instr->true_successor());
256256
SetReachable(instr->false_successor());
@@ -557,8 +557,8 @@ void ConstantPropagator::VisitStoreLocal(StoreLocalInstr* instr) {
557557
}
558558

559559
void ConstantPropagator::VisitIfThenElse(IfThenElseInstr* instr) {
560-
instr->comparison()->Accept(this);
561-
const Object& value = instr->comparison()->constant_value();
560+
instr->condition()->Accept(this);
561+
const Object& value = instr->condition()->constant_value();
562562
ASSERT(!value.IsNull());
563563
if (IsUnknown(value)) {
564564
return;
@@ -656,8 +656,6 @@ static bool CompareIntegers(Token::Kind kind,
656656
}
657657
}
658658

659-
// Comparison instruction that is equivalent to the (left & right) == 0
660-
// comparison pattern.
661659
void ConstantPropagator::VisitTestInt(TestIntInstr* instr) {
662660
const Object& left = instr->left()->definition()->constant_value();
663661
const Object& right = instr->right()->definition()->constant_value();
@@ -1614,10 +1612,11 @@ static RedefinitionInstr* InsertRedefinition(FlowGraph* graph,
16141612
void ConstantPropagator::InsertRedefinitionsAfterEqualityComparisons() {
16151613
for (auto block : graph_->reverse_postorder()) {
16161614
if (auto branch = block->last_instruction()->AsBranch()) {
1617-
auto comparison = branch->comparison();
1618-
if (comparison->IsStrictCompare() ||
1619-
(comparison->IsEqualityCompare() &&
1620-
comparison->operation_cid() != kDoubleCid)) {
1615+
auto comparison = branch->condition()->AsComparison();
1616+
if (comparison != nullptr &&
1617+
(comparison->IsStrictCompare() ||
1618+
(comparison->IsEqualityCompare() &&
1619+
comparison->operation_cid() != kDoubleCid))) {
16211620
Value* value;
16221621
ConstantInstr* constant_defn;
16231622
if (comparison->IsComparisonWithConstant(&value, &constant_defn) &&

runtime/vm/compiler/backend/flow_graph.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,9 +2818,9 @@ static GotoInstr* NewGoto(FlowGraph* graph,
28182818
}
28192819

28202820
static BranchInstr* NewBranch(FlowGraph* graph,
2821-
ComparisonInstr* cmp,
2821+
ConditionInstr* cond,
28222822
Instruction* inherit) {
2823-
BranchInstr* bra = new (graph->zone()) BranchInstr(cmp, DeoptId::kNone);
2823+
BranchInstr* bra = new (graph->zone()) BranchInstr(cond, DeoptId::kNone);
28242824
bra->InheritDeoptTarget(graph->zone(), inherit);
28252825
return bra;
28262826
}
@@ -2841,7 +2841,7 @@ static BranchInstr* NewBranch(FlowGraph* graph,
28412841
//
28422842
JoinEntryInstr* FlowGraph::NewDiamond(Instruction* instruction,
28432843
Instruction* inherit,
2844-
ComparisonInstr* compare,
2844+
ConditionInstr* condition,
28452845
TargetEntryInstr** b_true,
28462846
TargetEntryInstr** b_false) {
28472847
BlockEntryInstr* entry = instruction->GetBlock();
@@ -2851,7 +2851,7 @@ JoinEntryInstr* FlowGraph::NewDiamond(Instruction* instruction,
28512851
JoinEntryInstr* join = NewJoin(this, inherit);
28522852
GotoInstr* gotot = NewGoto(this, join, inherit);
28532853
GotoInstr* gotof = NewGoto(this, join, inherit);
2854-
BranchInstr* bra = NewBranch(this, compare, inherit);
2854+
BranchInstr* bra = NewBranch(this, condition, inherit);
28552855

28562856
instruction->AppendInstruction(bra);
28572857
entry->set_last_instruction(bra);

runtime/vm/compiler/backend/flow_graph.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,9 @@ class FlowGraph : public ZoneAllocated {
527527

528528
// Logical-AND (for use in short-circuit diamond).
529529
struct LogicalAnd {
530-
LogicalAnd(ComparisonInstr* x, ComparisonInstr* y) : oper1(x), oper2(y) {}
531-
ComparisonInstr* oper1;
532-
ComparisonInstr* oper2;
530+
LogicalAnd(ConditionInstr* x, ConditionInstr* y) : oper1(x), oper2(y) {}
531+
ConditionInstr* oper1;
532+
ConditionInstr* oper2;
533533
};
534534

535535
// Constructs a diamond control flow at the instruction, inheriting
@@ -538,7 +538,7 @@ class FlowGraph : public ZoneAllocated {
538538
// relation, but not the succ/pred ordering on block.
539539
JoinEntryInstr* NewDiamond(Instruction* instruction,
540540
Instruction* inherit,
541-
ComparisonInstr* compare,
541+
ConditionInstr* condition,
542542
TargetEntryInstr** block_true,
543543
TargetEntryInstr** block_false);
544544

runtime/vm/compiler/backend/flow_graph_compiler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void FlowGraphCompiler::InitCompiler() {
229229
for (ForwardInstructionIterator it(entry); !it.Done(); it.Advance()) {
230230
Instruction* current = it.Current();
231231
if (auto* branch = current->AsBranch()) {
232-
current = branch->comparison();
232+
current = branch->condition();
233233
}
234234
if (auto* instance_call = current->AsInstanceCall()) {
235235
const ICData* ic_data = instance_call->ic_data();

0 commit comments

Comments
 (0)