aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.misc-tests/optbench.c
blob: 9cee4305b64fd9e4708b37806fa25ca0ce87b014 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* ----------------------------------------------------- * 
 |                                                       | 
 |    PC Tech Journal Benchmark Series                   | 
 |    C Code Optimization Benchmark                      | 
 |                                                       | 
 |    Copyright (c) 1988 Ziff-Davis Publishing Company   | 
 |                                                       | 
 |    This benchmark code is designed to test the        | 
 |    code optimization techniques applied by a C        | 
 |    compiler.  It does not produce meaningful results  | 
 |    when executed, or represent good style.            | 
 |                                                       | 
 * ----------------------------------------------------- */ 
 
#include <string.h> 

#define max_vector   2 
#define constant5    5 
 
typedef    unsigned char   uchar; 
 
int    i,  j,  k , l,  m; 
int    i2, j2, k2; 
int    g3, h3, i3, k3, m3; 
int    i4, j4; 
int    i5, j5, k5; 
 
double flt_1, flt_2, flt_3, flt_4, flt_5, flt_6; 
 
int    ivector[ 3 ]; 
uchar  ivector2[ 3 ]; 
short  ivector4[ 6 ]; 
int    ivector5[ 203 ]; 
 
#ifndef NO_PROTOTYPES 
void   dead_code( int, char * ); 
void   unnecessary_loop( void ); 
void   loop_jamming( int ); 
void   loop_unrolling( int ); 
int    jump_compression ( int, int, int, int, int ); 
#else 
void   dead_code(); 
void   unnecessary_loop(); 
void   loop_jamming(); 
void   loop_unrolling(); 
int    jump_compression(); 
#endif 
 
 
int     main( argc, argv )      /* optbench */ 
        int  argc; 
        char     **argv; 
        { 
        /* ------------------------------ * 
         | Constant and copy propagation  | 
         * ------------------------------ */ 
 
        j4 = 2; 
        if( i2 < j4 && i4 < j4 ) 
           i5 = 2; 
 
        j4 = k5; 
        if( i2 < j4 && i4 < j4 ) 
           i5 = 3; 
        
        /* ---------------------------------------- * 
         |  Constant folding, arithmetic identities | 
         |  and redundant load/store operations     | 
         * ---------------------------------------- */ 
 
        i3 = 1 + 2; 
        flt_1 = 2.4 + 6.3; 
        i2 = 5;               
        j2 = i + 0; 
        k2 = i / 1; 
        i4 = i * 1; 
        i5 = i * 0; 
 
#ifndef NO_ZERO_DIVIDE 
        /* 
         *   Some compilers correctly recognize a zero divide 
         *   error and do not generate any object code. 
         */ 
/*
        i2 = i / 0;            
        flt_2 = flt_1 / 0.0; 
*/
#else    
/*
        printf( "This compiler handles divide-by-zero as an
error\n"); 
*/;
#endif 
        flt_3 = 2.4 / 1.0;  
        flt_4 = 1.0 + 0.0000001; 
        flt_5 = flt_6 * 0.0; 
        flt_6 = flt_2 * flt_3; 
  
        /* ------------ * 
         |  Dead store  | 
         * ------------ */ 
 
         k3 = 1; 
         k3 = 1; 
 
        /* --------------------- * 
         |  Strength reduction   | 
         * --------------------- */ 
 
        k2 = 4 * j5; 
           for( i = 0; i <= 5; i++ )
              ivector4[ i ] = i * 2; 
    
        /* ----------- * 
         | Simple loop | 
         * ----------- */ 
 
        j5 = 0; 
        k5 = 10000; 
        do { 
              k5 = k5 - 1; 
              j5 = j5 + 1; 
              i5 = ( k5 * 3 ) / ( j5 * constant5 ); 
           } while ( k5 > 0 ); 
 
        /* ----------------------------------- * 
         | Loop induction variable handling    | 
         * ----------------------------------- */ 
 
        for( i = 0; i < 100; i++) 
           ivector5[ i * 2 + 3 ] = 5; 
 
        /* ----------------------------- * 
         | Very busy expression handling | 
         * ----------------------------- */ 
 
        if( i < 10 ) 
           j5 = i5 + i2; 
        else 
           k5 = i5 + i2; 
 
        /* -------------------------------------------- * 
         | Check how the compiler generates the address | 
         | of a variable with a constant subscript,     | 
         | copy propagation, and register propagation.  | 
         * -------------------------------------------- */ 
 
         ivector[ 0  ] = 1;  /* constant address generation */ 
         ivector[ i2 ] = 2;  /* i2 should be a propagated value
*/ 
         ivector[ i2 ] = 2;  /* register propagation */ 
         ivector[ 2  ] = 3;  /* constant address generation */ 
     
     
         /* ---------------------------------- * 
          | Common subexpression elimination   | 
          * ---------------------------------- */ 
  
        if(( h3 + k3 ) < 0 || ( h3 + k3 ) > 5 )
        /* 
               printf("Common subexpression elimination\n"); 
        
        */;
        else { 
               m3 = ( h3 + k3 ) / i3; 
               g3 = i3 + (h3 + k3); 
         } 
        /* --------------------------------------- * 
         | Invariant code motion                   | 
         |  (j * k) can be moved outside the loop. |  
         * --------------------------------------- */ 
  
        for( i4 = 0; i4 <= max_vector; i4++ )    
           ivector2[ i4 ] = j * k; 
  
        /* ---------------------------- * 
         | Function call with arguments | 
         * ---------------------------- */ 
 
        dead_code( 1, "This line should not be printed" ); 
 
        /* ------------------------------- * 
         | Function call without arguments | 
         * ------------------------------- */ 
 
        unnecessary_loop(); 

	exit (0);
    
        }  /* end of main */ 
 
            
