The Retargetability Model of GCC Uday Khedker - - PowerPoint PPT Presentation

the retargetability model of gcc
SMART_READER_LITE
LIVE PREVIEW

The Retargetability Model of GCC Uday Khedker - - PowerPoint PPT Presentation

Tutorial on Essential Abstractions in GCC The Retargetability Model of GCC Uday Khedker (www.cse.iitb.ac.in/grc) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay April 2011 EA-GCC,


slide-1
SLIDE 1

Tutorial on Essential Abstractions in GCC

The Retargetability Model of GCC

Uday Khedker

(www.cse.iitb.ac.in/grc) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

April 2011

slide-2
SLIDE 2

EA-GCC, Chamonix Retargetability Model: Outline 1/16

Outline

  • A Recap
  • Generating the code generators
  • Using the generator code generators

Uday Khedker GRC, IIT Bombay

slide-3
SLIDE 3

Part 1

A Recap

slide-4
SLIDE 4

EA-GCC, Chamonix Retargetability Model: A Recap 2/16

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer Expander Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time Uday Khedker GRC, IIT Bombay

slide-5
SLIDE 5

EA-GCC, Chamonix Retargetability Model: A Recap 2/16

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer Expander Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time

GIMPLE → IR-RTL + IR-RTL → ASM

Uday Khedker GRC, IIT Bombay

slide-6
SLIDE 6

EA-GCC, Chamonix Retargetability Model: A Recap 2/16

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer Expander Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time

GIMPLE → PN + PN → IR-RTL + IR-RTL → ASM GIMPLE → IR-RTL + IR-RTL → ASM

Uday Khedker GRC, IIT Bombay

slide-7
SLIDE 7

EA-GCC, Chamonix Retargetability Model: A Recap 2/16

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer Expander Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time

GIMPLE → PN + PN → IR-RTL + IR-RTL → ASM GIMPLE → IR-RTL + IR-RTL → ASM

Uday Khedker GRC, IIT Bombay

slide-8
SLIDE 8

EA-GCC, Chamonix Retargetability Model: A Recap 2/16

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer Expander Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time

GIMPLE → PN + PN → IR-RTL + IR-RTL → ASM GIMPLE → IR-RTL + IR-RTL → ASM

Uday Khedker GRC, IIT Bombay

slide-9
SLIDE 9

EA-GCC, Chamonix Retargetability Model: A Recap 3/16

Plugin Structure in cc1

toplev main front end pass manager pass 1 pass 2

. . .

pass expand

. . .

pass n double arrow represents control flow whereas single arrow represents pointer or index code for pass 2 code for pass 1 code for pass n expander code

  • ptab table

langhook . . . code for language 1 code for language 2 code for language n langhook . . . code for language 1 code for language 2 code for language n

Uday Khedker GRC, IIT Bombay

slide-10
SLIDE 10

EA-GCC, Chamonix Retargetability Model: A Recap 3/16

Plugin Structure in cc1

toplev main front end pass manager pass 1 pass 2

. . .

pass expand

. . .

pass n code for pass 2 code for pass 1 code for pass n expander code

  • ptab table

langhook . . . code for language 1 code for language 2 code for language n langhook . . . code for language 1 code for language 2 code for language n insn data generated code for machine 1 MD 1 MD 2 MD n

Uday Khedker GRC, IIT Bombay

slide-11
SLIDE 11

EA-GCC, Chamonix Retargetability Model: A Recap 3/16

Plugin Structure in cc1

toplev main front end pass manager pass 1 pass 2

. . .

pass expand

. . .

pass n code for pass 2 code for pass 1 code for pass n expander code

  • ptab table

langhook . . . code for language 1 code for language 2 code for language n langhook . . . code for language 1 code for language 2 code for language n insn data generated code for machine 2 MD 1 MD 2 MD n

Uday Khedker GRC, IIT Bombay

slide-12
SLIDE 12

EA-GCC, Chamonix Retargetability Model: A Recap 3/16

Plugin Structure in cc1

toplev main front end pass manager pass 1 pass 2

. . .

pass expand

. . .

pass n code for pass 2 code for pass 1 code for pass n expander code

  • ptab table

langhook . . . code for language 1 code for language 2 code for language n langhook . . . code for language 1 code for language 2 code for language n insn data generated code for machine n MD 1 MD 2 MD n

Uday Khedker GRC, IIT Bombay

slide-13
SLIDE 13

EA-GCC, Chamonix Retargetability Model: A Recap 4/16

What is “Generated”?

  • Info about instructions supported by chosen target, e.g.

◮ Listing data structures (e.g. instruction pattern lists) ◮ Indexing data structures, since diff. targets give diff. lists.

  • C functions that generate RTL internal representation
  • Any useful “attributes”, e.g.

◮ Semantic groupings: arithmetic, logical, I/O etc. ◮ Processor unit usage groups for pipeline utilisation

Uday Khedker GRC, IIT Bombay

slide-14
SLIDE 14

EA-GCC, Chamonix Retargetability Model: A Recap 5/16

