Optimierende Compiler Aufbau GIMPLE IR Uberblick uber die GNU - - PowerPoint PPT Presentation

optimierende compiler
SMART_READER_LITE
LIVE PREVIEW

Optimierende Compiler Aufbau GIMPLE IR Uberblick uber die GNU - - PowerPoint PPT Presentation

OC A. Koch Compile-Flu Optimierende Compiler Aufbau GIMPLE IR Uberblick uber die GNU Compiler Collection gcc RTL IR Optimierung Andreas Koch FG Eingebettete Systeme und ihre Anwendungen Informatik, TU Darmstadt


slide-1
SLIDE 1

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Optimierende Compiler

¨ Uberblick ¨ uber die GNU Compiler Collection – gcc Andreas Koch

FG Eingebettete Systeme und ihre Anwendungen Informatik, TU Darmstadt

Sommersemester 2011

1 / 7

slide-2
SLIDE 2

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

GNU Compiler Collection

Nicht mehr nur GNU C Compiler

C, C++, Java, Ada, Objective-C, Objective-C++, Fortran Go, Modula-2, Modula-3, Pascal, PL/I, Mercury, VHDL UPC

Ab hier Material von Prof. Uday Khedker aus Essential Abstractions in GCC ’11 A Workshop on GCC Internals by GCC Resource Center Department of Computer Science & Engineering Indian Institute of Technology, Bombay

http://www.cse.iitb.ac.in/grc/gcc-workshop-11/

2 / 7

slide-3
SLIDE 3

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

GNU Compiler Collection

Nicht mehr nur GNU C Compiler

C, C++, Java, Ada, Objective-C, Objective-C++, Fortran Go, Modula-2, Modula-3, Pascal, PL/I, Mercury, VHDL UPC

Ab hier Material von Prof. Uday Khedker aus Essential Abstractions in GCC ’11 A Workshop on GCC Internals by GCC Resource Center Department of Computer Science & Engineering Indian Institute of Technology, Bombay

http://www.cse.iitb.ac.in/grc/gcc-workshop-11/

2 / 7

slide-4
SLIDE 4

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Compile-Fluß

3 / 7

slide-5
SLIDE 5

Part 4

GCC ≡ The Great Compiler Challenge

slide-6
SLIDE 6

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 19/43

What is GCC?

  • For the GCC developer community: The GNU Compiler Collection
  • For other compiler writers: The Great Compiler Challenge

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-7
SLIDE 7

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-8
SLIDE 8

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-9
SLIDE 9

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-10
SLIDE 10

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp as

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-11
SLIDE 11

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp as ld

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-12
SLIDE 12

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp as ld glibc/newlib

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-13
SLIDE 13

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 20/43

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp as ld glibc/newlib GCC

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-14
SLIDE 14

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Aufbau des GCC

4 / 7

slide-15
SLIDE 15

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 35/43

Why is Understanding GCC Difficult?

Deeper technical reasons

  • GCC is not a compiler but a compiler generation framework

Two distinct gaps that need to be bridged

◮ Input-output of the generation framework

The target specification and the generated compiler

◮ Input-output of the generated compiler

A source program and the generated assembly program

  • GCC generated compiler uses a derivative of the Davidson-Fraser

model of compilation

◮ Early instruction selection ◮ Machine dependent intermediate representation ◮ Simplistic instruction selection and retargatibility mechanism

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-16
SLIDE 16

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 36/43

The Architecture of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-17
SLIDE 17

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 36/43

The Architecture of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Parser Gimplifier Tree SSA Optimizer RTL Generator Optimizer Code Generator Generated Compiler (cc1) Source Program Assembly Program

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-18
SLIDE 18

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 36/43

The Architecture of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Parser Gimplifier Tree SSA Optimizer RTL Generator Optimizer Code Generator Generated Compiler (cc1) Source Program Assembly Program Input Language Target Name Selected Copied Copied Generated Generated

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-19
SLIDE 19

30 June 2011 Overview: GCC ≡ The Great Compiler Challenge 36/43

The Architecture of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Parser Gimplifier Tree SSA Optimizer RTL Generator Optimizer Code Generator Generated Compiler (cc1) Source Program Assembly Program Input Language Target Name Selected Copied Copied Generated Generated Development Time Build Time Use Time

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-20
SLIDE 20

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Zwischendarstellung GIMPLE

5 / 7

slide-21
SLIDE 21

1 July 2011 Graybox Probing-I: Preliminaries 4/43

Basic Transformations in GCC

Tranformation from a language to a different language Target Independent Target Dependent

Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM

GIMPLE → RTL RTL → ASM

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-22
SLIDE 22

1 July 2011 Graybox Probing-I: Preliminaries 4/43

Basic Transformations in GCC

Tranformation from a language to a different language Target Independent Target Dependent

Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM

GIMPLE → RTL RTL → ASM RTL Passes GIMPLE Passes

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-23
SLIDE 23

1 July 2011 Graybox Probing-I: Preliminaries 5/43

Transformation Passes in GCC 4.6.0

  • A total of 207 unique pass names initialized in

${SOURCE}/gcc/passes.c Total number of passes is 241.

◮ Some passes are called multiple times in different contexts

Conditional constant propagation and dead code elimination are called thrice

◮ Some passes are enabled for specific architectures ◮ Some passes have many variations (eg. special cases for loops)

Common subexpression elimination, dead code elimination

  • The pass sequence can be divided broadly in two parts

◮ Passes on GIMPLE ◮ Passes on RTL

  • Some passes are organizational passes to group related passes

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-24
SLIDE 24

1 July 2011 Graybox Probing-I: Preliminaries 6/43

Passes On GIMPLE in GCC 4.6.0

Pass Group Examples Number

  • f passes

Lowering GIMPLE IR, CFG Construction 10 Simple Interprocedural Passes (Non-LTO) Conditional Constant Propagation, Inlining, SSA Construction 38 Regular Interprocedural Passes (LTO) Constant Propagation, Inlining, Pointer Analysis 10 LTO generation passes 02 Other Intraprocedural Optimizations Constant Propagation, Dead Code Elimination, PRE Value Range Propagation, Rename SSA 65 Loop Optimizations Vectorization, Parallelization, Copy Propagation, Dead Code Elimination 28 Generating RTL 01 Total number of passes on GIMPLE 154

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-25
SLIDE 25

1 July 2011 Graybox Probing-I: Preliminaries 7/43

Passes On RTL in GCC 4.6.0

Pass Group Examples Number

  • f passes

Intraprocedural Optimizations CSE, Jump Optimization, Dead Code Elimination, Jump Optimization 27 Loop Optimizations Loop Invariant Movement, Peeling, Unswitching 07 Machine Dependent Optimizations Register Allocation, Instruction Scheduling, Peephole Optimizations 50 Assembly Emission and Finishing 03 Total number of passes on RTL 87

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-26
SLIDE 26

1 July 2011 Graybox Probing-I: Preliminaries 8/43

Finding Out List of Optimizations

Along with the associated flags

  • A complete list of optimizations with a brief description

gcc -c --help=optimizers

  • Optimizations enabled at level 2 (other levels are 0, 1, 3, and s)

gcc -c -O2 --help=optimizers -Q

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-27
SLIDE 27

1 July 2011 Graybox Probing-I: Preliminaries 9/43

Producing the Output of GCC Passes

  • Use the option -fdump-<ir>-<passname>

<ir> could be

