252-210: compiler design - eth zürich · pdf file252-210: compiler design 11 intermediate...

30
252-210: Compiler Design 11 Intermediate Representa/on(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Upload: lamtu

Post on 06-Feb-2018

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

252-210:CompilerDesign

11IntermediateRepresenta/on(s)

ThomasR.Gross

ComputerScienceDepartmentETHZurich,Switzerland

Page 2: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Outline

!  DesignconcernsforIR!  SSA:StaGcSingleAssignment

2

Page 3: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Simplecompiler:Tree-basedIR

!  Oneassignmentstatement:onebinarytreeint A, B, C, D;

A = B + C * D;

!  Atreehasaroot!  Topnode!  Rightandle8subtrees

!  Sequencesofstatements:forestoftrees

3

Page 4: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

5

Page 5: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

IRconcerns

!  Makedatadependencesexplicit!  Captureproducer–consumerrela/onship

// 1 // x = a + b ;

// other basic blocks //

// 2 // d = x + 1;

// 3 // b = a + c;

!  xproducedby1,consumedby2

!  bin1mustbereadbeforebiswriNenin39

Page 6: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

!  SimplesoluGon:execute1,2,and3inorder

!  WhatmaNersis1readstheoldvalueand3producesanewvalue

!  Idea:renamevariableand/ormakecopies

// 1 // x = a + foo ;

// 2 // d = x + 1;

// 3 // bar = a + c;

11

Page 7: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

!  Compilerintroducesanewnamewheneveranewvalueisproduced.

!  Coulduseanynamebutusuallyusesubscripts:b1,b2,…

// 1 // x = a + b34 ;

// 2 // d = x + 1;

// 3 // b56 = a + c;

!  IRusedinopGmizingcompilers:staGcsingleassignment(SSA)

12

Page 8: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

11.2StaGcSingleAssignmentIR

!  Thereisoneassignmentstatementthatwritesavariable/field/memorylocaGon!  Assignmentforshort:statement,expression,….

!  Sta/c:inthesource/IR!  Single:eachstatementwritesadifferentvariable

!  SSAmakesdatadependencesexplicit

13

Page 9: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

15

Page 10: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Example

!  Statement1producesavalueforstatement2

!  Onlytruedependencesrecorded!  Noconstraintsduetovariablenames

17

x1 = a0 + b0 d1 = x1 + 1

b1 = a0 + c0

Page 11: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

SSAasIR

!  Disclaimer:!  Onlymethod-localscalarvariablesconsidered

!  Arrays&fieldsignoredfornow!  Mustbeconserva/ve:nopointer-basedassignmentsfornow

!  SSAformforstraightlinecode!  Howtoturna(JavaLi/C/Java/…)programintoSSAform

!  CondiGonalstatements

18

Page 12: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

SSAforabasicblock

!  AssumpGon:Program(method)translatedintobasicblocks,forestofIRtreesforeachbasicblock!  E.g.,ASTorsimilarIR

!  Allsourcesanddes/na/onsofopera/onsvisible!  Programwri[enwithoutconsidera/onofSSAformat

!  Goal:transformonebasicblockintoSSAformat

!  Approach:considerallstatements(IRtrees)insequence

19

Page 13: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

!  ForeachvariableX:CounterCX!  CXini/alizedto0atstartofmethod

!  CXindicatesthe“current”version!  Givenastatementorexpression

D=S⊗TorS⊗T

1.   LookupCSandCT1.  Yieldscurrentversion,saySnandTm

2.   IncrementCD1.  Yieldsnewversion(Dk)

3.   ReplaceS,T,D:Dk=Sn⊗TmorSn⊗Tm

20

Page 14: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Example

x = a + b

y = x + 2

z = a + 1

x = c * 2

w = x + 1

21

Page 15: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Example

x1 = a0 + b0 y1 = x1 + 2

z1 = a0 + 1

x2 = c0 * 2

w1 = x2 + 1

22

Page 16: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Example

TurnthefollowingexampleintoSSAform(3min)

a = a + x

x = a + b

c = a + b

x = c + 2

a = x + b

23

Page 17: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Example

TurnthefollowingexampleintoSSAform(3min)

a1 = a0 + x0 x1 = a1 + b0 c1 = a1 + b0 x2 = c1 + 2

a2 = x2 + b0

24

Page 18: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

!  Eachbasicblockcanbehandledthatway….!  Onlythevariablenamesarechanged

!  Otherwiseusetreesasbefore!  CoulduseanyotherIR

!  SomeGmesvariableaj iscalledaversionofa

!  NeedrightvalueforcountersCXatthestartofabasicblock

25

Page 19: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

CondiGonalstatements

!  Simpleexample

a = 1;

if (b ≠ 0) {

a = 0;

}

x = a ;

Counters

!  Ca = !  Cb = !  Cx = 26

Page 20: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

2.2CondiGonalstatements

!  Simpleexample

a = 1;

if (b ≠ 0) {

a = 0;

}

x = a ;

Counters

!  Ca = !  Cb = !  Cx = 27

BB0

BB1

BB2

Page 21: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Findingthecurrentversion

!  Simpleexample

a = 1;

if (b ≠ 0) {

a = 0;

}

x = a ;

28

a1 = 1;

if (b0 ≠ 0) {

a2 = 0;

}

x1 = a??? ;

Can’tuse a1 Can’tuse a2

Page 22: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

SoluGon:φfuncGon

!  Introducea“magic”funcGonφ

!  φ funcGondeliversthecorrectversion!  (intheexample)

!  If(b0=0):returnsa1!  If(b0≠0):returnsa2

!  ThefuncGonpicksthecorrectversiondependingonthepathtakentoreachBB2!  Moreprecisely:thepointwhereweneedtouseeithera1ora2

30

Page 23: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

φfuncGon

!  Result(returnvalue)dependsonpathtaken!  Resultvalueassignedtoanewversionofvariable

!  Argumentsarepossiblereturnvalues!  Differentversionsof(conceptually)thesamevariable

32

a1 = 1;

if (b0 ≠ 0) {

a2 = 0;

}

a3 = φ (a2, a1)

x1 = a3 ;

Page 24: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

φfuncGon--Notes

!  φfuncGonappearsonlyontherighthandsideofanassignment

!  φfuncGonplacedatbeginningofbasicblock!  Notmandatorybutsimplifiesreadingexamples

!  Simplifiesop/miza/ons/transforma/ons

35

Page 25: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

38

Page 26: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

ConverGngtoSSA

!  GivenaCFGwithENTRYnode,forestofIRtreesorAST!  StartwithENTRYnode

!  Converttheopera/onsinthisbasicblock

!  Insertφ funcGonsasneeded!  MoreonthisinAdvancedCompilerDesign

!  ProcessnextbasicblockunGlallblockshavebeenprocessed

39

Page 27: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

ImplemenGngφ funcGons

40

BB0

BB1 BB2

BB2

if (b0 ≠ 0)

a2 = 0 a1 = 1

a3 = φ (a2 , a1)

Page 28: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

!  Assigna1anda2tothesameregister!  Botha1anda2areallocatedtoaregisterandtheygetthesame

register

!  Usefulifa3isreadbeforeitisspilled

!  Createatemporaryt(storedinmemory)andinsertcopystatements

41

BB1 BB2

BB2

a2 = 0 t = a2

a1 = 1 t = a1

a3 = t

Page 29: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

SSAinpracGce

!  Usedinmanycompilers!  InGCC3.0(2001)

!  InteresGngconsequencesforregisterallocaGon!  Interferencegraphchordalifvariableversionsdefinenodes

!  Edgesindicatethatthereisaconflict

42

Page 30: 252-210: Compiler Design - ETH Zürich · PDF file252-210: Compiler Design 11 Intermediate Representaon(s) Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

SSAinpracGce

!  Usedinmanycompilers

!  InteresGngconsequencesforregisterallocaGon!  Interferencegraphchordalifvariableversionsdefinenodes

!  Edgesindicatethatthereisaconflict!  Canbecoloredinpolynomial/me

!  Butthereisnofreelunch!  Interferencegraphbasedonvariableversionshasalargernumberof

nodesthaninterferencegraphbasedonliveranges

44