Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251
WebSVN – tendra.SVN – Diff – /trunk/src/installers/common/construct/foralls.c – Rev 2 and 7

Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
 
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
Line 77... Line 107...
77
 
107
 
78
#define topsh f_top
108
#define topsh f_top
79
 
109
 
80
#ifndef Assert
110
#ifndef Assert
81
#if FS_STDC_HASH
111
#if FS_STDC_HASH
82
#define Assert(x) if(!(x)) failer(#x)
112
#define Assert(x) if (!(x)) failer(#x)
83
#else
113
#else
84
#define Assert(x) if(!(x)) failer("x")
114
#define Assert(x) if (!(x)) failer("x")
85
#endif
115
#endif
86
#endif
116
#endif
87
 
117
 
88
exp * ifc_ptr_position
118
exp *
89
    PROTO_N ( (e) )
-
 
90
    PROTO_T ( exp e )
119
ifc_ptr_position(exp e)
91
{
120
{
92
	exp * a;
121
	exp *a;
93
	exp dad = father(e);
122
	exp dad = father(e);
94
	if (son(dad)==e) {
123
	if (son(dad) == e) {
95
		a = &son(dad);
124
		a = &son(dad);
96
	}
-
 
97
	else {
125
	} else {
98
		exp sib = son(dad);
126
		exp sib = son(dad);
99
		while (bro(sib)!=e) { sib = bro(sib); }
127
		while (bro(sib) != e) {
-
 
128
			sib = bro(sib);
-
 
129
		}
100
		a = &bro(sib);
130
		a = &bro(sib);
101
	}
131
	}
102
	return a;
132
	return a;
103
}
133
}
104
 
134
 
-
 
135
 
105
static exp * position
136
static exp *
106
    PROTO_N ( (e) )
-
 
107
    PROTO_T ( exp e )
137
position(exp e)
108
{
138
{
109
	exp dad = father(e);
139
	exp dad = father(e);
110
	exp *res = &son(dad);
140
	exp *res = &son(dad);
111
	while (*res != e) res = &bro(*res);
141
	while (*res != e) {
-
 
142
		res = &bro(*res);
-
 
143
	}
112
	return res;
144
	return res;
113
}
145
}
-
 
146
 
114
 
147
 
115
int incr_var
148
int
116
    PROTO_N ( (e) )
-
 
117
    PROTO_T ( exp e )
149
incr_var(exp e)
118
{
150
{
119
	/* is e  var = var + val; */
151
	/* is e  var = var + val; */
120
	exp dest;
152
	exp dest;
121
	exp src;
153
	exp src;
122
	if(name(e) != ass_tag) return false;
154
	if (name(e) != ass_tag) {
-
 
155
		return false;
-
 
156
	}
123
	dest = son(e);
157
	dest = son(e);
124
	src = bro(dest);
158
	src = bro(dest);
125
	return (name(dest)==name_tag &&
159
	return (name(dest) == name_tag && name(src) == plus_tag &&
126
                name(src)==plus_tag &&
-
 
127
                name(son(src))==name_tag &&
-
 
128
		name(bro(son(src)))==val_tag &&
160
		name(son(src)) == name_tag && name(bro(son(src))) == val_tag &&
129
                last(bro(son(src))) &&
-
 
130
		son(dest)==son(son(src)) &&
161
		last(bro(son(src))) && son(dest) == son(son(src)) &&
131
                no(dest) == no(son(src))
162
                no(dest) == no(son(src)));
132
               );
-
 
133
}
163
}
-
 
164
 
134
 
165
 
135
exp alteredset = nilexp;
166
exp alteredset = nilexp;
136
 
167
 
137
void isaltered
168
void
138
    PROTO_N ( (ld, always) )
-
 
139
    PROTO_T ( exp ld X int always )
169
isaltered(exp ld, int always)
140
{
170
{
141
	/* make copy of name!! - can be killed later */
171
	/* make copy of name!! - can be killed later */
142
	exp z ;
172
	exp z;
143
	exp nld;
173
	exp nld;
144
	for (z = alteredset; z!=nilexp; z=bro(z)) {
174
	for (z = alteredset; z != nilexp; z=bro(z)) {
145
	     if (son(ld)==son(son(z)) &&
175
	     if (son(ld) == son(son(z)) &&
146
		    (name(son(ld))!=proc_tag || no(ld) == no(son(z)) ) ){
176
		 (name(son(ld)) != proc_tag || no(ld) == no(son(z)))) {
147
		props(z) &= (prop)always;
177
		props(z) &= (prop)always;
148
		return;
178
		return;
149
	     }
179
	     }
150
	}
180
	}
151
	nld = getexp(sh(ld), alteredset, 1, son(ld), nilexp,
181
	nld = getexp(sh(ld), alteredset, 1, son(ld), nilexp, props(ld), no(ld),
152
                                             props(ld), no(ld), name(ld));
182
		     name(ld));
153
	alteredset = getexp(nilexp, alteredset, alteredset==nilexp,
183
	alteredset = getexp(nilexp, alteredset, alteredset == nilexp, nld,
154
                                                nld, nilexp, (prop)always,0,0);
184
			    nilexp, (prop)always, 0, 0);
155
}
185
}
-
 
186
 
156
 
187
 
157
int assign_alias;
188
int assign_alias;
158
int jump_out;
189
int jump_out;
159
 
190
 
160
void scan_for_incr
191
void
161
    PROTO_N ( (e, piece, f) )
-
 
162
    PROTO_T ( exp e X exp piece X void (*f) PROTO_S ((exp,int)) )
192
scan_for_incr(exp e, exp piece, void(*f)(exp, int))
163
{
193
{
164
	/* applies f to all (var = var + val) done  in piece with bool set if
194
	/* applies f to all (var = var + val) done in piece with bool set if
165
			always done.
-
 
166
		where var is non-local of piece but non-global and not
195
	 * always done.  where var is non-local of piece but non-global and not
167
		aliased and all other uses in piece are cont(var)
196
	 * aliased and all other uses in piece are cont(var) also applies
168
	   also applies altered to  named variables (name_tag) which are
197
	 * altered to  named variables (name_tag) which are assigned to, either
169
		assigned to, either in toto or by indexing.
-
 
170
	   if there are any other assign_alias is set true
198
	 * in toto or by indexing.  if there are any other assign_alias is set
171
	*/
199
	 * true */
172
    static int everytime = true;
200
    static int everytime = true;
173
    switch (name(e)){
201
    switch (name(e)) {
-
 
202
	case name_tag:
174
	case name_tag: case env_offset_tag: return;
203
	case env_offset_tag
-
 
204
	    : return;
175
	case ass_tag:{
205
	case ass_tag: {
176
	   exp dest = son(e);
206
	   exp dest = son(e);
177
	   exp src = bro(son(e));
207
	   exp src = bro(son(e));
178
	   if (name(dest)== name_tag  &&
208
	   if (name(dest) == name_tag && isvar(son(dest)) &&
179
               isvar(son(dest)) &&
-
 
180
               !isglob(son(dest)) &&
-
 
181
	       iscaonly(son(dest)) &&
209
	       !isglob(son(dest)) && iscaonly(son(dest)) &&
182
               name(src) == plus_tag )
210
	       name(src) == plus_tag) {
183
	       {exp la = son(src);
211
		exp la = son(src);
184
		exp ra = bro(son(src));
212
		exp ra = bro(son(src));
185
		if (last(ra) && name(ra)==val_tag &&
213
		if (last(ra) && name(ra) == val_tag && name(la) == cont_tag &&
186
                    name(la) == cont_tag &&
-
 
187
	            name(son(la)) == name_tag &&
-
 
188
                    son(son(la))== son(dest) &&
214
	            name(son(la)) == name_tag && son(son(la)) == son(dest) &&
189
	            no(dest) == no(son(la)) &&
-
 
190
                    !intnl_to(piece, son(dest)) )
215
	            no(dest) == no(son(la)) && !intnl_to(piece, son(dest))) {
191
		    {exp p = pt(son(dest)); /*uses of var */
216
		     exp p = pt(son(dest)); /*uses of var */
192
		     for(; p!=nilexp; p = pt(p))
217
		     for (; p != nilexp; p = pt(p)) {
193
#ifdef NEWDIAGS
218
#ifdef NEWDIAGS
194
			{if (isdiaginfo(p) || p==dest || p == son(la) ||
219
			if (isdiaginfo(p) || p == dest || p == son(la) ||
195
#else
220
#else
196
			{if (p==dest || p == son(la) ||
221
			if (p == dest || p == son(la) ||
197
#endif
222
#endif
198
				 (last(p) && name(bro(p))==cont_tag) ||
223
			    (last(p) && name(bro(p)) == cont_tag) ||
199
				 incr_var(father(p)) ||
224
			    incr_var(father(p)) || !intnl_to(piece, p)) {
200
				 !intnl_to(piece, p))
-
 
201
			     {continue;}
225
			    continue;
-
 
226
			}
202
			 goto tryalias;
227
			goto tryalias;
203
		        }
228
		     }
204
		     f(e, everytime); return;
229
		     f(e, everytime);
205
		    }
230
		     return;
206
	      }
231
		}
207
	tryalias:
-
 
208
	   if (name(dest) == name_tag && isvar(son(dest)) ) {
-
 
209
		isaltered(dest, everytime);
-
 
210
	   }
232
	   }
211
	   else
233
tryalias:
212
	   if (name(dest) == addptr_tag && name(son(dest))==name_tag
234
	   if (name(dest) == name_tag && isvar(son(dest))) {
213
		&& isvar(son(son(dest))) ){
-
 
214
		isaltered(son(dest), everytime);
235
		isaltered(dest, everytime);
215
	   }
-
 
216
	   else
236
	   } else if (name(dest) == addptr_tag && name(son(dest)) == name_tag &&
217
	   if (name(dest)==reff_tag &&
237
		      isvar(son(son(dest)))) {
218
               name(son(dest))== addptr_tag &&
238
	        isaltered(son(dest), everytime);
219
	       name(son(son(dest))) == name_tag &&
239
	   } else if (name(dest) == reff_tag && name(son(dest)) == addptr_tag &&
220
               isvar(son(son(son(dest)))) )
240
	       name(son(son(dest))) == name_tag && isvar(son(son(son(dest))))) {
221
		{isaltered(son(son(dest)), everytime);}
241
		   isaltered(son(son(dest)), everytime);
222
	   else
-
 
223
	   if (name(dest) == name_tag &&
242
	   } else if (name(dest) == name_tag &&
224
		( props(son(dest)) & 0x10) !=0 /* const in some loop */ ) {
243
		      (props(son(dest)) & 0x10) != 0) {
-
 
244
		   /* const in some loop */
225
		exp def = son(son(dest));
245
		   exp def = son(son(dest));
226
	   	if (name(def) == reff_tag) def = son(def);
246
		   if (name(def) == reff_tag) {
-
 
247
			   def = son(def);
-
 
248
		   }
227
	 	if (name(def) == addptr_tag && name(son(def)) == name_tag &&
249
		   if (name(def) == addptr_tag && name(son(def)) == name_tag &&
228
			isvar(son(son(def))) ) {
250
			isvar(son(son(def)))) {
229
			isaltered(dest, everytime);
251
			isaltered(dest, everytime);
-
 
252
		} else {
-
 
253
			assign_alias = true;
230
		}
254
		}
-
 
255
	   } else {
231
		else assign_alias = true;
256
		   assign_alias = true;
232
	   }
257
	   }
233
	   else assign_alias = true;
-
 
234
	   scan_for_incr(dest, piece, f);
258
	   scan_for_incr(dest, piece, f);
235
	   scan_for_incr(src, piece, f);
259
	   scan_for_incr(src, piece, f);
236
	   return ;
260
	   return;
237
	}
261
	}
238
 
262
 
239
	case case_tag: {
263
	case case_tag:
240
		scan_for_incr(son(e), piece, f);
264
		scan_for_incr(son(e), piece, f);
241
		everytime = false;
265
		everytime = false;
242
		return;
266
		return;
243
	}
-
 
244
 
267
 
245
	case goto_tag: case testbit_tag:
268
	case goto_tag: case testbit_tag:
246
	case test_tag: {
269
	case test_tag: {
247
		int x = intnl_to(piece, pt(e));
270
		int x = intnl_to(piece, pt(e));
-
 
271
		if (!x) {
248
		if (!x) jump_out = true;
272
			jump_out = true;
-
 
273
		}
-
 
274
		if (son(e) != nilexp) {
249
		if (son(e)!=nilexp) scan_for_incr(son(e), piece, f);
275
			scan_for_incr(son(e), piece, f);
-
 
276
		}
250
		everytime = (everytime && !x);
277
		everytime = (everytime && !x);
251
		return;
278
		return;
252
	}
279
	}
253
	case res_tag: {
280
	case res_tag: {
254
		jump_out = true;
281
		jump_out = true;
Line 256... Line 283...
256
		return;
283
		return;
257
	}
284
	}
258
	case solve_tag: case rep_tag: case cond_tag: {
285
	case solve_tag: case rep_tag: case cond_tag: {
259
	        exp z = son(e);
286
	        exp z = son(e);
260
		int et = everytime;
287
		int et = everytime;
261
		for(;;) {
288
		for (;;) {
262
			scan_for_incr(z, piece, f);
289
			scan_for_incr(z, piece, f);
263
			everytime = false;
290
			everytime = false;
264
			if (last(z)) break;
291
			if (last(z)) {
-
 
292
				break;
-
 
293
			}
265
			z=bro(z);
294
			z = bro(z);
266
		}
295
		}
267
		everytime = et;
296
		everytime = et;
268
		return;
297
		return;
269
	}
298
	}
270
 
299
 
271
	case apply_tag: assign_alias = true; /* and do default */
300
	case apply_tag: assign_alias = true; /* and do default */
272
	default: {
301
	default: {
273
	    exp z = son(e);
302
	    exp z = son(e);
274
	    if (z==nilexp) return;
303
	    if (z == nilexp) {
-
 
304
		    return;
-
 
305
	    }
275
	    for(;;) {
306
	    for (;;) {
276
		scan_for_incr(z, piece, f);
307
		scan_for_incr(z, piece, f);
277
		if (last(z)) return;
308
		if (last(z)) {
-
 
309
			return;
-
 
310
		}
278
		z = bro(z);
311
		z = bro(z);
279
	    }
312
	    }
280
	}
313
	}
281
    }
314
    }
282
}
315
}
283
 
316
 
284
 
317
 
285
int good_val
318
int
286
    PROTO_N ( (a, piece) )
-
 
287
    PROTO_T ( exp a X exp piece )
319
good_val(exp a, exp piece)
288
{
320
{
289
	/* result ((a is name external to piece)
321
	/* result ((a is name external to piece)
290
           || (a is cont(name) where all uses of name in piece is cont))
322
           || (a is cont(name) where all uses of name in piece is cont))
291
	*/
323
	*/
292
     if (name(a)==name_tag) {
324
     if (name(a) == name_tag) {
293
	return (!intnl_to(piece, son(a)));
325
	return (!intnl_to(piece, son(a)));
294
     }
-
 
295
     else
-
 
296
     if (name(a)==cont_tag && name(son(a))==name_tag &&
326
     } else if (name(a) == cont_tag && name(son(a)) == name_tag &&
297
		!intnl_to(piece, son(son(a))) && !isvis(son(son(a)))) {
327
		!intnl_to(piece, son(son(a))) && !isvis(son(son(a)))) {
298
	exp lda = son(a);
328
	exp lda = son(a);
299
	exp pa = pt(son(lda));
329
	exp pa = pt(son(lda));
300
	for(; pa!=nilexp; pa = pt(pa)) {
330
	for (; pa != nilexp; pa = pt(pa)) {
301
#ifdef NEWDIAGS
331
#ifdef NEWDIAGS
302
	     if (isdiaginfo(pa) || pa==lda || no(pa) != no(lda) ||
332
	     if (isdiaginfo(pa) || pa == lda || no(pa) != no(lda) ||
303
#else
333
#else
304
	     if (pa==lda || no(pa) != no(lda) ||
334
	     if (pa == lda || no(pa) != no(lda) ||
305
#endif
335
#endif
306
		   (last(pa) && name(bro(pa))==cont_tag) ||
336
		 (last(pa) && name(bro(pa)) == cont_tag) ||
307
			!intnl_to(piece, pa) ) continue;
337
		 !intnl_to(piece, pa))continue;
308
	     break;
338
	     break;
309
	}
339
	}
310
	return (pa==nilexp);
340
	return (pa == nilexp);
311
     }
341
     }
312
     return false;
342
     return false;
313
}
343
}
-
 
344
 
314
 
345
 
315
int usage_in
346
int
316
    PROTO_N ( (whole, part) )
-
 
317
    PROTO_T ( exp whole X exp part )
347
usage_in(exp whole, exp part)
318
{
348
{
319
  exp q = part;
349
  exp q = part;
320
  int res = 1;
350
  int res = 1;
321
  int n = (int)name(q);
351
  int n = (int)name(q);
322
  while (q != whole && q != nilexp &&
352
  while (q != whole && q != nilexp &&
323
      (n != ident_tag || (props (q) & 0x40) == 0)) {
353
	 (n != ident_tag || (props(q) & 0x40) == 0)) {
324
    q = father (q);
354
    q = father(q);
325
    n = (int)name(q);
355
    n = (int)name(q);
326
    if(n==cond_tag || n==rep_tag || n==solve_tag) res=2;
356
    if (n == cond_tag || n == rep_tag || n == solve_tag) {
-
 
357
	    res=2;
-
 
358
    }
327
  }
359
  }
328
 
360
 
329
 
361
 
330
  return (q == whole)?res:0;
362
  return (q == whole)?res:0;
331
}
363
}
332
 
364
 
333
int stride;
365
int stride;
334
/* 0 initially, -1 either no common stride or non-constant stride,
366
/* 0 initially, -1 either no common stride or non-constant stride, otherwise
335
               otherwise contains common stride. */
367
 * contains common stride. */
336
 
368
 
337
int find_common_index
369
int
338
    PROTO_N ( (ldname, piece, f) )
-
 
339
    PROTO_T ( exp ldname X exp piece X void (*f) PROTO_S ((exp,int)) )
370
find_common_index(exp ldname, exp piece, void(*f)(exp, int))
340
{
371
{
341
 
372
 
342
	/* applies f to all addptr(x, cont(varid)) in piece
373
	/* applies f to all addptr(x, cont(varid)) in piece where
343
			where good_index_factor(1)
-
 
344
		     and all addptr(x, mult(cont(varid), mval))
374
	 * good_index_factor(1) and all addptr(x, mult(cont(varid), mval))
345
			where good_index_factor(mval) and
375
	 * where good_index_factor(mval) and and all test(cont(varid), val ->
346
		     and all test(cont(varid), val -> outside piece)
-
 
347
	 	   with bool if it is done exactly once.
376
	 * outside piece) with bool if it is done exactly once.  if different
348
                   if different multiply facters are detected or
-
 
349
                   a multiplying factor is not good then stride is
377
	 * multiply facters are detected or a multiplying factor is not good
350
                   set to -1 and the procedure returned from.
378
	 * then stride is set to -1 and the procedure returned from.  stride
351
                stride holds the common multiplying factor, initially
379
	 * holds the common multiplying factor, initially being 0.  where x is
352
                being 0.
-
 
353
		where x is  name external to piece
-
 
354
		or x is cont(name) where name is external to piece and
380
	 * name external to piece or x is cont(name) where name is external to
355
		    all uses in piece are cont(name) NB no alias check
381
	 * piece and all uses in piece are cont(name) NB no alias check result
356
		result is no of other uses of varid in piece
382
	 * is no of other uses of varid in piece */
357
	*/
-
 
358
     exp p = pt(son(ldname));
383
     exp p = pt(son(ldname));
359
     int otheruses = 0;
384
     int otheruses = 0;
360
     for(; p!=nilexp; p = pt(p)) { /* examine each use of loop variable */
385
     /* examine each use of loop variable */
-
 
386
     for(; p != nilexp; p = pt(p)) {
361
	int usagex;
387
	int usagex;
362
#ifdef NEWDIAGS
388
#ifdef NEWDIAGS
363
	if (isdiaginfo(p)) continue;
389
	if (isdiaginfo(p)) {
-
 
390
		continue;
-
 
391
	}
364
#endif
392
#endif
365
	if (no(p)!=no(ldname)) continue;
393
	if (no(p) != no(ldname)) {
-
 
394
		continue;
-
 
395
	}
366
	usagex = usage_in(piece, p);
396
	usagex = usage_in(piece, p);
367
	if (usagex==0) continue;
397
	if (usagex == 0) {
-
 
398
		continue;
-
 
399
	}
368
	otheruses++;
400
	otheruses++;
369
	if (last(p) && name(bro(p))==cont_tag ) {
401
	if (last(p) && name(bro(p)) == cont_tag) {
370
	     exp dad = father(bro(p));
402
	     exp dad = father(bro(p));
371
             if (!good_index_factor(1))
403
             if (!good_index_factor(1)) {
372
                {stride = -1; UNUSED(dad); UNUSED(f); return 0;}
-
 
373
	     if (name(dad) == addptr_tag &&
-
 
374
                 bro(son(dad))==bro(p) &&
-
 
375
                 last(bro(p))
404
                stride = -1;
376
	        ) {
405
		UNUSED(dad);
377
		if(good_val(son(dad), piece))
406
		UNUSED(f);
378
                   { f(dad,usagex==1);
-
 
379
                     otheruses--;
-
 
380
                     if (stride==0) stride=1;
-
 
381
                     else if (stride>1) stride = -1; UNUSED(f); return 0;
-
 
382
                   }
407
		return 0;
383
	     }
408
	     }
-
 
409
	     if (name(dad) == addptr_tag && bro(son(dad)) == bro(p) &&
384
	     else
410
		 last(bro(p))) {
-
 
411
		if (good_val(son(dad), piece)) {
385
	     if (name(dad)==offset_mult_tag &&
412
		     f(dad, usagex == 1);
386
                 son(dad)==bro(p) &&
413
                     otheruses--;
387
                 name(bro(son(dad)))==val_tag &&
414
                     if (stride == 0) {
-
 
415
			     stride = 1;
-
 
416
		     } else if (stride > 1) {
-
 
417
			     stride = -1;
388
	         last(dad)
418
			     UNUSED(f);
389
                )
419
			     return 0;
-
 
420
		     }
-
 
421
		}
-
 
422
	     } else if (name(dad) == offset_mult_tag && son(dad) == bro(p) &&
-
 
423
			name(bro(son(dad))) == val_tag && last(dad)) {
390
	       {exp grandad = father(dad);
424
		     exp grandad = father(dad);
391
                if (!good_index_factor(no(bro(son(dad)))/8))
425
                if (!good_index_factor(no(bro(son(dad))) / 8)) {
392
                    {stride = -1; UNUSED(grandad); return 0;}
426
			stride = -1;
393
		if (name(grandad)==addptr_tag &&
427
			UNUSED(grandad);
-
 
428
			return 0;
-
 
429
		}
394
                    bro(son(grandad)) == dad &&
430
		if (name(grandad) == addptr_tag && bro(son(grandad)) == dad &&
395
                    last(dad))
431
		    last(dad)) {
396
		    {if(good_val(son(grandad), piece))
432
		      if (good_val(son(grandad), piece)) {
397
			 {f(grandad,usagex==1);
433
			  f(grandad, usagex == 1);
398
                          otheruses--;
434
                          otheruses--;
399
                          if (stride==0) stride=no(bro(son(dad)))/8;
435
                          if (stride == 0) {
400
                          else if (stride!=no(bro(son(dad)))/8)
436
			      stride = no(bro(son(dad))) / 8;
401
                              {stride = -1; return 0;}
437
			  } else if (stride != no(bro(son(dad))) / 8) {
402
                           /*printf("stride=%d\n",stride);*/
438
			      stride = -1;
403
                         }
439
			      return 0;
404
		    }
440
			  }
-
 
441
                           /*printf("stride=%d\n", stride);*/
405
	       }
442
		      }
406
	     else
443
		}
407
	     if ((name(dad)==test_tag || name(dad)==testbit_tag)
444
	     } else if ((name(dad) == test_tag || name(dad) == testbit_tag) &&
408
			 && piece == bro(son(pt(dad))) )
445
			piece == bro(son(pt(dad)))) {
409
		{f(dad, usagex==1); otheruses--;}
446
		     f(dad, usagex == 1);
-
 
447
		     otheruses--;
-
 
448
	     }
410
	}
449
	}
411
     }
450
     }
412
      return otheruses;
451
      return otheruses;
413
}
452
}
-
 
453
 
414
 
454
 
415
int find_pointer_opt
455
int
416
    PROTO_N ( (ldname, piece, f) )
-
 
417
    PROTO_T ( exp ldname X exp piece X void (*f) PROTO_S ((exp,int)) )
456
find_pointer_opt(exp ldname, exp piece, void(*f)(exp, int))
418
{
457
{
419
 
458
 
420
	/* applies f to all addptr(x, cont(varid)) in piece
459
	/* applies f to all addptr(x, cont(varid)) in piece where
421
			where good_pointer_factor(1)
-
 
422
		     and all addptr(x, mult(cont(varid), mval))
460
	 * good_pointer_factor(1) and all addptr(x, mult(cont(varid), mval))
423
			where good_pointer_factor(mval) and
461
	 * where good_pointer_factor(mval) and and all test(cont(varid), val ->
424
		     and all test(cont(varid), val -> outside piece)
-
 
425
	 	   with bool if it is done exactly once.
462
	 * outside piece) with bool if it is done exactly once.  where x is
426
		where x is  name external to piece
-
 
427
		or x is cont(name) where name is external to piece and
463
	 * name external to piece or x is cont(name) where name is external to
428
		    all uses in piece are cont(name) NB no alias check
464
	 * piece and all uses in piece are cont(name) NB no alias check result
429
		result is no of other uses of varid in piece
465
	 * is no of other uses of varid in piece */
430
	*/
-
 
431
     exp p = pt(son(ldname));
466
     exp p = pt(son(ldname));
432
     int otheruses = 0;
467
     int otheruses = 0;
-
 
468
     for(; p != nilexp; p = pt(p)) {
433
     for(; p!=nilexp; p = pt(p)) { /* examine each use of loop variable */
469
	/* examine each use of loop variable */
434
	int usagex;
470
	int usagex;
435
#ifdef NEWDIAGS
471
#ifdef NEWDIAGS
436
	if (isdiaginfo(p)) continue;
472
	if (isdiaginfo(p)) {
-
 
473
		continue;
-
 
474
	}
437
#endif
475
#endif
438
	if (no(p)!=no(ldname)) continue;
476
	if (no(p) != no(ldname)) {
-
 
477
		continue;
-
 
478
	}
439
	usagex = usage_in(piece, p);
479
	usagex = usage_in(piece, p);
440
	if (usagex==0) continue;
480
	if (usagex == 0) {
-
 
481
		continue;
-
 
482
	}
441
	otheruses++;
483
	otheruses++;
442
	if (last(p) && name(bro(p))==cont_tag ) {
484
	if (last(p) && name(bro(p)) == cont_tag) {
443
	     exp dad = father(bro(p));
485
	     exp dad = father(bro(p));
444
	     if (name(dad) == addptr_tag &&
486
	     if (name(dad) == addptr_tag && bro(son(dad)) == bro(p) &&
445
                 bro(son(dad))==bro(p) &&
-
 
446
                 last(bro(p)) &&
-
 
447
	         good_pointer_factor(1)) {
487
		 last(bro(p)) && good_pointer_factor(1)) {
448
		if(good_val(son(dad), piece))
488
		if (good_val(son(dad), piece)) {
449
                   { f(dad,usagex==1);
489
		     f(dad, usagex == 1);
450
                     otheruses--;
490
                     otheruses--;
451
		     if (stride==0){ stride=1;}
491
		     if (stride == 0) {
452
		     else
492
			     stride=1;
453
		     if (stride>1) {stride = -1;}
493
		     } else if (stride > 1) {
454
                   }
494
			     stride = -1;
455
	     }
495
		     }
456
	     else
496
		}
457
	     if (name(dad)==offset_mult_tag &&
497
	     } else if (name(dad) == offset_mult_tag && son(dad) == bro(p) &&
458
                 son(dad)==bro(p) &&
-
 
459
                 simple_const(piece,bro(son(dad)),false,!assign_alias) &&
498
			simple_const(piece, bro(son(dad)), false,
460
	         last(dad) &&
499
				     !assign_alias) && last(dad) &&
461
                 (name(bro(son(dad)))!=val_tag ||
500
			(name(bro(son(dad))) != val_tag ||
462
                  good_pointer_factor(no(bro(son(dad)))/8)
501
			 good_pointer_factor(no(bro(son(dad))) / 8))) {
463
                 )
-
 
464
                )
-
 
465
	       {exp grandad = father(dad);
502
		     exp grandad = father(dad);
466
		if (name(grandad)==addptr_tag &&
-
 
467
                    bro(son(grandad)) == dad &&
503
		if (name(grandad) == addptr_tag && bro(son(grandad)) == dad &&
468
                    last(dad))
504
                    last(dad)) {
469
		    {if(good_val(son(grandad), piece))
505
			if (good_val(son(grandad), piece)) {
470
			 {int n =  -1;
506
			  int n = -1;
471
			  f(grandad,usagex==1);
507
			  f(grandad, usagex == 1);
472
                          otheruses--;
508
                          otheruses--;
473
			  if (name(bro(son(dad)))==val_tag) {
509
			  if (name(bro(son(dad))) == val_tag) {
474
				n = no(bro(son(dad)))/8;
510
				n = no(bro(son(dad))) /8;
475
			  }
-
 
476
			  else
-
 
477
			  if (name(bro(son(dad))) == name_tag) {
511
			  } else if (name(bro(son(dad))) == name_tag) {
478
				exp id = son(bro(son(dad)));
512
				exp id = son(bro(son(dad)));
479
				if (name(son(id))==val_tag) {
513
				if (name(son(id)) == val_tag) {
480
					n = no(son(id));
514
					n = no(son(id));
481
				}
515
				}
482
			  }
516
			  }
483
 
517
 
484
			  if (stride==0) { stride = n; }
518
			  if (stride == 0) {
-
 
519
				  stride = n;
485
			  else if (stride != n) { stride = -1; }
520
			  } else if (stride != n) {
486
                         }
521
				  stride = -1;
487
		    }
522
			  }
488
	       }
523
			}
489
	     else
524
		}
490
	     if (name(dad)==test_tag && piece == bro(son(pt(dad))) )
525
	     } else if (name(dad) == test_tag && piece == bro(son(pt(dad)))) {
491
		{f(dad, usagex==1); otheruses--;}
526
		     f(dad, usagex == 1);
-
 
527
		     otheruses--;
-
 
528
	     }
492
	}
529
	}
493
     }
530
     }
494
      return otheruses;
531
     return otheruses;
495
}
532
}
-
 
533
 
496
 
534
 
497
exp addptrs;
535
exp addptrs;
498
/* nilexp  initially */
536
/* nilexp  initially */
499
	/* son = addptr exp;
537
/* son = addptr exp;
500
	   pt = [holder with son = different occurence of addptr exp]**(no-1)
538
   pt = [holder with son = different occurence of addptr exp]**(no-1)
501
			chained through ptr
539
		chained through ptr
502
	   props =1 if done exactly once
540
   props =1 if done exactly once
503
	   other addptrs chained similarly through bro
541
   other addptrs chained similarly through bro
504
	*/
542
*/
505
 
543
 
506
exp tests;
544
exp tests;
507
/* nilexp initially */
545
/* nilexp initially */
508
	/* son = test
546
/* son = test
509
	   pt = [holder with son = different occurence of test]**(no-1)
547
   pt = [holder with son = different occurence of test]**(no-1)
510
	   props = 1 if done exactly once
548
   props = 1 if done exactly once
511
	   other tests chained similarly through bro
549
   other tests chained similarly through bro
512
	*/
550
*/
513
 
551
 
514
void collect_loopthings
552
void
515
    PROTO_N ( (ind, everytime) )
-
 
516
    PROTO_T ( exp ind X int everytime )
553
collect_loopthings(exp ind, int everytime)
517
{
554
{
518
	/* builds addptrs and tests*/
555
	/* builds addptrs and tests*/
519
	exp z;
556
	exp z;
520
	exp * loopthing = (name(ind)==test_tag)? &tests: &addptrs;
557
	exp *loopthing = (name(ind) == test_tag) ? &tests : &addptrs;
521
	for(z= *loopthing; z!=nilexp; z= bro(z)) {
558
	for (z = *loopthing; z != nilexp; z = bro(z)) {
522
		if (eq_exp(son(z), ind)) {
559
		if (eq_exp(son(z), ind)) {
523
			exp n = getexp(topsh, nilexp, 0, ind, pt(z),0,0, 0);
560
			exp n = getexp(topsh, nilexp, 0, ind, pt(z), 0, 0, 0);
524
			pt(z) =n;
561
			pt(z) =n;
525
			no(z)++;
562
			no(z) ++;
526
			props(z) &= (prop)everytime;
563
			props(z) &= (prop)everytime;
527
		   	return;
564
		   	return;
528
		}
565
		}
529
	}
566
	}
530
	*loopthing = getexp(topsh, *loopthing, *loopthing==nilexp, ind,
567
	*loopthing = getexp(topsh, *loopthing, *loopthing == nilexp, ind,
531
				 nilexp, (prop)everytime, 1, 0);
568
			    nilexp, (prop)everytime, 1, 0);
532
 
-
 
533
}
569
}
534
 
570
 
535
 
571
 
536
exp incrs;
572
exp incrs;
537
/* nilexp initially */
573
/* nilexp initially */
538
	/* son = (v=v+val) exp;
574
/* son = (v=v+val) exp;
539
	   pt = [holder with son = different occurence with same v]**(no-1)
575
   pt = [holder with son = different occurence with same v]**(no-1)
540
			chained through pt
576
		chained through pt
541
	   props = 1 if done exactly once
577
   props = 1 if done exactly once
542
	   other (v=v+val)s chained similarly through bro
578
   other (v=v+val)s chained similarly through bro
543
	*/
579
*/
544
 
580
 
545
int maybe_incr
581
int
546
    PROTO_N ( (e) )
-
 
547
    PROTO_T ( exp e )
582
maybe_incr(exp e)
548
{
583
{
549
	exp incs = incrs;
584
	exp incs = incrs;
550
	if (name(e) == cont_tag) {
585
	if (name(e) == cont_tag) {
551
		e = son(e);
586
		e = son(e);
552
	}
587
	}
553
	if (name(e) != name_tag) return 1;
588
	if (name(e) != name_tag) {
-
 
589
		return 1;
-
 
590
	}
554
	while (incs != nilexp) {
591
	while (incs != nilexp) {
555
		exp dest = son(son(incs));
592
		exp dest = son(son(incs));
556
		Assert(name(dest) == name_tag);
593
		Assert(name(dest) == name_tag);
557
		if (son(dest)==son(e)) return 1;
594
		if (son(dest) == son(e)) {
-
 
595
			return 1;
-
 
596
		}
558
		incs = bro(incs);
597
		incs = bro(incs);
559
	}
598
	}
560
	return 0;
599
	return 0;
561
}
600
}
-
 
601
 
562
 
602
 
563
void collect_incrs
603
void
564
    PROTO_N ( (incr, everytime) )
-
 
565
    PROTO_T ( exp incr X int everytime )
604
collect_incrs(exp incr, int everytime)
566
{
605
{
567
	/* builds incrs */
606
	/* builds incrs */
568
	exp z;
607
	exp z;
569
	for(z=incrs; z!=nilexp; z= bro(z)) {
608
	for (z = incrs; z != nilexp; z = bro(z)) {
570
		if (son(son(son(z))) == son(son(incr))
609
		if (son(son(son(z))) == son(son(incr))
571
			&& no(son(son(z)))==no(son(incr))  ) {
610
			&& no(son(son(z))) == no(son(incr))) {
572
			exp n = getexp(topsh, nilexp, 0, incr, pt(z), 0,0, 0);
611
			exp n = getexp(topsh, nilexp, 0, incr, pt(z), 0, 0, 0);
573
			pt(z) =n;
612
			pt(z) = n;
574
			no(z)++;
613
			no(z) ++;
575
			props(z) &= (prop)everytime;
614
			props(z) &= (prop)everytime;
576
		   	return;
615
		   	return;
577
		}
616
		}
578
	}
617
	}
579
 
618
 
580
	incrs = getexp(topsh, incrs, incrs==nilexp, incr,
619
	incrs = getexp(topsh, incrs, incrs == nilexp, incr, nilexp,
581
				 nilexp, (prop)everytime, 1, 0);
620
		       (prop)everytime, 1, 0);
582
 
621
 
583
}
622
}
584
 
623
 
585
 
624
 
586
/* transforms:
625
/* transforms:
587
 
626
 
588
	strength reduction
627
	strength reduction
589
		addptr(x, y) if x is invariant of loop
628
		addptr(x, y) if x is invariant of loop
590
				ie x = simple var
629
				ie x = simple var
591
				or x = cont(z) and z not altered in loop
630
				or x = cont(z) and z not altered in loop
592
			NB difference between invariant and not altered
631
			NB difference between invariant and not altered
593
 
632
 
594
 
633
 
595
*/
634
*/
596
 
635
 
597
 
636
 
598
/* incr is only incrementer
637
/* incr is only incrementer
Line 600... Line 639...
600
   loop is rep l:body
639
   loop is rep l:body
601
   exp * looppos = position(loop)
640
   exp * looppos = position(loop)
602
 
641
 
603
 
642
 
604
always construct
643
always construct
605
	Var X = add_ptr(x,y)
644
	Var X = add_ptr(x, y)
606
		replace all add_ptrs by cont(X) in addptrset and loopbpdy
645
		replace all add_ptrs by cont(X) in addptrset and loopbpdy
607
		replace incr by {X = reff(inc)c(X); incr}
646
		replace incr by {X = reff(inc)c(X); incr}
608
                                               retaining handle on incr
647
                                               retaining handle on incr
609
		shift looppos to body
648
		shift looppos to body
610
 
649
 
Line 616... Line 655...
616
			var Cn = cont(ref(s+n*inc)cont(X)) in
655
			var Cn = cont(ref(s+n*inc)cont(X)) in
617
				body(reffn/Cn,  incr/{ Ci=C[i+1]; incr})
656
				body(reffn/Cn,  incr/{ Ci=C[i+1]; incr})
618
 
657
 
619
*/
658
*/
620
 
659
 
621
static void extract_addptrs
660
static void
622
    PROTO_N ( ( incr, addptrset, loop, inc, inci, cons ) )
-
 
623
    PROTO_T ( exp incr X exp addptrset X exp loop X exp inc X int inci X int cons )
661
extract_addptrs(exp incr, exp addptrset, exp loop, exp inc, int inci, int cons)
624
{
662
{
625
	/* replace loop by :
663
	/* replace loop by :
626
	Var X = add_ptr(x,y) in newbody :
664
	Var X = add_ptr(x, y) in newbody :
627
		replace all add_ptrs by cont(X) in addptrset and loopbpdy
665
		replace all add_ptrs by cont(X) in addptrset and loopbpdy
628
		replace incr by {X = reff(inc)c(X); incr}
666
		replace incr by {X = reff(inc)c(X); incr}
629
                                                 retaining handle on incr
667
                                                 retaining handle on incr
630
		inc is in bytes.
668
		inc is in bytes.
631
 
669
 
Line 634... Line 672...
634
	exp id = getexp(sh(loop), bro(loop), last(loop), son(addptrset),
672
	exp id = getexp(sh(loop), bro(loop), last(loop), son(addptrset),
635
			   nilexp, 1 /*var*/, 0, ident_tag);
673
			   nilexp, 1 /*var*/, 0, ident_tag);
636
			/* setsib(son(id), loop); setdad(loop, id) later */
674
			/* setsib(son(id), loop); setdad(loop, id) later */
637
	int i;
675
	int i;
638
	exp z = addptrset;
676
	exp z = addptrset;
639
	exp * pos;
677
	exp *pos;
640
	exp ld, ass, reff, cont, seq, dest;
678
	exp ld, ass, reff, cont, seq, dest;
641
        exp incr_2, prod_2, neg_prod_2, mult_2;
679
        exp incr_2, prod_2, neg_prod_2, mult_2;
642
	setcaonly(id);
680
	setcaonly(id);
643
	for(i=0; i<no(addptrset); i++) {
681
	for (i = 0; i < no(addptrset); i++) {
644
		/* replace addptrs by cont(ld(id)) */
682
		/* replace addptrs by cont(ld(id)) */
645
	     exp sz = son(z) /*the addptr */;
683
	     exp sz = son(z) /*the addptr */;
646
	     cont = getexp(sh(son(id)), bro(sz), last(sz), nilexp, nilexp,
684
	     cont = getexp(sh(son(id)), bro(sz), last(sz), nilexp, nilexp, 0,
647
				0,0, cont_tag);
685
			   0, cont_tag);
648
             ld = getexp(shvar, cont, 1, id, pt(id), 0, 0, name_tag);
686
             ld = getexp(shvar, cont, 1, id, pt(id), 0, 0, name_tag);
649
	     pos = position(sz);
687
	     pos = position(sz);
650
	     son(cont) = ld;
688
	     son(cont) = ld;
651
	     pt(id) = ld; no(id)++;
689
	     pt(id) = ld;
-
 
690
	     no(id) ++;
652
	     *pos = cont;
691
	     *pos = cont;
653
             son(z) = cont;
692
             son(z) = cont;
-
 
693
	     if (i != 0) {
654
	     if (i!=0) { kill_exp(sz, nilexp); }
694
		     kill_exp(sz, nilexp);
-
 
695
	     }
655
	     z = pt(z);
696
	     z = pt(z);
656
 
-
 
657
	}
697
	}
658
 
698
 
-
 
699
	bro(son(id)) = loop;
659
	bro(son(id))= loop; clearlast(son(id));
700
	clearlast(son(id));
660
	pos = position(loop);
701
	pos = position(loop);
661
        bro(loop) = id; setlast(loop);
702
        bro(loop) = id;
-
 
703
	setlast(loop);
662
	*pos = id;
704
	*pos = id;
663
if (cons || no(inc)!=0)
705
	if (cons || no(inc) != 0) {
664
	{int mult = no(inc)*inci;
706
		int mult = no(inc)*inci;
-
 
707
		if (cons) {
665
         if (cons) mult = inci*8;
708
			mult = inci * 8;
-
 
709
		}
666
         ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
710
		ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
667
	pt(id) = ld; no(id)++;
711
		pt(id) = ld;
-
 
712
		no(id) ++;
668
	cont = getexp(sh(son(id)), nilexp, 1, ld, nilexp, 0,0, cont_tag);
713
		cont = getexp(sh(son(id)), nilexp, 1, ld, nilexp, 0, 0,
-
 
714
			      cont_tag);
669
	bro(ld) = cont;
715
		bro(ld) = cont;
670
	reff = getexp(sh(cont), nilexp, 1, cont, nilexp, 0, mult, reff_tag);
716
		reff = getexp(sh(cont), nilexp, 1, cont, nilexp, 0, mult,
-
 
717
			      reff_tag);
671
	bro(cont) = reff;
718
		bro(cont) = reff;
672
	dest = getexp(shvar, reff, 0, id, pt(id), 0, 0, name_tag);
719
		dest = getexp(shvar, reff, 0, id, pt(id), 0, 0, name_tag);
673
	pt(id) = dest; no(id)++;
720
		pt(id) = dest;
-
 
721
		no(id) ++;
674
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
722
		ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
675
	bro(reff) = ass;
723
		bro(reff) = ass;
676
	z = getexp(topsh, incr, 0, ass, nilexp, 0,0, 0);
724
		z = getexp(topsh, incr, 0, ass, nilexp, 0, 0, 0);
677
	bro(ass) = z;
725
		bro(ass) = z;
678
	seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0,0, seq_tag);
726
		seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0, 0,
-
 
727
			     seq_tag);
679
	pos = position(incr);
728
		pos = position(incr);
680
	bro(incr) = seq; setlast(incr);
729
		bro(incr) = seq;
-
 
730
		setlast(incr);
681
	*pos = seq;}
731
		*pos = seq;
682
 
732
	} else{
683
else{        mult_2 = copy(inc);
733
		mult_2 = copy(inc);
684
        if (inci < 0)
734
		if (inci < 0) {
685
         {incr_2 = getexp(sh(inc),nilexp,1,nilexp,nilexp,0,-inci,val_tag);
735
			incr_2 = getexp(sh(inc), nilexp, 1, nilexp, nilexp, 0,
-
 
736
					-inci, val_tag);
686
          bro(incr_2) = mult_2;
737
			bro(incr_2) = mult_2;
687
          clearlast(incr_2);
738
			clearlast(incr_2);
688
          neg_prod_2 = getexp(sh(inc), nilexp, 1, incr_2, nilexp,0,
739
			neg_prod_2 = getexp(sh(inc), nilexp, 1, incr_2, nilexp,
689
                                                       0,offset_mult_tag);
740
					    0, 0, offset_mult_tag);
690
          neg_prod_2 = hc(neg_prod_2,mult_2);
741
			neg_prod_2 = hc(neg_prod_2, mult_2);
691
          prod_2 = getexp(sh(inc),nilexp,1,neg_prod_2,nilexp,0,0,neg_tag);
742
			prod_2 = getexp(sh(inc), nilexp, 1, neg_prod_2, nilexp,
-
 
743
					0, 0, neg_tag);
692
          bro(neg_prod_2) = prod_2;
744
			bro(neg_prod_2) = prod_2;
693
         }
745
		} else {
694
        else
-
 
695
         {incr_2 = getexp(sh(inc),nilexp,1,nilexp,nilexp,0,inci,val_tag);
746
			incr_2 = getexp(sh(inc), nilexp, 1, nilexp, nilexp, 0,
-
 
747
					inci, val_tag);
696
          bro(incr_2) = mult_2;
748
			bro(incr_2) = mult_2;
697
          clearlast(incr_2);
749
			clearlast(incr_2);
698
          prod_2 = getexp(sh(inc),nilexp,0,incr_2,nilexp,0,0,offset_mult_tag);
750
			prod_2 = getexp(sh(inc), nilexp, 0, incr_2, nilexp, 0,
-
 
751
					0, offset_mult_tag);
699
          prod_2 = hc(prod_2,mult_2);
752
			prod_2 = hc(prod_2, mult_2);
700
         }
753
		}
701
        ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
754
		ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
702
	pt(id) = ld; no(id)++;
755
		pt(id) = ld;
-
 
756
		no(id)++;
703
	cont = getexp(sh(son(id)), nilexp, 0, ld, nilexp, 0,0, cont_tag);
757
		cont = getexp(sh(son(id)), nilexp, 0, ld, nilexp, 0, 0,
-
 
758
			      cont_tag);
704
	bro(ld) = cont;
759
		bro(ld) = cont;
705
	reff = getexp(sh(son(id)), nilexp, 1, cont, nilexp, 0, 0, addptr_tag);
760
		reff = getexp(sh(son(id)), nilexp, 1, cont, nilexp, 0, 0,
-
 
761
			      addptr_tag);
706
        bro(cont) = prod_2;
762
		bro(cont) = prod_2;
707
	reff = hc(reff,prod_2);
763
		reff = hc(reff, prod_2);
708
	dest = getexp(shvar, reff, 0, id, pt(id), 0, 0, name_tag);
764
		dest = getexp(shvar, reff, 0, id, pt(id), 0, 0, name_tag);
709
	pt(id) = dest; no(id)++;
765
		pt(id) = dest;
-
 
766
		no(id)++;
710
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
767
		ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
711
	bro(reff) = ass;
768
		bro(reff) = ass;
712
	z = getexp(topsh, incr, 0, ass, nilexp, 0,0, 0);
769
		z = getexp(topsh, incr, 0, ass, nilexp, 0, 0, 0);
713
	bro(ass) = z;
770
		bro(ass) = z;
714
	seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0,0, seq_tag);
771
		seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0, 0,
-
 
772
			     seq_tag);
715
	pos = position(incr);
773
		pos = position(incr);
716
	bro(incr) = seq; setlast(incr);
774
		bro(incr) = seq;
-
 
775
		setlast(incr);
717
	*pos = seq;
776
		*pos = seq;
718
    }
777
	}
719
}
778
}
720
 
779
 
-
 
780
 
721
static void scale_loopid
781
static void
722
    PROTO_N ( ( loop, addptrset, incrset) )
-
 
723
    PROTO_T ( exp loop X exp addptrset X exp incrset )
782
scale_loopid(exp loop, exp addptrset, exp incrset)
724
{
783
{
725
	/* replace loop body by :
784
	/* replace loop body by :
726
	Var X = loopid*stride in newbody :
785
	Var X = loopid*stride in newbody :
727
		replace all offset_mults by cont(X) in addptrset and loopbody
786
		replace all offset_mults by cont(X) in addptrset and loopbody
728
		replace incr by {X = c(X)+stride; incr}
787
		replace incr by {X = c(X)+stride; incr}
729
                                                 retaining handle on incr
788
                                                 retaining handle on incr
730
		inc is in bytes.
789
		inc is in bytes.
731
 
790
 
732
	*/
791
	*/
733
	exp id = getexp(sh(loop), bro(loop), last(loop), bro(son(son(addptrset))),
792
	exp id = getexp(sh(loop), bro(loop), last(loop),
734
			   nilexp, 1 /*var*/, 0, ident_tag);
793
			bro(son(son(addptrset))), nilexp, 1 /*var*/, 0,
-
 
794
			ident_tag);
735
			/* setsib(son(id), loop); setdad(loop, id) later */
795
			/* setsib(son(id), loop); setdad(loop, id) later */
736
 
796
 
737
	exp * pos;
797
	exp *pos;
738
        exp incr = son(incrset);
798
        exp incr = son(incrset);
739
        shape shvar = sh(son(bro(son(incr))));
799
        shape shvar = sh(son(bro(son(incr))));
740
	exp ld, ass, plus, cont, seq, dest, inc, z, next;
800
	exp ld, ass, plus, cont, seq, dest, inc, z, next;
741
        while (addptrset != nilexp)
801
	while (addptrset != nilexp) {
742
	{int i;
802
		int i;
743
	z = addptrset;
803
		z = addptrset;
744
        next = bro(addptrset);
804
		next = bro(addptrset);
745
	setcaonly(id);
805
		setcaonly(id);
746
	for(i=0; i<no(addptrset); i++) {
806
		for (i = 0; i < no(addptrset); i++) {
747
		/* replace addptrs by cont(ld(id)) */
807
			/* replace addptrs by cont(ld(id)) */
748
	     exp sz = bro(son(son(z))) /* the offset_mult */;
808
			exp sz = bro(son(son(z))) /* the offset_mult */;
749
	     cont = getexp(sh(son(id)), bro(sz), last(sz), nilexp, nilexp,
809
			cont = getexp(sh(son(id)), bro(sz), last(sz), nilexp,
750
				0,0, cont_tag);
810
				      nilexp, 0, 0, cont_tag);
751
             ld = getexp(shvar, cont, 1, id, pt(id), 0, 0, name_tag);
811
			ld = getexp(shvar, cont, 1, id, pt(id), 0, 0, name_tag);
752
	     pos = position(sz);
812
			pos = position(sz);
753
	     son(cont) = ld;
813
			son(cont) = ld;
754
	     pt(id) = ld; no(id)++;
814
			pt(id) = ld;
-
 
815
			no(id) ++;
755
	     *pos = cont;
816
			*pos = cont;
756
             bro(son(son(z))) = cont;
817
			bro(son(son(z))) = cont;
-
 
818
			if (i != 0) {
757
	     if (i!=0) { kill_exp(sz, nilexp); }
819
				kill_exp(sz, nilexp);
-
 
820
			}
758
	     z = pt(z);
821
			z = pt(z);
759
 
822
 
-
 
823
		}
-
 
824
		retcell(addptrset);
-
 
825
		addptrset = next;
760
	}
826
	}
761
        retcell(addptrset);
-
 
762
        addptrset = next;
-
 
763
        }
-
 
764
 
827
 
-
 
828
	bro(son(id)) = loop;
765
	bro(son(id))= loop; clearlast(son(id));
829
	clearlast(son(id));
766
	pos = position(loop);
830
	pos = position(loop);
767
        bro(loop) = id; setlast(loop);
831
        bro(loop) = id;
-
 
832
	setlast(loop);
768
	*pos = id;
833
	*pos = id;
769
 
834
 
770
        inc = getexp(sh(son(id)),nilexp,1,nilexp,nilexp,0,
835
        inc = getexp(sh(son(id)), nilexp, 1, nilexp, nilexp, 0,
771
                  stride*8*no(bro(son(bro(son(son(incrset)))))),val_tag);
836
		     stride * 8 * no(bro(son(bro(son(son(incrset)))))),
-
 
837
		     val_tag);
772
        ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
838
        ld = getexp(shvar, nilexp, 1, id, pt(id), 0, 0, name_tag);
773
	pt(id) = ld; no(id)++;
839
	pt(id) = ld;
-
 
840
	no(id) ++;
774
	cont = getexp(sh(son(id)), nilexp, 0, ld, nilexp, 0,0, cont_tag);
841
	cont = getexp(sh(son(id)), nilexp, 0, ld, nilexp, 0, 0, cont_tag);
775
	bro(ld) = cont;
842
	bro(ld) = cont;
776
	plus = getexp(sh(cont), nilexp, 1, cont, nilexp, 0, 0, plus_tag);
843
	plus = getexp(sh(cont), nilexp, 1, cont, nilexp, 0, 0, plus_tag);
777
	bro(cont) = inc;
844
	bro(cont) = inc;
778
        bro(inc) = plus;
845
        bro(inc) = plus;
779
	dest = getexp(shvar, plus, 0, id, pt(id), 0, 0, name_tag);
846
	dest = getexp(shvar, plus, 0, id, pt(id), 0, 0, name_tag);
780
	pt(id) = dest; no(id)++;
847
	pt(id) = dest;
-
 
848
	no(id) ++;
781
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
849
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
782
	bro(plus) = ass;
850
	bro(plus) = ass;
783
	z = getexp(topsh, incr, 0, ass, nilexp, 0,0, 0);
851
	z = getexp(topsh, incr, 0, ass, nilexp, 0, 0, 0);
784
	bro(ass) = z;
852
	bro(ass) = z;
785
	seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0,0, seq_tag);
853
	seq = getexp(topsh, bro(incr), last(incr), z, nilexp, 0, 0, seq_tag);
786
	pos = position(incr);
854
	pos = position(incr);
787
	bro(incr) = seq; setlast(incr);
855
	bro(incr) = seq;
-
 
856
	setlast(incr);
788
	*pos = seq;
857
	*pos = seq;
789
}
858
}
790
 
