Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[28e18d]: Backend / If1opt / if1invar.c Maximize Restore History

Download this file

if1invar.c    409 lines (310 with data), 10.9 kB

  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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**************************************************************************/
/* FILE ************** if1invar.c ************************/
/**************************************************************************/
/* Author: Dave Cann */
/* Update: Patrick Miller -- Ansi support (Dec 2000) */
/* Copyright (C) University of California Regents */
/**************************************************************************/
/*
* $Log:
*
* Revision 12.7 1992/11/04 22:04:57 miller
* Initial revision
*
* Revision 12.7 1992/10/21 18:08:34 miller
* Initial RCS Version by Cann
*/
/**************************************************************************/
#include "world.h"
static int icnt = 0; /* COUNT OF INVARIANTS REMOVED */
static int eivcnt = 0; /* COUNT OF EXPOSED INVARIANTS */
static int amcnt = 0; /* COUNT OF ANTI-MOVEMENTS */
/**************************************************************************/
/* GLOBAL ************** OptIsEdgeInvariant ************************/
/**************************************************************************/
/* PURPOSE: RETURN TRUE IF NODE n IS LOOP INVARIANT. RETURN AND GENERATE */
/**************************************************************************/
int OptIsEdgeInvariant( i )
PEDGE i;
{
register PNODE dad;
if ( IsConst( i ) )
return( TRUE );
if ( !IsSGraph( i->src ) )
return( FALSE );
dad = i->src->G_DAD;
if ( !IsImport( dad, i->eport ) )
return( FALSE );
if ( IsLoop( dad ) || IsForall( dad ) )
return( TRUE );
return( FALSE );
}
/**************************************************************************/
/* GLOBAL ************** ExposeInvariants ************************/
/**************************************************************************/
/* PURPOSE: EXPOSE INVARIANTS ALONG THE ASSOCIATIVE AND COMMUTATIVE CHAIN */
/* TERMINATED BY s, GIVEN t IS A PREDECESSOR TO s ON THE CHAIN. */
/**************************************************************************/
void ExposeInvariants( s, t )
PNODE s;
PNODE t;
{
register PEDGE e;
register PEDGE ee;
/* HAS THE ENTIRE CHAIN BEEN SEARCHED? */
if ( s->type != t->type )
return;
/* LET CONSTANT FOLDING HAVE A SHOT AT IT! */
if ( IsConst( t->imp ) && IsConst( t->imp->isucc ) )
return;
/* IS TARGET t A CANDIDATE? */
if ( !OptIsEdgeInvariant( t->imp->isucc ) ) {
if ( t != s )
goto MoveUpTheChain;
else
return;
}
if ( OptIsEdgeInvariant( t->imp ) )
return;
/* ARE WE JUST STARTING? */
if ( s == t )
goto MoveUpTheChain;
/* OK, DO THE MODIFICATION */
e = s->imp;
UnlinkExport( e );
UnlinkImport( e );
e->eport = 1;
e->iport = 2;
LinkExportLists( e->src, s );
ee = t->imp->isucc;
UnlinkImport( ee );
ee->iport = 1;
LinkImport( s, ee );
LinkImport( t, e );
LinkExport( s, e );
UnlinkNode( s );
LinkNode( t->npred, s );
eivcnt++;
return;
MoveUpTheChain:
if ( IsConst( t->imp ) )
return;
/* FANOUT NOT ALLOWED IN THE CHAIN! */
if ( t->imp->src->exp->esucc != NULL )
return;
ExposeInvariants( s, t->imp->src );
}
/**************************************************************************/
/* LOCAL ************** RemoveInvariants ************************/
/**************************************************************************/
/* PURPOSE: REMOVE ALL LOOP INVARIANTS FROM THE LOOP AND FORALL NODES */
/* DEFINED IN GRAPH g. INVARIANTS OF INNERMOST LOOPS ARE */
/* REMOVED FIRST. THE NODES ARE EXAMINED IN DATAFLOW ORDER; */
/* HENCE, AN INVARIANT NODE WHOSE IMPORTS COME FROM OTHER */
/* INVARIANT NODES IN THE SAME SCOPE ARE EASILY DETECTED. */
/**************************************************************************/
static void RemoveInvariants( lvl, g )
int lvl;
PNODE g;
{
register PNODE n;
register PNODE nd;
register PNODE sn;
register PNODE sg;
register PEDGE e;
for ( n = g->G_NODES; n != NULL; n = n->nsucc )
if ( IsCompound( n ) )
for ( sg = n->C_SUBS; sg != NULL; sg = sg->gsucc )
RemoveInvariants( lvl + 1, sg );
for ( n = g->G_NODES; n != NULL; n = n->nsucc ) {
if ( !(IsForall( n ) || IsLoop( n )) )
continue;
for ( sg = n->C_SUBS; sg != NULL; sg = sg->gsucc )
for ( nd = sg->G_NODES; nd != NULL; nd = sn ) {
sn = nd->nsucc;
if ( IsLoop( nd ) || IsForall( nd ) )
if ( (lvl == 1) && (!Oinvar) )
continue;
if ( OptIsInvariant( nd ) ) {
switch ( nd->type ) {
case IFAFill:
case IFABuild:
case IFAAddH:
case IFAAddL:
case IFACatenate:
if ( !IsForall( n ) )
break;
if ( !IsReadOnly( nd, 1 ) )
continue;
break;
case IFForall:
if ( !IsForall( n ) )
break;
for ( e = nd->exp; e != NULL; e = e->esucc )
if ( !IsReadOnly( nd, e->eport ) )
break;
if ( e != NULL )
continue;
break;
default:
break;
}
RemoveNode( nd, sg );
InsertNode( n, nd ); icnt++;
}
}
}
}
/**************************************************************************/
/* LOCAL ************** ExlusiveUse ************************/
/**************************************************************************/
/* PURPOSE: RETURN THE EXCLUSIVE SUBGRAPH OF SELECT NODE s REFERENCING */
/* IMPORT iport, ELSE RETURN NULL. */
/**************************************************************************/
static PNODE ExclusiveUse( s, iport )
PNODE s;
int iport;
{
register PEDGE e1;
register PEDGE e2;
if ( IsExport( s->S_TEST, iport ) )
return( NULL );
e1 = FindExport( s->S_CONS, iport );
e2 = FindExport( s->S_ALT, iport );
if ( e1 != NULL && e2 != NULL )
return( NULL );
if ( e1 == NULL && e2 == NULL )
return( NULL );
if ( e1 == NULL )
return( s->S_ALT );
return( s->S_CONS );
}
/**************************************************************************/
/* STATIC ************** AntiMovement ************************/
/**************************************************************************/
/* PURPOSE: APPLY ANTI-NODE MOVEMENT TO GRAPH g. THIS IS AN ATTEMPT TO */
/* MOVE COMPUTATIONS BACK ACROSS SUBGRAPH BOARDERS TOWARD THEIR */
/* CONSUMERS. */
/**************************************************************************/
void AntiMovement( g )
PNODE g;
{
register PNODE n;
register PEDGE e;
register PNODE sg;
register PNODE s;
register PNODE np;
register PEDGE ee;
register PEDGE se;
register PEDGE see;
register PEDGE i;
register PEDGE si;
register int port;
if ( DeBuG ) return;
for ( n = FindLastNode( g ); n != g; n = np ) {
np = n->npred;
/* for -glue and PEEK */
if ( n->exp == NULL )
continue;
if ( IsCompound( n ) )
continue;
/* 12/10/91 CANN TO PREVENT BUILD-IN-PLACE FAILURES LIKE THE FOLLOWING: */
/* a := array_addh(for...end for,v); in if (x) error[work] else a end if */
for ( i = n->imp; i != NULL; i = i->isucc ) {
if ( !IsBasic( i->info ) )
break;
}
if ( i != NULL )
continue;
s = NULL;
sg = NULL;
for ( e = n->exp; e != NULL; e = e->esucc ) {
if ( !IsSelect( e->dst ) )
break;
if ( s == NULL ) {
s = e->dst;
if ( (sg = ExclusiveUse( s, e->iport )) == NULL )
break;
}
else if ( s != e->dst )
break;
if ( sg != ExclusiveUse( s, e->iport ) )
break;
}
if ( e != NULL )
continue;
/* OK, DO THE MOVEMENT */
UnlinkNode( n );
LinkNode( sg, n );
e = n->exp;
n->exp = NULL;
for ( /* NOTHING */; e != NULL; e = se ) {
se = e->esucc;
UnlinkImport( e );
for ( ee = sg->exp; ee != NULL; ee = see ) {
see = ee->esucc;
if ( ee->eport != e->iport )
continue;
UnlinkExport( ee );
ee->eport = e->eport;
LinkExport( n, ee );
}
}
i = n->imp;
n->imp = NULL;
for ( /* NOTHING */; i != NULL; i = si ) {
si = i->isucc;
if ( IsConst( i ) ) {
LinkImport( n, i );
continue;
}
port = ++maxint;
ee = EdgeAlloc( sg, port, n, i->iport );
ee->info = i->info;
i->iport = port;
LinkImport( s, i );
LinkExport( sg, ee );
LinkImport( n, ee );
}
amcnt++;
}
/* OK, NOW STEP INTO THE NEST */
for ( n = g->G_NODES; n != NULL; n = n->nsucc )
if ( IsCompound( n ) )
for ( sg = n->C_SUBS; sg != NULL; sg = sg->gsucc )
AntiMovement( sg );
}
/**************************************************************************/
/* GLOBAL ************** WriteInvarInfo ************************/
/**************************************************************************/
/* PURPOSE: PRINT INFORMATION GATHERED DURING INVARIANT REMOVAL TO stderr.*/
/**************************************************************************/
void WriteInvarInfo()
{
FPRINTF( infoptr, "\n **** LOOP INVARIANT NODE REMOVAL\n\n" );
FPRINTF( infoptr, " Loop Invariants Removed: %d\n", icnt );
FPRINTF( infoptr, " Exposed Invariants: %d\n", eivcnt );
FPRINTF( infoptr, " Movements into Selects: %d\n", amcnt );
}
/**************************************************************************/
/* GLOBAL ************** If1Invar ************************/
/**************************************************************************/
/* PURPOSE: PERFORM LOOP INVARIANT REMOVAL ON ALL FUNCTION GRAPHS. FIRST, */
/* SPECIAL SELECT INVARIANTS ARE MOVED. */
/**************************************************************************/
static int first = TRUE;
static int adone = FALSE;
void If1Invar()
{
register PNODE f;
for ( f = glstop->gsucc; f != NULL; f = f->gsucc ) {
/* EXPOSURE CAN ALTER CSE SO GIVE CSE A CHANCE BEFORE EXPOSURE */
if ( !first )
OptRemoveSCses( f );
RemoveInvariants( 1, f );
if ( amove && first && !adone )
AntiMovement( f );
}
first = FALSE;
adone = TRUE;
}