CSc 520
Principles of Programming Languages
:

Christian Collberg

Department of Computer Science

University of Arizona

1 Inline Expansion

2 Inline Expansion...

3 Inline Expansion...

4 Inline Expansion...

5 Inline Expansion...

6 Inline Expansion...

7 Inline Expansion...

8 Inline Expansion...

9 Algorithm

  1. Build the call graph:
    1. Create an empty directed graph 2#2.
    2. Add a node for each routine and for the main program.
    3. If procedure 1#1 calls procedure 3#3 then insert a directed edge 4#4.
CallGraph1

10 Algorithm...

11 Algorithm...

  1. Pick routines to inline. Possible heuristics:
    1. Discard recursive routines (Perform a topological sort of the call graph. Cycles indicate recursion.) or just inline them one or two levels deep.
    2. Select routines with indegree=1.
    3. Select calls to small routines in inner loops.
    4. Rely on user-defined INLINE pragmas.
    5. Use profiling information.
    6. Consider effects on caching, paging, register pressure, total code size, ...
    7. ...

12 Algorithm...

  1. FOR each call 5#5 in 3#3 to inline procedure 6#6, in reverse topological order of the call graph DO
    1. Replace the call 5#5 with 1#1's body.
    2. Replace references to call-by-reference formal 7#7 with a reference to the corresponding actual parameter 8#8.
    3. For each call-by-value formal parameter 7#7 create a new local 9#9. Insert code to copy the call-by-value actual 8#8 into 9#9. Replace references to the call-by-value formal 7#7 with a reference to its copy 9#9.
    4. For each of 1#1's local variables 10#10 create a new local 11#11 in 3#3. Replace references to local variable 10#10 with a reference to 11#11.

13 Topological Order

Example:
12#12

Topological Order:

CallGraph2

14 Topological Order...

15 Inlining Example (Original)


14#14

16 Inlining Example (Expanded)


15#15

17 Inlining Example (Optimized)


16#16

18 Inlining Methods

19 Inlining Methods...

MethodCall

20 Inlining Methods -- Example


19#19

21 Type Hierarchy Analysis

22 Type Hierarchy Analysis...


25#25

23 C Preprocessor

24 C Preprocessor...

Some popular ones are:

#include 26#26file27#27
The preprocessor searches the system directories (e.g. /usr/include) for a file named file and replaces this line with its contents.
#define word rest-of-line
Replaces word with rest-of-line thoroughout the rest of the source file.
#if expression 28#28 #else 28#28 #endif
If expression is non-zero, the lines up to the #else are included, otherwise the lines between the #else and the #endif are included.

25 C Preprocessor Examples

   #define ARRAY_SIZE 1000
   char str[ARRAY_SIZE];

   #define MAX(x,y) ((x) > (y) ? (x) : (y))
   int max_num;
   max_num = MAX(i,j);

   #define DEBUG 1

26 C Preprocessor Examples...

   #define ARRAY_SIZE 1000
   #ifdef DEBUG
      printf("got here!")
   #endif

   #if defined(DEBUG)
      #define Debug(x) printf(x)
   #else
      #define Debug(x)
   #endif

   Debug(("got here!"));

27 Macros vs. Inlined Procedures

28 Macros vs. Inlined Procedures...


29#29

29 Readings and References



Christian S. Collberg
2005-04-22