Type- and Control-Flow Analysis Matthew Fluet mtf@cs.rit.edu - - PowerPoint PPT Presentation

type and control flow analysis
SMART_READER_LITE
LIVE PREVIEW

Type- and Control-Flow Analysis Matthew Fluet mtf@cs.rit.edu - - PowerPoint PPT Presentation

Type- and Control-Flow Analysis Matthew Fluet mtf@cs.rit.edu Department of Computer Science Rochester Institute of Technolgy December 15, 2015 Advances in Programming Languages and Systems (APLS) Fluet (RIT) T&CFA APLS 1 / 18


slide-1
SLIDE 1

Type- and Control-Flow Analysis

Matthew Fluet mtf@cs.rit.edu

Department of Computer Science Rochester Institute of Technolgy

December 15, 2015 Advances in Programming Languages and Systems (APLS)

Fluet (RIT) T&CFA APLS 1 / 18

slide-2
SLIDE 2

Introduction

Control-flow analysis A compile-time approximation of the “flow” of functions in program: which functions might be bound to a given variable at run time. an enabling analysis for the compilation of functional languages

because control flow is not syntactically apparent

typically formulated for dynamically- or simply-typed languages

Fluet (RIT) T&CFA APLS 2 / 18

slide-3
SLIDE 3

Introduction

Control-flow analysis A compile-time approximation of the “flow” of functions in program: which functions might be bound to a given variable at run time. an enabling analysis for the compilation of functional languages

because control flow is not syntactically apparent

typically formulated for dynamically- or simply-typed languages popular statically-typed functional languages are richly-typed System F (and extensions) are popular intermediate languages Seek a control-flow analysis formulated for System F (and extensions).

Exploit well-typedness to obtain more precise control-flow information.

Fluet (RIT) T&CFA APLS 2 / 18

slide-4
SLIDE 4

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-5
SLIDE 5

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-6
SLIDE 6

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-7
SLIDE 7

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-8
SLIDE 8

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-9
SLIDE 9

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-10
SLIDE 10

Example

f1 = λ x1 . · · · f2 = λ x2 . · · · id = λ x . x res1 = id f1 res2 = id f2 0CFA ˆ ρ(x) { f1 , f2 } ˆ ρ(res1) { f1 , f2 } ˆ ρ(res2) { f1 , f2 } 0CFA — “classic” monovariant/context-insensitive control-flow analysis

Fluet (RIT) T&CFA APLS 3 / 18

slide-11
SLIDE 11

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-12
SLIDE 12

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2

Fluet (RIT) T&CFA APLS 3 / 18

slide-13
SLIDE 13

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2 CFA ˆ ρ(xα) { f1Int→Int , f2Bool→Bool } ˆ ρ(res1Int→Int) { f1Int→Int , f2Bool→Bool } ˆ ρ(res2Bool→Bool) { f1Int→Int , f2Bool→Bool }

Fluet (RIT) T&CFA APLS 3 / 18

slide-14
SLIDE 14

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2 CFA then type-filter ˆ ρ(xα) { f1Int→Int , f2Bool→Bool } ˆ ρ(res1Int→Int) { f1Int→Int } ˆ ρ(res2Bool→Bool) { f2Bool→Bool }

Fluet (RIT) T&CFA APLS 3 / 18

slide-15
SLIDE 15

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2 CFA then type-filter ˆ ρ(xα) { f1Int→Int , f2Bool→Bool } ˆ ρ(res1Int→Int) { f1Int→Int } ˆ ρ(res2Bool→Bool) { f2Bool→Bool }

Fluet (RIT) T&CFA APLS 3 / 18

slide-16
SLIDE 16

Example

f1 : Int → Int = λ x1 : Int. · · · f2 : Bool → Bool = λ x2 : Bool. · · · id : ∀α. α → α = Λ α . λ x : α . x res1 : Int → Int = id [ Int → Int ] f1 res2 : Bool → Bool = id [ Bool → Bool ] f2 TCFA ˆ ρ(xα) { f1Int→Int , f2Bool→Bool } ˆ ρ(res1Int→Int) { f1Int→Int } ˆ ρ(res2Bool→Bool) { f2Bool→Bool } ˆ φ(α) { Int → Int , Bool → Bool }

