Incremental Flow Analysis

Andreas Krall and Thomas Berger

Institut fur Computersprachen

Technische Universitat Wien

Argentinierstrae

A Wien

fanditbgmipscomplangtuwienacat

Abstract

Abstract interpretation is a technique for ow analysis widely used in the area of

logic programming Until now it has b een used only for the global compilation of

Prolog programs But the programming language enables the programmer to

change the program during run time using the builtin predicates assert and retract

To supp ort the generation of ecient co de for programs using these dynamic database

predicates we extended abstract interpretation to b e executed incrementally The aim

of incremental abstract interpretation is to gather program information with minimal

reinterpretation at a program change In this pap er we describ e the implementation

of incremental abstract interpretation and the integration in our VAM based Prolog

P

Preliminary results show that incremental global compilation can achieve

the same optimization quality as global compilation with little additional cost

Intro duction

Since several years we do research in the area of implementation of logic programming lan

guages To supp ort fast interpretation and compilation of Prolog programs we develop ed

the VAM Vienna Abstract Machine with two instruction p ointers Kra KN and

P

the VAM Vienna Abstract Machine with one instruction p ointer KB Although

P

the VAM manages more information ab out a program than the WAM Warren Abstract

Machine War it is necessary to add ow analysis for typ e inference and reference

chain length calculation Extending our global VAM compiler by supp ort for dynamic

P

database predicates it b ecame apparent that the data structures necessary for storing

the information for were sucient to integrate incremental global

abstract interpretation

In chapter we present our approach to global data ow analysis and show how it can

b e solved incrementally Chapter contains details of our prototyp e implementation and

chapter presents some preliminary results

Abstract Interpretation

Abstract interpretation is a technique of describing and implementing global ow analysis

of programs It was intro duced by CC for data ow analysis of imp erative languages any

bound free

atomic list struct

nil atom integer

Figure Abstract domain for typ e and mo de inference

This work was the basis of much of the recent work in the eld of logic programming AH

Bru Deb Mel RD Tay Abstract interpretation executes programs over

an abstract domain Recursion is handled by computing x p oints To guarantee the

termination and completeness of the execution a suitable choice of the abstract domain is

necessary Usually the elements of this domain form a lattice

Global Abstract Interpretation

We do gather information ab out mo de typ e and reference chain length of variables Trail

ing information is not handled by the abstract interpreter b ecause the VAM do es trail more

seldom than the WAM Fig describ es the lattice of the abstract domain typ es Each

typ e represents a set of terms

any is the top element of the domain

free describ es an unb ound variable and contains a reference to all aliased variables

b ound describ es nonvariable terms

atomic is the sup ertyp e of nil atom and integer

list is a non empty list it contains the typ es of its arguments

struct is a structure information ab out the functor and arguments is contained

nil represents the empty list

atom is the set of all atoms

integer is the set of all integer numb ers

Possible innite nesting of comp ound terms makes the handling of the typ es list and

struct dicult Therefore interpretation stops after the allowed limit of nested comp ound

terms is reached To gather useful information ab out recursive data structures a recursive

typ e is intro duced which contains also the information ab out the termination typ e The

typ e listintrecnil describ es the typ e list of integer

Each typ e of the domain additionally contains information ab out the reference chain

length Valid values for this length are and unknown A length of eg will b e

extracted during the unication of a new variable the rst o ccurrence and a constant

term is typical for the unication of two dierent new variables the compiler generates

rstvar which generates a free cell on the copy stack and makes b oth the macro rstvar

variables a reference to that cell If the length of the reference chain is not decidable its

length information gets the value unknown

We use a topdown approach for the analysis to extract the desired information Dif

ferent calls to the same clause are handled separately to get the exact typ es The gathered

information is a description of the living variables Recursive calls of a clause are com

puted until a x p oint for the gathered information is reached If a call is computed the

second time there exists already information ab out this call Now the old information is

