1
00:00:02,060 --> 00:00:07,023
In this short video, I'm going to say a
few words about a variation on local

2
00:00:07,023 --> 00:00:11,038
optimization that applies directly to
assembly code called Peephole

3
00:00:11,038 --> 00:00:19,054
Optimization. The basic idea here is that
instead of optimizing on intermediate code

4
00:00:19,054 --> 00:00:24,011
we could do our optimizations directly on
assembly code And people optimization is

5
00:00:24,011 --> 00:00:28,049
one such technique. The peephole is,
stands for a short sequence of usually

6
00:00:28,049 --> 00:00:32,097
continuous instructions. So, the idea is
that we have our program. We can see, we

7
00:00:32,097 --> 00:00:37,034
can think of it as a long sequence of
instructions and our peephole is some

8
00:00:37,034 --> 00:00:41,088
window onto this program. So, if we have a
peephole of size four, we can think of

9
00:00:41,088 --> 00:00:46,054
ourselves as staring through a small hole
at the program and all we can see is a

10
00:00:46,054 --> 00:00:51,002
short sequence of four instructions and
then we can optimize that sequence. So,

11
00:00:51,002 --> 00:00:55,074
then we can slide the peephole around and
optimize different parts of the program

12
00:00:55,074 --> 00:00:59,555
And the, what the, what the optimizer will
do is it will, you know, stare at this

13
00:00:59,728 --> 00:01:04,750
short sequence of instructions and if it
knows a better sequence it will replace

14
00:01:04,750 --> 00:01:09,355
that sequence by the other one and then it
will repeat this as I said. You know,

15
00:01:09,355 --> 00:01:13,614
applying other transformations to, to
possibly the same or other parts of the

16
00:01:13,614 --> 00:01:17,621
assembly program. So, people optimizations
are generally written as replacement

17
00:01:17,621 --> 00:01:22,039
rules. So, the we'll have the window of
instructions on the left. So, it'll be

18
00:01:22,039 --> 00:01:26,287
some sequence of instructions and we'll
know some other sequence of instructions

19
00:01:26,287 --> 00:01:29,695
that we would prefer on the right. So, if
we see this instruction sequence on the

20
00:01:29,695 --> 00:01:34,075
left, then we'll replace by the one on the
right-hand side. So, for example, if I

21
00:01:34,075 --> 00:01:39,898
have a move from register b to register a
and then I move back from register a to

22
00:01:39,898 --> 00:01:45,364
register b well, that's the second move is
useless, can, can just be deleted as a way

23
00:01:45,364 --> 00:01:50,227
to replace this two instruction sequence
by a one instruction, instruction

24
00:01:50,227 --> 00:01:56,549
sequence. And this will work provided that
there's no possible jump target here. So

25
00:01:56,549 --> 00:02:01,312
if, if there's no possibility that the
code would ever jump to this instruction

26
00:02:01,489 --> 00:02:06,102
then that instruction can be removed.
Another example, If I add i to the

27
00:02:06,102 --> 00:02:11,525
register a, and then I subsequently add j
to the register a, I can do a constant

28
00:02:11,525 --> 00:02:18,008
folding optimization here, and combine
those two add two additions into one

29
00:02:18,008 --> 00:02:24,336
addition where I add the sum of i = j to
the register A. So, many but not quite all

30
00:02:24,336 --> 00:02:30,091
of the basic block optimizations that
we've discussed in the last video, can be

31
00:02:30,091 --> 00:02:36,080
cast also as peephole optimizations. So,
for example if we are adding zero to a

32
00:02:36,080 --> 00:02:41,076
register and we're storing it in another
register, well, that can be replaced by a

33
00:02:41,076 --> 00:02:46,098
register move. If we're moving a value
from the same register to itself so this

34
00:02:46,098 --> 00:02:51,070
is like a self-assignment, well, that
instruction can just be deleted, replaced

35
00:02:51,070 --> 00:02:57,004
by the empty sequence of instructions. And
together for those two instructions would

36
00:02:57,004 --> 00:03:02,007
be those two optimizations, excuse me,
would be able to eliminate adding zero to

37
00:03:02,007 --> 00:03:06,097
a register. So, first this would get
translated into a move from a to a. And

38
00:03:06,097 --> 00:03:11,035
then the move from a to a would get
deleted. And as this little example

39
00:03:11,035 --> 00:03:15,810
illustrates just like with local
optimizations, people optimizations have

40
00:03:15,810 --> 00:03:21,787
to be applied repeatedly to get the
maximum effect. I hope this simple

41
00:03:21,787 --> 00:03:27,626
discussion has illustrated for you that
many optimizations can be applied directly

42
00:03:27,626 --> 00:03:32,042
to assembly code and that there's really
nothing magic about optimizing

43
00:03:32,042 --> 00:03:36,087
intermediate code. So, if you have a
program written in any language, source

44
00:03:36,087 --> 00:03:41,056
language, intermediate language, assembly
language. It makes sense to talk about

45
00:03:41,061 --> 00:03:46,073
doing transformations of programs written
in that language to improve the behavior

46
00:03:46,073 --> 00:03:51,072
of the program. And it's also a good time
here to mention that program optimization

47
00:03:51,072 --> 00:03:56,054
is really a terrible term. The compilers
do not produce optimal code and it's

48
00:03:56,054 --> 00:04:01,052
purely an accident if a compiler were to
somehow generate the best possible code

49
00:04:01,052 --> 00:04:05,093
for a given program. Really, what
compilers do is they have a bunch of

50
00:04:05,093 --> 00:04:10,078
transformations that they know will
improve the behavior of the program. And

51
00:04:10,078 --> 00:04:15,310
they'll just improve it as much as they ca
N. So, really what program optimization is

52
00:04:15,310 --> 00:04:21,000
all about is program improvement. We're
trying to make the program better but

53
00:04:21,000 --> 00:04:28,309
there's no guarantee that we will reads
the best possible code for a given
