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