◮ tree: Intraprocedural passes on GIMPLE ◮ ipa: Interprocedural passes on GIMPLE ◮ rtl: Intraprocedural passes on RTL

  • Use all in place of <pass> to see all dumps

Example: gcc -fdump-tree-all -fdump-rtl-all test.c

  • Dumping more details:

Suffix raw for tree passes and details or slim for RTL passes Individual passes may have more verbosity options (e.g.

  • fsched-verbose=5)
  • Use -S to stop the compilation with assembly generation
  • Use --verbose-asm to see more detailed assembly dump

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-28
SLIDE 28

1 July 2011 Graybox Probing-I: Preliminaries 10/43

Total Number of Dumps

Optimization Level Number of Dumps Goals Default 47 Fast compilation O1 134 O2 158 O3 168 Os 156 Optimize for space

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-29
SLIDE 29

1 July 2011 Graybox Probing-I: Preliminaries 11/43

Selected Dumps for Our Example Program

GIMPLE dumps (t) 001t.tu 003t.original

004t.gimple

006t.vcg 009t.omplower 010t.lower 012t.eh

013t.cfg

017t.ssa 018t.veclower 019t.inline param1 020t.einline 037t.release ssa 038t.inline param2 044i.whole-program 048i.inline 138t.cplxlower0 143t.optimized 224t.statistics ipa dumps (i)

000i.cgraph

014i.visibility 015i.early local cleanups 044i.whole-program 048i.inline rtl dumps (r)

144r.expand

145r.sibling 147r.initvals 148r.unshare 149r.vregs 150r.into cfglayout 151r.jump 163r.reginfo 183r.outof cfglayout 184r.split1 186r.dfinit 187r.mode sw 188r.asmcons

191r.ira

194r.split2 198r.pro and epilogue 211r.stack 212r.alignments 215r.mach 216r.barriers 220r.shorten 221r.nothrow 222r.final 223r.dfinish

assembly

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-30
SLIDE 30

1 July 2011 Graybox Probing-I: Preliminaries 12/43

Passes for First Level Graybox Probing of GCC

Parser C Source Code AST Gimplifier GIMPLE CFG Generator RTL Generator Reg Allocator pro epilogue generation Pattern Matcher ASM Program CFG RTL expand ira prologue-epilogue

Lowering of abstraction!

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-31
SLIDE 31

Part 2

Examining GIMPLE Dumps

slide-32
SLIDE 32

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 13/43

Gimplifier

  • About GIMPLE

◮ Three-address representation derived from GENERIC

Computation represented as a sequence of basic operations Temporaries introduced to hold intermediate values

◮ Control construct are explicated into conditional jumps

  • Examining GIMPLE Dumps

◮ Examining translation of data accesses ◮ Examining translation of control flow ◮ Examining translation of function calls

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-33
SLIDE 33

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 14/43

GIMPLE: Composite Expressions Involving Local and Global Variables

test.c test.c.004t.gimple int a; int main() { int x = 10; int y = 5; x = a + x * y; y = y - a * x; } x = 10; y = 5; D.1954 = x * y; a.0 = a; x = D.1954 + a.0; a.1 = a; D.1957 = a.1 * x; y = y - D.1957; Global variables are treated as “memory locations” and local variables are treated as “registers”

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-34
SLIDE 34

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 14/43

GIMPLE: Composite Expressions Involving Local and Global Variables

test.c test.c.004t.gimple int a; int main() { int x = 10; int y = 5; x = a + x * y; y = y - a * x; } x = 10; y = 5; D.1954 = x * y; a.0 = a; x = D.1954 + a.0; a.1 = a; D.1957 = a.1 * x; y = y - D.1957; Global variables are treated as “memory locations” and local variables are treated as “registers”

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-35
SLIDE 35

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 14/43

GIMPLE: Composite Expressions Involving Local and Global Variables

test.c test.c.004t.gimple int a; int main() { int x = 10; int y = 5; x = a + x * y; y = y - a * x; } x = 10; y = 5; D.1954 = x * y; a.0 = a; x = D.1954 + a.0; a.1 = a; D.1957 = a.1 * x; y = y - D.1957; Global variables are treated as “memory locations” and local variables are treated as “registers”

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-36
SLIDE 36

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 14/43

GIMPLE: Composite Expressions Involving Local and Global Variables

test.c test.c.004t.gimple int a; int main() { int x = 10; int y = 5; x = a + x * y; y = y - a * x; } x = 10; y = 5; D.1954 = x * y; a.0 = a; x = D.1954 + a.0; a.1 = a; D.1957 = a.1 * x; y = y - D.1957; Global variables are treated as “memory locations” and local variables are treated as “registers”

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-37
SLIDE 37

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 15/43

GIMPLE: 1-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x; } a[2] = 10; D.1952 = a[2]; a[1] = D.1952; D.1953 = a[1]; D.1954 = a[2]; x = D.1953 + D.1954; D.1955 = x + 1; D.1956 = a[1]; D.1957 = D.1955 * D.1956; a[0] = D.1957;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-38
SLIDE 38

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 15/43

GIMPLE: 1-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x; } a[2] = 10; D.1952 = a[2]; a[1] = D.1952; D.1953 = a[1]; D.1954 = a[2]; x = D.1953 + D.1954; D.1955 = x + 1; D.1956 = a[1]; D.1957 = D.1955 * D.1956; a[0] = D.1957;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-39
SLIDE 39

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 15/43

GIMPLE: 1-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x; } a[2] = 10; D.1952 = a[2]; a[1] = D.1952; D.1953 = a[1]; D.1954 = a[2]; x = D.1953 + D.1954; D.1955 = x + 1; D.1956 = a[1]; D.1957 = D.1955 * D.1956; a[0] = D.1957;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-40
SLIDE 40

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 15/43

GIMPLE: 1-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x; } a[2] = 10; D.1952 = a[2]; a[1] = D.1952; D.1953 = a[1]; D.1954 = a[2]; x = D.1953 + D.1954; D.1955 = x + 1; D.1956 = a[1]; D.1957 = D.1955 * D.1956; a[0] = D.1957;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-41
SLIDE 41

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 15/43

GIMPLE: 1-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x; } a[2] = 10; D.1952 = a[2]; a[1] = D.1952; D.1953 = a[1]; D.1954 = a[2]; x = D.1953 + D.1954; D.1955 = x + 1; D.1956 = a[1]; D.1957 = D.1955 * D.1956; a[0] = D.1957;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-42
SLIDE 42

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 16/43

GIMPLE: 2-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3][3], x, y; a[0][0] = 7; a[1][1] = 8; a[2][2] = 9; x = a[0][0] / a[1][1]; y = a[1][1] % a[2][2]; } a[0][0] = 7; a[1][1] = 8; a[2][2] = 9; D.1953 = a[0][0]; D.1954 = a[1][1]; x = D.1953 / D.1954; D.1955 = a[1][1]; D.1956 = a[2][2]; y = D.1955 % D.1956;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-43
SLIDE 43

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 16/43

GIMPLE: 2-D Array Accesses

test.c test.c.004t.gimple int main() { int a[3][3], x, y; a[0][0] = 7; a[1][1] = 8; a[2][2] = 9; x = a[0][0] / a[1][1]; y = a[1][1] % a[2][2]; } a[0][0] = 7; a[1][1] = 8; a[2][2] = 9; D.1953 = a[0][0]; D.1954 = a[1][1]; x = D.1953 / D.1954; D.1955 = a[1][1]; D.1956 = a[2][2]; y = D.1955 % D.1956;

  • No notion of “addressable memory” in GIMPLE.
  • Array reference is a single operation in GIMPLE and is linearized in RTL

