blob: e84d10be53bf3572f5eaa56c33b4015c50a23dbe [file] [log] [blame]
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001/*
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
Peter Maydell757e7252016-01-26 18:17:08 +000026#include "qemu/osdep.h"
Philippe Mathieu-Daudédcb32f12020-01-01 12:23:00 +010027#include "tcg/tcg-op.h"
Richard Henderson90163902021-03-18 10:21:45 -060028#include "tcg-internal.h"
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040029
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040030#define CASE_OP_32_64(x) \
31 glue(glue(case INDEX_op_, x), _i32): \
32 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040033
Richard Henderson170ba882017-11-22 09:07:11 +010034#define CASE_OP_32_64_VEC(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64): \
37 glue(glue(case INDEX_op_, x), _vec)
38
Richard Henderson6fcb98e2020-03-30 17:44:30 -070039typedef struct TempOptInfo {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020040 bool is_const;
Richard Henderson63490392017-06-20 13:43:15 -070041 TCGTemp *prev_copy;
42 TCGTemp *next_copy;
Richard Henderson54795542020-09-06 16:21:32 -070043 uint64_t val;
Richard Hendersonb1fde412021-08-23 13:07:49 -070044 uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */
Richard Henderson6fcb98e2020-03-30 17:44:30 -070045} TempOptInfo;
Kirill Batuzov22613af2011-07-07 16:37:13 +040046
Richard Henderson3b3f8472021-08-23 22:06:31 -070047typedef struct OptContext {
Richard Hendersondc849882021-08-24 07:13:45 -070048 TCGContext *tcg;
Richard Hendersond0ed5152021-08-24 07:38:39 -070049 TCGOp *prev_mb;
Richard Henderson3b3f8472021-08-23 22:06:31 -070050 TCGTempSet temps_used;
Richard Henderson137f1f42021-08-24 08:49:25 -070051
52 /* In flight values from optimization. */
Richard Hendersonfae450b2021-08-25 22:42:19 -070053 uint64_t a_mask; /* mask bit is 0 iff value identical to first input */
54 uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */
Richard Henderson67f84c92021-08-25 08:00:20 -070055 TCGType type;
Richard Henderson3b3f8472021-08-23 22:06:31 -070056} OptContext;
57
Richard Henderson6fcb98e2020-03-30 17:44:30 -070058static inline TempOptInfo *ts_info(TCGTemp *ts)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020059{
Richard Henderson63490392017-06-20 13:43:15 -070060 return ts->state_ptr;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020061}
62
Richard Henderson6fcb98e2020-03-30 17:44:30 -070063static inline TempOptInfo *arg_info(TCGArg arg)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020064{
Richard Henderson63490392017-06-20 13:43:15 -070065 return ts_info(arg_temp(arg));
66}
67
68static inline bool ts_is_const(TCGTemp *ts)
69{
70 return ts_info(ts)->is_const;
71}
72
73static inline bool arg_is_const(TCGArg arg)
74{
75 return ts_is_const(arg_temp(arg));
76}
77
78static inline bool ts_is_copy(TCGTemp *ts)
79{
80 return ts_info(ts)->next_copy != ts;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020081}
82
Aurelien Jarnob41059d2015-07-27 12:41:44 +020083/* Reset TEMP's state, possibly removing the temp for the list of copies. */
Richard Henderson63490392017-06-20 13:43:15 -070084static void reset_ts(TCGTemp *ts)
Kirill Batuzov22613af2011-07-07 16:37:13 +040085{
Richard Henderson6fcb98e2020-03-30 17:44:30 -070086 TempOptInfo *ti = ts_info(ts);
87 TempOptInfo *pi = ts_info(ti->prev_copy);
88 TempOptInfo *ni = ts_info(ti->next_copy);
Richard Henderson63490392017-06-20 13:43:15 -070089
90 ni->prev_copy = ti->prev_copy;
91 pi->next_copy = ti->next_copy;
92 ti->next_copy = ts;
93 ti->prev_copy = ts;
94 ti->is_const = false;
Richard Hendersonb1fde412021-08-23 13:07:49 -070095 ti->z_mask = -1;
Richard Henderson63490392017-06-20 13:43:15 -070096}
97
98static void reset_temp(TCGArg arg)
99{
100 reset_ts(arg_temp(arg));
Kirill Batuzov22613af2011-07-07 16:37:13 +0400101}
102
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200103/* Initialize and activate a temporary. */
Richard Henderson3b3f8472021-08-23 22:06:31 -0700104static void init_ts_info(OptContext *ctx, TCGTemp *ts)
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200105{
Richard Henderson63490392017-06-20 13:43:15 -0700106 size_t idx = temp_idx(ts);
Richard Henderson8f17a972020-03-30 19:52:02 -0700107 TempOptInfo *ti;
Richard Henderson63490392017-06-20 13:43:15 -0700108
Richard Henderson3b3f8472021-08-23 22:06:31 -0700109 if (test_bit(idx, ctx->temps_used.l)) {
Richard Henderson8f17a972020-03-30 19:52:02 -0700110 return;
111 }
Richard Henderson3b3f8472021-08-23 22:06:31 -0700112 set_bit(idx, ctx->temps_used.l);
Richard Henderson8f17a972020-03-30 19:52:02 -0700113
114 ti = ts->state_ptr;
115 if (ti == NULL) {
116 ti = tcg_malloc(sizeof(TempOptInfo));
Richard Henderson63490392017-06-20 13:43:15 -0700117 ts->state_ptr = ti;
Richard Henderson8f17a972020-03-30 19:52:02 -0700118 }
119
120 ti->next_copy = ts;
121 ti->prev_copy = ts;
122 if (ts->kind == TEMP_CONST) {
123 ti->is_const = true;
124 ti->val = ts->val;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700125 ti->z_mask = ts->val;
Richard Henderson8f17a972020-03-30 19:52:02 -0700126 if (TCG_TARGET_REG_BITS > 32 && ts->type == TCG_TYPE_I32) {
127 /* High bits of a 32-bit quantity are garbage. */
Richard Hendersonb1fde412021-08-23 13:07:49 -0700128 ti->z_mask |= ~0xffffffffull;
Richard Hendersonc0522132020-03-29 18:55:52 -0700129 }
Richard Henderson8f17a972020-03-30 19:52:02 -0700130 } else {
131 ti->is_const = false;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700132 ti->z_mask = -1;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200133 }
134}
135
Richard Henderson63490392017-06-20 13:43:15 -0700136static TCGTemp *find_better_copy(TCGContext *s, TCGTemp *ts)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200137{
Richard Henderson4c868ce2020-04-23 09:02:23 -0700138 TCGTemp *i, *g, *l;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200139
Richard Henderson4c868ce2020-04-23 09:02:23 -0700140 /* If this is already readonly, we can't do better. */
141 if (temp_readonly(ts)) {
Richard Henderson63490392017-06-20 13:43:15 -0700142 return ts;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200143 }
144
Richard Henderson4c868ce2020-04-23 09:02:23 -0700145 g = l = NULL;
Richard Henderson63490392017-06-20 13:43:15 -0700146 for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
Richard Henderson4c868ce2020-04-23 09:02:23 -0700147 if (temp_readonly(i)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200148 return i;
Richard Henderson4c868ce2020-04-23 09:02:23 -0700149 } else if (i->kind > ts->kind) {
150 if (i->kind == TEMP_GLOBAL) {
151 g = i;
152 } else if (i->kind == TEMP_LOCAL) {
153 l = i;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200154 }
155 }
156 }
157
Richard Henderson4c868ce2020-04-23 09:02:23 -0700158 /* If we didn't find a better representation, return the same temp. */
159 return g ? g : l ? l : ts;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200160}
161
Richard Henderson63490392017-06-20 13:43:15 -0700162static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200163{
Richard Henderson63490392017-06-20 13:43:15 -0700164 TCGTemp *i;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200165
Richard Henderson63490392017-06-20 13:43:15 -0700166 if (ts1 == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200167 return true;
168 }
169
Richard Henderson63490392017-06-20 13:43:15 -0700170 if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200171 return false;
172 }
173
Richard Henderson63490392017-06-20 13:43:15 -0700174 for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
175 if (i == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200176 return true;
177 }
178 }
179
180 return false;
181}
182
Richard Henderson63490392017-06-20 13:43:15 -0700183static bool args_are_copies(TCGArg arg1, TCGArg arg2)
184{
185 return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
186}
187
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700188static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400189{
Richard Henderson63490392017-06-20 13:43:15 -0700190 TCGTemp *dst_ts = arg_temp(dst);
191 TCGTemp *src_ts = arg_temp(src);
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700192 TempOptInfo *di;
193 TempOptInfo *si;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700194 uint64_t z_mask;
Richard Henderson63490392017-06-20 13:43:15 -0700195 TCGOpcode new_op;
196
197 if (ts_are_copies(dst_ts, src_ts)) {
Richard Hendersondc849882021-08-24 07:13:45 -0700198 tcg_op_remove(ctx->tcg, op);
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700199 return true;
Aurelien Jarno53657182015-06-04 21:53:25 +0200200 }
201
Richard Henderson63490392017-06-20 13:43:15 -0700202 reset_ts(dst_ts);
203 di = ts_info(dst_ts);
204 si = ts_info(src_ts);
Richard Henderson67f84c92021-08-25 08:00:20 -0700205
206 switch (ctx->type) {
207 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100208 new_op = INDEX_op_mov_i32;
Richard Henderson67f84c92021-08-25 08:00:20 -0700209 break;
210 case TCG_TYPE_I64:
211 new_op = INDEX_op_mov_i64;
212 break;
213 case TCG_TYPE_V64:
214 case TCG_TYPE_V128:
215 case TCG_TYPE_V256:
216 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
217 new_op = INDEX_op_mov_vec;
218 break;
219 default:
220 g_assert_not_reached();
Richard Henderson170ba882017-11-22 09:07:11 +0100221 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700222 op->opc = new_op;
Richard Henderson63490392017-06-20 13:43:15 -0700223 op->args[0] = dst;
224 op->args[1] = src;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700225
Richard Hendersonb1fde412021-08-23 13:07:49 -0700226 z_mask = si->z_mask;
Richard Henderson24666ba2014-05-22 11:14:10 -0700227 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
228 /* High bits of the destination are now garbage. */
Richard Hendersonb1fde412021-08-23 13:07:49 -0700229 z_mask |= ~0xffffffffull;
Richard Henderson24666ba2014-05-22 11:14:10 -0700230 }
Richard Hendersonb1fde412021-08-23 13:07:49 -0700231 di->z_mask = z_mask;
Richard Henderson24666ba2014-05-22 11:14:10 -0700232
Richard Henderson63490392017-06-20 13:43:15 -0700233 if (src_ts->type == dst_ts->type) {
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700234 TempOptInfo *ni = ts_info(si->next_copy);
Richard Henderson63490392017-06-20 13:43:15 -0700235
236 di->next_copy = si->next_copy;
237 di->prev_copy = src_ts;
238 ni->prev_copy = dst_ts;
239 si->next_copy = dst_ts;
240 di->is_const = si->is_const;
241 di->val = si->val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800242 }
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700243 return true;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400244}
245
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700246static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
Richard Hendersondc849882021-08-24 07:13:45 -0700247 TCGArg dst, uint64_t val)
Richard Henderson8fe35e02020-03-30 20:42:43 -0700248{
Richard Henderson8fe35e02020-03-30 20:42:43 -0700249 /* Convert movi to mov with constant temp. */
Richard Henderson67f84c92021-08-25 08:00:20 -0700250 TCGTemp *tv = tcg_constant_internal(ctx->type, val);
251
Richard Henderson3b3f8472021-08-23 22:06:31 -0700252 init_ts_info(ctx, tv);
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700253 return tcg_opt_gen_mov(ctx, op, dst, temp_arg(tv));
Richard Henderson8fe35e02020-03-30 20:42:43 -0700254}
255
Richard Henderson54795542020-09-06 16:21:32 -0700256static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400257{
Richard Henderson03271522013-08-14 14:35:56 -0700258 uint64_t l64, h64;
259
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400260 switch (op) {
261 CASE_OP_32_64(add):
262 return x + y;
263
264 CASE_OP_32_64(sub):
265 return x - y;
266
267 CASE_OP_32_64(mul):
268 return x * y;
269
Kirill Batuzov9a810902011-07-07 16:37:15 +0400270 CASE_OP_32_64(and):
271 return x & y;
272
273 CASE_OP_32_64(or):
274 return x | y;
275
276 CASE_OP_32_64(xor):
277 return x ^ y;
278
Kirill Batuzov55c09752011-07-07 16:37:16 +0400279 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700280 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400281
Kirill Batuzov55c09752011-07-07 16:37:16 +0400282 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700283 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400284
285 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700286 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400287
Kirill Batuzov55c09752011-07-07 16:37:16 +0400288 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700289 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400290
291 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700292 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400293
Kirill Batuzov55c09752011-07-07 16:37:16 +0400294 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700295 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400296
297 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700298 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400299
Kirill Batuzov55c09752011-07-07 16:37:16 +0400300 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700301 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400302
303 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700304 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400305
Kirill Batuzov55c09752011-07-07 16:37:16 +0400306 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700307 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400308
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700309 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400310 return ~x;
311
Richard Hendersoncb25c802011-08-17 14:11:47 -0700312 CASE_OP_32_64(neg):
313 return -x;
314
315 CASE_OP_32_64(andc):
316 return x & ~y;
317
318 CASE_OP_32_64(orc):
319 return x | ~y;
320
321 CASE_OP_32_64(eqv):
322 return ~(x ^ y);
323
324 CASE_OP_32_64(nand):
325 return ~(x & y);
326
327 CASE_OP_32_64(nor):
328 return ~(x | y);
329
Richard Henderson0e28d002016-11-16 09:23:28 +0100330 case INDEX_op_clz_i32:
331 return (uint32_t)x ? clz32(x) : y;
332
333 case INDEX_op_clz_i64:
334 return x ? clz64(x) : y;
335
336 case INDEX_op_ctz_i32:
337 return (uint32_t)x ? ctz32(x) : y;
338
339 case INDEX_op_ctz_i64:
340 return x ? ctz64(x) : y;
341
Richard Hendersona768e4e2016-11-21 11:13:39 +0100342 case INDEX_op_ctpop_i32:
343 return ctpop32(x);
344
345 case INDEX_op_ctpop_i64:
346 return ctpop64(x);
347
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700348 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400349 return (int8_t)x;
350
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700351 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400352 return (int16_t)x;
353
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700354 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400355 return (uint8_t)x;
356
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700357 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400358 return (uint16_t)x;
359
Richard Henderson64985942018-11-20 08:53:34 +0100360 CASE_OP_32_64(bswap16):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700361 x = bswap16(x);
362 return y & TCG_BSWAP_OS ? (int16_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100363
364 CASE_OP_32_64(bswap32):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700365 x = bswap32(x);
366 return y & TCG_BSWAP_OS ? (int32_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100367
368 case INDEX_op_bswap64_i64:
369 return bswap64(x);
370
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200371 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400372 case INDEX_op_ext32s_i64:
373 return (int32_t)x;
374
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200375 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700376 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400377 case INDEX_op_ext32u_i64:
378 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400379
Richard Henderson609ad702015-07-24 07:16:00 -0700380 case INDEX_op_extrh_i64_i32:
381 return (uint64_t)x >> 32;
382
Richard Henderson03271522013-08-14 14:35:56 -0700383 case INDEX_op_muluh_i32:
384 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
385 case INDEX_op_mulsh_i32:
386 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
387
388 case INDEX_op_muluh_i64:
389 mulu64(&l64, &h64, x, y);
390 return h64;
391 case INDEX_op_mulsh_i64:
392 muls64(&l64, &h64, x, y);
393 return h64;
394
Richard Henderson01547f72013-08-14 15:22:46 -0700395 case INDEX_op_div_i32:
396 /* Avoid crashing on divide by zero, otherwise undefined. */
397 return (int32_t)x / ((int32_t)y ? : 1);
398 case INDEX_op_divu_i32:
399 return (uint32_t)x / ((uint32_t)y ? : 1);
400 case INDEX_op_div_i64:
401 return (int64_t)x / ((int64_t)y ? : 1);
402 case INDEX_op_divu_i64:
403 return (uint64_t)x / ((uint64_t)y ? : 1);
404
405 case INDEX_op_rem_i32:
406 return (int32_t)x % ((int32_t)y ? : 1);
407 case INDEX_op_remu_i32:
408 return (uint32_t)x % ((uint32_t)y ? : 1);
409 case INDEX_op_rem_i64:
410 return (int64_t)x % ((int64_t)y ? : 1);
411 case INDEX_op_remu_i64:
412 return (uint64_t)x % ((uint64_t)y ? : 1);
413
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400414 default:
415 fprintf(stderr,
416 "Unrecognized operation %d in do_constant_folding.\n", op);
417 tcg_abort();
418 }
419}
420
Richard Henderson67f84c92021-08-25 08:00:20 -0700421static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
422 uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400423{
Richard Henderson54795542020-09-06 16:21:32 -0700424 uint64_t res = do_constant_folding_2(op, x, y);
Richard Henderson67f84c92021-08-25 08:00:20 -0700425 if (type == TCG_TYPE_I32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200426 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400427 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400428 return res;
429}
430
Richard Henderson9519da72012-10-02 11:32:26 -0700431static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
432{
433 switch (c) {
434 case TCG_COND_EQ:
435 return x == y;
436 case TCG_COND_NE:
437 return x != y;
438 case TCG_COND_LT:
439 return (int32_t)x < (int32_t)y;
440 case TCG_COND_GE:
441 return (int32_t)x >= (int32_t)y;
442 case TCG_COND_LE:
443 return (int32_t)x <= (int32_t)y;
444 case TCG_COND_GT:
445 return (int32_t)x > (int32_t)y;
446 case TCG_COND_LTU:
447 return x < y;
448 case TCG_COND_GEU:
449 return x >= y;
450 case TCG_COND_LEU:
451 return x <= y;
452 case TCG_COND_GTU:
453 return x > y;
454 default:
455 tcg_abort();
456 }
457}
458
459static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
460{
461 switch (c) {
462 case TCG_COND_EQ:
463 return x == y;
464 case TCG_COND_NE:
465 return x != y;
466 case TCG_COND_LT:
467 return (int64_t)x < (int64_t)y;
468 case TCG_COND_GE:
469 return (int64_t)x >= (int64_t)y;
470 case TCG_COND_LE:
471 return (int64_t)x <= (int64_t)y;
472 case TCG_COND_GT:
473 return (int64_t)x > (int64_t)y;
474 case TCG_COND_LTU:
475 return x < y;
476 case TCG_COND_GEU:
477 return x >= y;
478 case TCG_COND_LEU:
479 return x <= y;
480 case TCG_COND_GTU:
481 return x > y;
482 default:
483 tcg_abort();
484 }
485}
486
487static bool do_constant_folding_cond_eq(TCGCond c)
488{
489 switch (c) {
490 case TCG_COND_GT:
491 case TCG_COND_LTU:
492 case TCG_COND_LT:
493 case TCG_COND_GTU:
494 case TCG_COND_NE:
495 return 0;
496 case TCG_COND_GE:
497 case TCG_COND_GEU:
498 case TCG_COND_LE:
499 case TCG_COND_LEU:
500 case TCG_COND_EQ:
501 return 1;
502 default:
503 tcg_abort();
504 }
505}
506
Richard Henderson8d57bf12021-08-24 08:34:27 -0700507/*
508 * Return -1 if the condition can't be simplified,
509 * and the result of the condition (0 or 1) if it can.
510 */
Richard Henderson67f84c92021-08-25 08:00:20 -0700511static int do_constant_folding_cond(TCGType type, TCGArg x,
Richard Henderson8d57bf12021-08-24 08:34:27 -0700512 TCGArg y, TCGCond c)
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200513{
Richard Henderson54795542020-09-06 16:21:32 -0700514 uint64_t xv = arg_info(x)->val;
515 uint64_t yv = arg_info(y)->val;
516
Richard Henderson63490392017-06-20 13:43:15 -0700517 if (arg_is_const(x) && arg_is_const(y)) {
Richard Henderson67f84c92021-08-25 08:00:20 -0700518 switch (type) {
519 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100520 return do_constant_folding_cond_32(xv, yv, c);
Richard Henderson67f84c92021-08-25 08:00:20 -0700521 case TCG_TYPE_I64:
522 return do_constant_folding_cond_64(xv, yv, c);
523 default:
524 /* Only scalar comparisons are optimizable */
525 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200526 }
Richard Henderson63490392017-06-20 13:43:15 -0700527 } else if (args_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700528 return do_constant_folding_cond_eq(c);
Richard Henderson63490392017-06-20 13:43:15 -0700529 } else if (arg_is_const(y) && yv == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200530 switch (c) {
531 case TCG_COND_LTU:
532 return 0;
533 case TCG_COND_GEU:
534 return 1;
535 default:
Richard Henderson8d57bf12021-08-24 08:34:27 -0700536 return -1;
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200537 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200538 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700539 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200540}
541
Richard Henderson8d57bf12021-08-24 08:34:27 -0700542/*
543 * Return -1 if the condition can't be simplified,
544 * and the result of the condition (0 or 1) if it can.
545 */
546static int do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
Richard Henderson6c4382f2012-10-02 11:32:27 -0700547{
548 TCGArg al = p1[0], ah = p1[1];
549 TCGArg bl = p2[0], bh = p2[1];
550
Richard Henderson63490392017-06-20 13:43:15 -0700551 if (arg_is_const(bl) && arg_is_const(bh)) {
552 tcg_target_ulong blv = arg_info(bl)->val;
553 tcg_target_ulong bhv = arg_info(bh)->val;
554 uint64_t b = deposit64(blv, 32, 32, bhv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700555
Richard Henderson63490392017-06-20 13:43:15 -0700556 if (arg_is_const(al) && arg_is_const(ah)) {
557 tcg_target_ulong alv = arg_info(al)->val;
558 tcg_target_ulong ahv = arg_info(ah)->val;
559 uint64_t a = deposit64(alv, 32, 32, ahv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700560 return do_constant_folding_cond_64(a, b, c);
561 }
562 if (b == 0) {
563 switch (c) {
564 case TCG_COND_LTU:
565 return 0;
566 case TCG_COND_GEU:
567 return 1;
568 default:
569 break;
570 }
571 }
572 }
Richard Henderson63490392017-06-20 13:43:15 -0700573 if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700574 return do_constant_folding_cond_eq(c);
575 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700576 return -1;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700577}
578
Richard Henderson24c9ae42012-10-02 11:32:21 -0700579static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
580{
581 TCGArg a1 = *p1, a2 = *p2;
582 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700583 sum += arg_is_const(a1);
584 sum -= arg_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700585
586 /* Prefer the constant in second argument, and then the form
587 op a, a, b, which is better handled on non-RISC hosts. */
588 if (sum > 0 || (sum == 0 && dest == a2)) {
589 *p1 = a2;
590 *p2 = a1;
591 return true;
592 }
593 return false;
594}
595
Richard Henderson0bfcb862012-10-02 11:32:23 -0700596static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
597{
598 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700599 sum += arg_is_const(p1[0]);
600 sum += arg_is_const(p1[1]);
601 sum -= arg_is_const(p2[0]);
602 sum -= arg_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700603 if (sum > 0) {
604 TCGArg t;
605 t = p1[0], p1[0] = p2[0], p2[0] = t;
606 t = p1[1], p1[1] = p2[1], p2[1] = t;
607 return true;
608 }
609 return false;
610}
611
Richard Hendersone2577ea2021-08-24 08:00:48 -0700612static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
613{
614 for (int i = 0; i < nb_args; i++) {
615 TCGTemp *ts = arg_temp(op->args[i]);
616 if (ts) {
617 init_ts_info(ctx, ts);
618 }
619 }
620}
621
Richard Henderson8774dde2021-08-24 08:04:47 -0700622static void copy_propagate(OptContext *ctx, TCGOp *op,
623 int nb_oargs, int nb_iargs)
624{
625 TCGContext *s = ctx->tcg;
626
627 for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
628 TCGTemp *ts = arg_temp(op->args[i]);
629 if (ts && ts_is_copy(ts)) {
630 op->args[i] = temp_arg(find_better_copy(s, ts));
631 }
632 }
633}
634
Richard Henderson137f1f42021-08-24 08:49:25 -0700635static void finish_folding(OptContext *ctx, TCGOp *op)
636{
637 const TCGOpDef *def = &tcg_op_defs[op->opc];
638 int i, nb_oargs;
639
640 /*
641 * For an opcode that ends a BB, reset all temp data.
642 * We do no cross-BB optimization.
643 */
644 if (def->flags & TCG_OPF_BB_END) {
645 memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
646 ctx->prev_mb = NULL;
647 return;
648 }
649
650 nb_oargs = def->nb_oargs;
651 for (i = 0; i < nb_oargs; i++) {
652 reset_temp(op->args[i]);
653 /*
654 * Save the corresponding known-zero bits mask for the
655 * first output argument (only one supported so far).
656 */
657 if (i == 0) {
658 arg_info(op->args[i])->z_mask = ctx->z_mask;
659 }
660 }
661}
662
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700663/*
664 * The fold_* functions return true when processing is complete,
665 * usually by folding the operation to a constant or to a copy,
666 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
667 * like collect information about the value produced, for use in
668 * optimizing a subsequent operation.
669 *
670 * These first fold_* functions are all helpers, used by other
671 * folders for more specific operations.
672 */
673
674static bool fold_const1(OptContext *ctx, TCGOp *op)
675{
676 if (arg_is_const(op->args[1])) {
677 uint64_t t;
678
679 t = arg_info(op->args[1])->val;
Richard Henderson67f84c92021-08-25 08:00:20 -0700680 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700681 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
682 }
683 return false;
684}
685
686static bool fold_const2(OptContext *ctx, TCGOp *op)
687{
688 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
689 uint64_t t1 = arg_info(op->args[1])->val;
690 uint64_t t2 = arg_info(op->args[2])->val;
691
Richard Henderson67f84c92021-08-25 08:00:20 -0700692 t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700693 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
694 }
695 return false;
696}
697
Richard Hendersonfae450b2021-08-25 22:42:19 -0700698static bool fold_masks(OptContext *ctx, TCGOp *op)
699{
700 uint64_t a_mask = ctx->a_mask;
701 uint64_t z_mask = ctx->z_mask;
702
703 /*
704 * 32-bit ops generate 32-bit results. For the result is zero test
705 * below, we can ignore high bits, but for further optimizations we
706 * need to record that the high bits contain garbage.
707 */
708 if (ctx->type == TCG_TYPE_I32) {
709 ctx->z_mask |= MAKE_64BIT_MASK(32, 32);
710 a_mask &= MAKE_64BIT_MASK(0, 32);
711 z_mask &= MAKE_64BIT_MASK(0, 32);
712 }
713
714 if (z_mask == 0) {
715 return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
716 }
717 if (a_mask == 0) {
718 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
719 }
720 return false;
721}
722
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700723/*
724 * Convert @op to NOT, if NOT is supported by the host.
725 * Return true f the conversion is successful, which will still
726 * indicate that the processing is complete.
727 */
728static bool fold_not(OptContext *ctx, TCGOp *op);
729static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
730{
731 TCGOpcode not_op;
732 bool have_not;
733
734 switch (ctx->type) {
735 case TCG_TYPE_I32:
736 not_op = INDEX_op_not_i32;
737 have_not = TCG_TARGET_HAS_not_i32;
738 break;
739 case TCG_TYPE_I64:
740 not_op = INDEX_op_not_i64;
741 have_not = TCG_TARGET_HAS_not_i64;
742 break;
743 case TCG_TYPE_V64:
744 case TCG_TYPE_V128:
745 case TCG_TYPE_V256:
746 not_op = INDEX_op_not_vec;
747 have_not = TCG_TARGET_HAS_not_vec;
748 break;
749 default:
750 g_assert_not_reached();
751 }
752 if (have_not) {
753 op->opc = not_op;
754 op->args[1] = op->args[idx];
755 return fold_not(ctx, op);
756 }
757 return false;
758}
759
Richard Hendersonda48e272021-08-25 20:42:04 -0700760/* If the binary operation has first argument @i, fold to @i. */
761static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
762{
763 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
764 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
765 }
766 return false;
767}
768
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700769/* If the binary operation has first argument @i, fold to NOT. */
770static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
771{
772 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
773 return fold_to_not(ctx, op, 2);
774 }
775 return false;
776}
777
Richard Hendersone8679952021-08-25 13:19:52 -0700778/* If the binary operation has second argument @i, fold to @i. */
779static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
780{
781 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
782 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
783 }
784 return false;
785}
786
Richard Hendersona63ce0e2021-08-25 20:28:53 -0700787/* If the binary operation has second argument @i, fold to identity. */
788static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
789{
790 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
791 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
792 }
793 return false;
794}
795
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700796/* If the binary operation has second argument @i, fold to NOT. */
797static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
798{
799 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
800 return fold_to_not(ctx, op, 1);
801 }
802 return false;
803}
804
Richard Hendersoncbe42fb2021-08-25 13:02:00 -0700805/* If the binary operation has both arguments equal, fold to @i. */
806static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
807{
808 if (args_are_copies(op->args[1], op->args[2])) {
809 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
810 }
811 return false;
812}
813
Richard Hendersonca7bb042021-08-25 13:14:21 -0700814/* If the binary operation has both arguments equal, fold to identity. */
815static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
816{
817 if (args_are_copies(op->args[1], op->args[2])) {
818 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
819 }
820 return false;
821}
822
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700823/*
824 * These outermost fold_<op> functions are sorted alphabetically.
Richard Hendersonca7bb042021-08-25 13:14:21 -0700825 *
826 * The ordering of the transformations should be:
827 * 1) those that produce a constant
828 * 2) those that produce a copy
829 * 3) those that produce information about the result value.
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700830 */
831
832static bool fold_add(OptContext *ctx, TCGOp *op)
833{
Richard Hendersona63ce0e2021-08-25 20:28:53 -0700834 if (fold_const2(ctx, op) ||
835 fold_xi_to_x(ctx, op, 0)) {
836 return true;
837 }
838 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700839}
840
Richard Hendersone3f7dc22021-08-24 10:30:38 -0700841static bool fold_addsub2_i32(OptContext *ctx, TCGOp *op, bool add)
842{
843 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3]) &&
844 arg_is_const(op->args[4]) && arg_is_const(op->args[5])) {
845 uint32_t al = arg_info(op->args[2])->val;
846 uint32_t ah = arg_info(op->args[3])->val;
847 uint32_t bl = arg_info(op->args[4])->val;
848 uint32_t bh = arg_info(op->args[5])->val;
849 uint64_t a = ((uint64_t)ah << 32) | al;
850 uint64_t b = ((uint64_t)bh << 32) | bl;
851 TCGArg rl, rh;
852 TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_mov_i32);
853
854 if (add) {
855 a += b;
856 } else {
857 a -= b;
858 }
859
860 rl = op->args[0];
861 rh = op->args[1];
862 tcg_opt_gen_movi(ctx, op, rl, (int32_t)a);
863 tcg_opt_gen_movi(ctx, op2, rh, (int32_t)(a >> 32));
864 return true;
865 }
866 return false;
867}
868
869static bool fold_add2_i32(OptContext *ctx, TCGOp *op)
870{
871 return fold_addsub2_i32(ctx, op, true);
872}
873
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700874static bool fold_and(OptContext *ctx, TCGOp *op)
875{
Richard Hendersonfae450b2021-08-25 22:42:19 -0700876 uint64_t z1, z2;
877
Richard Hendersonca7bb042021-08-25 13:14:21 -0700878 if (fold_const2(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -0700879 fold_xi_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -0700880 fold_xi_to_x(ctx, op, -1) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -0700881 fold_xx_to_x(ctx, op)) {
882 return true;
883 }
Richard Hendersonfae450b2021-08-25 22:42:19 -0700884
885 z1 = arg_info(op->args[1])->z_mask;
886 z2 = arg_info(op->args[2])->z_mask;
887 ctx->z_mask = z1 & z2;
888
889 /*
890 * Known-zeros does not imply known-ones. Therefore unless
891 * arg2 is constant, we can't infer affected bits from it.
892 */
893 if (arg_is_const(op->args[2])) {
894 ctx->a_mask = z1 & ~z2;
895 }
896
897 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700898}
899
900static bool fold_andc(OptContext *ctx, TCGOp *op)
901{
Richard Hendersonfae450b2021-08-25 22:42:19 -0700902 uint64_t z1;
903
Richard Hendersoncbe42fb2021-08-25 13:02:00 -0700904 if (fold_const2(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700905 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -0700906 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700907 fold_ix_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -0700908 return true;
909 }
Richard Hendersonfae450b2021-08-25 22:42:19 -0700910
911 z1 = arg_info(op->args[1])->z_mask;
912
913 /*
914 * Known-zeros does not imply known-ones. Therefore unless
915 * arg2 is constant, we can't infer anything from it.
916 */
917 if (arg_is_const(op->args[2])) {
918 uint64_t z2 = ~arg_info(op->args[2])->z_mask;
919 ctx->a_mask = z1 & ~z2;
920 z1 &= z2;
921 }
922 ctx->z_mask = z1;
923
924 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700925}
926
Richard Henderson079b0802021-08-24 09:30:59 -0700927static bool fold_brcond(OptContext *ctx, TCGOp *op)
928{
929 TCGCond cond = op->args[2];
Richard Henderson67f84c92021-08-25 08:00:20 -0700930 int i = do_constant_folding_cond(ctx->type, op->args[0], op->args[1], cond);
Richard Henderson079b0802021-08-24 09:30:59 -0700931
932 if (i == 0) {
933 tcg_op_remove(ctx->tcg, op);
934 return true;
935 }
936 if (i > 0) {
937 op->opc = INDEX_op_br;
938 op->args[0] = op->args[3];
939 }
940 return false;
941}
942
Richard Henderson764d2ab2021-08-24 09:22:11 -0700943static bool fold_brcond2(OptContext *ctx, TCGOp *op)
944{
945 TCGCond cond = op->args[4];
946 int i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond);
947 TCGArg label = op->args[5];
948 int inv = 0;
949
950 if (i >= 0) {
951 goto do_brcond_const;
952 }
953
954 switch (cond) {
955 case TCG_COND_LT:
956 case TCG_COND_GE:
957 /*
958 * Simplify LT/GE comparisons vs zero to a single compare
959 * vs the high word of the input.
960 */
961 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == 0 &&
962 arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0) {
963 goto do_brcond_high;
964 }
965 break;
966
967 case TCG_COND_NE:
968 inv = 1;
969 QEMU_FALLTHROUGH;
970 case TCG_COND_EQ:
971 /*
972 * Simplify EQ/NE comparisons where one of the pairs
973 * can be simplified.
974 */
Richard Henderson67f84c92021-08-25 08:00:20 -0700975 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
Richard Henderson764d2ab2021-08-24 09:22:11 -0700976 op->args[2], cond);
977 switch (i ^ inv) {
978 case 0:
979 goto do_brcond_const;
980 case 1:
981 goto do_brcond_high;
982 }
983
Richard Henderson67f84c92021-08-25 08:00:20 -0700984 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Henderson764d2ab2021-08-24 09:22:11 -0700985 op->args[3], cond);
986 switch (i ^ inv) {
987 case 0:
988 goto do_brcond_const;
989 case 1:
990 op->opc = INDEX_op_brcond_i32;
991 op->args[1] = op->args[2];
992 op->args[2] = cond;
993 op->args[3] = label;
994 break;
995 }
996 break;
997
998 default:
999 break;
1000
1001 do_brcond_high:
1002 op->opc = INDEX_op_brcond_i32;
1003 op->args[0] = op->args[1];
1004 op->args[1] = op->args[3];
1005 op->args[2] = cond;
1006 op->args[3] = label;
1007 break;
1008
1009 do_brcond_const:
1010 if (i == 0) {
1011 tcg_op_remove(ctx->tcg, op);
1012 return true;
1013 }
1014 op->opc = INDEX_op_br;
1015 op->args[0] = label;
1016 break;
1017 }
1018 return false;
1019}
1020
Richard Henderson09bacdc2021-08-24 11:58:12 -07001021static bool fold_bswap(OptContext *ctx, TCGOp *op)
1022{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001023 uint64_t z_mask, sign;
1024
Richard Henderson09bacdc2021-08-24 11:58:12 -07001025 if (arg_is_const(op->args[1])) {
1026 uint64_t t = arg_info(op->args[1])->val;
1027
Richard Henderson67f84c92021-08-25 08:00:20 -07001028 t = do_constant_folding(op->opc, ctx->type, t, op->args[2]);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001029 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1030 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001031
1032 z_mask = arg_info(op->args[1])->z_mask;
1033 switch (op->opc) {
1034 case INDEX_op_bswap16_i32:
1035 case INDEX_op_bswap16_i64:
1036 z_mask = bswap16(z_mask);
1037 sign = INT16_MIN;
1038 break;
1039 case INDEX_op_bswap32_i32:
1040 case INDEX_op_bswap32_i64:
1041 z_mask = bswap32(z_mask);
1042 sign = INT32_MIN;
1043 break;
1044 case INDEX_op_bswap64_i64:
1045 z_mask = bswap64(z_mask);
1046 sign = INT64_MIN;
1047 break;
1048 default:
1049 g_assert_not_reached();
1050 }
1051
1052 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1053 case TCG_BSWAP_OZ:
1054 break;
1055 case TCG_BSWAP_OS:
1056 /* If the sign bit may be 1, force all the bits above to 1. */
1057 if (z_mask & sign) {
1058 z_mask |= sign;
1059 }
1060 break;
1061 default:
1062 /* The high bits are undefined: force all bits above the sign to 1. */
1063 z_mask |= sign << 1;
1064 break;
1065 }
1066 ctx->z_mask = z_mask;
1067
1068 return fold_masks(ctx, op);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001069}
1070
Richard Henderson5cf32be2021-08-24 08:17:08 -07001071static bool fold_call(OptContext *ctx, TCGOp *op)
1072{
1073 TCGContext *s = ctx->tcg;
1074 int nb_oargs = TCGOP_CALLO(op);
1075 int nb_iargs = TCGOP_CALLI(op);
1076 int flags, i;
1077
1078 init_arguments(ctx, op, nb_oargs + nb_iargs);
1079 copy_propagate(ctx, op, nb_oargs, nb_iargs);
1080
1081 /* If the function reads or writes globals, reset temp data. */
1082 flags = tcg_call_flags(op);
1083 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1084 int nb_globals = s->nb_globals;
1085
1086 for (i = 0; i < nb_globals; i++) {
1087 if (test_bit(i, ctx->temps_used.l)) {
1088 reset_ts(&ctx->tcg->temps[i]);
1089 }
1090 }
1091 }
1092
1093 /* Reset temp data for outputs. */
1094 for (i = 0; i < nb_oargs; i++) {
1095 reset_temp(op->args[i]);
1096 }
1097
1098 /* Stop optimizing MB across calls. */
1099 ctx->prev_mb = NULL;
1100 return true;
1101}
1102
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001103static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1104{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001105 uint64_t z_mask;
1106
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001107 if (arg_is_const(op->args[1])) {
1108 uint64_t t = arg_info(op->args[1])->val;
1109
1110 if (t != 0) {
Richard Henderson67f84c92021-08-25 08:00:20 -07001111 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001112 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1113 }
1114 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1115 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001116
1117 switch (ctx->type) {
1118 case TCG_TYPE_I32:
1119 z_mask = 31;
1120 break;
1121 case TCG_TYPE_I64:
1122 z_mask = 63;
1123 break;
1124 default:
1125 g_assert_not_reached();
1126 }
1127 ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask;
1128
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001129 return false;
1130}
1131
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001132static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1133{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001134 if (fold_const1(ctx, op)) {
1135 return true;
1136 }
1137
1138 switch (ctx->type) {
1139 case TCG_TYPE_I32:
1140 ctx->z_mask = 32 | 31;
1141 break;
1142 case TCG_TYPE_I64:
1143 ctx->z_mask = 64 | 63;
1144 break;
1145 default:
1146 g_assert_not_reached();
1147 }
1148 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001149}
1150
Richard Henderson1b1907b2021-08-24 10:47:04 -07001151static bool fold_deposit(OptContext *ctx, TCGOp *op)
1152{
1153 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1154 uint64_t t1 = arg_info(op->args[1])->val;
1155 uint64_t t2 = arg_info(op->args[2])->val;
1156
1157 t1 = deposit64(t1, op->args[3], op->args[4], t2);
1158 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1159 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001160
1161 ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
1162 op->args[3], op->args[4],
1163 arg_info(op->args[2])->z_mask);
Richard Henderson1b1907b2021-08-24 10:47:04 -07001164 return false;
1165}
1166
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001167static bool fold_divide(OptContext *ctx, TCGOp *op)
1168{
1169 return fold_const2(ctx, op);
1170}
1171
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07001172static bool fold_dup(OptContext *ctx, TCGOp *op)
1173{
1174 if (arg_is_const(op->args[1])) {
1175 uint64_t t = arg_info(op->args[1])->val;
1176 t = dup_const(TCGOP_VECE(op), t);
1177 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1178 }
1179 return false;
1180}
1181
1182static bool fold_dup2(OptContext *ctx, TCGOp *op)
1183{
1184 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1185 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1186 arg_info(op->args[2])->val);
1187 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1188 }
1189
1190 if (args_are_copies(op->args[1], op->args[2])) {
1191 op->opc = INDEX_op_dup_vec;
1192 TCGOP_VECE(op) = MO_32;
1193 }
1194 return false;
1195}
1196
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001197static bool fold_eqv(OptContext *ctx, TCGOp *op)
1198{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001199 if (fold_const2(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001200 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001201 fold_xi_to_not(ctx, op, 0)) {
1202 return true;
1203 }
1204 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001205}
1206
Richard Hendersonb6617c82021-08-24 10:44:53 -07001207static bool fold_extract(OptContext *ctx, TCGOp *op)
1208{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001209 uint64_t z_mask_old, z_mask;
1210
Richard Hendersonb6617c82021-08-24 10:44:53 -07001211 if (arg_is_const(op->args[1])) {
1212 uint64_t t;
1213
1214 t = arg_info(op->args[1])->val;
1215 t = extract64(t, op->args[2], op->args[3]);
1216 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1217 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001218
1219 z_mask_old = arg_info(op->args[1])->z_mask;
1220 z_mask = extract64(z_mask_old, op->args[2], op->args[3]);
1221 if (op->args[2] == 0) {
1222 ctx->a_mask = z_mask_old ^ z_mask;
1223 }
1224 ctx->z_mask = z_mask;
1225
1226 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001227}
1228
Richard Hendersondcd08992021-08-24 10:41:39 -07001229static bool fold_extract2(OptContext *ctx, TCGOp *op)
1230{
1231 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1232 uint64_t v1 = arg_info(op->args[1])->val;
1233 uint64_t v2 = arg_info(op->args[2])->val;
1234 int shr = op->args[3];
1235
1236 if (op->opc == INDEX_op_extract2_i64) {
1237 v1 >>= shr;
1238 v2 <<= 64 - shr;
1239 } else {
1240 v1 = (uint32_t)v1 >> shr;
1241 v2 = (int32_t)v2 << (32 - shr);
1242 }
1243 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1244 }
1245 return false;
1246}
1247
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001248static bool fold_exts(OptContext *ctx, TCGOp *op)
1249{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001250 uint64_t z_mask_old, z_mask, sign;
1251 bool type_change = false;
1252
1253 if (fold_const1(ctx, op)) {
1254 return true;
1255 }
1256
1257 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1258
1259 switch (op->opc) {
1260 CASE_OP_32_64(ext8s):
1261 sign = INT8_MIN;
1262 z_mask = (uint8_t)z_mask;
1263 break;
1264 CASE_OP_32_64(ext16s):
1265 sign = INT16_MIN;
1266 z_mask = (uint16_t)z_mask;
1267 break;
1268 case INDEX_op_ext_i32_i64:
1269 type_change = true;
1270 QEMU_FALLTHROUGH;
1271 case INDEX_op_ext32s_i64:
1272 sign = INT32_MIN;
1273 z_mask = (uint32_t)z_mask;
1274 break;
1275 default:
1276 g_assert_not_reached();
1277 }
1278
1279 if (z_mask & sign) {
1280 z_mask |= sign;
1281 } else if (!type_change) {
1282 ctx->a_mask = z_mask_old ^ z_mask;
1283 }
1284 ctx->z_mask = z_mask;
1285
1286 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001287}
1288
1289static bool fold_extu(OptContext *ctx, TCGOp *op)
1290{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001291 uint64_t z_mask_old, z_mask;
1292 bool type_change = false;
1293
1294 if (fold_const1(ctx, op)) {
1295 return true;
1296 }
1297
1298 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1299
1300 switch (op->opc) {
1301 CASE_OP_32_64(ext8u):
1302 z_mask = (uint8_t)z_mask;
1303 break;
1304 CASE_OP_32_64(ext16u):
1305 z_mask = (uint16_t)z_mask;
1306 break;
1307 case INDEX_op_extrl_i64_i32:
1308 case INDEX_op_extu_i32_i64:
1309 type_change = true;
1310 QEMU_FALLTHROUGH;
1311 case INDEX_op_ext32u_i64:
1312 z_mask = (uint32_t)z_mask;
1313 break;
1314 case INDEX_op_extrh_i64_i32:
1315 type_change = true;
1316 z_mask >>= 32;
1317 break;
1318 default:
1319 g_assert_not_reached();
1320 }
1321
1322 ctx->z_mask = z_mask;
1323 if (!type_change) {
1324 ctx->a_mask = z_mask_old ^ z_mask;
1325 }
1326 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001327}
1328
Richard Henderson3eefdf22021-08-25 11:06:43 -07001329static bool fold_mb(OptContext *ctx, TCGOp *op)
1330{
1331 /* Eliminate duplicate and redundant fence instructions. */
1332 if (ctx->prev_mb) {
1333 /*
1334 * Merge two barriers of the same type into one,
1335 * or a weaker barrier into a stronger one,
1336 * or two weaker barriers into a stronger one.
1337 * mb X; mb Y => mb X|Y
1338 * mb; strl => mb; st
1339 * ldaq; mb => ld; mb
1340 * ldaq; strl => ld; mb; st
1341 * Other combinations are also merged into a strong
1342 * barrier. This is stricter than specified but for
1343 * the purposes of TCG is better than not optimizing.
1344 */
1345 ctx->prev_mb->args[0] |= op->args[0];
1346 tcg_op_remove(ctx->tcg, op);
1347 } else {
1348 ctx->prev_mb = op;
1349 }
1350 return true;
1351}
1352
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001353static bool fold_mov(OptContext *ctx, TCGOp *op)
1354{
1355 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1356}
1357
Richard Henderson0c310a32021-08-24 10:37:24 -07001358static bool fold_movcond(OptContext *ctx, TCGOp *op)
1359{
Richard Henderson0c310a32021-08-24 10:37:24 -07001360 TCGCond cond = op->args[5];
Richard Henderson67f84c92021-08-25 08:00:20 -07001361 int i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
Richard Henderson0c310a32021-08-24 10:37:24 -07001362
1363 if (i >= 0) {
1364 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1365 }
1366
Richard Hendersonfae450b2021-08-25 22:42:19 -07001367 ctx->z_mask = arg_info(op->args[3])->z_mask
1368 | arg_info(op->args[4])->z_mask;
1369
Richard Henderson0c310a32021-08-24 10:37:24 -07001370 if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
1371 uint64_t tv = arg_info(op->args[3])->val;
1372 uint64_t fv = arg_info(op->args[4])->val;
Richard Henderson67f84c92021-08-25 08:00:20 -07001373 TCGOpcode opc;
Richard Henderson0c310a32021-08-24 10:37:24 -07001374
Richard Henderson67f84c92021-08-25 08:00:20 -07001375 switch (ctx->type) {
1376 case TCG_TYPE_I32:
1377 opc = INDEX_op_setcond_i32;
1378 break;
1379 case TCG_TYPE_I64:
1380 opc = INDEX_op_setcond_i64;
1381 break;
1382 default:
1383 g_assert_not_reached();
1384 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001385
1386 if (tv == 1 && fv == 0) {
1387 op->opc = opc;
1388 op->args[3] = cond;
1389 } else if (fv == 1 && tv == 0) {
1390 op->opc = opc;
1391 op->args[3] = tcg_invert_cond(cond);
1392 }
1393 }
1394 return false;
1395}
1396
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001397static bool fold_mul(OptContext *ctx, TCGOp *op)
1398{
Richard Hendersone8679952021-08-25 13:19:52 -07001399 if (fold_const2(ctx, op) ||
1400 fold_xi_to_i(ctx, op, 0)) {
1401 return true;
1402 }
1403 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001404}
1405
1406static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
1407{
Richard Hendersone8679952021-08-25 13:19:52 -07001408 if (fold_const2(ctx, op) ||
1409 fold_xi_to_i(ctx, op, 0)) {
1410 return true;
1411 }
1412 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001413}
1414
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001415static bool fold_mulu2_i32(OptContext *ctx, TCGOp *op)
1416{
1417 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
1418 uint32_t a = arg_info(op->args[2])->val;
1419 uint32_t b = arg_info(op->args[3])->val;
1420 uint64_t r = (uint64_t)a * b;
1421 TCGArg rl, rh;
1422 TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_mov_i32);
1423
1424 rl = op->args[0];
1425 rh = op->args[1];
1426 tcg_opt_gen_movi(ctx, op, rl, (int32_t)r);
1427 tcg_opt_gen_movi(ctx, op2, rh, (int32_t)(r >> 32));
1428 return true;
1429 }
1430 return false;
1431}
1432
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001433static bool fold_nand(OptContext *ctx, TCGOp *op)
1434{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001435 if (fold_const2(ctx, op) ||
1436 fold_xi_to_not(ctx, op, -1)) {
1437 return true;
1438 }
1439 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001440}
1441
1442static bool fold_neg(OptContext *ctx, TCGOp *op)
1443{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001444 uint64_t z_mask;
1445
Richard Henderson9caca882021-08-24 13:30:32 -07001446 if (fold_const1(ctx, op)) {
1447 return true;
1448 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001449
1450 /* Set to 1 all bits to the left of the rightmost. */
1451 z_mask = arg_info(op->args[1])->z_mask;
1452 ctx->z_mask = -(z_mask & -z_mask);
1453
Richard Henderson9caca882021-08-24 13:30:32 -07001454 /*
1455 * Because of fold_sub_to_neg, we want to always return true,
1456 * via finish_folding.
1457 */
1458 finish_folding(ctx, op);
1459 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001460}
1461
1462static bool fold_nor(OptContext *ctx, TCGOp *op)
1463{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001464 if (fold_const2(ctx, op) ||
1465 fold_xi_to_not(ctx, op, 0)) {
1466 return true;
1467 }
1468 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001469}
1470
1471static bool fold_not(OptContext *ctx, TCGOp *op)
1472{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001473 if (fold_const1(ctx, op)) {
1474 return true;
1475 }
1476
1477 /* Because of fold_to_not, we want to always return true, via finish. */
1478 finish_folding(ctx, op);
1479 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001480}
1481
1482static bool fold_or(OptContext *ctx, TCGOp *op)
1483{
Richard Hendersonca7bb042021-08-25 13:14:21 -07001484 if (fold_const2(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001485 fold_xi_to_x(ctx, op, 0) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001486 fold_xx_to_x(ctx, op)) {
1487 return true;
1488 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001489
1490 ctx->z_mask = arg_info(op->args[1])->z_mask
1491 | arg_info(op->args[2])->z_mask;
1492 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001493}
1494
1495static bool fold_orc(OptContext *ctx, TCGOp *op)
1496{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001497 if (fold_const2(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001498 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001499 fold_ix_to_not(ctx, op, 0)) {
1500 return true;
1501 }
1502 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001503}
1504
Richard Henderson3eefdf22021-08-25 11:06:43 -07001505static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
1506{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001507 const TCGOpDef *def = &tcg_op_defs[op->opc];
1508 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
1509 MemOp mop = get_memop(oi);
1510 int width = 8 * memop_size(mop);
1511
1512 if (!(mop & MO_SIGN) && width < 64) {
1513 ctx->z_mask = MAKE_64BIT_MASK(0, width);
1514 }
1515
Richard Henderson3eefdf22021-08-25 11:06:43 -07001516 /* Opcodes that touch guest memory stop the mb optimization. */
1517 ctx->prev_mb = NULL;
1518 return false;
1519}
1520
1521static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
1522{
1523 /* Opcodes that touch guest memory stop the mb optimization. */
1524 ctx->prev_mb = NULL;
1525 return false;
1526}
1527
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001528static bool fold_remainder(OptContext *ctx, TCGOp *op)
1529{
1530 return fold_const2(ctx, op);
1531}
1532
Richard Hendersonc63ff552021-08-24 09:35:30 -07001533static bool fold_setcond(OptContext *ctx, TCGOp *op)
1534{
1535 TCGCond cond = op->args[3];
Richard Henderson67f84c92021-08-25 08:00:20 -07001536 int i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
Richard Hendersonc63ff552021-08-24 09:35:30 -07001537
1538 if (i >= 0) {
1539 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1540 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001541
1542 ctx->z_mask = 1;
Richard Hendersonc63ff552021-08-24 09:35:30 -07001543 return false;
1544}
1545
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001546static bool fold_setcond2(OptContext *ctx, TCGOp *op)
1547{
1548 TCGCond cond = op->args[5];
1549 int i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond);
1550 int inv = 0;
1551
1552 if (i >= 0) {
1553 goto do_setcond_const;
1554 }
1555
1556 switch (cond) {
1557 case TCG_COND_LT:
1558 case TCG_COND_GE:
1559 /*
1560 * Simplify LT/GE comparisons vs zero to a single compare
1561 * vs the high word of the input.
1562 */
1563 if (arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0 &&
1564 arg_is_const(op->args[4]) && arg_info(op->args[4])->val == 0) {
1565 goto do_setcond_high;
1566 }
1567 break;
1568
1569 case TCG_COND_NE:
1570 inv = 1;
1571 QEMU_FALLTHROUGH;
1572 case TCG_COND_EQ:
1573 /*
1574 * Simplify EQ/NE comparisons where one of the pairs
1575 * can be simplified.
1576 */
Richard Henderson67f84c92021-08-25 08:00:20 -07001577 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001578 op->args[3], cond);
1579 switch (i ^ inv) {
1580 case 0:
1581 goto do_setcond_const;
1582 case 1:
1583 goto do_setcond_high;
1584 }
1585
Richard Henderson67f84c92021-08-25 08:00:20 -07001586 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001587 op->args[4], cond);
1588 switch (i ^ inv) {
1589 case 0:
1590 goto do_setcond_const;
1591 case 1:
1592 op->args[2] = op->args[3];
1593 op->args[3] = cond;
1594 op->opc = INDEX_op_setcond_i32;
1595 break;
1596 }
1597 break;
1598
1599 default:
1600 break;
1601
1602 do_setcond_high:
1603 op->args[1] = op->args[2];
1604 op->args[2] = op->args[4];
1605 op->args[3] = cond;
1606 op->opc = INDEX_op_setcond_i32;
1607 break;
1608 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001609
1610 ctx->z_mask = 1;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001611 return false;
1612
1613 do_setcond_const:
1614 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1615}
1616
Richard Hendersonb6617c82021-08-24 10:44:53 -07001617static bool fold_sextract(OptContext *ctx, TCGOp *op)
1618{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001619 int64_t z_mask_old, z_mask;
1620
Richard Hendersonb6617c82021-08-24 10:44:53 -07001621 if (arg_is_const(op->args[1])) {
1622 uint64_t t;
1623
1624 t = arg_info(op->args[1])->val;
1625 t = sextract64(t, op->args[2], op->args[3]);
1626 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1627 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001628
1629 z_mask_old = arg_info(op->args[1])->z_mask;
1630 z_mask = sextract64(z_mask_old, op->args[2], op->args[3]);
1631 if (op->args[2] == 0 && z_mask >= 0) {
1632 ctx->a_mask = z_mask_old ^ z_mask;
1633 }
1634 ctx->z_mask = z_mask;
1635
1636 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001637}
1638
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001639static bool fold_shift(OptContext *ctx, TCGOp *op)
1640{
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001641 if (fold_const2(ctx, op) ||
Richard Hendersonda48e272021-08-25 20:42:04 -07001642 fold_ix_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001643 fold_xi_to_x(ctx, op, 0)) {
1644 return true;
1645 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001646
1647 if (arg_is_const(op->args[2])) {
1648 ctx->z_mask = do_constant_folding(op->opc, ctx->type,
1649 arg_info(op->args[1])->z_mask,
1650 arg_info(op->args[2])->val);
1651 return fold_masks(ctx, op);
1652 }
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001653 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001654}
1655
Richard Henderson9caca882021-08-24 13:30:32 -07001656static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
1657{
1658 TCGOpcode neg_op;
1659 bool have_neg;
1660
1661 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
1662 return false;
1663 }
1664
1665 switch (ctx->type) {
1666 case TCG_TYPE_I32:
1667 neg_op = INDEX_op_neg_i32;
1668 have_neg = TCG_TARGET_HAS_neg_i32;
1669 break;
1670 case TCG_TYPE_I64:
1671 neg_op = INDEX_op_neg_i64;
1672 have_neg = TCG_TARGET_HAS_neg_i64;
1673 break;
1674 case TCG_TYPE_V64:
1675 case TCG_TYPE_V128:
1676 case TCG_TYPE_V256:
1677 neg_op = INDEX_op_neg_vec;
1678 have_neg = (TCG_TARGET_HAS_neg_vec &&
1679 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
1680 break;
1681 default:
1682 g_assert_not_reached();
1683 }
1684 if (have_neg) {
1685 op->opc = neg_op;
1686 op->args[1] = op->args[2];
1687 return fold_neg(ctx, op);
1688 }
1689 return false;
1690}
1691
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001692static bool fold_sub(OptContext *ctx, TCGOp *op)
1693{
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001694 if (fold_const2(ctx, op) ||
Richard Henderson9caca882021-08-24 13:30:32 -07001695 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001696 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson9caca882021-08-24 13:30:32 -07001697 fold_sub_to_neg(ctx, op)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001698 return true;
1699 }
1700 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001701}
1702
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001703static bool fold_sub2_i32(OptContext *ctx, TCGOp *op)
1704{
1705 return fold_addsub2_i32(ctx, op, false);
1706}
1707
Richard Hendersonfae450b2021-08-25 22:42:19 -07001708static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
1709{
1710 /* We can't do any folding with a load, but we can record bits. */
1711 switch (op->opc) {
1712 CASE_OP_32_64(ld8u):
1713 ctx->z_mask = MAKE_64BIT_MASK(0, 8);
1714 break;
1715 CASE_OP_32_64(ld16u):
1716 ctx->z_mask = MAKE_64BIT_MASK(0, 16);
1717 break;
1718 case INDEX_op_ld32u_i64:
1719 ctx->z_mask = MAKE_64BIT_MASK(0, 32);
1720 break;
1721 default:
1722 g_assert_not_reached();
1723 }
1724 return false;
1725}
1726
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001727static bool fold_xor(OptContext *ctx, TCGOp *op)
1728{
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001729 if (fold_const2(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001730 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001731 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001732 fold_xi_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001733 return true;
1734 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001735
1736 ctx->z_mask = arg_info(op->args[1])->z_mask
1737 | arg_info(op->args[2])->z_mask;
1738 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001739}
1740
Kirill Batuzov22613af2011-07-07 16:37:13 +04001741/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +02001742void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001743{
Richard Henderson5cf32be2021-08-24 08:17:08 -07001744 int nb_temps, i;
Richard Hendersond0ed5152021-08-24 07:38:39 -07001745 TCGOp *op, *op_next;
Richard Hendersondc849882021-08-24 07:13:45 -07001746 OptContext ctx = { .tcg = s };
Richard Henderson5d8f5362012-09-21 10:13:38 -07001747
Kirill Batuzov22613af2011-07-07 16:37:13 +04001748 /* Array VALS has an element for each temp.
1749 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001750 If this temp is a copy of other ones then the other copies are
1751 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001752
1753 nb_temps = s->nb_temps;
Richard Henderson8f17a972020-03-30 19:52:02 -07001754 for (i = 0; i < nb_temps; ++i) {
1755 s->temps[i].state_ptr = NULL;
1756 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001757
Richard Henderson15fa08f2017-11-02 15:19:14 +01001758 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001759 TCGOpcode opc = op->opc;
Richard Henderson5cf32be2021-08-24 08:17:08 -07001760 const TCGOpDef *def;
Richard Henderson404a1482021-08-24 11:08:21 -07001761 bool done = false;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001762
Richard Henderson5cf32be2021-08-24 08:17:08 -07001763 /* Calls are special. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001764 if (opc == INDEX_op_call) {
Richard Henderson5cf32be2021-08-24 08:17:08 -07001765 fold_call(&ctx, op);
1766 continue;
Richard Hendersoncf066672014-03-22 20:06:52 -07001767 }
Richard Henderson5cf32be2021-08-24 08:17:08 -07001768
1769 def = &tcg_op_defs[opc];
Richard Hendersonec5d4cb2021-08-24 08:20:27 -07001770 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
1771 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001772
Richard Henderson67f84c92021-08-25 08:00:20 -07001773 /* Pre-compute the type of the operation. */
1774 if (def->flags & TCG_OPF_VECTOR) {
1775 ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
1776 } else if (def->flags & TCG_OPF_64BIT) {
1777 ctx.type = TCG_TYPE_I64;
1778 } else {
1779 ctx.type = TCG_TYPE_I32;
1780 }
1781
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001782 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001783 switch (opc) {
Richard Henderson170ba882017-11-22 09:07:11 +01001784 CASE_OP_32_64_VEC(add):
1785 CASE_OP_32_64_VEC(mul):
1786 CASE_OP_32_64_VEC(and):
1787 CASE_OP_32_64_VEC(or):
1788 CASE_OP_32_64_VEC(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001789 CASE_OP_32_64(eqv):
1790 CASE_OP_32_64(nand):
1791 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001792 CASE_OP_32_64(muluh):
1793 CASE_OP_32_64(mulsh):
Richard Hendersonacd93702016-12-08 12:28:42 -08001794 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001795 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +02001796 CASE_OP_32_64(brcond):
Richard Hendersonacd93702016-12-08 12:28:42 -08001797 if (swap_commutative(-1, &op->args[0], &op->args[1])) {
1798 op->args[2] = tcg_swap_cond(op->args[2]);
Aurelien Jarno65a7cce2012-09-06 16:47:14 +02001799 }
1800 break;
1801 CASE_OP_32_64(setcond):
Richard Hendersonacd93702016-12-08 12:28:42 -08001802 if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) {
1803 op->args[3] = tcg_swap_cond(op->args[3]);
Aurelien Jarno65a7cce2012-09-06 16:47:14 +02001804 }
1805 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001806 CASE_OP_32_64(movcond):
Richard Hendersonacd93702016-12-08 12:28:42 -08001807 if (swap_commutative(-1, &op->args[1], &op->args[2])) {
1808 op->args[5] = tcg_swap_cond(op->args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001809 }
Richard Henderson5d8f5362012-09-21 10:13:38 -07001810 /* For movcond, we canonicalize the "false" input reg to match
1811 the destination reg so that the tcg backend can implement
1812 a "move if true" operation. */
Richard Hendersonacd93702016-12-08 12:28:42 -08001813 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
1814 op->args[5] = tcg_invert_cond(op->args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -07001815 }
Richard Henderson1e484e62012-10-02 11:32:22 -07001816 break;
Richard Hendersond7156f72013-02-19 23:51:52 -08001817 CASE_OP_32_64(add2):
Richard Hendersonacd93702016-12-08 12:28:42 -08001818 swap_commutative(op->args[0], &op->args[2], &op->args[4]);
1819 swap_commutative(op->args[1], &op->args[3], &op->args[5]);
Richard Henderson1e484e62012-10-02 11:32:22 -07001820 break;
Richard Hendersond7156f72013-02-19 23:51:52 -08001821 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -08001822 CASE_OP_32_64(muls2):
Richard Hendersonacd93702016-12-08 12:28:42 -08001823 swap_commutative(op->args[0], &op->args[2], &op->args[3]);
Richard Henderson14149682012-10-02 11:32:30 -07001824 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -07001825 case INDEX_op_brcond2_i32:
Richard Hendersonacd93702016-12-08 12:28:42 -08001826 if (swap_commutative2(&op->args[0], &op->args[2])) {
1827 op->args[4] = tcg_swap_cond(op->args[4]);
Richard Henderson0bfcb862012-10-02 11:32:23 -07001828 }
1829 break;
1830 case INDEX_op_setcond2_i32:
Richard Hendersonacd93702016-12-08 12:28:42 -08001831 if (swap_commutative2(&op->args[1], &op->args[3])) {
1832 op->args[5] = tcg_swap_cond(op->args[5]);
Richard Henderson0bfcb862012-10-02 11:32:23 -07001833 }
1834 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001835 default:
1836 break;
1837 }
1838
Richard Hendersonfae450b2021-08-25 22:42:19 -07001839 /* Assume all bits affected, and no bits known zero. */
1840 ctx.a_mask = -1;
1841 ctx.z_mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -08001842
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001843 /*
1844 * Process each opcode.
1845 * Sorted alphabetically by opcode as much as possible.
1846 */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001847 switch (opc) {
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001848 CASE_OP_32_64_VEC(add):
1849 done = fold_add(&ctx, op);
1850 break;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001851 case INDEX_op_add2_i32:
1852 done = fold_add2_i32(&ctx, op);
1853 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001854 CASE_OP_32_64_VEC(and):
1855 done = fold_and(&ctx, op);
1856 break;
1857 CASE_OP_32_64_VEC(andc):
1858 done = fold_andc(&ctx, op);
1859 break;
Richard Henderson079b0802021-08-24 09:30:59 -07001860 CASE_OP_32_64(brcond):
1861 done = fold_brcond(&ctx, op);
1862 break;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001863 case INDEX_op_brcond2_i32:
1864 done = fold_brcond2(&ctx, op);
1865 break;
Richard Henderson09bacdc2021-08-24 11:58:12 -07001866 CASE_OP_32_64(bswap16):
1867 CASE_OP_32_64(bswap32):
1868 case INDEX_op_bswap64_i64:
1869 done = fold_bswap(&ctx, op);
1870 break;
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001871 CASE_OP_32_64(clz):
1872 CASE_OP_32_64(ctz):
1873 done = fold_count_zeros(&ctx, op);
1874 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001875 CASE_OP_32_64(ctpop):
1876 done = fold_ctpop(&ctx, op);
1877 break;
Richard Henderson1b1907b2021-08-24 10:47:04 -07001878 CASE_OP_32_64(deposit):
1879 done = fold_deposit(&ctx, op);
1880 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001881 CASE_OP_32_64(div):
1882 CASE_OP_32_64(divu):
1883 done = fold_divide(&ctx, op);
1884 break;
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07001885 case INDEX_op_dup_vec:
1886 done = fold_dup(&ctx, op);
1887 break;
1888 case INDEX_op_dup2_vec:
1889 done = fold_dup2(&ctx, op);
1890 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001891 CASE_OP_32_64(eqv):
1892 done = fold_eqv(&ctx, op);
1893 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07001894 CASE_OP_32_64(extract):
1895 done = fold_extract(&ctx, op);
1896 break;
Richard Hendersondcd08992021-08-24 10:41:39 -07001897 CASE_OP_32_64(extract2):
1898 done = fold_extract2(&ctx, op);
1899 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001900 CASE_OP_32_64(ext8s):
1901 CASE_OP_32_64(ext16s):
1902 case INDEX_op_ext32s_i64:
1903 case INDEX_op_ext_i32_i64:
1904 done = fold_exts(&ctx, op);
1905 break;
1906 CASE_OP_32_64(ext8u):
1907 CASE_OP_32_64(ext16u):
1908 case INDEX_op_ext32u_i64:
1909 case INDEX_op_extu_i32_i64:
1910 case INDEX_op_extrl_i64_i32:
1911 case INDEX_op_extrh_i64_i32:
1912 done = fold_extu(&ctx, op);
1913 break;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001914 CASE_OP_32_64(ld8u):
1915 CASE_OP_32_64(ld16u):
1916 case INDEX_op_ld32u_i64:
1917 done = fold_tcg_ld(&ctx, op);
1918 break;
Richard Henderson3eefdf22021-08-25 11:06:43 -07001919 case INDEX_op_mb:
1920 done = fold_mb(&ctx, op);
1921 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001922 CASE_OP_32_64_VEC(mov):
1923 done = fold_mov(&ctx, op);
1924 break;
Richard Henderson0c310a32021-08-24 10:37:24 -07001925 CASE_OP_32_64(movcond):
1926 done = fold_movcond(&ctx, op);
1927 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001928 CASE_OP_32_64(mul):
1929 done = fold_mul(&ctx, op);
1930 break;
1931 CASE_OP_32_64(mulsh):
1932 CASE_OP_32_64(muluh):
1933 done = fold_mul_highpart(&ctx, op);
1934 break;
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001935 case INDEX_op_mulu2_i32:
1936 done = fold_mulu2_i32(&ctx, op);
1937 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001938 CASE_OP_32_64(nand):
1939 done = fold_nand(&ctx, op);
1940 break;
1941 CASE_OP_32_64(neg):
1942 done = fold_neg(&ctx, op);
1943 break;
1944 CASE_OP_32_64(nor):
1945 done = fold_nor(&ctx, op);
1946 break;
1947 CASE_OP_32_64_VEC(not):
1948 done = fold_not(&ctx, op);
1949 break;
1950 CASE_OP_32_64_VEC(or):
1951 done = fold_or(&ctx, op);
1952 break;
1953 CASE_OP_32_64_VEC(orc):
1954 done = fold_orc(&ctx, op);
1955 break;
Richard Henderson3eefdf22021-08-25 11:06:43 -07001956 case INDEX_op_qemu_ld_i32:
1957 case INDEX_op_qemu_ld_i64:
1958 done = fold_qemu_ld(&ctx, op);
1959 break;
1960 case INDEX_op_qemu_st_i32:
1961 case INDEX_op_qemu_st8_i32:
1962 case INDEX_op_qemu_st_i64:
1963 done = fold_qemu_st(&ctx, op);
1964 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001965 CASE_OP_32_64(rem):
1966 CASE_OP_32_64(remu):
1967 done = fold_remainder(&ctx, op);
1968 break;
1969 CASE_OP_32_64(rotl):
1970 CASE_OP_32_64(rotr):
1971 CASE_OP_32_64(sar):
1972 CASE_OP_32_64(shl):
1973 CASE_OP_32_64(shr):
1974 done = fold_shift(&ctx, op);
1975 break;
Richard Hendersonc63ff552021-08-24 09:35:30 -07001976 CASE_OP_32_64(setcond):
1977 done = fold_setcond(&ctx, op);
1978 break;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001979 case INDEX_op_setcond2_i32:
1980 done = fold_setcond2(&ctx, op);
1981 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07001982 CASE_OP_32_64(sextract):
1983 done = fold_sextract(&ctx, op);
1984 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001985 CASE_OP_32_64_VEC(sub):
1986 done = fold_sub(&ctx, op);
1987 break;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001988 case INDEX_op_sub2_i32:
1989 done = fold_sub2_i32(&ctx, op);
1990 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001991 CASE_OP_32_64_VEC(xor):
1992 done = fold_xor(&ctx, op);
Richard Hendersonb10f3832021-08-23 22:30:17 -07001993 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001994 default:
1995 break;
Richard Hendersonb10f3832021-08-23 22:30:17 -07001996 }
1997
Richard Henderson404a1482021-08-24 11:08:21 -07001998 if (!done) {
1999 finish_folding(&ctx, op);
2000 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002001 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002002}