[modular_aot] Fix swapped opcodes in comparison simplification

Updates the simplification of moving constat operands of comparisons to the right to correctly use the strictly swapped operand rather than the negated operand.

Also change the `flipOperands` function to a `swapped` getter to avoid confusion and be clearer that it returns a new value, rather than changing the current one. This better aligns with the Effective Dart guidelines for when to use a getter and how to name them.

TEST=pkg/cfg/testcases/simplification.dart

Change-Id: Id2c19b7cfcbc1586413251b2b9d6d54abcf590c0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/509840
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Slava Egorov <vegorov@google.com>
This commit is contained in:
Parker Lougheed
2026-06-08 05:34:33 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 890e259cfd
commit 95b8f52f9f
4 changed files with 24 additions and 15 deletions
+18 -9
View File
@@ -674,7 +674,16 @@ enum ComparisonOpcode {
_ => false,
};
ComparisonOpcode flipOperands() => switch (this) {
/// The opcode for an equivalent comparison with the operands swapped.
///
/// For example, `a < b` is equivalent to `b > a`,
/// so [intLess] becomes [intGreater].
/// Symmetric opcodes, such as those for equality,
/// stay the same when swapped.
///
/// Swapping preserves the comparison's result and strictness,
/// unlike [negate], which inverts the result.
ComparisonOpcode get swapped => switch (this) {
equal ||
notEqual ||
identical ||
@@ -685,14 +694,14 @@ enum ComparisonOpcode {
intTestIsNotZero ||
doubleEqual ||
doubleNotEqual => this,
intLess => intGreaterOrEqual,
intLessOrEqual => intGreater,
intGreater => intLessOrEqual,
intGreaterOrEqual => intLess,
doubleLess => doubleGreaterOrEqual,
doubleLessOrEqual => doubleGreater,
doubleGreater => doubleLessOrEqual,
doubleGreaterOrEqual => doubleLess,
intLess => intGreater,
intLessOrEqual => intGreaterOrEqual,
intGreater => intLess,
intGreaterOrEqual => intLessOrEqual,
doubleLess => doubleGreater,
doubleLessOrEqual => doubleGreaterOrEqual,
doubleGreater => doubleLess,
doubleGreaterOrEqual => doubleLessOrEqual,
};
bool get canBeNegated => switch (this) {
+1 -1
View File
@@ -115,7 +115,7 @@ final class Simplification extends Pass
}
// Move constant operand to the right.
if (left is Constant) {
instr.op = instr.op.flipOperands();
instr.op = instr.op.swapped;
instr.replaceInputAt(0, right);
instr.replaceInputAt(1, left);
left = instr.left;
+4 -4
View File
@@ -109,13 +109,13 @@ B0 = EntryBlock()
DirectCall print(v10)
v14 = Comparison int ==(v1, v12)
DirectCall print(v14)
v18 = Comparison int <=(v1, v16)
v18 = Comparison int <(v1, v16)
DirectCall print(v18)
v22 = Comparison int <(v1, v20)
v22 = Comparison int <=(v1, v20)
DirectCall print(v22)
v26 = Comparison int >=(v1, v24)
v26 = Comparison int >(v1, v24)
DirectCall print(v26)
v30 = Comparison int >(v1, v28)
v30 = Comparison int >=(v1, v28)
DirectCall print(v30)
v34 = BinaryDoubleOp +(v2, v60)
DirectCall print(v34)
@@ -73,7 +73,7 @@ B0 = EntryBlock() dominates:(B10, B9)
v22 = Constant(null)
Constant(false)
v1 = Parameter(n)
v8 = Comparison int >=(v1, v2)
v8 = Comparison int >(v1, v2)
Branch(v8, true: B9, false: B10)
B9 = TargetBlock() idom:B0
Return(v22)