during expansion

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-44
SLIDE 44

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 17/43

GIMPLE: Use of Pointers

test.c test.c.004t.gimple int main() { int **a,*b,c; b = &c; a = &b; **a = 10; /* c = 10 */ } ~ main () { int * D.1953; int * * a; int * b; int c; b = &c; a = &b; D.1953 = *a; *D.1953 = 10; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-45
SLIDE 45

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 17/43

GIMPLE: Use of Pointers

test.c test.c.004t.gimple int main() { int **a,*b,c; b = &c; a = &b; **a = 10; /* c = 10 */ } ~ main () { int * D.1953; int * * a; int * b; int c; b = &c; a = &b; D.1953 = *a; *D.1953 = 10; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-46
SLIDE 46

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 18/43

GIMPLE: Use of Structures

test.c test.c.004t.gimple typedef struct address { char *name; } ad; typedef struct student { int roll; ad *ct; } st; int main() { st *s; s = malloc(sizeof(st)); s->roll = 1; s->ct=malloc(sizeof(ad)); s->ct->name = "Mumbai"; } main () { void * D.1957; struct ad * D.1958; struct st * s; extern void * malloc (unsigned int); s = malloc (8); s->roll = 1; D.1957 = malloc (4); s->ct = D.1957; D.1958 = s->ct; D.1958->name = "Mumbai"; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-47
SLIDE 47

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 18/43

GIMPLE: Use of Structures

test.c test.c.004t.gimple typedef struct address { char *name; } ad; typedef struct student { int roll; ad *ct; } st; int main() { st *s; s = malloc(sizeof(st)); s->roll = 1; s->ct=malloc(sizeof(ad)); s->ct->name = "Mumbai"; } main () { void * D.1957; struct ad * D.1958; struct st * s; extern void * malloc (unsigned int); s = malloc (8); s->roll = 1; D.1957 = malloc (4); s->ct = D.1957; D.1958 = s->ct; D.1958->name = "Mumbai"; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-48
SLIDE 48

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 18/43

GIMPLE: Use of Structures

test.c test.c.004t.gimple typedef struct address { char *name; } ad; typedef struct student { int roll; ad *ct; } st; int main() { st *s; s = malloc(sizeof(st)); s->roll = 1; s->ct=malloc(sizeof(ad)); s->ct->name = "Mumbai"; } main () { void * D.1957; struct ad * D.1958; struct st * s; extern void * malloc (unsigned int); s = malloc (8); s->roll = 1; D.1957 = malloc (4); s->ct = D.1957; D.1958 = s->ct; D.1958->name = "Mumbai"; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-49
SLIDE 49

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 18/43

GIMPLE: Use of Structures

test.c test.c.004t.gimple typedef struct address { char *name; } ad; typedef struct student { int roll; ad *ct; } st; int main() { st *s; s = malloc(sizeof(st)); s->roll = 1; s->ct=malloc(sizeof(ad)); s->ct->name = "Mumbai"; } main () { void * D.1957; struct ad * D.1958; struct st * s; extern void * malloc (unsigned int); s = malloc (8); s->roll = 1; D.1957 = malloc (4); s->ct = D.1957; D.1958 = s->ct; D.1958->name = "Mumbai"; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-50
SLIDE 50

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 19/43

GIMPLE: Pointer to Array

test.c test.c.004t.gimple int main() { int *p a, a[3]; p a = &a[0]; *p a = 10; *(p a+1) = 20; *(p a+2) = 30; } main () { int * D.2048; int * D.2049; int * p a; int a[3]; p a = &a[0]; *p a = 10; D.2048 = p a + 4; *D.2048 = 20; D.2049 = p a + 8; *D.2049 = 30; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-51
SLIDE 51

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 19/43

GIMPLE: Pointer to Array

test.c test.c.004t.gimple int main() { int *p a, a[3]; p a = &a[0]; *p a = 10; *(p a+1) = 20; *(p a+2) = 30; } main () { int * D.2048; int * D.2049; int * p a; int a[3]; p a = &a[0]; *p a = 10; D.2048 = p a + 4; *D.2048 = 20; D.2049 = p a + 8; *D.2049 = 30; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-52
SLIDE 52

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 19/43

GIMPLE: Pointer to Array

test.c test.c.004t.gimple int main() { int *p a, a[3]; p a = &a[0]; *p a = 10; *(p a+1) = 20; *(p a+2) = 30; } main () { int * D.2048; int * D.2049; int * p a; int a[3]; p a = &a[0]; *p a = 10; D.2048 = p a + 4; *D.2048 = 20; D.2049 = p a + 8; *D.2049 = 30; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-53
SLIDE 53

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 19/43

GIMPLE: Pointer to Array

test.c test.c.004t.gimple int main() { int *p a, a[3]; p a = &a[0]; *p a = 10; *(p a+1) = 20; *(p a+2) = 30; } main () { int * D.2048; int * D.2049; int * p a; int a[3]; p a = &a[0]; *p a = 10; D.2048 = p a + 4; *D.2048 = 20; D.2049 = p a + 8; *D.2049 = 30; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-54
SLIDE 54

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 20/43

GIMPLE: Translation of Conditional Statements

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4; while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c;

} if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-55
SLIDE 55

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 20/43

GIMPLE: Translation of Conditional Statements

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4; while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c;

} if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-56
SLIDE 56

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 20/43

GIMPLE: Translation of Conditional Statements

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4; while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c;

} if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-57
SLIDE 57

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 21/43

GIMPLE: Translation of Loops

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4;

while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c; } goto <D.1197>; <D.1196>: a = a + 1; <D.1197>: if (a <= 7) goto <D.1196>; else goto <D.1198>; <D.1198>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-58
SLIDE 58

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 21/43

GIMPLE: Translation of Loops

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4;

while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c; } goto <D.1197>; <D.1196>: a = a + 1; <D.1197>: if (a <= 7) goto <D.1196>; else goto <D.1198>; <D.1198>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-59
SLIDE 59

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 21/43

GIMPLE: Translation of Loops

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4;

while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c; } goto <D.1197>; <D.1196>: a = a + 1; <D.1197>: if (a <= 7) goto <D.1196>; else goto <D.1198>; <D.1198>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-60
SLIDE 60

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 21/43

GIMPLE: Translation of Loops

test.c test.c.004t.gimple int main() { int a=2, b=3, c=4;

while (a<=7) { a = a+1; }

if (a<=12) a = a+b+c; } goto <D.1197>; <D.1196>: a = a + 1; <D.1197>: if (a <= 7) goto <D.1196>; else goto <D.1198>; <D.1198>:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-61
SLIDE 61

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 22/43

Control Flow Graph: Textual View

test.c.004t.gimple test.c.013t.cfg if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

<bb 5>: if (a <= 12) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1199 = a + b; a = D.1199 + c; <bb 7>: return;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-62
SLIDE 62

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 22/43

Control Flow Graph: Textual View

test.c.004t.gimple test.c.013t.cfg if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

<bb 5>: if (a <= 12) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1199 = a + b; a = D.1199 + c; <bb 7>: return;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-63
SLIDE 63

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 22/43

Control Flow Graph: Textual View

test.c.004t.gimple test.c.013t.cfg if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

<bb 5>: if (a <= 12) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1199 = a + b; a = D.1199 + c; <bb 7>: return;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-64
SLIDE 64

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 22/43

Control Flow Graph: Textual View

test.c.004t.gimple test.c.013t.cfg if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

<bb 5>: if (a <= 12) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1199 = a + b; a = D.1199 + c; <bb 7>: return;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-65
SLIDE 65

1 July 2011 Graybox Probing-I: Examining GIMPLE Dumps 22/43

Control Flow Graph: Textual View

test.c.004t.gimple test.c.013t.cfg if (a <= 12) goto <D.1200>; else goto <D.1201>; <D.1200>: D.1199 = a + b; a = D.1199 + c; <D.1201>:

<bb 5>: if (a <= 12) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1199 = a + b; a = D.1199 + c; <bb 7>: return;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-66
SLIDE 66

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Zwischendarstellung RTL

6 / 7

slide-67
SLIDE 67

Part 3

Examining RTL Dumps

slide-68
SLIDE 68

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-69
SLIDE 69

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) set mem plus reg 54

  • 4

plus mem plus reg 54

  • 4

1

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-70
SLIDE 70

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) set mem plus reg 54

  • 4

plus mem plus reg 54

  • 4

1

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-71
SLIDE 71

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) set mem plus reg 54

  • 4

plus mem plus reg 54

  • 4

1 a is a local variable allocated on stack

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-72
SLIDE 72

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) set mem plus reg 54

  • 4

plus mem plus reg 54

  • 4

1 a is a local variable allocated on stack

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-73
SLIDE 73

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) parallel clobber reg:CC set . . . . . . side-effect of plus may modify condition code register non-deterministically

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-74
SLIDE 74

1 July 2011 Graybox Probing-I: Examining RTL Dumps 29/43

RTL for i386: Arithmetic Operations (1)

Translation of a =a + 1 Dump file: test.c.144r.expand

(insn 12 11 13 4 (parallel [ ( set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) Output with slim suffix {[r54:SI-0x4]=[r54:SI-0x4]+0x1; clobber flags:CC; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-75
SLIDE 75

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-76
SLIDE 76

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) Current Instruction

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-77
SLIDE 77

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) Previous Instruction

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-78
SLIDE 78

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) Next Instruction

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-79
SLIDE 79

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) Basic Block

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-80
SLIDE 80

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) File name: Line number

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-81
SLIDE 81

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) memory reference that does not trap

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-82
SLIDE 82

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) scalar that is not a part of an aggregate

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-83
SLIDE 83

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) register that holds a pointer

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-84
SLIDE 84

1 July 2011 Graybox Probing-I: Examining RTL Dumps 30/43

Additional Information in RTL

(insn 12 11 13 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:24 -1 (nil)) single integer

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-85
SLIDE 85

1 July 2011 Graybox Probing-I: Examining RTL Dumps 31/43

RTL for i386: Arithmetic Operations (2)

Translation of a = a + 1 when a is a global variable Dump file: test.c.144r.expand