compared with the new one and the interpretation stops for this clause at this p oint if

the new information is more sp ecial ie the union of all the old typ es and the new typ es

is equal to the old typ es The gathered information has reached a x p oint

Incremental Abstract Interpretation

In most Prolog systems the mo dication of the database leads to a mixture of interpreted

and compiled co de Optimization information is not available for dynamically asserted

clauses In some systems it is not allowed to assert clauses for a predicate which is not

declared to b e dynamic We wanted to supp ort dynamic database predicates without any

loss of optimization information The idea of incremental abstract interpretation is to

get this information with minimal reinterpretation Changing the database by asserting

or retracting clauses causes a lo cal analysis of all pro cedures whose variable domains are

changed In the worst case the assertion of a new clause can take eect in the abstract

reinterpretation and recompilation of the whole program

To supp ort incremental abstract interpretation it is necessary to store additional in

formation with the compiled co de in the co de area This information consists of

 the intermediate co de of all clauses

 description of variables and their domains

 table containing the entry p oints for the clause co des

 table containing the callers parents of a clause

Incremental abstract interpretation starts the lo cal analysis with all the callers of

the mo died pro cedure and interpret the intermediate co de of all dep endent pro cedures

Interpretation is stopp ed if the new domains are equal to the domains derived by the

previous analysis If the domain has not changed new co de is generated only for the

changed part of the program If the domain has b een changed and the new domain is a

subtyp e of the old domain the previously generated co de ts for the changed program

part The information derived for the program b efore the change is less exact than the

p ossibly derivable information Incremental interpretation can stop now If the new

domain is instead a sup ertyp e of the old one the assertion of the new clause made the

p ossible information less precise and the old co de wrong for the new program Therefore

incremental abstract interpretation must further analyse the callers of the clause

The retraction of a clause has a similar eect as the assertion of a new one The only

dierence is that there cannot b e an information loss which makes the previously generated

co de of the callers of the calling clauses wrong It is sucient to start the incremental

analysis with the calling clauses of the changed pro cedure and interpret topdown until

the derived information is equal to the previously inferred one

Implementation

Our prototyp e compiler is implemented in Prolog To make incremental abstract interpre

tation p ossible some extensions to the Prolog interpreter were necessary We implemented

two new builtinpredic ates One assembles a list of symb olic assembly language instruc

tions pro duced by the compiler and saves the machine co de in the co de area The second

builtin calls an assembled program and after completion returns to the interpreter

The compilation pro cess comprise the following steps

 read a clause and compile it into intermediate co de

 incrementally execute abstract interpretation

 compile all changed clauses to VAM co de

P

 transform the VAM instructions into assembly language instructions

P

 p erform instruction scheduling

 assemble the instructions and save the instructions in the co de area

 up date branch instructions connect the new co de to the older one

Our intermediate co de is based on VAM co de enriched by information describing the

P

abstract domain of variables This approach is similar to the compiling data ow analysis

of TL with the dierence that our intermediate co de is not based on the WAM and

our abstract VAM interpreter is implemented in Prolog The gathered information is

P

used for

 reducing the co de for dereferencing variables

 eliminating tag checkings

 eliminating the unication co de or the copy co de handling structure arguments

 eliminating parts of the co de for clause indexing

 eliminating unnecessary choice p oints

To p erform the incremental interpretation some information ab out the compiled pro

gram has to b e available The intermediate co de is stored in the database at run time so

that it can b e used by the incremental interpretation For the entry p oints of all p ossible

calls to all clauses the following information is stored in a table

 the start address of the compiled co de in memory

 the addresses of all start p oints of the pro cedures

 the addresses of all branches to pro cedures

Recompilation after incremental analysis assembles the new pro duced co de to a free

part in the co de area and mo dies the branches of the unchanged co de to branch to the

start address of the called recompiled clauses

run time compile time

b enchmark not optimized optimized static incremental

ms scaled ms scaled

detapp end