Information supplied by the MD

  • The target instructions – as ASM strings
  • A description of the semantics of each
  • A description of the features of each like

◮ Data size limits ◮ One of the operands must be a register ◮ Implicit operands ◮ Register restrictions

Information supplied in define insn as The target instruction ASM string A description of it’s semantics RTL Template Operand data size limits predicates Register restrictions constraints

Uday Khedker GRC, IIT Bombay

slide-15
SLIDE 15

Part 2

Generating the Code Generators

slide-16
SLIDE 16

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 6/16

How GCC uses target specific RTL as IR

GIMPLE ASSIGN (set (<dest>) (<src>))

Uday Khedker GRC, IIT Bombay

slide-17
SLIDE 17

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 6/16

How GCC uses target specific RTL as IR

GIMPLE ASSIGN (set (<dest>) (<src>)) "movsi" Standard Pattern Name

Uday Khedker GRC, IIT Bombay

slide-18
SLIDE 18

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 6/16

How GCC uses target specific RTL as IR

GIMPLE ASSIGN (set (<dest>) (<src>)) "movsi" Standard Pattern Name Separate CGF code and MD GIMPLE ASSIGN "movsi" "movsi" (set (<dest>) (<src>))

Uday Khedker GRC, IIT Bombay

slide-19
SLIDE 19

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 6/16

How GCC uses target specific RTL as IR

GIMPLE ASSIGN (set (<dest>) (<src>)) "movsi" Standard Pattern Name Separate CGF code and MD GIMPLE ASSIGN "movsi" "movsi" (set (<dest>) (<src>)) Implement GIMPLE ASSIGN "movsi" "movsi" (set (<dest>) (<src>)) Unnecessary in CGF; hard code Implement in MD

Uday Khedker GRC, IIT Bombay

slide-20
SLIDE 20

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 7/16

Retargetability ⇒ Multiple MD vs. One CGF!

GIMPLE ASSIGN "movsi" CGF "movsi",(set (<dest>) (<src>)) MD 1 "movsi",(set (<dest>) (<src>)) MD n

CGF needs:

An interface immune to MD authoring variations

Uday Khedker GRC, IIT Bombay

slide-21
SLIDE 21

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 7/16

Retargetability ⇒ Multiple MD vs. One CGF!

GIMPLE ASSIGN "movsi" CGF "movsi",(set (<dest>) (<src>)) MD 1 "movsi",(set (<dest>) (<src>)) MD n How ?

CGF needs:

An interface immune to MD authoring variations

Uday Khedker GRC, IIT Bombay

slide-22
SLIDE 22

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 7/16

Retargetability ⇒ Multiple MD vs. One CGF!

GIMPLE ASSIGN "movsi" CGF "movsi",(set (<dest>) (<src>)) MD 1 "movsi",(set (<dest>) (<src>)) MD n How ? Basic Approach: Tabulate GIMPLE – RTL struct optab [] struct insn data []

CGF needs:

An interface immune to MD authoring variations

Uday Khedker GRC, IIT Bombay

slide-23
SLIDE 23

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 8/16

MD Information Data Structures Two principal data structures

  • struct optab – Interface to CGF
  • struct insn data – All information about a pattern

◮ Array of each pattern read ◮ Some patterns are SPNs ◮ Each pattern is accessed using the generated index

Supporting data structures

  • enum insn code: Index of patterns available in the given MD

Note

Data structures are named in the CGF, but populated at build time. Generating target specific code = populating these data structures.

Uday Khedker GRC, IIT Bombay

slide-24
SLIDE 24

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c

Uday Khedker GRC, IIT Bombay

slide-25
SLIDE 25

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c

Uday Khedker GRC, IIT Bombay

slide-26
SLIDE 26

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR Nothing SF insn code CODE FOR Nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c

Uday Khedker GRC, IIT Bombay

slide-27
SLIDE 27

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR Nothing SF insn code CODE FOR Nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

Uday Khedker GRC, IIT Bombay

slide-28
SLIDE 28

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR Nothing SF insn code CODE FOR Nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

$BUILD/gcc/insn-codes.h

CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing

Uday Khedker GRC, IIT Bombay

slide-29
SLIDE 29

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR Nothing SF insn code CODE FOR Nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

$BUILD/gcc/insn-codes.h

CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing

$BUILD/gcc/insn-opinit.c

...

Uday Khedker GRC, IIT Bombay

slide-30
SLIDE 30

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR Nothing SF insn code CODE FOR Nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

$BUILD/gcc/insn-codes.h

CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing

$BUILD/gcc/insn-opinit.c

...

Uday Khedker GRC, IIT Bombay

slide-31
SLIDE 31

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR movsi SF insn code CODE FOR nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

$BUILD/gcc/insn-codes.h

CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing

$BUILD/gcc/insn-opinit.c

...

Runtime initialization of data structure using func- tion set optab handler

Uday Khedker GRC, IIT Bombay

slide-32
SLIDE 32

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 9/16

