Pristine Ack-5.5
[Ack-5.5.git] / doc / ego / bo / bo1
1 .bp
2 .NH 1
3 Branch Optimization
4 .NH 2
5 Introduction
6 .PP
7 The Branch Optimization phase (BO) performs two related
8 (branch) optimizations.
9 .NH 3
10 Fusion of basic blocks
11 .PP
12 If two basic blocks B1 and B2 have the following properties:
13 .DS
14 SUCC(B1) = {B2}
15 PRED(B2) = {B1}
16 .DE
17 then B1 and B2 can be combined into one basic block.
18 If B1 ends in an unconditional jump to the beginning of B2, this
19 jump can be eliminated,
20 hence saving a little execution time and object code size.
21 This technique can be used to eliminate some deficiencies
22 introduced by the front ends (for example, the "C" front end
23 translates switch statements inefficiently due to its one pass nature).
24 .NH 3
25 While-loop optimization
26 .PP
27 The straightforward way to translate a while loop is to
28 put the test for loop termination at the beginning of the loop.
29 .DS
30 while cond loop                       \kyLAB1: \kxTest cond
31    body of the loop     --->\h'|\nxu'Branch On False To LAB2
32 end loop\h'|\nxu'code for body of loop
33 \h'|\nxu'Branch To LAB1
34 \h'|\nyu'LAB2:
35
36 Fig. 10.1 Example of Branch Optimization
37 .DE
38 If the condition fails at the Nth iteration, the following code
39 gets executed (dynamically):
40 .DS
41 .TS
42 l l l.
43 N       *       conditional branch (which fails N-1 times)
44 N-1     *       unconditional branch
45 N-1     *       body of the loop
46 .TE
47 .DE
48 An alternative translation is:
49 .DS
50      Branch To LAB2
51 LAB1:
52      code for body of loop
53 LAB2:
54      Test cond
55      Branch On True To LAB1
56 .DE
57 This translation results in the following profile:
58 .DS
59 .TS
60 l l l.
61 N       *       conditional branch (which succeeds N-1 times)
62 1       *       unconditional branch
63 N-1     *       body of the loop
64 .TE
65 .DE
66 So the second translation will be significantly faster if N >> 2.
67 If N=2, execution time will be slightly increased.
68 On the average, the program will be speeded up.
69 Note that the code sizes of the two translations will be the same.
70 .NH 2
71 Implementation
72 .PP
73 The basic block fusion technique is implemented
74 by traversing the control flow graph of a procedure,
75 looking for basic blocks B with only one successor (S).
76 If one is found, it is checked if S has only one predecessor
77 (which has to be B).
78 If so, the two basic blocks can in principle be combined.
79 However, as one basic block will have to be moved,
80 the textual order of the basic blocks will be altered.
81 This reordering causes severe problems in the presence
82 of conditional jumps.
83 For example, if S ends in a conditional branch,
84 the basic block that comes textually next to S must stay
85 in that position.
86 So the transformation in Fig. 10.2 is illegal.
87 .DS
88 .TS
89 l l l l l.
90 LAB1:   S1              LAB1:   S1
91         BRA LAB2                        S2
92         ...     -->             BEQ LAB3
93 LAB2:   S2                      ...
94         BEQ LAB3                        S3
95         S3
96 .TE
97
98 Fig. 10.2 An illegal transformation of Branch Optimization
99 .DE
100 If B is moved towards S the same problem occurs if the block before B
101 ends in a conditional jump.
102 The problem could be solved by adding one extra branch,
103 but this would reduce the gains of the optimization to zero.
104 Hence the optimization will only be done if the block that
105 follows S (in the textual order) is not a successor of S.
106 This condition assures that S does not end in a conditional branch.
107 The condition always holds for the code generated by the "C"
108 front end for a switch statement.
109 .PP
110 After the transformation has been performed,
111 some attributes of the basic blocks involved (such as successor and
112 predecessor sets and immediate dominator) must be recomputed.
113 .PP
114 The while-loop technique is applied to one loop at a time.
115 The list of basic blocks of the loop is traversed to find
116 a block B that satisfies the following conditions:
117 .IP 1.
118 the textually next block to B is not part of the loop
119 .IP 2.
120 the last instruction of B is an unconditional branch;
121 hence B has only one successor, say S
122 .IP 3.
123 the textually next block of B is a successor of S
124 .IP 4.
125 the last instruction of S is a conditional branch
126 .LP
127 If such a block B is found, the control flow graph is changed
128 as depicted in Fig. 10.3.
129 .DS
130 .ft 5
131        |                                    |
132        |                                    v
133        v                                    |
134        |-----<------|                       ----->-----|
135    ____|____        |                                  |
136    |       |        |               |-------|          |
137    |  S1   |        |               |       v          |
138    |  Bcc  |        |               |     ....         |
139 |--|       |        |               |                  |
140 |  ---------        |               |   ----|----      |
141 |                   |               |   |       |      |
142 |     ....          ^               |   |  S2   |      |
143 |                   |               |   |       |      |
144 |   ---------       |               |   |       |      |
145 v   |       |       |               ^   ---------      |
146 |   |  S2   |       |               |       |          |
147 |   | BRA   |       |               |       |-----<-----
148 |   |       |       |               |       v
149 |   ---------       |               |   ____|____
150 |       |           |               |   |       |
151 |       ------>------               |   |  S1   |
152 |                                   |   |  Bnn  |
153 |-------|                           |   |       |
154         |                           |   ----|----
155         v                           |       |
156                                     |----<--|
157                                             |
158                                             v
159 .ft R
160
161 Fig. 10.3 Transformation of the CFG by Branch Optimization
162 .DE