-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkod.c
141 lines (119 loc) · 1.64 KB
/
kod.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
void use(int var);
void escape(int *var);
void side_effect(void);
int a,b;
// Side effects and escape analysis
void code_1a()
{
int a,b;
a = 1;
b = a + 2;
printf("Hello");
a = b + 1;
use(a);
b = a + 1;
use(b);
}
void code_1b()
{
int a,b;
a = 1;
b = a + 2;
printf("Hello");
a = b + 1;
escape(&a);
b = a + 1;
use(b);
}
void code_1c()
{
a = 1;
b = a + 2;
printf("Hello");
a = b + 1;
use(a);
b = a + 1;
use(b);
}
// volatile & compiler barrier
void code_1d()
{
int a;
a = 1;
b = a + 2;
asm volatile("":::"memory");
a = b + 1;
use(a);
b = a + 1;
use(b);
}
// Loop invariant
int code_2a(int a, int n)
{
int i, sum = 0;
for (i = 0; i < n; i++) {
if (a == 0)
sum += 1;
else
sum += 2;
}
return sum;
}
int code_2c(int a, int n)
{
int i, sum = 0;
for (i = 0; i < n; i++) {
escape(&a);
if (a % 2 == 0)
sum += 2;
else
sum += 1;
}
return sum;
}
// Aliasing
int code_3a(int *a, int *b, int n)
{
int i, sum=0;
for (i = 0; i < n; i++) {
a[i] += b[0];
}
return sum;
}
int code_3b(int *a, int *b, int n)
{
int i, sum=0;
int b0 = b[0];
for (i = 0; i < n; i++) {
a[i] += b0;
}
return sum;
}
int code_3c(int * __restrict__ a, int * __restrict__ b, int n)
{
int i, sum=0;
for (i = 0; i < n; i++) {
a[i] += b[0];
}
return sum;
}
int code_3d(int * __restrict__ a, int * __restrict__ b, int n)
{
int i, sum=0;
escape(b);
for (i = 0; i < n; i++) {
side_effect();
a[i] += b[0];
}
return sum;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
printf("b = %d\n",b);
return 0;
}
// vim: tabstop=2 shiftwidth=2 expandtab