Assume movsi is supported but movsf is not supported. . .

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR movsi SF insn code CODE FOR nothing

$(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c

insn data . . . . . . 1280 "movsi" . . . gen movsi . . .

$BUILD/gcc/insn-codes.h

CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing

$BUILD/gcc/insn-opinit.c

...

Runtime initialization of data structure using func- tion set optab handler

Uday Khedker GRC, IIT Bombay

slide-33
SLIDE 33

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 10/16

GCC Generation Phase – Revisited

Generator Generated Information Description from MD genopinit insn-opinit.c void init all optabs (void); Operations Table Initialiser gencodes insn-codes.h enum insn code = {... CODE FOR movsi = 1280, ...} Index of patterns genooutput insn-output.c struct insn data [CODE].genfun = /* fn ptr */ All insn data e.g. gen function genemit insn-emit.c rtx gen rtx movsi (/* args */) {/* body */} RTL emission functions

Uday Khedker GRC, IIT Bombay

slide-34
SLIDE 34

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 11/16

Explicit Calls to gen<SPN> functions

  • In some cases, an entry is not made in insn data table for some

SPNs.

  • gen functions for such SPNs are explicitly called.
  • These are mostly related to

◮ Function calls ◮ Setting up of activation records ◮ Non-local jumps ◮ etc. (i.e. deeper study is required on this aspect)

Uday Khedker GRC, IIT Bombay

slide-35
SLIDE 35

EA-GCC, Chamonix Retargetability Model: Generating the Code Generators 12/16

Handling C Code in define expand

(define_expand "movsi" [( set (op0) (op1))] "" "{ /* C CODE OF DEFINE EXPAND */ }") rtx gen_movsi (rtx operand0, rtx operand1) { ... { /* C CODE OF DEFINE EXPAND */ } emit_insn (gen_rtx_ SET (VOIDmode, operand0, operand1) ... }

Uday Khedker GRC, IIT Bombay

slide-36
SLIDE 36

Part 3

Using the Code Generators

slide-37
SLIDE 37

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 13/16

cc1 Control Flow: GIMPLE to RTL Expansion (pass expand)

gimple_expand_cfg expand_gimple_basic_block(bb) expand_gimple_cond(stmt) expand_gimple_stmt(stmt) expand_gimple_stmt_1 (stmt) expand_expr_real_2 expand_expr /* Operands */ expand_expr_real

  • ptab_for_tree_code

expand_binop /* Now we have rtx for operands */ expand_binop_directly /* The plugin for a machine */ code=optab_handler(binoptab,mode)->insn_code; GEN_FCN emit_insn

Uday Khedker GRC, IIT Bombay

slide-38
SLIDE 38

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); }

Uday Khedker GRC, IIT Bombay

slide-39
SLIDE 39

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } Seek index

Uday Khedker GRC, IIT Bombay

slide-40
SLIDE 40

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } insn-codes.h enum insn code = {... CODE FOR movsi = 1280, ...}

Uday Khedker GRC, IIT Bombay

slide-41
SLIDE 41

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } insn-codes.h enum insn code = {... CODE FOR movsi = 1280, ...} Got index into optab

Uday Khedker GRC, IIT Bombay

slide-42
SLIDE 42

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } #define GENFCN(code) insn data[code].genfun Use icode (= 1280)

Uday Khedker GRC, IIT Bombay

slide-43
SLIDE 43

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } #define GENFCN(code) insn data[code].genfun insn-output.c insn data[1280].genfun = gen movsi

Uday Khedker GRC, IIT Bombay

slide-44
SLIDE 44

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 14/16

RTL Generation

expand_binop_directly ... /* Various cases of expansion */ /* One case: integer mode move */ icode = mov_optab->handler[SImode].insn_code if (icode != CODE_FOR_nothing) { ... /* preparatory code */ emit_insn (GEN_FCN(icode)(dest,src)); } #define GENFCN(code) insn data[code].genfun Execute: gen movsi (dest,src)

Uday Khedker GRC, IIT Bombay

slide-45
SLIDE 45

EA-GCC, Chamonix Retargetability Model: Using the Code Generators 15/16

RTL to ASM Conversion

  • Simple pattern matching of IR RTLs and the patterns present in all

named, un-named, standard, non-standard patterns defined using define insn.

  • A DFA (deterministic finite automaton) is constructed and the first

match is used.

Uday Khedker GRC, IIT Bombay

slide-46
SLIDE 46

Part 4

Conclusions

slide-47
SLIDE 47

EA-GCC, Chamonix Retargetability Model: Conclusions 16/16

A Comparison with Davidson Fraser Model

  • Retargetability in Davidson Fraser Model

◮ Manually rewriting Expander and patter matcher ◮ Expected to be simple for machines of 1984 Era

  • Retargetability in GCC

Automatic construction possible by separating machine specific details in carefully designed data structures

◮ List insns as they appear in the chosen MD ◮ Index them ◮ Supply index to the CGF

Uday Khedker GRC, IIT Bombay