Fluet (RIT) T&CFA APLS 3 / 18

slide-17
SLIDE 17

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz"

Fluet (RIT) T&CFA APLS 4 / 18

slide-18
SLIDE 18

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" CFA ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λa1 , λb1 , λc1 } ˆ ρ(res1) { λa1 , λb1 , λc1 } ˆ ρ(res2) { λa1 , λb1 , λc1 } ˆ ρ(res3) { λa2 , λb2 , λc2 }

Fluet (RIT) T&CFA APLS 4 / 18

slide-19
SLIDE 19

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" CFA then type-filter ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λa1 , λb1 , λc1 } ˆ ρ(res1) { λa1 , λb1 , λc1 } ˆ ρ(res2) { λa1 , λb1 , λc1 } ˆ ρ(res3) { λa2 , λb2 , λc2 }

Fluet (RIT) T&CFA APLS 4 / 18

slide-20
SLIDE 20

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" CFA then type-filter ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λa1 , λb1 , λc1 } ˆ ρ(res1) { λa1 } ˆ ρ(res2) { λb1 } ˆ ρ(res3) { λa2 , λb2 , λc2 }

Fluet (RIT) T&CFA APLS 4 / 18

slide-21
SLIDE 21

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" (T+C)FA ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λa1 , λb1 , λc1 } ˆ ρ(res1) { λa1 , λb1 , λc1 } ˆ ρ(res2) { λa1 , λb1 , λc1 } ˆ ρ(res3) { λa2 , λb2 , λc2 } ˆ φ(α) { int→int→int , bool→int→int , β→γ } ˆ φ(β) { str } ˆ φ(γ) { int→int }

Fluet (RIT) T&CFA APLS 4 / 18

slide-22
SLIDE 22

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" (T+C)FA then type-filter ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λa1 , λb1 , λc1 } ˆ ρ(res1) { λa1 , λb1 , λc1 } ˆ ρ(res2) { λa1 , λb1 , λc1 } ˆ ρ(res3) { λa2 , λb2 , λc2 } ˆ φ(α) { int→int→int , bool→int→int , β→γ } ˆ φ(β) { str } ˆ φ(γ) { int→int }

Fluet (RIT) T&CFA APLS 4 / 18

slide-23
SLIDE 23

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" (T+C)FA then type-filter ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λc1 } ˆ ρ(res1) { λa1 } ˆ ρ(res2) { λb1 } ˆ ρ(res3) { λa2 , λb2 , λc2 } ˆ φ(α) { int→int→int , bool→int→int , β→γ } ˆ φ(β) { str } ˆ φ(γ) { int→int }

Fluet (RIT) T&CFA APLS 4 / 18

slide-24
SLIDE 24

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" (T&C)FA ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λc1 } ˆ ρ(res1) { λa1 } ˆ ρ(res2) { λb1 } ˆ ρ(res3) { λa2 , λb2 , λc2 } ˆ φ(α) { int→int→int , bool→int→int , β→γ } ˆ φ(β) { str } ˆ φ(γ) { int→int }

Fluet (RIT) T&CFA APLS 4 / 18

slide-25
SLIDE 25

Example

id:∀α. α → α = Λα. λx:α. x app:∀β. ∀γ. (β → γ) → β → γ = Λβ.Λγ.λf:β → γ.λz:β. let g:β → γ = id [β → γ] f in g z h1:int → int → int = λa1:int.λa2:int. a1+a2 h2:bool → int → int = λb1:bool.λb2:int. if b1 then b2+1 else b2 h3:str → int → int = λc1:str.λc2:int. len(c1)+c2 res1:int → int → int = id [int → int → int] h1 res2:bool → int → int = id [bool → int → int] h2 res3:int → int = app [str] [int → int] h3 "zzz" (T&C)FA ˆ ρ(x) { λa1 , λb1 , λc1 } ˆ ρ(f) { λc1 } ˆ ρ(g) { λc1 } ˆ ρ(res1) { λa1 } ˆ ρ(res2) { λb1 } ˆ ρ(res3) { λc2 } ˆ φ(α) { int→int→int , bool→int→int , β→γ } ˆ φ(β) { str } ˆ φ(γ) { int→int }