859
 
791
 
860
 
792
exp inner_cont
861
exp
793
    PROTO_N ( (loopbody, contset) )
-
 
794
    PROTO_T ( exp loopbody X exp contset )
862
inner_cont(exp loopbody, exp contset)
795
{
863
{
796
 
864
 
797
/*
865
/*
798
	son contset = cont(X); pt = next instance
866
	son contset = cont(X);
-
 
867
	pt = next instance
799
	replace loopbody by Var Z = cont(X) in loopbody(cont(X)/cont(Z))
868
	replace loopbody by Var Z = cont(X) in loopbody(cont(X)/cont(Z))
800
 
869
 
801
*/
870
*/
802
 
871
 
803
	exp z = contset;
872
	exp z = contset;
804
	exp *pos;
873
	exp *pos;
805
	int i;
874
	int i;
806
	exp id = getexp(sh(loopbody), bro(loopbody), last(loopbody),
875
	exp id = getexp(sh(loopbody), bro(loopbody), last(loopbody),
807
			     son(contset), nilexp, 1/*var*/, 0, ident_tag);
876
			son(contset), nilexp, 1/*var*/, 0, ident_tag);
808
	setcaonly(id);
877
	setcaonly(id);
809
 
878
 
810
	for(i=0; z!=nilexp; i++) {
879
	for (i=0; z != nilexp; i++) {
811
	    exp ld = getexp(sh(son(son(id))), nilexp, 1, id,
880
	    exp ld = getexp(sh(son(son(id))), nilexp, 1, id, pt(id), 0, 0,
812
                                                   pt(id), 0, 0, name_tag);
881
			    name_tag);
813
	    exp cont = getexp(sh(son(id)), bro(son(z)), last(son(z)),
882
	    exp cont = getexp(sh(son(id)), bro(son(z)), last(son(z)), ld,
814
				               ld, nilexp, 0,0, cont_tag);
883
			      nilexp, 0, 0, cont_tag);
815
	    bro(ld)=cont;
884
	    bro(ld) = cont;
816
	    pt(id) = ld; no(id)++;
885
	    pt(id) = ld;
-
 
886
	    no(id) ++;
817
	    pos = position(son(z));
887
	    pos = position(son(z));
818
	    *pos = cont;
888
	    *pos = cont;
-
 
889
	    if (i != 0) {
819
	    if(i!=0) kill_exp(son(z), nilexp);
890
		    kill_exp(son(z), nilexp);
-
 
891
	    }
820
	    son(z) = cont;
892
	    son(z) = cont;
821
	    z = pt(z);
893
	    z = pt(z);
822
	}
894
	}
823
 
895
 
824
	pos = position(loopbody);
896
	pos = position(loopbody);
825
	bro(son(id)) = loopbody; clearlast(son(id));
897
	bro(son(id)) = loopbody;
-
 
898
	clearlast(son(id));
826
	bro(loopbody) = id; setlast(loopbody);
899
	bro(loopbody) = id;
-
 
900
	setlast(loopbody);
827
	*pos = id;
901
	*pos = id;
828
	return id;
902
	return id;
829
}
903
}
830
 
