May 7 Lecture Notes NoteTaker : Rashid Ali ----------------------------- Final Exam: Friday, May 15 7:45 - 9:45 AM 2080 Grainger ----------------------------- Prof Fischer Office Hours: Mon 5/11 1 - 2:30 PM Tues 5/12 1 - 2:30 Wed 5/13 11:00 - noon Thurs 5/14 1 - 2:30 ----------------------------- **Optimization in Compilers** -Optimization can be done by making programs smaller and faster -Optimization can be done in no. of ways and at no. of places -Broad range of coverage 1-Optimization can be done at code generation 2-can be done after code generation i.e., prior to execution 3-optimization at execution time - Just in time compilation -Optimization at AST level:- replace it by something that is better for example, ifthennode |-------------------------------------------------- | | | | | | Boolean con then else { true node false node } If we know that it's true we wont do the else part. So in this way code optimized Loops: for ------------------------------------------------- | | | | | init term relation incr body | | var,const var Can be optimized by loop unrolling By loop unrolling we mean that we can expand the loop body. We know that loop will be repeated certain number of times. So loop can be unrolled by duplication of this code. Q. What's the use of doing this? say, for (i=1;i<=10,i++) a[i] = i*2; This can be unrolled as follows: i=1; a[i] = i*2; i=2; a[i] = i*2; ...... ...... ...... It can be seen here that relation (condition i <= 10) does not take place as the loop is unrolled. Other ways: - copy propagation - const propagation - Folding ex 2: while (expr) { body; } TOP: { eval condition } if equal exit { Body } goto TOP EXIT: can be optimized by, goto BOTTOM TOP: { BOdy } BOTTOM: {EVal condition} ifne TOP -Optimization after code gen prior to exec ---> peephole opt. -----> LDC 1 more than 1 byte Jasmine -----> Iconst { 1 byte } So it's better to use Iconst 1 LDC 1 | iload n |--- iload n imul | loadreg $R1,1 load $R2,$FP(off) mul $R1,$R2,$R1 -JAVA APPLET ---- Java class file -JAVA provides platform indepoendant computing -C also provided platfrom independant computing when it was invented but fortran and cobol did not. ----------------------------------------------------------------------------