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