Fluet (RIT) T&CFA APLS 4 / 18

slide-26
SLIDE 26

Introduction

A type- and control-flow analysis for System F Type-flow analysis A compile-time approximation of the “flow” of types in program: which types might be bound to a given type variable at run time

determines type abstractions flowing to type applications

rejects flows incompatible with static typing

Control-flow analysis A compile-time approximation of the “flow” of functions in program: which functions might be bound to a given variable at run time

Fluet (RIT) T&CFA APLS 5 / 18

slide-27
SLIDE 27

Introduction

A type- and control-flow analysis for System F Types improve the precision of the control-flow analysis; improve the effectiveness of optimizations based on the analysis. inlining, copy propagation, dead-code elimination, . . . Type-flow analysis enables novel optimizations. polymorphic functions used at a finite number of types can be optimized to monomorphic instances

  • ptimization of intensional polymorphism (i.e., typecase α of · · · )

Fluet (RIT) T&CFA APLS 6 / 18

slide-28
SLIDE 28

Overview

Specification-Based Formulation of TCFA (IFL’12)

A collection of declarative constraints that a valid analysis result must satisfy; given a proposed analysis result, verify that it satisfies the constraints.

Soundness, Existence, Decidability, Computability, Complexity

Flow-Graph-Based Formulation of TCFA (IFL’14)

A graph with edges corresponding to flow of abstract values; analysis result determined by graph reachability.

Soundness, Algorithm, Complexity

Related and Future Work

Fluet (RIT) T&CFA APLS 7 / 18

slide-29
SLIDE 29

Specification-based Formulation: Acceptability

ˆ φ; ˆ ρ S e “ˆ φ and ˆ ρ are acceptable for e”

ˆ φ and ˆ ρ approximate every run-time type and value environment that arises during evaluation

Abstract type environments map type variables to sets of (syntactic) types. Abstract value environments map variables to sets of (syntactic) values. ATEnv = TyVar → P(Type) ∋ ˆ φ ::= { α → {τ, . . .}, . . . } AVEnv = Var → P(Val) ∋ ˆ ρ ::= { x → {v, . . .}, . . . }

Fluet (RIT) T&CFA APLS 8 / 18

slide-30
SLIDE 30

Specification-based Formulation: Acceptability

ˆ φ; ˆ ρ S e “ˆ φ and ˆ ρ are an acceptable type- and control-flow analysis for e”

Fluet (RIT) T&CFA APLS 9 / 18

slide-31
SLIDE 31

Specification-based Formulation: Acceptability

ˆ φ; ˆ ρ S e “ˆ φ and ˆ ρ are an acceptable type- and control-flow analysis for e” ˆ φ; ˆ ρ S x λz:τz.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = λz:τz.eb in e Λβ.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = Λβ.eb in e

  • λz:τz.eb∈ ˆ

ρ(xf )

∀va ∈ ˆ

ρ(xa) . va ∈ ˆ ρ(z) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf xa in e

  • Λβ.eb∈ ˆ

ρ(xf )

  • τa ∈ ˆ

φ(β) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf [τa] in e

Fluet (RIT) T&CFA APLS 9 / 18

slide-32
SLIDE 32

Specification-based Formulation: Acceptability

ˆ φ; ˆ ρ S e “ˆ φ and ˆ ρ are an acceptable type- and control-flow analysis for e” ˆ φ; ˆ ρ S x λz:τz.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = λz:τz.eb in e Λβ.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = Λβ.eb in e

  • λz:τz.eb∈ ˆ

ρ(xf )

∀va ∈ ˆ

ρ(xa) . va ∈ ˆ ρ(z) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf xa in e

  • Λβ.eb∈ ˆ

ρ(xf )

  • τa ∈ ˆ

φ(β) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf [τa] in e

Fluet (RIT) T&CFA APLS 9 / 18

slide-33
SLIDE 33

Specification-based Formulation: Acceptability

ˆ φ; ˆ ρ S e “ˆ φ and ˆ ρ are an acceptable type- and control-flow analysis for e” ˆ φ; ˆ ρ S x λz:τz.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = λz:τz.eb in e Λβ.eb ∈ ˆ ρ(x) ˆ φ; ˆ ρ S eb ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = Λβ.eb in e

  • λz:τz.eb∈ ˆ

ρ(xf )

  • ∀va ∈ ˆ

ρ(xa) . ˆ φ S TyOf(va) ∼ ∼ ∼ τz ⇒ va ∈ ˆ ρ(z) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . ˆ φ S TyOf(vb) ∼ ∼ ∼ τx ⇒ vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf xa in e

  • Λβ.eb∈ ˆ

ρ(xf )

  • τa ∈ ˆ

φ(β) ∧

∀vb ∈ ˆ

ρ(ResOf(eb)) . ˆ φ S TyOf(vb) ∼ ∼ ∼ τx ⇒ vb ∈ ˆ ρ(x)

  • ˆ

φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:τx = xf [τa] in e

Fluet (RIT) T&CFA APLS 9 / 18

slide-34
SLIDE 34

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ”

Fluet (RIT) T&CFA APLS 10 / 18

slide-35
SLIDE 35

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ” ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 = θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

Fluet (RIT) T&CFA APLS 10 / 18

slide-36
SLIDE 36

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ” ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 = θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

τ1 and τ2 “expand” to a common closed type; θ1 = θ2 is syntactic equality

Fluet (RIT) T&CFA APLS 10 / 18

slide-37
SLIDE 37

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ” ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 = θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

Is ˆ φ S τ1 ∼ ∼ ∼ τ2 decidable? “Recursion” in abstract type environment foils exhaustive enumeration; may be infinitely many θ such that ˆ φ S τ ⇒ ⇒ θ.

Fluet (RIT) T&CFA APLS 10 / 18

slide-38
SLIDE 38

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ” ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 = θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

ˆ φ S τ1 ∼ ∼ ∼ τ2 is decidable! Interpret ˆ φ as (productions for) a regular-tree grammar; a derivation of ˆ φ S α ⇒ ⇒ θ is a parse tree. Regular-tree grammars are closed under intersection and emptiness of regular-tree grammars is decidable.

Fluet (RIT) T&CFA APLS 10 / 18

slide-39
SLIDE 39

Specification-based Formulation: Type Compatibility

ˆ φ S τ1 ∼ ∼ ∼ τ2 “τ1 and τ2 are compatible under ˆ φ” ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 = θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

ˆ φ S τ1 ∼ ∼ ∼ τ2 is decidable! Interpret ˆ φ as (productions for) a regular-tree grammar; a derivation of ˆ φ S α ⇒ ⇒ θ is a parse tree. Regular-tree grammars are closed under intersection and emptiness of regular-tree grammars is decidable. Least ˆ ρ and ˆ φ such that ˆ φ; ˆ ρ S e computable by lfp iteration.

Fluet (RIT) T&CFA APLS 10 / 18

slide-40
SLIDE 40

Considering Complexity

Is the type- and control-flow analysis efficiently computable?

Fluet (RIT) T&CFA APLS 11 / 18

slide-41
SLIDE 41

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) “classic” CFA O(n5) TCFA

Fluet (RIT) T&CFA APLS 11 / 18

slide-42
SLIDE 42

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) “classic” CFA O(n5) TCFA O(n13)

Fluet (RIT) T&CFA APLS 11 / 18

slide-43
SLIDE 43

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) “classic” CFA O(n5) TCFA O(n13) O(n10) amortized

Fluet (RIT) T&CFA APLS 11 / 18

slide-44
SLIDE 44

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) Flow-Graph-Based (work-queue iteration) “classic” CFA O(n5) O(n3) TCFA O(n13) O(n10) amortized

Fluet (RIT) T&CFA APLS 11 / 18

slide-45
SLIDE 45

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) Flow-Graph-Based (work-queue iteration) “classic” CFA O(n5) O(n3) TCFA O(n13) O(n10) amortized O(n4)

Fluet (RIT) T&CFA APLS 11 / 18

slide-46
SLIDE 46

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) Flow-Graph-Based (work-queue iteration) “classic” CFA O(n5) O(n3) TCFA O(n13) O(n10) amortized O(n4) O(l3 + l2m2 + m4) l: the size of variables, values, and calls m: the size of type variables and types

