1 July 2012 GIMPLE and RTL: Outline 1/39 Outline Workshop on Essential Abstractions in GCC Manipulating GIMPLE and RTL IRs • An Overview of GIMPLE GCC Resource Center • Using GIMPLE API in GCC-4.6.0 (www.cse.iitb.ac.in/grc) • Adding a GIMPLE Pass to GCC Department of Computer Science and Engineering, • An Internal View of RTL Indian Institute of Technology, Bombay • Manipulating RTL IR 1 July 2012 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 2/39 GIMPLE: A Recap • Language independent three address code representation Part 1 ◮ Computation represented as a sequence of basic operations ◮ Temporaries introduced to hold intermediate values An Overview of GIMPLE • Control construct explicated into conditional and unconditional jumps Essential Abstractions in GCC GCC Resource Center, IIT Bombay
1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 3/39 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 4/39 Motivation Behind GIMPLE Why Not Abstract Syntax Trees for Optimization? • ASTs contain detailed function information but are not suitable for optimization because • Previously, the only common IR was RTL (Register Transfer Language) ◮ Lack of a common representation across languages • Drawbacks of RTL for performing high-level optimizations ◮ No single AST shared by all front-ends ◮ Low-level IR, more suitable for machine dependent optimizations ◮ So each language would have to have a different implementation of (e.g., peephole optimization) the same optimizations ◮ High level information is difficult to extract from RTL (e.g. array ◮ Difficult to maintain and upgrade so many optimization frameworks ◮ Structural Complexity references, data types etc.) ◮ Introduces stack too soon, even if later optimizations do not require it ◮ Lots of complexity due to the syntactic constructs of each language ◮ Hierarchical structure and not linear structure Control flow explication is required Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 5/39 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 6/39 Need for a New IR What is GENERIC? What? • Language independent IR for a complete function in the form of trees • Earlier versions of GCC would build up trees for a single statement,and • Obtained by removing language specific constructs from ASTs then lower them to RTL before moving on to the next statement • All tree codes defined in $(SOURCE)/gcc/tree.def • For higher level optimizations, entire function needs to be represented in trees in a language-independent way. Why? • Result of this effort - GENERIC and GIMPLE • Each language frontend can have its own AST • Once parsing is complete they must emit GENERIC Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay
1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 7/39 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 8/39 What is GIMPLE ? GIMPLE Goals • GIMPLE is influenced by SIMPLE IR of McCat compiler The Goals of GIMPLE are • But GIMPLE is not same as SIMPLE (GIMPLE supports GOTO) • Lower control flow Sequenced statements + conditional and unconditional jumps • It is a simplified subset of GENERIC • Simplify expressions ◮ 3 address representation Typically one operator and at most two operands ◮ Control flow lowering • Simplify scope ◮ Cleanups and simplification, restricted grammar Move local scope to block begin, including temporaries • Benefit : Optimizations become easier Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 9/39 1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 10/39 Tuple Based GIMPLE Representation Observing Internal Form of GIMPLE test.c.004t.gimple with compilation option test.c.004t.gimple • Earlier implementation of GIMPLE used trees as internal data structure with compilation option -fdump-tree-all-raw • Tree data structure was much more general than was required for three -fdump-tree-all address statements x = 10; gimple_assign <integer_cst, x, 10, NULL> • Now a three address statement is implemented as a tuple y = 5; gimple_assign <integer_cst, y, 5, NULL> D.1954 = x * y; gimple_assign <mult_expr, D.1954, x, y> • These tuples contain the following information a.0 = a; gimple_assign <var_decl, a.0, a, NULL> ◮ Type of the statement x = D.1954 + a.0; gimple_assign <plus_expr, x, D.1954, a.0> ◮ Result a.1 = a; gimple_assign <var_decl, a.1, a, NULL> ◮ Operator D.1957 = a.1 * x; gimple_assign <mult_expr, D.1957, a.1, x> ◮ Operands y = y - D.1957; gimple_assign <minus_expr, y, y, D.1957> The result and operands are still represented using trees Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay
1 July 2012 GIMPLE and RTL: An Overview of GIMPLE 11/39 Observing Internal Form of GIMPLE test.c.004t.gimple test.c.004t.gimple with compilation option with compilation option -fdump-tree-all-raw -fdump-tree-all if (a < c) Part 2 goto <D.1953>; gimple_cond <lt_expr, a,c,<D.1953>, <D.1954>> else gimple_label <<D.1953>> Manipulating GIMPLE goto <D.1954>; gimple_assign <plus_expr, a, b, c> <D.1953>: gimple_goto <<D.1955>> a = b + c; gimple_label <<D.1954>> goto <D.1955>; gimple_assign <minus_expr, a, b, c> <D.1954>: gimple_label <<D.1955>> a = b - c; <D.1955>: Essential Abstractions in GCC GCC Resource Center, IIT Bombay 1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 Iterating Over GIMPLE Statements Iterating Over GIMPLE Statements • A basic block contains a doubly linked-list of GIMPLE statements • The statements are represented as GIMPLE tuples, and the operands are represented by tree data structure • Processing of statements can be done through iterators • A basic block contains a doubly linked-list of GIMPLE statements • The statements are represented as GIMPLE tuples, and the operands are basic_block bb; represented by tree data structure gimple_stmt_iterator gsi; • Processing of statements can be done through iterators FOR_EACH_BB (bb) { % for ( gsi=gsi_start_bb (bb); !gsi_end_p (gsi); % gsi_next (&gsi)) find_pointer_assignmentsgsi_stmt (gsi)); } Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay
1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 Iterating Over GIMPLE Statements Iterating Over GIMPLE Statements • A basic block contains a doubly linked-list of GIMPLE statements • A basic block contains a doubly linked-list of GIMPLE statements • The statements are represented as GIMPLE tuples, and the operands are • The statements are represented as GIMPLE tuples, and the operands are represented by tree data structure represented by tree data structure • Processing of statements can be done through iterators • Processing of statements can be done through iterators basic_block bb; basic_block bb; gimple_stmt_iterator gsi; gimple_stmt_iterator gsi; FOR_EACH_BB (bb) FOR_EACH_BB (bb) { % { % for ( gsi=gsi_start_bb (bb); !gsi_end_p (gsi); % for ( gsi=gsi_start_bb (bb); !gsi_end_p (gsi); % gsi_next (&gsi)) gsi_next (&gsi)) find_pointer_assignmentsgsi_stmt (gsi)); find_pointer_assignmentsgsi_stmt (gsi)); } } Basic block iterator GIMPLE statement iterator Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay 1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 1 July 2012 GIMPLE and RTL: Manipulating GIMPLE 12/39 Iterating Over GIMPLE Statements Iterating Over GIMPLE Statements • A basic block contains a doubly linked-list of GIMPLE statements • A basic block contains a doubly linked-list of GIMPLE statements • The statements are represented as GIMPLE tuples, and the operands are • The statements are represented as GIMPLE tuples, and the operands are represented by tree data structure represented by tree data structure • Processing of statements can be done through iterators • Processing of statements can be done through iterators basic_block bb; basic_block bb; gimple_stmt_iterator gsi; gimple_stmt_iterator gsi; FOR_EACH_BB (bb) FOR_EACH_BB (bb) { % { % for ( gsi=gsi_start_bb (bb); !gsi_end_p (gsi); % for ( gsi=gsi_start_bb (bb); !gsi_end_p (gsi); % gsi_next (&gsi)) gsi_next (&gsi)) find_pointer_assignmentsgsi_stmt (gsi)); find_pointer_assignmentsgsi_stmt (gsi)); } } Get the first statement of bb True if end reached Essential Abstractions in GCC GCC Resource Center, IIT Bombay Essential Abstractions in GCC GCC Resource Center, IIT Bombay
Recommend
More recommend