Changeset 2091 for src/ASM/CPP2012policy/problem.tex
 Timestamp:
 Jun 15, 2012, 1:35:46 PM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/ASM/CPP2012policy/problem.tex
r2086 r2091 2 2 3 3 The problem of branch displacement optimisation, also known as jump encoding, is 4 a wellknown problem in assembler design. It is caused by the fact that in 5 many architecture sets, the encoding (and therefore size) of some instructions 6 depends on the distance to their operand (the instruction 'span'). The branch 7 displacement optimisation problem consists in encoding these spandependent 8 instructions in such a way that the resulting program is as small as possible. 4 a wellknown problem in assembler design~\cite{Hyde2006}. It is caused by the 5 fact that in many architecture sets, the encoding (and therefore size) of some 6 instructions depends on the distance to their operand (the instruction 'span'). 7 The branch displacement optimisation problem consists in encoding these 8 spandependent instructions in such a way that the resulting program is as 9 small as possible. 9 10 10 11 This problem is the subject of the present paper. After introducing the problem 11 12 in more detail, we will discuss the solutions used by other compilers, present 12 13 the algorithm used by us in the CerCo assembler, and discuss its verification, 13 that is the proofs of termination and correctness. 14 that is the proofs of termination and correctness using the Matita theorem 15 prover~\cite{Asperti2007}. 14 16 15 17 The research presented in this paper has been executed within the CerCo project … … 21 23 22 24 With this optimisation, however, comes increased complexity and hence 23 increased possibility for error. We must make sure that the jumps are encoded 24 correctly, otherwise the assembled program will behave unpredictably. 25 increased possibility for error. We must make sure that the branch instructions 26 are encoded correctly, otherwise the assembled program will behave 27 unpredictably. 25 28 26 29 \section{The branch displacement optimisation problem} 27 30 28 In most modern instruction sets , the only spandependent instructions are29 branch instructions. Taking the ubiquitous x8664 instruction set as an30 example, we find that it contains are eleven different forms of the31 unconditional jumpinstruction, all with different ranges, instruction sizes31 In most modern instruction sets that have them, the only spandependent 32 instructions are branch instructions. Taking the ubiquitous x8664 instruction 33 set as an example, we find that it contains are eleven different forms of the 34 unconditional branch instruction, all with different ranges, instruction sizes 32 35 and semantics (only six are valid in 64bit mode, for example). Some examples 33 36 are shown in figure~\ref{f:x86jumps}. … … 46 49 \end{tabular} 47 50 \end{center} 48 \caption{List of x86 jumpinstructions}51 \caption{List of x86 branch instructions} 49 52 \label{f:x86jumps} 50 53 \end{figure} … … 67 70 \end{tabular} 68 71 \end{center} 69 \caption{List of MCS51 jumpinstructions}72 \caption{List of MCS51 branch instructions} 70 73 \label{f:mcs51jumps} 71 74 \end{figure} 72 75 73 The conditional jump instruction is only available in short form, which 74 means that a conditional jump outside the short address range has to be 75 encoded using two jumps; the call instruction is only available in 76 absolute and long forms. 76 Conditional branch instructions are only available in short form, which 77 means that a conditional branch outside the short address range has to be 78 encoded using three branch instructions (for instructions whose logical 79 negation is available, it can be done with two branch instructions, but for 80 some instructions, this opposite is not available); the call instruction is 81 only available in absolute and long forms. 77 82 78 83 Note that even though the MCS51 architecture is much less advanced and more 79 simple than the x8664 architecture, the basic types of jump remain the same:80 a short jump with a limited range, an intrasegment jump and a jump that can 81 reach the entire available memory.84 simple than the x8664 architecture, the basic types of branch instruction 85 remain the same: a short jump with a limited range, an intrasegment jump and a 86 jump that can reach the entire available memory. 82 87 83 88 Generally, in the code that is sent to the assembler as input, the only 84 difference made between jumpinstructions is by semantics, not by span. This85 means that a distinction is made between the unconditional jump and the several86 kinds of conditional jump, but not between their short, absolute or long 87 variants.88 89 The algorithm used by the assembler to encode these jumps into the different90 machine instructions is known as the {\tt branch displacement algorithm}. The 91 optimisation problem consists in using as small an encoding as possible, thus92 minimising program length and execution time.89 difference made between branch instructions is by semantics, not by span. This 90 means that a distinction is made between an unconditional branch and the 91 several kinds of conditional branch, but not between their short, absolute or 92 long variants. 93 94 The algorithm used by the assembler to encode these branch instructions into 95 the different machine instructions is known as the {\em branch displacement 96 algorithm}. The optimisation problem consists in using as small an encoding as 97 possible, thus minimising program length and execution time. 93 98 94 99 This problem is known to be NPcomplete~\cite{Robertson1979,Szymanski1978}, … … 98 103 recently by Dickson~\cite{Dickson2008} for the x86 instruction set, is to use a 99 104 fixed point algorithm that starts out with the shortest possible encoding (all 100 jumps encoded as short jumps, which is very probably not a correct solution) 101 and then iterates over the program to reencode those jumps whose target is 102 outside their range.105 branch instruction encoded as short jumps, which is very probably not a correct 106 solution) and then iterates over the program to reencode those branch 107 instructions whose target is outside their range. 103 108 104 109 \subsection*{Adding absolute jumps} … … 109 114 110 115 Here, termination of the smallest fixed point algorithm is easy to prove. All 111 jumps start out encoded as short jumps, which means that the distance between 112 any jump and its target is as short as possible. If, in this situation, there 113 is a jump $j$ whose span is not within the range for a short jump, we can be 114 sure that we can never reach a situation where the span of $j$ is so small that 115 it can be encoded as a short jump. This reasoning holds throughout the 116 subsequent iterations of the algorithm: short jumps can change into long jumps, 117 but not vice versa (spans only increase). Hence, the algorithm either 118 terminates when a fixed point is reached or when all short jumps have been 119 changed into long jumps.116 branch instructions start out encoded as short jumps, which means that the 117 distance between any branch instruction and its target is as short as possible. 118 If, in this situation, there is a branch instruction $b$ whose span is not 119 within the range for a short jump, we can be sure that we can never reach a 120 situation where the span of $j$ is so small that it can be encoded as a short 121 jump. This argument continues to hold throughout the subsequent iterations of 122 the algorithm: short jumps can change into long jumps, but not vice versa 123 (spans only increase). Hence, the algorithm either terminates when a fixed 124 point is reached or when all short jumps have been changed into long jumps. 120 125 121 126 Also, we can be certain that we have reached an optimal solution: a short jump … … 125 130 the absolute jump. 126 131 127 The reason for this is that with absolute jumps, the encoding of a jump no 128 longer depends only on the distance between the jump and its target: in order 129 for an absolute jump to be possible, they need to be in the same segment (for 130 the MCS51, this means that the first 5 bytes of their addresses have to be 131 equal). It is therefore entirely possible for two jumps with the same span to 132 be encoded in different ways (absolute if the jump and its target are in the 133 same segment, long if this is not the case). 134 135 This invalidates the termination argument: a jump, once encoded as a long jump, 136 can be reencoded during a later iteration as an absolute jump. Consider the 137 program shown in figure~\ref{f:term_example}. At the start of the first 138 iteration, both the jump to {\tt X} and the jump to $\mathtt{L}_{0}$ are 139 encoded as small jumps. Let us assume that in this case, the placement of 140 $\mathtt{L}_{0}$ and the jump to it are such that $\mathtt{L}_{0}$ is just 141 outside the segment that contains this jump. Let us also assume that the 142 distance between $\mathtt{L}_{0}$ and the jump to it are too large for the jump 143 to be encoded as a short jump. 144 145 All this means that in the second iteration, the jump to $\mathtt{L}_{0}$ will 146 be encoded as a long jump. If we assume that the jump to {\tt X} is encoded as 147 a long jump as well, the size of the jump will increase and $\mathtt{L}_{0}$ 148 will be `propelled' into the same segment as its jump. Hence, in the third 149 iteration, it can be encoded as an absolute jump. At first glance, there is 150 nothing that prevents us from making a construction where two jumps interact 151 in such a way as to keep switching between long and absolute encodings for 152 an indefinite amount of iterations. 132 The reason for this is that with absolute jumps, the encoding of a branch 133 instruction no longer depends only on the distance between the branch 134 instruction and its target: in order for an absolute jump to be possible, they 135 need to be in the same segment (for the MCS51, this means that the first 5 136 bytes of their addresses have to be equal). It is therefore entirely possible 137 for two branch instructions with the same span to be encoded in different ways 138 (absolute if the branch instruction and its target are in the same segment, 139 long if this is not the case). 140 141 This invalidates the termination argument: a branch instruction, once encoded 142 as a long jump, can be reencoded during a later iteration as an absolute jump. 143 Consider the program shown in figure~\ref{f:term_example}. At the start of the 144 first iteration, both the branch to {\tt X} and the branch to $\mathtt{L}_{0}$ 145 are encoded as small jumps. Let us assume that in this case, the placement of 146 $\mathtt{L}_{0}$ and the branch to it are such that $\mathtt{L}_{0}$ is just 147 outside the segment that contains this branch. Let us also assume that the 148 distance between $\mathtt{L}_{0}$ and the branch to it are too large for the 149 branch instruction to be encoded as a short jump. 150 151 All this means that in the second iteration, the branch to $\mathtt{L}_{0}$ will 152 be encoded as a long jump. If we assume that the branch to {\tt X} is encoded as 153 a long jump as well, the size of the branch instruction will increase and 154 $\mathtt{L}_{0}$ will be `propelled' into the same segment as its branch 155 instruction, because every subsequent instruction will move one byte forward. 156 Hence, in the third iteration, the branch to $\mathtt{L}_{0}$ can be encoded as 157 an absolute jump. At first glance, there is nothing that prevents us from 158 making a construction where two branch instructions interact in such a way as 159 to keep switching between long and absolute encodings for an indefinite amount 160 of iterations. 153 161 154 162 \begin{figure}[h] … … 168 176 problem is NPcomplete. In this explanation, a condition for NPcompleteness 169 177 is the fact that programs be allowed to contain {\em pathological} jumps. 170 These are jumps that can normally not be encoded as a short(er) jump, but gain171 this property when some other jumps are encoded as a long(er) jump. This is172 exactly what happens in figure~\ref{f:term_example}: by encoding the first 173 jump as a long jump, another jump switches from long to absolute 174 (which is shorter).178 These are branch instructions that can normally not be encoded as a short(er) 179 jump, but gain this property when some other branch instructions are encoded as 180 a long(er) jump. This is exactly what happens in figure~\ref{f:term_example}: 181 by encoding the first branch instruction as a long jump, another branch 182 instructions switches from long to absolute (which is shorter). 175 183 176 184 The optimality argument no longer holds either. Let us consider the program … … 178 186 $\mathtt{L}_{0}$ and $\mathtt{L}_{1}$ is such that if {\tt jmp X} is encoded 179 187 as a short jump, there is a segment border just after $\mathtt{L}_{1}$. Let 180 us also assume that the three jumps to $\mathtt{L}_{1}$ are all in the same188 us also assume that the three branches to $\mathtt{L}_{1}$ are all in the same 181 189 segment, but far enough away from $\mathtt{L}_{1}$ that they cannot be encoded 182 190 as short jumps. 183 191 184 192 Then, if {\tt jmp X} were to be encoded as a short jump, which is clearly 185 possible, all of the jumps to $\mathtt{L}_{1}$ would have to be encoded as193 possible, all of the branches to $\mathtt{L}_{1}$ would have to be encoded as 186 194 long jumps. However, if {\tt jmp X} were to be encoded as a long jump, and 187 195 therefore increase in size, $\mathtt{L}_{1}$ would be `propelled' across the 188 segment border, so that the three jumps to $\mathtt{L}_{1}$ could be encoded196 segment border, so that the three branches to $\mathtt{L}_{1}$ could be encoded 189 197 as absolute jumps. Depending on the relative sizes of long and absolute jumps, 190 198 this solution might actually be smaller than the one reached by the smallest
Note: See TracChangeset
for help on using the changeset viewer.