Interprocedural Control Flow Analysis of First-Order Programs with Tail Call Optimization

Interprocedural Control Flow Analysis of First-Order Programs with Tail Call Optimization

Interpro cedural Control Flow Analysis of First-Order Programs with Tail Call Optimization Saumya K. Debray and To dd A. Pro ebsting Department of Computer Science The University of Arizona Tucson, AZ 85721, USA. Email: fdebray, [email protected] Technical Rep ort 96-20 Decemb er 2, 1996 Abstract The analysis of control ow Involves guring out where returns will go. How this may b e done With items LR-0 and -1 Is what in this pap er we show. The work of S. Debraywas supp orted in part by the National Science Foundation under grantnumb er CCR-9502826. The work of T. Pro ebsting was supp orted in part by NSF Grants CCR-9415932, CCR-9502397, ARPA GrantDABT63-95-C-0075, and IBM Corp oration. 1 Intro duction Most co de optimizations dep end on control ow analysis, typically expressed in the form of a control ow graph [1]. Traditional algorithms construct intrapro cedural ow graphs, which do not account for control owbetween pro cedures. Optimizations that dep end on this limited information cannot consider the b e- havior of other pro cedures. Interpro cedural versions of these optimizations must capture the owofcontrol across pro cedure b oundaries. Determining interpro cedural control ow for rst-order programs is relatively straightforward in the absence of tail call optimization, since pro cedures return control to the p oint imme- diately after the call. Tail call optimization complicates the analysis b ecause returns may transfer control to a pro cedure other than the active pro cedure's caller. The problem can b e illustrated by the following simple program that takes a list of values and prints, in their original order, all the values that satisfy some prop erty, e.g., exceed 100. To take advantage of tail-call optimization, it uses an accumulator to collect these values as it traverses the input list. However, this causes the order of the values in the accumulator to b e reversed, so the accumulated list is reversed|again using an accumulator|b efore it is returned. 1 mainL = print extractL, [] 2 extractxs, acc = 3 if xs = [] then reverseacc, [] 4 else if hdxs > 100 then extracttlxs, conshdxs,acc 5 else extracttlxs, acc 6 reversexs, acc = 7 if xs = [] then acc 8 else reversetlxs, conshdxs,acc Supp ose that, for co de optimization purp oses, wewant to construct a ow graph for this entire program. The return from the function reverse in line 7 corresp onds to some basic blo ck, and in order to construct the ow graph we need to determine the successors of this blo ck. The call graph of the program indicates that reverse can b e called either from extract, in line 3, or from reverse, in line 8. However, b ecause of tail call optimization, it turns out that reverse do es not return to either of these call sites. Instead, it returns to a di erent pro cedure entirely, namely, to the pro cedure main in line 1: the successor blo cktothe return in line 7 is the basic blo ck that calls print. Clearly, some nontrivial control ow analysis is necessary to determine this. Most of the work to date on control ow analysis has fo cused on higher-order languages: Shivers [17,18] and Jagannathan and Weeks [7] use abstract interpretation for this purp ose, while Heintze [4] and Tang and Jouvelot [19,20] use typ e-based analyses. These analyses are very general, but very complex. Many widely used languages, such as Sisal and Prolog, are rst-order languages. Furthermore, even for higher-order languages, sp eci c programs often use only rst-order constructs, or can have most higher-order constructs removed via transformations such as inlining and uncurrying [21]. As a pragmatic issue, therefore, we are interested in \ordinary" rst-order programs: our aim is to account for interpro cedural control ow in such programs in the presence of tail call optimization. To our knowledge, the only other work addressing this issue is that of Lindgren [9], who uses set-based analysis for control ow analysis of Prolog. Unlike Lindgren's work, our analyses can maintain context information see Section 6. The main contribution of this pap er is to showhow control ow analysis of rst-order programs with tail call optimization can b e formulated in terms of simple and well-understo o d concepts from parsing theory.In particular, we show that context-insensitive, or zeroth-order, control ow analysis corresp onds to the notion of FOLLOW sets in context free grammars, while context-sensitive, or rst-order, control ow analysis corresp onds to the notion of LR1 items. This is useful, b ecause it allows the immediate application of well-understo o d technology without, for example, having to construct complex abstract domains. It is also esthetically pleasing, in that it provides an application of concepts suchasFOLLOW sets and LR1 items, whichwere originally develop ed purely in the context of parsing, to a very di erent application. The remainder of the pap er is organized as follows. Section 2 intro duces de nitions and notation. Section 3 de nes an abstract mo del for control ow, and Section 4 shows how this mo del can b e describ ed using 1 context free grammars. Section 5 discusses control ow analysis that maintain no context information, and Section 6 discusses how context information can b e maintained to pro duce more precise analyses. Section 7 illustrates these ideas with a nontrivial example. Section 8 discusses tradeo s b etween eciency and precision. In order to maintain continuity, the pro ofs of theorems have b een relegated to the app endix. 2 De nitions and Notation We assume that a program consists of a set of pro cedure de nitions, together with an entry p oint pro cedure. It is straightforward to extend these ideas to accommo date multiple entry p oints. Since we assume a rst-order language, the intrapro cedural control ow can b e mo delled by a control ow graph [1]. This is a directed graph where each no de corresp onds to a basic blo ck, i.e., a maximal sequence of executable co de that has a single entry p oint and a single exit p oint, and where there is an edge from a no de A to a no de B if and only if it is p ossible for execution to leavenode A and immediately enter no de B . If there is an edge from a no de A toanodeB, then A is said to b e a predecessor of B and B is a successor of A. Because of the e ects of tail call optimization, interpro cedural control ow information cannot b e assumed to b e available. Therefore, we assume that the input to our analysis consists of one control ow graph for each pro cedure de ned in the program. For simplicity of exp osition, we assume that each ow graph has a single entry no de. Each ow graph consists of a set of vertices, which corresp ond to basic blo cks, and a set of edges, which capture control ow between basic blo cks. If a basic blo ck contains a pro cedure call, the call is assumed to terminate the blo ck; if a basic blo ck B ends in a call to a pro cedure p,wesay that B cal ls p. If the last action along an execution path in a pro cedure p is a call to some pro cedure q |i.e., if the only action that would b e p erformed on return from q would b e to return to the caller of p|the call to q is termed a tail cal l. A tail call can b e optimized: in particular, anyenvironment allo cated for the caller p can b e deallo cated, and control transfer e ected via a direct jump to the callee q ; this is usually referred to as \tail call optimization," and is crucial for ecient implementations of functional and logic languages. If a basic blo ck B ends in a tail call, wesay that it is a tail cal l block;ifB ends in a pro cedure call that is not a tail call, wesayB is a cal l block. In the latter case, B must set a return address L b efore making the call: L is said to b e a return label. If a basic blo ck B ends in a return from a pro cedure, it is said to b e a return block. As is standard in the program analysis literature, we assume that either branch of a conditional can b e executed at runtime. The ideas describ ed here are applicable to programs that do not satisfy this assumption; in that case, the analysis results will b e sound but p ossibly conservative. The set of basic blo cks and lab els app earing in a program P are denoted by Blo cks and Lab els P P resp ectively. The set of pro cedures de ned in it is denoted by Pro cs . Finally, the Kleene closure of a set P S , i.e., the set of all nite sequences of elements of S , is written S . The re exive transitive closure of a ? relation R is written R . 3 Abstracting Control Flow Before we can analyze the control ow b ehavior of such programs, it is necessary to sp ecify this b ehavior carefully.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us