]> Git Repo - qemu.git/blob - tcg/optimize.c
tcg/optimize: Simply some logical ops to NOT
[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 <[email protected]>
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_state;
43
44 struct tcg_temp_info {
45     tcg_temp_state state;
46     uint16_t prev_copy;
47     uint16_t next_copy;
48     tcg_target_ulong val;
49     tcg_target_ulong mask;
50 };
51
52 static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
54 /* Reset TEMP's state to TCG_TEMP_UNDEF.  If TEMP only had one copy, remove
55    the copy flag from the left temp.  */
56 static void reset_temp(TCGArg temp)
57 {
58     if (temps[temp].state == TCG_TEMP_COPY) {
59         if (temps[temp].prev_copy == temps[temp].next_copy) {
60             temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
61         } else {
62             temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
63             temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
64         }
65     }
66     temps[temp].state = TCG_TEMP_UNDEF;
67     temps[temp].mask = -1;
68 }
69
70 /* Reset all temporaries, given that there are NB_TEMPS of them.  */
71 static void reset_all_temps(int nb_temps)
72 {
73     int i;
74     for (i = 0; i < nb_temps; i++) {
75         temps[i].state = TCG_TEMP_UNDEF;
76         temps[i].mask = -1;
77     }
78 }
79
80 static int op_bits(TCGOpcode op)
81 {
82     const TCGOpDef *def = &tcg_op_defs[op];
83     return def->flags & TCG_OPF_64BIT ? 64 : 32;
84 }
85
86 static TCGOpcode op_to_movi(TCGOpcode op)
87 {
88     switch (op_bits(op)) {
89     case 32:
90         return INDEX_op_movi_i32;
91     case 64:
92         return INDEX_op_movi_i64;
93     default:
94         fprintf(stderr, "op_to_movi: unexpected return value of "
95                 "function op_bits.\n");
96         tcg_abort();
97     }
98 }
99
100 static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
101 {
102     TCGArg i;
103
104     /* If this is already a global, we can't do better. */
105     if (temp < s->nb_globals) {
106         return temp;
107     }
108
109     /* Search for a global first. */
110     for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
111         if (i < s->nb_globals) {
112             return i;
113         }
114     }
115
116     /* If it is a temp, search for a temp local. */
117     if (!s->temps[temp].temp_local) {
118         for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
119             if (s->temps[i].temp_local) {
120                 return i;
121             }
122         }
123     }
124
125     /* Failure to find a better representation, return the same temp. */
126     return temp;
127 }
128
129 static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
130 {
131     TCGArg i;
132
133     if (arg1 == arg2) {
134         return true;
135     }
136
137     if (temps[arg1].state != TCG_TEMP_COPY
138         || temps[arg2].state != TCG_TEMP_COPY) {
139         return false;
140     }
141
142     for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
143         if (i == arg2) {
144             return true;
145         }
146     }
147
148     return false;
149 }
150
151 static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
152                             TCGArg dst, TCGArg src)
153 {
154     reset_temp(dst);
155     temps[dst].mask = temps[src].mask;
156     assert(temps[src].state != TCG_TEMP_CONST);
157
158     if (s->temps[src].type == s->temps[dst].type) {
159         if (temps[src].state != TCG_TEMP_COPY) {
160             temps[src].state = TCG_TEMP_COPY;
161             temps[src].next_copy = src;
162             temps[src].prev_copy = src;
163         }
164         temps[dst].state = TCG_TEMP_COPY;
165         temps[dst].next_copy = temps[src].next_copy;
166         temps[dst].prev_copy = src;
167         temps[temps[dst].next_copy].prev_copy = dst;
168         temps[src].next_copy = dst;
169     }
170
171     gen_args[0] = dst;
172     gen_args[1] = src;
173 }
174
175 static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
176 {
177     reset_temp(dst);
178     temps[dst].state = TCG_TEMP_CONST;
179     temps[dst].val = val;
180     temps[dst].mask = val;
181     gen_args[0] = dst;
182     gen_args[1] = val;
183 }
184
185 static TCGOpcode op_to_mov(TCGOpcode op)
186 {
187     switch (op_bits(op)) {
188     case 32:
189         return INDEX_op_mov_i32;
190     case 64:
191         return INDEX_op_mov_i64;
192     default:
193         fprintf(stderr, "op_to_mov: unexpected return value of "
194                 "function op_bits.\n");
195         tcg_abort();
196     }
197 }
198
199 static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
200 {
201     uint64_t l64, h64;
202
203     switch (op) {
204     CASE_OP_32_64(add):
205         return x + y;
206
207     CASE_OP_32_64(sub):
208         return x - y;
209
210     CASE_OP_32_64(mul):
211         return x * y;
212
213     CASE_OP_32_64(and):
214         return x & y;
215
216     CASE_OP_32_64(or):
217         return x | y;
218
219     CASE_OP_32_64(xor):
220         return x ^ y;
221
222     case INDEX_op_shl_i32:
223         return (uint32_t)x << (uint32_t)y;
224
225     case INDEX_op_shl_i64:
226         return (uint64_t)x << (uint64_t)y;
227
228     case INDEX_op_shr_i32:
229         return (uint32_t)x >> (uint32_t)y;
230
231     case INDEX_op_shr_i64:
232         return (uint64_t)x >> (uint64_t)y;
233
234     case INDEX_op_sar_i32:
235         return (int32_t)x >> (int32_t)y;
236
237     case INDEX_op_sar_i64:
238         return (int64_t)x >> (int64_t)y;
239
240     case INDEX_op_rotr_i32:
241         return ror32(x, y);
242
243     case INDEX_op_rotr_i64:
244         return ror64(x, y);
245
246     case INDEX_op_rotl_i32:
247         return rol32(x, y);
248
249     case INDEX_op_rotl_i64:
250         return rol64(x, y);
251
252     CASE_OP_32_64(not):
253         return ~x;
254
255     CASE_OP_32_64(neg):
256         return -x;
257
258     CASE_OP_32_64(andc):
259         return x & ~y;
260
261     CASE_OP_32_64(orc):
262         return x | ~y;
263
264     CASE_OP_32_64(eqv):
265         return ~(x ^ y);
266
267     CASE_OP_32_64(nand):
268         return ~(x & y);
269
270     CASE_OP_32_64(nor):
271         return ~(x | y);
272
273     CASE_OP_32_64(ext8s):
274         return (int8_t)x;
275
276     CASE_OP_32_64(ext16s):
277         return (int16_t)x;
278
279     CASE_OP_32_64(ext8u):
280         return (uint8_t)x;
281
282     CASE_OP_32_64(ext16u):
283         return (uint16_t)x;
284
285     case INDEX_op_ext32s_i64:
286         return (int32_t)x;
287
288     case INDEX_op_ext32u_i64:
289         return (uint32_t)x;
290
291     case INDEX_op_muluh_i32:
292         return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
293     case INDEX_op_mulsh_i32:
294         return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
295
296     case INDEX_op_muluh_i64:
297         mulu64(&l64, &h64, x, y);
298         return h64;
299     case INDEX_op_mulsh_i64:
300         muls64(&l64, &h64, x, y);
301         return h64;
302
303     case INDEX_op_div_i32:
304         /* Avoid crashing on divide by zero, otherwise undefined.  */
305         return (int32_t)x / ((int32_t)y ? : 1);
306     case INDEX_op_divu_i32:
307         return (uint32_t)x / ((uint32_t)y ? : 1);
308     case INDEX_op_div_i64:
309         return (int64_t)x / ((int64_t)y ? : 1);
310     case INDEX_op_divu_i64:
311         return (uint64_t)x / ((uint64_t)y ? : 1);
312
313     case INDEX_op_rem_i32:
314         return (int32_t)x % ((int32_t)y ? : 1);
315     case INDEX_op_remu_i32:
316         return (uint32_t)x % ((uint32_t)y ? : 1);
317     case INDEX_op_rem_i64:
318         return (int64_t)x % ((int64_t)y ? : 1);
319     case INDEX_op_remu_i64:
320         return (uint64_t)x % ((uint64_t)y ? : 1);
321
322     default:
323         fprintf(stderr,
324                 "Unrecognized operation %d in do_constant_folding.\n", op);
325         tcg_abort();
326     }
327 }
328
329 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
330 {
331     TCGArg res = do_constant_folding_2(op, x, y);
332     if (op_bits(op) == 32) {
333         res &= 0xffffffff;
334     }
335     return res;
336 }
337
338 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
339 {
340     switch (c) {
341     case TCG_COND_EQ:
342         return x == y;
343     case TCG_COND_NE:
344         return x != y;
345     case TCG_COND_LT:
346         return (int32_t)x < (int32_t)y;
347     case TCG_COND_GE:
348         return (int32_t)x >= (int32_t)y;
349     case TCG_COND_LE:
350         return (int32_t)x <= (int32_t)y;
351     case TCG_COND_GT:
352         return (int32_t)x > (int32_t)y;
353     case TCG_COND_LTU:
354         return x < y;
355     case TCG_COND_GEU:
356         return x >= y;
357     case TCG_COND_LEU:
358         return x <= y;
359     case TCG_COND_GTU:
360         return x > y;
361     default:
362         tcg_abort();
363     }
364 }
365
366 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
367 {
368     switch (c) {
369     case TCG_COND_EQ:
370         return x == y;
371     case TCG_COND_NE:
372         return x != y;
373     case TCG_COND_LT:
374         return (int64_t)x < (int64_t)y;
375     case TCG_COND_GE:
376         return (int64_t)x >= (int64_t)y;
377     case TCG_COND_LE:
378         return (int64_t)x <= (int64_t)y;
379     case TCG_COND_GT:
380         return (int64_t)x > (int64_t)y;
381     case TCG_COND_LTU:
382         return x < y;
383     case TCG_COND_GEU:
384         return x >= y;
385     case TCG_COND_LEU:
386         return x <= y;
387     case TCG_COND_GTU:
388         return x > y;
389     default:
390         tcg_abort();
391     }
392 }
393
394 static bool do_constant_folding_cond_eq(TCGCond c)
395 {
396     switch (c) {
397     case TCG_COND_GT:
398     case TCG_COND_LTU:
399     case TCG_COND_LT:
400     case TCG_COND_GTU:
401     case TCG_COND_NE:
402         return 0;
403     case TCG_COND_GE:
404     case TCG_COND_GEU:
405     case TCG_COND_LE:
406     case TCG_COND_LEU:
407     case TCG_COND_EQ:
408         return 1;
409     default:
410         tcg_abort();
411     }
412 }
413
414 /* Return 2 if the condition can't be simplified, and the result
415    of the condition (0 or 1) if it can */
416 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
417                                        TCGArg y, TCGCond c)
418 {
419     if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
420         switch (op_bits(op)) {
421         case 32:
422             return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
423         case 64:
424             return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
425         default:
426             tcg_abort();
427         }
428     } else if (temps_are_copies(x, y)) {
429         return do_constant_folding_cond_eq(c);
430     } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
431         switch (c) {
432         case TCG_COND_LTU:
433             return 0;
434         case TCG_COND_GEU:
435             return 1;
436         default:
437             return 2;
438         }
439     } else {
440         return 2;
441     }
442 }
443
444 /* Return 2 if the condition can't be simplified, and the result
445    of the condition (0 or 1) if it can */
446 static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
447 {
448     TCGArg al = p1[0], ah = p1[1];
449     TCGArg bl = p2[0], bh = p2[1];
450
451     if (temps[bl].state == TCG_TEMP_CONST
452         && temps[bh].state == TCG_TEMP_CONST) {
453         uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
454
455         if (temps[al].state == TCG_TEMP_CONST
456             && temps[ah].state == TCG_TEMP_CONST) {
457             uint64_t a;
458             a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
459             return do_constant_folding_cond_64(a, b, c);
460         }
461         if (b == 0) {
462             switch (c) {
463             case TCG_COND_LTU:
464                 return 0;
465             case TCG_COND_GEU:
466                 return 1;
467             default:
468                 break;
469             }
470         }
471     }
472     if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
473         return do_constant_folding_cond_eq(c);
474     }
475     return 2;
476 }
477
478 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
479 {
480     TCGArg a1 = *p1, a2 = *p2;
481     int sum = 0;
482     sum += temps[a1].state == TCG_TEMP_CONST;
483     sum -= temps[a2].state == TCG_TEMP_CONST;
484
485     /* Prefer the constant in second argument, and then the form
486        op a, a, b, which is better handled on non-RISC hosts. */
487     if (sum > 0 || (sum == 0 && dest == a2)) {
488         *p1 = a2;
489         *p2 = a1;
490         return true;
491     }
492     return false;
493 }
494
495 static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
496 {
497     int sum = 0;
498     sum += temps[p1[0]].state == TCG_TEMP_CONST;
499     sum += temps[p1[1]].state == TCG_TEMP_CONST;
500     sum -= temps[p2[0]].state == TCG_TEMP_CONST;
501     sum -= temps[p2[1]].state == TCG_TEMP_CONST;
502     if (sum > 0) {
503         TCGArg t;
504         t = p1[0], p1[0] = p2[0], p2[0] = t;
505         t = p1[1], p1[1] = p2[1], p2[1] = t;
506         return true;
507     }
508     return false;
509 }
510
511 /* Propagate constants and copies, fold constant expressions. */
512 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
513                                     TCGArg *args, TCGOpDef *tcg_op_defs)
514 {
515     int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
516     tcg_target_ulong mask, affected;
517     TCGOpcode op;
518     const TCGOpDef *def;
519     TCGArg *gen_args;
520     TCGArg tmp;
521
522     /* Array VALS has an element for each temp.
523        If this temp holds a constant then its value is kept in VALS' element.
524        If this temp is a copy of other ones then the other copies are
525        available through the doubly linked circular list. */
526
527     nb_temps = s->nb_temps;
528     nb_globals = s->nb_globals;
529     reset_all_temps(nb_temps);
530
531     nb_ops = tcg_opc_ptr - s->gen_opc_buf;
532     gen_args = args;
533     for (op_index = 0; op_index < nb_ops; op_index++) {
534         op = s->gen_opc_buf[op_index];
535         def = &tcg_op_defs[op];
536         /* Do copy propagation */
537         if (op == INDEX_op_call) {
538             int nb_oargs = args[0] >> 16;
539             int nb_iargs = args[0] & 0xffff;
540             for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
541                 if (temps[args[i]].state == TCG_TEMP_COPY) {
542                     args[i] = find_better_copy(s, args[i]);
543                 }
544             }
545         } else {
546             for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
547                 if (temps[args[i]].state == TCG_TEMP_COPY) {
548                     args[i] = find_better_copy(s, args[i]);
549                 }
550             }
551         }
552
553         /* For commutative operations make constant second argument */
554         switch (op) {
555         CASE_OP_32_64(add):
556         CASE_OP_32_64(mul):
557         CASE_OP_32_64(and):
558         CASE_OP_32_64(or):
559         CASE_OP_32_64(xor):
560         CASE_OP_32_64(eqv):
561         CASE_OP_32_64(nand):
562         CASE_OP_32_64(nor):
563         CASE_OP_32_64(muluh):
564         CASE_OP_32_64(mulsh):
565             swap_commutative(args[0], &args[1], &args[2]);
566             break;
567         CASE_OP_32_64(brcond):
568             if (swap_commutative(-1, &args[0], &args[1])) {
569                 args[2] = tcg_swap_cond(args[2]);
570             }
571             break;
572         CASE_OP_32_64(setcond):
573             if (swap_commutative(args[0], &args[1], &args[2])) {
574                 args[3] = tcg_swap_cond(args[3]);
575             }
576             break;
577         CASE_OP_32_64(movcond):
578             if (swap_commutative(-1, &args[1], &args[2])) {
579                 args[5] = tcg_swap_cond(args[5]);
580             }
581             /* For movcond, we canonicalize the "false" input reg to match
582                the destination reg so that the tcg backend can implement
583                a "move if true" operation.  */
584             if (swap_commutative(args[0], &args[4], &args[3])) {
585                 args[5] = tcg_invert_cond(args[5]);
586             }
587             break;
588         CASE_OP_32_64(add2):
589             swap_commutative(args[0], &args[2], &args[4]);
590             swap_commutative(args[1], &args[3], &args[5]);
591             break;
592         CASE_OP_32_64(mulu2):
593         CASE_OP_32_64(muls2):
594             swap_commutative(args[0], &args[2], &args[3]);
595             break;
596         case INDEX_op_brcond2_i32:
597             if (swap_commutative2(&args[0], &args[2])) {
598                 args[4] = tcg_swap_cond(args[4]);
599             }
600             break;
601         case INDEX_op_setcond2_i32:
602             if (swap_commutative2(&args[1], &args[3])) {
603                 args[5] = tcg_swap_cond(args[5]);
604             }
605             break;
606         default:
607             break;
608         }
609
610         /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
611            and "sub r, 0, a => neg r, a" case.  */
612         switch (op) {
613         CASE_OP_32_64(shl):
614         CASE_OP_32_64(shr):
615         CASE_OP_32_64(sar):
616         CASE_OP_32_64(rotl):
617         CASE_OP_32_64(rotr):
618             if (temps[args[1]].state == TCG_TEMP_CONST
619                 && temps[args[1]].val == 0) {
620                 s->gen_opc_buf[op_index] = op_to_movi(op);
621                 tcg_opt_gen_movi(gen_args, args[0], 0);
622                 args += 3;
623                 gen_args += 2;
624                 continue;
625             }
626             break;
627         CASE_OP_32_64(sub):
628             {
629                 TCGOpcode neg_op;
630                 bool have_neg;
631
632                 if (temps[args[2]].state == TCG_TEMP_CONST) {
633                     /* Proceed with possible constant folding. */
634                     break;
635                 }
636                 if (op == INDEX_op_sub_i32) {
637                     neg_op = INDEX_op_neg_i32;
638                     have_neg = TCG_TARGET_HAS_neg_i32;
639                 } else {
640                     neg_op = INDEX_op_neg_i64;
641                     have_neg = TCG_TARGET_HAS_neg_i64;
642                 }
643                 if (!have_neg) {
644                     break;
645                 }
646                 if (temps[args[1]].state == TCG_TEMP_CONST
647                     && temps[args[1]].val == 0) {
648                     s->gen_opc_buf[op_index] = neg_op;
649                     reset_temp(args[0]);
650                     gen_args[0] = args[0];
651                     gen_args[1] = args[2];
652                     args += 3;
653                     gen_args += 2;
654                     continue;
655                 }
656             }
657             break;
658         CASE_OP_32_64(xor):
659         CASE_OP_32_64(nand):
660             if (temps[args[1]].state != TCG_TEMP_CONST
661                 && temps[args[2]].state == TCG_TEMP_CONST
662                 && temps[args[2]].val == -1) {
663                 i = 1;
664                 goto try_not;
665             }
666             break;
667         CASE_OP_32_64(nor):
668             if (temps[args[1]].state != TCG_TEMP_CONST
669                 && temps[args[2]].state == TCG_TEMP_CONST
670                 && temps[args[2]].val == 0) {
671                 i = 1;
672                 goto try_not;
673             }
674             break;
675         CASE_OP_32_64(andc):
676             if (temps[args[2]].state != TCG_TEMP_CONST
677                 && temps[args[1]].state == TCG_TEMP_CONST
678                 && temps[args[1]].val == -1) {
679                 i = 2;
680                 goto try_not;
681             }
682             break;
683         CASE_OP_32_64(orc):
684         CASE_OP_32_64(eqv):
685             if (temps[args[2]].state != TCG_TEMP_CONST
686                 && temps[args[1]].state == TCG_TEMP_CONST
687                 && temps[args[1]].val == 0) {
688                 i = 2;
689                 goto try_not;
690             }
691             break;
692         try_not:
693             {
694                 TCGOpcode not_op;
695                 bool have_not;
696
697                 if (def->flags & TCG_OPF_64BIT) {
698                     not_op = INDEX_op_not_i64;
699                     have_not = TCG_TARGET_HAS_not_i64;
700                 } else {
701                     not_op = INDEX_op_not_i32;
702                     have_not = TCG_TARGET_HAS_not_i32;
703                 }
704                 if (!have_not) {
705                     break;
706                 }
707                 s->gen_opc_buf[op_index] = not_op;
708                 reset_temp(args[0]);
709                 gen_args[0] = args[0];
710                 gen_args[1] = args[i];
711                 args += 3;
712                 gen_args += 2;
713                 continue;
714             }
715         default:
716             break;
717         }
718
719         /* Simplify expression for "op r, a, 0 => mov r, a" cases */
720         switch (op) {
721         CASE_OP_32_64(add):
722         CASE_OP_32_64(sub):
723         CASE_OP_32_64(shl):
724         CASE_OP_32_64(shr):
725         CASE_OP_32_64(sar):
726         CASE_OP_32_64(rotl):
727         CASE_OP_32_64(rotr):
728         CASE_OP_32_64(or):
729         CASE_OP_32_64(xor):
730             if (temps[args[1]].state == TCG_TEMP_CONST) {
731                 /* Proceed with possible constant folding. */
732                 break;
733             }
734             if (temps[args[2]].state == TCG_TEMP_CONST
735                 && temps[args[2]].val == 0) {
736                 if (temps_are_copies(args[0], args[1])) {
737                     s->gen_opc_buf[op_index] = INDEX_op_nop;
738                 } else {
739                     s->gen_opc_buf[op_index] = op_to_mov(op);
740                     tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
741                     gen_args += 2;
742                 }
743                 args += 3;
744                 continue;
745             }
746             break;
747         default:
748             break;
749         }
750
751         /* Simplify using known-zero bits. Currently only ops with a single
752            output argument is supported. */
753         mask = -1;
754         affected = -1;
755         switch (op) {
756         CASE_OP_32_64(ext8s):
757             if ((temps[args[1]].mask & 0x80) != 0) {
758                 break;
759             }
760         CASE_OP_32_64(ext8u):
761             mask = 0xff;
762             goto and_const;
763         CASE_OP_32_64(ext16s):
764             if ((temps[args[1]].mask & 0x8000) != 0) {
765                 break;
766             }
767         CASE_OP_32_64(ext16u):
768             mask = 0xffff;
769             goto and_const;
770         case INDEX_op_ext32s_i64:
771             if ((temps[args[1]].mask & 0x80000000) != 0) {
772                 break;
773             }
774         case INDEX_op_ext32u_i64:
775             mask = 0xffffffffU;
776             goto and_const;
777
778         CASE_OP_32_64(and):
779             mask = temps[args[2]].mask;
780             if (temps[args[2]].state == TCG_TEMP_CONST) {
781         and_const:
782                 affected = temps[args[1]].mask & ~mask;
783             }
784             mask = temps[args[1]].mask & mask;
785             break;
786
787         CASE_OP_32_64(andc):
788             /* Known-zeros does not imply known-ones.  Therefore unless
789                args[2] is constant, we can't infer anything from it.  */
790             if (temps[args[2]].state == TCG_TEMP_CONST) {
791                 mask = ~temps[args[2]].mask;
792                 goto and_const;
793             }
794             /* But we certainly know nothing outside args[1] may be set. */
795             mask = temps[args[1]].mask;
796             break;
797
798         case INDEX_op_sar_i32:
799             if (temps[args[2]].state == TCG_TEMP_CONST) {
800                 mask = (int32_t)temps[args[1]].mask >> temps[args[2]].val;
801             }
802             break;
803         case INDEX_op_sar_i64:
804             if (temps[args[2]].state == TCG_TEMP_CONST) {
805                 mask = (int64_t)temps[args[1]].mask >> temps[args[2]].val;
806             }
807             break;
808
809         case INDEX_op_shr_i32:
810             if (temps[args[2]].state == TCG_TEMP_CONST) {
811                 mask = (uint32_t)temps[args[1]].mask >> temps[args[2]].val;
812             }
813             break;
814         case INDEX_op_shr_i64:
815             if (temps[args[2]].state == TCG_TEMP_CONST) {
816                 mask = (uint64_t)temps[args[1]].mask >> temps[args[2]].val;
817             }
818             break;
819
820         CASE_OP_32_64(shl):
821             if (temps[args[2]].state == TCG_TEMP_CONST) {
822                 mask = temps[args[1]].mask << temps[args[2]].val;
823             }
824             break;
825
826         CASE_OP_32_64(neg):
827             /* Set to 1 all bits to the left of the rightmost.  */
828             mask = -(temps[args[1]].mask & -temps[args[1]].mask);
829             break;
830
831         CASE_OP_32_64(deposit):
832             tmp = ((1ull << args[4]) - 1);
833             mask = ((temps[args[1]].mask & ~(tmp << args[3]))
834                     | ((temps[args[2]].mask & tmp) << args[3]));
835             break;
836
837         CASE_OP_32_64(or):
838         CASE_OP_32_64(xor):
839             mask = temps[args[1]].mask | temps[args[2]].mask;
840             break;
841
842         CASE_OP_32_64(setcond):
843             mask = 1;
844             break;
845
846         CASE_OP_32_64(movcond):
847             mask = temps[args[3]].mask | temps[args[4]].mask;
848             break;
849
850         CASE_OP_32_64(ld8u):
851         case INDEX_op_qemu_ld8u:
852             mask = 0xff;
853             break;
854         CASE_OP_32_64(ld16u):
855         case INDEX_op_qemu_ld16u:
856             mask = 0xffff;
857             break;
858         case INDEX_op_ld32u_i64:
859 #if TCG_TARGET_REG_BITS == 64
860         case INDEX_op_qemu_ld32u:
861 #endif
862             mask = 0xffffffffu;
863             break;
864
865         CASE_OP_32_64(qemu_ld):
866             {
867                 TCGMemOp mop = args[def->nb_oargs + def->nb_iargs];
868                 if (!(mop & MO_SIGN)) {
869                     mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
870                 }
871             }
872             break;
873
874         default:
875             break;
876         }
877
878         /* 32-bit ops (non 64-bit ops and non load/store ops) generate 32-bit
879            results */
880         if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_64BIT))) {
881             mask &= 0xffffffffu;
882         }
883
884         if (mask == 0) {
885             assert(def->nb_oargs == 1);
886             s->gen_opc_buf[op_index] = op_to_movi(op);
887             tcg_opt_gen_movi(gen_args, args[0], 0);
888             args += def->nb_oargs + def->nb_iargs + def->nb_cargs;
889             gen_args += 2;
890             continue;
891         }
892         if (affected == 0) {
893             assert(def->nb_oargs == 1);
894             if (temps_are_copies(args[0], args[1])) {
895                 s->gen_opc_buf[op_index] = INDEX_op_nop;
896             } else if (temps[args[1]].state != TCG_TEMP_CONST) {
897                 s->gen_opc_buf[op_index] = op_to_mov(op);
898                 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
899                 gen_args += 2;
900             } else {
901                 s->gen_opc_buf[op_index] = op_to_movi(op);
902                 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
903                 gen_args += 2;
904             }
905             args += def->nb_iargs + 1;
906             continue;
907         }
908
909         /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
910         switch (op) {
911         CASE_OP_32_64(and):
912         CASE_OP_32_64(mul):
913         CASE_OP_32_64(muluh):
914         CASE_OP_32_64(mulsh):
915             if ((temps[args[2]].state == TCG_TEMP_CONST
916                 && temps[args[2]].val == 0)) {
917                 s->gen_opc_buf[op_index] = op_to_movi(op);
918                 tcg_opt_gen_movi(gen_args, args[0], 0);
919                 args += 3;
920                 gen_args += 2;
921                 continue;
922             }
923             break;
924         default:
925             break;
926         }
927
928         /* Simplify expression for "op r, a, a => mov r, a" cases */
929         switch (op) {
930         CASE_OP_32_64(or):
931         CASE_OP_32_64(and):
932             if (temps_are_copies(args[1], args[2])) {
933                 if (temps_are_copies(args[0], args[1])) {
934                     s->gen_opc_buf[op_index] = INDEX_op_nop;
935                 } else {
936                     s->gen_opc_buf[op_index] = op_to_mov(op);
937                     tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
938                     gen_args += 2;
939                 }
940                 args += 3;
941                 continue;
942             }
943             break;
944         default:
945             break;
946         }
947
948         /* Simplify expression for "op r, a, a => movi r, 0" cases */
949         switch (op) {
950         CASE_OP_32_64(sub):
951         CASE_OP_32_64(xor):
952             if (temps_are_copies(args[1], args[2])) {
953                 s->gen_opc_buf[op_index] = op_to_movi(op);
954                 tcg_opt_gen_movi(gen_args, args[0], 0);
955                 gen_args += 2;
956                 args += 3;
957                 continue;
958             }
959             break;
960         default:
961             break;
962         }
963
964         /* Propagate constants through copy operations and do constant
965            folding.  Constants will be substituted to arguments by register
966            allocator where needed and possible.  Also detect copies. */
967         switch (op) {
968         CASE_OP_32_64(mov):
969             if (temps_are_copies(args[0], args[1])) {
970                 args += 2;
971                 s->gen_opc_buf[op_index] = INDEX_op_nop;
972                 break;
973             }
974             if (temps[args[1]].state != TCG_TEMP_CONST) {
975                 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
976                 gen_args += 2;
977                 args += 2;
978                 break;
979             }
980             /* Source argument is constant.  Rewrite the operation and
981                let movi case handle it. */
982             op = op_to_movi(op);
983             s->gen_opc_buf[op_index] = op;
984             args[1] = temps[args[1]].val;
985             /* fallthrough */
986         CASE_OP_32_64(movi):
987             tcg_opt_gen_movi(gen_args, args[0], args[1]);
988             gen_args += 2;
989             args += 2;
990             break;
991
992         CASE_OP_32_64(not):
993         CASE_OP_32_64(neg):
994         CASE_OP_32_64(ext8s):
995         CASE_OP_32_64(ext8u):
996         CASE_OP_32_64(ext16s):
997         CASE_OP_32_64(ext16u):
998         case INDEX_op_ext32s_i64:
999         case INDEX_op_ext32u_i64:
1000             if (temps[args[1]].state == TCG_TEMP_CONST) {
1001                 s->gen_opc_buf[op_index] = op_to_movi(op);
1002                 tmp = do_constant_folding(op, temps[args[1]].val, 0);
1003                 tcg_opt_gen_movi(gen_args, args[0], tmp);
1004                 gen_args += 2;
1005                 args += 2;
1006                 break;
1007             }
1008             goto do_default;
1009
1010         CASE_OP_32_64(add):
1011         CASE_OP_32_64(sub):
1012         CASE_OP_32_64(mul):
1013         CASE_OP_32_64(or):
1014         CASE_OP_32_64(and):
1015         CASE_OP_32_64(xor):
1016         CASE_OP_32_64(shl):
1017         CASE_OP_32_64(shr):
1018         CASE_OP_32_64(sar):
1019         CASE_OP_32_64(rotl):
1020         CASE_OP_32_64(rotr):
1021         CASE_OP_32_64(andc):
1022         CASE_OP_32_64(orc):
1023         CASE_OP_32_64(eqv):
1024         CASE_OP_32_64(nand):
1025         CASE_OP_32_64(nor):
1026         CASE_OP_32_64(muluh):
1027         CASE_OP_32_64(mulsh):
1028         CASE_OP_32_64(div):
1029         CASE_OP_32_64(divu):
1030         CASE_OP_32_64(rem):
1031         CASE_OP_32_64(remu):
1032             if (temps[args[1]].state == TCG_TEMP_CONST
1033                 && temps[args[2]].state == TCG_TEMP_CONST) {
1034                 s->gen_opc_buf[op_index] = op_to_movi(op);
1035                 tmp = do_constant_folding(op, temps[args[1]].val,
1036                                           temps[args[2]].val);
1037                 tcg_opt_gen_movi(gen_args, args[0], tmp);
1038                 gen_args += 2;
1039                 args += 3;
1040                 break;
1041             }
1042             goto do_default;
1043
1044         CASE_OP_32_64(deposit):
1045             if (temps[args[1]].state == TCG_TEMP_CONST
1046                 && temps[args[2]].state == TCG_TEMP_CONST) {
1047                 s->gen_opc_buf[op_index] = op_to_movi(op);
1048                 tmp = ((1ull << args[4]) - 1);
1049                 tmp = (temps[args[1]].val & ~(tmp << args[3]))
1050                       | ((temps[args[2]].val & tmp) << args[3]);
1051                 tcg_opt_gen_movi(gen_args, args[0], tmp);
1052                 gen_args += 2;
1053                 args += 5;
1054                 break;
1055             }
1056             goto do_default;
1057
1058         CASE_OP_32_64(setcond):
1059             tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
1060             if (tmp != 2) {
1061                 s->gen_opc_buf[op_index] = op_to_movi(op);
1062                 tcg_opt_gen_movi(gen_args, args[0], tmp);
1063                 gen_args += 2;
1064                 args += 4;
1065                 break;
1066             }
1067             goto do_default;
1068
1069         CASE_OP_32_64(brcond):
1070             tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
1071             if (tmp != 2) {
1072                 if (tmp) {
1073                     reset_all_temps(nb_temps);
1074                     s->gen_opc_buf[op_index] = INDEX_op_br;
1075                     gen_args[0] = args[3];
1076                     gen_args += 1;
1077                 } else {
1078                     s->gen_opc_buf[op_index] = INDEX_op_nop;
1079                 }
1080                 args += 4;
1081                 break;
1082             }
1083             goto do_default;
1084
1085         CASE_OP_32_64(movcond):
1086             tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
1087             if (tmp != 2) {
1088                 if (temps_are_copies(args[0], args[4-tmp])) {
1089                     s->gen_opc_buf[op_index] = INDEX_op_nop;
1090                 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
1091                     s->gen_opc_buf[op_index] = op_to_movi(op);
1092                     tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
1093                     gen_args += 2;
1094                 } else {
1095                     s->gen_opc_buf[op_index] = op_to_mov(op);
1096                     tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
1097                     gen_args += 2;
1098                 }
1099                 args += 6;
1100                 break;
1101             }
1102             goto do_default;
1103
1104         case INDEX_op_add2_i32:
1105         case INDEX_op_sub2_i32:
1106             if (temps[args[2]].state == TCG_TEMP_CONST
1107                 && temps[args[3]].state == TCG_TEMP_CONST
1108                 && temps[args[4]].state == TCG_TEMP_CONST
1109                 && temps[args[5]].state == TCG_TEMP_CONST) {
1110                 uint32_t al = temps[args[2]].val;
1111                 uint32_t ah = temps[args[3]].val;
1112                 uint32_t bl = temps[args[4]].val;
1113                 uint32_t bh = temps[args[5]].val;
1114                 uint64_t a = ((uint64_t)ah << 32) | al;
1115                 uint64_t b = ((uint64_t)bh << 32) | bl;
1116                 TCGArg rl, rh;
1117
1118                 if (op == INDEX_op_add2_i32) {
1119                     a += b;
1120                 } else {
1121                     a -= b;
1122                 }
1123
1124                 /* We emit the extra nop when we emit the add2/sub2.  */
1125                 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
1126
1127                 rl = args[0];
1128                 rh = args[1];
1129                 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1130                 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
1131                 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
1132                 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
1133                 gen_args += 4;
1134                 args += 6;
1135                 break;
1136             }
1137             goto do_default;
1138
1139         case INDEX_op_mulu2_i32:
1140             if (temps[args[2]].state == TCG_TEMP_CONST
1141                 && temps[args[3]].state == TCG_TEMP_CONST) {
1142                 uint32_t a = temps[args[2]].val;
1143                 uint32_t b = temps[args[3]].val;
1144                 uint64_t r = (uint64_t)a * b;
1145                 TCGArg rl, rh;
1146
1147                 /* We emit the extra nop when we emit the mulu2.  */
1148                 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
1149
1150                 rl = args[0];
1151                 rh = args[1];
1152                 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1153                 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
1154                 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
1155                 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
1156                 gen_args += 4;
1157                 args += 4;
1158                 break;
1159             }
1160             goto do_default;
1161
1162         case INDEX_op_brcond2_i32:
1163             tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1164             if (tmp != 2) {
1165                 if (tmp) {
1166                     reset_all_temps(nb_temps);
1167                     s->gen_opc_buf[op_index] = INDEX_op_br;
1168                     gen_args[0] = args[5];
1169                     gen_args += 1;
1170                 } else {
1171                     s->gen_opc_buf[op_index] = INDEX_op_nop;
1172                 }
1173             } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1174                        && temps[args[2]].state == TCG_TEMP_CONST
1175                        && temps[args[3]].state == TCG_TEMP_CONST
1176                        && temps[args[2]].val == 0
1177                        && temps[args[3]].val == 0) {
1178                 /* Simplify LT/GE comparisons vs zero to a single compare
1179                    vs the high word of the input.  */
1180                 reset_all_temps(nb_temps);
1181                 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
1182                 gen_args[0] = args[1];
1183                 gen_args[1] = args[3];
1184                 gen_args[2] = args[4];
1185                 gen_args[3] = args[5];
1186                 gen_args += 4;
1187             } else {
1188                 goto do_default;
1189             }
1190             args += 6;
1191             break;
1192
1193         case INDEX_op_setcond2_i32:
1194             tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1195             if (tmp != 2) {
1196                 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1197                 tcg_opt_gen_movi(gen_args, args[0], tmp);
1198                 gen_args += 2;
1199             } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1200                        && temps[args[3]].state == TCG_TEMP_CONST
1201                        && temps[args[4]].state == TCG_TEMP_CONST
1202                        && temps[args[3]].val == 0
1203                        && temps[args[4]].val == 0) {
1204                 /* Simplify LT/GE comparisons vs zero to a single compare
1205                    vs the high word of the input.  */
1206                 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
1207                 reset_temp(args[0]);
1208                 gen_args[0] = args[0];
1209                 gen_args[1] = args[2];
1210                 gen_args[2] = args[4];
1211                 gen_args[3] = args[5];
1212                 gen_args += 4;
1213             } else {
1214                 goto do_default;
1215             }
1216             args += 6;
1217             break;
1218
1219         case INDEX_op_call:
1220             nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
1221             if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1222                                             TCG_CALL_NO_WRITE_GLOBALS))) {
1223                 for (i = 0; i < nb_globals; i++) {
1224                     reset_temp(i);
1225                 }
1226             }
1227             for (i = 0; i < (args[0] >> 16); i++) {
1228                 reset_temp(args[i + 1]);
1229             }
1230             i = nb_call_args + 3;
1231             while (i) {
1232                 *gen_args = *args;
1233                 args++;
1234                 gen_args++;
1235                 i--;
1236             }
1237             break;
1238
1239         default:
1240         do_default:
1241             /* Default case: we know nothing about operation (or were unable
1242                to compute the operation result) so no propagation is done.
1243                We trash everything if the operation is the end of a basic
1244                block, otherwise we only trash the output args.  "mask" is
1245                the non-zero bits mask for the first output arg.  */
1246             if (def->flags & TCG_OPF_BB_END) {
1247                 reset_all_temps(nb_temps);
1248             } else {
1249                 for (i = 0; i < def->nb_oargs; i++) {
1250                     reset_temp(args[i]);
1251                     /* Save the corresponding known-zero bits mask for the
1252                        first output argument (only one supported so far). */
1253                     if (i == 0) {
1254                         temps[args[i]].mask = mask;
1255                     }
1256                 }
1257             }
1258             for (i = 0; i < def->nb_args; i++) {
1259                 gen_args[i] = args[i];
1260             }
1261             args += def->nb_args;
1262             gen_args += def->nb_args;
1263             break;
1264         }
1265     }
1266
1267     return gen_args;
1268 }
1269
1270 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1271         TCGArg *args, TCGOpDef *tcg_op_defs)
1272 {
1273     TCGArg *res;
1274     res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1275     return res;
1276 }
This page took 0.091884 seconds and 4 git commands to generate.