You are on page 1of 4
Local optimization of Three-Address-Code Baptiste Wicht — 2012-02-08 08:55 — 2 Comments Some compilers ate using ‘Three-Address-Code (TAC) as an intetmediate representation, This representation is ‘very simple to understand and write, Moreover, i's easy to run some optimization on this representation, Each TAC statement has this general form : result = operand operator operand For example, here are some TAC statements: aet xsats if x > a goto test param "adda" call print param “asd call print In this post, we will see some of the local optimizations that can be applied on TAC. A local optimization is an optimization that is applied locally to a basie block. A basic block is a set of TAC statements that has only one entry point and one exit point. Once the first instruction of the basic block is executed, the rest of the instructions are necessarily executed exactly once. ‘These optimizations are easy to design and implement. If you want to un global optimizations (through all the basic blocks of a function) or even Interprocedural Optimization (IPO), you will need a far more complex framework to run optimizations. I will try to write something on global optimization when I will have implemented some of them in EDDI ‘The goal of optimization is of course to replace some statements with more efficient statements, ‘The list presented in this post is not exhaustive, this is only the optimizations that I've implemented in EDDIC, but this represent most of the local optimizations “The first three optimization techniques can be applied independently on each statement of the program. 1. Arithmetic Identities ‘The first optimization is about arithmetic identities. There are some properties in math that we ean use to simplify simple TAC statement Here are all the identities that are simplified in EDDIC: +00rx=0+a=>x=a a-0=>x=a > x store (1=>xFa@ Saf => esa fa=>x=0 =a/a=>x= RRRR RR RRR RR All the expressions on the right are more efficient to compute than the one on the left. 2. Reduce in strength Another easy optimization is the reduction of strength of some math operations. For example, an addition is cheaper than muliplication and multiplication is cheaper than division. If your language does not have floating point math, the only reduction that can be done is this one: Here are all the identities that are simplified in EDDIC: © x= 2*a0rxsa*2=>x=ata ‘With floating point math, we can do a litle better: © xSa/2=>x=0*05 a/4a> x= at 025 * ete. 3. Constant folding When both operands on the right side of the TAC statement are integers, we ean replace the math operation directly by the result of the computation, ‘With a and b being any integer, we can transform these TAC statements: 42 ee S5-15>K=2 *Qe>K=6 /2=>%=2 %2 We can also use this optimization to simplify conditional jumps. For example, (3 > 2 ofa B2 can be replaced by goto B2. More than being way more efficient statements, it also enables other optimization to be performed on the TAC program. ‘The next two optimizations cannot be made on each statement independently. ‘They have to be made on each basic blocks. They are replacing some variables by other variables or values 4, Constant propagation This optimization consists in replacing. variable by its constant value at each place we know it's constant, For example, this basic block: can be optimized into: 2 bect2 5 sab czat2 Two use of a variable have been replaced by its value, In this ease, we cannot replace the last use of a because we do not know its value there. ‘This optimization can be made using a simple algorithm on each statement of a basic block: 1. Ifthe statement is of the form + = constant, efx] = constant 2. If the statement is of the form x = a + b,«/x] = mull 3, For each variable appearing in an operand of a statement, if ¢{x] is not null, replace the variable by c[x] 5. Copy propagation Copy propagation is almost the same as constant propagation, We replace a variable by the variable it refers to. For example, we can optimize: erate “The algorithm is the same as the one for constant propagation but we keep track of the variables that are assigned to a variable. ‘These two optimizations does not create a more efficient code, but the optimized code can be optimized again, 6. Remove assign We can often find some assigns that are useless in a basic blocks. There are three types of assigns that can be removed: 1.2 = ais never useful 2. An assignment to a temporary variable that is not used after this assignment is not useful. A temporary is a variable created by the compiler to perform some complex expressions, They are not stored and are used only within one basic block, so its value is not useful after the basic block. 3. An assignment to x following another assignment to x, with no use of x between the two assignments, is not useful 7, Remove temporaries ‘When compiling some expression, we often generate this kind of code: date xed ‘We can simplify this code into: x=a+b For that optimization, we can apply a mote complex propagation. For each assignment to a temporary, we store the right side of the assignment. Then, for each assignment of a temporary to another variable (x = £1) if this use of the temporary is the only one, we replace the right side by the right side of the assignment to the temporary. 8, Remove needless jumps We can also find some jumps from basic blocks to basic that are not useful. A jump (conditional or not) to the next basic blocks is not useful. If we have two basic blocks: BL a=2 goto 52 B2 bea ‘we can optimize into: BI Ba bea For that optimization, we have to test cach jump for the distance of the target block. Ifthe distance is only one, we can remove the jump. This works also for conditional jumps. A conditional has two exit points. Ifthe condition is true, we jump to the specified block, otherwise we jump to the next block. If the target block is the next one, the effect is the same has a non-conditional jump and then can be removed, ‘The last two optimizations are not really local, but are simple versions of global optimizations. ‘There are not as powerful as they are not following a data-flow, but they can be greatly improve the efficiency of some function, even if they don't have the power of the equivalent global optimization 9. Remove dead basic blocks Sometimes after having simplified some conditional jamps into simple jumps or even removed some of them, some basic blocks are not teachable. For example, this set of basic block: bes can be optimized into: ‘There are no general algorithm for applying that. It depends on the instruction set that you TAC language has. 10, Merge basic blocks Finally, another optimization, not really local again, is to merge some basic blocks, After the basic blocks have been optimized, we often have some blocks that are redundant. For example: BL Ba bea can be optimized into’ Of course, the second basic block that is merged with the first one must not be referred by a jump. Optimization passes When you have ten optimization techniques, you will have to find a way to make them interact correctly. As you certainly saw on the examples, some of the optimized sample can be optimized again with another optimization. ‘There are no general algorithm to make all optimization techniques work together in an optimal way. I chose a simple technique in EDDI. All the techniques are run on the complete code one after another. Then, if one or more techniques have had an effect on the program, we tun again all the optimization techniques. So the optimizations run until there are no more changes to the program, Conclusion, By using all the techniques described in this post, you will be able to have an efficient code. It won't be as good as, with local optimization coupled to global optimization, but it's a good start for a simple compiler. T hope I will have the time to implement some global optimization techniques into eddic and then write about it on this blog, Compilers EDI. Optimization Contents © 2018 Baptiste Wicht (mailtocbaptistewicht@gmail.com) - Powered by Nikola (http://getnikola.com) License: © QF) (hutp://creativecommons.org/licenses/by/4.0/) Source (Jocal-optimization-on-three-address-code.wp)

You might also like