Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w11-02-constant... ·...

17
Compiler Design Spring 2017 8.4 Transfer functions; constant propagation Dr. Zoltán Majó Compiler Group – Java HotSpot Virtual Machine Oracle Corporation

Transcript of Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w11-02-constant... ·...

CompilerDesignSpring2017

8.4Transferfunctions;constantpropagation

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

Constantpropagation

§ Weareinterestedinknowingthatavariablehasaconstantvalueatagivenpointintheprogram.

2

a = 4;

if (…) {

c = 7;}

else {

b = d + 2;}

// S:

d = a * 2;

…3

Constantpropagation

§ Weareinterestedinknowingthatavariablehasaconstantvalueatagivenpointintheprogram.

§ Canweestablishthat“a==4”beforestatementSisexecuted?

§ NeedtoconsiderallpathsthatleadtopointPbefore_S

4

a = 4;

if (…) {

c = 7;}

else {

b = d + 2;}

// S:

d = a * 2;

…5

Pbefore

a = 4Tcond =if (Tcond)

b = d + 2 c = 7

d = a * 2

a = 4;

if (…) {

c = 7;}

else {

b = d + 2;}

// S:

d = a * 2;

…6

a = 4Tcond =if (Tcond)

b = d + 2 c = 7

d = a * 2

Pbefore

a = 4;

if (…) {

c = 7;}

else {

b = d + 2;}

// S:

d = a * 2;

…7

a = 4Tcond =if (Tcond)

b = d + 2 c = 7

d = a * 2

Pbefore

Constantpropagation

§ Weareinterestedinknowingthatavariablehasaconstantvalueatagivenpointintheprogram.

§ Canweestablishthat“a==4”beforestatementSisexecuted?

§ NeedtoconsiderallpathsthatleadtopointPbefore_S§ Whatistheeffectofstatementsalongthesepaths?

8

Path(basicblocks)

§ Apathisasequenceofpoints(x0,x1,x2,…,xn)suchthatforanypair(xj,xj+1)with0≤j<noneoftheseconditionsholds1. xj isthepointbeforebasicblockBandxj+1 isthepoint

afterbasicblockB2. xj isthepointafterbasicblockBk andxj+1 isthepoint

beforebasicblockBm andthereisanedgefromBk toBmintheCFG

§ Definitioncanbeextendedtodealwithpointsbefore/afterstatements.

9

Path(forstatements)

§ GivenaCFG,foreachnodeB(i.e.,basicblock)thereexistsan(ordered)sequenceofstatementsSB,0,SB,1,…SB,m.§ Emptybasicblock?-- Allow“noopstatement”§ SB,0:firststatement,SB,m: laststatement§ Order:programorder

§ Apathisasequenceofpoints(x0,x1,x2,…,xn)suchthatforanypair(xj,xj+1)with0≤j<noneoftheseconditionsholds1. xj isthepointbeforestatementSandxj+1 isthepoint

afterstatementS2. xj isthepointafterthelaststatementS’ofbasicblockBk

andxj+1 isthepointbeforethefirststatementS”ofbasicblockBm andthereisanedgefromBk toBm intheCFG

10

11

12

Claim:a==4@S

§ WhatistheeffectofstatementsalongthepathsthatendatPbefore_S?

§ Considereachstatementonapath§ Ifthelastassignmenttovariablea supportstheclaim,thentheclaim

isvalid§ Iftheclaimisvalidalongallpathsthenit’svalidatPbefore_S

13

Transferfunction

§ ThetransferfunctioncapturestheeffectofastatementSonourclaim§ ClaimIn(atPbefore_S)➞ ClaimOut(atPafter_S)

§ (Alternatively)Thetransferfunctioncapturestheeffectofthestatementsinabasicblockonourclaim.

14

Claim(forxandv)atPbefore_S

Claim(forxandv)atPafter_S

T:Transferfunction

Transferfunction(cont’d)

§ Transferfunctionforeachclaim(analysis)§ Here:wanttoseeifvariablexhasavaluev,withvaconstant

§ Transferfunction(forS)mapsinputclaimtooutputclaim

§ Here,thetransferfunctionallowsthreedifferentinputoroutputvaluesforourclaim:§ ⟙:(forTop)variablexisnotaconstant§ value:xhasthe(constant)valuevalue

§ Differentvariablesimplydifferentclaims

§ ⊥:(forBottom)wedon’tknow;statementisasfarasweknownotexecuted

15

Transferfunction(cont’d)

§ Transferfunctionmustbeconservative.§ Donotmisleadthecompiler

§ Variablexhasconstantvaluevbuttransferfunctionreturns⟙.§ Acceptable

§ Variablexhasconstantvaluevandtransferfunctionreturnsv.§ Great

§ Variablexhasconstantvaluevbuttransferfunctionreturnskwithk≠v.§ NotOK

16

17

notaccurateandnotsafe