code optimization in gcc
play

Code optimization in GCC S ebastian Pop Universit e Louis Pasteur - PowerPoint PPT Presentation

Code optimization in GCC S ebastian Pop Universit e Louis Pasteur Strasbourg FRANCE Code optimization in GCC p.1 Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Code optimization in GCC


  1. Code optimization in GCC S´ ebastian Pop Universit´ e Louis Pasteur Strasbourg FRANCE Code optimization in GCC – p.1

  2. Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Code optimization in GCC – p.2

  3. Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Generates code for 43 different architectures: i386, ia64, m68k, sparc, . . . Code optimization in GCC – p.2

  4. Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Generates code for 43 different architectures: i386, ia64, m68k, sparc, . . . Main compiler in GNU world Code optimization in GCC – p.2

  5. Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Generates code for 43 different architectures: i386, ia64, m68k, sparc, . . . Main compiler in GNU world Apple’s system compiler. Code optimization in GCC – p.2

  6. Introduction GCC : GNU Compiler Collection C, C++, Java, Ada, Frotran, Mercury, . . . Generates code for 43 different architectures: i386, ia64, m68k, sparc, . . . Main compiler in GNU world Apple’s system compiler. Industrial compiler. Code optimization in GCC – p.2

  7. Front-ends / back-end GCC Code optimization in GCC – p.3

  8. Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Code optimization in GCC – p.3

  9. Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Machine Description i386 ia64 m68k sparc Code optimization in GCC – p.3

  10. Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Machine Description i386 ia64 m68k sparc RTL Back−end Code optimization in GCC – p.3

  11. Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Machine Description i386 ia64 m68k sparc RTL Back−end GAS Assembler Code optimization in GCC – p.3

  12. Exemple: cross-compilation Suppose that I want to generate Sparc code: -target=sparc Code optimization in GCC – p.4

  13. Exemple: cross-compilation Suppose that I want to generate Sparc code: -target=sparc I build GCC on my laptop: -build=i586 Code optimization in GCC – p.4

  14. Exemple: cross-compilation Suppose that I want to generate Sparc code: -target=sparc I build GCC on my laptop: -build=i586 and I run the compiler on my laptop: -host=i586 Code optimization in GCC – p.4

  15. Exemple: cross-compilation Suppose that I want to generate Sparc code: -target=sparc I build GCC on my laptop: -build=i586 and I run the compiler on my laptop: -host=i586 ../gcc/configure -target=sparc -build=i586 -host=i586 Code optimization in GCC – p.4

  16. Exemple: cross-compilation GCC Front−ends gcc g++ gcj g77 Machine Description i386 ia64 m68k sparc RTL Back−end GAS Assembler Code optimization in GCC – p.5

  17. Exemple: cross-compilation GCC 1. Select SPARC machine Front−ends gcc g++ gcj g77 description Machine Description sparc SPARC specific RTL Back−end Code optimization in GCC – p.5

  18. Exemple: cross-compilation GCC 1. Select SPARC machine Front−ends gcc g++ gcj g77 description Machine Description sparc 2. Compile SPARC specific RTL Back−end SPARC assembler code Code optimization in GCC – p.5

  19. RTL Optimizations An optimization pass optimizes all front-ends. Code optimization in GCC – p.6

  20. RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Code optimization in GCC – p.6

  21. RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Types and memory structures after lowering to RTL contain less information. Memory accesses are under their canonical form: <start adress + offset> Code optimization in GCC – p.6

  22. RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Types and memory structures after lowering to RTL contain less information. Memory accesses are under their canonical form: <start adress + offset> Idea: we’d like to have Code optimization in GCC – p.6

  23. RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Types and memory structures after lowering to RTL contain less information. Memory accesses are under their canonical form: <start adress + offset> Idea: we’d like to have architecture independent optimizations. Code optimization in GCC – p.6

  24. RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Types and memory structures after lowering to RTL contain less information. Memory accesses are under their canonical form: <start adress + offset> Idea: we’d like to have architecture independent optimizations. on high level representations. Code optimization in GCC – p.6

  25. Intermediate Representations GCC gcc g++ gcj g77 Translation follows machines specificities Machine description RTL Code optimization in GCC – p.7

  26. Intermediate Representations GCC gcc g++ gcj g77 Translation Progressive transition from AST to RTL Mid−RTL Architecture independent IR Machine description RTL Code optimization in GCC – p.7

  27. Intermediate Representations GCC gcc g++ gcj g77 Simplify Imperative Normal Form Simple Language independent representation Translation Progressive transition from AST to RTL Mid−RTL Architecture independent IR Machine description RTL Code optimization in GCC – p.7

  28. Abstract Syntax Trees Simple linked list for statement nodes. Code optimization in GCC – p.8

  29. Abstract Syntax Trees Simple linked list for statement nodes. Manipulation of nodes through a macro interface: TREE_CHAIN, TREE_OPERAND, TREE_CODE, ... Code optimization in GCC – p.8

  30. Abstract Syntax Trees Simple linked list for statement nodes. Manipulation of nodes through a macro interface: TREE_CHAIN, TREE_OPERAND, TREE_CODE, ... Data structures hidden. Code optimization in GCC – p.8

  31. Abstract Syntax Trees Simple linked list for statement nodes. Manipulation of nodes through a macro interface: TREE_CHAIN, TREE_OPERAND, TREE_CODE, ... Data structures hidden. AST nodes are typed: allows tree-checking during development. Code optimization in GCC – p.8

  32. AST: example a = (−−b) * 7; x = y+z; Code optimization in GCC – p.9

  33. AST: example a = (−−b) * 7; EXPR_STMT x = y+z; a = (−−b) * 7; Code optimization in GCC – p.9

  34. AST: example TREE_CHAIN (S) a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; a = (−−b) * 7; x = y+z; Code optimization in GCC – p.9

  35. AST: example a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; EXPR_STMT_EXPR (S) MODIFY_EXPR Code optimization in GCC – p.9

  36. AST: example a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; MODIFY_EXPR TREE_OPERAND (M, 0) TREE_OPERAND (M, 1) VAR_DECL MULT_EXPR Code optimization in GCC – p.9

  37. AST: example a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; MODIFY_EXPR VAR_DECL MULT_EXPR DECL_NAME (V) a 7 IDENTIFIER_NODE PREDECREMENT_EXPR INTEGER_CST Code optimization in GCC – p.9

  38. AST: example a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; MODIFY_EXPR VAR_DECL MULT_EXPR a 7 IDENTIFIER_NODE PREDECREMENT_EXPR INTEGER_CST VAR_DECL b 1 IDENTIFIER_NODE INTEGER_CST Code optimization in GCC – p.9

  39. Simple: overview SIMPLE’s grammar defines an imperative normal form: Code optimization in GCC – p.10

  40. Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Code optimization in GCC – p.10

  41. Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Reduced number of control structures. Code optimization in GCC – p.10

  42. Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Reduced number of control structures. SIMPLE AST has a regular structure. Code optimization in GCC – p.10

  43. Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Reduced number of control structures. SIMPLE AST has a regular structure. Systematic AST analysis is possible. Code optimization in GCC – p.10

  44. Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Reduced number of control structures. SIMPLE AST has a regular structure. Systematic AST analysis is possible. Common intermediate representation for all front ends. Code optimization in GCC – p.10

  45. Simple: exemple a = −−b*7; Code optimization in GCC – p.11

  46. Simple: exemple b=b−1; a = −−b*7; a=b*7; Code optimization in GCC – p.11

  47. Simple: exemple b=b−1; a = −−b*7; a=b*7; if (i++ && −−k) { j=f(i+3*k); } Code optimization in GCC – p.11

  48. Simple: exemple b=b−1; a = −−b*7; a=b*7; if (i++ && −−k) if (i) { { j=f(i+3*k); k=k−1; if(k) } { i=i+1; T1=3*k; T2=i+T1; j=f(T2); } else i=i+1; } else i=i+1; Code optimization in GCC – p.11

  49. Simple: exemple while(i++ && −−k) { A[i]=A[i+3*k]; } Code optimization in GCC – p.12

Recommend


More recommend