xref: /openbmc/qemu/tcg/optimize.c (revision 9a6bc1840ec105902bda1a59c42e9e0c56a9ed05)
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 "qemu/osdep.h"
27 #include "qemu/int128.h"
28 #include "qemu/interval-tree.h"
29 #include "tcg/tcg-op-common.h"
30 #include "tcg-internal.h"
31 #include "tcg-has.h"
32 
33 #define CASE_OP_32_64(x)                        \
34         glue(glue(case INDEX_op_, x), _i32):    \
35         glue(glue(case INDEX_op_, x), _i64)
36 
37 #define CASE_OP_32_64_VEC(x)                    \
38         glue(glue(case INDEX_op_, x), _i32):    \
39         glue(glue(case INDEX_op_, x), _i64):    \
40         glue(glue(case INDEX_op_, x), _vec)
41 
42 typedef struct MemCopyInfo {
43     IntervalTreeNode itree;
44     QSIMPLEQ_ENTRY (MemCopyInfo) next;
45     TCGTemp *ts;
46     TCGType type;
47 } MemCopyInfo;
48 
49 typedef struct TempOptInfo {
50     bool is_const;
51     TCGTemp *prev_copy;
52     TCGTemp *next_copy;
53     QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy;
54     uint64_t val;
55     uint64_t z_mask;  /* mask bit is 0 if and only if value bit is 0 */
56     uint64_t s_mask;  /* mask bit is 1 if value bit matches msb */
57 } TempOptInfo;
58 
59 typedef struct OptContext {
60     TCGContext *tcg;
61     TCGOp *prev_mb;
62     TCGTempSet temps_used;
63 
64     IntervalTreeRoot mem_copy;
65     QSIMPLEQ_HEAD(, MemCopyInfo) mem_free;
66 
67     /* In flight values from optimization. */
68     TCGType type;
69 } OptContext;
70 
71 static inline TempOptInfo *ts_info(TCGTemp *ts)
72 {
73     return ts->state_ptr;
74 }
75 
76 static inline TempOptInfo *arg_info(TCGArg arg)
77 {
78     return ts_info(arg_temp(arg));
79 }
80 
81 static inline bool ti_is_const(TempOptInfo *ti)
82 {
83     return ti->is_const;
84 }
85 
86 static inline uint64_t ti_const_val(TempOptInfo *ti)
87 {
88     return ti->val;
89 }
90 
91 static inline bool ti_is_const_val(TempOptInfo *ti, uint64_t val)
92 {
93     return ti_is_const(ti) && ti_const_val(ti) == val;
94 }
95 
96 static inline bool ts_is_const(TCGTemp *ts)
97 {
98     return ti_is_const(ts_info(ts));
99 }
100 
101 static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val)
102 {
103     return ti_is_const_val(ts_info(ts), val);
104 }
105 
106 static inline bool arg_is_const(TCGArg arg)
107 {
108     return ts_is_const(arg_temp(arg));
109 }
110 
111 static inline bool arg_is_const_val(TCGArg arg, uint64_t val)
112 {
113     return ts_is_const_val(arg_temp(arg), val);
114 }
115 
116 static inline bool ts_is_copy(TCGTemp *ts)
117 {
118     return ts_info(ts)->next_copy != ts;
119 }
120 
121 static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b)
122 {
123     return a->kind < b->kind ? b : a;
124 }
125 
126 /* Initialize and activate a temporary.  */
127 static void init_ts_info(OptContext *ctx, TCGTemp *ts)
128 {
129     size_t idx = temp_idx(ts);
130     TempOptInfo *ti;
131 
132     if (test_bit(idx, ctx->temps_used.l)) {
133         return;
134     }
135     set_bit(idx, ctx->temps_used.l);
136 
137     ti = ts->state_ptr;
138     if (ti == NULL) {
139         ti = tcg_malloc(sizeof(TempOptInfo));
140         ts->state_ptr = ti;
141     }
142 
143     ti->next_copy = ts;
144     ti->prev_copy = ts;
145     QSIMPLEQ_INIT(&ti->mem_copy);
146     if (ts->kind == TEMP_CONST) {
147         ti->is_const = true;
148         ti->val = ts->val;
149         ti->z_mask = ts->val;
150         ti->s_mask = INT64_MIN >> clrsb64(ts->val);
151     } else {
152         ti->is_const = false;
153         ti->z_mask = -1;
154         ti->s_mask = 0;
155     }
156 }
157 
158 static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l)
159 {
160     IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l);
161     return r ? container_of(r, MemCopyInfo, itree) : NULL;
162 }
163 
164 static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l)
165 {
166     IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l);
167     return r ? container_of(r, MemCopyInfo, itree) : NULL;
168 }
169 
170 static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc)
171 {
172     TCGTemp *ts = mc->ts;
173     TempOptInfo *ti = ts_info(ts);
174 
175     interval_tree_remove(&mc->itree, &ctx->mem_copy);
176     QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next);
177     QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next);
178 }
179 
180 static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l)
181 {
182     while (true) {
183         MemCopyInfo *mc = mem_copy_first(ctx, s, l);
184         if (!mc) {
185             break;
186         }
187         remove_mem_copy(ctx, mc);
188     }
189 }
190 
191 static void remove_mem_copy_all(OptContext *ctx)
192 {
193     remove_mem_copy_in(ctx, 0, -1);
194     tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy));
195 }
196 
197 static TCGTemp *find_better_copy(TCGTemp *ts)
198 {
199     TCGTemp *i, *ret;
200 
201     /* If this is already readonly, we can't do better. */
202     if (temp_readonly(ts)) {
203         return ts;
204     }
205 
206     ret = ts;
207     for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
208         ret = cmp_better_copy(ret, i);
209     }
210     return ret;
211 }
212 
213 static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts)
214 {
215     TempOptInfo *si = ts_info(src_ts);
216     TempOptInfo *di = ts_info(dst_ts);
217     MemCopyInfo *mc;
218 
219     QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) {
220         tcg_debug_assert(mc->ts == src_ts);
221         mc->ts = dst_ts;
222     }
223     QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy);
224 }
225 
226 /* Reset TEMP's state, possibly removing the temp for the list of copies.  */
227 static void reset_ts(OptContext *ctx, TCGTemp *ts)
228 {
229     TempOptInfo *ti = ts_info(ts);
230     TCGTemp *pts = ti->prev_copy;
231     TCGTemp *nts = ti->next_copy;
232     TempOptInfo *pi = ts_info(pts);
233     TempOptInfo *ni = ts_info(nts);
234 
235     ni->prev_copy = ti->prev_copy;
236     pi->next_copy = ti->next_copy;
237     ti->next_copy = ts;
238     ti->prev_copy = ts;
239     ti->is_const = false;
240     ti->z_mask = -1;
241     ti->s_mask = 0;
242 
243     if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) {
244         if (ts == nts) {
245             /* Last temp copy being removed, the mem copies die. */
246             MemCopyInfo *mc;
247             QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) {
248                 interval_tree_remove(&mc->itree, &ctx->mem_copy);
249             }
250             QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy);
251         } else {
252             move_mem_copies(find_better_copy(nts), ts);
253         }
254     }
255 }
256 
257 static void reset_temp(OptContext *ctx, TCGArg arg)
258 {
259     reset_ts(ctx, arg_temp(arg));
260 }
261 
262 static void record_mem_copy(OptContext *ctx, TCGType type,
263                             TCGTemp *ts, intptr_t start, intptr_t last)
264 {
265     MemCopyInfo *mc;
266     TempOptInfo *ti;
267 
268     mc = QSIMPLEQ_FIRST(&ctx->mem_free);
269     if (mc) {
270         QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next);
271     } else {
272         mc = tcg_malloc(sizeof(*mc));
273     }
274 
275     memset(mc, 0, sizeof(*mc));
276     mc->itree.start = start;
277     mc->itree.last = last;
278     mc->type = type;
279     interval_tree_insert(&mc->itree, &ctx->mem_copy);
280 
281     ts = find_better_copy(ts);
282     ti = ts_info(ts);
283     mc->ts = ts;
284     QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next);
285 }
286 
287 static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
288 {
289     TCGTemp *i;
290 
291     if (ts1 == ts2) {
292         return true;
293     }
294 
295     if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
296         return false;
297     }
298 
299     for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
300         if (i == ts2) {
301             return true;
302         }
303     }
304 
305     return false;
306 }
307 
308 static bool args_are_copies(TCGArg arg1, TCGArg arg2)
309 {
310     return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
311 }
312 
313 static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s)
314 {
315     MemCopyInfo *mc;
316 
317     for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) {
318         if (mc->itree.start == s && mc->type == type) {
319             return find_better_copy(mc->ts);
320         }
321     }
322     return NULL;
323 }
324 
325 static TCGArg arg_new_constant(OptContext *ctx, uint64_t val)
326 {
327     TCGType type = ctx->type;
328     TCGTemp *ts;
329 
330     if (type == TCG_TYPE_I32) {
331         val = (int32_t)val;
332     }
333 
334     ts = tcg_constant_internal(type, val);
335     init_ts_info(ctx, ts);
336 
337     return temp_arg(ts);
338 }
339 
340 static TCGArg arg_new_temp(OptContext *ctx)
341 {
342     TCGTemp *ts = tcg_temp_new_internal(ctx->type, TEMP_EBB);
343     init_ts_info(ctx, ts);
344     return temp_arg(ts);
345 }
346 
347 static TCGOp *opt_insert_after(OptContext *ctx, TCGOp *op,
348                                TCGOpcode opc, unsigned narg)
349 {
350     return tcg_op_insert_after(ctx->tcg, op, opc, ctx->type, narg);
351 }
352 
353 static TCGOp *opt_insert_before(OptContext *ctx, TCGOp *op,
354                                 TCGOpcode opc, unsigned narg)
355 {
356     return tcg_op_insert_before(ctx->tcg, op, opc, ctx->type, narg);
357 }
358 
359 static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
360 {
361     TCGTemp *dst_ts = arg_temp(dst);
362     TCGTemp *src_ts = arg_temp(src);
363     TempOptInfo *di;
364     TempOptInfo *si;
365     TCGOpcode new_op;
366 
367     if (ts_are_copies(dst_ts, src_ts)) {
368         tcg_op_remove(ctx->tcg, op);
369         return true;
370     }
371 
372     reset_ts(ctx, dst_ts);
373     di = ts_info(dst_ts);
374     si = ts_info(src_ts);
375 
376     switch (ctx->type) {
377     case TCG_TYPE_I32:
378     case TCG_TYPE_I64:
379         new_op = INDEX_op_mov;
380         break;
381     case TCG_TYPE_V64:
382     case TCG_TYPE_V128:
383     case TCG_TYPE_V256:
384         /* TCGOP_TYPE and TCGOP_VECE remain unchanged.  */
385         new_op = INDEX_op_mov_vec;
386         break;
387     default:
388         g_assert_not_reached();
389     }
390     op->opc = new_op;
391     op->args[0] = dst;
392     op->args[1] = src;
393 
394     di->z_mask = si->z_mask;
395     di->s_mask = si->s_mask;
396 
397     if (src_ts->type == dst_ts->type) {
398         TempOptInfo *ni = ts_info(si->next_copy);
399 
400         di->next_copy = si->next_copy;
401         di->prev_copy = src_ts;
402         ni->prev_copy = dst_ts;
403         si->next_copy = dst_ts;
404         di->is_const = si->is_const;
405         di->val = si->val;
406 
407         if (!QSIMPLEQ_EMPTY(&si->mem_copy)
408             && cmp_better_copy(src_ts, dst_ts) == dst_ts) {
409             move_mem_copies(dst_ts, src_ts);
410         }
411     }
412     return true;
413 }
414 
415 static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
416                              TCGArg dst, uint64_t val)
417 {
418     /* Convert movi to mov with constant temp. */
419     return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val));
420 }
421 
422 static uint64_t do_constant_folding_2(TCGOpcode op, TCGType type,
423                                       uint64_t x, uint64_t y)
424 {
425     uint64_t l64, h64;
426 
427     switch (op) {
428     case INDEX_op_add:
429         return x + y;
430 
431     case INDEX_op_sub:
432         return x - y;
433 
434     case INDEX_op_mul:
435         return x * y;
436 
437     case INDEX_op_and:
438     case INDEX_op_and_vec:
439         return x & y;
440 
441     case INDEX_op_or:
442     case INDEX_op_or_vec:
443         return x | y;
444 
445     case INDEX_op_xor:
446     case INDEX_op_xor_vec:
447         return x ^ y;
448 
449     case INDEX_op_shl_i32:
450         return (uint32_t)x << (y & 31);
451 
452     case INDEX_op_shl_i64:
453         return (uint64_t)x << (y & 63);
454 
455     case INDEX_op_shr_i32:
456         return (uint32_t)x >> (y & 31);
457 
458     case INDEX_op_shr_i64:
459         return (uint64_t)x >> (y & 63);
460 
461     case INDEX_op_sar_i32:
462         return (int32_t)x >> (y & 31);
463 
464     case INDEX_op_sar_i64:
465         return (int64_t)x >> (y & 63);
466 
467     case INDEX_op_rotr_i32:
468         return ror32(x, y & 31);
469 
470     case INDEX_op_rotr_i64:
471         return ror64(x, y & 63);
472 
473     case INDEX_op_rotl_i32:
474         return rol32(x, y & 31);
475 
476     case INDEX_op_rotl_i64:
477         return rol64(x, y & 63);
478 
479     case INDEX_op_not:
480     case INDEX_op_not_vec:
481         return ~x;
482 
483     case INDEX_op_neg:
484         return -x;
485 
486     case INDEX_op_andc:
487     case INDEX_op_andc_vec:
488         return x & ~y;
489 
490     case INDEX_op_orc:
491     case INDEX_op_orc_vec:
492         return x | ~y;
493 
494     case INDEX_op_eqv:
495     case INDEX_op_eqv_vec:
496         return ~(x ^ y);
497 
498     case INDEX_op_nand:
499     case INDEX_op_nand_vec:
500         return ~(x & y);
501 
502     case INDEX_op_nor:
503     case INDEX_op_nor_vec:
504         return ~(x | y);
505 
506     case INDEX_op_clz_i32:
507         return (uint32_t)x ? clz32(x) : y;
508 
509     case INDEX_op_clz_i64:
510         return x ? clz64(x) : y;
511 
512     case INDEX_op_ctz_i32:
513         return (uint32_t)x ? ctz32(x) : y;
514 
515     case INDEX_op_ctz_i64:
516         return x ? ctz64(x) : y;
517 
518     case INDEX_op_ctpop_i32:
519         return ctpop32(x);
520 
521     case INDEX_op_ctpop_i64:
522         return ctpop64(x);
523 
524     CASE_OP_32_64(bswap16):
525         x = bswap16(x);
526         return y & TCG_BSWAP_OS ? (int16_t)x : x;
527 
528     CASE_OP_32_64(bswap32):
529         x = bswap32(x);
530         return y & TCG_BSWAP_OS ? (int32_t)x : x;
531 
532     case INDEX_op_bswap64_i64:
533         return bswap64(x);
534 
535     case INDEX_op_ext_i32_i64:
536         return (int32_t)x;
537 
538     case INDEX_op_extu_i32_i64:
539     case INDEX_op_extrl_i64_i32:
540         return (uint32_t)x;
541 
542     case INDEX_op_extrh_i64_i32:
543         return (uint64_t)x >> 32;
544 
545     case INDEX_op_muluh:
546         if (type == TCG_TYPE_I32) {
547             return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
548         }
549         mulu64(&l64, &h64, x, y);
550         return h64;
551 
552     case INDEX_op_mulsh:
553         if (type == TCG_TYPE_I32) {
554             return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
555         }
556         muls64(&l64, &h64, x, y);
557         return h64;
558 
559     case INDEX_op_divs:
560         /* Avoid crashing on divide by zero, otherwise undefined.  */
561         if (type == TCG_TYPE_I32) {
562             return (int32_t)x / ((int32_t)y ? : 1);
563         }
564         return (int64_t)x / ((int64_t)y ? : 1);
565 
566     case INDEX_op_divu:
567         if (type == TCG_TYPE_I32) {
568             return (uint32_t)x / ((uint32_t)y ? : 1);
569         }
570         return (uint64_t)x / ((uint64_t)y ? : 1);
571 
572     case INDEX_op_rems:
573         if (type == TCG_TYPE_I32) {
574             return (int32_t)x % ((int32_t)y ? : 1);
575         }
576         return (int64_t)x % ((int64_t)y ? : 1);
577 
578     case INDEX_op_remu_i32:
579         return (uint32_t)x % ((uint32_t)y ? : 1);
580     case INDEX_op_remu_i64:
581         return (uint64_t)x % ((uint64_t)y ? : 1);
582 
583     default:
584         g_assert_not_reached();
585     }
586 }
587 
588 static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
589                                     uint64_t x, uint64_t y)
590 {
591     uint64_t res = do_constant_folding_2(op, type, x, y);
592     if (type == TCG_TYPE_I32) {
593         res = (int32_t)res;
594     }
595     return res;
596 }
597 
598 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
599 {
600     switch (c) {
601     case TCG_COND_EQ:
602         return x == y;
603     case TCG_COND_NE:
604         return x != y;
605     case TCG_COND_LT:
606         return (int32_t)x < (int32_t)y;
607     case TCG_COND_GE:
608         return (int32_t)x >= (int32_t)y;
609     case TCG_COND_LE:
610         return (int32_t)x <= (int32_t)y;
611     case TCG_COND_GT:
612         return (int32_t)x > (int32_t)y;
613     case TCG_COND_LTU:
614         return x < y;
615     case TCG_COND_GEU:
616         return x >= y;
617     case TCG_COND_LEU:
618         return x <= y;
619     case TCG_COND_GTU:
620         return x > y;
621     case TCG_COND_TSTEQ:
622         return (x & y) == 0;
623     case TCG_COND_TSTNE:
624         return (x & y) != 0;
625     case TCG_COND_ALWAYS:
626     case TCG_COND_NEVER:
627         break;
628     }
629     g_assert_not_reached();
630 }
631 
632 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
633 {
634     switch (c) {
635     case TCG_COND_EQ:
636         return x == y;
637     case TCG_COND_NE:
638         return x != y;
639     case TCG_COND_LT:
640         return (int64_t)x < (int64_t)y;
641     case TCG_COND_GE:
642         return (int64_t)x >= (int64_t)y;
643     case TCG_COND_LE:
644         return (int64_t)x <= (int64_t)y;
645     case TCG_COND_GT:
646         return (int64_t)x > (int64_t)y;
647     case TCG_COND_LTU:
648         return x < y;
649     case TCG_COND_GEU:
650         return x >= y;
651     case TCG_COND_LEU:
652         return x <= y;
653     case TCG_COND_GTU:
654         return x > y;
655     case TCG_COND_TSTEQ:
656         return (x & y) == 0;
657     case TCG_COND_TSTNE:
658         return (x & y) != 0;
659     case TCG_COND_ALWAYS:
660     case TCG_COND_NEVER:
661         break;
662     }
663     g_assert_not_reached();
664 }
665 
666 static int do_constant_folding_cond_eq(TCGCond c)
667 {
668     switch (c) {
669     case TCG_COND_GT:
670     case TCG_COND_LTU:
671     case TCG_COND_LT:
672     case TCG_COND_GTU:
673     case TCG_COND_NE:
674         return 0;
675     case TCG_COND_GE:
676     case TCG_COND_GEU:
677     case TCG_COND_LE:
678     case TCG_COND_LEU:
679     case TCG_COND_EQ:
680         return 1;
681     case TCG_COND_TSTEQ:
682     case TCG_COND_TSTNE:
683         return -1;
684     case TCG_COND_ALWAYS:
685     case TCG_COND_NEVER:
686         break;
687     }
688     g_assert_not_reached();
689 }
690 
691 /*
692  * Return -1 if the condition can't be simplified,
693  * and the result of the condition (0 or 1) if it can.
694  */
695 static int do_constant_folding_cond(TCGType type, TCGArg x,
696                                     TCGArg y, TCGCond c)
697 {
698     if (arg_is_const(x) && arg_is_const(y)) {
699         uint64_t xv = arg_info(x)->val;
700         uint64_t yv = arg_info(y)->val;
701 
702         switch (type) {
703         case TCG_TYPE_I32:
704             return do_constant_folding_cond_32(xv, yv, c);
705         case TCG_TYPE_I64:
706             return do_constant_folding_cond_64(xv, yv, c);
707         default:
708             /* Only scalar comparisons are optimizable */
709             return -1;
710         }
711     } else if (args_are_copies(x, y)) {
712         return do_constant_folding_cond_eq(c);
713     } else if (arg_is_const_val(y, 0)) {
714         switch (c) {
715         case TCG_COND_LTU:
716         case TCG_COND_TSTNE:
717             return 0;
718         case TCG_COND_GEU:
719         case TCG_COND_TSTEQ:
720             return 1;
721         default:
722             return -1;
723         }
724     }
725     return -1;
726 }
727 
728 /**
729  * swap_commutative:
730  * @dest: TCGArg of the destination argument, or NO_DEST.
731  * @p1: first paired argument
732  * @p2: second paired argument
733  *
734  * If *@p1 is a constant and *@p2 is not, swap.
735  * If *@p2 matches @dest, swap.
736  * Return true if a swap was performed.
737  */
738 
739 #define NO_DEST  temp_arg(NULL)
740 
741 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
742 {
743     TCGArg a1 = *p1, a2 = *p2;
744     int sum = 0;
745     sum += arg_is_const(a1);
746     sum -= arg_is_const(a2);
747 
748     /* Prefer the constant in second argument, and then the form
749        op a, a, b, which is better handled on non-RISC hosts. */
750     if (sum > 0 || (sum == 0 && dest == a2)) {
751         *p1 = a2;
752         *p2 = a1;
753         return true;
754     }
755     return false;
756 }
757 
758 static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
759 {
760     int sum = 0;
761     sum += arg_is_const(p1[0]);
762     sum += arg_is_const(p1[1]);
763     sum -= arg_is_const(p2[0]);
764     sum -= arg_is_const(p2[1]);
765     if (sum > 0) {
766         TCGArg t;
767         t = p1[0], p1[0] = p2[0], p2[0] = t;
768         t = p1[1], p1[1] = p2[1], p2[1] = t;
769         return true;
770     }
771     return false;
772 }
773 
774 /*
775  * Return -1 if the condition can't be simplified,
776  * and the result of the condition (0 or 1) if it can.
777  */
778 static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest,
779                                      TCGArg *p1, TCGArg *p2, TCGArg *pcond)
780 {
781     TCGCond cond;
782     TempOptInfo *i1;
783     bool swap;
784     int r;
785 
786     swap = swap_commutative(dest, p1, p2);
787     cond = *pcond;
788     if (swap) {
789         *pcond = cond = tcg_swap_cond(cond);
790     }
791 
792     r = do_constant_folding_cond(ctx->type, *p1, *p2, cond);
793     if (r >= 0) {
794         return r;
795     }
796     if (!is_tst_cond(cond)) {
797         return -1;
798     }
799 
800     i1 = arg_info(*p1);
801 
802     /*
803      * TSTNE x,x -> NE x,0
804      * TSTNE x,i -> NE x,0 if i includes all nonzero bits of x
805      */
806     if (args_are_copies(*p1, *p2) ||
807         (arg_is_const(*p2) && (i1->z_mask & ~arg_info(*p2)->val) == 0)) {
808         *p2 = arg_new_constant(ctx, 0);
809         *pcond = tcg_tst_eqne_cond(cond);
810         return -1;
811     }
812 
813     /* TSTNE x,i -> LT x,0 if i only includes sign bit copies */
814     if (arg_is_const(*p2) && (arg_info(*p2)->val & ~i1->s_mask) == 0) {
815         *p2 = arg_new_constant(ctx, 0);
816         *pcond = tcg_tst_ltge_cond(cond);
817         return -1;
818     }
819 
820     /* Expand to AND with a temporary if no backend support. */
821     if (!TCG_TARGET_HAS_tst) {
822         TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3);
823         TCGArg tmp = arg_new_temp(ctx);
824 
825         op2->args[0] = tmp;
826         op2->args[1] = *p1;
827         op2->args[2] = *p2;
828 
829         *p1 = tmp;
830         *p2 = arg_new_constant(ctx, 0);
831         *pcond = tcg_tst_eqne_cond(cond);
832     }
833     return -1;
834 }
835 
836 static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args)
837 {
838     TCGArg al, ah, bl, bh;
839     TCGCond c;
840     bool swap;
841     int r;
842 
843     swap = swap_commutative2(args, args + 2);
844     c = args[4];
845     if (swap) {
846         args[4] = c = tcg_swap_cond(c);
847     }
848 
849     al = args[0];
850     ah = args[1];
851     bl = args[2];
852     bh = args[3];
853 
854     if (arg_is_const(bl) && arg_is_const(bh)) {
855         tcg_target_ulong blv = arg_info(bl)->val;
856         tcg_target_ulong bhv = arg_info(bh)->val;
857         uint64_t b = deposit64(blv, 32, 32, bhv);
858 
859         if (arg_is_const(al) && arg_is_const(ah)) {
860             tcg_target_ulong alv = arg_info(al)->val;
861             tcg_target_ulong ahv = arg_info(ah)->val;
862             uint64_t a = deposit64(alv, 32, 32, ahv);
863 
864             r = do_constant_folding_cond_64(a, b, c);
865             if (r >= 0) {
866                 return r;
867             }
868         }
869 
870         if (b == 0) {
871             switch (c) {
872             case TCG_COND_LTU:
873             case TCG_COND_TSTNE:
874                 return 0;
875             case TCG_COND_GEU:
876             case TCG_COND_TSTEQ:
877                 return 1;
878             default:
879                 break;
880             }
881         }
882 
883         /* TSTNE x,-1 -> NE x,0 */
884         if (b == -1 && is_tst_cond(c)) {
885             args[3] = args[2] = arg_new_constant(ctx, 0);
886             args[4] = tcg_tst_eqne_cond(c);
887             return -1;
888         }
889 
890         /* TSTNE x,sign -> LT x,0 */
891         if (b == INT64_MIN && is_tst_cond(c)) {
892             /* bl must be 0, so copy that to bh */
893             args[3] = bl;
894             args[4] = tcg_tst_ltge_cond(c);
895             return -1;
896         }
897     }
898 
899     if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
900         r = do_constant_folding_cond_eq(c);
901         if (r >= 0) {
902             return r;
903         }
904 
905         /* TSTNE x,x -> NE x,0 */
906         if (is_tst_cond(c)) {
907             args[3] = args[2] = arg_new_constant(ctx, 0);
908             args[4] = tcg_tst_eqne_cond(c);
909             return -1;
910         }
911     }
912 
913     /* Expand to AND with a temporary if no backend support. */
914     if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) {
915         TCGOp *op1 = opt_insert_before(ctx, op, INDEX_op_and, 3);
916         TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3);
917         TCGArg t1 = arg_new_temp(ctx);
918         TCGArg t2 = arg_new_temp(ctx);
919 
920         op1->args[0] = t1;
921         op1->args[1] = al;
922         op1->args[2] = bl;
923         op2->args[0] = t2;
924         op2->args[1] = ah;
925         op2->args[2] = bh;
926 
927         args[0] = t1;
928         args[1] = t2;
929         args[3] = args[2] = arg_new_constant(ctx, 0);
930         args[4] = tcg_tst_eqne_cond(c);
931     }
932     return -1;
933 }
934 
935 static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
936 {
937     for (int i = 0; i < nb_args; i++) {
938         TCGTemp *ts = arg_temp(op->args[i]);
939         init_ts_info(ctx, ts);
940     }
941 }
942 
943 static void copy_propagate(OptContext *ctx, TCGOp *op,
944                            int nb_oargs, int nb_iargs)
945 {
946     for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
947         TCGTemp *ts = arg_temp(op->args[i]);
948         if (ts_is_copy(ts)) {
949             op->args[i] = temp_arg(find_better_copy(ts));
950         }
951     }
952 }
953 
954 static void finish_bb(OptContext *ctx)
955 {
956     /* We only optimize memory barriers across basic blocks. */
957     ctx->prev_mb = NULL;
958 }
959 
960 static void finish_ebb(OptContext *ctx)
961 {
962     finish_bb(ctx);
963     /* We only optimize across extended basic blocks. */
964     memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
965     remove_mem_copy_all(ctx);
966 }
967 
968 static bool finish_folding(OptContext *ctx, TCGOp *op)
969 {
970     const TCGOpDef *def = &tcg_op_defs[op->opc];
971     int i, nb_oargs;
972 
973     nb_oargs = def->nb_oargs;
974     for (i = 0; i < nb_oargs; i++) {
975         TCGTemp *ts = arg_temp(op->args[i]);
976         reset_ts(ctx, ts);
977     }
978     return true;
979 }
980 
981 /*
982  * The fold_* functions return true when processing is complete,
983  * usually by folding the operation to a constant or to a copy,
984  * and calling tcg_opt_gen_{mov,movi}.  They may do other things,
985  * like collect information about the value produced, for use in
986  * optimizing a subsequent operation.
987  *
988  * These first fold_* functions are all helpers, used by other
989  * folders for more specific operations.
990  */
991 
992 static bool fold_const1(OptContext *ctx, TCGOp *op)
993 {
994     if (arg_is_const(op->args[1])) {
995         uint64_t t;
996 
997         t = arg_info(op->args[1])->val;
998         t = do_constant_folding(op->opc, ctx->type, t, 0);
999         return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1000     }
1001     return false;
1002 }
1003 
1004 static bool fold_const2(OptContext *ctx, TCGOp *op)
1005 {
1006     if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1007         uint64_t t1 = arg_info(op->args[1])->val;
1008         uint64_t t2 = arg_info(op->args[2])->val;
1009 
1010         t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
1011         return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1012     }
1013     return false;
1014 }
1015 
1016 static bool fold_commutative(OptContext *ctx, TCGOp *op)
1017 {
1018     swap_commutative(op->args[0], &op->args[1], &op->args[2]);
1019     return false;
1020 }
1021 
1022 static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
1023 {
1024     swap_commutative(op->args[0], &op->args[1], &op->args[2]);
1025     return fold_const2(ctx, op);
1026 }
1027 
1028 /*
1029  * Record "zero" and "sign" masks for the single output of @op.
1030  * See TempOptInfo definition of z_mask and s_mask.
1031  * If z_mask allows, fold the output to constant zero.
1032  * The passed s_mask may be augmented by z_mask.
1033  */
1034 static bool fold_masks_zs(OptContext *ctx, TCGOp *op,
1035                           uint64_t z_mask, int64_t s_mask)
1036 {
1037     const TCGOpDef *def = &tcg_op_defs[op->opc];
1038     TCGTemp *ts;
1039     TempOptInfo *ti;
1040     int rep;
1041 
1042     /* Only single-output opcodes are supported here. */
1043     tcg_debug_assert(def->nb_oargs == 1);
1044 
1045     /*
1046      * 32-bit ops generate 32-bit results, which for the purpose of
1047      * simplifying tcg are sign-extended.  Certainly that's how we
1048      * represent our constants elsewhere.  Note that the bits will
1049      * be reset properly for a 64-bit value when encountering the
1050      * type changing opcodes.
1051      */
1052     if (ctx->type == TCG_TYPE_I32) {
1053         z_mask = (int32_t)z_mask;
1054         s_mask |= INT32_MIN;
1055     }
1056 
1057     if (z_mask == 0) {
1058         return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
1059     }
1060 
1061     ts = arg_temp(op->args[0]);
1062     reset_ts(ctx, ts);
1063 
1064     ti = ts_info(ts);
1065     ti->z_mask = z_mask;
1066 
1067     /* Canonicalize s_mask and incorporate data from z_mask. */
1068     rep = clz64(~s_mask);
1069     rep = MAX(rep, clz64(z_mask));
1070     rep = MAX(rep - 1, 0);
1071     ti->s_mask = INT64_MIN >> rep;
1072 
1073     return true;
1074 }
1075 
1076 static bool fold_masks_z(OptContext *ctx, TCGOp *op, uint64_t z_mask)
1077 {
1078     return fold_masks_zs(ctx, op, z_mask, 0);
1079 }
1080 
1081 static bool fold_masks_s(OptContext *ctx, TCGOp *op, uint64_t s_mask)
1082 {
1083     return fold_masks_zs(ctx, op, -1, s_mask);
1084 }
1085 
1086 /*
1087  * An "affected" mask bit is 0 if and only if the result is identical
1088  * to the first input.  Thus if the entire mask is 0, the operation
1089  * is equivalent to a copy.
1090  */
1091 static bool fold_affected_mask(OptContext *ctx, TCGOp *op, uint64_t a_mask)
1092 {
1093     if (ctx->type == TCG_TYPE_I32) {
1094         a_mask = (uint32_t)a_mask;
1095     }
1096     if (a_mask == 0) {
1097         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1098     }
1099     return false;
1100 }
1101 
1102 /*
1103  * Convert @op to NOT, if NOT is supported by the host.
1104  * Return true f the conversion is successful, which will still
1105  * indicate that the processing is complete.
1106  */
1107 static bool fold_not(OptContext *ctx, TCGOp *op);
1108 static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
1109 {
1110     TCGOpcode not_op;
1111     bool have_not;
1112 
1113     switch (ctx->type) {
1114     case TCG_TYPE_I32:
1115     case TCG_TYPE_I64:
1116         not_op = INDEX_op_not;
1117         have_not = tcg_op_supported(INDEX_op_not, ctx->type, 0);
1118         break;
1119     case TCG_TYPE_V64:
1120     case TCG_TYPE_V128:
1121     case TCG_TYPE_V256:
1122         not_op = INDEX_op_not_vec;
1123         have_not = TCG_TARGET_HAS_not_vec;
1124         break;
1125     default:
1126         g_assert_not_reached();
1127     }
1128     if (have_not) {
1129         op->opc = not_op;
1130         op->args[1] = op->args[idx];
1131         return fold_not(ctx, op);
1132     }
1133     return false;
1134 }
1135 
1136 /* If the binary operation has first argument @i, fold to @i. */
1137 static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1138 {
1139     if (arg_is_const_val(op->args[1], i)) {
1140         return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1141     }
1142     return false;
1143 }
1144 
1145 /* If the binary operation has first argument @i, fold to NOT. */
1146 static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
1147 {
1148     if (arg_is_const_val(op->args[1], i)) {
1149         return fold_to_not(ctx, op, 2);
1150     }
1151     return false;
1152 }
1153 
1154 /* If the binary operation has second argument @i, fold to @i. */
1155 static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1156 {
1157     if (arg_is_const_val(op->args[2], i)) {
1158         return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1159     }
1160     return false;
1161 }
1162 
1163 /* If the binary operation has second argument @i, fold to identity. */
1164 static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
1165 {
1166     if (arg_is_const_val(op->args[2], i)) {
1167         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1168     }
1169     return false;
1170 }
1171 
1172 /* If the binary operation has second argument @i, fold to NOT. */
1173 static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
1174 {
1175     if (arg_is_const_val(op->args[2], i)) {
1176         return fold_to_not(ctx, op, 1);
1177     }
1178     return false;
1179 }
1180 
1181 /* If the binary operation has both arguments equal, fold to @i. */
1182 static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1183 {
1184     if (args_are_copies(op->args[1], op->args[2])) {
1185         return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1186     }
1187     return false;
1188 }
1189 
1190 /* If the binary operation has both arguments equal, fold to identity. */
1191 static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
1192 {
1193     if (args_are_copies(op->args[1], op->args[2])) {
1194         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1195     }
1196     return false;
1197 }
1198 
1199 /*
1200  * These outermost fold_<op> functions are sorted alphabetically.
1201  *
1202  * The ordering of the transformations should be:
1203  *   1) those that produce a constant
1204  *   2) those that produce a copy
1205  *   3) those that produce information about the result value.
1206  */
1207 
1208 static bool fold_or(OptContext *ctx, TCGOp *op);
1209 static bool fold_orc(OptContext *ctx, TCGOp *op);
1210 static bool fold_xor(OptContext *ctx, TCGOp *op);
1211 
1212 static bool fold_add(OptContext *ctx, TCGOp *op)
1213 {
1214     if (fold_const2_commutative(ctx, op) ||
1215         fold_xi_to_x(ctx, op, 0)) {
1216         return true;
1217     }
1218     return finish_folding(ctx, op);
1219 }
1220 
1221 /* We cannot as yet do_constant_folding with vectors. */
1222 static bool fold_add_vec(OptContext *ctx, TCGOp *op)
1223 {
1224     if (fold_commutative(ctx, op) ||
1225         fold_xi_to_x(ctx, op, 0)) {
1226         return true;
1227     }
1228     return finish_folding(ctx, op);
1229 }
1230 
1231 static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
1232 {
1233     bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]);
1234     bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]);
1235 
1236     if (a_const && b_const) {
1237         uint64_t al = arg_info(op->args[2])->val;
1238         uint64_t ah = arg_info(op->args[3])->val;
1239         uint64_t bl = arg_info(op->args[4])->val;
1240         uint64_t bh = arg_info(op->args[5])->val;
1241         TCGArg rl, rh;
1242         TCGOp *op2;
1243 
1244         if (ctx->type == TCG_TYPE_I32) {
1245             uint64_t a = deposit64(al, 32, 32, ah);
1246             uint64_t b = deposit64(bl, 32, 32, bh);
1247 
1248             if (add) {
1249                 a += b;
1250             } else {
1251                 a -= b;
1252             }
1253 
1254             al = sextract64(a, 0, 32);
1255             ah = sextract64(a, 32, 32);
1256         } else {
1257             Int128 a = int128_make128(al, ah);
1258             Int128 b = int128_make128(bl, bh);
1259 
1260             if (add) {
1261                 a = int128_add(a, b);
1262             } else {
1263                 a = int128_sub(a, b);
1264             }
1265 
1266             al = int128_getlo(a);
1267             ah = int128_gethi(a);
1268         }
1269 
1270         rl = op->args[0];
1271         rh = op->args[1];
1272 
1273         /* The proper opcode is supplied by tcg_opt_gen_mov. */
1274         op2 = opt_insert_before(ctx, op, 0, 2);
1275 
1276         tcg_opt_gen_movi(ctx, op, rl, al);
1277         tcg_opt_gen_movi(ctx, op2, rh, ah);
1278         return true;
1279     }
1280 
1281     /* Fold sub2 r,x,i to add2 r,x,-i */
1282     if (!add && b_const) {
1283         uint64_t bl = arg_info(op->args[4])->val;
1284         uint64_t bh = arg_info(op->args[5])->val;
1285 
1286         /* Negate the two parts without assembling and disassembling. */
1287         bl = -bl;
1288         bh = ~bh + !bl;
1289 
1290         op->opc = (ctx->type == TCG_TYPE_I32
1291                    ? INDEX_op_add2_i32 : INDEX_op_add2_i64);
1292         op->args[4] = arg_new_constant(ctx, bl);
1293         op->args[5] = arg_new_constant(ctx, bh);
1294     }
1295     return finish_folding(ctx, op);
1296 }
1297 
1298 static bool fold_add2(OptContext *ctx, TCGOp *op)
1299 {
1300     /* Note that the high and low parts may be independently swapped. */
1301     swap_commutative(op->args[0], &op->args[2], &op->args[4]);
1302     swap_commutative(op->args[1], &op->args[3], &op->args[5]);
1303 
1304     return fold_addsub2(ctx, op, true);
1305 }
1306 
1307 static bool fold_and(OptContext *ctx, TCGOp *op)
1308 {
1309     uint64_t z1, z2, z_mask, s_mask;
1310     TempOptInfo *t1, *t2;
1311 
1312     if (fold_const2_commutative(ctx, op) ||
1313         fold_xi_to_i(ctx, op, 0) ||
1314         fold_xi_to_x(ctx, op, -1) ||
1315         fold_xx_to_x(ctx, op)) {
1316         return true;
1317     }
1318 
1319     t1 = arg_info(op->args[1]);
1320     t2 = arg_info(op->args[2]);
1321     z1 = t1->z_mask;
1322     z2 = t2->z_mask;
1323 
1324     /*
1325      * Known-zeros does not imply known-ones.  Therefore unless
1326      * arg2 is constant, we can't infer affected bits from it.
1327      */
1328     if (ti_is_const(t2) && fold_affected_mask(ctx, op, z1 & ~z2)) {
1329         return true;
1330     }
1331 
1332     z_mask = z1 & z2;
1333 
1334     /*
1335      * Sign repetitions are perforce all identical, whether they are 1 or 0.
1336      * Bitwise operations preserve the relative quantity of the repetitions.
1337      */
1338     s_mask = t1->s_mask & t2->s_mask;
1339 
1340     return fold_masks_zs(ctx, op, z_mask, s_mask);
1341 }
1342 
1343 static bool fold_andc(OptContext *ctx, TCGOp *op)
1344 {
1345     uint64_t z_mask, s_mask;
1346     TempOptInfo *t1, *t2;
1347 
1348     if (fold_const2(ctx, op) ||
1349         fold_xx_to_i(ctx, op, 0) ||
1350         fold_xi_to_x(ctx, op, 0) ||
1351         fold_ix_to_not(ctx, op, -1)) {
1352         return true;
1353     }
1354 
1355     t1 = arg_info(op->args[1]);
1356     t2 = arg_info(op->args[2]);
1357     z_mask = t1->z_mask;
1358 
1359     if (ti_is_const(t2)) {
1360         /* Fold andc r,x,i to and r,x,~i. */
1361         switch (ctx->type) {
1362         case TCG_TYPE_I32:
1363         case TCG_TYPE_I64:
1364             op->opc = INDEX_op_and;
1365             break;
1366         case TCG_TYPE_V64:
1367         case TCG_TYPE_V128:
1368         case TCG_TYPE_V256:
1369             op->opc = INDEX_op_and_vec;
1370             break;
1371         default:
1372             g_assert_not_reached();
1373         }
1374         op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2));
1375         return fold_and(ctx, op);
1376     }
1377 
1378     /*
1379      * Known-zeros does not imply known-ones.  Therefore unless
1380      * arg2 is constant, we can't infer anything from it.
1381      */
1382     if (ti_is_const(t2)) {
1383         uint64_t v2 = ti_const_val(t2);
1384         if (fold_affected_mask(ctx, op, z_mask & v2)) {
1385             return true;
1386         }
1387         z_mask &= ~v2;
1388     }
1389 
1390     s_mask = t1->s_mask & t2->s_mask;
1391     return fold_masks_zs(ctx, op, z_mask, s_mask);
1392 }
1393 
1394 static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op)
1395 {
1396     /* If true and false values are the same, eliminate the cmp. */
1397     if (args_are_copies(op->args[2], op->args[3])) {
1398         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1399     }
1400 
1401     if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
1402         uint64_t tv = arg_info(op->args[2])->val;
1403         uint64_t fv = arg_info(op->args[3])->val;
1404 
1405         if (tv == -1 && fv == 0) {
1406             return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1407         }
1408         if (tv == 0 && fv == -1) {
1409             if (TCG_TARGET_HAS_not_vec) {
1410                 op->opc = INDEX_op_not_vec;
1411                 return fold_not(ctx, op);
1412             } else {
1413                 op->opc = INDEX_op_xor_vec;
1414                 op->args[2] = arg_new_constant(ctx, -1);
1415                 return fold_xor(ctx, op);
1416             }
1417         }
1418     }
1419     if (arg_is_const(op->args[2])) {
1420         uint64_t tv = arg_info(op->args[2])->val;
1421         if (tv == -1) {
1422             op->opc = INDEX_op_or_vec;
1423             op->args[2] = op->args[3];
1424             return fold_or(ctx, op);
1425         }
1426         if (tv == 0 && TCG_TARGET_HAS_andc_vec) {
1427             op->opc = INDEX_op_andc_vec;
1428             op->args[2] = op->args[1];
1429             op->args[1] = op->args[3];
1430             return fold_andc(ctx, op);
1431         }
1432     }
1433     if (arg_is_const(op->args[3])) {
1434         uint64_t fv = arg_info(op->args[3])->val;
1435         if (fv == 0) {
1436             op->opc = INDEX_op_and_vec;
1437             return fold_and(ctx, op);
1438         }
1439         if (fv == -1 && TCG_TARGET_HAS_orc_vec) {
1440             op->opc = INDEX_op_orc_vec;
1441             op->args[2] = op->args[1];
1442             op->args[1] = op->args[3];
1443             return fold_orc(ctx, op);
1444         }
1445     }
1446     return finish_folding(ctx, op);
1447 }
1448 
1449 static bool fold_brcond(OptContext *ctx, TCGOp *op)
1450 {
1451     int i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[0],
1452                                       &op->args[1], &op->args[2]);
1453     if (i == 0) {
1454         tcg_op_remove(ctx->tcg, op);
1455         return true;
1456     }
1457     if (i > 0) {
1458         op->opc = INDEX_op_br;
1459         op->args[0] = op->args[3];
1460         finish_ebb(ctx);
1461     } else {
1462         finish_bb(ctx);
1463     }
1464     return true;
1465 }
1466 
1467 static bool fold_brcond2(OptContext *ctx, TCGOp *op)
1468 {
1469     TCGCond cond;
1470     TCGArg label;
1471     int i, inv = 0;
1472 
1473     i = do_constant_folding_cond2(ctx, op, &op->args[0]);
1474     cond = op->args[4];
1475     label = op->args[5];
1476     if (i >= 0) {
1477         goto do_brcond_const;
1478     }
1479 
1480     switch (cond) {
1481     case TCG_COND_LT:
1482     case TCG_COND_GE:
1483         /*
1484          * Simplify LT/GE comparisons vs zero to a single compare
1485          * vs the high word of the input.
1486          */
1487         if (arg_is_const_val(op->args[2], 0) &&
1488             arg_is_const_val(op->args[3], 0)) {
1489             goto do_brcond_high;
1490         }
1491         break;
1492 
1493     case TCG_COND_NE:
1494         inv = 1;
1495         QEMU_FALLTHROUGH;
1496     case TCG_COND_EQ:
1497         /*
1498          * Simplify EQ/NE comparisons where one of the pairs
1499          * can be simplified.
1500          */
1501         i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
1502                                      op->args[2], cond);
1503         switch (i ^ inv) {
1504         case 0:
1505             goto do_brcond_const;
1506         case 1:
1507             goto do_brcond_high;
1508         }
1509 
1510         i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
1511                                      op->args[3], cond);
1512         switch (i ^ inv) {
1513         case 0:
1514             goto do_brcond_const;
1515         case 1:
1516             goto do_brcond_low;
1517         }
1518         break;
1519 
1520     case TCG_COND_TSTEQ:
1521     case TCG_COND_TSTNE:
1522         if (arg_is_const_val(op->args[2], 0)) {
1523             goto do_brcond_high;
1524         }
1525         if (arg_is_const_val(op->args[3], 0)) {
1526             goto do_brcond_low;
1527         }
1528         break;
1529 
1530     default:
1531         break;
1532 
1533     do_brcond_low:
1534         op->opc = INDEX_op_brcond_i32;
1535         op->args[1] = op->args[2];
1536         op->args[2] = cond;
1537         op->args[3] = label;
1538         return fold_brcond(ctx, op);
1539 
1540     do_brcond_high:
1541         op->opc = INDEX_op_brcond_i32;
1542         op->args[0] = op->args[1];
1543         op->args[1] = op->args[3];
1544         op->args[2] = cond;
1545         op->args[3] = label;
1546         return fold_brcond(ctx, op);
1547 
1548     do_brcond_const:
1549         if (i == 0) {
1550             tcg_op_remove(ctx->tcg, op);
1551             return true;
1552         }
1553         op->opc = INDEX_op_br;
1554         op->args[0] = label;
1555         finish_ebb(ctx);
1556         return true;
1557     }
1558 
1559     finish_bb(ctx);
1560     return true;
1561 }
1562 
1563 static bool fold_bswap(OptContext *ctx, TCGOp *op)
1564 {
1565     uint64_t z_mask, s_mask, sign;
1566     TempOptInfo *t1 = arg_info(op->args[1]);
1567 
1568     if (ti_is_const(t1)) {
1569         return tcg_opt_gen_movi(ctx, op, op->args[0],
1570                                 do_constant_folding(op->opc, ctx->type,
1571                                                     ti_const_val(t1),
1572                                                     op->args[2]));
1573     }
1574 
1575     z_mask = t1->z_mask;
1576     switch (op->opc) {
1577     case INDEX_op_bswap16_i32:
1578     case INDEX_op_bswap16_i64:
1579         z_mask = bswap16(z_mask);
1580         sign = INT16_MIN;
1581         break;
1582     case INDEX_op_bswap32_i32:
1583     case INDEX_op_bswap32_i64:
1584         z_mask = bswap32(z_mask);
1585         sign = INT32_MIN;
1586         break;
1587     case INDEX_op_bswap64_i64:
1588         z_mask = bswap64(z_mask);
1589         sign = INT64_MIN;
1590         break;
1591     default:
1592         g_assert_not_reached();
1593     }
1594 
1595     s_mask = 0;
1596     switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1597     case TCG_BSWAP_OZ:
1598         break;
1599     case TCG_BSWAP_OS:
1600         /* If the sign bit may be 1, force all the bits above to 1. */
1601         if (z_mask & sign) {
1602             z_mask |= sign;
1603         }
1604         /* The value and therefore s_mask is explicitly sign-extended. */
1605         s_mask = sign;
1606         break;
1607     default:
1608         /* The high bits are undefined: force all bits above the sign to 1. */
1609         z_mask |= sign << 1;
1610         break;
1611     }
1612 
1613     return fold_masks_zs(ctx, op, z_mask, s_mask);
1614 }
1615 
1616 static bool fold_call(OptContext *ctx, TCGOp *op)
1617 {
1618     TCGContext *s = ctx->tcg;
1619     int nb_oargs = TCGOP_CALLO(op);
1620     int nb_iargs = TCGOP_CALLI(op);
1621     int flags, i;
1622 
1623     init_arguments(ctx, op, nb_oargs + nb_iargs);
1624     copy_propagate(ctx, op, nb_oargs, nb_iargs);
1625 
1626     /* If the function reads or writes globals, reset temp data. */
1627     flags = tcg_call_flags(op);
1628     if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1629         int nb_globals = s->nb_globals;
1630 
1631         for (i = 0; i < nb_globals; i++) {
1632             if (test_bit(i, ctx->temps_used.l)) {
1633                 reset_ts(ctx, &ctx->tcg->temps[i]);
1634             }
1635         }
1636     }
1637 
1638     /* If the function has side effects, reset mem data. */
1639     if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) {
1640         remove_mem_copy_all(ctx);
1641     }
1642 
1643     /* Reset temp data for outputs. */
1644     for (i = 0; i < nb_oargs; i++) {
1645         reset_temp(ctx, op->args[i]);
1646     }
1647 
1648     /* Stop optimizing MB across calls. */
1649     ctx->prev_mb = NULL;
1650     return true;
1651 }
1652 
1653 static bool fold_cmp_vec(OptContext *ctx, TCGOp *op)
1654 {
1655     /* Canonicalize the comparison to put immediate second. */
1656     if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
1657         op->args[3] = tcg_swap_cond(op->args[3]);
1658     }
1659     return finish_folding(ctx, op);
1660 }
1661 
1662 static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op)
1663 {
1664     /* If true and false values are the same, eliminate the cmp. */
1665     if (args_are_copies(op->args[3], op->args[4])) {
1666         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
1667     }
1668 
1669     /* Canonicalize the comparison to put immediate second. */
1670     if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
1671         op->args[5] = tcg_swap_cond(op->args[5]);
1672     }
1673     /*
1674      * Canonicalize the "false" input reg to match the destination,
1675      * so that the tcg backend can implement "move if true".
1676      */
1677     if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
1678         op->args[5] = tcg_invert_cond(op->args[5]);
1679     }
1680     return finish_folding(ctx, op);
1681 }
1682 
1683 static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1684 {
1685     uint64_t z_mask, s_mask;
1686     TempOptInfo *t1 = arg_info(op->args[1]);
1687     TempOptInfo *t2 = arg_info(op->args[2]);
1688 
1689     if (ti_is_const(t1)) {
1690         uint64_t t = ti_const_val(t1);
1691 
1692         if (t != 0) {
1693             t = do_constant_folding(op->opc, ctx->type, t, 0);
1694             return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1695         }
1696         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1697     }
1698 
1699     switch (ctx->type) {
1700     case TCG_TYPE_I32:
1701         z_mask = 31;
1702         break;
1703     case TCG_TYPE_I64:
1704         z_mask = 63;
1705         break;
1706     default:
1707         g_assert_not_reached();
1708     }
1709     s_mask = ~z_mask;
1710     z_mask |= t2->z_mask;
1711     s_mask &= t2->s_mask;
1712 
1713     return fold_masks_zs(ctx, op, z_mask, s_mask);
1714 }
1715 
1716 static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1717 {
1718     uint64_t z_mask;
1719 
1720     if (fold_const1(ctx, op)) {
1721         return true;
1722     }
1723 
1724     switch (ctx->type) {
1725     case TCG_TYPE_I32:
1726         z_mask = 32 | 31;
1727         break;
1728     case TCG_TYPE_I64:
1729         z_mask = 64 | 63;
1730         break;
1731     default:
1732         g_assert_not_reached();
1733     }
1734     return fold_masks_z(ctx, op, z_mask);
1735 }
1736 
1737 static bool fold_deposit(OptContext *ctx, TCGOp *op)
1738 {
1739     TempOptInfo *t1 = arg_info(op->args[1]);
1740     TempOptInfo *t2 = arg_info(op->args[2]);
1741     int ofs = op->args[3];
1742     int len = op->args[4];
1743     int width = 8 * tcg_type_size(ctx->type);
1744     uint64_t z_mask, s_mask;
1745 
1746     if (ti_is_const(t1) && ti_is_const(t2)) {
1747         return tcg_opt_gen_movi(ctx, op, op->args[0],
1748                                 deposit64(ti_const_val(t1), ofs, len,
1749                                           ti_const_val(t2)));
1750     }
1751 
1752     /* Inserting a value into zero at offset 0. */
1753     if (ti_is_const_val(t1, 0) && ofs == 0) {
1754         uint64_t mask = MAKE_64BIT_MASK(0, len);
1755 
1756         op->opc = INDEX_op_and;
1757         op->args[1] = op->args[2];
1758         op->args[2] = arg_new_constant(ctx, mask);
1759         return fold_and(ctx, op);
1760     }
1761 
1762     /* Inserting zero into a value. */
1763     if (ti_is_const_val(t2, 0)) {
1764         uint64_t mask = deposit64(-1, ofs, len, 0);
1765 
1766         op->opc = INDEX_op_and;
1767         op->args[2] = arg_new_constant(ctx, mask);
1768         return fold_and(ctx, op);
1769     }
1770 
1771     /* The s_mask from the top portion of the deposit is still valid. */
1772     if (ofs + len == width) {
1773         s_mask = t2->s_mask << ofs;
1774     } else {
1775         s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len);
1776     }
1777 
1778     z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask);
1779     return fold_masks_zs(ctx, op, z_mask, s_mask);
1780 }
1781 
1782 static bool fold_divide(OptContext *ctx, TCGOp *op)
1783 {
1784     if (fold_const2(ctx, op) ||
1785         fold_xi_to_x(ctx, op, 1)) {
1786         return true;
1787     }
1788     return finish_folding(ctx, op);
1789 }
1790 
1791 static bool fold_dup(OptContext *ctx, TCGOp *op)
1792 {
1793     if (arg_is_const(op->args[1])) {
1794         uint64_t t = arg_info(op->args[1])->val;
1795         t = dup_const(TCGOP_VECE(op), t);
1796         return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1797     }
1798     return finish_folding(ctx, op);
1799 }
1800 
1801 static bool fold_dup2(OptContext *ctx, TCGOp *op)
1802 {
1803     if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1804         uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1805                                arg_info(op->args[2])->val);
1806         return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1807     }
1808 
1809     if (args_are_copies(op->args[1], op->args[2])) {
1810         op->opc = INDEX_op_dup_vec;
1811         TCGOP_VECE(op) = MO_32;
1812     }
1813     return finish_folding(ctx, op);
1814 }
1815 
1816 static bool fold_eqv(OptContext *ctx, TCGOp *op)
1817 {
1818     uint64_t s_mask;
1819     TempOptInfo *t1, *t2;
1820 
1821     if (fold_const2_commutative(ctx, op) ||
1822         fold_xi_to_x(ctx, op, -1) ||
1823         fold_xi_to_not(ctx, op, 0)) {
1824         return true;
1825     }
1826 
1827     t2 = arg_info(op->args[2]);
1828     if (ti_is_const(t2)) {
1829         /* Fold eqv r,x,i to xor r,x,~i. */
1830         switch (ctx->type) {
1831         case TCG_TYPE_I32:
1832         case TCG_TYPE_I64:
1833             op->opc = INDEX_op_xor;
1834             break;
1835         case TCG_TYPE_V64:
1836         case TCG_TYPE_V128:
1837         case TCG_TYPE_V256:
1838             op->opc = INDEX_op_xor_vec;
1839             break;
1840         default:
1841             g_assert_not_reached();
1842         }
1843         op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2));
1844         return fold_xor(ctx, op);
1845     }
1846 
1847     t1 = arg_info(op->args[1]);
1848     s_mask = t1->s_mask & t2->s_mask;
1849     return fold_masks_s(ctx, op, s_mask);
1850 }
1851 
1852 static bool fold_extract(OptContext *ctx, TCGOp *op)
1853 {
1854     uint64_t z_mask_old, z_mask;
1855     TempOptInfo *t1 = arg_info(op->args[1]);
1856     int pos = op->args[2];
1857     int len = op->args[3];
1858 
1859     if (ti_is_const(t1)) {
1860         return tcg_opt_gen_movi(ctx, op, op->args[0],
1861                                 extract64(ti_const_val(t1), pos, len));
1862     }
1863 
1864     z_mask_old = t1->z_mask;
1865     z_mask = extract64(z_mask_old, pos, len);
1866     if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) {
1867         return true;
1868     }
1869 
1870     return fold_masks_z(ctx, op, z_mask);
1871 }
1872 
1873 static bool fold_extract2(OptContext *ctx, TCGOp *op)
1874 {
1875     if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1876         uint64_t v1 = arg_info(op->args[1])->val;
1877         uint64_t v2 = arg_info(op->args[2])->val;
1878         int shr = op->args[3];
1879 
1880         if (op->opc == INDEX_op_extract2_i64) {
1881             v1 >>= shr;
1882             v2 <<= 64 - shr;
1883         } else {
1884             v1 = (uint32_t)v1 >> shr;
1885             v2 = (uint64_t)((int32_t)v2 << (32 - shr));
1886         }
1887         return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1888     }
1889     return finish_folding(ctx, op);
1890 }
1891 
1892 static bool fold_exts(OptContext *ctx, TCGOp *op)
1893 {
1894     uint64_t s_mask, z_mask;
1895     TempOptInfo *t1;
1896 
1897     if (fold_const1(ctx, op)) {
1898         return true;
1899     }
1900 
1901     t1 = arg_info(op->args[1]);
1902     z_mask = t1->z_mask;
1903     s_mask = t1->s_mask;
1904 
1905     switch (op->opc) {
1906     case INDEX_op_ext_i32_i64:
1907         s_mask |= INT32_MIN;
1908         z_mask = (int32_t)z_mask;
1909         break;
1910     default:
1911         g_assert_not_reached();
1912     }
1913     return fold_masks_zs(ctx, op, z_mask, s_mask);
1914 }
1915 
1916 static bool fold_extu(OptContext *ctx, TCGOp *op)
1917 {
1918     uint64_t z_mask;
1919 
1920     if (fold_const1(ctx, op)) {
1921         return true;
1922     }
1923 
1924     z_mask = arg_info(op->args[1])->z_mask;
1925     switch (op->opc) {
1926     case INDEX_op_extrl_i64_i32:
1927     case INDEX_op_extu_i32_i64:
1928         z_mask = (uint32_t)z_mask;
1929         break;
1930     case INDEX_op_extrh_i64_i32:
1931         z_mask >>= 32;
1932         break;
1933     default:
1934         g_assert_not_reached();
1935     }
1936     return fold_masks_z(ctx, op, z_mask);
1937 }
1938 
1939 static bool fold_mb(OptContext *ctx, TCGOp *op)
1940 {
1941     /* Eliminate duplicate and redundant fence instructions.  */
1942     if (ctx->prev_mb) {
1943         /*
1944          * Merge two barriers of the same type into one,
1945          * or a weaker barrier into a stronger one,
1946          * or two weaker barriers into a stronger one.
1947          *   mb X; mb Y => mb X|Y
1948          *   mb; strl => mb; st
1949          *   ldaq; mb => ld; mb
1950          *   ldaq; strl => ld; mb; st
1951          * Other combinations are also merged into a strong
1952          * barrier.  This is stricter than specified but for
1953          * the purposes of TCG is better than not optimizing.
1954          */
1955         ctx->prev_mb->args[0] |= op->args[0];
1956         tcg_op_remove(ctx->tcg, op);
1957     } else {
1958         ctx->prev_mb = op;
1959     }
1960     return true;
1961 }
1962 
1963 static bool fold_mov(OptContext *ctx, TCGOp *op)
1964 {
1965     return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1966 }
1967 
1968 static bool fold_movcond(OptContext *ctx, TCGOp *op)
1969 {
1970     uint64_t z_mask, s_mask;
1971     TempOptInfo *tt, *ft;
1972     int i;
1973 
1974     /* If true and false values are the same, eliminate the cmp. */
1975     if (args_are_copies(op->args[3], op->args[4])) {
1976         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
1977     }
1978 
1979     /*
1980      * Canonicalize the "false" input reg to match the destination reg so
1981      * that the tcg backend can implement a "move if true" operation.
1982      */
1983     if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
1984         op->args[5] = tcg_invert_cond(op->args[5]);
1985     }
1986 
1987     i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1],
1988                                   &op->args[2], &op->args[5]);
1989     if (i >= 0) {
1990         return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1991     }
1992 
1993     tt = arg_info(op->args[3]);
1994     ft = arg_info(op->args[4]);
1995     z_mask = tt->z_mask | ft->z_mask;
1996     s_mask = tt->s_mask & ft->s_mask;
1997 
1998     if (ti_is_const(tt) && ti_is_const(ft)) {
1999         uint64_t tv = ti_const_val(tt);
2000         uint64_t fv = ti_const_val(ft);
2001         TCGOpcode opc, negopc = 0;
2002         TCGCond cond = op->args[5];
2003 
2004         switch (ctx->type) {
2005         case TCG_TYPE_I32:
2006             opc = INDEX_op_setcond_i32;
2007             if (TCG_TARGET_HAS_negsetcond_i32) {
2008                 negopc = INDEX_op_negsetcond_i32;
2009             }
2010             tv = (int32_t)tv;
2011             fv = (int32_t)fv;
2012             break;
2013         case TCG_TYPE_I64:
2014             opc = INDEX_op_setcond_i64;
2015             if (TCG_TARGET_HAS_negsetcond_i64) {
2016                 negopc = INDEX_op_negsetcond_i64;
2017             }
2018             break;
2019         default:
2020             g_assert_not_reached();
2021         }
2022 
2023         if (tv == 1 && fv == 0) {
2024             op->opc = opc;
2025             op->args[3] = cond;
2026         } else if (fv == 1 && tv == 0) {
2027             op->opc = opc;
2028             op->args[3] = tcg_invert_cond(cond);
2029         } else if (negopc) {
2030             if (tv == -1 && fv == 0) {
2031                 op->opc = negopc;
2032                 op->args[3] = cond;
2033             } else if (fv == -1 && tv == 0) {
2034                 op->opc = negopc;
2035                 op->args[3] = tcg_invert_cond(cond);
2036             }
2037         }
2038     }
2039 
2040     return fold_masks_zs(ctx, op, z_mask, s_mask);
2041 }
2042 
2043 static bool fold_mul(OptContext *ctx, TCGOp *op)
2044 {
2045     if (fold_const2(ctx, op) ||
2046         fold_xi_to_i(ctx, op, 0) ||
2047         fold_xi_to_x(ctx, op, 1)) {
2048         return true;
2049     }
2050     return finish_folding(ctx, op);
2051 }
2052 
2053 static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
2054 {
2055     if (fold_const2_commutative(ctx, op) ||
2056         fold_xi_to_i(ctx, op, 0)) {
2057         return true;
2058     }
2059     return finish_folding(ctx, op);
2060 }
2061 
2062 static bool fold_multiply2(OptContext *ctx, TCGOp *op)
2063 {
2064     swap_commutative(op->args[0], &op->args[2], &op->args[3]);
2065 
2066     if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
2067         uint64_t a = arg_info(op->args[2])->val;
2068         uint64_t b = arg_info(op->args[3])->val;
2069         uint64_t h, l;
2070         TCGArg rl, rh;
2071         TCGOp *op2;
2072 
2073         switch (op->opc) {
2074         case INDEX_op_mulu2_i32:
2075             l = (uint64_t)(uint32_t)a * (uint32_t)b;
2076             h = (int32_t)(l >> 32);
2077             l = (int32_t)l;
2078             break;
2079         case INDEX_op_muls2_i32:
2080             l = (int64_t)(int32_t)a * (int32_t)b;
2081             h = l >> 32;
2082             l = (int32_t)l;
2083             break;
2084         case INDEX_op_mulu2_i64:
2085             mulu64(&l, &h, a, b);
2086             break;
2087         case INDEX_op_muls2_i64:
2088             muls64(&l, &h, a, b);
2089             break;
2090         default:
2091             g_assert_not_reached();
2092         }
2093 
2094         rl = op->args[0];
2095         rh = op->args[1];
2096 
2097         /* The proper opcode is supplied by tcg_opt_gen_mov. */
2098         op2 = opt_insert_before(ctx, op, 0, 2);
2099 
2100         tcg_opt_gen_movi(ctx, op, rl, l);
2101         tcg_opt_gen_movi(ctx, op2, rh, h);
2102         return true;
2103     }
2104     return finish_folding(ctx, op);
2105 }
2106 
2107 static bool fold_nand(OptContext *ctx, TCGOp *op)
2108 {
2109     uint64_t s_mask;
2110 
2111     if (fold_const2_commutative(ctx, op) ||
2112         fold_xi_to_not(ctx, op, -1)) {
2113         return true;
2114     }
2115 
2116     s_mask = arg_info(op->args[1])->s_mask
2117            & arg_info(op->args[2])->s_mask;
2118     return fold_masks_s(ctx, op, s_mask);
2119 }
2120 
2121 static bool fold_neg_no_const(OptContext *ctx, TCGOp *op)
2122 {
2123     /* Set to 1 all bits to the left of the rightmost.  */
2124     uint64_t z_mask = arg_info(op->args[1])->z_mask;
2125     z_mask = -(z_mask & -z_mask);
2126 
2127     return fold_masks_z(ctx, op, z_mask);
2128 }
2129 
2130 static bool fold_neg(OptContext *ctx, TCGOp *op)
2131 {
2132     return fold_const1(ctx, op) || fold_neg_no_const(ctx, op);
2133 }
2134 
2135 static bool fold_nor(OptContext *ctx, TCGOp *op)
2136 {
2137     uint64_t s_mask;
2138 
2139     if (fold_const2_commutative(ctx, op) ||
2140         fold_xi_to_not(ctx, op, 0)) {
2141         return true;
2142     }
2143 
2144     s_mask = arg_info(op->args[1])->s_mask
2145            & arg_info(op->args[2])->s_mask;
2146     return fold_masks_s(ctx, op, s_mask);
2147 }
2148 
2149 static bool fold_not(OptContext *ctx, TCGOp *op)
2150 {
2151     if (fold_const1(ctx, op)) {
2152         return true;
2153     }
2154     return fold_masks_s(ctx, op, arg_info(op->args[1])->s_mask);
2155 }
2156 
2157 static bool fold_or(OptContext *ctx, TCGOp *op)
2158 {
2159     uint64_t z_mask, s_mask;
2160     TempOptInfo *t1, *t2;
2161 
2162     if (fold_const2_commutative(ctx, op) ||
2163         fold_xi_to_x(ctx, op, 0) ||
2164         fold_xx_to_x(ctx, op)) {
2165         return true;
2166     }
2167 
2168     t1 = arg_info(op->args[1]);
2169     t2 = arg_info(op->args[2]);
2170     z_mask = t1->z_mask | t2->z_mask;
2171     s_mask = t1->s_mask & t2->s_mask;
2172     return fold_masks_zs(ctx, op, z_mask, s_mask);
2173 }
2174 
2175 static bool fold_orc(OptContext *ctx, TCGOp *op)
2176 {
2177     uint64_t s_mask;
2178     TempOptInfo *t1, *t2;
2179 
2180     if (fold_const2(ctx, op) ||
2181         fold_xx_to_i(ctx, op, -1) ||
2182         fold_xi_to_x(ctx, op, -1) ||
2183         fold_ix_to_not(ctx, op, 0)) {
2184         return true;
2185     }
2186 
2187     t2 = arg_info(op->args[2]);
2188     if (ti_is_const(t2)) {
2189         /* Fold orc r,x,i to or r,x,~i. */
2190         switch (ctx->type) {
2191         case TCG_TYPE_I32:
2192         case TCG_TYPE_I64:
2193             op->opc = INDEX_op_or;
2194             break;
2195         case TCG_TYPE_V64:
2196         case TCG_TYPE_V128:
2197         case TCG_TYPE_V256:
2198             op->opc = INDEX_op_or_vec;
2199             break;
2200         default:
2201             g_assert_not_reached();
2202         }
2203         op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2));
2204         return fold_or(ctx, op);
2205     }
2206 
2207     t1 = arg_info(op->args[1]);
2208     s_mask = t1->s_mask & t2->s_mask;
2209     return fold_masks_s(ctx, op, s_mask);
2210 }
2211 
2212 static bool fold_qemu_ld_1reg(OptContext *ctx, TCGOp *op)
2213 {
2214     const TCGOpDef *def = &tcg_op_defs[op->opc];
2215     MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
2216     MemOp mop = get_memop(oi);
2217     int width = 8 * memop_size(mop);
2218     uint64_t z_mask = -1, s_mask = 0;
2219 
2220     if (width < 64) {
2221         if (mop & MO_SIGN) {
2222             s_mask = MAKE_64BIT_MASK(width - 1, 64 - (width - 1));
2223         } else {
2224             z_mask = MAKE_64BIT_MASK(0, width);
2225         }
2226     }
2227 
2228     /* Opcodes that touch guest memory stop the mb optimization.  */
2229     ctx->prev_mb = NULL;
2230 
2231     return fold_masks_zs(ctx, op, z_mask, s_mask);
2232 }
2233 
2234 static bool fold_qemu_ld_2reg(OptContext *ctx, TCGOp *op)
2235 {
2236     /* Opcodes that touch guest memory stop the mb optimization.  */
2237     ctx->prev_mb = NULL;
2238     return finish_folding(ctx, op);
2239 }
2240 
2241 static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
2242 {
2243     /* Opcodes that touch guest memory stop the mb optimization.  */
2244     ctx->prev_mb = NULL;
2245     return true;
2246 }
2247 
2248 static bool fold_remainder(OptContext *ctx, TCGOp *op)
2249 {
2250     if (fold_const2(ctx, op) ||
2251         fold_xx_to_i(ctx, op, 0)) {
2252         return true;
2253     }
2254     return finish_folding(ctx, op);
2255 }
2256 
2257 /* Return 1 if finished, -1 if simplified, 0 if unchanged. */
2258 static int fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg)
2259 {
2260     uint64_t a_zmask, b_val;
2261     TCGCond cond;
2262 
2263     if (!arg_is_const(op->args[2])) {
2264         return false;
2265     }
2266 
2267     a_zmask = arg_info(op->args[1])->z_mask;
2268     b_val = arg_info(op->args[2])->val;
2269     cond = op->args[3];
2270 
2271     if (ctx->type == TCG_TYPE_I32) {
2272         a_zmask = (uint32_t)a_zmask;
2273         b_val = (uint32_t)b_val;
2274     }
2275 
2276     /*
2277      * A with only low bits set vs B with high bits set means that A < B.
2278      */
2279     if (a_zmask < b_val) {
2280         bool inv = false;
2281 
2282         switch (cond) {
2283         case TCG_COND_NE:
2284         case TCG_COND_LEU:
2285         case TCG_COND_LTU:
2286             inv = true;
2287             /* fall through */
2288         case TCG_COND_GTU:
2289         case TCG_COND_GEU:
2290         case TCG_COND_EQ:
2291             return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv);
2292         default:
2293             break;
2294         }
2295     }
2296 
2297     /*
2298      * A with only lsb set is already boolean.
2299      */
2300     if (a_zmask <= 1) {
2301         bool convert = false;
2302         bool inv = false;
2303 
2304         switch (cond) {
2305         case TCG_COND_EQ:
2306             inv = true;
2307             /* fall through */
2308         case TCG_COND_NE:
2309             convert = (b_val == 0);
2310             break;
2311         case TCG_COND_LTU:
2312         case TCG_COND_TSTEQ:
2313             inv = true;
2314             /* fall through */
2315         case TCG_COND_GEU:
2316         case TCG_COND_TSTNE:
2317             convert = (b_val == 1);
2318             break;
2319         default:
2320             break;
2321         }
2322         if (convert) {
2323             if (!inv && !neg) {
2324                 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
2325             }
2326 
2327             if (!inv) {
2328                 op->opc = INDEX_op_neg;
2329             } else if (neg) {
2330                 op->opc = INDEX_op_add;
2331                 op->args[2] = arg_new_constant(ctx, -1);
2332             } else {
2333                 op->opc = INDEX_op_xor;
2334                 op->args[2] = arg_new_constant(ctx, 1);
2335             }
2336             return -1;
2337         }
2338     }
2339     return 0;
2340 }
2341 
2342 static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg)
2343 {
2344     TCGOpcode shr_opc;
2345     TCGOpcode uext_opc = 0, sext_opc = 0;
2346     TCGCond cond = op->args[3];
2347     TCGArg ret, src1, src2;
2348     TCGOp *op2;
2349     uint64_t val;
2350     int sh;
2351     bool inv;
2352 
2353     if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) {
2354         return;
2355     }
2356 
2357     src2 = op->args[2];
2358     val = arg_info(src2)->val;
2359     if (!is_power_of_2(val)) {
2360         return;
2361     }
2362     sh = ctz64(val);
2363 
2364     switch (ctx->type) {
2365     case TCG_TYPE_I32:
2366         shr_opc = INDEX_op_shr_i32;
2367         if (TCG_TARGET_extract_valid(TCG_TYPE_I32, sh, 1)) {
2368             uext_opc = INDEX_op_extract_i32;
2369         }
2370         if (TCG_TARGET_sextract_valid(TCG_TYPE_I32, sh, 1)) {
2371             sext_opc = INDEX_op_sextract_i32;
2372         }
2373         break;
2374     case TCG_TYPE_I64:
2375         shr_opc = INDEX_op_shr_i64;
2376         if (TCG_TARGET_extract_valid(TCG_TYPE_I64, sh, 1)) {
2377             uext_opc = INDEX_op_extract_i64;
2378         }
2379         if (TCG_TARGET_sextract_valid(TCG_TYPE_I64, sh, 1)) {
2380             sext_opc = INDEX_op_sextract_i64;
2381         }
2382         break;
2383     default:
2384         g_assert_not_reached();
2385     }
2386 
2387     ret = op->args[0];
2388     src1 = op->args[1];
2389     inv = cond == TCG_COND_TSTEQ;
2390 
2391     if (sh && sext_opc && neg && !inv) {
2392         op->opc = sext_opc;
2393         op->args[1] = src1;
2394         op->args[2] = sh;
2395         op->args[3] = 1;
2396         return;
2397     } else if (sh && uext_opc) {
2398         op->opc = uext_opc;
2399         op->args[1] = src1;
2400         op->args[2] = sh;
2401         op->args[3] = 1;
2402     } else {
2403         if (sh) {
2404             op2 = opt_insert_before(ctx, op, shr_opc, 3);
2405             op2->args[0] = ret;
2406             op2->args[1] = src1;
2407             op2->args[2] = arg_new_constant(ctx, sh);
2408             src1 = ret;
2409         }
2410         op->opc = INDEX_op_and;
2411         op->args[1] = src1;
2412         op->args[2] = arg_new_constant(ctx, 1);
2413     }
2414 
2415     if (neg && inv) {
2416         op2 = opt_insert_after(ctx, op, INDEX_op_add, 3);
2417         op2->args[0] = ret;
2418         op2->args[1] = ret;
2419         op2->args[2] = arg_new_constant(ctx, -1);
2420     } else if (inv) {
2421         op2 = opt_insert_after(ctx, op, INDEX_op_xor, 3);
2422         op2->args[0] = ret;
2423         op2->args[1] = ret;
2424         op2->args[2] = arg_new_constant(ctx, 1);
2425     } else if (neg) {
2426         op2 = opt_insert_after(ctx, op, INDEX_op_neg, 2);
2427         op2->args[0] = ret;
2428         op2->args[1] = ret;
2429     }
2430 }
2431 
2432 static bool fold_setcond(OptContext *ctx, TCGOp *op)
2433 {
2434     int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
2435                                       &op->args[2], &op->args[3]);
2436     if (i >= 0) {
2437         return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2438     }
2439 
2440     i = fold_setcond_zmask(ctx, op, false);
2441     if (i > 0) {
2442         return true;
2443     }
2444     if (i == 0) {
2445         fold_setcond_tst_pow2(ctx, op, false);
2446     }
2447 
2448     return fold_masks_z(ctx, op, 1);
2449 }
2450 
2451 static bool fold_negsetcond(OptContext *ctx, TCGOp *op)
2452 {
2453     int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
2454                                       &op->args[2], &op->args[3]);
2455     if (i >= 0) {
2456         return tcg_opt_gen_movi(ctx, op, op->args[0], -i);
2457     }
2458 
2459     i = fold_setcond_zmask(ctx, op, true);
2460     if (i > 0) {
2461         return true;
2462     }
2463     if (i == 0) {
2464         fold_setcond_tst_pow2(ctx, op, true);
2465     }
2466 
2467     /* Value is {0,-1} so all bits are repetitions of the sign. */
2468     return fold_masks_s(ctx, op, -1);
2469 }
2470 
2471 static bool fold_setcond2(OptContext *ctx, TCGOp *op)
2472 {
2473     TCGCond cond;
2474     int i, inv = 0;
2475 
2476     i = do_constant_folding_cond2(ctx, op, &op->args[1]);
2477     cond = op->args[5];
2478     if (i >= 0) {
2479         goto do_setcond_const;
2480     }
2481 
2482     switch (cond) {
2483     case TCG_COND_LT:
2484     case TCG_COND_GE:
2485         /*
2486          * Simplify LT/GE comparisons vs zero to a single compare
2487          * vs the high word of the input.
2488          */
2489         if (arg_is_const_val(op->args[3], 0) &&
2490             arg_is_const_val(op->args[4], 0)) {
2491             goto do_setcond_high;
2492         }
2493         break;
2494 
2495     case TCG_COND_NE:
2496         inv = 1;
2497         QEMU_FALLTHROUGH;
2498     case TCG_COND_EQ:
2499         /*
2500          * Simplify EQ/NE comparisons where one of the pairs
2501          * can be simplified.
2502          */
2503         i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
2504                                      op->args[3], cond);
2505         switch (i ^ inv) {
2506         case 0:
2507             goto do_setcond_const;
2508         case 1:
2509             goto do_setcond_high;
2510         }
2511 
2512         i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
2513                                      op->args[4], cond);
2514         switch (i ^ inv) {
2515         case 0:
2516             goto do_setcond_const;
2517         case 1:
2518             goto do_setcond_low;
2519         }
2520         break;
2521 
2522     case TCG_COND_TSTEQ:
2523     case TCG_COND_TSTNE:
2524         if (arg_is_const_val(op->args[3], 0)) {
2525             goto do_setcond_high;
2526         }
2527         if (arg_is_const_val(op->args[4], 0)) {
2528             goto do_setcond_low;
2529         }
2530         break;
2531 
2532     default:
2533         break;
2534 
2535     do_setcond_low:
2536         op->args[2] = op->args[3];
2537         op->args[3] = cond;
2538         op->opc = INDEX_op_setcond_i32;
2539         return fold_setcond(ctx, op);
2540 
2541     do_setcond_high:
2542         op->args[1] = op->args[2];
2543         op->args[2] = op->args[4];
2544         op->args[3] = cond;
2545         op->opc = INDEX_op_setcond_i32;
2546         return fold_setcond(ctx, op);
2547     }
2548 
2549     return fold_masks_z(ctx, op, 1);
2550 
2551  do_setcond_const:
2552     return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2553 }
2554 
2555 static bool fold_sextract(OptContext *ctx, TCGOp *op)
2556 {
2557     uint64_t z_mask, s_mask, s_mask_old;
2558     TempOptInfo *t1 = arg_info(op->args[1]);
2559     int pos = op->args[2];
2560     int len = op->args[3];
2561 
2562     if (ti_is_const(t1)) {
2563         return tcg_opt_gen_movi(ctx, op, op->args[0],
2564                                 sextract64(ti_const_val(t1), pos, len));
2565     }
2566 
2567     s_mask_old = t1->s_mask;
2568     s_mask = s_mask_old >> pos;
2569     s_mask |= -1ull << (len - 1);
2570 
2571     if (pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) {
2572         return true;
2573     }
2574 
2575     z_mask = sextract64(t1->z_mask, pos, len);
2576     return fold_masks_zs(ctx, op, z_mask, s_mask);
2577 }
2578 
2579 static bool fold_shift(OptContext *ctx, TCGOp *op)
2580 {
2581     uint64_t s_mask, z_mask;
2582     TempOptInfo *t1, *t2;
2583 
2584     if (fold_const2(ctx, op) ||
2585         fold_ix_to_i(ctx, op, 0) ||
2586         fold_xi_to_x(ctx, op, 0)) {
2587         return true;
2588     }
2589 
2590     t1 = arg_info(op->args[1]);
2591     t2 = arg_info(op->args[2]);
2592     s_mask = t1->s_mask;
2593     z_mask = t1->z_mask;
2594 
2595     if (ti_is_const(t2)) {
2596         int sh = ti_const_val(t2);
2597 
2598         z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
2599         s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
2600 
2601         return fold_masks_zs(ctx, op, z_mask, s_mask);
2602     }
2603 
2604     switch (op->opc) {
2605     CASE_OP_32_64(sar):
2606         /*
2607          * Arithmetic right shift will not reduce the number of
2608          * input sign repetitions.
2609          */
2610         return fold_masks_s(ctx, op, s_mask);
2611     CASE_OP_32_64(shr):
2612         /*
2613          * If the sign bit is known zero, then logical right shift
2614          * will not reduce the number of input sign repetitions.
2615          */
2616         if (~z_mask & -s_mask) {
2617             return fold_masks_s(ctx, op, s_mask);
2618         }
2619         break;
2620     default:
2621         break;
2622     }
2623 
2624     return finish_folding(ctx, op);
2625 }
2626 
2627 static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
2628 {
2629     TCGOpcode neg_op;
2630     bool have_neg;
2631 
2632     if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
2633         return false;
2634     }
2635 
2636     switch (ctx->type) {
2637     case TCG_TYPE_I32:
2638     case TCG_TYPE_I64:
2639         neg_op = INDEX_op_neg;
2640         have_neg = true;
2641         break;
2642     case TCG_TYPE_V64:
2643     case TCG_TYPE_V128:
2644     case TCG_TYPE_V256:
2645         neg_op = INDEX_op_neg_vec;
2646         have_neg = (TCG_TARGET_HAS_neg_vec &&
2647                     tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
2648         break;
2649     default:
2650         g_assert_not_reached();
2651     }
2652     if (have_neg) {
2653         op->opc = neg_op;
2654         op->args[1] = op->args[2];
2655         return fold_neg_no_const(ctx, op);
2656     }
2657     return false;
2658 }
2659 
2660 /* We cannot as yet do_constant_folding with vectors. */
2661 static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
2662 {
2663     if (fold_xx_to_i(ctx, op, 0) ||
2664         fold_xi_to_x(ctx, op, 0) ||
2665         fold_sub_to_neg(ctx, op)) {
2666         return true;
2667     }
2668     return finish_folding(ctx, op);
2669 }
2670 
2671 static bool fold_sub(OptContext *ctx, TCGOp *op)
2672 {
2673     if (fold_const2(ctx, op) ||
2674         fold_xx_to_i(ctx, op, 0) ||
2675         fold_xi_to_x(ctx, op, 0) ||
2676         fold_sub_to_neg(ctx, op)) {
2677         return true;
2678     }
2679 
2680     /* Fold sub r,x,i to add r,x,-i */
2681     if (arg_is_const(op->args[2])) {
2682         uint64_t val = arg_info(op->args[2])->val;
2683 
2684         op->opc = INDEX_op_add;
2685         op->args[2] = arg_new_constant(ctx, -val);
2686     }
2687     return finish_folding(ctx, op);
2688 }
2689 
2690 static bool fold_sub2(OptContext *ctx, TCGOp *op)
2691 {
2692     return fold_addsub2(ctx, op, false);
2693 }
2694 
2695 static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
2696 {
2697     uint64_t z_mask = -1, s_mask = 0;
2698 
2699     /* We can't do any folding with a load, but we can record bits. */
2700     switch (op->opc) {
2701     CASE_OP_32_64(ld8s):
2702         s_mask = INT8_MIN;
2703         break;
2704     CASE_OP_32_64(ld8u):
2705         z_mask = MAKE_64BIT_MASK(0, 8);
2706         break;
2707     CASE_OP_32_64(ld16s):
2708         s_mask = INT16_MIN;
2709         break;
2710     CASE_OP_32_64(ld16u):
2711         z_mask = MAKE_64BIT_MASK(0, 16);
2712         break;
2713     case INDEX_op_ld32s_i64:
2714         s_mask = INT32_MIN;
2715         break;
2716     case INDEX_op_ld32u_i64:
2717         z_mask = MAKE_64BIT_MASK(0, 32);
2718         break;
2719     default:
2720         g_assert_not_reached();
2721     }
2722     return fold_masks_zs(ctx, op, z_mask, s_mask);
2723 }
2724 
2725 static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op)
2726 {
2727     TCGTemp *dst, *src;
2728     intptr_t ofs;
2729     TCGType type;
2730 
2731     if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2732         return finish_folding(ctx, op);
2733     }
2734 
2735     type = ctx->type;
2736     ofs = op->args[2];
2737     dst = arg_temp(op->args[0]);
2738     src = find_mem_copy_for(ctx, type, ofs);
2739     if (src && src->base_type == type) {
2740         return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src));
2741     }
2742 
2743     reset_ts(ctx, dst);
2744     record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1);
2745     return true;
2746 }
2747 
2748 static bool fold_tcg_st(OptContext *ctx, TCGOp *op)
2749 {
2750     intptr_t ofs = op->args[2];
2751     intptr_t lm1;
2752 
2753     if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2754         remove_mem_copy_all(ctx);
2755         return true;
2756     }
2757 
2758     switch (op->opc) {
2759     CASE_OP_32_64(st8):
2760         lm1 = 0;
2761         break;
2762     CASE_OP_32_64(st16):
2763         lm1 = 1;
2764         break;
2765     case INDEX_op_st32_i64:
2766     case INDEX_op_st_i32:
2767         lm1 = 3;
2768         break;
2769     case INDEX_op_st_i64:
2770         lm1 = 7;
2771         break;
2772     case INDEX_op_st_vec:
2773         lm1 = tcg_type_size(ctx->type) - 1;
2774         break;
2775     default:
2776         g_assert_not_reached();
2777     }
2778     remove_mem_copy_in(ctx, ofs, ofs + lm1);
2779     return true;
2780 }
2781 
2782 static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op)
2783 {
2784     TCGTemp *src;
2785     intptr_t ofs, last;
2786     TCGType type;
2787 
2788     if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2789         return fold_tcg_st(ctx, op);
2790     }
2791 
2792     src = arg_temp(op->args[0]);
2793     ofs = op->args[2];
2794     type = ctx->type;
2795 
2796     /*
2797      * Eliminate duplicate stores of a constant.
2798      * This happens frequently when the target ISA zero-extends.
2799      */
2800     if (ts_is_const(src)) {
2801         TCGTemp *prev = find_mem_copy_for(ctx, type, ofs);
2802         if (src == prev) {
2803             tcg_op_remove(ctx->tcg, op);
2804             return true;
2805         }
2806     }
2807 
2808     last = ofs + tcg_type_size(type) - 1;
2809     remove_mem_copy_in(ctx, ofs, last);
2810     record_mem_copy(ctx, type, src, ofs, last);
2811     return true;
2812 }
2813 
2814 static bool fold_xor(OptContext *ctx, TCGOp *op)
2815 {
2816     uint64_t z_mask, s_mask;
2817     TempOptInfo *t1, *t2;
2818 
2819     if (fold_const2_commutative(ctx, op) ||
2820         fold_xx_to_i(ctx, op, 0) ||
2821         fold_xi_to_x(ctx, op, 0) ||
2822         fold_xi_to_not(ctx, op, -1)) {
2823         return true;
2824     }
2825 
2826     t1 = arg_info(op->args[1]);
2827     t2 = arg_info(op->args[2]);
2828     z_mask = t1->z_mask | t2->z_mask;
2829     s_mask = t1->s_mask & t2->s_mask;
2830     return fold_masks_zs(ctx, op, z_mask, s_mask);
2831 }
2832 
2833 /* Propagate constants and copies, fold constant expressions. */
2834 void tcg_optimize(TCGContext *s)
2835 {
2836     int nb_temps, i;
2837     TCGOp *op, *op_next;
2838     OptContext ctx = { .tcg = s };
2839 
2840     QSIMPLEQ_INIT(&ctx.mem_free);
2841 
2842     /* Array VALS has an element for each temp.
2843        If this temp holds a constant then its value is kept in VALS' element.
2844        If this temp is a copy of other ones then the other copies are
2845        available through the doubly linked circular list. */
2846 
2847     nb_temps = s->nb_temps;
2848     for (i = 0; i < nb_temps; ++i) {
2849         s->temps[i].state_ptr = NULL;
2850     }
2851 
2852     QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2853         TCGOpcode opc = op->opc;
2854         const TCGOpDef *def;
2855         bool done = false;
2856 
2857         /* Calls are special. */
2858         if (opc == INDEX_op_call) {
2859             fold_call(&ctx, op);
2860             continue;
2861         }
2862 
2863         def = &tcg_op_defs[opc];
2864         init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
2865         copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
2866 
2867         /* Pre-compute the type of the operation. */
2868         ctx.type = TCGOP_TYPE(op);
2869 
2870         /*
2871          * Process each opcode.
2872          * Sorted alphabetically by opcode as much as possible.
2873          */
2874         switch (opc) {
2875         case INDEX_op_add:
2876             done = fold_add(&ctx, op);
2877             break;
2878         case INDEX_op_add_vec:
2879             done = fold_add_vec(&ctx, op);
2880             break;
2881         CASE_OP_32_64(add2):
2882             done = fold_add2(&ctx, op);
2883             break;
2884         case INDEX_op_and:
2885         case INDEX_op_and_vec:
2886             done = fold_and(&ctx, op);
2887             break;
2888         case INDEX_op_andc:
2889         case INDEX_op_andc_vec:
2890             done = fold_andc(&ctx, op);
2891             break;
2892         CASE_OP_32_64(brcond):
2893             done = fold_brcond(&ctx, op);
2894             break;
2895         case INDEX_op_brcond2_i32:
2896             done = fold_brcond2(&ctx, op);
2897             break;
2898         CASE_OP_32_64(bswap16):
2899         CASE_OP_32_64(bswap32):
2900         case INDEX_op_bswap64_i64:
2901             done = fold_bswap(&ctx, op);
2902             break;
2903         CASE_OP_32_64(clz):
2904         CASE_OP_32_64(ctz):
2905             done = fold_count_zeros(&ctx, op);
2906             break;
2907         CASE_OP_32_64(ctpop):
2908             done = fold_ctpop(&ctx, op);
2909             break;
2910         CASE_OP_32_64(deposit):
2911             done = fold_deposit(&ctx, op);
2912             break;
2913         case INDEX_op_divs:
2914         case INDEX_op_divu:
2915             done = fold_divide(&ctx, op);
2916             break;
2917         case INDEX_op_dup_vec:
2918             done = fold_dup(&ctx, op);
2919             break;
2920         case INDEX_op_dup2_vec:
2921             done = fold_dup2(&ctx, op);
2922             break;
2923         case INDEX_op_eqv:
2924         case INDEX_op_eqv_vec:
2925             done = fold_eqv(&ctx, op);
2926             break;
2927         CASE_OP_32_64(extract):
2928             done = fold_extract(&ctx, op);
2929             break;
2930         CASE_OP_32_64(extract2):
2931             done = fold_extract2(&ctx, op);
2932             break;
2933         case INDEX_op_ext_i32_i64:
2934             done = fold_exts(&ctx, op);
2935             break;
2936         case INDEX_op_extu_i32_i64:
2937         case INDEX_op_extrl_i64_i32:
2938         case INDEX_op_extrh_i64_i32:
2939             done = fold_extu(&ctx, op);
2940             break;
2941         CASE_OP_32_64(ld8s):
2942         CASE_OP_32_64(ld8u):
2943         CASE_OP_32_64(ld16s):
2944         CASE_OP_32_64(ld16u):
2945         case INDEX_op_ld32s_i64:
2946         case INDEX_op_ld32u_i64:
2947             done = fold_tcg_ld(&ctx, op);
2948             break;
2949         case INDEX_op_ld_i32:
2950         case INDEX_op_ld_i64:
2951         case INDEX_op_ld_vec:
2952             done = fold_tcg_ld_memcopy(&ctx, op);
2953             break;
2954         CASE_OP_32_64(st8):
2955         CASE_OP_32_64(st16):
2956         case INDEX_op_st32_i64:
2957             done = fold_tcg_st(&ctx, op);
2958             break;
2959         case INDEX_op_st_i32:
2960         case INDEX_op_st_i64:
2961         case INDEX_op_st_vec:
2962             done = fold_tcg_st_memcopy(&ctx, op);
2963             break;
2964         case INDEX_op_mb:
2965             done = fold_mb(&ctx, op);
2966             break;
2967         case INDEX_op_mov:
2968         case INDEX_op_mov_vec:
2969             done = fold_mov(&ctx, op);
2970             break;
2971         CASE_OP_32_64(movcond):
2972             done = fold_movcond(&ctx, op);
2973             break;
2974         case INDEX_op_mul:
2975             done = fold_mul(&ctx, op);
2976             break;
2977         case INDEX_op_mulsh:
2978         case INDEX_op_muluh:
2979             done = fold_mul_highpart(&ctx, op);
2980             break;
2981         CASE_OP_32_64(muls2):
2982         CASE_OP_32_64(mulu2):
2983             done = fold_multiply2(&ctx, op);
2984             break;
2985         case INDEX_op_nand:
2986         case INDEX_op_nand_vec:
2987             done = fold_nand(&ctx, op);
2988             break;
2989         case INDEX_op_neg:
2990             done = fold_neg(&ctx, op);
2991             break;
2992         case INDEX_op_nor:
2993         case INDEX_op_nor_vec:
2994             done = fold_nor(&ctx, op);
2995             break;
2996         case INDEX_op_not:
2997         case INDEX_op_not_vec:
2998             done = fold_not(&ctx, op);
2999             break;
3000         case INDEX_op_or:
3001         case INDEX_op_or_vec:
3002             done = fold_or(&ctx, op);
3003             break;
3004         case INDEX_op_orc:
3005         case INDEX_op_orc_vec:
3006             done = fold_orc(&ctx, op);
3007             break;
3008         case INDEX_op_qemu_ld_i32:
3009             done = fold_qemu_ld_1reg(&ctx, op);
3010             break;
3011         case INDEX_op_qemu_ld_i64:
3012             if (TCG_TARGET_REG_BITS == 64) {
3013                 done = fold_qemu_ld_1reg(&ctx, op);
3014                 break;
3015             }
3016             QEMU_FALLTHROUGH;
3017         case INDEX_op_qemu_ld_i128:
3018             done = fold_qemu_ld_2reg(&ctx, op);
3019             break;
3020         case INDEX_op_qemu_st8_i32:
3021         case INDEX_op_qemu_st_i32:
3022         case INDEX_op_qemu_st_i64:
3023         case INDEX_op_qemu_st_i128:
3024             done = fold_qemu_st(&ctx, op);
3025             break;
3026         case INDEX_op_rems:
3027         CASE_OP_32_64(remu):
3028             done = fold_remainder(&ctx, op);
3029             break;
3030         CASE_OP_32_64(rotl):
3031         CASE_OP_32_64(rotr):
3032         CASE_OP_32_64(sar):
3033         CASE_OP_32_64(shl):
3034         CASE_OP_32_64(shr):
3035             done = fold_shift(&ctx, op);
3036             break;
3037         CASE_OP_32_64(setcond):
3038             done = fold_setcond(&ctx, op);
3039             break;
3040         CASE_OP_32_64(negsetcond):
3041             done = fold_negsetcond(&ctx, op);
3042             break;
3043         case INDEX_op_setcond2_i32:
3044             done = fold_setcond2(&ctx, op);
3045             break;
3046         case INDEX_op_cmp_vec:
3047             done = fold_cmp_vec(&ctx, op);
3048             break;
3049         case INDEX_op_cmpsel_vec:
3050             done = fold_cmpsel_vec(&ctx, op);
3051             break;
3052         case INDEX_op_bitsel_vec:
3053             done = fold_bitsel_vec(&ctx, op);
3054             break;
3055         CASE_OP_32_64(sextract):
3056             done = fold_sextract(&ctx, op);
3057             break;
3058         case INDEX_op_sub:
3059             done = fold_sub(&ctx, op);
3060             break;
3061         case INDEX_op_sub_vec:
3062             done = fold_sub_vec(&ctx, op);
3063             break;
3064         CASE_OP_32_64(sub2):
3065             done = fold_sub2(&ctx, op);
3066             break;
3067         case INDEX_op_xor:
3068         case INDEX_op_xor_vec:
3069             done = fold_xor(&ctx, op);
3070             break;
3071         case INDEX_op_set_label:
3072         case INDEX_op_br:
3073         case INDEX_op_exit_tb:
3074         case INDEX_op_goto_tb:
3075         case INDEX_op_goto_ptr:
3076             finish_ebb(&ctx);
3077             done = true;
3078             break;
3079         default:
3080             done = finish_folding(&ctx, op);
3081             break;
3082         }
3083         tcg_debug_assert(done);
3084     }
3085 }
3086