(insn 11 10 12 4 (set (reg:SI 64 [ a.0 ]) (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32])) t.c:26 -1 (nil)) (insn 12 11 13 4 (parallel [ (set (reg:SI 63 [ a.1 ]) (plus:SI (reg:SI 64 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:26 -1 (nil)) (insn 13 12 14 4 (set (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32]) (reg:SI 63 [ a.1 ])) t.c:26 -1 (nil))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-86
SLIDE 86

1 July 2011 Graybox Probing-I: Examining RTL Dumps 31/43

RTL for i386: Arithmetic Operations (2)

Translation of a = a + 1 when a is a global variable Dump file: test.c.144r.expand

(insn 11 10 12 4 (set (reg:SI 64 [ a.0 ]) (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32])) t.c:26 -1 (nil)) (insn 12 11 13 4 (parallel [ (set (reg:SI 63 [ a.1 ]) (plus:SI (reg:SI 64 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:26 -1 (nil)) (insn 13 12 14 4 (set (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32]) (reg:SI 63 [ a.1 ])) t.c:26 -1 (nil)) Load a into reg64

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-87
SLIDE 87

1 July 2011 Graybox Probing-I: Examining RTL Dumps 31/43

RTL for i386: Arithmetic Operations (2)

Translation of a = a + 1 when a is a global variable Dump file: test.c.144r.expand

(insn 11 10 12 4 (set (reg:SI 64 [ a.0 ]) (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32])) t.c:26 -1 (nil)) (insn 12 11 13 4 (parallel [ (set (reg:SI 63 [ a.1 ]) (plus:SI (reg:SI 64 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:26 -1 (nil)) (insn 13 12 14 4 (set (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32]) (reg:SI 63 [ a.1 ])) t.c:26 -1 (nil)) Load a into reg64 reg63 = reg64 + 1

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-88
SLIDE 88

1 July 2011 Graybox Probing-I: Examining RTL Dumps 31/43

RTL for i386: Arithmetic Operations (2)

Translation of a = a + 1 when a is a global variable Dump file: test.c.144r.expand

(insn 11 10 12 4 (set (reg:SI 64 [ a.0 ]) (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32])) t.c:26 -1 (nil)) (insn 12 11 13 4 (parallel [ (set (reg:SI 63 [ a.1 ]) (plus:SI (reg:SI 64 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:26 -1 (nil)) (insn 13 12 14 4 (set (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32]) (reg:SI 63 [ a.1 ])) t.c:26 -1 (nil)) Load a into reg64 reg63 = reg64 + 1 store reg63 into a

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-89
SLIDE 89

1 July 2011 Graybox Probing-I: Examining RTL Dumps 31/43

RTL for i386: Arithmetic Operations (2)

Translation of a = a + 1 when a is a global variable Dump file: test.c.144r.expand

(insn 11 10 12 4 (set (reg:SI 64 [ a.0 ]) (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32])) t.c:26 -1 (nil)) (insn 12 11 13 4 (parallel [ (set (reg:SI 63 [ a.1 ]) (plus:SI (reg:SI 64 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t.c:26 -1 (nil)) (insn 13 12 14 4 (set (mem/c/i:SI (symbol_ref:SI ("a") <var_decl 0xb7d8d000 a>) [0 a+0 S4 A32]) (reg:SI 63 [ a.1 ])) t.c:26 -1 (nil)) Load a into reg64 reg63 = reg64 + 1 store reg63 into a Output with slim suffix r64:SI=[‘a’] {r63:SI=r64:SI+0x1; clobber flags:CC; } [‘a’]=r63:SI

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-90
SLIDE 90

1 July 2011 Graybox Probing-I: Examining RTL Dumps 32/43

RTL for i386: Arithmetic Operations (3)

Translation of a = a + 1 when a is a formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-91
SLIDE 91

1 July 2011 Graybox Probing-I: Examining RTL Dumps 32/43

RTL for i386: Arithmetic Operations (3)

Translation of a = a + 1 when a is a formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Access through argument pointer register instead of frame pointer register

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-92
SLIDE 92

1 July 2011 Graybox Probing-I: Examining RTL Dumps 32/43

RTL for i386: Arithmetic Operations (3)

Translation of a = a + 1 when a is a formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Access through argument pointer register instead of frame pointer register No offset required?

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-93
SLIDE 93

1 July 2011 Graybox Probing-I: Examining RTL Dumps 32/43

RTL for i386: Arithmetic Operations (3)

Translation of a = a + 1 when a is a formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (reg/f:SI 53 virtual-incoming-args) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Access through argument pointer register instead of frame pointer register No offset required? Output with slim suffix {[r53:SI]=[r53:SI]+0x1; clobber flags:CC; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-94
SLIDE 94

1 July 2011 Graybox Probing-I: Examining RTL Dumps 33/43

RTL for i386: Arithmetic Operation (4)

Translation of a = a + 1 when a is the second formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-95
SLIDE 95

1 July 2011 Graybox Probing-I: Examining RTL Dumps 33/43

RTL for i386: Arithmetic Operation (4)

Translation of a = a + 1 when a is the second formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Offset 4 added to the argument pointer register

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-96
SLIDE 96

1 July 2011 Graybox Probing-I: Examining RTL Dumps 33/43

RTL for i386: Arithmetic Operation (4)

Translation of a = a + 1 when a is the second formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Offset 4 added to the argument pointer register When a is the first parameter, its

  • ffset is 0!

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-97
SLIDE 97

1 July 2011 Graybox Probing-I: Examining RTL Dumps 33/43

RTL for i386: Arithmetic Operation (4)

Translation of a = a + 1 when a is the second formal parameter Dump file: test.c.144r.expand

(insn 10 9 11 4 (parallel [ (set (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (plus:SI (mem/c/i:SI (plus:SI (reg/f:SI 53 virtual-incoming-args) (const_int 4 [0x4])) [0 a+0 S4 A32]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) t1.c:25 -1 (nil)) Offset 4 added to the argument pointer register When a is the first parameter, its

  • ffset is 0!

Output with slim suffix {[r53:SI+0x4]=[r53:SI+0x4]+0x1; clobber flags:CC; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-98
SLIDE 98

1 July 2011 Graybox Probing-I: Examining RTL Dumps 34/43

RTL for spim: Arithmetic Operations

Translation of a = a + 1 when a is a local variable Dump file: test.c.144r.expand

(insn 7 6 8 4 (set (reg:SI 39) (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...])) -1 (nil)) (insn 8 7 9 4 test.c:6 (set (reg:SI 40) (plus:SI (reg:SI 39) (const_int 1 [...]))) -1 (nil)) (insn 9 8 10 4 test.c:6 (set (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...]) (reg:SI 40)) test.c:6 -1 (nil)) r39=stack($fp - 4) r40=r39+1 stack($fp - 4)=r40

In spim, a variable is loaded into register to perform any instruction, hence three instructions are generated

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-99
SLIDE 99

1 July 2011 Graybox Probing-I: Examining RTL Dumps 34/43

RTL for spim: Arithmetic Operations

Translation of a = a + 1 when a is a local variable Dump file: test.c.144r.expand

(insn 7 6 8 4 (set (reg:SI 39) (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...])) -1 (nil)) (insn 8 7 9 4 test.c:6 (set (reg:SI 40) (plus:SI (reg:SI 39) (const_int 1 [...]))) -1 (nil)) (insn 9 8 10 4 test.c:6 (set (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...]) (reg:SI 40)) test.c:6 -1 (nil)) r39=stack($fp - 4) r40=r39+1 stack($fp - 4)=r40

In spim, a variable is loaded into register to perform any instruction, hence three instructions are generated

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-100
SLIDE 100

1 July 2011 Graybox Probing-I: Examining RTL Dumps 34/43

RTL for spim: Arithmetic Operations

Translation of a = a + 1 when a is a local variable Dump file: test.c.144r.expand

(insn 7 6 8 4 (set (reg:SI 39) (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...])) -1 (nil)) (insn 8 7 9 4 test.c:6 (set (reg:SI 40) (plus:SI (reg:SI 39) (const_int 1 [...]))) -1 (nil)) (insn 9 8 10 4 test.c:6 (set (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...]) (reg:SI 40)) test.c:6 -1 (nil)) r39=stack($fp - 4) r40=r39+1 stack($fp - 4)=r40

In spim, a variable is loaded into register to perform any instruction, hence three instructions are generated

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-101
SLIDE 101

1 July 2011 Graybox Probing-I: Examining RTL Dumps 34/43

RTL for spim: Arithmetic Operations

Translation of a = a + 1 when a is a local variable Dump file: test.c.144r.expand

(insn 7 6 8 4 (set (reg:SI 39) (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...])) -1 (nil)) (insn 8 7 9 4 test.c:6 (set (reg:SI 40) (plus:SI (reg:SI 39) (const_int 1 [...]))) -1 (nil)) (insn 9 8 10 4 test.c:6 (set (mem/c/i:SI (plus:SI (reg/f:SI 33 virtual-stack-vars) (const_int -4 [...])) [...]) (reg:SI 40)) test.c:6 -1 (nil)) r39=stack($fp - 4) r40=r39+1 stack($fp - 4)=r40

In spim, a variable is loaded into register to perform any instruction, hence three instructions are generated

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-102
SLIDE 102

1 July 2011 Graybox Probing-I: Examining RTL Dumps 35/43

RTL for i386: Control Flow

What does this represent? (jump insn 15 14 16 4 (set (pc) (if then else (lt (reg:CCGC 17 flags) (const int 0 [0x0])) (label ref 12) (pc))) p1.c:6 -1 (nil) (nil)

  • > 12)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-103
SLIDE 103

1 July 2011 Graybox Probing-I: Examining RTL Dumps 35/43

RTL for i386: Control Flow

What does this represent? (jump insn 15 14 16 4 (set (pc) (if then else (lt (reg:CCGC 17 flags) (const int 0 [0x0])) (label ref 12) (pc))) p1.c:6 -1 (nil) (nil)

  • > 12)

pc = r17 <0 ? label(12) : pc

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-104
SLIDE 104

1 July 2011 Graybox Probing-I: Examining RTL Dumps 36/43

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ } Dump file: test.c.144r.expand (insn 8 7 9 (set (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -8 [0xfffffff8])) [0 a+0 S4 A32])) test.c:7 -1 ( (insn 9 8 10 (set (reg:CCGC 17 flags) (compare:CCGC (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -4 [0xfffffffc])) [0 b+0 S4 A32]))) test.c:7 (jump insn 10 9 0 (set (pc) (if then else (le (reg:CCGC 17 flags) (const int 0 [0x0])) (label ref 13) (pc))) test.c:7 -1 (nil)

  • > 13)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-105
SLIDE 105

1 July 2011 Graybox Probing-I: Examining RTL Dumps 36/43

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ } Dump file: test.c.144r.expand (insn 8 7 9 (set (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -8 [0xfffffff8])) [0 a+0 S4 A32])) test.c:7 -1 ( (insn 9 8 10 (set (reg:CCGC 17 flags) (compare:CCGC (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -4 [0xfffffffc])) [0 b+0 S4 A32]))) test.c:7 (jump insn 10 9 0 (set (pc) (if then else (le (reg:CCGC 17 flags) (const int 0 [0x0])) (label ref 13) (pc))) test.c:7 -1 (nil)

  • > 13)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-106
SLIDE 106

1 July 2011 Graybox Probing-I: Examining RTL Dumps 36/43

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ } Dump file: test.c.144r.expand (insn 8 7 9 (set (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -8 [0xfffffff8])) [0 a+0 S4 A32])) test.c:7 -1 ( (insn 9 8 10 (set (reg:CCGC 17 flags) (compare:CCGC (reg:SI 61) (mem/c/i:SI (plus:SI (reg/f:SI 54 virtual-stack-vars) (const int -4 [0xfffffffc])) [0 b+0 S4 A32]))) test.c:7 (jump insn 10 9 0 (set (pc) (if then else (le (reg:CCGC 17 flags) (const int 0 [0x0])) (label ref 13) (pc))) test.c:7 -1 (nil)

  • > 13)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-107
SLIDE 107

1 July 2011 Graybox Probing-I: Examining RTL Dumps 37/43

Observing Register Allocation for i386

test.c test.c.188r.asmcons

(observable dump before register allocation)

int main() { int a=2, b=3; if(a<=12) a = a * b; }

(insn 10 9 11 3 (set (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32 (insn 11 10 12 3 (parallel [ (set (reg:SI 60) (mult:SI (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame (const_int -8 [0xfffffff8])) [0 b+0 (clobber (reg:CC 17 flags)) ]) 262 *mulsi3_1 test.c:5 (nil)) (insn 12 11 22 3 (set (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 (reg:SI 60)) 44 *movsi_internal test.c:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-108
SLIDE 108

1 July 2011 Graybox Probing-I: Examining RTL Dumps 37/43

Observing Register Allocation for i386

test.c test.c.188r.asmcons

(observable dump before register allocation)

int main() { int a=2, b=3; if(a<=12) a = a * b; }

(insn 10 9 11 3 (set (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32 (insn 11 10 12 3 (parallel [ (set (reg:SI 60) (mult:SI (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame (const_int -8 [0xfffffff8])) [0 b+0 (clobber (reg:CC 17 flags)) ]) 262 *mulsi3_1 test.c:5 (nil)) (insn 12 11 22 3 (set (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 (reg:SI 60)) 44 *movsi_internal test.c:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-109
SLIDE 109

1 July 2011 Graybox Probing-I: Examining RTL Dumps 37/43

Observing Register Allocation for i386

test.c test.c.188r.asmcons

(observable dump before register allocation)

int main() { int a=2, b=3; if(a<=12) a = a * b; }

(insn 10 9 11 3 (set (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32 (insn 11 10 12 3 (parallel [ (set (reg:SI 60) (mult:SI (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame (const_int -8 [0xfffffff8])) [0 b+0 (clobber (reg:CC 17 flags)) ]) 262 *mulsi3_1 test.c:5 (nil)) (insn 12 11 22 3 (set (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 (reg:SI 60)) 44 *movsi_internal test.c:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-110
SLIDE 110

1 July 2011 Graybox Probing-I: Examining RTL Dumps 37/43

Observing Register Allocation for i386

test.c test.c.188r.asmcons

(observable dump before register allocation)

int main() { int a=2, b=3; if(a<=12) a = a * b; }

(insn 10 9 11 3 (set (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 A32 (insn 11 10 12 3 (parallel [ (set (reg:SI 60) (mult:SI (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame (const_int -8 [0xfffffff8])) [0 b+0 (clobber (reg:CC 17 flags)) ]) 262 *mulsi3_1 test.c:5 (nil)) (insn 12 11 22 3 (set (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4 [0xfffffffc])) [0 a+0 S4 (reg:SI 60)) 44 *movsi_internal test.c:

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-111
SLIDE 111

1 July 2011 Graybox Probing-I: Examining RTL Dumps 38/43

Observing Register Allocation for i386

test.c.188r.asmcons test.c.188r.ira

(set (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4)))) (set (reg:SI 60) (mult:SI (reg:SI 59) (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -8)) ))) (set (mem/c/i:SI (plus:SI (reg/f:SI 20 frame) (const_int -4))) (reg:SI 60)) (set (reg:SI 0 ax [59]) (mem/c/i:SI (plus:SI (reg/f:SI 6 bp) (const_int -4)))) (set (reg:SI 0 ax [60]) (mult:SI (reg:SI 0 ax [59]) (mem/c/i:SI (plus:SI (reg/f:SI 6 bp) (const_int -8)) ))) (set (mem/c/i:SI (plus:SI (reg/f:SI 6 bp) (const_int -4))) (reg:SI 0 ax [60]))

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-112
SLIDE 112

OC

  • A. Koch

Compile-Fluß Aufbau GIMPLE IR RTL IR Optimierung

Maschinenunabh¨ angige Optimierung

7 / 7

slide-113
SLIDE 113

30 June 2011 Machine Independent Optimizations: Outline 1/29

Outline

  • Example 1

◮ Constant Propagation ◮ Copy Propagation ◮ Dead Code Elimination ◮ Loop unrolling

  • Example 2

◮ Partial Redundancy Elimination ◮ Copy Propagation ◮ Dead Code Elimination

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-114
SLIDE 114

Part 1

First Example Program

slide-115
SLIDE 115

30 June 2011 Machine Independent Optimizations: First Example Program 2/29

Example Program 1

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; }

  • What does this program return?

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-116
SLIDE 116

30 June 2011 Machine Independent Optimizations: First Example Program 2/29

Example Program 1

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; }

  • What does this program return?
  • 12

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-117
SLIDE 117

30 June 2011 Machine Independent Optimizations: First Example Program 2/29

Example Program 1

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; }

  • What does this program return?
  • 12
  • We use this program to illustrate

various shades of the following

  • ptimizations:

Constant propagation, Copy propagation, Loop unrolling, Dead code elimination

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-118
SLIDE 118

30 June 2011 Machine Independent Optimizations: First Example Program 3/29

Compilation Command

$gcc -fdump-tree-all

  • O2 ccp.c

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-119
SLIDE 119

30 June 2011 Machine Independent Optimizations: First Example Program 4/29

Example Program 1

Program ccp.c Control flow graph

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-120
SLIDE 120

30 June 2011 Machine Independent Optimizations: First Example Program 4/29

Example Program 1

Program ccp.c Control flow graph

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; } B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-121
SLIDE 121

30 June 2011 Machine Independent Optimizations: First Example Program 4/29

Example Program 1

Program ccp.c Control flow graph

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; } B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 T

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-122
SLIDE 122

30 June 2011 Machine Independent Optimizations: First Example Program 4/29

Example Program 1

Program ccp.c Control flow graph

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; } B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 T F T

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-123
SLIDE 123

30 June 2011 Machine Independent Optimizations: First Example Program 4/29

Example Program 1

Program ccp.c Control flow graph

int main() { int a, b, c, n; a = 1; b = 2; c = 3; n = c*2; while (a <= n) { a = a+1; } if (a < 12) a = a+b+c; return a; } B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-124
SLIDE 124

30 June 2011 Machine Independent Optimizations: First Example Program 5/29

Control Flow Graph: Pictorial and Textual View

Control flow graph Dump file ccp.c.013t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-125
SLIDE 125

30 June 2011 Machine Independent Optimizations: First Example Program 5/29

Control Flow Graph: Pictorial and Textual View

Control flow graph Dump file ccp.c.013t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

<bb 2>: a = 1; b = 2; c = 3; n = c * 2; goto <bb 4>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-126
SLIDE 126

30 June 2011 Machine Independent Optimizations: First Example Program 5/29

Control Flow Graph: Pictorial and Textual View

Control flow graph Dump file ccp.c.013t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

<bb 3>: a = a + 1; <bb 4>: if (a <= n) goto <bb 3>; else goto <bb 5>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-127
SLIDE 127

30 June 2011 Machine Independent Optimizations: First Example Program 5/29

Control Flow Graph: Pictorial and Textual View

Control flow graph Dump file ccp.c.013t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

<bb 5>: if (a <= 11) goto <bb 6>; else goto <bb 7>; <bb 6>: D.1200 = a + b; a = D.1200 + c;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-128
SLIDE 128

30 June 2011 Machine Independent Optimizations: First Example Program 5/29

Control Flow Graph: Pictorial and Textual View

Control flow graph Dump file ccp.c.013t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

<bb 7>: D.1201 = a; return D.1201;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-129
SLIDE 129

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-130
SLIDE 130

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-131
SLIDE 131

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-132
SLIDE 132

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-133
SLIDE 133

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-134
SLIDE 134

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-135
SLIDE 135

30 June 2011 Machine Independent Optimizations: First Example Program 6/29

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2 B4 if a ≤ n B4 B3 a = a + 1 B3 B5 if a ≤ 11 B6 D.1200 = a + b a = D.1200 + c B6 B7 D.1201 = a return D.1201 T F T F B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-136
SLIDE 136

30 June 2011 Machine Independent Optimizations: First Example Program 7/29

Properties of SSA Form

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

  • A φ function is a multiplexer
  • r a selection function
  • Every use of a variable

corresponds to a unique definition of the variable

  • For every use, the definition

is guaranteed to appear on every path leading to the use SSA construction algorithm is expected to insert as few φ functions as possible to ensure the above properties

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-137
SLIDE 137

30 June 2011 Machine Independent Optimizations: First Example Program 8/29

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.017t.ssa

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-138
SLIDE 138

30 June 2011 Machine Independent Optimizations: First Example Program 8/29

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.017t.ssa

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

<bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = c 5 * 2; goto <bb 4>; <bb 3>: a 7 = a 1 + 1;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-139
SLIDE 139

30 June 2011 Machine Independent Optimizations: First Example Program 8/29

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.017t.ssa

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

<bb 4>: # a 1 = PHI <a 3(2), a 7(3)> if (a 1 <= n 6) goto <bb 3>; else goto <bb 5>; <bb 5>: if (a 1 <= 11) goto <bb 6>; else goto <bb 7>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-140
SLIDE 140

30 June 2011 Machine Independent Optimizations: First Example Program 8/29

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.017t.ssa

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F

<bb 6>: D.1200 8 = a 1 + b 4; a 9 = D.1200 8 + c 5; <bb 7>: # a 2 = PHI <a 1(5), a 9(6)> D.1201 10 = a 2; return D.1201 10;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-141
SLIDE 141

30 June 2011 Machine Independent Optimizations: First Example Program 9/29

A Comparison of CFG and SSA Dumps

Dump file ccp.c.013t.cfg Dump file ccp.c.017t.ssa

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-142
SLIDE 142

30 June 2011 Machine Independent Optimizations: First Example Program 9/29

A Comparison of CFG and SSA Dumps

Dump file ccp.c.013t.cfg Dump file ccp.c.017t.ssa <bb 2>: a = 1; b = 2; c = 3; n = c * 2; goto <bb 4>; <bb 3>: a = a + 1; <bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = c 5 * 2; goto <bb 4>; <bb 3>: a 7 = a 1 + 1;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-143
SLIDE 143

30 June 2011 Machine Independent Optimizations: First Example Program 9/29

A Comparison of CFG and SSA Dumps

Dump file ccp.c.013t.cfg Dump file ccp.c.017t.ssa <bb 4>: if (a <= n) goto <bb 3>; else goto <bb 5>; <bb 5>: if (a <= 11) goto <bb 6>; else goto <bb 7>; <bb 4>: # a 1 = PHI <a 3(2), a 7(3)> if (a 1 <= n 6) goto <bb 3>; else goto <bb 5>; <bb 5>: if (a 1 <= 11) goto <bb 6>; else goto <bb 7>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-144
SLIDE 144

30 June 2011 Machine Independent Optimizations: First Example Program 9/29

A Comparison of CFG and SSA Dumps

Dump file ccp.c.013t.cfg Dump file ccp.c.017t.ssa <bb 6>: D.1200 = a + b; a = D.1200 + c; <bb 7>: D.1201 = a; return D.1201; <bb 6>: D.1200 8 = a 1 + b 4; a 9 = D.1200 8 + c 5; <bb 7>: # a 2 = PHI <a 1(5), a 9(6)> D.1201 10 = a 2; return D.1201 10;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-145
SLIDE 145

30 June 2011 Machine Independent Optimizations: First Example Program 10/29

Copy Renamimg

Input dump: ccp.c.017t.ssa Output dump: ccp.c.022t.copyrename1

<bb 7>: # a 2 = PHI <a 1(5), a 9(6)> D.1201 10 = a 2; return D.1201 10;

<bb 7>: # a 2 = PHI <a 1(5), a 9(6)> a 10 = a 2; return a 10;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-146
SLIDE 146

30 June 2011 Machine Independent Optimizations: First Example Program 11/29

First Level Constant and Copy Propagation

Input dump: ccp.c.022t.copyrename1 Output dump: ccp.c.023t.ccp1

<bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = c 5 * 2; goto <bb 4>; <bb 3>: a 7 = a 1 + 1; <bb 4>: # a 1 = PHI < a 3(2), a 7(3)> if (a 1 <= n 6) goto <bb 3>; else goto <bb 5>; <bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = 6; goto <bb 4>; <bb 3>: a 7 = a 1 + 1; <bb 4>: # a 1 = PHI < 1(2), a 7(3)> if (a 1 <= 6) goto <bb 3>; else goto <bb 5>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-147
SLIDE 147

30 June 2011 Machine Independent Optimizations: First Example Program 11/29

First Level Constant and Copy Propagation

Input dump: ccp.c.022t.copyrename1 Output dump: ccp.c.023t.ccp1

<bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = 6; goto <bb 4>; ... <bb 6>: D.1200 8 = a 1 + b 4; a 9 = D.1200 8 + c 5; <bb 2>: a 3 = 1; b 4 = 2; c 5 = 3; n 6 = 6; goto <bb 4>; ... <bb 6>: D.1200 8 = a 1 + 2; a 9 = D.1200 8 + 3;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-148
SLIDE 148

30 June 2011 Machine Independent Optimizations: First Example Program 12/29

Second Level Copy Propagation

Input dump: ccp.c.023t.ccp1 Output dump: ccp.c.027t.copyprop1

<bb 6>: D.1200 8 = a 1 + 2; a 9 = D.1200 8 + 3; <bb 7>: # a 2 = PHI <a 1(5), a 9(6)> a 10 = a 2; return a 10;

<bb 6>: a 9 = a 1 + 5; <bb 7>: # a 2 = PHI <a 1(5), a 9(6)> return a 2;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-149
SLIDE 149

30 June 2011 Machine Independent Optimizations: First Example Program 13/29

The Result of Copy Propagation and Renaming

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = c 5 ∗ 2 B2 B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6 B7 a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10 T F T F B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-150
SLIDE 150

30 June 2011 Machine Independent Optimizations: First Example Program 14/29

The Result of Copy Propagation and Renaming

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F

  • No uses for variables a 3,

b 4, c 5, and n 6

  • Assignments to these

variables can be deleted

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-151
SLIDE 151

30 June 2011 Machine Independent Optimizations: First Example Program 15/29

Dead Code Elimination Using Control Dependence

Dump file ccp.c.029t.cddce1

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F

<bb 2>: goto <bb 4>; <bb 3>: a 7 = a 1 + 1; <bb 4>: # a 1 = PHI <1(2), a 7(3)> if (a 1 <= 6) goto <bb 3>; else goto <bb 5>; <bb 5>: if (a 1 <= 11) goto <bb 6>; else goto <bb 7>; <bb 6>: a 9 = a 1 + 5; <bb 7>: # a 2 = PHI <a 1(5), a 9(6)> return a 2;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-152
SLIDE 152

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-153
SLIDE 153

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 1

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-154
SLIDE 154

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 2

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-155
SLIDE 155

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 2

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-156
SLIDE 156

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 3

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-157
SLIDE 157

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 3

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-158
SLIDE 158

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 4

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-159
SLIDE 159

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 4

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-160
SLIDE 160

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 5

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-161
SLIDE 161

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 5

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-162
SLIDE 162

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 6

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-163
SLIDE 163

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 6

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-164
SLIDE 164

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B3 a 7 = a 1 + 1 a = 7

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-165
SLIDE 165

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 a = 7

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-166
SLIDE 166

30 June 2011 Machine Independent Optimizations: First Example Program 16/29

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2 B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4 B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F T F B4 a = 2 a = a + 1 a = a + 1 a = a + 1 a = a + 1 a = a + 1 B4 B5 if a 1 ≤ 11 B6 a 9 = a 1 + 5 B6 B7 a 2 = φ (a 1, a 9) return a 2 T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-167
SLIDE 167

30 June 2011 Machine Independent Optimizations: First Example Program 17/29

Complete Unrolling of Inner Loops

Dump file: ccp.c.058t.cunrolli <bb 2>: a_12 = 2; a_14 = a_12 + 1; a_16 = a_14 + 1; a_18 = a_16 + 1; a_20 = a_18 + 1; a_22 = a_20 + 1; if (a_22 <= 11) goto <bb 3>; else goto <bb 4>; <bb 3>: a_9 = a_22 + 5; <bb 4>: # a_2 = PHI <a_22(2), a_9(3)> return a_2;

B2 a 12 = 2 a 14 = a 12 + 1 a 16 = a 14 + 1 a 18 = a 16 + 1 a 20 = a 18 + 1 a 22 = a 20 + 1 if a 22 ≤ 11 B3 a 9 = a 22 + 5 B3 B4 a 2 = φ (a 22, a 9) return a 2 T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-168
SLIDE 168

30 June 2011 Machine Independent Optimizations: First Example Program 18/29

Another Round of Constant Propagation

Input

Dump file: ccp.c.059t.ccp2

B2 a 12 = 2 a 14 = a 12 + 1 a 16 = a 14 + 1 a 18 = a 16 + 1 a 20 = a 18 + 1 a 22 = a 20 + 1 if a 22 ≤ 11 B3 a 9 = a 22 + 5 B3 B4 a 2 = φ (a 22, a 9) return a 2 T F

main () { <bb 2>: return 12; }

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-169
SLIDE 169

Part 2

Second Example Program

slide-170
SLIDE 170

30 June 2011 Machine Independent Optimizations: Second Example Program 19/29

Example Program 2

int f(int b, int c, int n) { int a; do { a = b+c; } while (a <= n); return a; } We use this program to illustrate the following optimizations: Partial Redundancy Elimination, Copy Propagation, Dead Code Elimination

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-171
SLIDE 171

30 June 2011 Machine Independent Optimizations: Second Example Program 20/29

Compilation Command

$gcc -fdump-tree-all

  • O2 -S ccp.c

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-172
SLIDE 172

30 June 2011 Machine Independent Optimizations: Second Example Program 21/29

Example Program 2

loop.c Control Flow Graph int f(int b, int c, int n) { int a; do { a = b+c; } while (a <= n); return a; } a = 1 a = b + c if (a ≤ n) return a

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-173
SLIDE 173

30 June 2011 Machine Independent Optimizations: Second Example Program 22/29

Dump of Input to PRE Pass

Control Flow Graph loop.c.091t.crited a = 1 B2 a = b + c if (a ≤ n) B3 retur B6 retur B5 return a B4 <bb 2>: <bb 3>: a_3 = b_1(D) + c_2(D); if (a_3 <= n_4(D)) goto <bb 5>; else goto <bb 6>; <bb 5>: goto <bb 3>; <bb 6>: <bb 4>: # a_6 = PHI <a_3(6)> return a_6;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-174
SLIDE 174

30 June 2011 Machine Independent Optimizations: Second Example Program 23/29

Input and Output of PRE Pass

loop.c.091t.crited loop.c.092t.pre

<bb 2>: <bb 3>: a_3 = b_1(D) + c_2(D); if (a_3 <= n_4(D)) goto <bb 5>; else goto <bb 6>; <bb 5>: goto <bb 3>; <bb 6>: <bb 4>: # a_6 = PHI <a_3(6)> return a_6; <bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: a_3 = pretmp.2_7; if (a_3 <= n_4(D)) goto <bb 5>; else goto <bb 6>; <bb 5>: goto <bb 3>; <bb 6>: <bb 4>: # a_6 = PHI <a_3(6)> return a_6;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-175
SLIDE 175

30 June 2011 Machine Independent Optimizations: Second Example Program 24/29

Copy Propagation after PRE

loop.c.092t.pre loop.c.097t.copyprop4

<bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: a_3 = pretmp.2_7; if ( a_3 <= n_4(D)) goto <bb 5>; else goto <bb 6>; <bb 5>: goto <bb 3>; <bb 6>: <bb 4>: # a_6 = PHI <a_3(6)> return a_6; <bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: a_3 = pretmp.2_7; if ( n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 5>; <bb 4>: goto <bb 3>; <bb 5>: # a_8 = PHI <pretmp.2_7(3)> a_6 = a_8; return a_8;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-176
SLIDE 176

30 June 2011 Machine Independent Optimizations: Second Example Program 25/29

Dead Code Elimination

loop.c.097t.copyprop4 loop.c.098t.dceloop1

<bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: a_3 = pretmp.2_7; if (n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 5>; <bb 4>: goto <bb 3>; <bb 5>: # a_8 = PHI <pretmp.2_7(3)> a_6 = a_8; return a_8; <bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: if (n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 5>; <bb 4>: goto <bb 3>; <bb 5>: # a_8 = PHI <pretmp.2_7(3)> return a_8;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-177
SLIDE 177

30 June 2011 Machine Independent Optimizations: Second Example Program 26/29

Redundant φ Function Elimination and Copy Propagation

loop.c.098t.dceloop1 loop.c.125t.phicprop2

<bb 2>: pretmp.2_7 = b_1(D) + c_2(D); <bb 3>: if (n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 5>; <bb 4>: goto <bb 3>; <bb 5>: # a_8 = PHI <pretmp.2_7(3)> return a_8; <bb 2>: pretmp.2_7 = c_2(D) + b_1(D); if (n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 3>; <bb 3>: return pretmp.2_7; <bb 4>: goto <bb 4>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-178
SLIDE 178

30 June 2011 Machine Independent Optimizations: Second Example Program 27/29

Final Assembly Program

loop.c.125t.phicprop2 loop.s

<bb 2>: pretmp.2_7 = c_2(D) + b_1(D); if (n_4(D) >= pretmp.2_7) goto <bb 4>; else goto <bb 3>; <bb 3>: return pretmp.2_7; <bb 4>: goto <bb 4>; movl 8(%esp), %eax addl 4(%esp), %eax cmpl %eax, 12(%esp) jge .L2 rep ret .L2: .L3: jmp .L3

Why infinite loop?

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-179
SLIDE 179

30 June 2011 Machine Independent Optimizations: Second Example Program 28/29

Infinite Loop in Example Program 2

int f(int b, int c, int n) { int a; do { a = b+c; } while (a <= n); return a; } The program does not terminate unless a > n

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

slide-180
SLIDE 180

Part 3

Conclusions