Fluet (RIT) T&CFA APLS 11 / 18

slide-47
SLIDE 47

Considering Complexity

Is the type- and control-flow analysis efficiently computable? Specification-Based (naïve lfp iteration) Flow-Graph-Based (work-queue iteration) “classic” CFA O(n5) O(n3) TCFA O(n13) O(n10) amortized O(n4) O(l3 + l2m2 + m4) O(l3 + m4) amortized l: the size of variables, values, and calls m: the size of type variables and types

Fluet (RIT) T&CFA APLS 11 / 18

slide-48
SLIDE 48

Considering Complexity

O(l3 + m4)

Fluet (RIT) T&CFA APLS 12 / 18

slide-49
SLIDE 49

Considering Complexity

O(l3 + m4) Expect size of programs of size n to be dominated by size l of variables, values, and calls, not by size m of type variables and types

A program may have many defs and uses of int → int funs, all sharing the same (top-level) type def

Fluet (RIT) T&CFA APLS 12 / 18

slide-50
SLIDE 50

Considering Complexity

O(l3 + m4) Expect size of programs of size n to be dominated by size l of variables, values, and calls, not by size m of type variables and types

A program may have many defs and uses of int → int funs, all sharing the same (top-level) type def

Not bad in theory; much better in practice

Fluet (RIT) T&CFA APLS 12 / 18

slide-51
SLIDE 51

Considering Complexity

O(l3 + m4) Expect size of programs of size n to be dominated by size l of variables, values, and calls, not by size m of type variables and types

A program may have many defs and uses of int → int funs, all sharing the same (top-level) type def

Not bad in theory; much better in practice Worst-case analysis assumes every value flows to every variable and every type flows to every type variable

But, CFA (and TCFA?) is useful and used in compilers

CFA finds many variables with small numbers (often 1) of values TCFA finds many type variables with small numbers (often 1) of types Algorithm only explores concl of found flows

Fluet (RIT) T&CFA APLS 12 / 18

slide-52
SLIDE 52

Related Work (and Lack Thereof)

Need to distinguish between

flow analyses expressed as sophisticated type systems (many) flow analyses of languages with sophisticated type systems (few)

Fluet (RIT) T&CFA APLS 13 / 18

slide-53
SLIDE 53

Related Work (and Lack Thereof)

Need to distinguish between

flow analyses expressed as sophisticated type systems (many) flow analyses of languages with sophisticated type systems (few)

For simply-typed λ-calculus, 0CFA not improved by type filtering For rank-1 polymorphism (i.e., “let”-polymorphism), either use

monomorphisation (explicitly eliminate polymorphism before analysis) Flow-directed Closure Conversion [Cejtin, Jagannathan, & Weeks (ESOP’00)] polyvariance (implicitly eliminate polymorphism during analysis) Set-based Program Analysis of ML Programs [Heintze (LFP’94)]

For full System F, assume type-based analyses are “good enough”

Fluet (RIT) T&CFA APLS 13 / 18

slide-54
SLIDE 54

Related Work (and Lack Thereof)

Need to distinguish between

flow analyses expressed as sophisticated type systems (many) flow analyses of languages with sophisticated type systems (few)

Type-Directed Flow Analysis for Typed Intermediate Languages; Jagannathan, Weeks, & Wright (SAS’97)

limited to predicative System F with recursion polyvariant analysis diverges on programs using polymorphic recursion

Type-sensitive Control-Flow Analysis; Reppy (ML’06)

Suggests mapping polymorphism to ⊤

Fluet (RIT) T&CFA APLS 13 / 18

slide-55
SLIDE 55

Future Work

Improve precision of type- and control-flow analysis Extend type- and control-flow to System Fω Applications of type- and control-flow analysis

Fluet (RIT) T&CFA APLS 14 / 18

slide-56
SLIDE 56

Future Work

Improve precision of type- and control-flow analysis: Adapt well-known improvements to control-flow analyses:

polyvariant/context-sensitive analysis reachability/demand-driven analysis abstract reachability & abstract cardinality . . .

Fluet (RIT) T&CFA APLS 15 / 18

slide-57
SLIDE 57

Future Work

