understanding and manipulating rtl · 1 july 2013 rtl: examining rtl dumps 6/25 rtl for i386:...

65
Workshop on Essential Abstractions in GCC Understanding and Manipulating RTL GCC Resource Center (www.cse.iitb.ac.in/grc) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay 1 July 2013

Upload: others

Post on 05-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

Workshop on Essential Abstractions in GCC

Understanding and Manipulating RTL

GCC Resource Center

(www.cse.iitb.ac.in/grc)

Department of Computer Science and Engineering,

Indian Institute of Technology, Bombay

1 July 2013

Page 2: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Outline 1/25

Outline

• An overview of RTL

• Examining RTL IR dumps

• An internal view of RTL

• Manipulating RTL IR

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 3: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

Part 1

An Overview of RTL

Page 4: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Overview of RTL 2/25

What is RTL ?

• Register Transfer Language

• Davidson Fraser Model’s key idea

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 5: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Overview of RTL 3/25

Why RTL?

A lot of work in the back-end depends on RTL

• Low level optimizations like loop optimization, loop dependence, commonsubexpression elimination, etc

• Instruction scheduling

• Register Allocation

• Register Movement

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 6: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Overview of RTL 4/25

Why RTL?

For tasks such as those, RTL supports many low level features, like,

• Register classes

• Memory addressing modes

• Word sizes and types

• Compare and branch instructions

• Calling Conventions

• Bitfield operations

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 7: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Overview of RTL 5/25

The Dual Role of RTL

• For specifying machine descriptions

Machine description constructs:

◮ define insn, define expand, match operand

• For representing program during compilation

IR constructs

◮ insn, jump insn, code label, note, barrier

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 8: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Overview of RTL 5/25

The Dual Role of RTL

• For specifying machine descriptions

Machine description constructs:

◮ define insn, define expand, match operand

• For representing program during compilation

IR constructs

◮ insn, jump insn, code label, note, barrier

This lecture focusses on RTL as an IR

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 9: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

Part 2

Examining RTL Dumps

Page 10: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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

Page 11: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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

Page 12: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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

Page 13: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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 variableallocated on stack

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 14: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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 variableallocated on stack

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 15: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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 maymodify condition code register

non-deterministically

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 16: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 6/25

RTL for i386: Arithmetic Operations (1)

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