904
 
-
 
905
 
831
exp outer_cont
906
exp
832
    PROTO_N ( ( loop, contset, lastid, incr) )
-
 
833
    PROTO_T ( exp loop X exp contset X exp lastid X exp incr )
907
outer_cont(exp loop, exp contset, exp lastid, exp incr)
834
{
908
{
835
 
909
 
836
/*
910
/*
837
	son contset = cont(X); pt = next instance
911
	son contset = cont(X);
-
 
912
	pt = next instance
838
	replace loop by Var Z = cont(x) in
913
	replace loop by Var Z = cont(x) in
839
			loop(cont(x)/cont(Z), incr/{Z=cont(lasttid); incr})
914
			loop(cont(x)/cont(Z), incr/{Z=cont(lasttid); incr})
840
	returning new iddec
915
	returning new iddec
841
*/
916
*/
842
 
917
 
Line 845... Line 920...
845
	exp *pos;
920
	exp *pos;
846
	int i;
921
	int i;
847
	exp id = getexp(sh(loop), bro(loop), last(loop), son(contset),
922
	exp id = getexp(sh(loop), bro(loop), last(loop), son(contset),
848
			nilexp, 1/*var*/, 0, ident_tag);
923
			nilexp, 1/*var*/, 0, ident_tag);
849
 	setcaonly(id);
924
 	setcaonly(id);
850
	for(i=0; z!=nilexp; i++) {
925
	for (i = 0; z != nilexp; i++) {
851
	    ld = getexp(sh(son(son(id))), nilexp, 1, id,
926
	    ld = getexp(sh(son(son(id))), nilexp, 1, id, pt(id), 0, 0,
852
                                                 pt(id), 0, 0, name_tag);
927
			name_tag);
853
	    cont = getexp(sh(son(id)), bro(son(z)), last(son(z)),
928
	    cont = getexp(sh(son(id)), bro(son(z)), last(son(z)), ld, nilexp,
854
				           ld, nilexp, 0,0, cont_tag);
929
			  0, 0, cont_tag);
855
	    bro(ld)=cont;
930
	    bro(ld) = cont;
856
	    pt(id) = ld; no(id)++;
931
	    pt(id) = ld;
-
 
932
	    no(id)++;
857
	    pos = position(son(z));
933
	    pos = position(son(z));
858
	    *pos = cont;
934
	    *pos = cont;
-
 
935
	    if (i != 0) {
859
	    if(i!=0) kill_exp(son(z), nilexp);
936
		    kill_exp(son(z), nilexp);
-
 
937
	    }
860
	    son(z) = cont;
938
	    son(z) = cont;
861
	    z = pt(z);
939
	    z = pt(z);
862
	}
940
	}
863
 
941
 
864
	pos = position(loop);
942
	pos = position(loop);
865
	bro(son(id)) = loop; clearlast(son(id));
943
	bro(son(id)) = loop;
-
 
944
	clearlast(son(id));
866
	bro(loop) = id; setlast(loop);
945
	bro(loop) = id;
-
 
946
	setlast(loop);
867
	*pos = id;
947
	*pos = id;
868
 
948
 
869
 
949
 
870
	ld = getexp(sh(son(son(id))), nilexp, 1, lastid,
950
	ld = getexp(sh(son(son(id))), nilexp, 1, lastid, pt(lastid), 0, 0,
871
                                            pt(lastid), 0, 0, name_tag);
951
		    name_tag);
872
	pt(lastid)=ld; no(lastid)++;
952
	pt(lastid) = ld;
-
 
953
	no(lastid)++;
873
	cont = getexp(sh(son(id)), nilexp, 1, ld, nilexp, 0,0,cont_tag);
954
	cont = getexp(sh(son(id)), nilexp, 1, ld, nilexp, 0, 0, cont_tag);
874
	bro(ld) = cont;
955
	bro(ld) = cont;
875
	dest = getexp(sh(son(son(id))), cont, 0, id, pt(id), 0, 0, name_tag);
956
	dest = getexp(sh(son(son(id))), cont, 0, id, pt(id), 0, 0, name_tag);
876
	pt(id) = dest; no(id)++;
957
	pt(id) = dest;
-
 
958
	no(id)++;
877
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
959
	ass = getexp(topsh, nilexp, 1, dest, nilexp, 0, 0, ass_tag);
878
	bro(cont)=ass;
960
	bro(cont) = ass;
879
 	z = getexp(sh(incr), incr, 0, ass, nilexp, 0,0, 0);
961
 	z = getexp(sh(incr), incr, 0, ass, nilexp, 0, 0, 0);
880
	bro(ass) = z;
962
	bro(ass) = z;
881
	seq = getexp(sh(incr), bro(incr), last(incr),z, nilexp, 0,0,seq_tag);
963
	seq = getexp(sh(incr), bro(incr), last(incr), z, nilexp, 0, 0, seq_tag);
882
	pos = position(incr);
964
	pos = position(incr);
883
	bro(incr) = seq; setlast(incr);
965
	bro(incr) = seq;
-
 
966
	setlast(incr);
884
	*pos = seq;
967
	*pos = seq;
885
 
968
 
886
	return id;
969
	return id;
887
}
970
}
888
 
971
 
889
 
972
 
890
int unaltered
973
int
891
    PROTO_N ( ( e, assign_alias) )
-
 
892
    PROTO_T ( exp e X int assign_alias )
974
unaltered(exp e, int assign_alias)
893
{
975
{
894
	exp z = alteredset;
976
	exp z = alteredset;
895
	if (name(e) == name_tag && isvar(son(e))) {
977
	if (name(e) == name_tag && isvar(son(e))) {
896
	     for(; z!=nilexp; z = bro(z) ) {
978
	     for (; z != nilexp; z = bro(z)) {
897
		exp dest = son(z);
979
		exp dest = son(z);
898
		Assert(name(dest)==name_tag);
980
		Assert(name(dest) == name_tag);
899
	                  if (!isvar(son(dest)) ) {
981
		if (!isvar(son(dest))) {
900
			dest = son(son(dest));
982
			dest = son(son(dest));
901
			if (name(dest) == reff_tag) dest = son(dest);
983
			if (name(dest) == reff_tag) {
-
 
984
				dest = son(dest);
-
 
985
			}
902
			Assert(name(dest) == addptr_tag);
986
			Assert(name(dest) == addptr_tag);
903
			dest = son(dest);
987
			dest = son(dest);
904
		}
988
		}
905
		if (son(e) == son(dest) &&
989
		if (son(e) == son(dest) &&
906
			(name(son(e)) != proc_tag || no(e) == no(dest) ) ) {
990
			(name(son(e)) != proc_tag || no(e) == no(dest))) {
907
			return false;
991
			return false;
908
		}
992
		}
909
	     }
993
	     }
910
	     return (iscaonly(son(e)) || !assign_alias);
994
	     return (iscaonly(son(e)) || !assign_alias);
911
	}
995
	}
912
	return false;
996
	return false;
913
}
997
}
914
 
-
 
915
 
998
 
916
 
999
 
917
int invariant
1000
int
918
    PROTO_N ( ( e, assign_alias) )
-
 
919
    PROTO_T ( exp e X int assign_alias )
1001
invariant(exp e, int assign_alias)
920
{
1002
{
921
	return ((name(e) == name_tag  )   ||
1003
	return ((name(e) == name_tag) ||
922
		(name(e) == cont_tag && unaltered(son(e), assign_alias) )  ) ;
1004
	       (name(e) == cont_tag && unaltered(son(e), assign_alias)));
923
}
1005
}
924
 
1006
 
925
 
1007
 
926
static int multiplier;	/*part of answer to weaken */
1008
static int multiplier;	/*part of answer to weaken */
927
static int arraystep;	/*part of answer to weaken */
1009
static int arraystep;	/*part of answer to weaken */
928
 
1010
 
929
int weaken
1011
int
930
    PROTO_N ( (loop, addptrset, incrset) )
-
 
931
    PROTO_T ( exp loop X exp addptrset X exp incrset )
1012
weaken(exp loop, exp addptrset, exp incrset)
932
{
1013
{
933
	/* applies strength reduction to addptrs in addptrset and
1014
	/* applies strength reduction to addptrs in addptrset and
934
		delivers the multiplying factor if suitable for unwinding
1015
		delivers the multiplying factor if suitable for unwinding
935
			otherwise 0
1016
			otherwise 0
936
	*/
1017
	*/
Line 938... Line 1019...
938
	exp addptr = son(addptrset);
1019
	exp addptr = son(addptrset);
939
	int inci = no(bro(son(bro(son(incr)))));
1020
	int inci = no(bro(son(bro(son(incr)))));
940
	exp minc = bro(son(addptr));
1021
	exp minc = bro(son(addptr));
941
        int simple_c = 0;
1022
        int simple_c = 0;
942
	int res = -1;
1023
	int res = -1;
943
	if (name(minc)==cont_tag) {
1024
	if (name(minc) == cont_tag) {
944
		multiplier = inci;
1025
		multiplier = inci;
945
		arraystep = 1;
1026
		arraystep = 1;
946
                simple_c = 1;
1027
                simple_c = 1;
947
	}
-
 
948
	else {
1028
	} else {
949
		arraystep = no(bro(son(minc)))>>3;
1029
		arraystep = no(bro(son(minc))) >> 3;
950
		multiplier = inci*arraystep;
1030
		multiplier = inci * arraystep;
951
	}
1031
	}
952
 
1032
 
953
 
-
 
954
 
-
 
955
 
-
 
956
	if (!invariant(son(addptr), assign_alias) || no(incrset)!=1)
1033
	if (!invariant(son(addptr), assign_alias) || no(incrset) != 1) {
957
        return 0;
1034
		return 0;
-
 
1035
	}
958
	if (props(addptrset) && unaltered(son(addptr), assign_alias) )
1036
	if (props(addptrset) && unaltered(son(addptr), assign_alias)) {
959
		res= 1;
1037
		res = 1;
-
 
1038
	}
960
 
1039
 
961
	extract_addptrs(incr, addptrset, loop,
-
 
962
                                bro(son(minc)),inci,simple_c);
1040
	extract_addptrs(incr, addptrset, loop, bro(son(minc)), inci, simple_c);
963
  	return res;
1041
  	return res;
964
}
1042
}
-
 
1043
 
965
 
1044
 
966
struct en{ exp e;
1045
struct en{
-
 
1046
	exp e;
967
int disp;
1047
	int disp;
968
};
1048
};
969
 
1049
 
970
int unwind
1050
int
971
    PROTO_N ( (loop, contset, incr, incval) )
-
 
972
    PROTO_T ( exp loop X exp contset X exp incr X int incval )
