-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1.c
109 lines (93 loc) · 1.82 KB
/
problem1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
Problem 1
Written by Sondre Engebråten
for (int i = 0; i < n; i++){
valueToInsert = array[i];
holePos = i;
while(holePos > 0 && valueToInsert < array[holePos-1]){
array[holePos] = array[holePos-1];
holePos = holePos - 1;
}
array[holePos] = valueToInsert;
}
Problem 1a
t0 = 0
startFor:
if t0 >= n goto exitFor
a0 = i*4
valueToInsert = array[a0];
holePos = i;
startWhile:
t2 = holePos > 0
a1 = holePos - 1
a2 = a1*4
t3 = valueToInsert < array[a2]
t4 = t2 && t3
if not t4: goto exitWhile
a5 = holePos*4
a6 = holePos - 1
a7 = a6*4
array[a5] = array[a7];
holePos = holePos - 1;
goto startWhile
exitWhile:
a8 = holePos * 4
array[a8] = valueToInsert;
t0 = t0 + 1
goto startFor
exitFor:
//}
Problem 1c
t0 = 0
startFor:
if t0 >= n goto exitFor
a0 = i*4
valueToInsert = array[a0];
holePos = i;
startWhile:
t2 = holePos > 0
a5 = holePos*4
a2 = a5 - 4
t3 = valueToInsert < array[a2]
t4 = t2 && t3
if not t4: goto exitWhile
array[a5] = array[a2];
holePos = holePos - 1;
goto startWhile
exitWhile:
array[a5] = valueToInsert;
t0 = t0 + 1
goto startFor
exitFor:
//}
Case 1 (Common subexpression):
a5 = holePos*4
a6 = holePos -1
a7 = a6*4
=>
a7 = 4*holePos - 4
=>
a7 = a5 - 4
Case 2 (Dead code):
a6 = holePos - 1
(a6 is never used and can be removed)
Case 3 (Copy propagation):
array[a5] = array[a7]
=>
array[a5] = array[a2]
Case 4 (Dead code):
a7 = a5 - 4
(a7 is never used)
Case 5 (Common subexpression):
a1 = holePos - 1
a5 = holePos*4
a2 = a1*4
=>
a5 = holePos*4
a2 = a5 - 1
(a1 is only used for a2, by using a5 instead it saves one line)
Case 6 (Copy propagation):
holePos is updated at start of for loop and end of whileloop, however when exiting the loop updating holepos is skipped. As such exitWhile block can reuse holePos from inside the whileloop
a5 = holePos*4
a8 = holePos*4
=>
replace a8 with a5