A direction-independent algorithm for determining the forward and backward compute point for a term or subscript during compilation By Joseph T. Ryan' This paper describes a second-scan procedure for determining the forward and backward compute points for all of the elements of subscripted variables. Here, the forward and backward compute points are the earliest time at which a subscript can be computed and the latest point at which it can still be used. The procedure is independent of the direction of the second scan. (The first scan is assumed to be a forward read of the source program.) The subscripted variables may occur at any level within the nesting of DO-type loops. The optimality and collapsibility of loops is unaffected by the algorithm. Definitions In order to clarify the terminology used in this paper, the following definitions are made: Until recently most compilers interpreted the sourcelanguage input file just to sequence and categorize the source statements which were to be stored in memory or to be put onto an intermediate file for later use in the compilation. Each source statement was carefully dissected in accordance with the appropriate syntactical and orthographical rules but little or no attention was given to the flow analysis of the source program until after the source file was entirely read. Some more recent compilers have been advertized as "one-pass" compilers, but these, for the most part, have been inefficient because the program flow analysis and the compute point determination for subscripted variables have not been oriented in a mutually sequential fashion. Several subpasses were required to orient the two operations and this increased rather than decreased the compilation time. Further, the optimality and collapsibility of loops were determined to be virtually impossible (see Flores, 1965). It is possible, however, to at least provide (conveniently) both the syntax analysis and program flow analysis during the first pass. This is because the BEGIN DO's and END DO's (and their associated nest levels) occur sequentially according to prescribed rules. The output file from this first phase can be read backward to perform the necessary subscript analysis and compute point determination; alternatively the output file can be rewound (in the case of a tape file) and the subscript analysis and compute point determination can be combined with the code generation and preassembly edit phase of the compilation. This paper presents an algorithm that can be used for determining the compute point for terms and subscripts which allows for either contingency; i.e. the algorithm is independent of the direction of the second pass. Further, the optimality and collapsibility of loops is unaffected by the algorithm. DEFINITION 1. The forward and backward compute points are the sequence numbers associated with the earliest point at which a subscript can be computed and the latest point at which it can still be used, respectively. 2. A loop is called unsafe if there is a transfer into the loop from a point in the source program at level zero as well as a transfer from within the loop out to level zero; otherwise, the loop is called safe. DEFINITION 3. An inner loop is optimal with respect to the next outer loop if every execution of the outer loop specifies the number of times that the inner loop is executed. The following two segments of code are samples of an optimal and a non-optimal loop-pair, respectively. DEFINITION Temp l=3A,+ 4Aj 1 DO pj = Aj,Bj, TX)<xI = Af,B,,C, r Subscript Occurrence (3/ + 4/) a CONTINUE '•— Temp 1 = Temp 1 j8 CONTINUE Temp 1 = Temp 1 Optimal • Looppair + 4 C , - X*3C, J (Note: X — Number of times inner loop is executed per outer loop execution.) 'Research Triangle Institute, Durham, North Carolina. 157 Compilation algorithm Temp 1 = AAj J Temp 2 = 3Ar + Temp 1 DO a / = Aj, B,, NonSubscript Occurrence optimal (3/+4/) Looppair a CONTINUE — Temp 2 = Temp 2 j8 CONTINUE Temp 1 = Temp 1 DEFINITION 4. A loop-pair is collapsible if the following conditions hold: (1) the loop-pair is optimal, (2) the two loops coincide, (e.g., DO a / = A,, Bi, C, DO a / = Aj, Bj, Cj a CONTINUE (3) The inner loop is incremented to the exact point where it is also correct for the outer increment, i.e., where Temp 1 + 4 Q - X * 3C, = 0 (1) in the first example of DEFINITION 3 above. DEFINITION (1) (2) (3) (4) (5) 5. A term is any one of the following: relative constant subscript, initial value, increment value, test value, subscripted variable. DEFINITION 6. The level associated with a given point in the source program is equal to the number of BEGIN DO's less the number of END DO's preceding the given point in the source program. 7. The plateau associated with a given point in the source program is equal to the number of BEGIN DO's and END DO's preceding the given point in the source program. DEFINITION 8. A unique sequence number is transmitted to the interscan file for each item put on the file during the first scan through the source program. In the context of this paper, sequence numbers will increase in the forward direction through the source program. DEFINITION 158 The algorithm This section describes the algorithm in detail. Flow charts of the algorithm together with a legend defining the symbols used in the flow charts are presented. For simplicity, FORTRAN terminology is used throughout the discussion. The technique, however, is applicable to any FORTRAN-type algebraic compiler. The algorithm described is invoked at each occurrence of a term during the scan. If all of the elements comprising the term (we shall call these elements variables) are at compute level 0, then the forward compute point for the term is set to zero and the backward compute point for the term is set to infinity; otherwise, the algorithm will be used to determine these compute points. Since the flow diagrams of Figs. 1 and 2 embody the scheme which is described here, references to boxes in the flow charts will be made in this section when necessary. The algorithm begins by initializing the forward and backward compute points to zero and to infinity, respectively (Box 1, Fig. 1). Next, all of the variables comprising the term are tabulated and listed. The individual variables are designated V; (i = 1 , 2 , . . . , KT), where KT is the number of such variables (Boxes 2 and 3, Fig. 1). Box 4 of Fig. 1 contains the only decision junction based on the direction of the scan. The previous sequence number of an occurrence of each of the variables that comprise the term is used to provide a first estimate for the forward and backward compute points. If these points are modified on the basis of previous variable occurrences, the modifications are made in Boxes 7 and 13 (Fig. 1). Occurrences of variables in EQUIVALENCE, COMMON variables, and CALL'S next influence the compute points. If the forward compute point is improved on the basis of the sequential order of such occurrences, the changes are made in Boxes 13 through 17 (Fig. 1). If the backward compute point is improved on the basis of such occurrences, the changes are made in Boxes 7 through 12 (Fig. 1). The two basic rules used in this stage of the algorithm are as follows: (1) If the source program contains no variables in both COMMON and EQUIVALENCE, then a variable in EQUIVALENCE will redefine all variables in EQUIVALENCE and each COMMON variable is redefined at all CALL'S. (2) If the source program contains at least one variable in both COMMON and EQUIVALENCE, a COMMON variable will redefine all of EQUIVALENCE, and CALL'S will redefine all of COMMON. (The algorithm could be made simpler—but less efficient—if COMMON variables also redefine all of COMMON.) The forward compute point then is improved further and the compute level is determined on the basis of the flow analysis which was performed during the first scan. Theflowdiagram for this is shown in Fig. 2. In Box 18, level and plateau counters are initialized. Then, based Compilation algorithm FCPT - 0 BCP- - - List the terminal variables comprising Term T and Denote then v i <1-1-2 V : SEQ(P')? HZ) 19 Is the DO loop f o r \ plateau P1 safe? I As the scaA I forward? J No SEQ(P') I Yes • No • 13 ( PSV^KCPT Is P' a s s o c i a t e d \ with a BEGIN DO? J 11 SEQ(P') < FCPT? BED - ^ B C P T 1 L pn No -1 BCP Is the DO loop for plateau P " safe? \ J f BED?I 22 23 C L T=L' F' —>FCP_ FCP = point just prior to SEQ(P') Clj. - V - 1 Yes I Is the DO loop for plateau P' safe? < FED?] No 17 OJ FED -s. FCPT N Fig. 1.—Flow chart of algorithm to determine preliminary compute points for terms ° f SEQ(P') <FCP T ? J Fig. 2.—Flow chart of algorithms for improving the forward compute point and determining the compute level on the basis of first scan flow analysis 159 Compilation algorithm on the current nest level and the sequence numbers associated with the next outer BEGIN DO, a new forward compute point is determined. This determination as well as the compute level also depends on whether or not each of the loops nesting the current variable occurrence are safe loops (Boxes 19, 20, and 21, Fig. 2). The forward compute point and compute level are finally prescribed in either Box 22 or 23 (Fig. 2). At the occurrence of labels (in the FORTRAN sense) all level-dependent subscripts and terms whose computed level equals the current level will be put out with a compute sequence equal to that of the label. BCP r = Backward compute point for term T (sequence number) BED = Backward EQUIVALENCE definition (sequence number) BPL = The plateau of the BEGIN DO associated with level L I = > V is in COMMON 0 = > V is not in COMMON 1 = > at least 1 variable is in COMMON or EQUIVALENCE 7. CEFLAG = 0 = > no variable is in COMMON or EQUIVALENCE CL r = Compute level for term T CSj/ = Sequence number of current occurrence of variable V 1 = > V is a variable in EQUIVALENCE fcv = > y j s n o t a v a r i a b l e in EQUIVALENCE 11. F' = Temporary storage slot 12. FCD = Forward COMMON definition (sequence number) 13. FCO = Forward CALL occurrence (sequence number) 14. FCP r = Forward compute point for term T (sequence number) 15. FED = Forward EQUIVALENCE definition (sequence number) K r = The number of terminal variables comprising 16. termT 17. L = Current nest level L' = Temporary storage slot 18. Plateau of END DO if P is plateau of BEGIN DO of same loop 19. Lp = Linked plateau = Plateau of BEGIN DO if P is plateau of END DO of same loop Np = Plateau of the BEGIN DO of the next outer 20. loop 21. P = Plateau (associated with a BEGIN or END DO) 22. P' = Temporary storage slot 23. P" = Temporary storage slot 24. PS,/ = Sequence number of previous occurrence of variable V 25. SEQ (P) = Sequence number of plateau P Summary and conclusions The algorithm described above was implemented in an experimental FORTRAN-type compiler, FTNX-I, written for the UNIVAC 1107 computer. The algorithm comprises the second pass of the compiler. In the few tests which have been made to date (object program decks of up to 150 cards with as many as twenty subscripted variables), the compiled programs were the same whether the second pass read the output file from the first pass backwards or whether the output tape from the first pass was rewound and read in a forward manner. Confirmation of the efficiency of the algorithm is presently being considered. A mathematical proof of the optimality of the program, with respect to minimizing the number of times that subscripts must be computed, is impossible since there are an infinite number of possible loop configurations within a program. The standard criteria which prescribe the "best" placement of subscript compute points are used by the algorithm (see Hill, Langmaack, Schwarz, and Seegmuller, 1962). Symbols used in Figs. 1 and 2 1. BCD = Backward COMMON definition (sequence number) 2. BCO = Backward CALL occurrence (sequence number) References FLORES, I. (1965). Computer Software: Programming Systems for Digital Computers, New York: Prentice-Hall. HILL, U., LANGMAACK, H., SCHWARZ, H. R., and SEEGMULLER, G. (1962). "Efficient Handling of Subscripted Variables in ALGOL 60 Compilers," Proc. 1962 Rome Symposium on Symbolic Languages in Data Processing, New York: Gordon and Breach Publishing Co. 160
© Copyright 2026 Paperzz