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