Introduction to Machine Descriptions Uday Khedker - - PowerPoint PPT Presentation

introduction to machine descriptions
SMART_READER_LITE
LIVE PREVIEW

Introduction to Machine Descriptions Uday Khedker - - PowerPoint PPT Presentation

Tutorial on Essential Abstractions in GCC Introduction to Machine Descriptions 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-1
SLIDE 1

Tutorial on Essential Abstractions in GCC

Introduction to Machine Descriptions

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 MD Intro: Outline 1/21

Outline

  • Influences on GCC Machine Descriptions
  • Organization of GCC Machine Descriptions
  • Machine description constructs
  • The essence of retargetability in GCC

Uday Khedker GRC, IIT Bombay

slide-3
SLIDE 3

EA-GCC, Chamonix MD Intro: Outline 2/21

Examples of Influences on the Machine Descriptions

Machine Description Source Language

  • INT TYPE SIZE
  • Activation Record

<target>.h GCC Architecture

  • Generation of nop
  • tree covers for

instruction selection

  • define predicate

<target>.h Build System Host System hwint.h Target System

  • Instruction Set

Architecture

  • Assembly and

executable formats

  • <target>.md

<target>.h <target>.h

  • ther headers

Uday Khedker GRC, IIT Bombay

slide-4
SLIDE 4

Part 1

Organization of GCC MD

slide-5
SLIDE 5

EA-GCC, Chamonix MD Intro: Organization of GCC MD 3/21

Contents of GCC MD

  • Processor instructions useful to GCC
  • Processor characteristics useful to GCC
  • Target ASM syntax
  • Target specific optimizations as IR-RTL → IR-RTL transformations

(GCC code performs the transformation computations, MD supplies their target patterns)

◮ Peephole optimizations ◮ Transformations for enabling scheduling

Uday Khedker GRC, IIT Bombay

slide-6
SLIDE 6

EA-GCC, Chamonix MD Intro: Organization of GCC MD 4/21

Syntactic Entities in GCC MD

  • Necessary Specifications

◮ Processor instructions useful to GCC ◮ One GIMPLE → One IR-RTL

define insn

◮ One GIMPLE → More than one IR-RTL

define expand

◮ Processor characteristics useful to GCC

define cpu unit

◮ Target ASM syntax

part of define insn

◮ IR-RTL → IR-RTL transformations

define split

◮ Target Specific Optimizations

define peephole2

  • Programming Conveniences

(eg. define insn and split, define constants, define cond exec, define automaton )

Uday Khedker GRC, IIT Bombay

slide-7
SLIDE 7

EA-GCC, Chamonix MD Intro: Organization of GCC MD 5/21

File Organization of GCC MD The GCC MD comprises of

  • <target>.h: A set of C macros that describe

◮ HLL properties: e.g. INT TYPE SIZE to h/w bits ◮ Activation record structure ◮ Target Register (sub)sets, and characteristics

(lists of read-only regs, dedicated regs, etc.)

◮ System Software details: formats of assembler, executable etc.

  • <target>.md: Target instructions described using MD constructs.

(Our main interest!)

  • <target>.c: Optional, but usually required.

C functions that implement target specific code (e.g. target specific activation layout).

Uday Khedker GRC, IIT Bombay

slide-8
SLIDE 8

EA-GCC, Chamonix MD Intro: Organization of GCC MD 5/21

File Organization of GCC MD The GCC MD comprises of

  • <target>.h: A set of C macros that describe

◮ HLL properties: e.g. INT TYPE SIZE to h/w bits ◮ Activation record structure ◮ Target Register (sub)sets, and characteristics

(lists of read-only regs, dedicated regs, etc.)

◮ System Software details: formats of assembler, executable etc.

  • <target>.md: Target instructions described using MD constructs.

(Our main interest!)

  • <target>.c: Optional, but usually required.

C functions that implement target specific code (e.g. target specific activation layout).

Uday Khedker GRC, IIT Bombay

slide-9
SLIDE 9

Part 2

Essential Constructs in Machine Descriptions

slide-10
SLIDE 10

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 6/21

The GCC Phase Sequence

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

Uday Khedker GRC, IIT Bombay

slide-11
SLIDE 11

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 6/21

The GCC Phase Sequence

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

