Skip to content

Commit 3df15e9

Browse files
authored
Update example.md
1 parent 5269446 commit 3df15e9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/step13/example.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,65 @@ int f() {
109109
- 如果不能合并,那么把 `a``b` 中间的虚线边改为实线,表示不再考虑二者合并的情况。
110110

111111
上面的说明只是简要介绍了算法的原理,请阅读论文 [TOPLAS'1996: *Iterated Register Coalescing*](https://dl.acm.org/doi/pdf/10.1145/229542.229546) 获取更详细的说明。**别忘了论文末尾的附录有完整的伪代码实现。**
112+
113+
## 如何比较新的寄存器分配算法有哪些提升
114+
115+
下面是两个例子,分别是有大量活跃变量和大量分支语句,**助教以C的语法编写,不符合minidecaf语法**,你可以设计类似的测试样例来说明新的寄存器分配算法的效果,你可以比较运行时间以及生成的汇编代码:
116+
117+
```c
118+
int test_many_branches() {
119+
int a = 0, b = 1, c = 2, d = 3;
120+
int result = 0;
121+
122+
for (int x = 0; x < 10000; x++) {
123+
if (x % 2 == 0) {
124+
a += 1;
125+
if (x % 3 == 0) {
126+
b += 2;
127+
} else {
128+
c += 3;
129+
}
130+
} else {
131+
d += 4;
132+
if (x % 5 == 0) {
133+
a -= 1;
134+
} else if (x % 7 == 0) {
135+
b -= 2;
136+
} else {
137+
c -= 3;
138+
}
139+
}
140+
141+
result += a + b + c + d;
142+
143+
if (result % 100 == 0) {
144+
d += 1;
145+
}
146+
}
147+
148+
return result;
149+
}
150+
```
151+
152+
```c
153+
int test_many_live_variables() {
154+
int a = 1, b = 2, c = 3, d = 4, e = 5;
155+
int f = 6, g = 7, h = 8, i = 9, j = 10;
156+
int k = 11, l = 12, m = 13, n = 14, o = 15;
157+
int p = 16, q = 17, r = 18, s = 19, t = 20;
158+
159+
int result = 0;
160+
161+
for (int x = 0; x < 10000; x++) {
162+
result += a + b + c + d + e + f + g + h + i + j
163+
+ k + l + m + n + o + p + q + r + s + t;
164+
165+
a += 1; b += 2; c += 3; d += 4; e += 5;
166+
f += 6; g += 7; h += 8; i += 9; j += 10;
167+
k += 11; l += 12; m += 13; n += 14; o += 15;
168+
p += 16; q += 17; r += 18; s += 19; t += 20;
169+
}
170+
171+
return result;
172+
}
173+
```

0 commit comments

Comments
 (0)