(insn 12 11 13 4 (parallel [

( set (mem/c:SI

(plus:SI

(reg/f:SI 54 virtual-stack-vars)

(const_int -4 [0xfffffffc])) [0 a+0 S4 A32])

(plus:SI

(mem/c: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

Page 17: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 18: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 19: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 20: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 21: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 22: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 23: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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 referencethat does not trap

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 24: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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 apart of an aggregate

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 25: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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 thatholds a pointer

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 26: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 7/25

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

Page 27: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 8/25

RTL for i386: Arithmetic Operations (2)

Translation of a = a+ 1 when a is a global variableDump file: test.c.150r.expand

(insn 11 10 12 4 (set

(reg:SI 64 [ a.0 ])

(mem/c: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: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

Page 28: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 8/25

RTL for i386: Arithmetic Operations (2)

Translation of a = a+ 1 when a is a global variableDump file: test.c.150r.expand

(insn 11 10 12 4 (set

(reg:SI 64 [ a.0 ])

(mem/c: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: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

Page 29: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 8/25

RTL for i386: Arithmetic Operations (2)

Translation of a = a+ 1 when a is a global variableDump file: test.c.150r.expand

(insn 11 10 12 4 (set

(reg:SI 64 [ a.0 ])

(mem/c: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: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

Page 30: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 8/25

RTL for i386: Arithmetic Operations (2)

Translation of a = a+ 1 when a is a global variableDump file: test.c.150r.expand

(insn 11 10 12 4 (set

(reg:SI 64 [ a.0 ])

(mem/c: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: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

Page 31: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 8/25

RTL for i386: Arithmetic Operations (2)

Translation of a = a+ 1 when a is a global variableDump file: test.c.150r.expand

(insn 11 10 12 4 (set

(reg:SI 64 [ a.0 ])

(mem/c: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: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

Page 32: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 9/25

RTL for i386: Arithmetic Operations (3)

Translation of a = a+ 1 when a is a formal parameterDump file: test.c.150r.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

Page 33: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 9/25

RTL for i386: Arithmetic Operations (3)

Translation of a = a+ 1 when a is a formal parameterDump file: test.c.150r.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 argumentpointer register instead offrame pointer register

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 34: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 9/25

RTL for i386: Arithmetic Operations (3)

Translation of a = a+ 1 when a is a formal parameterDump file: test.c.150r.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 argumentpointer register instead offrame pointer register

No offset required?

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 35: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 9/25

RTL for i386: Arithmetic Operations (3)

Translation of a = a+ 1 when a is a formal parameterDump file: test.c.150r.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 argumentpointer register instead offrame 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

Page 36: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 10/25

RTL for i386: Arithmetic Operation (4)

Translation of a = a+ 1 when a is the second formal parameterDump file: test.c.150r.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

Page 37: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 10/25

RTL for i386: Arithmetic Operation (4)

Translation of a = a+ 1 when a is the second formal parameterDump file: test.c.150r.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 argumentpointer register

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 38: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 10/25

RTL for i386: Arithmetic Operation (4)

Translation of a = a+ 1 when a is the second formal parameterDump file: test.c.150r.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 argumentpointer register

When a is the first parameter, itsoffset is 0!

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 39: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 10/25

RTL for i386: Arithmetic Operation (4)

Translation of a = a+ 1 when a is the second formal parameterDump file: test.c.150r.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 argumentpointer register

When a is the first parameter, itsoffset 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

Page 40: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 11/25

RTL for spim: Arithmetic Operations

Translation of a = a+ 1 when a is a local variableDump file: test.c.150r.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+1stack($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

Page 41: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 11/25

RTL for spim: Arithmetic Operations

Translation of a = a+ 1 when a is a local variableDump file: test.c.150r.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+1stack($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

Page 42: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 11/25

RTL for spim: Arithmetic Operations

Translation of a = a+ 1 when a is a local variableDump file: test.c.150r.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+1stack($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

Page 43: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 11/25

RTL for spim: Arithmetic Operations

Translation of a = a+ 1 when a is a local variableDump file: test.c.150r.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+1stack($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

Page 44: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 12/25

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

Page 45: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 12/25

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

Page 46: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 13/25

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ }Dump file: test.c.150r.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 (nil))

(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 -1 (nil))

(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

Page 47: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 13/25

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ }Dump file: test.c.150r.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 (nil))

(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 -1 (nil))

(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

Page 48: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Examining RTL Dumps 13/25

RTL for i386: Control Flow

Translation of if (a > b) { /* something */ }Dump file: test.c.150r.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 (nil))

(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 -1 (nil))

(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

Page 49: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

Part 3

An Internal View of RTL

Page 50: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 14/25

RTL Objects

• Types of RTL Objects

◮ Expressions◮ Integers◮ Wide Integers◮ Strings◮ Vectors

• Internal representation of RTL Expressions

◮ Expressions in RTX are represented as trees◮ A pointer to the C data structure for RTL is called rtx

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 51: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 15/25

RTX Codes

RTL Expressions are classified into RTX codes :

• Expression codes are names defined in rtl.def

• RTX codes are C enumeration constants

• Expression codes and their meanings are machine-independent

• Extract the code of a RTX with the macro GET CODE(x)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 52: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 16/25

RTL Classes

RTL expressions are divided into few classes, like:

• RTX UNARY : NEG, NOT, ABS

• RTX BIN ARITH : MINUS, DIV

• RTX COMM ARITH : PLUS, MULT

• RTX OBJ : REG, MEM, SYMBOL REF

• RTX COMPARE : GE, LT

• RTX TERNARY : IF THEN ELSE

• RTX INSN : INSN, JUMP INSN, CALL INSN

• RTX EXTRA : SET, USE

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 53: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 17/25

RTX CodesThe RTX codes are defined in rtl.def using cpp macro call DEF RTL EXPR,like :

• DEF RTL EXPR(INSN, "insn", "iuuBieie", RTX INSN)

• DEF RTL EXPR(SET, "set", "ee", RTX EXTRA)

• DEF RTL EXPR(PLUS, "plus", "ee", RTX COMM ARITH)

• DEF RTL EXPR(IF THEN ELSE, "if then else", "eee",

RTX TERNARY)

The operands of the macro are :

• Internal name of the rtx used in C source. It’s a tag in enumeration enum

rtx code

• name of the rtx in the external ASCII format

• Format string of the rtx, defined in rtx format[]

• Class of the rtx

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 54: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 18/25

RTX Formats

DEF RTL EXPR(INSN, "insn", "iuuBieie", RTX INSN)

• i : Integer

• u : Integer representing a pointer

• B : Pointer to basic block

• e : Expression

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 55: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 19/25

RTL statements

• RTL statements are instances of type rtx

• RTL insns contain embedded links

• Types of RTL insns :

◮ INSN : Normal non-jumping instruction◮ JUMP INSN : Conditional and unconditional jumps◮ CALL INSN : Function calls◮ CODE LABEL: Target label for JUMP INSN◮ BARRIER : End of control Flow◮ NOTE : Debugging information

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 56: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 20/25

Basic RTL APIs

• XEXP,XINT,XWINT,XSTR

◮ Example: XINT(x,2) accesses the 2nd operand of rtx x as aninteger

◮ Example: XEXP(x,2) accesses the same operand as an expression

• Any operand can be accessed as any type of RTX object

◮ So operand accessor to be chosen based on the format string of thecontaining expression

• Special macros are available for Vector operands

◮ XVEC(exp,idx) : Access the vector-pointer which is operandnumber idx in exp

◮ XVECLEN (exp, idx ) : Access the length (number of elements) inthe vector which is in operand number idx in exp. This value is an int

◮ XVECEXP (exp, idx, eltnum ) : Access element number“eltnum” in the vector which is in operand number idx in exp. Thisvalue is an RTX

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 57: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: An Internal View of RTL 21/25

RTL Insns

• A function’s code is a doubly linked chain of INSN objects

• Insns are rtxs with special code

• Each insn contains atleast 3 extra fields :

◮ Unique id of the insn , accessed by INSN UID(i)◮ PREV INSN(i) accesses the chain pointer to the INSN

preceeding i◮ NEXT INSN(i) accesses the chain pointer to the INSN

succeeding i

• The first insn is accessed by using get insns()

• The last insn is accessed by using get last insn()

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 58: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

Part 4

Manipulating RTL IR

Page 59: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 22/25

Adding an RTL Pass

Similar to adding GIMPLE intraporcedural pass except for the following

• Use the data structure struct rtl opt pass

• Replace the first field GIMPLE PASS by RTL PASS

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 60: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 23/25

Sample Demo Program

Problem statement : Counting the number of SET objects in a basic block byadding a new RTL pass

• Add your new pass after pass expand

• new rtl pass main is the main function of the pass

• Iterate through different instructions in the doubly linked list ofinstructions and for each expression, call eval rtx(insn) for thatexpression which recurse in the expression tree to find the set statements

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 61: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 24/25

Sample Demo Program

int new rtl pass main(void){

basic block bb;

rtx last,insn,opd1,opd2;

int bbno,code,type;

count = 0;

for (insn=get insns(), last=get last insn(),

last=NEXT INSN(last); insn!=last; insn=NEXT INSN(insn))

{ int is insn;

is insn = INSN P (insn);

if(flag dump new rtl pass)

print rtl single(dump file,insn);

code = GET CODE(insn);

if(code==NOTE){ ... }

if(is insn)

{ rtx subexp = XEXP(insn,5);

eval rtx(subexp);

}

}

...

}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 62: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 24/25

Sample Demo Program

int new rtl pass main(void){

basic block bb;

rtx last,insn,opd1,opd2;

int bbno,code,type;

count = 0;

for (insn=get insns(), last=get last insn(),

last=NEXT INSN(last); insn!=last; insn=NEXT INSN(insn))

{ int is insn;

is insn = INSN P (insn);

if(flag dump new rtl pass)

print rtl single(dump file,insn);

code = GET CODE(insn);

if(code==NOTE){ ... }

if(is insn)

{ rtx subexp = XEXP(insn,5);

eval rtx(subexp);

}

}

...

}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 63: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 25/25

Sample Demo Program

void eval rtx(rtx exp)

{ rtx temp;

int veclen,i,

int rt code = GET CODE(exp);

switch(rt code)

{ case SET:

if(flag dump new rtl pass){

fprintf(dump file,"\nSet statement %d : \t",count+1);

print rtl single(dump file,exp);}

count++; break;

case PARALLEL:

veclen = XVECLEN(exp, 0);

for(i = 0; i < veclen; i++)

{ temp = XVECEXP(exp, 0, i);

eval rtx(temp);

}

break;

default: break;

}

}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 64: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 25/25

Sample Demo Program

void eval rtx(rtx exp)

{ rtx temp;

int veclen,i,

int rt code = GET CODE(exp);

switch(rt code)

{ case SET:

if(flag dump new rtl pass){

fprintf(dump file,"\nSet statement %d : \t",count+1);

print rtl single(dump file,exp);}

count++; break;

case PARALLEL:

veclen = XVECLEN(exp, 0);

for(i = 0; i < veclen; i++)

{ temp = XVECEXP(exp, 0, i);

eval rtx(temp);

}

break;

default: break;

}

}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Page 65: Understanding and Manipulating RTL · 1 July 2013 RTL: Examining RTL Dumps 6/25 RTL for i386: Arithmetic Operations (1) Translation of a=a+1 Dump file: test.c.150r.expand (insn 12

1 July 2013 RTL: Manipulating RTL IR 25/25

Sample Demo Program

void eval rtx(rtx exp)

{ rtx temp;

int veclen,i,

int rt code = GET CODE(exp);

switch(rt code)

{ case SET:

if(flag dump new rtl pass){

fprintf(dump file,"\nSet statement %d : \t",count+1);

print rtl single(dump file,exp);}

count++; break;

case PARALLEL:

veclen = XVECLEN(exp, 0);

for(i = 0; i < veclen; i++)

{ temp = XVECEXP(exp, 0, i);

eval rtx(temp);

}

break;

default: break;

}

}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay