Skip to content

Commit 5cdde4a

Browse files
alexmarkovCommit Queue
authored andcommitted
IR simplification pass
Issue: #61635 Change-Id: I0c20792bd7bc133026b62a32b498b2411c863d0f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465640 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 81f561c commit 5cdde4a

File tree

9 files changed

+832
-25
lines changed

9 files changed

+832
-25
lines changed

pkg/cfg/lib/passes/simplification.dart

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

pkg/cfg/test/ir_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:cfg/ir/functions.dart';
2222
import 'package:cfg/ir/ir_to_text.dart';
2323
import 'package:cfg/ir/ssa_computation.dart';
2424
import 'package:cfg/passes/pass.dart';
25+
import 'package:cfg/passes/simplification.dart';
2526
import 'package:kernel/type_environment.dart';
2627
import 'package:test/test.dart';
2728
import 'package:vm/modular/target/vm.dart';
@@ -110,7 +111,7 @@ class CompileAndDumpIr extends RecursiveVisitor {
110111
recognizedMethods,
111112
enableAsserts: true,
112113
).buildFlowGraph();
113-
final pipeline = Pipeline([SSAComputation()]);
114+
final pipeline = Pipeline([SSAComputation(), Simplification()]);
114115
pipeline.run(graph);
115116
buffer.writeln('--- ${node.name}');
116117
buffer.writeln(

pkg/cfg/testcases/calls.dart.expect

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ B0 = EntryBlock() dominates:(B7, B9, B6)
4242
v4 = Constant(0)
4343
v12 = Constant(1)
4444
v20 = Constant(null)
45+
v22 = Constant(-1)
4546
v1 = Parameter(a)
4647
v2 = Parameter(b)
4748
v5 = Comparison int >(v1, v4)
4849
Branch(v5, true: B6, false: B7)
4950
B6 = TargetBlock() idom:B0
50-
v13 = BinaryIntOp -(v1, v12)
51-
DirectCall staticCalls(v13, v2)
51+
v23 = BinaryIntOp +(v1, v22)
52+
DirectCall staticCalls(v23, v2)
5253
Goto(B9)
5354
B7 = TargetBlock() idom:B0
5455
Goto(B9)

pkg/cfg/testcases/loops.dart.expect

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,25 @@ B37 = TargetBlock() idom:B32
5151

5252
--- chainedHeaders
5353
B0 = EntryBlock() dominates:(B2)
54-
v7 = Constant(17)
54+
Constant(17)
5555
v11 = Constant(0)
5656
v18 = Constant(2)
5757
v25 = Constant(null)
58+
v29 = Constant(-17)
5859
v1 = Parameter(i)
5960
Goto(B2)
6061
B2 = JoinBlock(B0, B21) idom:B0 dominates:(B4) loop-header (depth:1 body:(B2, B4, B14, B21, B13) back-edges:(B21))
61-
v27 = Phi(v1, v8)
62+
v27 = Phi(v1, v30)
6263
Goto(B4)
6364
B4 = JoinBlock(B2, B13) idom:B2 dominates:(B14, B13) loop-header (depth:2 body:(B4, B13) back-edges:(B13))
64-
v28 = Phi(v27, v8)
65-
v8 = BinaryIntOp -(v28, v7)
66-
v12 = Comparison int >=(v8, v11)
65+
v28 = Phi(v27, v30)
66+
v30 = BinaryIntOp +(v28, v29)
67+
v12 = Comparison int >=(v30, v11)
6768
Branch(v12, true: B13, false: B14)
6869
B13 = TargetBlock() idom:B4 in-loop:B4
6970
Goto(B4)
7071
B14 = TargetBlock() idom:B4 dominates:(B22, B21) in-loop:B2
71-
v19 = BinaryIntOp %(v8, v18)
72+
v19 = BinaryIntOp %(v30, v18)
7273
v20 = Comparison int ==(v19, v11)
7374
Branch(v20, true: B21, false: B22)
7475
B21 = TargetBlock() idom:B14 in-loop:B2

pkg/cfg/testcases/numbers.dart.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ B0 = EntryBlock()
99
B0 = EntryBlock() dominates:(B7)
1010
v4 = Constant(0)
1111
v23 = Constant(1)
12-
v25 = Constant(2)
12+
Constant(2)
1313
v39 = Constant(3)
14+
v44 = Constant(-2)
1415
v1 = Parameter(a)
1516
v2 = Parameter(b)
1617
v3 = Parameter(data)
@@ -25,8 +26,8 @@ B13 = TargetBlock() idom:B7 in-loop:B7
2526
v20 = BinaryIntOp ~/(v43, v1)
2627
v22 = BinaryIntOp *(v20, v2)
2728
v24 = BinaryIntOp >>(v22, v23)
28-
v26 = BinaryIntOp -(v24, v25)
29-
v27 = InterfaceCall List.[](v3, v26)
29+
v45 = BinaryIntOp +(v24, v44)
30+
v27 = InterfaceCall List.[](v3, v45)
3031
v29 = BinaryIntOp <<(v27, v1)
3132
v31 = BinaryIntOp %(v29, v2)
3233
v32 = BinaryIntOp +(v42, v31)
@@ -40,8 +41,9 @@ B14 = TargetBlock() idom:B7
4041
B0 = EntryBlock() dominates:(B8)
4142
v4 = Constant(0.0)
4243
v6 = Constant(0)
43-
v22 = Constant(3)
44+
Constant(3)
4445
v36 = Constant(1)
46+
v46 = Constant(3.0)
4547
v1 = Parameter(a)
4648
v2 = Parameter(b)
4749
v3 = Parameter(data)
@@ -54,8 +56,7 @@ B8 = JoinBlock(B0, B14) idom:B0 dominates:(B15, B14) loop-header (depth:1 body:(
5456
Branch(v13, true: B14, false: B15)
5557
B14 = TargetBlock() idom:B8 in-loop:B8
5658
v21 = InterfaceCall List.[](v3, v45)
57-
v23 = UnaryIntOp toDouble(v22)
58-
v24 = BinaryDoubleOp +(v21, v23)
59+
v24 = BinaryDoubleOp +(v21, v46)
5960
v26 = BinaryDoubleOp *(v24, v1)
6061
v28 = UnaryIntOp toDouble(v2)
6162
v29 = BinaryDoubleOp /(v26, v28)
@@ -66,8 +67,7 @@ B14 = TargetBlock() idom:B8 in-loop:B8
6667
v37 = BinaryIntOp +(v45, v36)
6768
Goto(B8)
6869
B15 = TargetBlock() idom:B8
69-
v41 = UnaryIntOp toDouble(v22)
70-
v42 = BinaryDoubleOp remainder(v44, v41)
70+
v42 = BinaryDoubleOp remainder(v44, v46)
7171
Return(v42)
7272

7373
--- main
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
void constantFoldInt() {
6+
var x = 100;
7+
var y = 24;
8+
print(x + y);
9+
print(x - y);
10+
print(x * y);
11+
print(x ~/ y);
12+
print(x % y);
13+
print(x.remainder(y));
14+
print(x / y);
15+
print(x | y);
16+
print(x & y);
17+
print(x ^ y);
18+
print(x == y);
19+
print(x > y);
20+
print(x >= y);
21+
print(x < y);
22+
print(x <= y);
23+
print(x << y);
24+
print(x >> y);
25+
print((-x) >>> y);
26+
print(-x);
27+
print(~x);
28+
print(x.toDouble());
29+
print((-x).abs());
30+
print(x.sign);
31+
}
32+
33+
void constantFoldDouble() {
34+
var x = 4.0;
35+
var y = 0.5;
36+
var z = double.nan;
37+
print(x + y);
38+
print(x + z);
39+
print(x - y);
40+
print(x * y);
41+
print(x ~/ y);
42+
print(x % y);
43+
print(x.remainder(y));
44+
print(x / y);
45+
print(x == y);
46+
print(x == z);
47+
print(x > y);
48+
print(x > z);
49+
print(x >= y);
50+
print(x < y);
51+
print(x <= y);
52+
print(-x);
53+
print(x.ceil());
54+
print(y.ceilToDouble());
55+
print(y.round());
56+
print(x.roundToDouble());
57+
print((-x).truncate());
58+
print((-y).truncateToDouble());
59+
}
60+
61+
void moveConstantToRight(int x, double y, Object? obj) {
62+
print(2 + x);
63+
print(3 * x);
64+
print(4 == x);
65+
print(5 > x);
66+
print(6 >= x);
67+
print(7 < x);
68+
print(8 <= x);
69+
print(2 + y);
70+
print(3 * y);
71+
print(4 == y);
72+
print(5 > y);
73+
print(6 >= y);
74+
print(7 < y);
75+
print(8 <= y);
76+
print(null == obj);
77+
}
78+
79+
void arithmeticPatterns(int x, double y) {
80+
print(0 + x);
81+
print(0 - x);
82+
print(1 * x);
83+
print(x * 0);
84+
print(x * 16);
85+
print(x ~/ 32);
86+
print(x * (-1));
87+
print(x << 65);
88+
print(x >> 65);
89+
print(x >>> 65);
90+
print(x | 0);
91+
print(x | (-1));
92+
print(x | x);
93+
print(x & 0);
94+
print(x & (-1));
95+
print(x & x);
96+
print(x ^ 0);
97+
print(x ^ (-1));
98+
print(x ^ x);
99+
print(y * 1.0);
100+
print(y * y);
101+
}
102+
103+
void redundantPhi(int x) {
104+
if (x > 0) {
105+
x = x + 0;
106+
}
107+
print(x);
108+
}
109+
110+
void main() {}

0 commit comments

Comments
 (0)