/* --------------------------------------------- * 
 | Function: dead_code                           | 
 |           Test for dead code and dead stores. | 
 |           NO code should be generated.        | 
 * ----------------------------------------------*/ 
 
void   dead_code( a, b ) 
       int a; 
       char *b; 
       { 
         int idead_store; 
 
         idead_store = a; 
         if( 0 )
         /* 
           printf( "%s\n", b );
          */; 
       }  /* end of dead_code */ 
 
/* ----------------------------------------------- * 
 | Function: unnecessary_loop                      | 
 |           The loop in the following function is | 
 |           not necessary since the value of the  | 
 |           assignment is constant. Ideally, the  | 
 |           loop should be optimized out.         | 
 * ----------------------------------------------- */ 
 
void   unnecessary_loop() 
       { 
           int x;  
 
           x = 0; 
           for( i = 0; i < 5; i++ ) /* loop should not be generated */ 
           k5 = x + j5;                  
       }   /* end of unnecessary_loop */ 
 
/* --------------------------------------------- * 
 | Function: loop_jamming                        | 
 |           The two loop in this function share | 
 |           the same loop conditions and could  | 
 |           be coalesced together.              | 
 * --------------------------------------------- */ 
 
void   loop_jamming( x ) 
       int x; 
       { 
          for( i = 0; i < 5; i++ ) 
             k5 = x + j5 * i; 
          for( i = 0; i < 5; i++ ) 
             i5 = x * k5 * i; 
       }  /* end of loop_jamming */ 
 
/* ------------------------------------------------ * 
 | Function: loop_unrolling                         | 
 |           The loop in this function should be    | 
 |           replaced with three inline word stores | 
 |           using constant array arithmetic or by  | 
 |           specialized machine instructions used  | 
 |           for block memory initializiation.      | 
 * ------------------------------------------------ */ 
 
void  loop_unrolling( x ) 
      int x; 
      { 
         for( i = 0; i < 6; i++ ) 
           ivector4[ i ] = 0; 
       }  /* end of loop_unrolling */ 
 
/* ------------------------------------------------------ * 
 | Function: jump_compression                             | 
 |           This awkward code is useful to illustrate    | 
 |           jump chain compression. The goto 'end_1' can | 
 |           be replaced by a direct jump to 'beg_1'.     | 
 *------------------------------------------------------ */ 
 
int  jump_compression ( i, j, k, l, m ) 
int i,j ,l, m; 
        { 
beg_1: 
           if( i < j ) 
               if( j < k ) 
                   if( k < l ) 
                       if ( l < m ) 
                           l += m; 
                       else 
                           goto end_1;
                   else 
                       k += l; 
               else { 
                   j += k; 
end_1:            goto beg_1; 
               } 
           else 
               i += j; 
          return( i + j + k + l + m ); 
        }   /* end of jump_compression */