naive reverse

quicksort

queens

serialize

dierentiate

query

bucket

p ermutation

Table run time and compilation time for the b enchmarks

Results

Preliminary results show that incremental abstract interpretation can collect the same

information as global abstract information

We compiled a set of small b enchmarks on a DecStation MHz MIPS

R pro cessor A description and the source co de of the b enchmarks can b e found

in Bee All b enchmarks were compiled without abstract interpretation with static

abstract interpretation and incrementally with abstract interpretation see Tab

Computation times were measured by executing each b enchmark b etween and

times Tab shows that abstract interpretation made p ossible a sp eedup b etween

and p ercent Eg the query b enchmark executes times faster than without

abstract interpretation

Tab compares also the compilation times Incremental compilation needs b etween

two and ve times as much time as static compilation for the b enchmarks For incremental

compilation each clause of the program was read by the compiler compiled to intermediate

co de incrementally interpreted over the abstract domain all already compiled clauses of

the program whose variable domains were changed through the new clause were recom

piled the co de scheduled and assembled into memory The new intermediate co de and

the table containing the branch addresses were asserted as a fact to the database

Tab compares the co de sizes pro duced by the incremental and the static metho ds

Due to the reduction of dereferencing and tag checking and rened constant indexing

the optimized programs needs only to p ercent of the size of the unoptimized one

Incremental compilation increases the sizes b ecause we have no garbage collector for the

co de area where the incrementally compiled co de is placed

Further Work

The prototyp e implementation has demonstrated that it is feasible to supp ort abstract

interpretation for Prolog programs using dynamic database predicates To enhance the

size

b enchmark not optimized static incremental

byte scaled scaled

detapp end

naive reverse

quicksort

queens

serialize

dierentiate

query

bucket

p ermutation

Table sizes of the compiled programs

sp eed of our incremental compiler and to reduce the size of the data structures we will

implement the incremental compiler in The incremental abstract interpreter will exe

cute mo died VAM co de The integration of the incremental compiler in the run time

P

environment enables the execution of interpreted VAM co de and incrementally compiled

P

VAM co de

P

Acknowledgement

We express our thanks to Anton Ertl Ulrich Neumerkel and Franz Puntigam for their

comments on earlier drafts of this pap er

References

AH Samson Abramsky and Chris Hankin editors Abstract Interpretation of Declar

ative Languages Ellis Horwo o d

Bee Joachim Beer Concepts Design and Performance Analysis of a Paral lel Prolog

Machine Springer

Bru Maurice Bruyno oghe A practical framework for the abstract interpretation of

logic programs Journal of Logic programming

CC Patrick Cousot and Radhia Cousot Abstract interpretation A unied lattice

mo del for static analysis of programs by construction or approximation of x

p oints In Fourth Symp Priciples of Programming Languages ACM

Deb Saumya Debray A simple co de improvement scheme for Prolog Journal of Logic

Programming

KB Andreas Krall and Thomas Berger Fast Prolog with a VAM based Prolog

P

compiler In PLILP LNCS Springer

KN Andreas Krall and Ulrich Neumerkel The Vienna abstract machine In PLILP

LNCS Springer

Kra Andreas Krall Implementation of a highsp eed Prolog interpreter In Conf on

Interpreters and Interpretative Techniques volume of SIGPLAN ACM

Mel Christopher S Mellish Some global optimizations for a Prolog compiler Journal

of Logic Programming

RD Peter Van Roy and Alvin M Despain Highp erformance logic programming with

the Aquarius Prolog compiler IEEE Computer

Tay Andrew Taylor Removal of dereferencing and trailing in Prolog compilation In

Sixth International Conference on Logic Programming Lisb on

TL Jichang Tan and IPeng Lin Compiling dataow analysis of logic programs In

Conference on Programming Language Design and Implementation volume

of SIGPLAN ACM

War David HD Warren An abstract Prolog instruction set Technical Note SRI

International