Incremental Flow Analysis 1 Introduction 2 Abstract Interpretation
Total Page:16
File Type:pdf, Size:1020Kb
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 Prolog 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 compiler 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 dynamic compilation 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