1051
unwind(exp loop, exp contset, exp incr, int incval)
973
{
1052
{
974
	exp body = bro(son(bro(son(loop))));
1053
	exp body = bro(son(bro(son(loop))));
975
	int i,j;
1054
	int i, j;
976
	exp z = contset;
1055
	exp z = contset;
977
	int n = no(contset);
1056
	int n = no(contset);
978
	int insts = 0;
1057
	int insts = 0;
979
	struct en * s = (struct en*)xcalloc(n, sizeof(struct en));
1058
	struct en *s = (struct en *)xcalloc(n, sizeof(struct en));
980
	for(i=0; i<n; i++) {
1059
	for (i=0; i<n; i++) {
981
	    /* sort cont([reff (disp) cont(X)) into s */
1060
	    /* sort cont([reff (disp) cont(X)) into s */
982
	    exp c = son(z);
1061
	    exp c = son(z);
983
	    exp w;
1062
	    exp w;
984
	    int n;
1063
	    int n;
985
	    exp next = pt(z);
1064
	    exp next = pt(z);
986
	    Assert(name(c)==cont_tag);
1065
	    Assert(name(c) == cont_tag);
987
	    if (!last(c)) { z= next; continue; }
1066
	    if (!last(c)) {
-
 
1067
		    z = next;
-
 
1068
		    continue;
-
 
1069
	    }
988
	    if (name(bro(c))==cont_tag) { n =0; w = bro(c);}
1070
	    if (name(bro(c)) == cont_tag) {
989
	    else
1071
		    n = 0;
-
 
1072
		    w = bro(c);
990
	    if (name(bro(c))==reff_tag && last(bro(c)) &&
1073
	    } else if (name(bro(c)) == reff_tag && last(bro(c)) &&
991
			 name(bro(bro(c)))==cont_tag ) {
1074
		       name(bro(bro(c))) == cont_tag) {
-
 
1075
		    n = no(bro(c));
992
		    n= no(bro(c)); w = bro(bro(c));
1076
		    w = bro(bro(c));
993
	    }
1077
	    } else {
-
 
1078
		    z= next;
994
            else { z= next; continue; }
1079
		    continue;
-
 
1080
	    }
995
 
1081
 
996
	    son(z)= w;
1082
	    son(z) = w;
997
 
1083
 
998
	    for(j=0; j <insts; j++) {
1084
	    for (j=0; j <insts; j++) {
999
		int d = s[j].disp;
1085
		int d = s[j].disp;
1000
		if (d==n) break;
1086
		if (d == n) {
-
 
1087
			break;
-
 
1088
		}
1001
		if ((incval>0 && d<n) || (incval <0 && d > n) ) {
1089
		if ((incval > 0 && d < n) || (incval < 0 && d > n)) {
1002
			/*make a hole  at jth position */
1090
			/*make a hole at jth position */
1003
			int k;
1091
			int k;
1004
			for(k=insts-1; k>=j; k--) {
1092
			for (k = insts - 1; k >= j; k--) {
1005
				s[k+1] = s[k];
1093
				s[k + 1] = s[k];
1006
			}
1094
			}
1007
			s[j].e = nilexp;
1095
			s[j].e = nilexp;
1008
			insts++;
1096
			insts++;
1009
			break;
1097
			break;
1010
		}
1098
		}
1011
	    }
1099
	    }
1012
	    if (j==insts) { /* add another */
1100
	    if (j == insts) {
-
 
1101
		/* add another */
1013
		insts++;
1102
		insts++;
1014
		s[j].e = nilexp;
1103
		s[j].e = nilexp;
1015
	    };
1104
	    };
1016
	    pt(z) = s[j].e;
1105
	    pt(z) = s[j].e;
1017
	    s[j].e = z;
1106
	    s[j].e = z;
1018
	    s[j].disp= n;
1107
	    s[j].disp= n;
1019
	    z = next;
1108
	    z = next;
1020
	}
1109
	}
-
 
1110
	if (insts == 0) {
1021
	if (insts==0) return false;
1111
		return false;
-
 
1112
	}
1022
	z = nilexp;
1113
	z = nilexp;
1023
 
1114
 
1024
	for (i = 0; i < insts; i++) {
1115
	for (i = 0; i < insts; i++) {
-
 
1116
	      if (no(s[i].e) > 1) {
1025
	      if (no(s[i].e) >1 ) z = inner_cont(body, s[i].e);
1117
		      z = inner_cont(body, s[i].e);
-
 
1118
	      }
1026
	      for (; i < insts-1 && s[i].disp-incval*8 ==  s[i+1].disp; i++) {
1119
	      for (; i < insts-1 && s[i].disp - incval * 8 == s[i+1].disp;
-
 
1120
		   i++) {
-
 
1121
		if (z == nilexp) {
1027
		if (z==nilexp) z = inner_cont(body, s[i].e);
1122
			z = inner_cont(body, s[i].e);
-
 
1123
		}
1028
		z = outer_cont(loop, s[i+1].e, z, son(incr));
1124
		z = outer_cont(loop, s[i + 1].e, z, son(incr));
1029
	      }
1125
	      }
1030
	      z= nilexp;
1126
	      z= nilexp;
1031
	}
1127
	}
1032
 
-
 
1033
 
1128
 
1034
	return true;
1129
	return true;
1035
}
1130
}
1036
 
1131
 
-
 
1132
 
1037
int all_before
1133
int
1038
    PROTO_N ( ( addptrset, inc, body) )
-
 
1039
    PROTO_T ( exp addptrset X exp inc X exp body )
1134
all_before(exp addptrset, exp inc, exp body)
1040
{
1135
{
1041
     	exp z=inc;
1136
     	exp z=inc;
1042
	exp w;
1137
	exp w;
1043
     	while ( z != body) {
1138
     	while (z != body) {
1044
	    exp b = bro(z);
1139
	    exp b = bro(z);
1045
	    if (!last(z) ) {
1140
	    if (!last(z)) {
1046
	         for (w=addptrset; w!=nilexp; w= pt(w)) {
1141
		    for (w=addptrset; w != nilexp; w= pt(w)) {
1047
		/* son(w) is internal to body - is it in bro(z) ? ie after z*/
1142
			    /* son(w) is internal to body - is it in bro(z) ?
-
 
1143
			     * ie after z*/
1048
		exp s = son(w);
1144
			    exp s = son(w);
1049
		while(s!=body && s !=b) s = father(s);
1145
			    while (s != body && s != b) {
-
 
1146
				    s = father(s);
-
 
1147
			    }
-
 
1148
			    if (s == b) {
1050
		if (s == b) return false;
1149
				    return false;
1051
	        }
1150
			    }
-
 
1151
		    }
1052
	    }
1152
	    }
1053
	    z = b;
1153
	    z = b;
1054
	}
1154
	}
1055
                  return true;
1155
	return true;
1056
}
1156
}
1057
 
1157
 
1058
 
1158
 
1059
void replace_var
1159
void
1060
    PROTO_N ( (ldcpy, loop, shcont) )
-
 
1061
    PROTO_T ( exp ldcpy X exp loop X shape shcont )
1160
replace_var(exp ldcpy, exp loop, shape shcont)
1062
{
1161
{
1063
	/* ld is copy of the name(id) assigned to safely in loop
1162
	/* ld is copy of the name(id) assigned to safely in loop
1064
            (see do_one_rep)
1163
            (see do_one_rep)
1065
	    replace loop(id) by Var x := cont(id) in loop(x); id = cont(x) ni;
1164
	    replace loop(id) by Var x := cont(id) in loop(x); id = cont(x) ni;
1066
	*/
1165
	*/
1067
	exp z;
1166
	exp z;
1068
	exp * pos;
1167
	exp *pos;
1069
	exp ld = getexp(sh(ldcpy), nilexp, 1, son(ldcpy),pt(son(ldcpy)),
1168
	exp ld = getexp(sh(ldcpy), nilexp, 1, son(ldcpy), pt(son(ldcpy)),
1070
			                props(ldcpy), no(ldcpy), name(ldcpy));
1169
			props(ldcpy), no(ldcpy), name(ldcpy));
1071
	exp def = getexp(shcont, nilexp, 0, ld, nilexp, 0, 0, cont_tag);
1170
	exp def = getexp(shcont, nilexp, 0, ld, nilexp, 0, 0, cont_tag);
1072
	exp varid = getexp(sh(loop), bro(loop), last(loop),
1171
	exp varid = getexp(sh(loop), bro(loop), last(loop), def, nilexp,
1073
                                def, nilexp, subvar |1/*var*/, 1, ident_tag);
1172
			   subvar | 1 /*var*/, 1, ident_tag);
1074
	exp ldvar = getexp(sh(ld), nilexp, 1, varid, nilexp, 0, 0, name_tag);
1173
	exp ldvar = getexp(sh(ld), nilexp, 1, varid, nilexp, 0, 0, name_tag);
1075
	exp contvar = getexp(shcont, nilexp, 1, ldvar, nilexp, 0, 0,cont_tag);
1174
	exp contvar = getexp(shcont, nilexp, 1, ldvar, nilexp, 0, 0, cont_tag);
1076
	exp nld = getexp(sh(ld), contvar, 0, son(ld), ld, 0, no(ld),name_tag);
1175
	exp nld = getexp(sh(ld), contvar, 0, son(ld), ld, 0, no(ld), name_tag);
1077
	exp ass = getexp(topsh, nilexp, 1, nld , nilexp, 0, 0, ass_tag);
1176
	exp ass = getexp(topsh, nilexp, 1, nld, nilexp, 0, 0, ass_tag);
1078
	exp seqh = getexp(topsh, ass, 0, loop, nilexp, 0, 0, 0);
1177
	exp seqh = getexp(topsh, ass, 0, loop, nilexp, 0, 0, 0);
1079
	exp seq = getexp(topsh, varid, 1, seqh, nilexp, 0, 0, seq_tag);
1178
	exp seq = getexp(topsh, varid, 1, seqh, nilexp, 0, 0, seq_tag);
1080
	bro(ass) = seq; /*father*/
1179
	bro(ass) = seq; /*father*/
1081
	bro(contvar) = ass;/*father*/
1180
	bro(contvar) = ass;/*father*/
1082
	bro(ldvar) = contvar;/*father*/
1181
	bro(ldvar) = contvar;/*father*/
1083
	bro(ld) = def; /* father */
1182
	bro(ld) = def; /* father */
-
 
1183
	pt(son(ld)) = nld;
1084
	pt(son(ld)) = nld; no(son(ld))+=2; 	/* two new used of id */
1184
	no(son(ld)) += 2; 	/* two new used of id */
1085
	bro(def) = seq;
1185
	bro(def) = seq;
1086
	pt(varid) = ldvar;
1186
	pt(varid) = ldvar;
1087
	setcaonly(varid);
1187
	setcaonly(varid);
1088
 
1188
 
1089
	/* now relpace all old uses of ld in loop by varid */
1189
	/* now relpace all old uses of ld in loop by varid */
1090
	for( z = pt(ld); z != nilexp; z = pt(z)) {
1190
	for (z = pt(ld); z != nilexp; z = pt(z)) {
1091
	      if (no(z) == no(ld) && intnl_to(loop, z) ) {
1191
	      if (no(z) == no(ld) && intnl_to(loop, z)) {
1092
/* ALTERATION #1 */
1192
/* ALTERATION #1 */
1093
		exp lu = getexp(sh(z), bro(z), last(z), varid, pt(varid), 0,
1193
		exp lu = getexp(sh(z), bro(z), last(z), varid, pt(varid), 0,
1094
					0, name_tag);
1194
				0, name_tag);
1095
		pos = position(z);
1195
		pos = position(z);
1096
		pt(varid) = lu; no(varid)++;
1196
		pt(varid) = lu;
-
 
1197
		no(varid) ++;
1097
		kill_exp(z, nilexp); /* this should not kill the def of ld! */
1198
		kill_exp(z, nilexp); /* this should not kill the def of ld! */
1098
		*pos = lu;
1199
		*pos = lu;
1099
	      }
1200
	      }
1100
	}
1201
	}
1101
	pos = position(loop);
1202
	pos = position(loop);
1102
	*pos = varid;
1203
	*pos = varid;
1103
	bro(loop) = seqh; setlast(loop);
1204
	bro(loop) = seqh;
-
 
1205
	setlast(loop);
1104
}
1206
}
1105
 
1207
 
1106
 
1208
 
1107
exp limexp
1209
exp
1108
    PROTO_N ( ( test, ld) )
-
 
1109
    PROTO_T ( exp test X exp ld )
1210
limexp(exp test, exp ld)
1110
{
1211
{
1111
	exp lh = son(test);
1212
	exp lh = son(test);
1112
	exp rh = bro(lh);
1213
	exp rh = bro(lh);
1113
	if (name(lh) == cont_tag && name(son(lh) )== name_tag
1214
	if (name(lh) == cont_tag && name(son(lh)) == name_tag &&
1114
		&& son(son(lh))==son(ld) && no(son(lh))==no(ld) ) return rh;
1215
	    son(son(lh)) == son(ld) && no(son(lh)) == no(ld)) {
-
 
1216
		return rh;
-
 
1217
	}
1115
 
1218
 
1116
	if (name(rh) == cont_tag && name(son(rh) )== name_tag
1219
	if (name(rh) == cont_tag && name(son(rh)) == name_tag &&
1117
		&& son(son(rh))==son(ld) && no(son(rh))==no(ld) ) return lh;
1220
	    son(son(rh)) == son(ld) && no(son(rh)) == no(ld)) {
-
 
1221
		return lh;
-
 
1222
	}
1118
	return nilexp;
1223
	return nilexp;
1119
}
1224
}
1120
 
1225
 
1121
exp limaddptr
1226
exp
1122
    PROTO_N ( (arr, val, m) )
-
 
1123
    PROTO_T ( exp arr X exp val X int m )
1227
limaddptr(exp arr, exp val, int m)
1124
{
1228
{
1125
	exp naddptr = getexp(sh(arr), nilexp, 0, copy(arr), nilexp, 0, 0,
1229
	exp naddptr = getexp(sh(arr), nilexp, 0, copy(arr), nilexp, 0, 0,
1126
			addptr_tag);
1230
			     addptr_tag);
1127
	exp z, v;
1231
	exp z, v;
1128
        shape s;
1232
        shape s;
1129
	if (m==1 ) {
1233
	if (m == 1) {
1130
	    z = copy(val);
1234
	    z = copy(val);
1131
	    bro(z) = naddptr; setlast(z);
1235
	    bro(z) = naddptr;
1132
	}
1236
	    setlast(z);
1133
	else {
1237
	} else {
1134
            s = f_offset(al1_of(sh(naddptr)),al1_of(sh(naddptr)));
1238
            s = f_offset(al1_of(sh(naddptr)), al1_of(sh(naddptr)));
1135
	    z = getexp(s, naddptr, 1, copy(val), nilexp, 0,0,offset_mult_tag);
1239
	    z = getexp(s, naddptr, 1, copy(val), nilexp, 0, 0, offset_mult_tag);
1136
	    v = getexp(s, z, 1, nilexp, nilexp, 0, m*8, val_tag);
1240
	    v = getexp(s, z, 1, nilexp, nilexp, 0, m*8, val_tag);
-
 
1241
	    bro(son(z)) = v;
1137
	    bro(son(z)) = v; clearlast(son(z));
1242
	    clearlast(son(z));
1138
	}
1243
	}
-
 
1244
	bro(son(naddptr)) = z;
1139
	bro(son(naddptr)) = z; clearlast(son(naddptr));
1245
	clearlast(son(naddptr));
1140
	return naddptr;
1246
	return naddptr;
1141
		/* a new addptr with index replaced by val - used in limdec*/
1247
	/* a new addptr with index replaced by val - used in limdec*/
1142
}
1248
}
1143
 
1249
 
1144
exp limmult
1250
exp
1145
    PROTO_N ( (arr, val, m) )
-
 
1146
    PROTO_T ( exp arr X exp val X int m )