MD Info Required

Uday Khedker GRC, IIT Bombay

slide-12
SLIDE 12

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 7/21

The GCC Phase Sequence

Observe that

  • RTL is a target specific IR
  • GIMPLE → non strict RTL → strict RTL.
  • SPN: “(Semantic) Glue” between GIMPLE and RTL

◮ operator match + coarse operand match, and ◮ refine the operand match

  • Finally: Strict RTL ⇔ Unique target ASM string

Consider generating RTL expressions of GIMPLE nodes

  • Two constructs available: define insn and define expand

Uday Khedker GRC, IIT Bombay

slide-13
SLIDE 13

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 8/21

Running Example

Consider a data move operation

  • reads data from source location, and
  • writes it to the destination location.
  • GIMPLE node: GIMPLE ASSIGN
  • SPN: “movsi”

Some possible combinations are:

  • Reg ← Reg : Register move
  • Reg ← Mem : Load
  • Reg ← Const : Load immediate
  • Mem ← Reg : Store
  • Mem ← Mem : Illegal instruction

Uday Khedker GRC, IIT Bombay

slide-14
SLIDE 14

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 9/21

Specifying Target Instruction Semantics

(define_insn "movsi"

[(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k") )]

"" /*

C boolean expression, if required */

"li %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-15
SLIDE 15

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 9/21

Specifying Target Instruction Semantics

(define_insn "movsi"

[(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k") )]

"" /*

C boolean expression, if required */

"li %0, %1" ) Define instruction pattern Standard Pattern Name RTL Expression (RTX): Semantics of target instruction target asm inst. = Concrete syntax for RTX

Uday Khedker GRC, IIT Bombay

slide-16
SLIDE 16

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 9/21

Specifying Target Instruction Semantics

(define_insn "movsi"

[(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k") )]

"" /*

C boolean expression, if required */

"li %0, %1" ) RTL operator MD constructs Constraints Mode Predicates

Uday Khedker GRC, IIT Bombay

slide-17
SLIDE 17

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 10/21

Instruction Specification and Translation

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

(define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-18
SLIDE 18

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 10/21

Instruction Specification and Translation

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

  • GIMPLE: target independent
  • RTL: target dependent
  • Need: associate the semantics

⇒GCC Solution: Standard Pattern Names

(define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-19
SLIDE 19

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 10/21

Instruction Specification and Translation

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

  • GIMPLE: target independent
  • RTL: target dependent
  • Need: associate the semantics

⇒GCC Solution: Standard Pattern Names GIMPLE ASSIGN

RTL Template ASM (define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-20
SLIDE 20

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 11/21

General Move Instruction

(define_insn "maybe_spn_like_movsi" [(set (match_operand:SI 0 "general_operand" "") (match_operand:SI 1 "general_operand" ""))] "" "mov %0, %1" )

  • This define insn can generate data movement patterns of all

combinations

  • Even Mem → Mem is possible.
  • We need a mechanism to generate more restricted data movement

RTX instances!

Uday Khedker GRC, IIT Bombay

slide-21
SLIDE 21

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 12/21

The define expand Construct

(define_expand "movsi" [(set (match_operand:SI 0 "nonimmediate_operand" "") (match_operand:SI 1 "general_operand" "") )] "" { if (GET_CODE (operands[0]) == MEM && GET_CODE (operands[1]) != REG) if (can_create_pseudo_p())

  • perands[1] = force_reg (SImode, operands[1]);

} )

Uday Khedker GRC, IIT Bombay

slide-22
SLIDE 22

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 13/21

Relationship Between <target>.md, <target>.c, and <target>.h Files

Example:

  • Register class constraints are used in <target>.md file
  • Register class is defined in <target>.h file
  • Checks for register class are implemented in <target>.c file

Uday Khedker GRC, IIT Bombay

slide-23
SLIDE 23

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 14/21

Register Class Constraints in <target>.md File

;; Here z is the constraint character defined in ;; REG_CLASS_FROM_LETTER_P ;; The register $zero is used here. (define_insn "IITB_move_zero" [(set (match_operand:SI 0 "nonimmediate_operand" "=r,m") (match_operand:SI 1 "zero_register_operand" " z ,z") )] "" "@ move \t%0,%1 sw \t%1, %m0" ) The Register Class letter code

Uday Khedker GRC, IIT Bombay

slide-24
SLIDE 24

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 15/21

Register Class specification in <target>.h File

/* From spim.h */ #define REG_CLASS_FROM_LETTER_P \ reg_class_from_letter enum reg_class \ { \ NO_REGS, ZERO_REGS , \ CALLER_SAVED_REGS, CALLEE_SAVED_REGS, \ BASE_REGS, GENERAL_REGS, \ ALL_REGS, LIM_REG_CLASSES \ }; #define REG_CLASS_CONTENTS \ {0x00000000, 0x00000003 , 0xff00ffff, 0x00ff0000, \ 0xf0000000, 0x0cfffff3, 0xffffffff} The Register Classes The Register Class Enumeration

Uday Khedker GRC, IIT Bombay

slide-25
SLIDE 25

EA-GCC, Chamonix MD Intro: Essential Constructs in Machine Descriptions 16/21

The <target>.c File

enum reg_class reg_class_from_letter (char ch) { switch(ch) { case ’b’:return BASE_REGS; case ’x’:return CALLEE_SAVED_REGS; case ’y’:return CALLER_SAVED_REGS; case ’z’:return ZERO_REGS; } return NO_REGS; } Get the enumeration from the Register class letter

Uday Khedker GRC, IIT Bombay

slide-26
SLIDE 26

Part 3

The Essence of Retargetability

slide-27
SLIDE 27

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 17/21

Instruction Specification and Translation: A Recap

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

(define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-28
SLIDE 28

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 17/21

Instruction Specification and Translation: A Recap

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

  • GIMPLE: target independent
  • RTL: target dependent
  • Need: associate the semantics

⇒GCC Solution: Standard Pattern Names

(define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-29
SLIDE 29

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 17/21

Instruction Specification and Translation: A Recap

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM

  • GIMPLE: target independent
  • RTL: target dependent
  • Need: associate the semantics

⇒GCC Solution: Standard Pattern Names GIMPLE ASSIGN

RTL Template ASM (define_insn "movsi" [(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "mov %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-30
SLIDE 30

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 18/21

Translation Sequence in GCC

(define_insn "movsi"

[(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k") )]

"" /*

C boolean expression, if required */

"li %0, %1" )

Uday Khedker GRC, IIT Bombay

slide-31
SLIDE 31

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 18/21

Translation Sequence in GCC

(define_insn "movsi"

[(set (match_operand:SI 0 "register_operand" "r") (match_operand:SI 1 "const_int_operand" "k") )]

"" /*

C boolean expression, if required */

"li %0, %1" ) D.1283 = 10;

(set (reg:SI 58 [D.1283]) (const int 10 [0xa]) )

li $t0, 10 Development Use

Uday Khedker GRC, IIT Bombay

slide-32
SLIDE 32

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 19/21

The Essence of Retargetability

When are the machine descriptions read?

Uday Khedker GRC, IIT Bombay

slide-33
SLIDE 33

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 19/21

The Essence of Retargetability

When are the machine descriptions read?

  • During the build process

Uday Khedker GRC, IIT Bombay

slide-34
SLIDE 34

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 19/21

The Essence of Retargetability

When are the machine descriptions read?

  • During the build process
  • When a program is compiled by gcc the information gleaned from

machine descriptions is consulted

Uday Khedker GRC, IIT Bombay

slide-35
SLIDE 35

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 20/21

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-36
SLIDE 36

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 20/21

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-37
SLIDE 37

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 20/21

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-38
SLIDE 38

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 20/21

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-39
SLIDE 39

EA-GCC, Chamonix MD Intro: The Essence of Retargetability 20/21

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-40
SLIDE 40

Part 4

Summary

slide-41
SLIDE 41

EA-GCC, Chamonix MD Intro: Summary 21/21

Summary

  • GCC achieves retargetability by reading the machine descriptions

and generating a back end customised to the machine descriptions

  • Machine descriptions are influenced by

HLLs, GCC architecture, and properties of target, host and build systems

  • Writing machine descriptions requires

specifying the C macros, target instructions and required support functions

  • define insn and define expand are used to convert a GIMPLE

representation to RTL

Uday Khedker GRC, IIT Bombay