Computer Science 313 – Advanced Programming Topics
Optimization Speed Global optimizations improve whole methods Some do more and look at entire programs Can provide big improvement if code written well But takes a lot of time and may not pay off in Java Optimize basic blocks to simplify analysis Lets compiler assume all instructions executed Were created first, so much simpler to develop Quicker & often improves performance decently
Value Numbering Value numbering biggest local optimization Approach created by Cocke & Schwartz in 1970 Eliminates common subexpressions found in code Finds and “folds” constants so code is simplified Global CSE similar, but uses entire method Based upon value numbering, but adds deadness SSA form not required but can improve results
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway j = Math.pow((k + 1) * 5, i.foo); becomes
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway j = Math.pow((k + 1) * 5, i.foo); becomes t1 = k + 1;
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway j = Math.pow((k + 1) * 5, i.foo); becomes t1 = k + 1; t2 = t1 * 5;
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway j = Math.pow((k + 1) * 5, i.foo); becomes t1 = k + 1; t2 = t1 * 5; t3 = address of i; t4 = t3 + offset of field foo; t5 = *t4;
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway j = Math.pow((k + 1) * 5, i.foo); becomes t1 = k + 1; t2 = t1 * 5; t3 = address of i; t4 = t3 + offset of field foo; t5 = *t4; j = Math.pow(t2,t5);
Before Value Numbering... Complex lines rewritten as simple instructions Lines split up by adding temporary variables Simple operations needed by processor anyway Once done, expressions should be written as: result leftOperand rightOperand result leftOperand OP rightOperand or result value result value or methodCallparam1param2param3param4 methodCall(param1, param2, param3, param4)
Key Ideas Used Use maps associating expression to number If mapped to same number, expressions equal Available expressions reused not recalculated Number meaningless, just very easy to compare Create tuple representing each expression Tag specifies if constant, value, calculation Needs map from expression to object to work
Value Numbering Outline value result value If instruction is simple assignment then Retrieve number for value (if it was not there, add it) Update maps so result mapped to getNumber(value) leftOperand leftOperand rightOperand rightOperand result result result result result Else if instruction is assignment then lNum number for leftOperand (if it was not there, add it) If lNum.isConstant() then leftOperand lNum.value rNum number for rightOperand (if it was not there, add it) If rNum.isConstant() then rightOperand rNum.value If lNum.isConstant() && rNum.isConstant() then Update maps so result mapped to evaluate(lNum, OP, rNum)) Rewrite instruction as “result ”+ evaluate(lNum, OP, rNum) Else if mappedToNumber(lNum, OP, rNum) then Update maps so result mapped to getNumber(lNum, OP, rNum) Rewrite instruction as “result ”+ getNumber(lNum, OP, rNum).value Else Update maps so result mapped to newNumber(lNum, OP, rNum)
Result of This Pass Automatically expands & evaluates constants Propagates through block whenever possible Ensures block will compute value only once Change other computations with assignment Simplifies code to use only original assignment Dead assignments eliminated in second pass Programmer’s code not major limitation Lines split into simple instructions before starting Makes it possible to replaces portion of line
Example Original a 4 k (i * j) + 5 m 5 * a * k n i b n * j + i * aSimplified
Example Original a 4 k (i * j) + 5 m 5 * a * k n i b n * j + i * aSimplified a 4 t1 i * j k t1 + 5 t2 5 * a m t2 * k n i t3 n * j t4 i * a b t3 + t4
Example MapsNumbered a 4 t1 i * j k t1 + 5 t2 5 * a m t2 * k n i t3 n * j t4 i * a b t3 + t4 #TypeValue
Example Numbered a 1 4 1 t1 4 i 2 * j 3 k 6 t t2 7 5 5 * a 1 m 8 t2 7 * k 6 n 2 i 2 t3 4 n 2 * j 3 t4 9 i 2 * a 1 b 10 t3 4 + t4 9Rewritten a 4 t1 i * j k t1 + 5 t2 20 m 20 * k n i t3 t1 t4 i * 4 b t1 + t4
For Next Class Lab due Friday before noon Please, please, please do not wait until last minute… Design is hard, but code is very simple Give yourself time to think and ask questions Read up on our last design pattern Frequently used for distributed coding Multi-tier processing uses this also Becoming increasingly common to see this pattern