1251
limmult(exp arr, exp val, int m)
1147
{
1252
{
1148
	exp naddptr = getexp(sh(son(arr)), nilexp, 0, copy(val), nilexp, 0, 0,
1253
	exp naddptr = getexp(sh(son(arr)), nilexp, 0, copy(val), nilexp, 0, 0,
1149
			mult_tag);
1254
			     mult_tag);
1150
	exp v = getexp(sh(son(arr)), nilexp, 1, nilexp, nilexp, 0, m, val_tag);
1255
	exp v = getexp(sh(son(arr)), nilexp, 1, nilexp, nilexp, 0, m, val_tag);
1151
	bro(v) = naddptr;
1256
	bro(v) = naddptr;
-
 
1257
	bro(son(naddptr)) = v;
1152
	bro(son(naddptr)) = v; clearlast(son(naddptr));
1258
	clearlast(son(naddptr));
1153
	return naddptr;
1259
	return naddptr;
1154
		/* a new addptr with index replaced by val - used in limdec*/
1260
	/* a new addptr with index replaced by val - used in limdec*/
-
 
1261
}
-
 
1262
 
-
 
1263
 
-
 
1264
exp
-
 
1265
limreff(exp arr, int bytedisp)
-
 
1266
{
-
 
1267
	if (bytedisp != 0) {
-
 
1268
		exp nreff = getexp(sh(arr), nilexp, 0, copy(arr), nilexp, 0,
-
 
1269
				   bytedisp * 8, reff_tag);
-
 
1270
		bro(son(nreff)) = nreff;
-
 
1271
		setlast(son(nreff));
-
 
1272
		return nreff;
-
 
1273
	} else {
-
 
1274
		return copy(arr);
-
 
1275
	}
1155
}
1276
}
1156
 
1277
 
1157
exp limreff
1278
exp
1158
    PROTO_N ( (arr, bytedisp) )
-
 
1159
    PROTO_T ( exp arr X int bytedisp )
1279
limconst(exp arr, int bytedisp)
1160
{
1280
{
1161
        if (bytedisp != 0) {
-
 
1162
	exp nreff = getexp(sh(arr), nilexp, 0, copy(arr),
-
 
1163
                                              nilexp, 0, bytedisp*8, reff_tag);
-
 
1164
	bro(son(nreff)) = nreff; setlast(son(nreff));
-
 
1165
	return nreff;
-
 
1166
        }
-
 
1167
        else return copy(arr);
-
 
1168
}
-
 
1169
 
-
 
1170
exp limconst
-
 
1171
    PROTO_N ( (arr, bytedisp) )
-
 
1172
    PROTO_T ( exp arr X int bytedisp )
-
 
1173
{
-
 
1174
	exp nreff = getexp(/*sh(son(arr))*/slongsh, nilexp, 1, nilexp,
1281
	exp nreff = getexp(/*sh(son(arr))*/slongsh, nilexp, 1, nilexp, nilexp,
1175
                                              nilexp, 0, bytedisp, val_tag);
1282
			   0, bytedisp, val_tag);
1176
	UNUSED(arr);
1283
	UNUSED(arr);
1177
	bro(nreff) = nreff;
1284
	bro(nreff) = nreff;
1178
 	return nreff;
1285
 	return nreff;
1179
}
1286
}
1180
 
1287
 
1181
exp limdec
-
 
1182
    PROTO_N ( ( adec, val, mult) )
-
 
1183
    PROTO_T ( exp adec X exp val X int mult )
-
 
1184
{
-
 
1185
	exp init = son(adec);
-
 
1186
	exp bdy = bro(init);
-
 
1187
	exp ninit =  (name(val)!=val_tag) ?
-
 
1188
                      limaddptr(son(init), val, mult):
-
 
1189
                      limreff(son(init), mult*no(val)
-
 
1190
                     );
-
 
1191
	exp nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
-
 
1192
	bro(ninit) = bdy; clearlast(ninit);
-
 
1193
	bro(bdy) = nb; setlast(bdy);
-
 
1194
	bro(init) = nb;
-
 
1195
	return nb; /* the declaration of the limit value */
-
 
1196
}
-
 
1197
 
1288
 
1198
exp limdec2
1289
exp
1199
    PROTO_N ( ( adec, val, mult) )
-
 
1200
    PROTO_T ( exp adec X exp val X int mult )
1290
limdec(exp adec, exp val, int mult)
1201
{
1291
{
1202
	exp init = son(adec);
1292
	exp init = son(adec);
1203
	exp bdy = bro(init);
1293
	exp bdy = bro(init);
1204
	exp ninit =  (name(val)!=val_tag) ?
-
 
1205
                      limmult(son(init), val, mult):
1294
	exp ninit = (name(val) != val_tag) ? limaddptr(son(init), val, mult) :
1206
                      limconst(son(init), mult*no(val)
1295
		limreff(son(init), mult * no(val));
1207
                     );
-
 
1208
	exp nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
1296
	exp nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
-
 
1297
	bro(ninit) = bdy;
1209
	bro(ninit) = bdy; clearlast(ninit);
1298
	clearlast(ninit);
1210
	bro(bdy) = nb; setlast(bdy);
1299
	bro(bdy) = nb;
-
 
1300
	setlast(bdy);
1211
	bro(init) = nb;
1301
	bro(init) = nb;
1212
	return nb; /* the declaration of the limit value */
1302
	return nb; /* the declaration of the limit value */
1213
}
1303
}
-
 
1304
 
-
 
1305
 
-
 
1306
exp
-
 
1307
limdec2(exp adec, exp val, int mult)
-
 
1308
{
-
 
1309
	exp init = son(adec);
-
 
1310
	exp bdy = bro(init);
-
 
1311
	exp ninit = (name(val) != val_tag) ? limmult(son(init), val, mult) :
-
 
1312
		limconst(son(init), mult * no(val));
-
 
1313
	exp nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
-
 
1314
	bro(ninit) = bdy;
-
 
1315
	clearlast(ninit);
-
 
1316
	bro(bdy) = nb;
-
 
1317
	setlast(bdy);
-
 
1318
	bro(init) = nb;
-
 
1319
	return nb; /* the declaration of the limit value */
-
 
1320
}
1214
 
1321
 
1215
 
1322
 
1216
void remove_incr
1323
void
1217
    PROTO_N ( (adec, test, incr, mult) )
-
 
1218
    PROTO_T ( exp adec X exp test X exp incr X int mult )
1324
remove_incr(exp adec, exp test, exp incr, int mult)
1219
{
1325
{
1220
	exp le = limexp(test, son(incr));
1326
	exp le = limexp(test, son(incr));
1221
	exp * pos;
1327
	exp *pos;
1222
	exp ndec = limdec(adec, le, mult);
1328
	exp ndec = limdec(adec, le, mult);
1223
	exp lda = getexp(f_pointer(long_to_al(shape_align(sh(son(adec))))),
1329
	exp lda = getexp(f_pointer(long_to_al(shape_align(sh(son(adec))))),
1224
                                   nilexp, 1, adec, pt(adec), 0, 0, name_tag);
1330
                                   nilexp, 1, adec, pt(adec), 0, 0, name_tag);
1225
	exp clda = getexp(sh(son(adec)), nilexp, 0, lda,
1331
	exp clda = getexp(sh(son(adec)), nilexp, 0, lda, nilexp, 0, 0,
1226
                                              nilexp, 0, 0, cont_tag);
1332
			  cont_tag);
1227
	exp ldn = getexp(sh(son(ndec)), nilexp, 0, ndec,
1333
	exp ldn = getexp(sh(son(ndec)), nilexp, 0, ndec, pt(ndec), 0, 0,
1228
                                              pt(ndec), 0, 0, name_tag);
1334
			 name_tag);
1229
	exp ntestx = getexp(sh(test), bro(test), last(test),
1335
	exp ntestx = getexp(sh(test), bro(test), last(test), nilexp, pt(test),
1230
                       nilexp, pt(test), props(test), no(test), name(test));
1336
			    props(test), no(test), name(test));
1231
	bro(lda) = clda;   pt(adec) = lda; no(adec)++;
1337
	bro(lda) = clda;
-
 
1338
	pt(adec) = lda;
-
 
1339
	no(adec)++;
1232
	pt(ndec) = ldn; no(ndec)++;
1340
	pt(ndec) = ldn;
-
 
1341
	no(ndec)++;
1233
	if (last(le)) {
1342
	if (last(le)) {
1234
	       son(ntestx) = clda;
1343
	       son(ntestx) = clda;
-
 
1344
	       bro(clda) = ldn;
1235
	       bro(clda) = ldn; clearlast(clda);
1345
	       clearlast(clda);
1236
	       bro(ldn) = ntestx; setlast(ldn);
1346
	       bro(ldn) = ntestx;
1237
	}
1347
	       setlast(ldn);
1238
	else {
1348
	} else {
1239
	       son(ntestx) = ldn;
1349
	       son(ntestx) = ldn;
1240
	       bro(ldn) = clda; clearlast(ldn);
1350
	       bro(ldn) = clda;
-
 
1351
	       clearlast(ldn);
1241
	       bro(clda) = ntestx; setlast(clda);
1352
	       bro(clda) = ntestx;
-
 
1353
	       setlast(clda);
1242
	};
1354
	}
1243
	pos = position(test);
1355
	pos = position(test);
1244
	*pos = ntestx;
1356
	*pos = ntestx;
1245
	kill_exp(test, nilexp);
1357
	kill_exp(test, nilexp);
1246
	setname(incr, top_tag);
1358
	setname(incr, top_tag);
1247
	kill_exp(bro(son(incr)), nilexp);
1359
	kill_exp(bro(son(incr)), nilexp);
1248
	kill_exp(son(incr), nilexp);
1360
	kill_exp(son(incr), nilexp);
1249
	son(incr) = nilexp;
1361
	son(incr) = nilexp;
1250
}
1362
}
-
 
1363
 
1251
 
1364
 
1252
void remove_incr2
1365
void
1253
    PROTO_N ( (adec, test, incr, mult) )
-
 
1254
    PROTO_T ( exp adec X exp test X exp incr X int mult )
1366
remove_incr2(exp adec, exp test, exp incr, int mult)
1255
{
1367
{
1256
	exp le = limexp(test, son(incr));
1368
	exp le = limexp(test, son(incr));
1257
	exp * pos;
1369
	exp *pos;
1258
	exp init = son(adec);
1370
	exp init = son(adec);
1259
	exp bdy = bro(init);
1371
	exp bdy = bro(init);
1260
	exp ninit,ldn,ntestx,lda,clda;
1372
	exp ninit, ldn, ntestx, lda, clda;
1261
        if (name(le)!=val_tag && !remove_unused_index_counters)
1373
        if (name(le) != val_tag && !remove_unused_index_counters) {
1262
           return;
1374
           return;
-
 
1375
	}
1263
	lda = getexp(f_pointer(long_to_al(shape_align(sh(son(adec))))),
1376
	lda = getexp(f_pointer(long_to_al(shape_align(sh(son(adec))))), nilexp,
1264
                                   nilexp, 1, adec, pt(adec), 0, 0, name_tag);
1377
		     1, adec, pt(adec), 0, 0, name_tag);
1265
	clda = getexp(/*sh(son(adec))*/slongsh, nilexp, 0, lda,
1378
	clda = getexp(/*sh(son(adec))*/slongsh, nilexp, 0, lda, nilexp, 0, 0,
1266
                                              nilexp, 0, 0, cont_tag);
1379
		      cont_tag);
1267
        if  (name(le)==val_tag)
1380
        if (name(le) == val_tag) {
1268
             {ninit = limconst(son(init), mult*no(le));
1381
		ninit = limconst(son(init), mult*no(le));
1269
              ldn = ninit;
1382
		ldn = ninit;
1270
             }
1383
	} else {
1271
        else {exp nb;
1384
		exp nb;
1272
              ninit =  limmult(son(init), le, mult);
1385
		ninit =  limmult(son(init), le, mult);
1273
              nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
1386
		nb = getexp(sh(bdy), adec, 1, ninit, nilexp, 0, 0, ident_tag);
-
 
1387
		bro(ninit) = bdy;
1274
	      bro(ninit) = bdy; clearlast(ninit);
1388
		clearlast(ninit);
1275
	      bro(bdy) = nb; setlast(bdy);
1389
		bro(bdy) = nb;
-
 
1390
		setlast(bdy);
1276
	      bro(init) = nb;
1391
		bro(init) = nb;
1277
              ninit = nb;
1392
		ninit = nb;
1278
              ldn = getexp(sh(son(ninit)), nilexp, 0, ninit,
1393
		ldn = getexp(sh(son(ninit)), nilexp, 0, ninit, pt(ninit), 0, 0,
1279
                                              pt(ninit), 0, 0, name_tag);
1394
			     name_tag);
1280
              pt(ninit) = ldn; no(ninit)++;
1395
		pt(ninit) = ldn;
1281
             }
1396
		no(ninit)++;
-
 
1397
	}
1282
	ntestx = getexp(sh(test), bro(test), last(test),
1398
	ntestx = getexp(sh(test), bro(test), last(test), nilexp, pt(test),
1283
                       nilexp, pt(test), props(test), no(test), name(test));
1399
			props(test), no(test), name(test));
1284
	bro(lda) = clda;   pt(adec) = lda; no(adec)++;
1400
	bro(lda) = clda;
-
 
1401
	pt(adec) = lda;
-
 
1402
	no(adec)++;
1285
	if (last(le)) {
1403
	if (last(le)) {
1286
	       son(ntestx) = clda;
1404
	       son(ntestx) = clda;
-
 
1405
	       bro(clda) = ldn;
1287
	       bro(clda) = ldn; clearlast(clda);
1406
	       clearlast(clda);
1288
	       bro(ldn) = ntestx; setlast(ldn);
1407
	       bro(ldn) = ntestx;
1289
	}
1408
	       setlast(ldn);
1290
	else {
1409
	} else {
1291
	       son(ntestx) = ldn;
1410
	       son(ntestx) = ldn;
1292
	       bro(ldn) = clda; clearlast(ldn);
1411
	       bro(ldn) = clda;
-
 
1412
	       clearlast(ldn);
1293
	       bro(clda) = ntestx; setlast(clda);
1413
	       bro(clda) = ntestx;
-
 
1414
	       setlast(clda);
1294
	};
1415
	}
1295
	pos = position(test);
1416
	pos = position(test);
1296
	*pos = ntestx;
1417
	*pos = ntestx;
1297
	kill_exp(test, nilexp);
1418
	kill_exp(test, nilexp);
1298
	setname(incr, top_tag);
1419
	setname(incr, top_tag);
1299
	kill_exp(bro(son(incr)), nilexp);
1420
	kill_exp(bro(son(incr)), nilexp);
1300
	kill_exp(son(incr), nilexp);
1421
	kill_exp(son(incr), nilexp);
1301
	son(incr) = nilexp;
1422
	son(incr) = nilexp;
1302
}
1423
}
1303
 
