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 – p.2
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
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
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
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
Front-ends / back-end GCC Code optimization in GCC – p.3
Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Code optimization in GCC – p.3
Front-ends / back-end GCC Front−ends gcc g++ gcj g77 Machine Description i386 ia64 m68k sparc Code optimization in GCC – p.3
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
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
Exemple: cross-compilation Suppose that I want to generate Sparc code: -target=sparc Code optimization in GCC – p.4
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
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
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
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
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
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
RTL Optimizations An optimization pass optimizes all front-ends. Code optimization in GCC – p.6
RTL Optimizations An optimization pass optimizes all front-ends. Machine dependent optimizations. Code optimization in GCC – p.6
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
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
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
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
Intermediate Representations GCC gcc g++ gcj g77 Translation follows machines specificities Machine description RTL Code optimization in GCC – p.7
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
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
Abstract Syntax Trees Simple linked list for statement nodes. Code optimization in GCC – p.8
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
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
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
AST: example a = (−−b) * 7; x = y+z; Code optimization in GCC – p.9
AST: example a = (−−b) * 7; EXPR_STMT x = y+z; a = (−−b) * 7; Code optimization in GCC – p.9
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
AST: example a = (−−b) * 7; EXPR_STMT EXPR_STMT x = y+z; EXPR_STMT_EXPR (S) MODIFY_EXPR Code optimization in GCC – p.9
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
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
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
Simple: overview SIMPLE’s grammar defines an imperative normal form: Code optimization in GCC – p.10
Simple: overview SIMPLE’s grammar defines an imperative normal form: Reduced number of expressions. Code optimization in GCC – p.10
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
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
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
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
Simple: exemple a = −−b*7; Code optimization in GCC – p.11
Simple: exemple b=b−1; a = −−b*7; a=b*7; Code optimization in GCC – p.11
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
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
Simple: exemple while(i++ && −−k) { A[i]=A[i+3*k]; } Code optimization in GCC – p.12
Recommend
More recommend