Debian Codegraph @

generic/tclOptimize.c

raw
1 /*
2 * tclOptimize.c --
3 *
4 * This file contains the bytecode optimizer.
5 *
6 * Copyright (c) 2013 by Donal Fellows.
7 *
8 * See the file "license.terms" for information on usage and redistribution of
9 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 */
11
12 #include "tclInt.h"
13 #include "tclCompile.h"
14 #include <assert.h>
15
16 /*
17 * Forward declarations.
18 */
19
20 static void AdvanceJumps(CompileEnv *envPtr);
21 static void ConvertZeroEffectToNOP(CompileEnv *envPtr);
22 static void LocateTargetAddresses(CompileEnv *envPtr,
23 Tcl_HashTable *tablePtr);
24 static void TrimUnreachable(CompileEnv *envPtr);
25
26 /*
27 * Helper macros.
28 */
29
30 #define DefineTargetAddress(tablePtr, address) \
31 ((void) Tcl_CreateHashEntry((tablePtr), (void *) (address), &isNew))
32 #define IsTargetAddress(tablePtr, address) \
33 (Tcl_FindHashEntry((tablePtr), (void *) (address)) != NULL)
34 #define AddrLength(address) \
35 (tclInstructionTable[*(unsigned char *)(address)].numBytes)
36 #define InstLength(instruction) \
37 (tclInstructionTable[UCHAR(instruction)].numBytes)
38
39 /*
40 * ----------------------------------------------------------------------
41 *
42 * LocateTargetAddresses --
43 *
44 * Populate a hash table with places that we need to be careful around
45 * because they're the targets of various kinds of jumps and other
46 * non-local behavior.
47 *
48 * ----------------------------------------------------------------------
49 */
50
51 static void
52 LocateTargetAddresses(
53 CompileEnv *envPtr,
54 Tcl_HashTable *tablePtr)
55 {
56 unsigned char *currentInstPtr, *targetInstPtr;
57 int isNew, i;
58 Tcl_HashEntry *hPtr;
59 Tcl_HashSearch hSearch;
60
61 Tcl_InitHashTable(tablePtr, TCL_ONE_WORD_KEYS);
62
63 /*
64 * The starts of commands represent target addresses.
65 */
66
67 for (i=0 ; i<envPtr->numCommands ; i++) {
68 DefineTargetAddress(tablePtr,
69 envPtr->codeStart + envPtr->cmdMapPtr[i].codeOffset);
70 }
71
72 /*
73 * Find places where we should be careful about replacing instructions
74 * because they are the targets of various types of jumps.
75 */
76
77 for (currentInstPtr = envPtr->codeStart ;
78 currentInstPtr < envPtr->codeNext ;
79 currentInstPtr += AddrLength(currentInstPtr)) {
80 switch (*currentInstPtr) {
81 case INST_JUMP1:
82 case INST_JUMP_TRUE1:
83 case INST_JUMP_FALSE1:
84 targetInstPtr = currentInstPtr+TclGetInt1AtPtr(currentInstPtr+1);
85 goto storeTarget;
86 case INST_JUMP4:
87 case INST_JUMP_TRUE4:
88 case INST_JUMP_FALSE4:
89 case INST_START_CMD:
90 targetInstPtr = currentInstPtr+TclGetInt4AtPtr(currentInstPtr+1);
91 goto storeTarget;
92 case INST_BEGIN_CATCH4:
93 targetInstPtr = envPtr->codeStart + envPtr->exceptArrayPtr[
94 TclGetUInt4AtPtr(currentInstPtr+1)].codeOffset;
95 storeTarget:
96 DefineTargetAddress(tablePtr, targetInstPtr);
97 break;
98 case INST_JUMP_TABLE:
99 hPtr = Tcl_FirstHashEntry(
100 &JUMPTABLEINFO(envPtr, currentInstPtr+1)->hashTable,
101 &hSearch);
102 for (; hPtr ; hPtr = Tcl_NextHashEntry(&hSearch)) {
103 targetInstPtr = currentInstPtr +
104 PTR2INT(Tcl_GetHashValue(hPtr));
105 DefineTargetAddress(tablePtr, targetInstPtr);
106 }
107 break;
108 case INST_RETURN_CODE_BRANCH:
109 for (i=TCL_ERROR ; i<TCL_CONTINUE+1 ; i++) {
110 DefineTargetAddress(tablePtr, currentInstPtr + 2*i - 1);
111 }
112 break;
113 }
114 }
115
116 /*
117 * Add a marker *after* the last bytecode instruction. WARNING: points to
118 * one past the end!
119 */
120
121 DefineTargetAddress(tablePtr, currentInstPtr);
122
123 /*
124 * Enter in the targets of exception ranges.
125 */
126
127 for (i=0 ; i<envPtr->exceptArrayNext ; i++) {
128 ExceptionRange *rangePtr = &envPtr->exceptArrayPtr[i];
129
130 if (rangePtr->type == CATCH_EXCEPTION_RANGE) {
131 targetInstPtr = envPtr->codeStart + rangePtr->catchOffset;
132 DefineTargetAddress(tablePtr, targetInstPtr);
133 } else {
134 targetInstPtr = envPtr->codeStart + rangePtr->breakOffset;
135 DefineTargetAddress(tablePtr, targetInstPtr);
136 if (rangePtr->continueOffset >= 0) {
137 targetInstPtr = envPtr->codeStart + rangePtr->continueOffset;
138 DefineTargetAddress(tablePtr, targetInstPtr);
139 }
140 }
141 }
142 }
143
144 /*
145 * ----------------------------------------------------------------------
146 *
147 * TrimUnreachable --
148 *
149 * Converts code that provably can't be executed into NOPs and reduces
150 * the overall reported length of the bytecode where that is possible.
151 *
152 * ----------------------------------------------------------------------
153 */
154
155 static void
156 TrimUnreachable(
157 CompileEnv *envPtr)
158 {
159 unsigned char *currentInstPtr;
160 Tcl_HashTable targets;
161
162 LocateTargetAddresses(envPtr, &targets);
163
164 for (currentInstPtr = envPtr->codeStart ;
165 currentInstPtr < envPtr->codeNext-1 ;
166 currentInstPtr += AddrLength(currentInstPtr)) {
167 int clear = 0;
168
169 if (*currentInstPtr != INST_DONE) {
170 continue;
171 }
172
173 while (!IsTargetAddress(&targets, currentInstPtr + 1 + clear)) {
174 clear += AddrLength(currentInstPtr + 1 + clear);
175 }
176 if (currentInstPtr + 1 + clear == envPtr->codeNext) {
177 envPtr->codeNext -= clear;
178 } else {
179 while (clear --> 0) {
180 *(currentInstPtr + 1 + clear) = INST_NOP;
181 }
182 }
183 }
184
185 Tcl_DeleteHashTable(&targets);
186 }
187
188 /*
189 * ----------------------------------------------------------------------
190 *
191 * ConvertZeroEffectToNOP --
192 *
193 * Replace PUSH/POP sequences (when non-hazardous) with NOPs. Also
194 * replace PUSH empty/STR_CONCAT and TRY_CVT_NUMERIC (when followed by an
195 * operation that guarantees the check for arithmeticity) and eliminate
196 * LNOT when we can invert the following JUMP condition.
197 *
198 * ----------------------------------------------------------------------
199 */
200
201 static void
202 ConvertZeroEffectToNOP(
203 CompileEnv *envPtr)
204 {
205 unsigned char *currentInstPtr;
206 int size;
207 Tcl_HashTable targets;
208
209 LocateTargetAddresses(envPtr, &targets);
210 for (currentInstPtr = envPtr->codeStart ;
211 currentInstPtr < envPtr->codeNext ; currentInstPtr += size) {
212 int blank = 0, i, nextInst;
213
214 size = AddrLength(currentInstPtr);
215 while ((currentInstPtr + size < envPtr->codeNext)
216 && *(currentInstPtr+size) == INST_NOP) {
217 if (IsTargetAddress(&targets, currentInstPtr + size)) {
218 break;
219 }
220 size += InstLength(INST_NOP);
221 }
222 if (IsTargetAddress(&targets, currentInstPtr + size)) {
223 continue;
224 }
225 nextInst = *(currentInstPtr + size);
226 switch (*currentInstPtr) {
227 case INST_PUSH1:
228 if (nextInst == INST_POP) {
229 blank = size + InstLength(nextInst);
230 } else if (nextInst == INST_STR_CONCAT1
231 && TclGetUInt1AtPtr(currentInstPtr + size + 1) == 2) {
232 Tcl_Obj *litPtr = TclFetchLiteral(envPtr,
233 TclGetUInt1AtPtr(currentInstPtr + 1));
234 int numBytes;
235
236 (void) Tcl_GetStringFromObj(litPtr, &numBytes);
237 if (numBytes == 0) {
238 blank = size + InstLength(nextInst);
239 }
240 }
241 break;
242 case INST_PUSH4:
243 if (nextInst == INST_POP) {
244 blank = size + 1;
245 } else if (nextInst == INST_STR_CONCAT1
246 && TclGetUInt1AtPtr(currentInstPtr + size + 1) == 2) {
247 Tcl_Obj *litPtr = TclFetchLiteral(envPtr,
248 TclGetUInt4AtPtr(currentInstPtr + 1));
249 int numBytes;
250
251 (void) Tcl_GetStringFromObj(litPtr, &numBytes);
252 if (numBytes == 0) {
253 blank = size + InstLength(nextInst);
254 }
255 }
256 break;
257
258 case INST_LNOT:
259 switch (nextInst) {
260 case INST_JUMP_TRUE1:
261 blank = size;
262 *(currentInstPtr + size) = INST_JUMP_FALSE1;
263 break;
264 case INST_JUMP_FALSE1:
265 blank = size;
266 *(currentInstPtr + size) = INST_JUMP_TRUE1;
267 break;
268 case INST_JUMP_TRUE4:
269 blank = size;
270 *(currentInstPtr + size) = INST_JUMP_FALSE4;
271 break;
272 case INST_JUMP_FALSE4:
273 blank = size;
274 *(currentInstPtr + size) = INST_JUMP_TRUE4;
275 break;
276 }
277 break;
278
279 case INST_TRY_CVT_TO_NUMERIC:
280 switch (nextInst) {
281 case INST_JUMP_TRUE1:
282 case INST_JUMP_TRUE4:
283 case INST_JUMP_FALSE1:
284 case INST_JUMP_FALSE4:
285 case INST_INCR_SCALAR1:
286 case INST_INCR_ARRAY1:
287 case INST_INCR_ARRAY_STK:
288 case INST_INCR_SCALAR_STK:
289 case INST_INCR_STK:
290 case INST_LOR:
291 case INST_LAND:
292 case INST_EQ:
293 case INST_NEQ:
294 case INST_LT:
295 case INST_LE:
296 case INST_GT:
297 case INST_GE:
298 case INST_MOD:
299 case INST_LSHIFT:
300 case INST_RSHIFT:
301 case INST_BITOR:
302 case INST_BITXOR:
303 case INST_BITAND:
304 case INST_EXPON:
305 case INST_ADD:
306 case INST_SUB:
307 case INST_DIV:
308 case INST_MULT:
309 case INST_LNOT:
310 case INST_BITNOT:
311 case INST_UMINUS:
312 case INST_UPLUS:
313 case INST_TRY_CVT_TO_NUMERIC:
314 blank = size;
315 break;
316 }
317 break;
318 }
319
320 if (blank > 0) {
321 for (i=0 ; i<blank ; i++) {
322 *(currentInstPtr + i) = INST_NOP;
323 }
324 size = blank;
325 }
326 }
327 Tcl_DeleteHashTable(&targets);
328 }
329
330 /*
331 * ----------------------------------------------------------------------
332 *
333 * AdvanceJumps --
334 *
335 * Advance jumps past NOPs and chained JUMPs. After this runs, the only
336 * JUMPs that jump to a NOP or a JUMP will be length-1 ones that run out
337 * of room in their opcode to be targeted to where they really belong.
338 *
339 * ----------------------------------------------------------------------
340 */
341
342 static void
343 AdvanceJumps(
344 CompileEnv *envPtr)
345 {
346 unsigned char *currentInstPtr;
347 Tcl_HashTable jumps;
348
349 for (currentInstPtr = envPtr->codeStart ;
350 currentInstPtr < envPtr->codeNext-1 ;
351 currentInstPtr += AddrLength(currentInstPtr)) {
352 int offset, delta, isNew;
353
354 switch (*currentInstPtr) {
355 case INST_JUMP1:
356 case INST_JUMP_TRUE1:
357 case INST_JUMP_FALSE1:
358 offset = TclGetInt1AtPtr(currentInstPtr + 1);
359 Tcl_InitHashTable(&jumps, TCL_ONE_WORD_KEYS);
360 for (delta=0 ; offset+delta != 0 ;) {
361 if (offset + delta < -128 || offset + delta > 127) {
362 break;
363 }
364 Tcl_CreateHashEntry(&jumps, INT2PTR(offset), &isNew);
365 if (!isNew) {
366 offset = TclGetInt1AtPtr(currentInstPtr + 1);
367 break;
368 }
369 offset += delta;
370 switch (*(currentInstPtr + offset)) {
371 case INST_NOP:
372 delta = InstLength(INST_NOP);
373 continue;
374 case INST_JUMP1:
375 delta = TclGetInt1AtPtr(currentInstPtr + offset + 1);
376 continue;
377 case INST_JUMP4:
378 delta = TclGetInt4AtPtr(currentInstPtr + offset + 1);
379 continue;
380 }
381 break;
382 }
383 Tcl_DeleteHashTable(&jumps);
384 TclStoreInt1AtPtr(offset, currentInstPtr + 1);
385 continue;
386
387 case INST_JUMP4:
388 case INST_JUMP_TRUE4:
389 case INST_JUMP_FALSE4:
390 Tcl_InitHashTable(&jumps, TCL_ONE_WORD_KEYS);
391 Tcl_CreateHashEntry(&jumps, INT2PTR(0), &isNew);
392 for (offset = TclGetInt4AtPtr(currentInstPtr + 1); offset!=0 ;) {
393 Tcl_CreateHashEntry(&jumps, INT2PTR(offset), &isNew);
394 if (!isNew) {
395 offset = TclGetInt4AtPtr(currentInstPtr + 1);
396 break;
397 }
398 switch (*(currentInstPtr + offset)) {
399 case INST_NOP:
400 offset += InstLength(INST_NOP);
401 continue;
402 case INST_JUMP1:
403 offset += TclGetInt1AtPtr(currentInstPtr + offset + 1);
404 continue;
405 case INST_JUMP4:
406 offset += TclGetInt4AtPtr(currentInstPtr + offset + 1);
407 continue;
408 }
409 break;
410 }
411 Tcl_DeleteHashTable(&jumps);
412 TclStoreInt4AtPtr(offset, currentInstPtr + 1);
413 continue;
414 }
415 }
416 }
417
418 /*
419 * ----------------------------------------------------------------------
420 *
421 * TclOptimizeBytecode --
422 *
423 * A very simple peephole optimizer for bytecode.
424 *
425 * ----------------------------------------------------------------------
426 */
427
428 void
429 TclOptimizeBytecode(
430 void *envPtr)
431 {
432 ConvertZeroEffectToNOP(envPtr);
433 AdvanceJumps(envPtr);
434 TrimUnreachable(envPtr);
435 }
436
437 /*
438 * Local Variables:
439 * mode: c
440 * c-basic-offset: 4
441 * fill-column: 78
442 * tab-width: 8
443 * End:
444 */