blob: d8e437c8262ff73436c7b41e390675f470de175d [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"
Richard Henderson9531c072021-08-26 06:51:39 -070027#include "qemu/int128.h"
Richard Hendersonab84dc32023-08-23 23:04:24 -070028#include "qemu/interval-tree.h"
Richard Hendersonad3d0e42023-03-28 18:17:24 -070029#include "tcg/tcg-op-common.h"
Richard Henderson90163902021-03-18 10:21:45 -060030#include "tcg-internal.h"
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040031
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040032#define CASE_OP_32_64(x) \
33 glue(glue(case INDEX_op_, x), _i32): \
34 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040035
Richard Henderson170ba882017-11-22 09:07:11 +010036#define CASE_OP_32_64_VEC(x) \
37 glue(glue(case INDEX_op_, x), _i32): \
38 glue(glue(case INDEX_op_, x), _i64): \
39 glue(glue(case INDEX_op_, x), _vec)
40
Richard Hendersonab84dc32023-08-23 23:04:24 -070041typedef struct MemCopyInfo {
42 IntervalTreeNode itree;
43 QSIMPLEQ_ENTRY (MemCopyInfo) next;
44 TCGTemp *ts;
45 TCGType type;
46} MemCopyInfo;
47
Richard Henderson6fcb98e2020-03-30 17:44:30 -070048typedef struct TempOptInfo {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020049 bool is_const;
Richard Henderson63490392017-06-20 13:43:15 -070050 TCGTemp *prev_copy;
51 TCGTemp *next_copy;
Richard Hendersonab84dc32023-08-23 23:04:24 -070052 QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy;
Richard Henderson54795542020-09-06 16:21:32 -070053 uint64_t val;
Richard Hendersonb1fde412021-08-23 13:07:49 -070054 uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */
Richard Henderson57fe5c62021-08-26 12:04:46 -070055 uint64_t s_mask; /* a left-aligned mask of clrsb(value) bits. */
Richard Henderson6fcb98e2020-03-30 17:44:30 -070056} TempOptInfo;
Kirill Batuzov22613af2011-07-07 16:37:13 +040057
Richard Henderson3b3f8472021-08-23 22:06:31 -070058typedef struct OptContext {
Richard Hendersondc849882021-08-24 07:13:45 -070059 TCGContext *tcg;
Richard Hendersond0ed5152021-08-24 07:38:39 -070060 TCGOp *prev_mb;
Richard Henderson3b3f8472021-08-23 22:06:31 -070061 TCGTempSet temps_used;
Richard Henderson137f1f42021-08-24 08:49:25 -070062
Richard Hendersonab84dc32023-08-23 23:04:24 -070063 IntervalTreeRoot mem_copy;
64 QSIMPLEQ_HEAD(, MemCopyInfo) mem_free;
65
Richard Henderson137f1f42021-08-24 08:49:25 -070066 /* In flight values from optimization. */
Richard Hendersonfae450b2021-08-25 22:42:19 -070067 uint64_t a_mask; /* mask bit is 0 iff value identical to first input */
68 uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */
Richard Henderson57fe5c62021-08-26 12:04:46 -070069 uint64_t s_mask; /* mask of clrsb(value) bits */
Richard Henderson67f84c92021-08-25 08:00:20 -070070 TCGType type;
Richard Henderson3b3f8472021-08-23 22:06:31 -070071} OptContext;
72
Richard Henderson57fe5c62021-08-26 12:04:46 -070073/* Calculate the smask for a specific value. */
74static uint64_t smask_from_value(uint64_t value)
75{
76 int rep = clrsb64(value);
77 return ~(~0ull >> rep);
78}
79
80/*
81 * Calculate the smask for a given set of known-zeros.
82 * If there are lots of zeros on the left, we can consider the remainder
83 * an unsigned field, and thus the corresponding signed field is one bit
84 * larger.
85 */
86static uint64_t smask_from_zmask(uint64_t zmask)
87{
88 /*
89 * Only the 0 bits are significant for zmask, thus the msb itself
90 * must be zero, else we have no sign information.
91 */
92 int rep = clz64(zmask);
93 if (rep == 0) {
94 return 0;
95 }
96 rep -= 1;
97 return ~(~0ull >> rep);
98}
99
Richard Henderson93a967f2021-08-26 13:24:59 -0700100/*
101 * Recreate a properly left-aligned smask after manipulation.
102 * Some bit-shuffling, particularly shifts and rotates, may
103 * retain sign bits on the left, but may scatter disconnected
104 * sign bits on the right. Retain only what remains to the left.
105 */
106static uint64_t smask_from_smask(int64_t smask)
107{
108 /* Only the 1 bits are significant for smask */
109 return smask_from_zmask(~smask);
110}
111
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700112static inline TempOptInfo *ts_info(TCGTemp *ts)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200113{
Richard Henderson63490392017-06-20 13:43:15 -0700114 return ts->state_ptr;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200115}
116
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700117static inline TempOptInfo *arg_info(TCGArg arg)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200118{
Richard Henderson63490392017-06-20 13:43:15 -0700119 return ts_info(arg_temp(arg));
120}
121
122static inline bool ts_is_const(TCGTemp *ts)
123{
124 return ts_info(ts)->is_const;
125}
126
127static inline bool arg_is_const(TCGArg arg)
128{
129 return ts_is_const(arg_temp(arg));
130}
131
132static inline bool ts_is_copy(TCGTemp *ts)
133{
134 return ts_info(ts)->next_copy != ts;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200135}
136
Richard Henderson9f75e522023-11-02 13:37:46 -0700137static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b)
138{
139 return a->kind < b->kind ? b : a;
140}
141
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200142/* Initialize and activate a temporary. */
Richard Henderson3b3f8472021-08-23 22:06:31 -0700143static void init_ts_info(OptContext *ctx, TCGTemp *ts)
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200144{
Richard Henderson63490392017-06-20 13:43:15 -0700145 size_t idx = temp_idx(ts);
Richard Henderson8f17a972020-03-30 19:52:02 -0700146 TempOptInfo *ti;
Richard Henderson63490392017-06-20 13:43:15 -0700147
Richard Henderson3b3f8472021-08-23 22:06:31 -0700148 if (test_bit(idx, ctx->temps_used.l)) {
Richard Henderson8f17a972020-03-30 19:52:02 -0700149 return;
150 }
Richard Henderson3b3f8472021-08-23 22:06:31 -0700151 set_bit(idx, ctx->temps_used.l);
Richard Henderson8f17a972020-03-30 19:52:02 -0700152
153 ti = ts->state_ptr;
154 if (ti == NULL) {
155 ti = tcg_malloc(sizeof(TempOptInfo));
Richard Henderson63490392017-06-20 13:43:15 -0700156 ts->state_ptr = ti;
Richard Henderson8f17a972020-03-30 19:52:02 -0700157 }
158
159 ti->next_copy = ts;
160 ti->prev_copy = ts;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700161 QSIMPLEQ_INIT(&ti->mem_copy);
Richard Henderson8f17a972020-03-30 19:52:02 -0700162 if (ts->kind == TEMP_CONST) {
163 ti->is_const = true;
164 ti->val = ts->val;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700165 ti->z_mask = ts->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700166 ti->s_mask = smask_from_value(ts->val);
Richard Henderson8f17a972020-03-30 19:52:02 -0700167 } else {
168 ti->is_const = false;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700169 ti->z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700170 ti->s_mask = 0;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200171 }
172}
173
Richard Hendersonab84dc32023-08-23 23:04:24 -0700174static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l)
175{
176 IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l);
177 return r ? container_of(r, MemCopyInfo, itree) : NULL;
178}
179
180static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l)
181{
182 IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l);
183 return r ? container_of(r, MemCopyInfo, itree) : NULL;
184}
185
186static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc)
187{
188 TCGTemp *ts = mc->ts;
189 TempOptInfo *ti = ts_info(ts);
190
191 interval_tree_remove(&mc->itree, &ctx->mem_copy);
192 QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next);
193 QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next);
194}
195
196static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l)
197{
198 while (true) {
199 MemCopyInfo *mc = mem_copy_first(ctx, s, l);
200 if (!mc) {
201 break;
202 }
203 remove_mem_copy(ctx, mc);
204 }
205}
206
207static void remove_mem_copy_all(OptContext *ctx)
208{
209 remove_mem_copy_in(ctx, 0, -1);
210 tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy));
211}
212
Richard Henderson9f75e522023-11-02 13:37:46 -0700213static TCGTemp *find_better_copy(TCGTemp *ts)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200214{
Richard Henderson9f75e522023-11-02 13:37:46 -0700215 TCGTemp *i, *ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200216
Richard Henderson4c868ce2020-04-23 09:02:23 -0700217 /* If this is already readonly, we can't do better. */
218 if (temp_readonly(ts)) {
Richard Henderson63490392017-06-20 13:43:15 -0700219 return ts;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200220 }
221
Richard Henderson9f75e522023-11-02 13:37:46 -0700222 ret = ts;
Richard Henderson63490392017-06-20 13:43:15 -0700223 for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700224 ret = cmp_better_copy(ret, i);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200225 }
Richard Henderson9f75e522023-11-02 13:37:46 -0700226 return ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200227}
228
Richard Hendersonab84dc32023-08-23 23:04:24 -0700229static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts)
230{
231 TempOptInfo *si = ts_info(src_ts);
232 TempOptInfo *di = ts_info(dst_ts);
233 MemCopyInfo *mc;
234
235 QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) {
236 tcg_debug_assert(mc->ts == src_ts);
237 mc->ts = dst_ts;
238 }
239 QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy);
240}
241
242/* Reset TEMP's state, possibly removing the temp for the list of copies. */
243static void reset_ts(OptContext *ctx, TCGTemp *ts)
244{
245 TempOptInfo *ti = ts_info(ts);
246 TCGTemp *pts = ti->prev_copy;
247 TCGTemp *nts = ti->next_copy;
248 TempOptInfo *pi = ts_info(pts);
249 TempOptInfo *ni = ts_info(nts);
250
251 ni->prev_copy = ti->prev_copy;
252 pi->next_copy = ti->next_copy;
253 ti->next_copy = ts;
254 ti->prev_copy = ts;
255 ti->is_const = false;
256 ti->z_mask = -1;
257 ti->s_mask = 0;
258
259 if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) {
260 if (ts == nts) {
261 /* Last temp copy being removed, the mem copies die. */
262 MemCopyInfo *mc;
263 QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) {
264 interval_tree_remove(&mc->itree, &ctx->mem_copy);
265 }
266 QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy);
267 } else {
268 move_mem_copies(find_better_copy(nts), ts);
269 }
270 }
271}
272
273static void reset_temp(OptContext *ctx, TCGArg arg)
274{
275 reset_ts(ctx, arg_temp(arg));
276}
277
278static void record_mem_copy(OptContext *ctx, TCGType type,
279 TCGTemp *ts, intptr_t start, intptr_t last)
280{
281 MemCopyInfo *mc;
282 TempOptInfo *ti;
283
284 mc = QSIMPLEQ_FIRST(&ctx->mem_free);
285 if (mc) {
286 QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next);
287 } else {
288 mc = tcg_malloc(sizeof(*mc));
289 }
290
291 memset(mc, 0, sizeof(*mc));
292 mc->itree.start = start;
293 mc->itree.last = last;
294 mc->type = type;
295 interval_tree_insert(&mc->itree, &ctx->mem_copy);
296
297 ts = find_better_copy(ts);
298 ti = ts_info(ts);
299 mc->ts = ts;
300 QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next);
301}
302
Richard Henderson63490392017-06-20 13:43:15 -0700303static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200304{
Richard Henderson63490392017-06-20 13:43:15 -0700305 TCGTemp *i;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200306
Richard Henderson63490392017-06-20 13:43:15 -0700307 if (ts1 == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200308 return true;
309 }
310
Richard Henderson63490392017-06-20 13:43:15 -0700311 if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200312 return false;
313 }
314
Richard Henderson63490392017-06-20 13:43:15 -0700315 for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
316 if (i == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200317 return true;
318 }
319 }
320
321 return false;
322}
323
Richard Henderson63490392017-06-20 13:43:15 -0700324static bool args_are_copies(TCGArg arg1, TCGArg arg2)
325{
326 return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
327}
328
Richard Hendersonab84dc32023-08-23 23:04:24 -0700329static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s)
330{
331 MemCopyInfo *mc;
332
333 for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) {
334 if (mc->itree.start == s && mc->type == type) {
335 return find_better_copy(mc->ts);
336 }
337 }
338 return NULL;
339}
340
Richard Henderson26aac972023-10-23 12:31:57 -0700341static TCGArg arg_new_constant(OptContext *ctx, uint64_t val)
342{
343 TCGType type = ctx->type;
344 TCGTemp *ts;
345
346 if (type == TCG_TYPE_I32) {
347 val = (int32_t)val;
348 }
349
350 ts = tcg_constant_internal(type, val);
351 init_ts_info(ctx, ts);
352
353 return temp_arg(ts);
354}
355
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700356static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400357{
Richard Henderson63490392017-06-20 13:43:15 -0700358 TCGTemp *dst_ts = arg_temp(dst);
359 TCGTemp *src_ts = arg_temp(src);
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700360 TempOptInfo *di;
361 TempOptInfo *si;
Richard Henderson63490392017-06-20 13:43:15 -0700362 TCGOpcode new_op;
363
364 if (ts_are_copies(dst_ts, src_ts)) {
Richard Hendersondc849882021-08-24 07:13:45 -0700365 tcg_op_remove(ctx->tcg, op);
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700366 return true;
Aurelien Jarno53657182015-06-04 21:53:25 +0200367 }
368
Richard Henderson986cac12023-01-09 13:59:35 -0800369 reset_ts(ctx, dst_ts);
Richard Henderson63490392017-06-20 13:43:15 -0700370 di = ts_info(dst_ts);
371 si = ts_info(src_ts);
Richard Henderson67f84c92021-08-25 08:00:20 -0700372
373 switch (ctx->type) {
374 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100375 new_op = INDEX_op_mov_i32;
Richard Henderson67f84c92021-08-25 08:00:20 -0700376 break;
377 case TCG_TYPE_I64:
378 new_op = INDEX_op_mov_i64;
379 break;
380 case TCG_TYPE_V64:
381 case TCG_TYPE_V128:
382 case TCG_TYPE_V256:
383 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
384 new_op = INDEX_op_mov_vec;
385 break;
386 default:
387 g_assert_not_reached();
Richard Henderson170ba882017-11-22 09:07:11 +0100388 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700389 op->opc = new_op;
Richard Henderson63490392017-06-20 13:43:15 -0700390 op->args[0] = dst;
391 op->args[1] = src;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700392
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700393 di->z_mask = si->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700394 di->s_mask = si->s_mask;
Richard Henderson24666ba2014-05-22 11:14:10 -0700395
Richard Henderson63490392017-06-20 13:43:15 -0700396 if (src_ts->type == dst_ts->type) {
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700397 TempOptInfo *ni = ts_info(si->next_copy);
Richard Henderson63490392017-06-20 13:43:15 -0700398
399 di->next_copy = si->next_copy;
400 di->prev_copy = src_ts;
401 ni->prev_copy = dst_ts;
402 si->next_copy = dst_ts;
403 di->is_const = si->is_const;
404 di->val = si->val;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700405
406 if (!QSIMPLEQ_EMPTY(&si->mem_copy)
407 && cmp_better_copy(src_ts, dst_ts) == dst_ts) {
408 move_mem_copies(dst_ts, src_ts);
409 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800410 }
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700411 return true;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400412}
413
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700414static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
Richard Hendersondc849882021-08-24 07:13:45 -0700415 TCGArg dst, uint64_t val)
Richard Henderson8fe35e02020-03-30 20:42:43 -0700416{
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700417 /* Convert movi to mov with constant temp. */
Richard Henderson26aac972023-10-23 12:31:57 -0700418 return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val));
Richard Henderson8fe35e02020-03-30 20:42:43 -0700419}
420
Richard Henderson54795542020-09-06 16:21:32 -0700421static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400422{
Richard Henderson03271522013-08-14 14:35:56 -0700423 uint64_t l64, h64;
424
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400425 switch (op) {
426 CASE_OP_32_64(add):
427 return x + y;
428
429 CASE_OP_32_64(sub):
430 return x - y;
431
432 CASE_OP_32_64(mul):
433 return x * y;
434
Richard Hendersonc578ff12021-12-16 06:07:25 -0800435 CASE_OP_32_64_VEC(and):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400436 return x & y;
437
Richard Hendersonc578ff12021-12-16 06:07:25 -0800438 CASE_OP_32_64_VEC(or):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400439 return x | y;
440
Richard Hendersonc578ff12021-12-16 06:07:25 -0800441 CASE_OP_32_64_VEC(xor):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400442 return x ^ y;
443
Kirill Batuzov55c09752011-07-07 16:37:16 +0400444 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700445 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400446
Kirill Batuzov55c09752011-07-07 16:37:16 +0400447 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700448 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400449
450 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700451 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400452
Kirill Batuzov55c09752011-07-07 16:37:16 +0400453 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700454 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400455
456 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700457 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400458
Kirill Batuzov55c09752011-07-07 16:37:16 +0400459 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700460 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400461
462 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700463 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400464
Kirill Batuzov55c09752011-07-07 16:37:16 +0400465 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700466 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400467
468 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700469 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400470
Kirill Batuzov55c09752011-07-07 16:37:16 +0400471 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700472 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400473
Richard Hendersonc578ff12021-12-16 06:07:25 -0800474 CASE_OP_32_64_VEC(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400475 return ~x;
476
Richard Hendersoncb25c802011-08-17 14:11:47 -0700477 CASE_OP_32_64(neg):
478 return -x;
479
Richard Hendersonc578ff12021-12-16 06:07:25 -0800480 CASE_OP_32_64_VEC(andc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700481 return x & ~y;
482
Richard Hendersonc578ff12021-12-16 06:07:25 -0800483 CASE_OP_32_64_VEC(orc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700484 return x | ~y;
485
Richard Hendersoned523472021-12-16 11:17:46 -0800486 CASE_OP_32_64_VEC(eqv):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700487 return ~(x ^ y);
488
Richard Hendersoned523472021-12-16 11:17:46 -0800489 CASE_OP_32_64_VEC(nand):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700490 return ~(x & y);
491
Richard Hendersoned523472021-12-16 11:17:46 -0800492 CASE_OP_32_64_VEC(nor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700493 return ~(x | y);
494
Richard Henderson0e28d002016-11-16 09:23:28 +0100495 case INDEX_op_clz_i32:
496 return (uint32_t)x ? clz32(x) : y;
497
498 case INDEX_op_clz_i64:
499 return x ? clz64(x) : y;
500
501 case INDEX_op_ctz_i32:
502 return (uint32_t)x ? ctz32(x) : y;
503
504 case INDEX_op_ctz_i64:
505 return x ? ctz64(x) : y;
506
Richard Hendersona768e4e2016-11-21 11:13:39 +0100507 case INDEX_op_ctpop_i32:
508 return ctpop32(x);
509
510 case INDEX_op_ctpop_i64:
511 return ctpop64(x);
512
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700513 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400514 return (int8_t)x;
515
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700516 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400517 return (int16_t)x;
518
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700519 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400520 return (uint8_t)x;
521
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700522 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400523 return (uint16_t)x;
524
Richard Henderson64985942018-11-20 08:53:34 +0100525 CASE_OP_32_64(bswap16):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700526 x = bswap16(x);
527 return y & TCG_BSWAP_OS ? (int16_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100528
529 CASE_OP_32_64(bswap32):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700530 x = bswap32(x);
531 return y & TCG_BSWAP_OS ? (int32_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100532
533 case INDEX_op_bswap64_i64:
534 return bswap64(x);
535
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200536 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400537 case INDEX_op_ext32s_i64:
538 return (int32_t)x;
539
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200540 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700541 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400542 case INDEX_op_ext32u_i64:
543 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400544
Richard Henderson609ad702015-07-24 07:16:00 -0700545 case INDEX_op_extrh_i64_i32:
546 return (uint64_t)x >> 32;
547
Richard Henderson03271522013-08-14 14:35:56 -0700548 case INDEX_op_muluh_i32:
549 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
550 case INDEX_op_mulsh_i32:
551 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
552
553 case INDEX_op_muluh_i64:
554 mulu64(&l64, &h64, x, y);
555 return h64;
556 case INDEX_op_mulsh_i64:
557 muls64(&l64, &h64, x, y);
558 return h64;
559
Richard Henderson01547f72013-08-14 15:22:46 -0700560 case INDEX_op_div_i32:
561 /* Avoid crashing on divide by zero, otherwise undefined. */
562 return (int32_t)x / ((int32_t)y ? : 1);
563 case INDEX_op_divu_i32:
564 return (uint32_t)x / ((uint32_t)y ? : 1);
565 case INDEX_op_div_i64:
566 return (int64_t)x / ((int64_t)y ? : 1);
567 case INDEX_op_divu_i64:
568 return (uint64_t)x / ((uint64_t)y ? : 1);
569
570 case INDEX_op_rem_i32:
571 return (int32_t)x % ((int32_t)y ? : 1);
572 case INDEX_op_remu_i32:
573 return (uint32_t)x % ((uint32_t)y ? : 1);
574 case INDEX_op_rem_i64:
575 return (int64_t)x % ((int64_t)y ? : 1);
576 case INDEX_op_remu_i64:
577 return (uint64_t)x % ((uint64_t)y ? : 1);
578
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400579 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700580 g_assert_not_reached();
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400581 }
582}
583
Richard Henderson67f84c92021-08-25 08:00:20 -0700584static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
585 uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400586{
Richard Henderson54795542020-09-06 16:21:32 -0700587 uint64_t res = do_constant_folding_2(op, x, y);
Richard Henderson67f84c92021-08-25 08:00:20 -0700588 if (type == TCG_TYPE_I32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200589 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400590 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400591 return res;
592}
593
Richard Henderson9519da72012-10-02 11:32:26 -0700594static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
595{
596 switch (c) {
597 case TCG_COND_EQ:
598 return x == y;
599 case TCG_COND_NE:
600 return x != y;
601 case TCG_COND_LT:
602 return (int32_t)x < (int32_t)y;
603 case TCG_COND_GE:
604 return (int32_t)x >= (int32_t)y;
605 case TCG_COND_LE:
606 return (int32_t)x <= (int32_t)y;
607 case TCG_COND_GT:
608 return (int32_t)x > (int32_t)y;
609 case TCG_COND_LTU:
610 return x < y;
611 case TCG_COND_GEU:
612 return x >= y;
613 case TCG_COND_LEU:
614 return x <= y;
615 case TCG_COND_GTU:
616 return x > y;
617 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700618 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700619 }
620}
621
622static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
623{
624 switch (c) {
625 case TCG_COND_EQ:
626 return x == y;
627 case TCG_COND_NE:
628 return x != y;
629 case TCG_COND_LT:
630 return (int64_t)x < (int64_t)y;
631 case TCG_COND_GE:
632 return (int64_t)x >= (int64_t)y;
633 case TCG_COND_LE:
634 return (int64_t)x <= (int64_t)y;
635 case TCG_COND_GT:
636 return (int64_t)x > (int64_t)y;
637 case TCG_COND_LTU:
638 return x < y;
639 case TCG_COND_GEU:
640 return x >= y;
641 case TCG_COND_LEU:
642 return x <= y;
643 case TCG_COND_GTU:
644 return x > y;
645 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700646 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700647 }
648}
649
650static bool do_constant_folding_cond_eq(TCGCond c)
651{
652 switch (c) {
653 case TCG_COND_GT:
654 case TCG_COND_LTU:
655 case TCG_COND_LT:
656 case TCG_COND_GTU:
657 case TCG_COND_NE:
658 return 0;
659 case TCG_COND_GE:
660 case TCG_COND_GEU:
661 case TCG_COND_LE:
662 case TCG_COND_LEU:
663 case TCG_COND_EQ:
664 return 1;
665 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700666 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700667 }
668}
669
Richard Henderson8d57bf12021-08-24 08:34:27 -0700670/*
671 * Return -1 if the condition can't be simplified,
672 * and the result of the condition (0 or 1) if it can.
673 */
Richard Henderson67f84c92021-08-25 08:00:20 -0700674static int do_constant_folding_cond(TCGType type, TCGArg x,
Richard Henderson8d57bf12021-08-24 08:34:27 -0700675 TCGArg y, TCGCond c)
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200676{
Richard Henderson63490392017-06-20 13:43:15 -0700677 if (arg_is_const(x) && arg_is_const(y)) {
Alex Bennée9becc362022-02-09 11:21:42 +0000678 uint64_t xv = arg_info(x)->val;
679 uint64_t yv = arg_info(y)->val;
680
Richard Henderson67f84c92021-08-25 08:00:20 -0700681 switch (type) {
682 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100683 return do_constant_folding_cond_32(xv, yv, c);
Richard Henderson67f84c92021-08-25 08:00:20 -0700684 case TCG_TYPE_I64:
685 return do_constant_folding_cond_64(xv, yv, c);
686 default:
687 /* Only scalar comparisons are optimizable */
688 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200689 }
Richard Henderson63490392017-06-20 13:43:15 -0700690 } else if (args_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700691 return do_constant_folding_cond_eq(c);
Alex Bennée9becc362022-02-09 11:21:42 +0000692 } else if (arg_is_const(y) && arg_info(y)->val == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200693 switch (c) {
694 case TCG_COND_LTU:
695 return 0;
696 case TCG_COND_GEU:
697 return 1;
698 default:
Richard Henderson8d57bf12021-08-24 08:34:27 -0700699 return -1;
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200700 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200701 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700702 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200703}
704
Richard Henderson8d57bf12021-08-24 08:34:27 -0700705/*
706 * Return -1 if the condition can't be simplified,
707 * and the result of the condition (0 or 1) if it can.
708 */
709static int do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
Richard Henderson6c4382f2012-10-02 11:32:27 -0700710{
711 TCGArg al = p1[0], ah = p1[1];
712 TCGArg bl = p2[0], bh = p2[1];
713
Richard Henderson63490392017-06-20 13:43:15 -0700714 if (arg_is_const(bl) && arg_is_const(bh)) {
715 tcg_target_ulong blv = arg_info(bl)->val;
716 tcg_target_ulong bhv = arg_info(bh)->val;
717 uint64_t b = deposit64(blv, 32, 32, bhv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700718
Richard Henderson63490392017-06-20 13:43:15 -0700719 if (arg_is_const(al) && arg_is_const(ah)) {
720 tcg_target_ulong alv = arg_info(al)->val;
721 tcg_target_ulong ahv = arg_info(ah)->val;
722 uint64_t a = deposit64(alv, 32, 32, ahv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700723 return do_constant_folding_cond_64(a, b, c);
724 }
725 if (b == 0) {
726 switch (c) {
727 case TCG_COND_LTU:
728 return 0;
729 case TCG_COND_GEU:
730 return 1;
731 default:
732 break;
733 }
734 }
735 }
Richard Henderson63490392017-06-20 13:43:15 -0700736 if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700737 return do_constant_folding_cond_eq(c);
738 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700739 return -1;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700740}
741
Richard Henderson7a2f7082021-08-26 07:06:39 -0700742/**
743 * swap_commutative:
744 * @dest: TCGArg of the destination argument, or NO_DEST.
745 * @p1: first paired argument
746 * @p2: second paired argument
747 *
748 * If *@p1 is a constant and *@p2 is not, swap.
749 * If *@p2 matches @dest, swap.
750 * Return true if a swap was performed.
751 */
752
753#define NO_DEST temp_arg(NULL)
754
Richard Henderson24c9ae42012-10-02 11:32:21 -0700755static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
756{
757 TCGArg a1 = *p1, a2 = *p2;
758 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700759 sum += arg_is_const(a1);
760 sum -= arg_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700761
762 /* Prefer the constant in second argument, and then the form
763 op a, a, b, which is better handled on non-RISC hosts. */
764 if (sum > 0 || (sum == 0 && dest == a2)) {
765 *p1 = a2;
766 *p2 = a1;
767 return true;
768 }
769 return false;
770}
771
Richard Henderson0bfcb862012-10-02 11:32:23 -0700772static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
773{
774 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700775 sum += arg_is_const(p1[0]);
776 sum += arg_is_const(p1[1]);
777 sum -= arg_is_const(p2[0]);
778 sum -= arg_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700779 if (sum > 0) {
780 TCGArg t;
781 t = p1[0], p1[0] = p2[0], p2[0] = t;
782 t = p1[1], p1[1] = p2[1], p2[1] = t;
783 return true;
784 }
785 return false;
786}
787
Richard Hendersone2577ea2021-08-24 08:00:48 -0700788static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
789{
790 for (int i = 0; i < nb_args; i++) {
791 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000792 init_ts_info(ctx, ts);
Richard Hendersone2577ea2021-08-24 08:00:48 -0700793 }
794}
795
Richard Henderson8774dde2021-08-24 08:04:47 -0700796static void copy_propagate(OptContext *ctx, TCGOp *op,
797 int nb_oargs, int nb_iargs)
798{
Richard Henderson8774dde2021-08-24 08:04:47 -0700799 for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
800 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000801 if (ts_is_copy(ts)) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700802 op->args[i] = temp_arg(find_better_copy(ts));
Richard Henderson8774dde2021-08-24 08:04:47 -0700803 }
804 }
805}
806
Richard Henderson137f1f42021-08-24 08:49:25 -0700807static void finish_folding(OptContext *ctx, TCGOp *op)
808{
809 const TCGOpDef *def = &tcg_op_defs[op->opc];
810 int i, nb_oargs;
811
812 /*
Richard Hendersond97f8f32023-10-16 19:10:42 -0700813 * We only optimize extended basic blocks. If the opcode ends a BB
814 * and is not a conditional branch, reset all temp data.
Richard Henderson137f1f42021-08-24 08:49:25 -0700815 */
816 if (def->flags & TCG_OPF_BB_END) {
Richard Henderson137f1f42021-08-24 08:49:25 -0700817 ctx->prev_mb = NULL;
Richard Hendersond97f8f32023-10-16 19:10:42 -0700818 if (!(def->flags & TCG_OPF_COND_BRANCH)) {
819 memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
Richard Hendersonab84dc32023-08-23 23:04:24 -0700820 remove_mem_copy_all(ctx);
Richard Hendersond97f8f32023-10-16 19:10:42 -0700821 }
Richard Henderson137f1f42021-08-24 08:49:25 -0700822 return;
823 }
824
825 nb_oargs = def->nb_oargs;
826 for (i = 0; i < nb_oargs; i++) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700827 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson986cac12023-01-09 13:59:35 -0800828 reset_ts(ctx, ts);
Richard Henderson137f1f42021-08-24 08:49:25 -0700829 /*
Richard Henderson57fe5c62021-08-26 12:04:46 -0700830 * Save the corresponding known-zero/sign bits mask for the
Richard Henderson137f1f42021-08-24 08:49:25 -0700831 * first output argument (only one supported so far).
832 */
833 if (i == 0) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700834 ts_info(ts)->z_mask = ctx->z_mask;
835 ts_info(ts)->s_mask = ctx->s_mask;
Richard Henderson137f1f42021-08-24 08:49:25 -0700836 }
837 }
838}
839
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700840/*
841 * The fold_* functions return true when processing is complete,
842 * usually by folding the operation to a constant or to a copy,
843 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
844 * like collect information about the value produced, for use in
845 * optimizing a subsequent operation.
846 *
847 * These first fold_* functions are all helpers, used by other
848 * folders for more specific operations.
849 */
850
851static bool fold_const1(OptContext *ctx, TCGOp *op)
852{
853 if (arg_is_const(op->args[1])) {
854 uint64_t t;
855
856 t = arg_info(op->args[1])->val;
Richard Henderson67f84c92021-08-25 08:00:20 -0700857 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700858 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
859 }
860 return false;
861}
862
863static bool fold_const2(OptContext *ctx, TCGOp *op)
864{
865 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
866 uint64_t t1 = arg_info(op->args[1])->val;
867 uint64_t t2 = arg_info(op->args[2])->val;
868
Richard Henderson67f84c92021-08-25 08:00:20 -0700869 t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700870 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
871 }
872 return false;
873}
874
Richard Hendersonc578ff12021-12-16 06:07:25 -0800875static bool fold_commutative(OptContext *ctx, TCGOp *op)
876{
877 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
878 return false;
879}
880
Richard Henderson7a2f7082021-08-26 07:06:39 -0700881static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
882{
883 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
884 return fold_const2(ctx, op);
885}
886
Richard Hendersonfae450b2021-08-25 22:42:19 -0700887static bool fold_masks(OptContext *ctx, TCGOp *op)
888{
889 uint64_t a_mask = ctx->a_mask;
890 uint64_t z_mask = ctx->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700891 uint64_t s_mask = ctx->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -0700892
893 /*
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700894 * 32-bit ops generate 32-bit results, which for the purpose of
895 * simplifying tcg are sign-extended. Certainly that's how we
896 * represent our constants elsewhere. Note that the bits will
897 * be reset properly for a 64-bit value when encountering the
898 * type changing opcodes.
Richard Hendersonfae450b2021-08-25 22:42:19 -0700899 */
900 if (ctx->type == TCG_TYPE_I32) {
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700901 a_mask = (int32_t)a_mask;
902 z_mask = (int32_t)z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700903 s_mask |= MAKE_64BIT_MASK(32, 32);
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700904 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700905 ctx->s_mask = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -0700906 }
907
908 if (z_mask == 0) {
909 return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
910 }
911 if (a_mask == 0) {
912 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
913 }
914 return false;
915}
916
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700917/*
918 * Convert @op to NOT, if NOT is supported by the host.
919 * Return true f the conversion is successful, which will still
920 * indicate that the processing is complete.
921 */
922static bool fold_not(OptContext *ctx, TCGOp *op);
923static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
924{
925 TCGOpcode not_op;
926 bool have_not;
927
928 switch (ctx->type) {
929 case TCG_TYPE_I32:
930 not_op = INDEX_op_not_i32;
931 have_not = TCG_TARGET_HAS_not_i32;
932 break;
933 case TCG_TYPE_I64:
934 not_op = INDEX_op_not_i64;
935 have_not = TCG_TARGET_HAS_not_i64;
936 break;
937 case TCG_TYPE_V64:
938 case TCG_TYPE_V128:
939 case TCG_TYPE_V256:
940 not_op = INDEX_op_not_vec;
941 have_not = TCG_TARGET_HAS_not_vec;
942 break;
943 default:
944 g_assert_not_reached();
945 }
946 if (have_not) {
947 op->opc = not_op;
948 op->args[1] = op->args[idx];
949 return fold_not(ctx, op);
950 }
951 return false;
952}
953
Richard Hendersonda48e272021-08-25 20:42:04 -0700954/* If the binary operation has first argument @i, fold to @i. */
955static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
956{
957 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
958 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
959 }
960 return false;
961}
962
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700963/* If the binary operation has first argument @i, fold to NOT. */
964static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
965{
966 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
967 return fold_to_not(ctx, op, 2);
968 }
969 return false;
970}
971
Richard Hendersone8679952021-08-25 13:19:52 -0700972/* If the binary operation has second argument @i, fold to @i. */
973static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
974{
975 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
976 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
977 }
978 return false;
979}
980
Richard Hendersona63ce0e2021-08-25 20:28:53 -0700981/* If the binary operation has second argument @i, fold to identity. */
982static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
983{
984 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
985 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
986 }
987 return false;
988}
989
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700990/* If the binary operation has second argument @i, fold to NOT. */
991static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
992{
993 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
994 return fold_to_not(ctx, op, 1);
995 }
996 return false;
997}
998
Richard Hendersoncbe42fb2021-08-25 13:02:00 -0700999/* If the binary operation has both arguments equal, fold to @i. */
1000static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1001{
1002 if (args_are_copies(op->args[1], op->args[2])) {
1003 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1004 }
1005 return false;
1006}
1007
Richard Hendersonca7bb042021-08-25 13:14:21 -07001008/* If the binary operation has both arguments equal, fold to identity. */
1009static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
1010{
1011 if (args_are_copies(op->args[1], op->args[2])) {
1012 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1013 }
1014 return false;
1015}
1016
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001017/*
1018 * These outermost fold_<op> functions are sorted alphabetically.
Richard Hendersonca7bb042021-08-25 13:14:21 -07001019 *
1020 * The ordering of the transformations should be:
1021 * 1) those that produce a constant
1022 * 2) those that produce a copy
1023 * 3) those that produce information about the result value.
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001024 */
1025
1026static bool fold_add(OptContext *ctx, TCGOp *op)
1027{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001028 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001029 fold_xi_to_x(ctx, op, 0)) {
1030 return true;
1031 }
1032 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001033}
1034
Richard Hendersonc578ff12021-12-16 06:07:25 -08001035/* We cannot as yet do_constant_folding with vectors. */
1036static bool fold_add_vec(OptContext *ctx, TCGOp *op)
1037{
1038 if (fold_commutative(ctx, op) ||
1039 fold_xi_to_x(ctx, op, 0)) {
1040 return true;
1041 }
1042 return false;
1043}
1044
Richard Henderson9531c072021-08-26 06:51:39 -07001045static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001046{
1047 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3]) &&
1048 arg_is_const(op->args[4]) && arg_is_const(op->args[5])) {
Richard Henderson9531c072021-08-26 06:51:39 -07001049 uint64_t al = arg_info(op->args[2])->val;
1050 uint64_t ah = arg_info(op->args[3])->val;
1051 uint64_t bl = arg_info(op->args[4])->val;
1052 uint64_t bh = arg_info(op->args[5])->val;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001053 TCGArg rl, rh;
Richard Henderson9531c072021-08-26 06:51:39 -07001054 TCGOp *op2;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001055
Richard Henderson9531c072021-08-26 06:51:39 -07001056 if (ctx->type == TCG_TYPE_I32) {
1057 uint64_t a = deposit64(al, 32, 32, ah);
1058 uint64_t b = deposit64(bl, 32, 32, bh);
1059
1060 if (add) {
1061 a += b;
1062 } else {
1063 a -= b;
1064 }
1065
1066 al = sextract64(a, 0, 32);
1067 ah = sextract64(a, 32, 32);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001068 } else {
Richard Henderson9531c072021-08-26 06:51:39 -07001069 Int128 a = int128_make128(al, ah);
1070 Int128 b = int128_make128(bl, bh);
1071
1072 if (add) {
1073 a = int128_add(a, b);
1074 } else {
1075 a = int128_sub(a, b);
1076 }
1077
1078 al = int128_getlo(a);
1079 ah = int128_gethi(a);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001080 }
1081
1082 rl = op->args[0];
1083 rh = op->args[1];
Richard Henderson9531c072021-08-26 06:51:39 -07001084
1085 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01001086 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson9531c072021-08-26 06:51:39 -07001087
1088 tcg_opt_gen_movi(ctx, op, rl, al);
1089 tcg_opt_gen_movi(ctx, op2, rh, ah);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001090 return true;
1091 }
1092 return false;
1093}
1094
Richard Henderson9531c072021-08-26 06:51:39 -07001095static bool fold_add2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001096{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001097 /* Note that the high and low parts may be independently swapped. */
1098 swap_commutative(op->args[0], &op->args[2], &op->args[4]);
1099 swap_commutative(op->args[1], &op->args[3], &op->args[5]);
1100
Richard Henderson9531c072021-08-26 06:51:39 -07001101 return fold_addsub2(ctx, op, true);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001102}
1103
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001104static bool fold_and(OptContext *ctx, TCGOp *op)
1105{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001106 uint64_t z1, z2;
1107
Richard Henderson7a2f7082021-08-26 07:06:39 -07001108 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001109 fold_xi_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001110 fold_xi_to_x(ctx, op, -1) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001111 fold_xx_to_x(ctx, op)) {
1112 return true;
1113 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001114
1115 z1 = arg_info(op->args[1])->z_mask;
1116 z2 = arg_info(op->args[2])->z_mask;
1117 ctx->z_mask = z1 & z2;
1118
1119 /*
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001120 * Sign repetitions are perforce all identical, whether they are 1 or 0.
1121 * Bitwise operations preserve the relative quantity of the repetitions.
1122 */
1123 ctx->s_mask = arg_info(op->args[1])->s_mask
1124 & arg_info(op->args[2])->s_mask;
1125
1126 /*
Richard Hendersonfae450b2021-08-25 22:42:19 -07001127 * Known-zeros does not imply known-ones. Therefore unless
1128 * arg2 is constant, we can't infer affected bits from it.
1129 */
1130 if (arg_is_const(op->args[2])) {
1131 ctx->a_mask = z1 & ~z2;
1132 }
1133
1134 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001135}
1136
1137static bool fold_andc(OptContext *ctx, TCGOp *op)
1138{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001139 uint64_t z1;
1140
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001141 if (fold_const2(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001142 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001143 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001144 fold_ix_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001145 return true;
1146 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001147
1148 z1 = arg_info(op->args[1])->z_mask;
1149
1150 /*
1151 * Known-zeros does not imply known-ones. Therefore unless
1152 * arg2 is constant, we can't infer anything from it.
1153 */
1154 if (arg_is_const(op->args[2])) {
1155 uint64_t z2 = ~arg_info(op->args[2])->z_mask;
1156 ctx->a_mask = z1 & ~z2;
1157 z1 &= z2;
1158 }
1159 ctx->z_mask = z1;
1160
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001161 ctx->s_mask = arg_info(op->args[1])->s_mask
1162 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001163 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001164}
1165
Richard Henderson079b0802021-08-24 09:30:59 -07001166static bool fold_brcond(OptContext *ctx, TCGOp *op)
1167{
1168 TCGCond cond = op->args[2];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001169 int i;
Richard Henderson079b0802021-08-24 09:30:59 -07001170
Richard Henderson7a2f7082021-08-26 07:06:39 -07001171 if (swap_commutative(NO_DEST, &op->args[0], &op->args[1])) {
1172 op->args[2] = cond = tcg_swap_cond(cond);
1173 }
1174
1175 i = do_constant_folding_cond(ctx->type, op->args[0], op->args[1], cond);
Richard Henderson079b0802021-08-24 09:30:59 -07001176 if (i == 0) {
1177 tcg_op_remove(ctx->tcg, op);
1178 return true;
1179 }
1180 if (i > 0) {
1181 op->opc = INDEX_op_br;
1182 op->args[0] = op->args[3];
1183 }
1184 return false;
1185}
1186
Richard Henderson764d2ab2021-08-24 09:22:11 -07001187static bool fold_brcond2(OptContext *ctx, TCGOp *op)
1188{
1189 TCGCond cond = op->args[4];
Richard Henderson764d2ab2021-08-24 09:22:11 -07001190 TCGArg label = op->args[5];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001191 int i, inv = 0;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001192
Richard Henderson7a2f7082021-08-26 07:06:39 -07001193 if (swap_commutative2(&op->args[0], &op->args[2])) {
1194 op->args[4] = cond = tcg_swap_cond(cond);
1195 }
1196
1197 i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond);
Richard Henderson764d2ab2021-08-24 09:22:11 -07001198 if (i >= 0) {
1199 goto do_brcond_const;
1200 }
1201
1202 switch (cond) {
1203 case TCG_COND_LT:
1204 case TCG_COND_GE:
1205 /*
1206 * Simplify LT/GE comparisons vs zero to a single compare
1207 * vs the high word of the input.
1208 */
1209 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == 0 &&
1210 arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0) {
1211 goto do_brcond_high;
1212 }
1213 break;
1214
1215 case TCG_COND_NE:
1216 inv = 1;
1217 QEMU_FALLTHROUGH;
1218 case TCG_COND_EQ:
1219 /*
1220 * Simplify EQ/NE comparisons where one of the pairs
1221 * can be simplified.
1222 */
Richard Henderson67f84c92021-08-25 08:00:20 -07001223 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001224 op->args[2], cond);
1225 switch (i ^ inv) {
1226 case 0:
1227 goto do_brcond_const;
1228 case 1:
1229 goto do_brcond_high;
1230 }
1231
Richard Henderson67f84c92021-08-25 08:00:20 -07001232 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001233 op->args[3], cond);
1234 switch (i ^ inv) {
1235 case 0:
1236 goto do_brcond_const;
1237 case 1:
1238 op->opc = INDEX_op_brcond_i32;
1239 op->args[1] = op->args[2];
1240 op->args[2] = cond;
1241 op->args[3] = label;
1242 break;
1243 }
1244 break;
1245
1246 default:
1247 break;
1248
1249 do_brcond_high:
1250 op->opc = INDEX_op_brcond_i32;
1251 op->args[0] = op->args[1];
1252 op->args[1] = op->args[3];
1253 op->args[2] = cond;
1254 op->args[3] = label;
1255 break;
1256
1257 do_brcond_const:
1258 if (i == 0) {
1259 tcg_op_remove(ctx->tcg, op);
1260 return true;
1261 }
1262 op->opc = INDEX_op_br;
1263 op->args[0] = label;
1264 break;
1265 }
1266 return false;
1267}
1268
Richard Henderson09bacdc2021-08-24 11:58:12 -07001269static bool fold_bswap(OptContext *ctx, TCGOp *op)
1270{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001271 uint64_t z_mask, s_mask, sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001272
Richard Henderson09bacdc2021-08-24 11:58:12 -07001273 if (arg_is_const(op->args[1])) {
1274 uint64_t t = arg_info(op->args[1])->val;
1275
Richard Henderson67f84c92021-08-25 08:00:20 -07001276 t = do_constant_folding(op->opc, ctx->type, t, op->args[2]);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001277 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1278 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001279
1280 z_mask = arg_info(op->args[1])->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001281
Richard Hendersonfae450b2021-08-25 22:42:19 -07001282 switch (op->opc) {
1283 case INDEX_op_bswap16_i32:
1284 case INDEX_op_bswap16_i64:
1285 z_mask = bswap16(z_mask);
1286 sign = INT16_MIN;
1287 break;
1288 case INDEX_op_bswap32_i32:
1289 case INDEX_op_bswap32_i64:
1290 z_mask = bswap32(z_mask);
1291 sign = INT32_MIN;
1292 break;
1293 case INDEX_op_bswap64_i64:
1294 z_mask = bswap64(z_mask);
1295 sign = INT64_MIN;
1296 break;
1297 default:
1298 g_assert_not_reached();
1299 }
Richard Henderson57fe5c62021-08-26 12:04:46 -07001300 s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001301
1302 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1303 case TCG_BSWAP_OZ:
1304 break;
1305 case TCG_BSWAP_OS:
1306 /* If the sign bit may be 1, force all the bits above to 1. */
1307 if (z_mask & sign) {
1308 z_mask |= sign;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001309 s_mask = sign << 1;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001310 }
1311 break;
1312 default:
1313 /* The high bits are undefined: force all bits above the sign to 1. */
1314 z_mask |= sign << 1;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001315 s_mask = 0;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001316 break;
1317 }
1318 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001319 ctx->s_mask = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001320
1321 return fold_masks(ctx, op);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001322}
1323
Richard Henderson5cf32be2021-08-24 08:17:08 -07001324static bool fold_call(OptContext *ctx, TCGOp *op)
1325{
1326 TCGContext *s = ctx->tcg;
1327 int nb_oargs = TCGOP_CALLO(op);
1328 int nb_iargs = TCGOP_CALLI(op);
1329 int flags, i;
1330
1331 init_arguments(ctx, op, nb_oargs + nb_iargs);
1332 copy_propagate(ctx, op, nb_oargs, nb_iargs);
1333
1334 /* If the function reads or writes globals, reset temp data. */
1335 flags = tcg_call_flags(op);
1336 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1337 int nb_globals = s->nb_globals;
1338
1339 for (i = 0; i < nb_globals; i++) {
1340 if (test_bit(i, ctx->temps_used.l)) {
Richard Henderson986cac12023-01-09 13:59:35 -08001341 reset_ts(ctx, &ctx->tcg->temps[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001342 }
1343 }
1344 }
1345
Richard Hendersonab84dc32023-08-23 23:04:24 -07001346 /* If the function has side effects, reset mem data. */
1347 if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) {
1348 remove_mem_copy_all(ctx);
1349 }
1350
Richard Henderson5cf32be2021-08-24 08:17:08 -07001351 /* Reset temp data for outputs. */
1352 for (i = 0; i < nb_oargs; i++) {
Richard Henderson986cac12023-01-09 13:59:35 -08001353 reset_temp(ctx, op->args[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001354 }
1355
1356 /* Stop optimizing MB across calls. */
1357 ctx->prev_mb = NULL;
1358 return true;
1359}
1360
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001361static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1362{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001363 uint64_t z_mask;
1364
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001365 if (arg_is_const(op->args[1])) {
1366 uint64_t t = arg_info(op->args[1])->val;
1367
1368 if (t != 0) {
Richard Henderson67f84c92021-08-25 08:00:20 -07001369 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001370 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1371 }
1372 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1373 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001374
1375 switch (ctx->type) {
1376 case TCG_TYPE_I32:
1377 z_mask = 31;
1378 break;
1379 case TCG_TYPE_I64:
1380 z_mask = 63;
1381 break;
1382 default:
1383 g_assert_not_reached();
1384 }
1385 ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask;
Richard Henderson2b9d0c52021-08-26 13:24:17 -07001386 ctx->s_mask = smask_from_zmask(ctx->z_mask);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001387 return false;
1388}
1389
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001390static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1391{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001392 if (fold_const1(ctx, op)) {
1393 return true;
1394 }
1395
1396 switch (ctx->type) {
1397 case TCG_TYPE_I32:
1398 ctx->z_mask = 32 | 31;
1399 break;
1400 case TCG_TYPE_I64:
1401 ctx->z_mask = 64 | 63;
1402 break;
1403 default:
1404 g_assert_not_reached();
1405 }
Richard Henderson2b9d0c52021-08-26 13:24:17 -07001406 ctx->s_mask = smask_from_zmask(ctx->z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001407 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001408}
1409
Richard Henderson1b1907b2021-08-24 10:47:04 -07001410static bool fold_deposit(OptContext *ctx, TCGOp *op)
1411{
Richard Henderson8f7a8402023-08-13 11:03:05 -07001412 TCGOpcode and_opc;
1413
Richard Henderson1b1907b2021-08-24 10:47:04 -07001414 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1415 uint64_t t1 = arg_info(op->args[1])->val;
1416 uint64_t t2 = arg_info(op->args[2])->val;
1417
1418 t1 = deposit64(t1, op->args[3], op->args[4], t2);
1419 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1420 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001421
Richard Henderson8f7a8402023-08-13 11:03:05 -07001422 switch (ctx->type) {
1423 case TCG_TYPE_I32:
1424 and_opc = INDEX_op_and_i32;
1425 break;
1426 case TCG_TYPE_I64:
1427 and_opc = INDEX_op_and_i64;
1428 break;
1429 default:
1430 g_assert_not_reached();
1431 }
1432
1433 /* Inserting a value into zero at offset 0. */
1434 if (arg_is_const(op->args[1])
1435 && arg_info(op->args[1])->val == 0
1436 && op->args[3] == 0) {
1437 uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
1438
1439 op->opc = and_opc;
1440 op->args[1] = op->args[2];
Richard Henderson26aac972023-10-23 12:31:57 -07001441 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001442 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1443 return false;
1444 }
1445
1446 /* Inserting zero into a value. */
1447 if (arg_is_const(op->args[2])
1448 && arg_info(op->args[2])->val == 0) {
1449 uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
1450
1451 op->opc = and_opc;
Richard Henderson26aac972023-10-23 12:31:57 -07001452 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001453 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1454 return false;
1455 }
1456
Richard Hendersonfae450b2021-08-25 22:42:19 -07001457 ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
1458 op->args[3], op->args[4],
1459 arg_info(op->args[2])->z_mask);
Richard Henderson1b1907b2021-08-24 10:47:04 -07001460 return false;
1461}
1462
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001463static bool fold_divide(OptContext *ctx, TCGOp *op)
1464{
Richard Henderson2f9d9a32021-10-25 11:30:14 -07001465 if (fold_const2(ctx, op) ||
1466 fold_xi_to_x(ctx, op, 1)) {
1467 return true;
1468 }
1469 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001470}
1471
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07001472static bool fold_dup(OptContext *ctx, TCGOp *op)
1473{
1474 if (arg_is_const(op->args[1])) {
1475 uint64_t t = arg_info(op->args[1])->val;
1476 t = dup_const(TCGOP_VECE(op), t);
1477 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1478 }
1479 return false;
1480}
1481
1482static bool fold_dup2(OptContext *ctx, TCGOp *op)
1483{
1484 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1485 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1486 arg_info(op->args[2])->val);
1487 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1488 }
1489
1490 if (args_are_copies(op->args[1], op->args[2])) {
1491 op->opc = INDEX_op_dup_vec;
1492 TCGOP_VECE(op) = MO_32;
1493 }
1494 return false;
1495}
1496
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001497static bool fold_eqv(OptContext *ctx, TCGOp *op)
1498{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001499 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001500 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001501 fold_xi_to_not(ctx, op, 0)) {
1502 return true;
1503 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001504
1505 ctx->s_mask = arg_info(op->args[1])->s_mask
1506 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001507 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001508}
1509
Richard Hendersonb6617c82021-08-24 10:44:53 -07001510static bool fold_extract(OptContext *ctx, TCGOp *op)
1511{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001512 uint64_t z_mask_old, z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001513 int pos = op->args[2];
1514 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07001515
Richard Hendersonb6617c82021-08-24 10:44:53 -07001516 if (arg_is_const(op->args[1])) {
1517 uint64_t t;
1518
1519 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001520 t = extract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001521 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1522 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001523
1524 z_mask_old = arg_info(op->args[1])->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001525 z_mask = extract64(z_mask_old, pos, len);
1526 if (pos == 0) {
Richard Hendersonfae450b2021-08-25 22:42:19 -07001527 ctx->a_mask = z_mask_old ^ z_mask;
1528 }
1529 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001530 ctx->s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001531
1532 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001533}
1534
Richard Hendersondcd08992021-08-24 10:41:39 -07001535static bool fold_extract2(OptContext *ctx, TCGOp *op)
1536{
1537 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1538 uint64_t v1 = arg_info(op->args[1])->val;
1539 uint64_t v2 = arg_info(op->args[2])->val;
1540 int shr = op->args[3];
1541
1542 if (op->opc == INDEX_op_extract2_i64) {
1543 v1 >>= shr;
1544 v2 <<= 64 - shr;
1545 } else {
1546 v1 = (uint32_t)v1 >> shr;
Richard Henderson225bec02021-11-09 23:17:59 +01001547 v2 = (uint64_t)((int32_t)v2 << (32 - shr));
Richard Hendersondcd08992021-08-24 10:41:39 -07001548 }
1549 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1550 }
1551 return false;
1552}
1553
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001554static bool fold_exts(OptContext *ctx, TCGOp *op)
1555{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001556 uint64_t s_mask_old, s_mask, z_mask, sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001557 bool type_change = false;
1558
1559 if (fold_const1(ctx, op)) {
1560 return true;
1561 }
1562
Richard Henderson57fe5c62021-08-26 12:04:46 -07001563 z_mask = arg_info(op->args[1])->z_mask;
1564 s_mask = arg_info(op->args[1])->s_mask;
1565 s_mask_old = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001566
1567 switch (op->opc) {
1568 CASE_OP_32_64(ext8s):
1569 sign = INT8_MIN;
1570 z_mask = (uint8_t)z_mask;
1571 break;
1572 CASE_OP_32_64(ext16s):
1573 sign = INT16_MIN;
1574 z_mask = (uint16_t)z_mask;
1575 break;
1576 case INDEX_op_ext_i32_i64:
1577 type_change = true;
1578 QEMU_FALLTHROUGH;
1579 case INDEX_op_ext32s_i64:
1580 sign = INT32_MIN;
1581 z_mask = (uint32_t)z_mask;
1582 break;
1583 default:
1584 g_assert_not_reached();
1585 }
1586
1587 if (z_mask & sign) {
1588 z_mask |= sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001589 }
Richard Henderson57fe5c62021-08-26 12:04:46 -07001590 s_mask |= sign << 1;
1591
Richard Hendersonfae450b2021-08-25 22:42:19 -07001592 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001593 ctx->s_mask = s_mask;
1594 if (!type_change) {
1595 ctx->a_mask = s_mask & ~s_mask_old;
1596 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001597
1598 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001599}
1600
1601static bool fold_extu(OptContext *ctx, TCGOp *op)
1602{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001603 uint64_t z_mask_old, z_mask;
1604 bool type_change = false;
1605
1606 if (fold_const1(ctx, op)) {
1607 return true;
1608 }
1609
1610 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1611
1612 switch (op->opc) {
1613 CASE_OP_32_64(ext8u):
1614 z_mask = (uint8_t)z_mask;
1615 break;
1616 CASE_OP_32_64(ext16u):
1617 z_mask = (uint16_t)z_mask;
1618 break;
1619 case INDEX_op_extrl_i64_i32:
1620 case INDEX_op_extu_i32_i64:
1621 type_change = true;
1622 QEMU_FALLTHROUGH;
1623 case INDEX_op_ext32u_i64:
1624 z_mask = (uint32_t)z_mask;
1625 break;
1626 case INDEX_op_extrh_i64_i32:
1627 type_change = true;
1628 z_mask >>= 32;
1629 break;
1630 default:
1631 g_assert_not_reached();
1632 }
1633
1634 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001635 ctx->s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001636 if (!type_change) {
1637 ctx->a_mask = z_mask_old ^ z_mask;
1638 }
1639 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001640}
1641
Richard Henderson3eefdf22021-08-25 11:06:43 -07001642static bool fold_mb(OptContext *ctx, TCGOp *op)
1643{
1644 /* Eliminate duplicate and redundant fence instructions. */
1645 if (ctx->prev_mb) {
1646 /*
1647 * Merge two barriers of the same type into one,
1648 * or a weaker barrier into a stronger one,
1649 * or two weaker barriers into a stronger one.
1650 * mb X; mb Y => mb X|Y
1651 * mb; strl => mb; st
1652 * ldaq; mb => ld; mb
1653 * ldaq; strl => ld; mb; st
1654 * Other combinations are also merged into a strong
1655 * barrier. This is stricter than specified but for
1656 * the purposes of TCG is better than not optimizing.
1657 */
1658 ctx->prev_mb->args[0] |= op->args[0];
1659 tcg_op_remove(ctx->tcg, op);
1660 } else {
1661 ctx->prev_mb = op;
1662 }
1663 return true;
1664}
1665
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001666static bool fold_mov(OptContext *ctx, TCGOp *op)
1667{
1668 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1669}
1670
Richard Henderson0c310a32021-08-24 10:37:24 -07001671static bool fold_movcond(OptContext *ctx, TCGOp *op)
1672{
Richard Henderson0c310a32021-08-24 10:37:24 -07001673 TCGCond cond = op->args[5];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001674 int i;
Richard Henderson0c310a32021-08-24 10:37:24 -07001675
Richard Henderson7a2f7082021-08-26 07:06:39 -07001676 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
1677 op->args[5] = cond = tcg_swap_cond(cond);
1678 }
1679 /*
1680 * Canonicalize the "false" input reg to match the destination reg so
1681 * that the tcg backend can implement a "move if true" operation.
1682 */
1683 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
1684 op->args[5] = cond = tcg_invert_cond(cond);
1685 }
1686
1687 i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
Richard Henderson0c310a32021-08-24 10:37:24 -07001688 if (i >= 0) {
1689 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1690 }
1691
Richard Hendersonfae450b2021-08-25 22:42:19 -07001692 ctx->z_mask = arg_info(op->args[3])->z_mask
1693 | arg_info(op->args[4])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001694 ctx->s_mask = arg_info(op->args[3])->s_mask
1695 & arg_info(op->args[4])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001696
Richard Henderson0c310a32021-08-24 10:37:24 -07001697 if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
1698 uint64_t tv = arg_info(op->args[3])->val;
1699 uint64_t fv = arg_info(op->args[4])->val;
Richard Henderson36355022023-08-04 23:24:04 +00001700 TCGOpcode opc, negopc = 0;
Richard Henderson0c310a32021-08-24 10:37:24 -07001701
Richard Henderson67f84c92021-08-25 08:00:20 -07001702 switch (ctx->type) {
1703 case TCG_TYPE_I32:
1704 opc = INDEX_op_setcond_i32;
Richard Henderson36355022023-08-04 23:24:04 +00001705 if (TCG_TARGET_HAS_negsetcond_i32) {
1706 negopc = INDEX_op_negsetcond_i32;
1707 }
1708 tv = (int32_t)tv;
1709 fv = (int32_t)fv;
Richard Henderson67f84c92021-08-25 08:00:20 -07001710 break;
1711 case TCG_TYPE_I64:
1712 opc = INDEX_op_setcond_i64;
Richard Henderson36355022023-08-04 23:24:04 +00001713 if (TCG_TARGET_HAS_negsetcond_i64) {
1714 negopc = INDEX_op_negsetcond_i64;
1715 }
Richard Henderson67f84c92021-08-25 08:00:20 -07001716 break;
1717 default:
1718 g_assert_not_reached();
1719 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001720
1721 if (tv == 1 && fv == 0) {
1722 op->opc = opc;
1723 op->args[3] = cond;
1724 } else if (fv == 1 && tv == 0) {
1725 op->opc = opc;
1726 op->args[3] = tcg_invert_cond(cond);
Richard Henderson36355022023-08-04 23:24:04 +00001727 } else if (negopc) {
1728 if (tv == -1 && fv == 0) {
1729 op->opc = negopc;
1730 op->args[3] = cond;
1731 } else if (fv == -1 && tv == 0) {
1732 op->opc = negopc;
1733 op->args[3] = tcg_invert_cond(cond);
1734 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001735 }
1736 }
1737 return false;
1738}
1739
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001740static bool fold_mul(OptContext *ctx, TCGOp *op)
1741{
Richard Hendersone8679952021-08-25 13:19:52 -07001742 if (fold_const2(ctx, op) ||
Richard Henderson5b5cf472021-10-25 11:19:14 -07001743 fold_xi_to_i(ctx, op, 0) ||
1744 fold_xi_to_x(ctx, op, 1)) {
Richard Hendersone8679952021-08-25 13:19:52 -07001745 return true;
1746 }
1747 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001748}
1749
1750static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
1751{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001752 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001753 fold_xi_to_i(ctx, op, 0)) {
1754 return true;
1755 }
1756 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001757}
1758
Richard Henderson407112b2021-08-26 06:33:04 -07001759static bool fold_multiply2(OptContext *ctx, TCGOp *op)
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001760{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001761 swap_commutative(op->args[0], &op->args[2], &op->args[3]);
1762
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001763 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
Richard Henderson407112b2021-08-26 06:33:04 -07001764 uint64_t a = arg_info(op->args[2])->val;
1765 uint64_t b = arg_info(op->args[3])->val;
1766 uint64_t h, l;
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001767 TCGArg rl, rh;
Richard Henderson407112b2021-08-26 06:33:04 -07001768 TCGOp *op2;
1769
1770 switch (op->opc) {
1771 case INDEX_op_mulu2_i32:
1772 l = (uint64_t)(uint32_t)a * (uint32_t)b;
1773 h = (int32_t)(l >> 32);
1774 l = (int32_t)l;
1775 break;
1776 case INDEX_op_muls2_i32:
1777 l = (int64_t)(int32_t)a * (int32_t)b;
1778 h = l >> 32;
1779 l = (int32_t)l;
1780 break;
1781 case INDEX_op_mulu2_i64:
1782 mulu64(&l, &h, a, b);
1783 break;
1784 case INDEX_op_muls2_i64:
1785 muls64(&l, &h, a, b);
1786 break;
1787 default:
1788 g_assert_not_reached();
1789 }
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001790
1791 rl = op->args[0];
1792 rh = op->args[1];
Richard Henderson407112b2021-08-26 06:33:04 -07001793
1794 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01001795 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson407112b2021-08-26 06:33:04 -07001796
1797 tcg_opt_gen_movi(ctx, op, rl, l);
1798 tcg_opt_gen_movi(ctx, op2, rh, h);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001799 return true;
1800 }
1801 return false;
1802}
1803
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001804static bool fold_nand(OptContext *ctx, TCGOp *op)
1805{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001806 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001807 fold_xi_to_not(ctx, op, -1)) {
1808 return true;
1809 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001810
1811 ctx->s_mask = arg_info(op->args[1])->s_mask
1812 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001813 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001814}
1815
1816static bool fold_neg(OptContext *ctx, TCGOp *op)
1817{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001818 uint64_t z_mask;
1819
Richard Henderson9caca882021-08-24 13:30:32 -07001820 if (fold_const1(ctx, op)) {
1821 return true;
1822 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001823
1824 /* Set to 1 all bits to the left of the rightmost. */
1825 z_mask = arg_info(op->args[1])->z_mask;
1826 ctx->z_mask = -(z_mask & -z_mask);
1827
Richard Henderson9caca882021-08-24 13:30:32 -07001828 /*
1829 * Because of fold_sub_to_neg, we want to always return true,
1830 * via finish_folding.
1831 */
1832 finish_folding(ctx, op);
1833 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001834}
1835
1836static bool fold_nor(OptContext *ctx, TCGOp *op)
1837{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001838 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001839 fold_xi_to_not(ctx, op, 0)) {
1840 return true;
1841 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001842
1843 ctx->s_mask = arg_info(op->args[1])->s_mask
1844 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001845 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001846}
1847
1848static bool fold_not(OptContext *ctx, TCGOp *op)
1849{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001850 if (fold_const1(ctx, op)) {
1851 return true;
1852 }
1853
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001854 ctx->s_mask = arg_info(op->args[1])->s_mask;
1855
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001856 /* Because of fold_to_not, we want to always return true, via finish. */
1857 finish_folding(ctx, op);
1858 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001859}
1860
1861static bool fold_or(OptContext *ctx, TCGOp *op)
1862{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001863 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001864 fold_xi_to_x(ctx, op, 0) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001865 fold_xx_to_x(ctx, op)) {
1866 return true;
1867 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001868
1869 ctx->z_mask = arg_info(op->args[1])->z_mask
1870 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001871 ctx->s_mask = arg_info(op->args[1])->s_mask
1872 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001873 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001874}
1875
1876static bool fold_orc(OptContext *ctx, TCGOp *op)
1877{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001878 if (fold_const2(ctx, op) ||
Richard Henderson4e858d92021-08-26 07:31:13 -07001879 fold_xx_to_i(ctx, op, -1) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001880 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001881 fold_ix_to_not(ctx, op, 0)) {
1882 return true;
1883 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001884
1885 ctx->s_mask = arg_info(op->args[1])->s_mask
1886 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001887 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001888}
1889
Richard Henderson3eefdf22021-08-25 11:06:43 -07001890static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
1891{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001892 const TCGOpDef *def = &tcg_op_defs[op->opc];
1893 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
1894 MemOp mop = get_memop(oi);
1895 int width = 8 * memop_size(mop);
1896
Richard Henderson57fe5c62021-08-26 12:04:46 -07001897 if (width < 64) {
1898 ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width);
1899 if (!(mop & MO_SIGN)) {
1900 ctx->z_mask = MAKE_64BIT_MASK(0, width);
1901 ctx->s_mask <<= 1;
1902 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001903 }
1904
Richard Henderson3eefdf22021-08-25 11:06:43 -07001905 /* Opcodes that touch guest memory stop the mb optimization. */
1906 ctx->prev_mb = NULL;
1907 return false;
1908}
1909
1910static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
1911{
1912 /* Opcodes that touch guest memory stop the mb optimization. */
1913 ctx->prev_mb = NULL;
1914 return false;
1915}
1916
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001917static bool fold_remainder(OptContext *ctx, TCGOp *op)
1918{
Richard Henderson267c17e2021-10-25 11:30:33 -07001919 if (fold_const2(ctx, op) ||
1920 fold_xx_to_i(ctx, op, 0)) {
1921 return true;
1922 }
1923 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001924}
1925
Richard Hendersonc63ff552021-08-24 09:35:30 -07001926static bool fold_setcond(OptContext *ctx, TCGOp *op)
1927{
1928 TCGCond cond = op->args[3];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001929 int i;
Richard Hendersonc63ff552021-08-24 09:35:30 -07001930
Richard Henderson7a2f7082021-08-26 07:06:39 -07001931 if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) {
1932 op->args[3] = cond = tcg_swap_cond(cond);
1933 }
1934
1935 i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
Richard Hendersonc63ff552021-08-24 09:35:30 -07001936 if (i >= 0) {
1937 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1938 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001939
1940 ctx->z_mask = 1;
Richard Henderson275d7d82021-08-26 13:20:39 -07001941 ctx->s_mask = smask_from_zmask(1);
Richard Hendersonc63ff552021-08-24 09:35:30 -07001942 return false;
1943}
1944
Richard Henderson36355022023-08-04 23:24:04 +00001945static bool fold_negsetcond(OptContext *ctx, TCGOp *op)
1946{
1947 TCGCond cond = op->args[3];
1948 int i;
1949
1950 if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) {
1951 op->args[3] = cond = tcg_swap_cond(cond);
1952 }
1953
1954 i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
1955 if (i >= 0) {
1956 return tcg_opt_gen_movi(ctx, op, op->args[0], -i);
1957 }
1958
1959 /* Value is {0,-1} so all bits are repetitions of the sign. */
1960 ctx->s_mask = -1;
1961 return false;
1962}
1963
1964
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001965static bool fold_setcond2(OptContext *ctx, TCGOp *op)
1966{
1967 TCGCond cond = op->args[5];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001968 int i, inv = 0;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001969
Richard Henderson7a2f7082021-08-26 07:06:39 -07001970 if (swap_commutative2(&op->args[1], &op->args[3])) {
1971 op->args[5] = cond = tcg_swap_cond(cond);
1972 }
1973
1974 i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond);
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001975 if (i >= 0) {
1976 goto do_setcond_const;
1977 }
1978
1979 switch (cond) {
1980 case TCG_COND_LT:
1981 case TCG_COND_GE:
1982 /*
1983 * Simplify LT/GE comparisons vs zero to a single compare
1984 * vs the high word of the input.
1985 */
1986 if (arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0 &&
1987 arg_is_const(op->args[4]) && arg_info(op->args[4])->val == 0) {
1988 goto do_setcond_high;
1989 }
1990 break;
1991
1992 case TCG_COND_NE:
1993 inv = 1;
1994 QEMU_FALLTHROUGH;
1995 case TCG_COND_EQ:
1996 /*
1997 * Simplify EQ/NE comparisons where one of the pairs
1998 * can be simplified.
1999 */
Richard Henderson67f84c92021-08-25 08:00:20 -07002000 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002001 op->args[3], cond);
2002 switch (i ^ inv) {
2003 case 0:
2004 goto do_setcond_const;
2005 case 1:
2006 goto do_setcond_high;
2007 }
2008
Richard Henderson67f84c92021-08-25 08:00:20 -07002009 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002010 op->args[4], cond);
2011 switch (i ^ inv) {
2012 case 0:
2013 goto do_setcond_const;
2014 case 1:
2015 op->args[2] = op->args[3];
2016 op->args[3] = cond;
2017 op->opc = INDEX_op_setcond_i32;
2018 break;
2019 }
2020 break;
2021
2022 default:
2023 break;
2024
2025 do_setcond_high:
2026 op->args[1] = op->args[2];
2027 op->args[2] = op->args[4];
2028 op->args[3] = cond;
2029 op->opc = INDEX_op_setcond_i32;
2030 break;
2031 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002032
2033 ctx->z_mask = 1;
Richard Henderson275d7d82021-08-26 13:20:39 -07002034 ctx->s_mask = smask_from_zmask(1);
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002035 return false;
2036
2037 do_setcond_const:
2038 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2039}
2040
Richard Hendersonb6617c82021-08-24 10:44:53 -07002041static bool fold_sextract(OptContext *ctx, TCGOp *op)
2042{
Richard Henderson57fe5c62021-08-26 12:04:46 -07002043 uint64_t z_mask, s_mask, s_mask_old;
2044 int pos = op->args[2];
2045 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07002046
Richard Hendersonb6617c82021-08-24 10:44:53 -07002047 if (arg_is_const(op->args[1])) {
2048 uint64_t t;
2049
2050 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002051 t = sextract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002052 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
2053 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002054
Richard Henderson57fe5c62021-08-26 12:04:46 -07002055 z_mask = arg_info(op->args[1])->z_mask;
2056 z_mask = sextract64(z_mask, pos, len);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002057 ctx->z_mask = z_mask;
2058
Richard Henderson57fe5c62021-08-26 12:04:46 -07002059 s_mask_old = arg_info(op->args[1])->s_mask;
2060 s_mask = sextract64(s_mask_old, pos, len);
2061 s_mask |= MAKE_64BIT_MASK(len, 64 - len);
2062 ctx->s_mask = s_mask;
2063
2064 if (pos == 0) {
2065 ctx->a_mask = s_mask & ~s_mask_old;
2066 }
2067
Richard Hendersonfae450b2021-08-25 22:42:19 -07002068 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002069}
2070
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002071static bool fold_shift(OptContext *ctx, TCGOp *op)
2072{
Richard Henderson93a967f2021-08-26 13:24:59 -07002073 uint64_t s_mask, z_mask, sign;
2074
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002075 if (fold_const2(ctx, op) ||
Richard Hendersonda48e272021-08-25 20:42:04 -07002076 fold_ix_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002077 fold_xi_to_x(ctx, op, 0)) {
2078 return true;
2079 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002080
Richard Henderson93a967f2021-08-26 13:24:59 -07002081 s_mask = arg_info(op->args[1])->s_mask;
2082 z_mask = arg_info(op->args[1])->z_mask;
2083
Richard Hendersonfae450b2021-08-25 22:42:19 -07002084 if (arg_is_const(op->args[2])) {
Richard Henderson93a967f2021-08-26 13:24:59 -07002085 int sh = arg_info(op->args[2])->val;
2086
2087 ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
2088
2089 s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
2090 ctx->s_mask = smask_from_smask(s_mask);
2091
Richard Hendersonfae450b2021-08-25 22:42:19 -07002092 return fold_masks(ctx, op);
2093 }
Richard Henderson93a967f2021-08-26 13:24:59 -07002094
2095 switch (op->opc) {
2096 CASE_OP_32_64(sar):
2097 /*
2098 * Arithmetic right shift will not reduce the number of
2099 * input sign repetitions.
2100 */
2101 ctx->s_mask = s_mask;
2102 break;
2103 CASE_OP_32_64(shr):
2104 /*
2105 * If the sign bit is known zero, then logical right shift
2106 * will not reduced the number of input sign repetitions.
2107 */
2108 sign = (s_mask & -s_mask) >> 1;
2109 if (!(z_mask & sign)) {
2110 ctx->s_mask = s_mask;
2111 }
2112 break;
2113 default:
2114 break;
2115 }
2116
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002117 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002118}
2119
Richard Henderson9caca882021-08-24 13:30:32 -07002120static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
2121{
2122 TCGOpcode neg_op;
2123 bool have_neg;
2124
2125 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
2126 return false;
2127 }
2128
2129 switch (ctx->type) {
2130 case TCG_TYPE_I32:
2131 neg_op = INDEX_op_neg_i32;
Richard Hendersonb701f192023-10-25 21:14:04 -07002132 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002133 break;
2134 case TCG_TYPE_I64:
2135 neg_op = INDEX_op_neg_i64;
Richard Hendersonb701f192023-10-25 21:14:04 -07002136 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002137 break;
2138 case TCG_TYPE_V64:
2139 case TCG_TYPE_V128:
2140 case TCG_TYPE_V256:
2141 neg_op = INDEX_op_neg_vec;
2142 have_neg = (TCG_TARGET_HAS_neg_vec &&
2143 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
2144 break;
2145 default:
2146 g_assert_not_reached();
2147 }
2148 if (have_neg) {
2149 op->opc = neg_op;
2150 op->args[1] = op->args[2];
2151 return fold_neg(ctx, op);
2152 }
2153 return false;
2154}
2155
Richard Hendersonc578ff12021-12-16 06:07:25 -08002156/* We cannot as yet do_constant_folding with vectors. */
2157static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002158{
Richard Hendersonc578ff12021-12-16 06:07:25 -08002159 if (fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002160 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson9caca882021-08-24 13:30:32 -07002161 fold_sub_to_neg(ctx, op)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002162 return true;
2163 }
2164 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002165}
2166
Richard Hendersonc578ff12021-12-16 06:07:25 -08002167static bool fold_sub(OptContext *ctx, TCGOp *op)
2168{
2169 return fold_const2(ctx, op) || fold_sub_vec(ctx, op);
2170}
2171
Richard Henderson9531c072021-08-26 06:51:39 -07002172static bool fold_sub2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002173{
Richard Henderson9531c072021-08-26 06:51:39 -07002174 return fold_addsub2(ctx, op, false);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002175}
2176
Richard Hendersonfae450b2021-08-25 22:42:19 -07002177static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
2178{
2179 /* We can't do any folding with a load, but we can record bits. */
2180 switch (op->opc) {
Richard Henderson57fe5c62021-08-26 12:04:46 -07002181 CASE_OP_32_64(ld8s):
2182 ctx->s_mask = MAKE_64BIT_MASK(8, 56);
2183 break;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002184 CASE_OP_32_64(ld8u):
2185 ctx->z_mask = MAKE_64BIT_MASK(0, 8);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002186 ctx->s_mask = MAKE_64BIT_MASK(9, 55);
2187 break;
2188 CASE_OP_32_64(ld16s):
2189 ctx->s_mask = MAKE_64BIT_MASK(16, 48);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002190 break;
2191 CASE_OP_32_64(ld16u):
2192 ctx->z_mask = MAKE_64BIT_MASK(0, 16);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002193 ctx->s_mask = MAKE_64BIT_MASK(17, 47);
2194 break;
2195 case INDEX_op_ld32s_i64:
2196 ctx->s_mask = MAKE_64BIT_MASK(32, 32);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002197 break;
2198 case INDEX_op_ld32u_i64:
2199 ctx->z_mask = MAKE_64BIT_MASK(0, 32);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002200 ctx->s_mask = MAKE_64BIT_MASK(33, 31);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002201 break;
2202 default:
2203 g_assert_not_reached();
2204 }
2205 return false;
2206}
2207
Richard Hendersonab84dc32023-08-23 23:04:24 -07002208static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op)
2209{
2210 TCGTemp *dst, *src;
2211 intptr_t ofs;
2212 TCGType type;
2213
2214 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2215 return false;
2216 }
2217
2218 type = ctx->type;
2219 ofs = op->args[2];
2220 dst = arg_temp(op->args[0]);
2221 src = find_mem_copy_for(ctx, type, ofs);
2222 if (src && src->base_type == type) {
2223 return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src));
2224 }
2225
2226 reset_ts(ctx, dst);
2227 record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1);
2228 return true;
2229}
2230
2231static bool fold_tcg_st(OptContext *ctx, TCGOp *op)
2232{
2233 intptr_t ofs = op->args[2];
2234 intptr_t lm1;
2235
2236 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2237 remove_mem_copy_all(ctx);
2238 return false;
2239 }
2240
2241 switch (op->opc) {
2242 CASE_OP_32_64(st8):
2243 lm1 = 0;
2244 break;
2245 CASE_OP_32_64(st16):
2246 lm1 = 1;
2247 break;
2248 case INDEX_op_st32_i64:
2249 case INDEX_op_st_i32:
2250 lm1 = 3;
2251 break;
2252 case INDEX_op_st_i64:
2253 lm1 = 7;
2254 break;
2255 case INDEX_op_st_vec:
2256 lm1 = tcg_type_size(ctx->type) - 1;
2257 break;
2258 default:
2259 g_assert_not_reached();
2260 }
2261 remove_mem_copy_in(ctx, ofs, ofs + lm1);
2262 return false;
2263}
2264
2265static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op)
2266{
2267 TCGTemp *src;
2268 intptr_t ofs, last;
2269 TCGType type;
2270
2271 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2272 fold_tcg_st(ctx, op);
2273 return false;
2274 }
2275
2276 src = arg_temp(op->args[0]);
2277 ofs = op->args[2];
2278 type = ctx->type;
Richard Henderson3eaadae2023-08-23 23:13:06 -07002279
2280 /*
2281 * Eliminate duplicate stores of a constant.
2282 * This happens frequently when the target ISA zero-extends.
2283 */
2284 if (ts_is_const(src)) {
2285 TCGTemp *prev = find_mem_copy_for(ctx, type, ofs);
2286 if (src == prev) {
2287 tcg_op_remove(ctx->tcg, op);
2288 return true;
2289 }
2290 }
2291
Richard Hendersonab84dc32023-08-23 23:04:24 -07002292 last = ofs + tcg_type_size(type) - 1;
2293 remove_mem_copy_in(ctx, ofs, last);
2294 record_mem_copy(ctx, type, src, ofs, last);
2295 return false;
2296}
2297
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002298static bool fold_xor(OptContext *ctx, TCGOp *op)
2299{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002300 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002301 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002302 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002303 fold_xi_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002304 return true;
2305 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002306
2307 ctx->z_mask = arg_info(op->args[1])->z_mask
2308 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002309 ctx->s_mask = arg_info(op->args[1])->s_mask
2310 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002311 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002312}
2313
Kirill Batuzov22613af2011-07-07 16:37:13 +04002314/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +02002315void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002316{
Richard Henderson5cf32be2021-08-24 08:17:08 -07002317 int nb_temps, i;
Richard Hendersond0ed5152021-08-24 07:38:39 -07002318 TCGOp *op, *op_next;
Richard Hendersondc849882021-08-24 07:13:45 -07002319 OptContext ctx = { .tcg = s };
Richard Henderson5d8f5362012-09-21 10:13:38 -07002320
Richard Hendersonab84dc32023-08-23 23:04:24 -07002321 QSIMPLEQ_INIT(&ctx.mem_free);
2322
Kirill Batuzov22613af2011-07-07 16:37:13 +04002323 /* Array VALS has an element for each temp.
2324 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02002325 If this temp is a copy of other ones then the other copies are
2326 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002327
2328 nb_temps = s->nb_temps;
Richard Henderson8f17a972020-03-30 19:52:02 -07002329 for (i = 0; i < nb_temps; ++i) {
2330 s->temps[i].state_ptr = NULL;
2331 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002332
Richard Henderson15fa08f2017-11-02 15:19:14 +01002333 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002334 TCGOpcode opc = op->opc;
Richard Henderson5cf32be2021-08-24 08:17:08 -07002335 const TCGOpDef *def;
Richard Henderson404a1482021-08-24 11:08:21 -07002336 bool done = false;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002337
Richard Henderson5cf32be2021-08-24 08:17:08 -07002338 /* Calls are special. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002339 if (opc == INDEX_op_call) {
Richard Henderson5cf32be2021-08-24 08:17:08 -07002340 fold_call(&ctx, op);
2341 continue;
Richard Hendersoncf066672014-03-22 20:06:52 -07002342 }
Richard Henderson5cf32be2021-08-24 08:17:08 -07002343
2344 def = &tcg_op_defs[opc];
Richard Hendersonec5d4cb2021-08-24 08:20:27 -07002345 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
2346 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
Kirill Batuzov22613af2011-07-07 16:37:13 +04002347
Richard Henderson67f84c92021-08-25 08:00:20 -07002348 /* Pre-compute the type of the operation. */
2349 if (def->flags & TCG_OPF_VECTOR) {
2350 ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
2351 } else if (def->flags & TCG_OPF_64BIT) {
2352 ctx.type = TCG_TYPE_I64;
2353 } else {
2354 ctx.type = TCG_TYPE_I32;
2355 }
2356
Richard Henderson57fe5c62021-08-26 12:04:46 -07002357 /* Assume all bits affected, no bits known zero, no sign reps. */
Richard Hendersonfae450b2021-08-25 22:42:19 -07002358 ctx.a_mask = -1;
2359 ctx.z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002360 ctx.s_mask = 0;
Paolo Bonzini633f6502013-01-11 15:42:53 -08002361
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002362 /*
2363 * Process each opcode.
2364 * Sorted alphabetically by opcode as much as possible.
2365 */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002366 switch (opc) {
Richard Hendersonc578ff12021-12-16 06:07:25 -08002367 CASE_OP_32_64(add):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002368 done = fold_add(&ctx, op);
2369 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002370 case INDEX_op_add_vec:
2371 done = fold_add_vec(&ctx, op);
2372 break;
Richard Henderson9531c072021-08-26 06:51:39 -07002373 CASE_OP_32_64(add2):
2374 done = fold_add2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002375 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002376 CASE_OP_32_64_VEC(and):
2377 done = fold_and(&ctx, op);
2378 break;
2379 CASE_OP_32_64_VEC(andc):
2380 done = fold_andc(&ctx, op);
2381 break;
Richard Henderson079b0802021-08-24 09:30:59 -07002382 CASE_OP_32_64(brcond):
2383 done = fold_brcond(&ctx, op);
2384 break;
Richard Henderson764d2ab2021-08-24 09:22:11 -07002385 case INDEX_op_brcond2_i32:
2386 done = fold_brcond2(&ctx, op);
2387 break;
Richard Henderson09bacdc2021-08-24 11:58:12 -07002388 CASE_OP_32_64(bswap16):
2389 CASE_OP_32_64(bswap32):
2390 case INDEX_op_bswap64_i64:
2391 done = fold_bswap(&ctx, op);
2392 break;
Richard Henderson30dd0bf2021-08-24 10:51:34 -07002393 CASE_OP_32_64(clz):
2394 CASE_OP_32_64(ctz):
2395 done = fold_count_zeros(&ctx, op);
2396 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002397 CASE_OP_32_64(ctpop):
2398 done = fold_ctpop(&ctx, op);
2399 break;
Richard Henderson1b1907b2021-08-24 10:47:04 -07002400 CASE_OP_32_64(deposit):
2401 done = fold_deposit(&ctx, op);
2402 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002403 CASE_OP_32_64(div):
2404 CASE_OP_32_64(divu):
2405 done = fold_divide(&ctx, op);
2406 break;
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07002407 case INDEX_op_dup_vec:
2408 done = fold_dup(&ctx, op);
2409 break;
2410 case INDEX_op_dup2_vec:
2411 done = fold_dup2(&ctx, op);
2412 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002413 CASE_OP_32_64_VEC(eqv):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002414 done = fold_eqv(&ctx, op);
2415 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07002416 CASE_OP_32_64(extract):
2417 done = fold_extract(&ctx, op);
2418 break;
Richard Hendersondcd08992021-08-24 10:41:39 -07002419 CASE_OP_32_64(extract2):
2420 done = fold_extract2(&ctx, op);
2421 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002422 CASE_OP_32_64(ext8s):
2423 CASE_OP_32_64(ext16s):
2424 case INDEX_op_ext32s_i64:
2425 case INDEX_op_ext_i32_i64:
2426 done = fold_exts(&ctx, op);
2427 break;
2428 CASE_OP_32_64(ext8u):
2429 CASE_OP_32_64(ext16u):
2430 case INDEX_op_ext32u_i64:
2431 case INDEX_op_extu_i32_i64:
2432 case INDEX_op_extrl_i64_i32:
2433 case INDEX_op_extrh_i64_i32:
2434 done = fold_extu(&ctx, op);
2435 break;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002436 CASE_OP_32_64(ld8s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002437 CASE_OP_32_64(ld8u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002438 CASE_OP_32_64(ld16s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002439 CASE_OP_32_64(ld16u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002440 case INDEX_op_ld32s_i64:
Richard Hendersonfae450b2021-08-25 22:42:19 -07002441 case INDEX_op_ld32u_i64:
2442 done = fold_tcg_ld(&ctx, op);
2443 break;
Richard Hendersonab84dc32023-08-23 23:04:24 -07002444 case INDEX_op_ld_i32:
2445 case INDEX_op_ld_i64:
2446 case INDEX_op_ld_vec:
2447 done = fold_tcg_ld_memcopy(&ctx, op);
2448 break;
2449 CASE_OP_32_64(st8):
2450 CASE_OP_32_64(st16):
2451 case INDEX_op_st32_i64:
2452 done = fold_tcg_st(&ctx, op);
2453 break;
2454 case INDEX_op_st_i32:
2455 case INDEX_op_st_i64:
2456 case INDEX_op_st_vec:
2457 done = fold_tcg_st_memcopy(&ctx, op);
2458 break;
Richard Henderson3eefdf22021-08-25 11:06:43 -07002459 case INDEX_op_mb:
2460 done = fold_mb(&ctx, op);
2461 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002462 CASE_OP_32_64_VEC(mov):
2463 done = fold_mov(&ctx, op);
2464 break;
Richard Henderson0c310a32021-08-24 10:37:24 -07002465 CASE_OP_32_64(movcond):
2466 done = fold_movcond(&ctx, op);
2467 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002468 CASE_OP_32_64(mul):
2469 done = fold_mul(&ctx, op);
2470 break;
2471 CASE_OP_32_64(mulsh):
2472 CASE_OP_32_64(muluh):
2473 done = fold_mul_highpart(&ctx, op);
2474 break;
Richard Henderson407112b2021-08-26 06:33:04 -07002475 CASE_OP_32_64(muls2):
2476 CASE_OP_32_64(mulu2):
2477 done = fold_multiply2(&ctx, op);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07002478 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002479 CASE_OP_32_64_VEC(nand):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002480 done = fold_nand(&ctx, op);
2481 break;
2482 CASE_OP_32_64(neg):
2483 done = fold_neg(&ctx, op);
2484 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002485 CASE_OP_32_64_VEC(nor):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002486 done = fold_nor(&ctx, op);
2487 break;
2488 CASE_OP_32_64_VEC(not):
2489 done = fold_not(&ctx, op);
2490 break;
2491 CASE_OP_32_64_VEC(or):
2492 done = fold_or(&ctx, op);
2493 break;
2494 CASE_OP_32_64_VEC(orc):
2495 done = fold_orc(&ctx, op);
2496 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07002497 case INDEX_op_qemu_ld_a32_i32:
2498 case INDEX_op_qemu_ld_a64_i32:
2499 case INDEX_op_qemu_ld_a32_i64:
2500 case INDEX_op_qemu_ld_a64_i64:
2501 case INDEX_op_qemu_ld_a32_i128:
2502 case INDEX_op_qemu_ld_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07002503 done = fold_qemu_ld(&ctx, op);
2504 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07002505 case INDEX_op_qemu_st8_a32_i32:
2506 case INDEX_op_qemu_st8_a64_i32:
2507 case INDEX_op_qemu_st_a32_i32:
2508 case INDEX_op_qemu_st_a64_i32:
2509 case INDEX_op_qemu_st_a32_i64:
2510 case INDEX_op_qemu_st_a64_i64:
2511 case INDEX_op_qemu_st_a32_i128:
2512 case INDEX_op_qemu_st_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07002513 done = fold_qemu_st(&ctx, op);
2514 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002515 CASE_OP_32_64(rem):
2516 CASE_OP_32_64(remu):
2517 done = fold_remainder(&ctx, op);
2518 break;
2519 CASE_OP_32_64(rotl):
2520 CASE_OP_32_64(rotr):
2521 CASE_OP_32_64(sar):
2522 CASE_OP_32_64(shl):
2523 CASE_OP_32_64(shr):
2524 done = fold_shift(&ctx, op);
2525 break;
Richard Hendersonc63ff552021-08-24 09:35:30 -07002526 CASE_OP_32_64(setcond):
2527 done = fold_setcond(&ctx, op);
2528 break;
Richard Henderson36355022023-08-04 23:24:04 +00002529 CASE_OP_32_64(negsetcond):
2530 done = fold_negsetcond(&ctx, op);
2531 break;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002532 case INDEX_op_setcond2_i32:
2533 done = fold_setcond2(&ctx, op);
2534 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07002535 CASE_OP_32_64(sextract):
2536 done = fold_sextract(&ctx, op);
2537 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002538 CASE_OP_32_64(sub):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002539 done = fold_sub(&ctx, op);
2540 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002541 case INDEX_op_sub_vec:
2542 done = fold_sub_vec(&ctx, op);
2543 break;
Richard Henderson9531c072021-08-26 06:51:39 -07002544 CASE_OP_32_64(sub2):
2545 done = fold_sub2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002546 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002547 CASE_OP_32_64_VEC(xor):
2548 done = fold_xor(&ctx, op);
Richard Hendersonb10f3832021-08-23 22:30:17 -07002549 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002550 default:
2551 break;
Richard Hendersonb10f3832021-08-23 22:30:17 -07002552 }
2553
Richard Henderson404a1482021-08-24 11:08:21 -07002554 if (!done) {
2555 finish_folding(&ctx, op);
2556 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002557 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002558}