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

Download this file

if1inline.c    469 lines (364 with data), 15.5 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**************************************************************************/
/* FILE ************** if1inline.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"
#define FIRST_COLOR 0
#define SIZE_THRESHOLD 6000
#define MIN_FUNCTION_SIZE 100
static PCALL callhead = NULL; /* HEAD AND TAIL OF CALL GRAPH TRUNK */
static PCALL calltail = NULL;
static int color = FIRST_COLOR; /* CURRENT COLOR IN CYCLE DETECTION */
static int sskip = 0; /* COUNT OF SKIPPED EXPANSIONS */
int NoInlining = FALSE; /* Allow Inlined functions */
int noincnt = -1; /* FUNCTION CALL LIST INDEX */
char *noin[500]; /* FUNCTION CALL LIST */
static int GetGraphSize( g )
PNODE g;
{
register PNODE sg;
register int size;
register PNODE n;
size = 1;
for ( n = g->G_NODES; n != NULL; n = n->nsucc ) {
if ( IsCompound( n ) )
for ( sg = n->C_SUBS; sg != NULL; sg = sg->gsucc )
size += GetGraphSize( sg );
size++;
}
return( size );
}
/**************************************************************************/
/* LOCAL ************** OptFindCallee ************************/
/**************************************************************************/
/* PURPOSE: FIND AND RETURN THE CALL GRAPH TRUNK NODE ASSOCIATED WITH */
/* FUNCTION nm. IF NOT FOUND, NULL IS RETURNED. */
/**************************************************************************/
static PCALL OptFindCallee( nm )
char *nm;
{
register PCALL c;
for ( c = callhead; c != NULL; c = c->callee )
if ( strcmp( nm, c->graph->G_NAME ) == 0 )
return( c );
return( NULL );
}
/**************************************************************************/
/* LOCAL ************** OptAddCalleeReferences ************************/
/**************************************************************************/
/* PURPOSE: RECURSIVELY TRAVERSE THE IF1 GRAPH g, ADDING A CALL GRAPH */
/* NODE TO THE CALLEE REFERENCE LIST HEADED BY TRUNK NODE c FOR */
/* EACH ENCOUNTERED IF1 CALL NODE. THE REFERENCE COUNT OF THE */
/* CALLEE'S CALL GRAPH TRUNK NODE IS INCREMENTED. */
/**************************************************************************/
static void OptAddCalleeReferences( c, g )
PCALL c;
PNODE g;
{
register PNODE n;
register PCALL r;
register PCALL cee;
if ( IsIGraph( g ) )
return;
/* BUG Oct. 21, 1991 */
for ( n = g->G_NODES; n != NULL; n = n->nsucc ) {
if ( IsCall( n ) ) {
if ( (cee = OptFindCallee( n->imp->CoNsT )) == NULL )
continue;
r = CallAlloc( n );
r->caller = c->caller;
c->caller = r;
r->callee = cee;
r->callee->rcnt++;
continue;
}
if ( IsCompound( n ) )
for ( g = n->C_SUBS; g != NULL; g = g->gsucc )
OptAddCalleeReferences( c, g );
}
}
/**************************************************************************/
/* LOCAL ************** OptMakeCallGraph ************************/
/**************************************************************************/
/* PURPOSE: CONSTRUCT A CALL GRAPH. FIRST THE TRUNK OF THE CALL GRAPH IS */
/* BUILT. THE TRUNK CONTAINS AN ENTRY FOR EACH FUNCTION IN THE */
/* IF1 FILE. IF A CALLEE IS EXPORTED, THEN ITS REFERENCE COUNT */
/* FIELD (rcnt) IS INCREMENTED TO MAKE SURE THAT IT WILL NOT BE */
/* DESTROYED DURING EXPANSION. AN IMPORTED CALLEE CAN NEVER BE */
/* INLINED. AFTER THE TRUNK IS BUILT, THE CALLEE REFERENCE LISTS */
/* ARE CONSTRUCTED. */
/**************************************************************************/
static void OptMakeCallGraph()
{
register PNODE f;
register PCALL c;
for ( f = glstop->gsucc; f != NULL; f = f->gsucc ) {
c = CallAlloc( cfunct = f );
if ( IsIGraph( f ) )
c->InLineFunction = FALSE;
else if ( IsXGraph( f ) )
c->rcnt++;
if ( callhead != NULL )
calltail->callee = c;
else
callhead = c;
calltail = c;
}
for ( c = callhead; c != NULL; c = c->callee )
OptAddCalleeReferences( c, c->graph );
}
/**************************************************************************/
/* LOCAL ************** ReadInlineRequests ************************/
/**************************************************************************/
/* PURPOSE: READ FROM stdin EXPANSION REQUESTS. A PROMPT IS WRITTEN TO */
/* stdout FOR ONLY FUNCTION'S THAT ARE ALLOWED TO BE INLINED; */
/* THE USER HAS NO CONTROL OVER OTHER FUNCTIONS: THOSE IMPORTED */
/* OR PART OF RECURSIVE CYLCES THAT WERE BROKEN. */
/**************************************************************************/
static void ReadInlineRequests()
{
register int x;
register PCALL c;
char ans;
/* FIRST, OBEY COMMAND LINE REQUESTS FOR FUNCTION PRESERVATION */
for ( x = 0; x <= noincnt; x++ ) {
if ( (c = OptFindCallee( noin[x] )) == NULL )
Error2( "CANNOT FIND FUNCTION", noin[x] );
if ( c->InLineFunction ) {
c->InLineFunction = FALSE;
c->disabled = TRUE;
}
}
if ( !inter )
return;
for ( c = callhead; c != NULL; c = c->callee )
if ( c->InLineFunction ) {
PRINTF(" Expand %s Calls Inline (y/n): ",
c->graph->G_NAME );
ans = '\0';
(void)scanf( " %c", &ans );
if ( ans == 'n' ) {
c->InLineFunction = FALSE;
c->disabled = TRUE;
}
}
}
/**************************************************************************/
/* LOCAL ************** OptBreakCycles ************************/
/**************************************************************************/
/* PURPOSE: BREAK RECURSIVE CYCLES. STARTING AT TRUNK NODE c WITH COLOR */
/* color, FOLLOW ALL CALL PATHS THAT HAVE YET TO BE TRAVERSED. */
/* IF c HAS THE SAME COLOR AS color THEN IT CAN NOT BE INLINED. */
/**************************************************************************/
static void OptBreakCycles( c )
PCALL c;
{
register PCALL r;
if ( c->checked )
return;
if ( c->color == color ) {
c->InLineFunction = FALSE;
c->cycle = TRUE;
return;
}
c->color = color;
for ( r = c->caller; r != NULL; r = r->caller )
OptBreakCycles( r->callee );
c->checked = TRUE;
}
/**************************************************************************/
/* GLOBAL ************** SpliceInGraph ************************/
/**************************************************************************/
/* PURPOSE: REPLACE CALL NODE c WITH THE NODES DEFINING GRAPH g AND MAKE */
/* THE APPROPRIATE EDGE LINKAGES. DEAD EDGES AND NODES ARE */
/* DESTROYED (INCLUDING c). */
/**************************************************************************/
void SpliceInGraph( g, c )
PNODE g;
PNODE c;
{
register PEDGE e;
register PEDGE i;
register PNODE n;
register PNODE sn;
register PEDGE se;
register PEDGE si;
register PEDGE ee;
register PEDGE see;
for ( i = c->imp; i != NULL; i = i->isucc ) /* SO ARGUMENTS USE PORTS */
i->iport--; /* 1 -> N AND NOT 2 -> N + 1 */
for ( e = g->exp; e != NULL; e = se ) { /* LINK ARGUMENTS */
se = e->esucc;
/* IS e A SHORT CIRCUIT: funct f( x:int returns int ) x end function */
if ( e->dst == g ) {
for ( ee = c->exp; ee != NULL; ee = see ) {
see = ee->esucc;
if ( ee->eport != e->iport )
continue;
UnlinkExport( ee );
ee->eport = e->eport;
FindAndLinkToSource( c, ee, NULL_NODE );
}
continue;
}
FindAndLinkToSource( c, e, NULL_NODE );
}
for ( e = c->exp; e != NULL; e = se ) { /* LINK RESULTS */
se = e->esucc;
FindAndLinkToSource( g, e, NULL_NODE );
}
for ( n = g->G_NODES; n != NULL; n = sn ) { /* MOVE GRAPH NODES */
sn = n->nsucc;
LinkNode( c->npred, n );
}
for ( i = g->imp; i != NULL; i = si ) { /* REMOVE GRAPH RESULT EDGES */
si = i->isucc;
/* IF NOT PART OF A SHORT CIRCUIT */
if ( i->src != g )
RemoveDeadEdge( i );
}
c->exp = NULL;
if ( glue )
c->type = IFNoOp; /* FORCE c'S REMOVAL IN glue MODE */
/* OptRemoveDeadNode WILL NOT FREE CALL NODES!!!!!! AS THIS WILL CORRUPT THE */
/* CALL GRAPH. */
OptRemoveDeadNode( c );
}
/**************************************************************************/
/* LOCAL ************** ExpandCalls ************************/
/**************************************************************************/
/* PURPOSE: EXPAND THE CALL NODES FOUND IN THE FUNCTION DEFINED BY CALL */
/* GRAPH TRUNK NODE c. IF THE FUNCTION HAS ALREADY BEEN */
/* EXPANDED, THEN NOTHING IS DONE. BEFORE A CALL IS EXPANDED, */
/* THE CALLEE ITSELF HAS ITS CALLS EXPANDED. IF THE REFERENCE */
/* BEING EXPANDED IS THE CALLEE'S LAST REFERENCE, THEN A COPY OF */
/* CALLEE's GRAPH NEED NOT BE MADE; IT CAN BE DIRECTLY SPLICED */
/* INTO THE CALLER'S GRAPH; THEN THE CALLEE'S GRAPH NODE IS */
/* REMOVED FROM THE IF1 FUNCTION GRAPH LIST. */
/**************************************************************************/
static void ExpandCalls( c )
PCALL c;
{
register PCALL r;
register int size;
if ( c->expanded )
return;
if ( c->caller == NULL )
goto TheEnd;
size = c->graph->size;
for ( r = c->caller; r != NULL; r = r->caller ) {
if ( r->callee->InLineFunction ) {
ExpandCalls( r->callee );
size += r->callee->graph->size;
if ( !inlineall ) {
if ( r->callee->graph->size > MIN_FUNCTION_SIZE ) {
if ( size > SIZE_THRESHOLD && (!inter) ) {
sskip++;
r->skipped = TRUE;
size -= r->callee->graph->size;
continue;
}
}
}
/* ------------------------------------------------------------ */
/* IS THIS CALL NODE NOW DEAD AS A RESULT OF INLINING? */
/* ( Or anything else (like CSE) */
/* ------------------------------------------------------------ */
if ( r->graph->exp == NULL ) {
r->callee->rcnt--; /* No results, so don't bother to inline */
continue;
}
if ( glue || (--(r->callee->rcnt)) != 0 ) {
SpliceInGraph( CopyNode( r->callee->graph ), r->graph );
continue;
}
SpliceInGraph( r->callee->graph, r->graph );
UnlinkGraph( r->callee->graph );
}
}
c->graph->size = size;
TheEnd:
c->expanded = TRUE;
}
/**************************************************************************/
/* LOCAL ************** WriteInlineInfo ************************/
/**************************************************************************/
/* PURPOSE: PRINT INFORMATION GATHERED DURING GRAPH EXPANSION TO stderr. */
/**************************************************************************/
static void WriteInlineInfo()
{
register PCALL c;
register PCALL r;
char *prevc = "prevc";
FPRINTF( infoptr, "\n **** ANNOTATED CALL GRAPH: (skip=%d,MaxSZ=%d) [%s]\n\n",
sskip, SIZE_THRESHOLD,
(IsStamp(MONOLITH))? "MONOLITH" : "INCOMPLETE" );
for ( c = callhead; c != NULL; c = c->callee ) {
if ( IsXGraph( c->graph ) )
FPRINTF( infoptr, " EXPORT" );
FPRINTF( infoptr, " FUNCTION %s(...) sz=%d [mk=%c]:",
(c->graph->funct)? c->graph->funct : c->graph->G_NAME,
c->graph->size,
(c->graph->mark)? c->graph->mark : ' ' );
if ( !c->InLineFunction ) {
if ( c->disabled )
FPRINTF( infoptr, " Not Inlined By Request of User" );
else if ( c->cycle )
FPRINTF( infoptr, " Not Inlined Because Part of Cycle" );
else
FPRINTF( infoptr, " Not Inlined Because Imported" );
}
FPRINTF( infoptr, "\n" );
for ( r = c->caller; r != NULL; r = r->caller ) {
if (strcmp (prevc, r->callee->graph->G_NAME) != 0)
FPRINTF( infoptr, " CALL %s %s\n", r->callee->graph->G_NAME,
(r->skipped == TRUE)? "(SKIPPED)" : "" );
prevc = r->callee->graph->G_NAME;
}
}
}
/**************************************************************************/
/* GLOBAL ************** If1Inline ************************/
/**************************************************************************/
/* PURPOSE: INLINE FUNCTION REFERENCES. ALL FUNCTIONS ARE CONSIDERED */
/* CANDIDATES EXCEPT THOSE SPECIFIED BY THE USER OR SELECTED TO */
/* BREAK CYCLES RESULTING FROM RECURSION. A CALL GRAPH IS BUILT */
/* TO HELP DETECT THE CYCLES. IF inter IS TRUE, THE USER IS */
/* QUERIED FOR FUNCTIONS TO INLINE. */
/**************************************************************************/
void If1Inline()
{
register PNODE f;
register PCALL c;
for ( f = glstop->gsucc; f != NULL; f = f->gsucc )
f->size = GetGraphSize( f );
OptMakeCallGraph();
for ( c = callhead; c != NULL; c = c->callee ) {
color++;
OptBreakCycles( c );
}
/* ------------------------------------------------------------ */
/* Only inline functions as needed. Get requests if inter==1 */
/* ------------------------------------------------------------ */
if ( !NoInlining ) {
ReadInlineRequests();
for ( c = callhead; c != NULL; c = c->callee ) {
ExpandCalls( c );
}
}
if ( RequestInfo(I_Info1,info) )
WriteInlineInfo();
}