1424
 
-
 
1425
 
1304
int  use_in
1426
int
1305
    PROTO_N ( (w, ld) )
-
 
1306
    PROTO_T ( exp w X exp ld )
1427
use_in(exp w, exp ld)
1307
{
1428
{
1308
      switch (name(w)) {
1429
	switch (name(w)) {
1309
	case name_tag: {
1430
	case name_tag:
1310
	      return (son(w)==son(ld) && no(w)==no(ld) ) ;
1431
		return (son(w) == son(ld) && no(w) == no(ld));
1311
	}
-
 
1312
	case ass_tag: {
1432
	case ass_tag: {
1313
	      int z = use_in(bro(son(w)), ld) ;
1433
		int z = use_in(bro(son(w)), ld);
1314
	      if (z!=0 ) return z;
1434
		if (z != 0) {
1315
	      if (name(son(w)) == name_tag &&
1435
			return z;
-
 
1436
		}
1316
                  son(son(w))==son(ld) &&
1437
		if (name(son(w)) == name_tag && son(son(w)) == son(ld) &&
1317
                  no(son(w))==no(ld))
1438
		    no(son(w)) == no(ld)) {
1318
 		  { return -1;}
1439
			return -1;
-
 
1440
		}
1319
	      return use_in(son(w), ld);
1441
		return use_in(son(w), ld);
1320
	}
1442
	}
1321
  	case goto_tag:
1443
	case goto_tag:
1322
        case case_tag:
1444
	case case_tag:
-
 
1445
	case test_tag:
1323
        case test_tag: case testbit_tag:
1446
	case testbit_tag:
1324
        case labst_tag:{ return 1; }
1447
	case labst_tag:
-
 
1448
		return 1;
1325
	default: {
1449
	default: {
1326
	     exp z = son(w);
1450
		exp z = son(w);
1327
	     if (z==nilexp) return 0;
1451
		if (z == nilexp) {
-
 
1452
			return 0;
-
 
1453
		}
1328
	     for(;;) {
1454
		for (;;) {
1329
		int a = use_in(z, ld);
1455
			int a = use_in(z, ld);
1330
		if (a !=0 || last(z)) return a;
1456
			if (a != 0 || last(z)) {
-
 
1457
				return a;
-
 
1458
			}
1331
	 	z = bro(z);
1459
			z = bro(z);
1332
	     }
1460
		}
1333
	}
1461
	}
1334
     }
1462
	}
1335
}
1463
}
1336
 
1464
 
1337
 
1465
 
1338
 
1466
int
1339
int suitable_test
-
 
1340
    PROTO_N ( (tests, incrld, loop) )
-
 
1341
    PROTO_T ( exp tests X exp incrld X exp loop )
1467
suitable_test(exp tests, exp incrld, exp loop)
1342
{
1468
{
1343
	/* is test such that one can remove the increment ? */
1469
	/* is test such that one can remove the increment ? */
1344
	exp t, p;
1470
	exp t, p;
1345
	exp decx = son(incrld);
1471
	exp decx = son(incrld);
1346
	exp v;
1472
	exp v;
1347
	if (tests == nilexp || no(tests)!=1 || bro(tests) != nilexp )
1473
	if (tests == nilexp || no(tests) != 1 || bro(tests) != nilexp) {
1348
          return 0;
1474
		return 0;
-
 
1475
	}
1349
	t = son(tests);
1476
	t = son(tests);
1350
	v = limexp(t, incrld);
1477
	v = limexp(t, incrld);
-
 
1478
	if (name(v) != val_tag &&
1351
	if (name(v) != val_tag && (!invariant(v, assign_alias)|| maybe_incr(v)) )
1479
	    (!invariant(v, assign_alias) || maybe_incr(v))) {
1352
          return 0;
1480
		return 0;
-
 
1481
	}
1353
	while(t!=loop && last(t)) t= bro(t);
1482
	while (t != loop && last(t)) {
-
 
1483
		t= bro(t);
-
 
1484
	}
1354
	if (t!=loop) return 0;
1485
	if (t != loop) {
-
 
1486
		return 0;
-
 
1487
	}
1355
	while (name(t) !=proc_tag && t != decx) {
1488
	while (name(t) != proc_tag && t != decx) {
1356
	     exp b = bro(t);
1489
		exp b = bro(t);
1357
	     if (!last(t)) {
1490
		if (!last(t)) {
1358
		for (p=pt(decx); p!=nilexp; p=pt(p)) {
1491
			for (p = pt(decx); p != nilexp; p = pt(p)) {
1359
			if (intnl_to(b, p)  && use_in(b, incrld) == 1)
1492
				if (intnl_to(b, p) && use_in(b, incrld) == 1) {
1360
                           return 0;
1493
					return 0;
-
 
1494
				}
-
 
1495
			}
1361
		}
1496
		}
1362
	     }
-
 
1363
	     t = b;
1497
		t = b;
1364
	}
1498
	}
1365
	return 1;
1499
	return 1;
1366
}
1500
}
1367
 
1501
 
1368
 
1502
 
1369
 
1503
int
1370
int do_one_rep
-
 
1371
    PROTO_N ( (loop) )
-
 
1372
    PROTO_T ( exp loop )
1504
do_one_rep(exp loop)
1373
{
1505
{
1374
	exp body = bro(son(bro(son(loop))));
1506
	exp body = bro(son(bro(son(loop))));
1375
	exp z ;
1507
	exp z;
1376
	int res =0;
1508
	int res =0;
1377
	exp xincrs;
1509
	exp xincrs;
1378
	Assert(name(loop)==rep_tag);
1510
	Assert(name(loop) == rep_tag);
-
 
1511
	incrs = nilexp;
1379
	incrs=nilexp; alteredset=nilexp;
1512
	alteredset=nilexp;
1380
        assign_alias = false; jump_out = false;
1513
	assign_alias = false;
-
 
1514
	jump_out = false;
1381
	scan_for_incr(body,loop, collect_incrs);
1515
	scan_for_incr(body, loop, collect_incrs);
1382
	if (  !jump_out && name(sh(loop)) == tophd) {
1516
	if (!jump_out && name(sh(loop)) == tophd) {
1383
	     for (z=alteredset;   z != nilexp; z = bro(z)) {
1517
		for (z = alteredset; z != nilexp; z = bro(z)) {
1384
	/* look to see if var assigned to in loop can be locally declared
1518
			/* look to see if var assigned to in loop can be
1385
		ie Rep f(z) => Var x := cont(z) in Rep f(x); z = cont x ni;
1519
			 * locally declared ie Rep f(z) => Var x := cont(z) in
1386
                ? only worth while if z is global
1520
			 * Rep f(x); z = cont x ni; ? only worth while if z is
1387
		if z is local only worthwhile if it isnt being allocated
1521
			 * global if z is local only worthwhile if it isnt
1388
                in reg anyway
1522
			 * being allocated in reg anyway
1389
	*/
1523
			 */
1390
		exp a = son(z);
1524
			exp a = son(z);
1391
 
1525
 
1392
		if (name(a)==name_tag &&
1526
			if (name(a) == name_tag &&
1393
                   (isglob(son(a)) || !isvar(son(a)) ) &&
1527
			    (isglob(son(a)) || !isvar(son(a))) &&
1394
	           (props(son(a)) & subvar) == 0 &&
1528
			    (props(son(a)) & subvar) == 0 &&
-
 
1529
			    (!assign_alias ||
1395
		   (!assign_alias || (isvar(son(a)) && iscaonly(son(a)))) &&
1530
			     (isvar(son(a)) && iscaonly(son(a)))) &&
1396
	           !intnl_to(body, son(a)) ){
1531
			    !intnl_to(body, son(a))) {
1397
		    exp p ;
1532
				exp p;
1398
		    exp dc = son(a);
1533
				exp dc = son(a);
1399
		    shape shcont;
1534
				shape shcont;
1400
		    int const_init = !isglob(dc) &&
1535
				int const_init = !isglob(dc) &&
1401
				      (name(son(dc)) == clear_tag ||
1536
				    (name(son(dc)) == clear_tag ||
1402
                                       name(son(dc)) == val_tag ||
1537
				     name(son(dc)) == val_tag ||
1403
				       name(son(dc)) == real_tag ||
1538
				     name(son(dc)) == real_tag ||
1404
				       (name(son(dc)) == name_tag &&
1539
				     (name(son(dc)) == name_tag &&
1405
                                        !isvar(son(son(dc)))
1540
				      !isvar(son(son(dc)))));
1406
                                       )
-
 
1407
                                      );
-
 
1408
		    for ( p = pt(son(a)); p != nilexp; p = pt(p)) {
1541
				for (p = pt(son(a)); p != nilexp; p = pt(p)) {
1409
#ifdef NEWDIAGS
1542
#ifdef NEWDIAGS
1410
			int inb;
1543
					int inb;
1411
			if (isdiaginfo(p)) continue;
1544
					if (isdiaginfo(p)) {
-
 
1545
						continue;
-
 
1546
					}
1412
			inb = intnl_to(body,p);
1547
					inb = intnl_to(body, p);
1413
#else
1548
#else
1414
			int inb = intnl_to(body,p);
1549
					int inb = intnl_to(body, p);
1415
#endif
1550
#endif
1559
		exp z = bro(incrs);
1748
		exp z = bro(incrs);
1560
		retcell(incrs);
1749
		retcell(incrs);
1561
		incrs = z;
1750
		incrs = z;
1562
	}
1751
	}
1563
	while (alteredset !=nilexp)
1752
	while (alteredset != nilexp) {
1564
		{ exp z = bro(alteredset);
1753
		exp z = bro(alteredset);
1565
		  retcell(son(alteredset));
1754
		retcell(son(alteredset));
1566
                  retcell(alteredset);
1755
		retcell(alteredset);
1567
                  alteredset = z; }
1756
		alteredset = z;
-
 
1757
	}
1568
	return res;
1758
	return res;
1569
}
1759
}
1570
 
1760
 
-
 
1761
 
1571
void order_loops
1762
void
1572
    PROTO_N ( (reps) )
-
 
1573
    PROTO_T ( exp reps )
1763
order_loops(exp reps)
1574
{
1764
{
1575
	/* start at outer loop ?! */
1765
	/* start at outer loop ?! */
1576
	if ( (props(reps) & 0x80) == 0 ){
1766
	if ((props(reps) & 0x80) == 0) {
1577
		if (bro(reps)!=nilexp) order_loops(bro(reps));
1767
		if (bro(reps) != nilexp) {
1578
		if (son(reps) != nilexp &&
1768
			order_loops(bro(reps));
-
 
1769
		}
1579
                    name(son(reps))==rep_tag &&
1770
		if (son(reps) != nilexp && name(son(reps)) == rep_tag &&
1580
                    no(reps) < max_loop_depth)
1771
		    no(reps) < max_loop_depth) {
1581
		  { exp loop = son(reps);
1772
			exp loop = son(reps);
1582
/* ALTERATION #2 - does not effect C */
1773
			/* ALTERATION #2 - does not effect C */
1583
		    if (name(son(loop)) != top_tag) {
1774
			if (name(son(loop)) != top_tag) {
1584
		    	/* make loop(st,b) into seq((st), loop(make_top, b))
1775
				/* make loop(st, b) into seq((st),
1585
		    		analysis assumes son(loop) = top!
1776
				 * loop(make_top, b)) analysis assumes
1586
		    	*/
1777
				 * son(loop) = top! */
1587
		    	exp st = son(loop);
1778
				exp st = son(loop);
1588
		    	exp b = bro(st);
1779
				exp b = bro(st);
1589
		    	exp * pos = ifc_ptr_position(loop);
1780
				exp * pos = ifc_ptr_position(loop);
1590
		    	exp mt = getexp(f_top, b,0, nilexp,
1781
				exp mt = getexp(f_top, b, 0, nilexp, nilexp, 0,
1591
		    				nilexp, 0, 0, top_tag);
1782
						0, top_tag);
1592
		    	exp sl = getexp(f_top, loop, 0 ,st, nilexp, 0,0,0);
1783
				exp sl = getexp(f_top, loop, 0 ,st, nilexp, 0,
-
 
1784
						0, 0);
1593
		    	exp s = getexp(sh(loop), bro(loop), last(loop), sl,
1785
				exp s = getexp(sh(loop), bro(loop), last(loop),
1594
		    			nilexp, 0, 0, seq_tag);
1786
					       sl, nilexp, 0, 0, seq_tag);
1595
 
1787
 
1596
		        bro(st) = sl; setlast(st);
1788
				bro(st) = sl;
-
 
1789
				setlast(st);
1597
		        son(loop) = mt;
1790
				son(loop) = mt;
-
 
1791
				bro(loop) = s;
1598
		        bro(loop) = s; setlast(loop);
1792
				setlast(loop);
1599
		        *pos = s;
1793
				*pos = s;
1600
 
-
 
1601
		    }
1794
			}
1602
		    IGNORE do_one_rep(loop);
1795
			IGNORE do_one_rep(loop);
1603
		  }
1796
		}
1604
		props(reps) |=0x80;
1797
		props(reps) |=0x80;
1605
	}
1798
	}
1606
}
1799
}
1607
 
1800
 
-
 
1801
 
1608
void forall_opt
1802
void
1609
    PROTO_Z ()
1803
forall_opt(void)
1610
{
1804
{
1611
	exp reps =get_repeats();
1805
	exp reps = get_repeats();
1612
	while (reps != nilexp) {
1806
	while (reps != nilexp) {
1613
		order_loops(reps);
1807
		order_loops(reps);
1614
		reps = pt(reps);
1808
		reps = pt(reps);
1615
	}
1809
	}
1616
  if (do_loopconsts) {
1810
	if (do_loopconsts) {
1617
    do_foralls = false;
1811
		do_foralls = false;
1618
    repeat_consts ();		/* try constant extraction again */
1812
		/* try constant extraction again */
-
 
1813
		repeat_consts();
1619
  }
1814
	}
1620
}
1815
}