Technical Review of Peephole Technique in Compiler To Optimize Intermediate Code
Technical Review of Peephole Technique in Compiler To Optimize Intermediate Code
2013
American Journal of Engineering Research (AJER) e-ISSN : 2320-0847 p-ISSN : 2320-0936 Volume-02, Issue-05, pp-140-144 www.ajer.us Research Paper Open Access
(Department of MCA/ Atmiya Institute of Technology & Science-Rajot, India) (Department of MCA/ Atmiya Institute of Technology & Science-Rajot, India) 3 (Department of MCA/ Atmiya Institute of Technology & Science-Rajot, India) 4 (Department of MCA/ Atmiya Institute of Technology & Science-Rajot, India)
Abstract: Peephole optimization is a efficient and easy optimization technique used by compilers sometime called window or peephole is set of code that replace one sequence of instructions by another equivalent set of instructions, but shorter, faster. Peephole optimization is traditionally done through String pattern matching that is using regular expression. There are some the techniques of peephole optimization like constant folding, Strength reduction, null sequences, combine operation, algebraic laws, special case instructions, address mode operations. The peephole optimization is applied to several parts or section of program or code so main question is where to apply it before compilation, on the intermediate code or after compilation of the code .The aim of this dissertation to show the current state of peephole optimization and how apply it to the IR (Intermediate Representation) code that is generated by any programming language. Keywords: Compiler, peephole, Intermediate code, code generation, PO (peephole optimization) I. INTRODUCTION
Compilers take a source code as input, and typically produce semantically correspondent target machine instructions as output. A compiler (or a language translator) performs two operations: analysis and synthesis. The analysis phase determines the meaning of the source text, and the synthesis phase creates an equivalent program in a different language. In the case of compilers, automatic generation of language translation tools requires and facilitates understanding of language translation. Syntactic analysis especially is well understood, encompasses a sizeable formal literature, and is habitually done mechanically by programs like yacc and lex. A common method for implementing a group of languages on a collection of machines is to have one front end per language and one back end per machine. Each front end translates from its source language to a common intermediate code, called an UNCOL, and each back end translates from the common intermediate code to a specific target machine's assembly language.
Fig 1: Compiler Process Thus a "compiler" consists of a front end, a back end, and possibly programs that optimize the
www.ajer.us
Page 140
2013
intermediate code. When this model has been adopted, adding a new language or new machine only requires writing one new program (front end or back end) to maintain the property that all languages are available on all machines. The question of where to perform the optimization arises. There are three theoretical possibilities: 1. in the front ends; 2. on the intermediate code; 3. in the back ends. If the first option is chosen, many common optimizations will have to be programmed into each front end, increasing development effort. Similarly, putting the optimizations in the back ends will also require a duplication of effort. Though, any optimizations that can be performed on the intermediate code itself only need be done once, with the results being applicable to all front ends and all machines being used. The compilers task is to provide the translation. An optimal translation would ideally require minimal CPU time and memory. This is usually not possible. But optimizing compilers can approach this by improving generated code with various techniques. Therefore, code optimization aims at producing more proficient, faster, and shorter code without changing its effects. The problem with naive code generation is that resulting code often contains redundancies when juxtaposed. These can easily be reduced with an effective optimization technique peephole optimization. The peephole optimizer is repeatedly passed over the Modified assembly code, performing optimizations until no further changes are possible. Peephole optimization is an effective technique for locally improving the target code. small sequences of target code instructions are examined and replacement by faster sequences wherever possible.Common techniques applied in peephole optimization.
Fig 2: Optimization Level 1) 2) 3) 4) 5) 6) 7) 8) Constant Folding Strength Reduction Null sequences Combine Operations Algebraic Laws Special Case Instructions Address Mode Operations Copy Propagation
II.
STRATEGIES
In peephole optimization technique, this part presents a formal analysis of the pattern matching strategies that have been implemented 1.1Strategy Part
www.ajer.us
Page 141
2013
Fig 3: Peephole optimization as information processing problem The main focus of this work is the strategy part shown in Fig 3, which is divided into two parts: 1.1.1 Pattern matching strategy: This component particularly deals with the nature of the application of a single optimization rule to the input Matching strategies that have been Implemented in this work is the more primitive declarative, regular Expressions-based matching strategy, and a more abstract, object-oriented generic matching strategy. 1.1.2 Rule application strategy: For this component, the grouping and ordering of the rules is analyzed as for a certain aim. In the implementation, the backwards strategy has been employed for both the regular expressions strategy and the generic matching strategy. The general matching policy has been extended with the ability to cascade rules, i.e. to control a number of rule application sequences.
www.ajer.us
Page 142
2013
xx = EQ, NE, LT, LE, GT, and GE, but fact is that many more are needed, since a entire set is needed for singleprecision integers, pointers,floating-point numbers, double precision integers, unsigned integers, sets, etc. To avoid this large number, EM has one compare instruction for each data type that pops two operands and replaces them with a -1, 0, or +1, depending on whether the first is greater than, equal or less than the second. Then there are six Txx instructions for replacing this number with true (represented by 1) or false (represented by 0), it is based on the relational operator.
V.
MEASURED RESULT
To measure the effect of the peephole optimizer, we have run two tests. In the first we compared the number of machine instructions in each optimized EM program with the number in the unoptimized EM program. Thus, for each program we have a number between 0.00 and 1.00 giving the number of instructions in the optimized program as a fraction of the original. This metric was chosen since it is independent of both the source language and the target machine and directly measures what the optimizer primarily attempts to do, namely, eliminate EM instructions. This metric can also be protected on theoretical grounds. EM code is really just glorified reverse Polish, in other words, the parse tree linearized into postfix order. Removal of an EM instruction typically corresponds to a removal of a node in the parse tree. Since object code size is typically proportional to parse tree size, such elimination normally has a straight impact on the final object code size. The dimensions presented below bear this out. The occurrence frequencies per 1000 optimizations are shown in Table III which is labeled column EM. The median saving is 16 percent: one in six EM instructions is eliminated. The second test consisted of translating the optimized and unoptimized EM code into PDP-11 object code and comparing the number of bytes produced in each case. These results are given in Table III in the column labeled PDP-11. The median reduction in the object code is 14 percent, close to the EM result. This closeness suggests that nearly all the EM optimizations are indeed reflected in the final object code. In 2 test programs, the optimized PDP-11 code was increased by 2 percent over the unoptimized code due to optimization 50; this was traced to a design error in the (original) EM to PDP-11 back end. (With the optimization the operands were actually stacked, whereas without it they were not.) This fault can merely be fixed, however. On the basis of these results, we believe peep holing the intermediate code to be worthwhile, since the optimizer need only be written once, for all languages and every machines, and it in no way inhibits extra, more sophisticated optimizers, e1ither on the source code, on the EM code, or on the target code. Moreover, the peephole optimizer is fast: 1140 EM instructions per CPU second on a PDP-11/ 45 excluding certain overhead not related to peephole optimization and 650 instructions per CPU second including all overhead. This speed was achieved without any special effort to tune the program. It could easily be made faster still by hashing the pattern table instead of examining all patterns starting with the current op code TABLE 4: DISTRIBUTION OF AMOUNT OF REDUCTION IN SIZE
www.ajer.us
Page 143
2013
REFERENCE
[1]. [2]. [3]. [4]. [5]. [6]. [7]. [8]. [9]. [10]. [11]. [12]. [13]. [14]. [15]. [16]. McKee man, W. M. (1965) Peephole optimization. CACM 8(7):443-444. Lamb, D. A. (1981) Construction of a Peephole Optimizer. Software-Practice & Experience 11(6):639 647 Fraser, C. W. (1982) Copt, a simple, retargetable peephole optimizer. Software, available at ftp://ftp.cs.princeton.edu/pub/lcc/contrib/ copt.shar (up 24/02/2005). Davidson, J. W., Fraser, C. W. (1980) The design and application of a retargetable peephole optimizer. ACM TOPLAS 2(2):191 202. Fraser, C. W. (1979) A compact, machine-independent peephole optimizer. POPL79:1-6. Tanenbaum, A. S., van Staveren, H., Stevenson, J. W. (1982) Using Peephole Optimization on Intermediate Code. ACM TOPLAS 4(1):21-36. Davidson, J. W., Fraser, C. W. (1987) Automatic Inference and Fast Interpretation of Peephole Optimization Rules. Software Practice & Experience 17(11):801-812. Davidson, J. W., Fraser, C. W. (1984) Automatic generation of peephole E optimizations. CC84:111-116. Kessler, P. B. (1986) Discovering machine-specific code improvements.CC86:249-254. Kessler, R. R. (1984) Peep - An architectural description driven peephole optimizer. CC84:106-110. STEEL, T.B., JR. UNCOL: The myth and the fact. Annu. Rev. Autom. Program. 2 (1960), 325-344. Warfield, J. W., Bauer, III, H. R. (1988) An Expert System for a Retargetable Peephole Optimizer. ACM SIGPLAN Notices 23(10):123130. Davidson, J. W., Fraser, C. W. (1984) Register allocation and exhaustive peephole optimization. Software - Practice & Experience 14(9):857-865. Davidson, J. W., Fraser, C. W. (1984) Register allocation and exhaustive peephole optimization. Software - Practice & Experience 14(9):857-865. Aho, A. V., Ganapathi, M., Tjiang, S. W. K. (1989) Code Generation Using Tree Matching and Dynamic Programming. ACM TOPLAS11(4):491-516. Fraser, C. W., Wendt, A. L. (1986) Integrating Code Generation and Optimization. Proceedings of the SIGPLAN86 symposium on Compiler Construction, pp. 242 248.
www.ajer.us
Page 144