]> git.proxmox.com Git - qemu.git/blob - tcg/optimize.c
tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases
[qemu.git] / tcg / optimize.c
1 /*
2 * Optimizations for Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "qemu-common.h"
32 #include "tcg-op.h"
33
34 #define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
37
38 typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
42 TCG_TEMP_HAS_COPY,
43 TCG_TEMP_ANY
44 } tcg_temp_state;
45
46 struct tcg_temp_info {
47 tcg_temp_state state;
48 uint16_t prev_copy;
49 uint16_t next_copy;
50 tcg_target_ulong val;
51 };
52
53 static struct tcg_temp_info temps[TCG_MAX_TEMPS];
54
55 /* Reset TEMP's state to TCG_TEMP_ANY. If TEMP was a representative of some
56 class of equivalent temp's, a new representative should be chosen in this
57 class. */
58 static void reset_temp(TCGArg temp, int nb_temps, int nb_globals)
59 {
60 int i;
61 TCGArg new_base = (TCGArg)-1;
62 if (temps[temp].state == TCG_TEMP_HAS_COPY) {
63 for (i = temps[temp].next_copy; i != temp; i = temps[i].next_copy) {
64 if (i >= nb_globals) {
65 temps[i].state = TCG_TEMP_HAS_COPY;
66 new_base = i;
67 break;
68 }
69 }
70 for (i = temps[temp].next_copy; i != temp; i = temps[i].next_copy) {
71 if (new_base == (TCGArg)-1) {
72 temps[i].state = TCG_TEMP_ANY;
73 } else {
74 temps[i].val = new_base;
75 }
76 }
77 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
78 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
79 } else if (temps[temp].state == TCG_TEMP_COPY) {
80 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
81 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
82 new_base = temps[temp].val;
83 }
84 temps[temp].state = TCG_TEMP_ANY;
85 if (new_base != (TCGArg)-1 && temps[new_base].next_copy == new_base) {
86 temps[new_base].state = TCG_TEMP_ANY;
87 }
88 }
89
90 static int op_bits(TCGOpcode op)
91 {
92 const TCGOpDef *def = &tcg_op_defs[op];
93 return def->flags & TCG_OPF_64BIT ? 64 : 32;
94 }
95
96 static TCGOpcode op_to_movi(TCGOpcode op)
97 {
98 switch (op_bits(op)) {
99 case 32:
100 return INDEX_op_movi_i32;
101 case 64:
102 return INDEX_op_movi_i64;
103 default:
104 fprintf(stderr, "op_to_movi: unexpected return value of "
105 "function op_bits.\n");
106 tcg_abort();
107 }
108 }
109
110 static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args, TCGArg dst,
111 TCGArg src, int nb_temps, int nb_globals)
112 {
113 reset_temp(dst, nb_temps, nb_globals);
114 assert(temps[src].state != TCG_TEMP_COPY);
115 /* Don't try to copy if one of temps is a global or either one
116 is local and another is register */
117 if (src >= nb_globals && dst >= nb_globals &&
118 tcg_arg_is_local(s, src) == tcg_arg_is_local(s, dst)) {
119 assert(temps[src].state != TCG_TEMP_CONST);
120 if (temps[src].state != TCG_TEMP_HAS_COPY) {
121 temps[src].state = TCG_TEMP_HAS_COPY;
122 temps[src].next_copy = src;
123 temps[src].prev_copy = src;
124 }
125 temps[dst].state = TCG_TEMP_COPY;
126 temps[dst].val = src;
127 temps[dst].next_copy = temps[src].next_copy;
128 temps[dst].prev_copy = src;
129 temps[temps[dst].next_copy].prev_copy = dst;
130 temps[src].next_copy = dst;
131 }
132 gen_args[0] = dst;
133 gen_args[1] = src;
134 }
135
136 static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val,
137 int nb_temps, int nb_globals)
138 {
139 reset_temp(dst, nb_temps, nb_globals);
140 temps[dst].state = TCG_TEMP_CONST;
141 temps[dst].val = val;
142 gen_args[0] = dst;
143 gen_args[1] = val;
144 }
145
146 static TCGOpcode op_to_mov(TCGOpcode op)
147 {
148 switch (op_bits(op)) {
149 case 32:
150 return INDEX_op_mov_i32;
151 case 64:
152 return INDEX_op_mov_i64;
153 default:
154 fprintf(stderr, "op_to_mov: unexpected return value of "
155 "function op_bits.\n");
156 tcg_abort();
157 }
158 }
159
160 static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
161 {
162 switch (op) {
163 CASE_OP_32_64(add):
164 return x + y;
165
166 CASE_OP_32_64(sub):
167 return x - y;
168
169 CASE_OP_32_64(mul):
170 return x * y;
171
172 CASE_OP_32_64(and):
173 return x & y;
174
175 CASE_OP_32_64(or):
176 return x | y;
177
178 CASE_OP_32_64(xor):
179 return x ^ y;
180
181 case INDEX_op_shl_i32:
182 return (uint32_t)x << (uint32_t)y;
183
184 case INDEX_op_shl_i64:
185 return (uint64_t)x << (uint64_t)y;
186
187 case INDEX_op_shr_i32:
188 return (uint32_t)x >> (uint32_t)y;
189
190 case INDEX_op_shr_i64:
191 return (uint64_t)x >> (uint64_t)y;
192
193 case INDEX_op_sar_i32:
194 return (int32_t)x >> (int32_t)y;
195
196 case INDEX_op_sar_i64:
197 return (int64_t)x >> (int64_t)y;
198
199 case INDEX_op_rotr_i32:
200 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
201 return x;
202
203 case INDEX_op_rotr_i64:
204 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
205 return x;
206
207 case INDEX_op_rotl_i32:
208 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
209 return x;
210
211 case INDEX_op_rotl_i64:
212 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
213 return x;
214
215 CASE_OP_32_64(not):
216 return ~x;
217
218 CASE_OP_32_64(neg):
219 return -x;
220
221 CASE_OP_32_64(andc):
222 return x & ~y;
223
224 CASE_OP_32_64(orc):
225 return x | ~y;
226
227 CASE_OP_32_64(eqv):
228 return ~(x ^ y);
229
230 CASE_OP_32_64(nand):
231 return ~(x & y);
232
233 CASE_OP_32_64(nor):
234 return ~(x | y);
235
236 CASE_OP_32_64(ext8s):
237 return (int8_t)x;
238
239 CASE_OP_32_64(ext16s):
240 return (int16_t)x;
241
242 CASE_OP_32_64(ext8u):
243 return (uint8_t)x;
244
245 CASE_OP_32_64(ext16u):
246 return (uint16_t)x;
247
248 case INDEX_op_ext32s_i64:
249 return (int32_t)x;
250
251 case INDEX_op_ext32u_i64:
252 return (uint32_t)x;
253
254 default:
255 fprintf(stderr,
256 "Unrecognized operation %d in do_constant_folding.\n", op);
257 tcg_abort();
258 }
259 }
260
261 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
262 {
263 TCGArg res = do_constant_folding_2(op, x, y);
264 if (op_bits(op) == 32) {
265 res &= 0xffffffff;
266 }
267 return res;
268 }
269
270 /* Propagate constants and copies, fold constant expressions. */
271 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
272 TCGArg *args, TCGOpDef *tcg_op_defs)
273 {
274 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
275 TCGOpcode op;
276 const TCGOpDef *def;
277 TCGArg *gen_args;
278 TCGArg tmp;
279 /* Array VALS has an element for each temp.
280 If this temp holds a constant then its value is kept in VALS' element.
281 If this temp is a copy of other ones then this equivalence class'
282 representative is kept in VALS' element.
283 If this temp is neither copy nor constant then corresponding VALS'
284 element is unused. */
285
286 nb_temps = s->nb_temps;
287 nb_globals = s->nb_globals;
288 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
289
290 nb_ops = tcg_opc_ptr - gen_opc_buf;
291 gen_args = args;
292 for (op_index = 0; op_index < nb_ops; op_index++) {
293 op = gen_opc_buf[op_index];
294 def = &tcg_op_defs[op];
295 /* Do copy propagation */
296 if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_SIDE_EFFECTS))) {
297 assert(op != INDEX_op_call);
298 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
299 if (temps[args[i]].state == TCG_TEMP_COPY) {
300 args[i] = temps[args[i]].val;
301 }
302 }
303 }
304
305 /* For commutative operations make constant second argument */
306 switch (op) {
307 CASE_OP_32_64(add):
308 CASE_OP_32_64(mul):
309 CASE_OP_32_64(and):
310 CASE_OP_32_64(or):
311 CASE_OP_32_64(xor):
312 CASE_OP_32_64(eqv):
313 CASE_OP_32_64(nand):
314 CASE_OP_32_64(nor):
315 if (temps[args[1]].state == TCG_TEMP_CONST) {
316 tmp = args[1];
317 args[1] = args[2];
318 args[2] = tmp;
319 }
320 break;
321 default:
322 break;
323 }
324
325 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
326 switch (op) {
327 CASE_OP_32_64(shl):
328 CASE_OP_32_64(shr):
329 CASE_OP_32_64(sar):
330 CASE_OP_32_64(rotl):
331 CASE_OP_32_64(rotr):
332 if (temps[args[1]].state == TCG_TEMP_CONST
333 && temps[args[1]].val == 0) {
334 gen_opc_buf[op_index] = op_to_movi(op);
335 tcg_opt_gen_movi(gen_args, args[0], 0, nb_temps, nb_globals);
336 args += 3;
337 gen_args += 2;
338 continue;
339 }
340 break;
341 default:
342 break;
343 }
344
345 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
346 switch (op) {
347 CASE_OP_32_64(add):
348 CASE_OP_32_64(sub):
349 CASE_OP_32_64(shl):
350 CASE_OP_32_64(shr):
351 CASE_OP_32_64(sar):
352 CASE_OP_32_64(rotl):
353 CASE_OP_32_64(rotr):
354 CASE_OP_32_64(or):
355 CASE_OP_32_64(xor):
356 if (temps[args[1]].state == TCG_TEMP_CONST) {
357 /* Proceed with possible constant folding. */
358 break;
359 }
360 if (temps[args[2]].state == TCG_TEMP_CONST
361 && temps[args[2]].val == 0) {
362 if ((temps[args[0]].state == TCG_TEMP_COPY
363 && temps[args[0]].val == args[1])
364 || args[0] == args[1]) {
365 args += 3;
366 gen_opc_buf[op_index] = INDEX_op_nop;
367 } else {
368 gen_opc_buf[op_index] = op_to_mov(op);
369 tcg_opt_gen_mov(s, gen_args, args[0], args[1],
370 nb_temps, nb_globals);
371 gen_args += 2;
372 args += 3;
373 }
374 continue;
375 }
376 break;
377 default:
378 break;
379 }
380
381 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
382 switch (op) {
383 CASE_OP_32_64(and):
384 CASE_OP_32_64(mul):
385 if ((temps[args[2]].state == TCG_TEMP_CONST
386 && temps[args[2]].val == 0)) {
387 gen_opc_buf[op_index] = op_to_movi(op);
388 tcg_opt_gen_movi(gen_args, args[0], 0, nb_temps, nb_globals);
389 args += 3;
390 gen_args += 2;
391 continue;
392 }
393 break;
394 default:
395 break;
396 }
397
398 /* Simplify expression for "op r, a, a => mov r, a" cases */
399 switch (op) {
400 CASE_OP_32_64(or):
401 CASE_OP_32_64(and):
402 if (args[1] == args[2]) {
403 if (args[1] == args[0]) {
404 args += 3;
405 gen_opc_buf[op_index] = INDEX_op_nop;
406 } else {
407 gen_opc_buf[op_index] = op_to_mov(op);
408 tcg_opt_gen_mov(s, gen_args, args[0], args[1], nb_temps,
409 nb_globals);
410 gen_args += 2;
411 args += 3;
412 }
413 continue;
414 }
415 break;
416 default:
417 break;
418 }
419
420 /* Propagate constants through copy operations and do constant
421 folding. Constants will be substituted to arguments by register
422 allocator where needed and possible. Also detect copies. */
423 switch (op) {
424 CASE_OP_32_64(mov):
425 if ((temps[args[1]].state == TCG_TEMP_COPY
426 && temps[args[1]].val == args[0])
427 || args[0] == args[1]) {
428 args += 2;
429 gen_opc_buf[op_index] = INDEX_op_nop;
430 break;
431 }
432 if (temps[args[1]].state != TCG_TEMP_CONST) {
433 tcg_opt_gen_mov(s, gen_args, args[0], args[1],
434 nb_temps, nb_globals);
435 gen_args += 2;
436 args += 2;
437 break;
438 }
439 /* Source argument is constant. Rewrite the operation and
440 let movi case handle it. */
441 op = op_to_movi(op);
442 gen_opc_buf[op_index] = op;
443 args[1] = temps[args[1]].val;
444 /* fallthrough */
445 CASE_OP_32_64(movi):
446 tcg_opt_gen_movi(gen_args, args[0], args[1], nb_temps, nb_globals);
447 gen_args += 2;
448 args += 2;
449 break;
450 CASE_OP_32_64(not):
451 CASE_OP_32_64(neg):
452 CASE_OP_32_64(ext8s):
453 CASE_OP_32_64(ext8u):
454 CASE_OP_32_64(ext16s):
455 CASE_OP_32_64(ext16u):
456 case INDEX_op_ext32s_i64:
457 case INDEX_op_ext32u_i64:
458 if (temps[args[1]].state == TCG_TEMP_CONST) {
459 gen_opc_buf[op_index] = op_to_movi(op);
460 tmp = do_constant_folding(op, temps[args[1]].val, 0);
461 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
462 gen_args += 2;
463 args += 2;
464 break;
465 } else {
466 reset_temp(args[0], nb_temps, nb_globals);
467 gen_args[0] = args[0];
468 gen_args[1] = args[1];
469 gen_args += 2;
470 args += 2;
471 break;
472 }
473 CASE_OP_32_64(add):
474 CASE_OP_32_64(sub):
475 CASE_OP_32_64(mul):
476 CASE_OP_32_64(or):
477 CASE_OP_32_64(and):
478 CASE_OP_32_64(xor):
479 CASE_OP_32_64(shl):
480 CASE_OP_32_64(shr):
481 CASE_OP_32_64(sar):
482 CASE_OP_32_64(rotl):
483 CASE_OP_32_64(rotr):
484 CASE_OP_32_64(andc):
485 CASE_OP_32_64(orc):
486 CASE_OP_32_64(eqv):
487 CASE_OP_32_64(nand):
488 CASE_OP_32_64(nor):
489 if (temps[args[1]].state == TCG_TEMP_CONST
490 && temps[args[2]].state == TCG_TEMP_CONST) {
491 gen_opc_buf[op_index] = op_to_movi(op);
492 tmp = do_constant_folding(op, temps[args[1]].val,
493 temps[args[2]].val);
494 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
495 gen_args += 2;
496 args += 3;
497 break;
498 } else {
499 reset_temp(args[0], nb_temps, nb_globals);
500 gen_args[0] = args[0];
501 gen_args[1] = args[1];
502 gen_args[2] = args[2];
503 gen_args += 3;
504 args += 3;
505 break;
506 }
507 case INDEX_op_call:
508 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
509 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
510 for (i = 0; i < nb_globals; i++) {
511 reset_temp(i, nb_temps, nb_globals);
512 }
513 }
514 for (i = 0; i < (args[0] >> 16); i++) {
515 reset_temp(args[i + 1], nb_temps, nb_globals);
516 }
517 i = nb_call_args + 3;
518 while (i) {
519 *gen_args = *args;
520 args++;
521 gen_args++;
522 i--;
523 }
524 break;
525 case INDEX_op_set_label:
526 case INDEX_op_jmp:
527 case INDEX_op_br:
528 CASE_OP_32_64(brcond):
529 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
530 for (i = 0; i < def->nb_args; i++) {
531 *gen_args = *args;
532 args++;
533 gen_args++;
534 }
535 break;
536 default:
537 /* Default case: we do know nothing about operation so no
538 propagation is done. We only trash output args. */
539 for (i = 0; i < def->nb_oargs; i++) {
540 reset_temp(args[i], nb_temps, nb_globals);
541 }
542 for (i = 0; i < def->nb_args; i++) {
543 gen_args[i] = args[i];
544 }
545 args += def->nb_args;
546 gen_args += def->nb_args;
547 break;
548 }
549 }
550
551 return gen_args;
552 }
553
554 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
555 TCGArg *args, TCGOpDef *tcg_op_defs)
556 {
557 TCGArg *res;
558 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
559 return res;
560 }