Improve precision of type- and control-flow analysis: Explore improvements motivated by well-typedness:

  • λz:τz.eb∈ ˆ

ρ(xf )

  ∀va ∈ ˆ

ρ(xa) . ˆ φ S TyOf(v) ∼ ∼ ∼ τz ⇒ va ∈ ˆ ρ(z) ∧

  • ∃va ∈ ˆ

ρ(xa) . ˆ φ S TyOf(v) ∼ ∼ ∼ τz ⇒

∀vb ∈ ˆ

ρ(ResOf(eb)) . ˆ φ S TyOf(v) ∼ ∼ ∼ τx ⇒ vb ∈ ˆ ρ(x)

ˆ φ; ˆ ρ S e ˆ φ; ˆ ρ S let x:αx = xf xa in e

if no actual arguments flow to formal argument, then function could not be called (and no flow of function result)

Fluet (RIT) T&CFA APLS 15 / 18

slide-58
SLIDE 58

Future Work

Improve precision of type- and control-flow analysis: Explore improvements motivated by well-typedness:

rec loop = Λα. λx:α. loop [α × α] (x,x) loop [Int] 1 ˆ φ(α) = { Int , α × α } ˆ φ S α ⇒ ⇒ (Int × Int) × Int

but all dynamic instantiations of α are “perfect binary trees” interpret (portions of) abstract type environment as an L-system?

Fluet (RIT) T&CFA APLS 15 / 18

slide-59
SLIDE 59

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

Fluet (RIT) T&CFA APLS 16 / 18

slide-60
SLIDE 60

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

is type compatibility decidable?

Fluet (RIT) T&CFA APLS 16 / 18

slide-61
SLIDE 61

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

is type compatibility decidable?

θ1 ≡βη θ2 is decidable for well-kinded types (by normalization) regular-tree grammar intersection/emptiness not directly applicable

Fluet (RIT) T&CFA APLS 16 / 18

slide-62
SLIDE 62

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

is type compatibility decidable?

run a control-flow analysis over types to approximate applications; reduce to regular-tree grammar intersection/emptiness, with a loss of precision and completeness (wrt. type-compatibility judgement)

Fluet (RIT) T&CFA APLS 16 / 18

slide-63
SLIDE 63

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

is type compatibility decidable?

algorithm for regular-tree grammar intersection somewhat similar to type equivalence with type definitions by weak-head normalization; combine the two algorithms in a decision procedure?

Fluet (RIT) T&CFA APLS 16 / 18

slide-64
SLIDE 64

Future Work

Extend type- and control-flow analysis to System Fω:

Kind ∋ κ ::= ⋆ | κ ⇒ κ Type ∋ τ ::= τ → τ | α | ∀α:κ. τ | λα:κ. τ | τ τ

flow soundness with modified type-compatibility judgement:

ˆ φ S τ1 ⇒ ⇒ θ1 ˆ φ S τ2 ⇒ ⇒ θ2 θ1 ≡βη θ2 ˆ φ S τ1 ∼ ∼ ∼ τ2

is type compatibility decidable?

canonizing substitution is alternative approach to type equivalence, maintaining types in a canonical β-normal/η-long form; exploit this added structure in a decision procedure?

Fluet (RIT) T&CFA APLS 16 / 18

slide-65
SLIDE 65

Future Work

Applications of type- and control-flow analysis: Flow Directed Defunctionalization of System F

compile higher-order polymorphic source language to first-order polymorphic target language type- and control-flow analysis to minimize dispatches Flow-directed Closure Conversion [Cejtin, Jagannathan, & Weeks (ESOP’00)] Polymorphic Typed Defunctionalization [Pottier & Gauthier (POPL’04)] Defunctionalizing Polymorphic Types [Midtgaard (PhD Dissertation’07)]

Fluet (RIT) T&CFA APLS 17 / 18

slide-66
SLIDE 66

Type- and Control-Flow Analysis for System F

Exploit types to obtain more precise control-flow information. Type-flow and control-flow are mutually beneficial. Sound analysis via specification-based formulation. Computable analysis via interpretation as regular-tree grammar. Efficient algorithm via flow-graph-based formulation. Many directions for future work.

Questions?

Fluet (RIT) T&CFA APLS 18 / 18