![Page 1: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/1.jpg)
IR Optimization
![Page 2: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/2.jpg)
Announcements
● Programming Project 4 due Wednesday, August 10 at 11:59PM.
● Written Assignment 2 graded.● Hard copies returned after lecture.● Electronic copies returned by email.
● Programming Assignment 2 grading almost done.● We'll email back feedback later today.
![Page 3: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/3.jpg)
Where We Are
Lexical Analysis
IR Generation
Syntax Analysis
Semantic Analysis
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 4: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/4.jpg)
Where We Are
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
Wond-IR-ful!
Lexical Analysis
Syntax Analysis
IR Generation
Semantic Analysis
Wond-IR-ful!
![Page 5: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/5.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Optimization
IR Generation
Code Generation
Optimization
SourceCode
MachineCode
![Page 6: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/6.jpg)
IR Optimization
● Goal: Improve the IR generated by the previous step to take better advantage of resources.
● One of the most important and complex parts of any modern compiler.
● A very active area of research.● There is a whole class (CS243) dedicated to
this material.
![Page 7: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/7.jpg)
Sources of Optimization● In order to optimize our IR, we need to understand
why it can be improved in the first place.● Reason one: IR generation introduces redundancy.
● A naïve translation of high-level language features into IR often introduces subcomputations.
● Those subcomputations can often be sped up, shared, or eliminated.
● Reason two: Programmers are lazy.● Code executed inside of a loop can often be factored out
of the loop.● Language features with side effects often used for
purposes other than those side effects.
![Page 8: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/8.jpg)
Optimizations from IR Generation
int x;int y;bool b1;bool b2;bool b3;
b1 = x + x < yb2 = x + x == yb3 = x + x > y
![Page 9: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/9.jpg)
Optimizations from IR Generation
int x;int y;bool b1;bool b2;bool b3;
b1 = x + x < yb2 = x + x == yb3 = x + x > y
_t0 = x + x;_t1 = y;b1 = _t0 < _t1;
_t2 = x + x;_t3 = y;b2 = _t2 == _t3;
_t4 = x + x;_t5 = y;b3 = _t5 < _t4;
![Page 10: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/10.jpg)
Optimizations from IR Generation
int x;int y;bool b1;bool b2;bool b3;
b1 = x + x < yb2 = x + x == yb3 = x + x > y
_t0 = x + x;_t1 = y;b1 = _t0 < _t1;
_t2 = x + x;_t3 = y;b2 = _t2 == _t3;
_t4 = x + x;_t5 = y;b3 = _t5 < _t4;
![Page 11: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/11.jpg)
Optimizations from IR Generation
int x;int y;bool b1;bool b2;bool b3;
b1 = x + x < yb2 = x + x == yb3 = x + x > y
_t0 = x + x;_t1 = y;b1 = _t0 < _t1;
b2 = _t0 == _t1;
b3 = _t0 < _t1;
![Page 12: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/12.jpg)
Optimizations from Lazy Coders
while (x < y + z) { x = x – y;}
![Page 13: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/13.jpg)
Optimizations from Lazy Coders
while (x < y + z) { x = x – y;}
_L0: _t0 = y + z; _t1 = x < _t0; IfZ _t1 Goto _L1; x = x – y; Goto _L0;_L1:
![Page 14: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/14.jpg)
Optimizations from Lazy Coders
while (x < y + z) { x = x – y;}
_L0: _t0 = y + z; _t1 = x < _t0; IfZ _t1 Goto _L1; x = x – y; Goto _L0;_L1:
![Page 15: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/15.jpg)
Optimizations from Lazy Coders
while (x < y + z) { x = x – y;}
_t0 = y + z;_L0: _t1 = x < _t0; IfZ _t1 Goto _L1; x = x – y; Goto _L0;_L1:
![Page 16: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/16.jpg)
Optimizations from Lazy Coders
while (x < y + z) { x = x – y;}
_t0 = y + z;_L0: _t1 = x < _t0; IfZ _t1 Goto _L1; x = x - y; Goto _L0;_L1:
![Page 17: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/17.jpg)
A Note on Terminology
● The term “optimization” implies looking for an “optimal” piece of code for a program.
● This is, in general, undecidable.● e.g. create a program that can be simplified iff some
other program halts.
● Our goal will be IR improvement rather than IR optimization.
![Page 18: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/18.jpg)
The Challenge of Optimization
● A good optimizer● Should never change the observable behavior of a
program.● Should produce IR that is as efficient as possible.● Should not take too long to process inputs.
● Unfortunately:● Even good optimizers sometimes introduce bugs into
code.● Optimizers often miss “easy” optimizations due to
limitations of their algorithms.● Almost all interesting optimizations are NP-hard.
![Page 19: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/19.jpg)
What are we Optimizing?
● Optimizers can try to improve code usage with respect to many observable properties.
● What are some quantities we might want to optimize?● Runtime (make the program as fast as possible at the
expense of time and power)● Memory usage (generate the smallest possible
executable at the expense of time and power)● Power consumption (choose simple instructions at the
expense of speed and memory usage)● Plus a lot more (minimize function calls, reduce use of
floating-point hardware, etc.)
![Page 20: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/20.jpg)
What are we Optimizing?
● Optimizers can try to improve code usage with respect to many observable properties.
● What are some quantities we might want to optimize?● Runtime (make the program as fast as possible at the
expense of time and power)● Memory usage (generate the smallest possible
executable at the expense of time and power)● Power consumption (choose simple instructions at the
expense of speed and memory usage)● Plus a lot more (minimize function calls, reduce use of
floating-point hardware, etc.)
![Page 21: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/21.jpg)
IR Optimization vs Code Optimization
● There is not always a clear distinction between what belongs to “IR optimization” versus “code optimization.”
● Typically:● IR optimizations try to perform simplifications that are
valid across all machines.● Code optimizations try to improve performance based
on the specifics of the machine.
● Some optimizations are somewhere in-between:● Replacing x / 2 with x * 0.5
![Page 22: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/22.jpg)
Overview of IR Optimization
● Formalisms and Terminology (Today)● Control-flow graphs.● Basic blocks.
● Local optimizations (Today)● Speeding up small pieces of a function.
● Global optimizations (Monday)● Speeding up functions as a whole.
● The dataflow framework (Monday/Wednesday)● Defining and implementing a wide class of optimizations.
● Lazy code motion (Wednesday)● An extremely powerful IR optimization used in many compilers.
![Page 23: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/23.jpg)
Formalisms and Terminology
![Page 24: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/24.jpg)
Analyzing a Program
● In order to optimize a program, the compiler has to be able to reason about the properties of that program.
● An analysis is called sound if it never asserts an incorrect fact about a program.
● All the analyses we will discuss in this class are sound.● (Why?)
![Page 25: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/25.jpg)
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 26: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/26.jpg)
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 27: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/27.jpg)
Soundness
“At this point in the program, x holds some integer value.”
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 28: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/28.jpg)
Soundness
“At this point in the program, x is either 137 or 42”
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 29: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/29.jpg)
Soundness
“At this point in the program, x is 137”
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 30: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/30.jpg)
Soundness
“At this point in the program, x is either 137, 42, or 271”
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
![Page 31: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/31.jpg)
Semantics-Preserving Optimizations
● An optimization is semantics-preserving if it does not alter the semantics of the original program.
● Examples:● Eliminating unnecessary temporary variables.● Computing values that are known statically at compile-time
instead of runtime.● Evaluating constant expressions outside of a loop instead of
inside.
● Non-examples:● Replacing bubble sort with quicksort.
● The optimizations we will consider in this class are all semantics-preserving.
![Page 32: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/32.jpg)
A Formalism for IR Optimization
● Every phase of the compiler uses some new abstraction:● Scanning uses regular expressions.● Parsing uses CFGs.● Semantic analysis uses proof systems and symbol
tables.● IR generation uses ASTs.
● In optimization, we need a formalism that captures the structure of a program in a way amenable to optimization.
![Page 33: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/33.jpg)
Visualizing IR
main: BeginFunc 40; _tmp0 = LCall _ReadInteger; a = _tmp0; _tmp1 = LCall _ReadInteger; b = _tmp1;_L0: _tmp2 = 0; _tmp3 = b == _tmp2; _tmp4 = 0; _tmp5 = _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c = a; a = b; _tmp6 = c % a; b = _tmp6; Goto _L0;_L1: PushParam a; LCall _PrintInt; PopParams 4; EndFunc;
![Page 34: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/34.jpg)
Visualizing IR
main: BeginFunc 40; _tmp0 = LCall _ReadInteger; a = _tmp0; _tmp1 = LCall _ReadInteger; b = _tmp1;_L0: _tmp2 = 0; _tmp3 = b == _tmp2; _tmp4 = 0; _tmp5 = _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c = a; a = b; _tmp6 = c % a; b = _tmp6; Goto _L0;_L1: PushParam a; LCall _PrintInt; PopParams 4; EndFunc;
![Page 35: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/35.jpg)
Visualizing IR
main: BeginFunc 40; _tmp0 = LCall _ReadInteger; a = _tmp0; _tmp1 = LCall _ReadInteger; b = _tmp1;_L0: _tmp2 = 0; _tmp3 = b == _tmp2; _tmp4 = 0; _tmp5 = _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c = a; a = b; _tmp6 = c % a; b = _tmp6; Goto _L0;_L1: PushParam a; LCall _PrintInt; PopParams 4; EndFunc;
_tmp0 = LCall _ReadInteger;a = _tmp0 ;_tmp1 = LCall _ReadInteger;b = _tmp1 ;
_tmp2 = 0 ;_tmp3 = b == _tmp2 ;_tmp4 = 0 ;_tmp5 = _tmp3 == _tmp4 ;IfZ _tmp5 Goto _L1 ;
c = a ;a = b ;_tmp6 = c % a ;b = _tmp6 ;Goto _L0 ;
PushParam a ;LCall _PrintInt ;PopParams 4 ;
![Page 36: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/36.jpg)
Visualizing IR
main: BeginFunc 40; _tmp0 = LCall _ReadInteger; a = _tmp0; _tmp1 = LCall _ReadInteger; b = _tmp1;_L0: _tmp2 = 0; _tmp3 = b == _tmp2; _tmp4 = 0; _tmp5 = _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c = a; a = b; _tmp6 = c % a; b = _tmp6; Goto _L0;_L1: PushParam a; LCall _PrintInt; PopParams 4; EndFunc;
start
_tmp0 = LCall _ReadInteger;a = _tmp0 ;_tmp1 = LCall _ReadInteger;b = _tmp1 ;
_tmp2 = 0 ;_tmp3 = b == _tmp2 ;_tmp4 = 0 ;_tmp5 = _tmp3 == _tmp4 ;IfZ _tmp5 Goto _L1 ;
c = a ;a = b ;_tmp6 = c % a ;b = _tmp6 ;Goto _L0 ;
PushParam a ;LCall _PrintInt ;PopParams 4 ;
end
![Page 37: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/37.jpg)
Basic Blocks
● A basic block is a sequence of IR instructions where● There is exactly one spot where control enters the
sequence, which must be at the start of the sequence.
● There is exactly one spot where control leaves the sequence, which must be at the end of the sequence.
● Informally, a sequence of instructions that always execute as a group.
![Page 38: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/38.jpg)
Control-Flow Graphs
● A control-flow graph (CFG) is a graph of the basic blocks in a function.● The term CFG is overloaded – from here on out,
we'll mean “control-flow graph” and not “context-free grammar.”
● Each edge from one basic block to another indicates that control can flow from the end of the first block to the start of the second block.
● There is a dedicated node for the start and end of a function.
![Page 39: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/39.jpg)
Types of Optimizations
● An optimization is local if it works on just a single basic block.
● An optimization is global if it works on an entire control-flow graph.
● An optimization is interprocedural if it works across the control-flow graphs of multiple functions.● We won't talk about this in this course.
![Page 40: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/40.jpg)
Local Optimizations
_t0 = 137;y = _t0;
IfZ x Goto _L0;
_t1 = y;z = _t1;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 41: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/41.jpg)
Local Optimizations
_t0 = 137;y = _t0;
IfZ x Goto _L0;
_t1 = y;z = _t1;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 42: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/42.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
_t1 = y;z = _t1;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 43: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/43.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
_t1 = y;z = _t1;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 44: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/44.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 45: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/45.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
_t2 = y;x = _t2;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 46: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/46.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
x = y;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 47: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/47.jpg)
Local Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
x = y;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 48: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/48.jpg)
Global Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
x = y;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 49: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/49.jpg)
Global Optimizations
y = 137;
IfZ x Goto _L0;
z = y;
x = y;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 50: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/50.jpg)
Global Optimizations
y = 137;
IfZ x Goto _L0;
z = 137;
x = 137;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 51: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/51.jpg)
Global Optimizations
y = 137;
IfZ x Goto _L0;
z = 137;
x = 137;
end
int main() { int x; int y; int z;
y = 137; if (x == 0) z = y; else x = y; }
start
![Page 52: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/52.jpg)
Local Optimizations
![Page 53: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/53.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
![Page 54: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/54.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = a + b ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 55: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/55.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = a + b ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 56: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/56.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 57: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/57.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 58: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/58.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 59: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/59.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 60: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/60.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 61: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/61.jpg)
Common Subexpression Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 62: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/62.jpg)
Common Subexpression Elimination
● If we have two variable assignments
v1 = a op b
…
v2 = a op b
and the values of v1, a, and b have not changed between the
assignments, rewrite the code as
v1 = a op b
…
v2 = v
1
● Eliminates useless recalculation.
● Paves the way for later optimizations.
![Page 63: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/63.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 64: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/64.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 65: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/65.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 66: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/66.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 67: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/67.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 68: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/68.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 69: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/69.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 70: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/70.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 71: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/71.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp6) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 72: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/72.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp6) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 73: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/73.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 74: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/74.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp3 ; _tmp4 = _tmp3 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 75: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/75.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp0 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 76: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/76.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0 ; a = _tmp0 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 77: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/77.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 78: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/78.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = c ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam c ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 79: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/79.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 80: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/80.jpg)
Copy Propagation
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 81: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/81.jpg)
Copy Propagation
● If we have a variable assignment
v1 = v
2
then as long as v1 and v
2 are not reassigned, we
can rewrite expressions of the form
a = … v1 …
as
a = … v2 …
provided that such a rewrite is legal.● This will help immensely later on, as you'll see.
![Page 82: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/82.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 83: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/83.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 84: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/84.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 85: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/85.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp3 = 4 ; a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 86: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/86.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 87: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/87.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
a = 4 ; _tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 88: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/88.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 89: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/89.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 90: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/90.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ;
_tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 91: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/91.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ;
_tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 92: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/92.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ;
_tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 93: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/93.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ;
_tmp6 = _tmp2 ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 94: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/94.jpg)
Dead Code Elimination
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ;
_tmp4 = _tmp0 + b ;
_tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
![Page 95: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/95.jpg)
Dead Code Elimination
● An assignment to a variable v is called dead if the value of that assignment is never read anywhere.
● Dead code elimination removes dead assignments from IR.
● Determining whether an assignment is dead depends on what variable is being assigned to and when it's being assigned.
![Page 96: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/96.jpg)
For Comparison _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 = _tmp0 + b ; _tmp7 = *(_tmp2) ; PushParam _tmp4 ; PushParam _tmp1 ; ACall _tmp7 ; PopParams 8 ;
_tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = a + b ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ;
![Page 97: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/97.jpg)
Applying Local Optimizations
● The different optimizations we've seen so far all take care of just a small piece of the optimization.● Common subexpression elimination eliminates
unnecessary statements.● Copy propagation helps identify dead code.● Dead code elimination removes statements that are
no longer needed.
● To get maximum effect, we may have to apply these optimizations numerous times.
![Page 98: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/98.jpg)
Applying Local Optimizations
b = a * a;c = a * a;d = b + c;e = b + b;
![Page 99: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/99.jpg)
Applying Local Optimizations
b = a * a;c = a * a;d = b + c;e = b + b;
![Page 100: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/100.jpg)
Applying Local Optimizations
b = a * a;c = a * a;d = b + c;e = b + b;
Common Subexpression Elimination
![Page 101: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/101.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + c;e = b + b;
Common Subexpression Elimination
![Page 102: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/102.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + c;e = b + b;
Common Subexpression Elimination
![Page 103: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/103.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + c;e = b + b;
Common Subexpression Elimination
![Page 104: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/104.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + c;e = b + b;
Copy Propagation
![Page 105: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/105.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = b + b;
Copy Propagation
![Page 106: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/106.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = b + b;
Copy Propagation
![Page 107: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/107.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = b + b;
Dead Code Elimination
![Page 108: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/108.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = b + b;
Common Subexpression Elimination (Again)
![Page 109: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/109.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = d;
Common Subexpression Elimination (Again)
![Page 110: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/110.jpg)
Applying Local Optimizations
b = a * a;c = b;d = b + b;e = d;
![Page 111: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/111.jpg)
Other Types of Local Optimization
● Arithmetic Simplification● Replace “hard” operations with easier ones.● e.g. rewrite x = 4 * a; as x = a << 2;
● Constant Folding● Evaluate expressions at compile-time if they have a
constant value.● e.g. rewrite x = 4 * 5; as x = 20;.
![Page 112: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/112.jpg)
Implementing Local Optimization
![Page 113: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/113.jpg)
Optimizations and Analyses
● Most optimizations are only possible given some analysis of the program's behavior.
● In order to implement an optimization, we will talk about the corresponding program analyses.
![Page 114: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/114.jpg)
Available Expressions
● Both common subexpression elimination and copy propagation depend on an analysis of the available expressions in a program.
● An expression is called available if some variable in the program holds the value of that expression.
● In common subexpression elimination, we replace an available expression by the variable holding its value.
● In copy propagation, we replace the use of a variable by the available expression it holds.
![Page 115: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/115.jpg)
Finding Available Expressions
● Initially, no expressions are available.● Whenever we execute a statement a = b + c:
● Any expression holding a is invalidated.● The expression a = b + c becomes available.
● Idea: Iterate across the basic block, beginning with the empty set of expressions and updating available expressions at each variable.
![Page 116: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/116.jpg)
Available Expressions
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;
![Page 117: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/117.jpg)
Available Expressions
{ }a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;
![Page 118: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/118.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;
![Page 119: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/119.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
e = a + b;
d = b;
f = a + b;
![Page 120: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/120.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
d = b;
f = a + b;
![Page 121: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/121.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
f = a + b;
![Page 122: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/122.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
![Page 123: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/123.jpg)
Available Expressions
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 124: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/124.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 125: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/125.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = b;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 126: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/126.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 127: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/127.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = a + b;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 128: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/128.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = d;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 129: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/129.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = d;
{ a = b, c = b, d = a + b, e = a + b }d = b;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 130: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/130.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = d;
{ a = b, c = b, d = a + b, e = a + b }d = a;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 131: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/131.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = d;
{ a = b, c = b, d = a + b, e = a + b }d = a;
{ a = b, c = b, d = b, e = a + b }f = a + b;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 132: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/132.jpg)
Common Subexpression Elimination
{ }a = b;
{ a = b }c = a;
{ a = b, c = b }d = a + b;
{ a = b, c = b, d = a + b }e = d;
{ a = b, c = b, d = a + b, e = a + b }d = a;
{ a = b, c = b, d = b, e = a + b }f = e;
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 133: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/133.jpg)
Common Subexpression Elimination
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
![Page 134: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/134.jpg)
Live Variables
● The analysis corresponding to dead code elimination is called liveness analysis.
● A variable is live at a point in a program if later in the program its value will be read before it is written to again.
● Dead code elimination works by computing liveness for each variable, then eliminating assignments to dead variables.
![Page 135: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/135.jpg)
Computing Live Variables
● To know if a variable will be used at some point, we iterate across the statements in a basic block in reverse order.
● Initially, some small set of values are known to be live (which ones depends on the particular program).
● When we see the statement a = b + c:● Just before the statement, a is not alive, since its value is
about to be overwritten.● Just before the statement, both b and c are alive, since
we're about to read their values.● (what if we have a = a + b?)
![Page 136: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/136.jpg)
Liveness Analysis
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
![Page 137: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/137.jpg)
Liveness Analysis
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ b, d }
![Page 138: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/138.jpg)
Liveness Analysis
a = b;
c = a;
d = a + b;
e = d;
d = a;{ b, d, e }
f = e;{ b, d }
![Page 139: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/139.jpg)
Liveness Analysis
a = b;
c = a;
d = a + b;
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 140: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/140.jpg)
Liveness Analysis
a = b;
c = a;
d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 141: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/141.jpg)
Liveness Analysis
a = b;
c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 142: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/142.jpg)
Liveness Analysis
a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 143: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/143.jpg)
Liveness Analysis
{ b }a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 144: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/144.jpg)
Dead Code Elimination
{ b }a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 145: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/145.jpg)
Dead Code Elimination
{ b }a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
f = e;{ b, d }
![Page 146: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/146.jpg)
Dead Code Elimination
{ b }a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
{ b, d }
![Page 147: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/147.jpg)
Dead Code Elimination
{ b }a = b;{ a, b }c = a;{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
{ b, d }
![Page 148: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/148.jpg)
Dead Code Elimination
{ b }a = b;{ a, b }
{ a, b }d = a + b;{ a, b, d }
e = d;{ a, b, e }
d = a;{ b, d, e }
{ b, d }
![Page 149: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/149.jpg)
Dead Code Elimination
a = b;
d = a + b;
e = d;
d = a;
![Page 150: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/150.jpg)
Liveness Analysis II
a = b;
d = a + b;
e = d;
d = a;
![Page 151: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/151.jpg)
Liveness Analysis II
a = b;
d = a + b;
e = d;
d = a;{ b, d }
![Page 152: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/152.jpg)
Liveness Analysis II
a = b;
d = a + b;
e = d;{ a, b }d = a;{ b, d }
![Page 153: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/153.jpg)
Liveness Analysis II
a = b;
d = a + b;{ a, b, d }
e = d;{ a, b }d = a;{ b, d }
![Page 154: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/154.jpg)
Liveness Analysis II
a = b;
{ a, b }
d = a + b;{ a, b, d }
e = d;{ a, b }d = a;{ b, d }
![Page 155: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/155.jpg)
Liveness Analysis II
{ b }a = b;
{ a, b }
d = a + b;{ a, b, d }
e = d;{ a, b }d = a;{ b, d }
![Page 156: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/156.jpg)
Dead Code Elimination
{ b }a = b;
{ a, b }
d = a + b;{ a, b, d }
e = d;{ a, b }d = a;{ b, d }
![Page 157: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/157.jpg)
Dead Code Elimination
{ b }a = b;
{ a, b }
d = a + b;{ a, b, d }
e = d;{ a, b }d = a;{ b, d }
![Page 158: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/158.jpg)
Dead Code Elimination
{ b }a = b;
{ a, b }
d = a + b;{ a, b, d }
{ a, b }d = a;{ b, d }
![Page 159: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/159.jpg)
Dead Code Elimination
a = b;
d = a + b;
d = a;
![Page 160: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/160.jpg)
Liveness Analysis III
a = b;
d = a + b;
d = a;
![Page 161: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/161.jpg)
Liveness Analysis III
a = b;
d = a + b;
d = a;{b, d}
![Page 162: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/162.jpg)
Liveness Analysis III
a = b;
d = a + b;
{a, b}
d = a;{b, d}
![Page 163: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/163.jpg)
Liveness Analysis III
a = b;
{a, b}
d = a + b;
{a, b}
d = a;{b, d}
![Page 164: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/164.jpg)
Liveness Analysis III
{b}a = b;
{a, b}
d = a + b;
{a, b}
d = a;{b, d}
![Page 165: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/165.jpg)
Dead Code Elimination
{b}a = b;
{a, b}
d = a + b;
{a, b}
d = a;{b, d}
![Page 166: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/166.jpg)
Dead Code Elimination
{b}a = b;
{a, b}
d = a + b;
{a, b}
d = a;{b, d}
![Page 167: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/167.jpg)
Dead Code Elimination
{b}a = b;
{a, b}
{a, b}
d = a;{b, d}
![Page 168: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/168.jpg)
Dead Code Elimination
a = b;
d = a;
![Page 169: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/169.jpg)
A Combined Algorithm
![Page 170: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/170.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
![Page 171: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/171.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{b, d}
![Page 172: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/172.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{b, d}
![Page 173: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/173.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
{b, d}
![Page 174: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/174.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;{a, b}d = a;
{b, d}
![Page 175: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/175.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
e = d;{a, b}d = a;
{b, d}
![Page 176: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/176.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
{a, b}d = a;
{b, d}
![Page 177: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/177.jpg)
A Combined Algorithm
a = b;
c = a;
d = a + b;
{a, b}d = a;
{b, d}
![Page 178: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/178.jpg)
A Combined Algorithm
a = b;
c = a;
{a, b}d = a;
{b, d}
![Page 179: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/179.jpg)
A Combined Algorithm
a = b;
c = a;
{a, b}d = a;
{b, d}
![Page 180: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/180.jpg)
A Combined Algorithm
a = b;
{a, b}d = a;
{b, d}
![Page 181: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/181.jpg)
A Combined Algorithm
{b}a = b;
{a, b}d = a;
{b, d}
![Page 182: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/182.jpg)
A Combined Algorithm
a = b;
d = a;
![Page 183: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/183.jpg)
Properties of Local Analysis
● The only way to find out what a program will actually do is to run it.
● Problems:● The program might not terminate.● The program might have some behavior we didn't see
when we ran it on a particular input.
● However, this is not a problem inside a basic block.● Basic blocks contain no loops.● There is only one path through the basic block.
![Page 184: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/184.jpg)
Another View of Local Optimization
● In local optimization, we want to reason about some property of the runtime behavior of the program.
● We know that we can run the code in a basic block and guarantee termination.
● Could we run the program and just watch what happens?
● Idea: Redefine the semantics of our programming language to give us information about our analysis.
![Page 185: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/185.jpg)
Assigning New Semantics
● Example: Available Expressions● Redefine the statement a = b + c to mean “a
now holds the value of b + c, and any variable holding the value a is now invalid.”
● Run the program assuming these new semantics.
● Treat the compiler as an interpreter for these new semantics.
![Page 186: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/186.jpg)
Next Time
● Global optimization● Optimizing across basic blocks.● Meet operators and the dataflow framework.
![Page 187: IR Optimization - KeithSchwarz.com · Sources of Optimization In order to optimize our IR, we need to understand why it can be improved in the first place. Reason one: IR generation](https://reader034.vdocuments.us/reader034/viewer/2022042402/5f10d7b57e708231d44b14a6/html5/thumbnails/187.jpg)
0 10 20 30 40 50 60 70 80 90 100 110 1200
2
4
6
8
10
12
14
Written Assignment 2 ScoresF
requ
ency
Mean: 86 / 120Median: 93.5 / 120
Stdev: 23