252 210 compiler design 9 2 points and paths 9 3 transfer
play

252-210: Compiler Design 9.2 Points and paths 9.3 - PowerPoint PPT Presentation

252-210: Compiler Design 9.2 Points and paths 9.3 Transfer func6ons Thomas R. Gross Computer Science Department ETH Zurich, Switzerland Outline


  1. 252-­‑210: ¡Compiler ¡Design ¡ ¡ 9.2 ¡Points ¡and ¡paths ¡ 9.3 ¡Transfer ¡func6ons ¡ Thomas ¡R. ¡Gross ¡ ¡ Computer ¡Science ¡Department ¡ ETH ¡Zurich, ¡Switzerland ¡

  2. Outline ¡ § Points ¡ § Paths ¡ § Transfer ¡funcEons ¡ 2 ¡

  3. Terminology ¡ § Local ¡{analysis ¡| ¡transformaEon}: ¡inside ¡a ¡basic ¡block ¡ § Global ¡{analysis ¡| ¡transformaEon}: ¡inside ¡a ¡method/funcEon ¡ § Intra-­‑procedural ¡.. ¡ § Inter-­‑procedural ¡{analysis ¡| ¡transformaEon}: ¡across ¡ methods/funcEons ¡ 3 ¡

  4. 9.2 ¡Paths ¡and ¡points ¡ ¡ § Want ¡to ¡talk ¡about ¡flow ¡of ¡data ¡in ¡program ¡ § Point: ¡a ¡place ¡in ¡the ¡program ¡ § Given ¡a ¡statement ¡S ¡ § P before_S : ¡point ¡before ¡statement ¡S ¡is ¡executed ¡ § P aBer_S : ¡ ¡point ¡aBer ¡statement ¡S ¡is ¡executed ¡ § Drop ¡S ¡if ¡no ¡risk ¡of ¡confusion ¡ § Example ¡ a = b + c; //S1 x = a + 2; //S2 4 ¡

  5. 5 ¡

  6. § Point: ¡a ¡place ¡in ¡a ¡program ¡ § ¡Given ¡a ¡basic ¡block ¡B ¡ § P before_B : ¡point ¡before ¡basic ¡block ¡B ¡is ¡executed ¡ § P aBer_B : ¡ ¡point ¡aBer ¡basic ¡block ¡B ¡is ¡executed ¡ § Drop ¡B ¡if ¡no ¡risk ¡of ¡confusion ¡ 6 ¡

  7. B0 ¡ k = 1 minVal = A[0] B1 ¡ L: TCond1 = k < max if (TCond1) B2 ¡ TCond2 = A[k] < minVal if (TCond2) B3 ¡ minVal = A[k] k = k + 1 B4 ¡ Goto L B5 ¡ return minVal 7 ¡

  8. § A ¡path ¡is ¡a ¡sequence ¡of ¡points ¡(x 0 , ¡x 1 , ¡x 2 , ¡…, ¡x n ) ¡such ¡that ¡for ¡ any ¡pair ¡(x j , ¡x j+1 ) ¡ with ¡0≤j<n ¡ one ¡of ¡these ¡condiEons ¡holds ¡ 1. x j ¡is ¡the ¡point ¡before ¡basic ¡block ¡B ¡and ¡x j+1 ¡is ¡the ¡point ¡ aBer ¡basic ¡block ¡B ¡ 2. x j ¡is ¡the ¡point ¡aBer ¡basic ¡block ¡B k ¡and ¡x j+1 ¡is ¡the ¡point ¡ before ¡basic ¡block ¡B m ¡and ¡there ¡is ¡an ¡edge ¡from ¡B k ¡to ¡B m ¡ in ¡the ¡CFG ¡ § DefiniEon ¡can ¡be ¡extended ¡to ¡deal ¡with ¡points ¡before ¡ statements. ¡ 8 ¡

  9. Comments ¡ § Points ¡may ¡have ¡mulEple ¡predecessors ¡ § Join ¡points ¡ § Join ¡nodes ¡(in ¡the ¡CFG) ¡ § Points ¡may ¡have ¡mulEple ¡successors ¡ § Split ¡points ¡ § Split ¡nodes ¡ § When ¡summarizing ¡paths ¡we ¡may ¡just ¡list ¡the ¡basic ¡blocks ¡ 9 ¡

  10. Paths ¡ B0 ¡ ¡ B1 ¡ ¡ B2 ¡ ¡ B3 ¡ ¡ 10 ¡

  11. Paths ¡ i ¡= ¡0 ¡ B0 ¡ ¡ if ¡(i>0) ¡ ¡ B1 ¡ ¡ B2 ¡ ¡ B3 ¡ ¡ 11 ¡

  12. Paths ¡ B0 ¡ ¡ if ¡(i>0) ¡ ¡ B1 ¡ ¡ B2 ¡ ¡ if ¡(i<0) ¡ ¡ B3 ¡ ¡ B4 ¡ ¡ B5 ¡ ¡ B6 ¡ ¡ 12 ¡

  13. Paths ¡ § We ¡are ¡interested ¡in ¡all ¡paths ¡in ¡a ¡CFG ¡ § Even ¡if ¡they ¡can ¡never ¡be ¡taken ¡in ¡an ¡execu6on ¡ § Need ¡summary ¡informaEon ¡ § There ¡can ¡be ¡arbitrarily ¡many ¡paths ¡ 13 ¡

  14. B0 ¡ Loops ¡ B1 ¡ if ¡(COND) ¡ ¡ B2 ¡ B3 ¡ 14 ¡

  15. B0 ¡ Loops ¡ B1 ¡ if ¡(COND) ¡ ¡ B2 ¡ B3 ¡ § B0 ¡B1 ¡B3 ¡ § B0 ¡B1 ¡B2 ¡B1 ¡B3 ¡ § B0 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B3 ¡ 15 ¡ § B0 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡B1 ¡B2 ¡… ¡

  16. Paths ¡ § We ¡deal ¡only ¡with ¡finite ¡paths ¡ § PracEcal ¡view: ¡what ¡has ¡happened ¡when ¡program ¡execuEon ¡ reaches ¡B3 ¡ § P before_B3 ¡ § Execu6on ¡(for ¡some ¡input) ¡may ¡never ¡reach ¡B3 ¡…. ¡ § Summary ¡informaEon ¡is ¡needed ¡ 16 ¡

  17. 9.3 ¡Transfer ¡funcEons ¡ § We ¡are ¡interested ¡in ¡knowing ¡that ¡a ¡variable ¡has ¡a ¡constant ¡ value ¡at ¡a ¡given ¡point ¡in ¡the ¡program. ¡ 17 ¡

  18. a = 4; if (…) { c = 7; } else { b = d + 2; } // S: d = a * 2; … 18 ¡

  19. a = 4; if (…) { c = 7; } else { b = d + 2; } // S: d = a * 2; … 19 ¡

  20. 9.3 ¡Transfer ¡funcEons ¡ § We ¡are ¡interested ¡in ¡knowing ¡that ¡a ¡variable ¡has ¡a ¡constant ¡ value ¡at ¡a ¡given ¡point ¡in ¡the ¡program. ¡ § Can ¡we ¡establish ¡that ¡“a ¡= ¡4” ¡ ¡before ¡statement ¡S ¡is ¡ executed? ¡ § Need ¡to ¡consider ¡all ¡paths ¡that ¡lead ¡to ¡point ¡P before_S ¡ 20 ¡

  21. a = 4 Tcond = if (Tcond) a = 4; if (…) { c = 7; } b = d + 2 c = 7 else { b = d + 2; } // S: P before ¡ d = a * 2; d = a * 2 … 21 ¡

  22. a = 4 Tcond = if (Tcond) a = 4; if (…) { c = 7; } b = d + 2 c = 7 else { b = d + 2; } // S: P before ¡ d = a * 2; d = a * 2 … 22 ¡

  23. a = 4 Tcond = if (Tcond) a = 4; if (…) { c = 7; } b = d + 2 c = 7 else { b = d + 2; } // S: P before ¡ d = a * 2; d = a * 2 … 23 ¡

  24. 9.3 ¡Transfer ¡funcEons ¡ § We ¡are ¡interested ¡in ¡knowing ¡that ¡a ¡variable ¡has ¡a ¡constant ¡ value ¡at ¡a ¡given ¡point ¡in ¡the ¡program. ¡ § Can ¡we ¡establish ¡that ¡“a ¡= ¡4” ¡ ¡before ¡statement ¡S ¡is ¡ executed? ¡ § Need ¡to ¡consider ¡all ¡paths ¡that ¡lead ¡to ¡point ¡P before_S ¡ § What ¡is ¡the ¡effect ¡of ¡statements ¡along ¡these ¡paths? ¡ 24 ¡

  25. Path ¡(basic ¡blocks) ¡ § A ¡path ¡is ¡a ¡sequence ¡of ¡points ¡(x 0 , ¡x 1 , ¡x 2 , ¡…, ¡x n ) ¡such ¡that ¡for ¡ any ¡pair ¡(x j , ¡x j+1 ) ¡ with ¡0≤j<n ¡ one ¡of ¡these ¡condiEons ¡holds ¡ 1. x j ¡is ¡the ¡point ¡before ¡basic ¡block ¡B ¡and ¡x j+1 ¡is ¡the ¡point ¡ aBer ¡basic ¡block ¡B ¡ 2. x j ¡is ¡the ¡point ¡aBer ¡basic ¡block ¡B k ¡and ¡x j+1 ¡is ¡the ¡point ¡ before ¡basic ¡block ¡B m ¡and ¡there ¡is ¡an ¡edge ¡from ¡B k ¡to ¡B m ¡ in ¡the ¡CFG ¡ § DefiniEon ¡can ¡be ¡extended ¡to ¡deal ¡with ¡points ¡before ¡ statements. ¡ 25 ¡

  26. Path ¡(for ¡statements) ¡ § Given ¡a ¡CFG. ¡For ¡each ¡node ¡B ¡(i.e., ¡basic ¡block) ¡there ¡exists ¡ an ¡(ordered) ¡sequence ¡of ¡statements ¡S B,0 , ¡S B,1 , ¡… ¡S B,m . ¡ § Empty ¡basic ¡block? ¡ ¡-­‑-­‑ ¡Allow ¡“no ¡op ¡statement” ¡ § S B,0 : ¡first ¡statement, ¡S B,m : ¡last ¡statement ¡ § Order: ¡program ¡order ¡ ¡ § A ¡path ¡is ¡a ¡sequence ¡of ¡points ¡(x 0 , ¡x 1 , ¡x 2 , ¡…, ¡x n ) ¡such ¡that ¡for ¡ any ¡pair ¡(x j , ¡x j+1 ) ¡ with ¡0≤j<n ¡ one ¡of ¡these ¡condiEons ¡holds ¡ 1. x j ¡is ¡the ¡point ¡before ¡statement ¡S ¡and ¡x j+1 ¡is ¡the ¡point ¡ aBer ¡statement ¡S ¡ 2. x j ¡is ¡the ¡point ¡aBer ¡the ¡last ¡statement ¡S’ ¡of ¡basic ¡block ¡B k ¡ and ¡x j+1 ¡is ¡the ¡point ¡before ¡the ¡first ¡statement ¡S” ¡of ¡basic ¡ block ¡B m ¡and ¡there ¡is ¡an ¡edge ¡from ¡B k ¡to ¡B m ¡in ¡the ¡CFG ¡ 26 ¡ ¡

  27. Claim: ¡a=4 ¡@ ¡S ¡ § What ¡is ¡the ¡effect ¡of ¡statements ¡along ¡the ¡ ¡paths ¡that ¡end ¡at ¡ P before_S ? ¡ § Consider ¡each ¡statement ¡on ¡a ¡path ¡ § If ¡the ¡last ¡assignment ¡to ¡variable ¡ ¡ a ¡supports ¡the ¡claim ¡then ¡the ¡claim ¡ is ¡valid ¡ § If ¡the ¡claim ¡is ¡valid ¡along ¡all ¡paths ¡then ¡it’s ¡valid ¡at ¡P before_S ¡ 27 ¡

  28. Transfer ¡funcEon ¡ § The ¡transfer ¡funcEon ¡captures ¡the ¡effect ¡of ¡a ¡statement ¡S ¡on ¡ our ¡claim ¡ § Claim ¡In ¡(at ¡P before_S ) ¡ ¡ ➞ ¡Claim ¡Out ¡(at ¡P aBer_S ) ¡ § (AlternaEvely) ¡The ¡transfer ¡funcEon ¡captures ¡the ¡effect ¡of ¡ the ¡statements ¡in ¡a ¡basic ¡block ¡on ¡our ¡claim. ¡ ¡ Claim ¡(for ¡x ¡and ¡v) ¡at ¡P before_S ¡ T: ¡Transfer ¡func,on ¡ Claim ¡(for ¡x ¡and ¡v) ¡at ¡P aBer_S ¡ 28 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend