Test yourself ·Exercise8.6.l ·Exercise8.6.3 CS308 Compiler Theory
Test yourself • Exercise 8.6.1 • Exercise 8.6.3 CS308 Compiler Theory 11
Peephole Optimization The peephole is a small,sliding window on a program. 。 Peephole optimization,is done by examining a sliding window of target instructions and replacing instruction sequences within the peephole by a shorter or faster sequence,whenever possible. Peephole optimization can be applied directly after intermediate code generation to improve the intermediate representation. CS308 Compiler Theory
Peephole Optimization • The peephole is a small, sliding window on a program. • Peephole optimization, is done by examining a sliding window of target instructions and replacing instruction sequences within the peephole by a sh t f t h ibl hor ter or fas ter sequence, w henever possible. • Peephole optimization can be applied directly after intermediate code generation to improve the intermediate representation generation to improve the intermediate representation. CS308 Compiler Theory 12
Eliminating Redundant Loads and Stores LD instruction can be deleted for the sequence of LD a,RO ST RO,a ·Exception: The store instruction had a label. CS308 Compiler Theory 13
Eliminating Redundant Loads and Stores • LD instruction can be deleted for the sequence of • Exception: – The store instruction had a label. CS308 Compiler Theory 13
Eliminating Unreachable Code An unlabeled instruction immediately following an unconditional jump may be removed. This operation can be repeated to eliminate a sequence of instructions. if debug =1 goto L1 goto L2 L1:print debugging information L2: if debug!=1 goto L2 print debugging information L2: if0!=1got0L2卷 print debugging information L2: CS308 Compiler Theory 14
Eliminating Unreachable Code • An unlabeled instruction immediately following an unconditional jump may b d e remove d. • This operation can be repeated to eliminate a sequence of instructions. CS308 Compiler Theory 14
Flow-of-Control Optimizations Unnecessary jumps can be eliminated in either the intermediate code or the target code by peephole optimizations. goto Li goto L1 if a b goto Li L1:goto L2 L1:goto L2 L1:if a b goto L2 L3: if a b goto L2 goto L2 if a b goto L2 goto L3 L1:goto L2 L1:goto L2 L3: Suppose there is only one jump to Ll CS308 Compiler Theory 15
Flow-of- Control Optimizations • Unnecessary jumps can be eliminated in either the intermediate code or th d b hl i i i he target co de by peep h o le opt i m izat ions. Suppose there is only one jump to L1 CS308 Compiler Theory 15