📄 cdoc2
字号:
.SHExpression Optimization.PPEach expression tree, as it is read in, is subjected toa fairly comprehensiveanalysis.This is performedby the.II optimroutine and a number of subroutines;the major things done are.IP 1.Modifications and simplificationsof the tree so its value may be computed more efficientlyand conveniently by the code generator..RT.IP 2.Marking each interior node with an estimate of the number ofregisters required to evaluate it.This register count is needed to guide the code generation algorithm..PPOne thing that is definitely not done isdiscovery or exploitation of common subexpressions, nor is this done anywhere in thecompiler..PPThe basic organization is simple: a depth-first scan of the tree..II Optimdoes nothing for leaf nodes (except for automatics; see below),and calls.II unoptimto handle unary operators.For binary operators,it calls itself to process the operands,then treats each operator separately.One important case iscommutative and associative operators, which are handledby.II acommute..PPHere is a brief catalog of the transformations carried out byby.II optimitself.It is not intended to be complete.Some of the transformations are machine-dependent,although they may well be useful on machines other than thePDP-11..IP 1.As indicated in the discussion of.II unoptimbelow, the optimizer can create a node type correspondingto the location addressed by a register plus a constant offset.Since this is precisely the implementation of automatic variablesand arguments, where the register is fixed by convention,such variables are changed to the new form to simplifylater processing..RT.IP 2.Associative and commutative operators are processed by thespecial routine.II acommute..RT.IP 3.After processing by.II acommute,the bitwise & operator is turned into a new.II andnoperator; `a & b' becomes`a.II andn~b'.This is done because the PDP-11 providesno.II andoperator, but only.II andn.A similar transformation takes place for`=&'..RT.IP 4.Relationals are turned around so themore complicated expression is on the left.(So that `2 > f(x)' becomes `f(x) < 2').This improves code generation sincethe algorithm prefers to have the right operandrequire fewer registers than the left..RT.IP 5.An expression minus a constant is turned intothe expression plus the negative constant,and the.II acommuteroutine is calledto take advantage of the properties of addition..RT.IP 6.Operators with constant operands are evaluated..RT.IP 7.Right shifts (unless by 1)are turned into left shifts with a negated right operand,since the PDP-11 lacks a general right-shift operator..RT.IP 8.A number of special cases are simplified, such as division ormultiplication by 1,and shifts by 0..LPThe.II unoptimroutine performs the same sort of processing for unary operators..IP 1.`*&x' and `&*x' are simplified to `x'..RT.IP 2.If.II ris a register and.II cis a constant or the address of a static or externalvariable,the expressions `*(r+c)'and `*r' are turned into a special kind of name nodewhich expressesthe name itself and the offset.This simplifies subsequent processingbecause such constructions can appear asthe the address of a PDP-11 instruction..RT.IP 3.When the unary `&' operator is applied toa name node of the special kind just discussed,it is reworked to make the additionexplicit again;this is done because the PDP-11 has no `load address' instruction..RT.IP 4.Constructionslike`*r++' and`*\-\-r'where.II ris a register are discovered and markedas being implementable using the PDP-11auto-increment and -decrement modes..RT.IP 5.If `!' is applied to a relational,the `!' is discardedand the sense of the relational is reversed..RT.IP 6.Special cases involving reflexiveuse of negation and complementation are discovered..RT.IP 7.Operations applying to constants are evaluated..PPThe.II acommuteroutine, called for associative and commutative operators,discovers clusters of the same operator at the top levelsof the current tree, and arranges them in a list:for `a+((b+c)+(d+f))'the list would be`a,b,c,d,e,f'.After each subtree is optimized, the list is sorted indecreasing difficulty of computation;as mentioned above,the code generation algorithm works best when left operandsare the difficult ones.The `degree of difficulty'computed is actually finer thanthe mere number of registers required;a constant is considered simplerthan the address of a static or external, which is simplerthan reference to a variable.This makes it easy to fold all the constantstogether,and also to merge together the sum of a constant and the address ofa staticor external (since in such nodes there is space foran `offset' value).There are also special cases, like multiplication by 1 and addition of 0..IIA special routine is invoked to handle sums of products..II Distribis based on the fact that it is betterto compute `c1*c2*x + c1*y' as `c1*(c2*x + y)'and makes the divisibility tests required to assure thecorrectness of the transformation.This transformation is rarelypossible with code directly written by the user,but it invariably occurs as a result of theimplementation of multi-dimensional arrays..PPFinally,.II acommutereconstructs a tree from the listof expressions which result.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -