BSD 4_2 development
[unix-history] / usr / doc / ctour / cdoc2
CommitLineData
c9528a00
C
1.SH
2Expression Optimization
3.PP
4Each expression tree, as it is read in, is subjected to
5a fairly comprehensive
6analysis.
7This is performed
8by the
9.II optim
10routine and a number of subroutines;
11the major things done are
12.IP 1.
13Modifications and simplifications
14of the tree so its value may be computed more efficiently
15and conveniently by the code generator.
16.RT
17.IP 2.
18Marking each interior node with an estimate of the number of
19registers required to evaluate it.
20This register count is needed to guide the code generation algorithm.
21.PP
22One thing that is definitely not done is
23discovery or exploitation of common subexpressions, nor is this done anywhere in the
24compiler.
25.PP
26The basic organization is simple: a depth-first scan of the tree.
27.II Optim
28does nothing for leaf nodes (except for automatics; see below),
29and calls
30.II unoptim
31to handle unary operators.
32For binary operators,
33it calls itself to process the operands,
34then treats each operator separately.
35One important case is
36commutative and associative operators, which are handled
37by
38.II acommute.
39.PP
40Here is a brief catalog of the transformations carried out by
41by
42.II optim
43itself.
44It is not intended to be complete.
45Some of the transformations are machine-dependent,
46although they may well be useful on machines other than the
47PDP-11.
48.IP 1.
49As indicated in the discussion of
50.II unoptim
51below, the optimizer can create a node type corresponding
52to the location addressed by a register plus a constant offset.
53Since this is precisely the implementation of automatic variables
54and arguments, where the register is fixed by convention,
55such variables are changed to the new form to simplify
56later processing.
57.RT
58.IP 2.
59Associative and commutative operators are processed by the
60special routine
61.II acommute.
62.RT
63.IP 3.
64After processing by
65.II acommute,
66the bitwise & operator is turned into a new
67.II andn
68operator; `a & b' becomes
69`a
70.II andn
71~b'.
72This is done because the PDP-11 provides
73no
74.II and
75operator, but only
76.II andn.
77A similar transformation takes place for
78`=&'.
79.RT
80.IP 4.
81Relationals are turned around so the
82more complicated expression is on the left.
83(So that `2 > f(x)' becomes `f(x) < 2').
84This improves code generation since
85the algorithm prefers to have the right operand
86require fewer registers than the left.
87.RT
88.IP 5.
89An expression minus a constant is turned into
90the expression plus the negative constant,
91and the
92.II acommute
93routine is called
94to take advantage of the properties of addition.
95.RT
96.IP 6.
97Operators with constant operands are evaluated.
98.RT
99.IP 7.
100Right shifts (unless by 1)
101are turned into left shifts with a negated right operand,
102since the PDP-11 lacks a general right-shift operator.
103.RT
104.IP 8.
105A number of special cases are simplified, such as division or
106multiplication by 1,
107and shifts by 0.
108.LP
109The
110.II unoptim
111routine performs the same sort of processing for unary operators.
112.IP 1.
113`*&x' and `&*x' are simplified to `x'.
114.RT
115.IP 2.
116If
117.II r
118is a register and
119.II c
120is a constant or the address of a static or external
121variable,
122the expressions `*(r+c)'
123and `*r' are turned into a special kind of name node
124which expresses
125the name itself and the offset.
126This simplifies subsequent processing
127because such constructions can appear as
128the the address of a PDP-11 instruction.
129.RT
130.IP 3.
131When the unary `&' operator is applied to
132a name node of the special kind just discussed,
133it is reworked to make the addition
134explicit again;
135this is done because the PDP-11 has no `load address' instruction.
136.RT
137.IP 4.
138Constructions
139like
140`*r++' and
141`*\-\-r'
142where
143.II r
144is a register are discovered and marked
145as being implementable using the PDP-11
146auto-increment and -decrement modes.
147.RT
148.IP 5.
149If `!' is applied to a relational,
150the `!' is discarded
151and the sense of the relational is reversed.
152.RT
153.IP 6.
154Special cases involving reflexive
155use of negation and complementation are discovered.
156.RT
157.IP 7.
158Operations applying to constants are evaluated.
159.PP
160The
161.II acommute
162routine, called for associative and commutative operators,
163discovers clusters of the same operator at the top levels
164of the current tree, and arranges them in a list:
165for `a+((b+c)+(d+f))'
166the list would be`a,b,c,d,e,f'.
167After each subtree is optimized, the list is sorted in
168decreasing difficulty of computation;
169as mentioned above,
170the code generation algorithm works best when left operands
171are the difficult ones.
172The `degree of difficulty'
173computed is actually finer than
174the mere number of registers required;
175a constant is considered simpler
176than the address of a static or external, which is simpler
177than reference to a variable.
178This makes it easy to fold all the constants
179together,
180and also to merge together the sum of a constant and the address of
181a static
182or external (since in such nodes there is space for
183an `offset' value).
184There are also special cases, like multiplication by 1 and addition of 0.
185.II
186A special routine is invoked to handle sums of products.
187.II Distrib
188is based on the fact that it is better
189to compute `c1*c2*x + c1*y' as `c1*(c2*x + y)'
190and makes the divisibility tests required to assure the
191correctness of the transformation.
192This transformation is rarely
193possible with code directly written by the user,
194but it invariably occurs as a result of the
195implementation of multi-dimensional arrays.
196.PP
197Finally,
198.II acommute
199reconstructs a tree from the list
200of expressions which result.