bo1 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 LAB1: Test cond
  31. body of the loop ---> Branch On False To LAB2
  32. end loop code for body of loop
  33. Branch To LAB1
  34. LAB2:
  35. Fig. 10.1 Example of Branch Optimization
  36. .DE
  37. If the condition fails at the Nth iteration, the following code
  38. gets executed (dynamically):
  39. .DS
  40. N * conditional branch (which fails N-1 times)
  41. N-1 * unconditional branch
  42. N-1 * body of the loop
  43. .DE
  44. An alternative translation is:
  45. .DS
  46. Branch To LAB2
  47. LAB1:
  48. code for body of loop
  49. LAB2:
  50. Test cond
  51. Branch On True To LAB1
  52. .DE
  53. This translation results in the following profile:
  54. .DS
  55. N * conditional branch (which succeeds N-1 times)
  56. 1 * unconditional branch
  57. N-1 * body of the loop
  58. .DE
  59. So the second translation will be significantly faster if N >> 2.
  60. If N=2, execution time will be slightly increased.
  61. On the average, the program will be speeded up.
  62. Note that the code sizes of the two translations will be the same.
  63. .NH 2
  64. Implementation
  65. .PP
  66. The basic block fusion technique is implemented
  67. by traversing the control flow graph of a procedure,
  68. looking for basic blocks B with only one successor (S).
  69. If one is found, it is checked if S has only one predecessor
  70. (which has to be B).
  71. If so, the two basic blocks can in principle be combined.
  72. However, as one basic block will have to be moved,
  73. the textual order of the basic blocks will be altered.
  74. This reordering causes severe problems in the presence
  75. of conditional jumps.
  76. For example, if S ends in a conditional branch,
  77. the basic block that comes textually next to S must stay
  78. in that position.
  79. So the transformation in Fig. 10.2 is illegal.
  80. .DS
  81. LAB1: S1 LAB1: S1
  82. BRA LAB2 S2
  83. ... --> BEQ LAB3
  84. LAB2: S2 ...
  85. BEQ LAB3 S3
  86. S3
  87. Fig. 10.2 An illegal transformation of Branch Optimization
  88. .DE
  89. If B is moved towards S the same problem occurs if the block before B
  90. ends in a conditional jump.
  91. The problem could be solved by adding one extra branch,
  92. but this would reduce the gains of the optimization to zero.
  93. Hence the optimization will only be done if the block that
  94. follows S (in the textual order) is not a successor of S.
  95. This condition assures that S does not end in a conditional branch.
  96. The condition always holds for the code generated by the "C"
  97. front end for a switch statement.
  98. .PP
  99. After the transformation has been performed,
  100. some attributes of the basic blocks involved (such as successor and
  101. predecessor sets and immediate dominator) must be recomputed.
  102. .PP
  103. The while-loop technique is applied to one loop at a time.
  104. The list of basic blocks of the loop is traversed to find
  105. a block B that satisfies the following conditions:
  106. .IP 1.
  107. the textually next block to B is not part of the loop
  108. .IP 2.
  109. the last instruction of B is an unconditional branch;
  110. hence B has only one successor, say S
  111. .IP 3.
  112. the textually next block of B is a successor of S
  113. .IP 4.
  114. the last instruction of S is a conditional branch
  115. .LP
  116. If such a block B is found, the control flow graph is changed
  117. as depicted in Fig. 10.3.
  118. .DS
  119. | |
  120. | v
  121. v |
  122. |-----<------| ----->-----|
  123. ____|____ | |
  124. | | | |-------| |
  125. | S1 | | | v |
  126. | Bcc | | | .... |
  127. |--| | | | |
  128. | --------- | | ----|---- |
  129. | | | | | |
  130. | .... ^ | | S2 | |
  131. | | | | | |
  132. | --------- | | | | |
  133. v | | | ^ --------- |
  134. | | S2 | | | | |
  135. | | BRA | | | |-----<-----
  136. | | | | | v
  137. | --------- | | ____|____
  138. | | | | | |
  139. | ------>------ | | S1 |
  140. | | | Bnn |
  141. |-------| | | |
  142. | | ----|----
  143. v | |
  144. |----<--|
  145. |
  146. v
  147. Fig. 10.3 Transformation of the CFG by Branch Optimization
  148. .DE