blob: 9c04dba099b68b71d092cd73c530b8fb9e28686b [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
Richard Henderson27cdb852023-10-23 11:38:00 -0700127static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val)
128{
129 TempOptInfo *ti = ts_info(ts);
130 return ti->is_const && ti->val == val;
131}
132
Richard Henderson63490392017-06-20 13:43:15 -0700133static inline bool arg_is_const(TCGArg arg)
134{
135 return ts_is_const(arg_temp(arg));
136}
137
Richard Henderson27cdb852023-10-23 11:38:00 -0700138static inline bool arg_is_const_val(TCGArg arg, uint64_t val)
139{
140 return ts_is_const_val(arg_temp(arg), val);
141}
142
Richard Henderson63490392017-06-20 13:43:15 -0700143static inline bool ts_is_copy(TCGTemp *ts)
144{
145 return ts_info(ts)->next_copy != ts;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200146}
147
Richard Henderson9f75e522023-11-02 13:37:46 -0700148static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b)
149{
150 return a->kind < b->kind ? b : a;
151}
152
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200153/* Initialize and activate a temporary. */
Richard Henderson3b3f8472021-08-23 22:06:31 -0700154static void init_ts_info(OptContext *ctx, TCGTemp *ts)
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200155{
Richard Henderson63490392017-06-20 13:43:15 -0700156 size_t idx = temp_idx(ts);
Richard Henderson8f17a972020-03-30 19:52:02 -0700157 TempOptInfo *ti;
Richard Henderson63490392017-06-20 13:43:15 -0700158
Richard Henderson3b3f8472021-08-23 22:06:31 -0700159 if (test_bit(idx, ctx->temps_used.l)) {
Richard Henderson8f17a972020-03-30 19:52:02 -0700160 return;
161 }
Richard Henderson3b3f8472021-08-23 22:06:31 -0700162 set_bit(idx, ctx->temps_used.l);
Richard Henderson8f17a972020-03-30 19:52:02 -0700163
164 ti = ts->state_ptr;
165 if (ti == NULL) {
166 ti = tcg_malloc(sizeof(TempOptInfo));
Richard Henderson63490392017-06-20 13:43:15 -0700167 ts->state_ptr = ti;
Richard Henderson8f17a972020-03-30 19:52:02 -0700168 }
169
170 ti->next_copy = ts;
171 ti->prev_copy = ts;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700172 QSIMPLEQ_INIT(&ti->mem_copy);
Richard Henderson8f17a972020-03-30 19:52:02 -0700173 if (ts->kind == TEMP_CONST) {
174 ti->is_const = true;
175 ti->val = ts->val;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700176 ti->z_mask = ts->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700177 ti->s_mask = smask_from_value(ts->val);
Richard Henderson8f17a972020-03-30 19:52:02 -0700178 } else {
179 ti->is_const = false;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700180 ti->z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700181 ti->s_mask = 0;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200182 }
183}
184
Richard Hendersonab84dc32023-08-23 23:04:24 -0700185static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l)
186{
187 IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l);
188 return r ? container_of(r, MemCopyInfo, itree) : NULL;
189}
190
191static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l)
192{
193 IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l);
194 return r ? container_of(r, MemCopyInfo, itree) : NULL;
195}
196
197static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc)
198{
199 TCGTemp *ts = mc->ts;
200 TempOptInfo *ti = ts_info(ts);
201
202 interval_tree_remove(&mc->itree, &ctx->mem_copy);
203 QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next);
204 QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next);
205}
206
207static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l)
208{
209 while (true) {
210 MemCopyInfo *mc = mem_copy_first(ctx, s, l);
211 if (!mc) {
212 break;
213 }
214 remove_mem_copy(ctx, mc);
215 }
216}
217
218static void remove_mem_copy_all(OptContext *ctx)
219{
220 remove_mem_copy_in(ctx, 0, -1);
221 tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy));
222}
223
Richard Henderson9f75e522023-11-02 13:37:46 -0700224static TCGTemp *find_better_copy(TCGTemp *ts)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200225{
Richard Henderson9f75e522023-11-02 13:37:46 -0700226 TCGTemp *i, *ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200227
Richard Henderson4c868ce2020-04-23 09:02:23 -0700228 /* If this is already readonly, we can't do better. */
229 if (temp_readonly(ts)) {
Richard Henderson63490392017-06-20 13:43:15 -0700230 return ts;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200231 }
232
Richard Henderson9f75e522023-11-02 13:37:46 -0700233 ret = ts;
Richard Henderson63490392017-06-20 13:43:15 -0700234 for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700235 ret = cmp_better_copy(ret, i);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200236 }
Richard Henderson9f75e522023-11-02 13:37:46 -0700237 return ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200238}
239
Richard Hendersonab84dc32023-08-23 23:04:24 -0700240static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts)
241{
242 TempOptInfo *si = ts_info(src_ts);
243 TempOptInfo *di = ts_info(dst_ts);
244 MemCopyInfo *mc;
245
246 QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) {
247 tcg_debug_assert(mc->ts == src_ts);
248 mc->ts = dst_ts;
249 }
250 QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy);
251}
252
253/* Reset TEMP's state, possibly removing the temp for the list of copies. */
254static void reset_ts(OptContext *ctx, TCGTemp *ts)
255{
256 TempOptInfo *ti = ts_info(ts);
257 TCGTemp *pts = ti->prev_copy;
258 TCGTemp *nts = ti->next_copy;
259 TempOptInfo *pi = ts_info(pts);
260 TempOptInfo *ni = ts_info(nts);
261
262 ni->prev_copy = ti->prev_copy;
263 pi->next_copy = ti->next_copy;
264 ti->next_copy = ts;
265 ti->prev_copy = ts;
266 ti->is_const = false;
267 ti->z_mask = -1;
268 ti->s_mask = 0;
269
270 if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) {
271 if (ts == nts) {
272 /* Last temp copy being removed, the mem copies die. */
273 MemCopyInfo *mc;
274 QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) {
275 interval_tree_remove(&mc->itree, &ctx->mem_copy);
276 }
277 QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy);
278 } else {
279 move_mem_copies(find_better_copy(nts), ts);
280 }
281 }
282}
283
284static void reset_temp(OptContext *ctx, TCGArg arg)
285{
286 reset_ts(ctx, arg_temp(arg));
287}
288
289static void record_mem_copy(OptContext *ctx, TCGType type,
290 TCGTemp *ts, intptr_t start, intptr_t last)
291{
292 MemCopyInfo *mc;
293 TempOptInfo *ti;
294
295 mc = QSIMPLEQ_FIRST(&ctx->mem_free);
296 if (mc) {
297 QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next);
298 } else {
299 mc = tcg_malloc(sizeof(*mc));
300 }
301
302 memset(mc, 0, sizeof(*mc));
303 mc->itree.start = start;
304 mc->itree.last = last;
305 mc->type = type;
306 interval_tree_insert(&mc->itree, &ctx->mem_copy);
307
308 ts = find_better_copy(ts);
309 ti = ts_info(ts);
310 mc->ts = ts;
311 QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next);
312}
313
Richard Henderson63490392017-06-20 13:43:15 -0700314static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200315{
Richard Henderson63490392017-06-20 13:43:15 -0700316 TCGTemp *i;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200317
Richard Henderson63490392017-06-20 13:43:15 -0700318 if (ts1 == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200319 return true;
320 }
321
Richard Henderson63490392017-06-20 13:43:15 -0700322 if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200323 return false;
324 }
325
Richard Henderson63490392017-06-20 13:43:15 -0700326 for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
327 if (i == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200328 return true;
329 }
330 }
331
332 return false;
333}
334
Richard Henderson63490392017-06-20 13:43:15 -0700335static bool args_are_copies(TCGArg arg1, TCGArg arg2)
336{
337 return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
338}
339
Richard Hendersonab84dc32023-08-23 23:04:24 -0700340static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s)
341{
342 MemCopyInfo *mc;
343
344 for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) {
345 if (mc->itree.start == s && mc->type == type) {
346 return find_better_copy(mc->ts);
347 }
348 }
349 return NULL;
350}
351
Richard Henderson26aac972023-10-23 12:31:57 -0700352static TCGArg arg_new_constant(OptContext *ctx, uint64_t val)
353{
354 TCGType type = ctx->type;
355 TCGTemp *ts;
356
357 if (type == TCG_TYPE_I32) {
358 val = (int32_t)val;
359 }
360
361 ts = tcg_constant_internal(type, val);
362 init_ts_info(ctx, ts);
363
364 return temp_arg(ts);
365}
366
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700367static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400368{
Richard Henderson63490392017-06-20 13:43:15 -0700369 TCGTemp *dst_ts = arg_temp(dst);
370 TCGTemp *src_ts = arg_temp(src);
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700371 TempOptInfo *di;
372 TempOptInfo *si;
Richard Henderson63490392017-06-20 13:43:15 -0700373 TCGOpcode new_op;
374
375 if (ts_are_copies(dst_ts, src_ts)) {
Richard Hendersondc849882021-08-24 07:13:45 -0700376 tcg_op_remove(ctx->tcg, op);
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700377 return true;
Aurelien Jarno53657182015-06-04 21:53:25 +0200378 }
379
Richard Henderson986cac12023-01-09 13:59:35 -0800380 reset_ts(ctx, dst_ts);
Richard Henderson63490392017-06-20 13:43:15 -0700381 di = ts_info(dst_ts);
382 si = ts_info(src_ts);
Richard Henderson67f84c92021-08-25 08:00:20 -0700383
384 switch (ctx->type) {
385 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100386 new_op = INDEX_op_mov_i32;
Richard Henderson67f84c92021-08-25 08:00:20 -0700387 break;
388 case TCG_TYPE_I64:
389 new_op = INDEX_op_mov_i64;
390 break;
391 case TCG_TYPE_V64:
392 case TCG_TYPE_V128:
393 case TCG_TYPE_V256:
394 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
395 new_op = INDEX_op_mov_vec;
396 break;
397 default:
398 g_assert_not_reached();
Richard Henderson170ba882017-11-22 09:07:11 +0100399 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700400 op->opc = new_op;
Richard Henderson63490392017-06-20 13:43:15 -0700401 op->args[0] = dst;
402 op->args[1] = src;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700403
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700404 di->z_mask = si->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700405 di->s_mask = si->s_mask;
Richard Henderson24666ba2014-05-22 11:14:10 -0700406
Richard Henderson63490392017-06-20 13:43:15 -0700407 if (src_ts->type == dst_ts->type) {
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700408 TempOptInfo *ni = ts_info(si->next_copy);
Richard Henderson63490392017-06-20 13:43:15 -0700409
410 di->next_copy = si->next_copy;
411 di->prev_copy = src_ts;
412 ni->prev_copy = dst_ts;
413 si->next_copy = dst_ts;
414 di->is_const = si->is_const;
415 di->val = si->val;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700416
417 if (!QSIMPLEQ_EMPTY(&si->mem_copy)
418 && cmp_better_copy(src_ts, dst_ts) == dst_ts) {
419 move_mem_copies(dst_ts, src_ts);
420 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800421 }
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700422 return true;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400423}
424
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700425static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
Richard Hendersondc849882021-08-24 07:13:45 -0700426 TCGArg dst, uint64_t val)
Richard Henderson8fe35e02020-03-30 20:42:43 -0700427{
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700428 /* Convert movi to mov with constant temp. */
Richard Henderson26aac972023-10-23 12:31:57 -0700429 return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val));
Richard Henderson8fe35e02020-03-30 20:42:43 -0700430}
431
Richard Henderson54795542020-09-06 16:21:32 -0700432static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400433{
Richard Henderson03271522013-08-14 14:35:56 -0700434 uint64_t l64, h64;
435
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400436 switch (op) {
437 CASE_OP_32_64(add):
438 return x + y;
439
440 CASE_OP_32_64(sub):
441 return x - y;
442
443 CASE_OP_32_64(mul):
444 return x * y;
445
Richard Hendersonc578ff12021-12-16 06:07:25 -0800446 CASE_OP_32_64_VEC(and):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400447 return x & y;
448
Richard Hendersonc578ff12021-12-16 06:07:25 -0800449 CASE_OP_32_64_VEC(or):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400450 return x | y;
451
Richard Hendersonc578ff12021-12-16 06:07:25 -0800452 CASE_OP_32_64_VEC(xor):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400453 return x ^ y;
454
Kirill Batuzov55c09752011-07-07 16:37:16 +0400455 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700456 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400457
Kirill Batuzov55c09752011-07-07 16:37:16 +0400458 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700459 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400460
461 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700462 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400463
Kirill Batuzov55c09752011-07-07 16:37:16 +0400464 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700465 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400466
467 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700468 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400469
Kirill Batuzov55c09752011-07-07 16:37:16 +0400470 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700471 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400472
473 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700474 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400475
Kirill Batuzov55c09752011-07-07 16:37:16 +0400476 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700477 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400478
479 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700480 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400481
Kirill Batuzov55c09752011-07-07 16:37:16 +0400482 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700483 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400484
Richard Hendersonc578ff12021-12-16 06:07:25 -0800485 CASE_OP_32_64_VEC(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400486 return ~x;
487
Richard Hendersoncb25c802011-08-17 14:11:47 -0700488 CASE_OP_32_64(neg):
489 return -x;
490
Richard Hendersonc578ff12021-12-16 06:07:25 -0800491 CASE_OP_32_64_VEC(andc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700492 return x & ~y;
493
Richard Hendersonc578ff12021-12-16 06:07:25 -0800494 CASE_OP_32_64_VEC(orc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700495 return x | ~y;
496
Richard Hendersoned523472021-12-16 11:17:46 -0800497 CASE_OP_32_64_VEC(eqv):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700498 return ~(x ^ y);
499
Richard Hendersoned523472021-12-16 11:17:46 -0800500 CASE_OP_32_64_VEC(nand):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700501 return ~(x & y);
502
Richard Hendersoned523472021-12-16 11:17:46 -0800503 CASE_OP_32_64_VEC(nor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700504 return ~(x | y);
505
Richard Henderson0e28d002016-11-16 09:23:28 +0100506 case INDEX_op_clz_i32:
507 return (uint32_t)x ? clz32(x) : y;
508
509 case INDEX_op_clz_i64:
510 return x ? clz64(x) : y;
511
512 case INDEX_op_ctz_i32:
513 return (uint32_t)x ? ctz32(x) : y;
514
515 case INDEX_op_ctz_i64:
516 return x ? ctz64(x) : y;
517
Richard Hendersona768e4e2016-11-21 11:13:39 +0100518 case INDEX_op_ctpop_i32:
519 return ctpop32(x);
520
521 case INDEX_op_ctpop_i64:
522 return ctpop64(x);
523
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700524 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400525 return (int8_t)x;
526
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700527 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400528 return (int16_t)x;
529
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700530 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400531 return (uint8_t)x;
532
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700533 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400534 return (uint16_t)x;
535
Richard Henderson64985942018-11-20 08:53:34 +0100536 CASE_OP_32_64(bswap16):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700537 x = bswap16(x);
538 return y & TCG_BSWAP_OS ? (int16_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100539
540 CASE_OP_32_64(bswap32):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700541 x = bswap32(x);
542 return y & TCG_BSWAP_OS ? (int32_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100543
544 case INDEX_op_bswap64_i64:
545 return bswap64(x);
546
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200547 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400548 case INDEX_op_ext32s_i64:
549 return (int32_t)x;
550
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200551 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700552 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400553 case INDEX_op_ext32u_i64:
554 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400555
Richard Henderson609ad702015-07-24 07:16:00 -0700556 case INDEX_op_extrh_i64_i32:
557 return (uint64_t)x >> 32;
558
Richard Henderson03271522013-08-14 14:35:56 -0700559 case INDEX_op_muluh_i32:
560 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
561 case INDEX_op_mulsh_i32:
562 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
563
564 case INDEX_op_muluh_i64:
565 mulu64(&l64, &h64, x, y);
566 return h64;
567 case INDEX_op_mulsh_i64:
568 muls64(&l64, &h64, x, y);
569 return h64;
570
Richard Henderson01547f72013-08-14 15:22:46 -0700571 case INDEX_op_div_i32:
572 /* Avoid crashing on divide by zero, otherwise undefined. */
573 return (int32_t)x / ((int32_t)y ? : 1);
574 case INDEX_op_divu_i32:
575 return (uint32_t)x / ((uint32_t)y ? : 1);
576 case INDEX_op_div_i64:
577 return (int64_t)x / ((int64_t)y ? : 1);
578 case INDEX_op_divu_i64:
579 return (uint64_t)x / ((uint64_t)y ? : 1);
580
581 case INDEX_op_rem_i32:
582 return (int32_t)x % ((int32_t)y ? : 1);
583 case INDEX_op_remu_i32:
584 return (uint32_t)x % ((uint32_t)y ? : 1);
585 case INDEX_op_rem_i64:
586 return (int64_t)x % ((int64_t)y ? : 1);
587 case INDEX_op_remu_i64:
588 return (uint64_t)x % ((uint64_t)y ? : 1);
589
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400590 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700591 g_assert_not_reached();
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400592 }
593}
594
Richard Henderson67f84c92021-08-25 08:00:20 -0700595static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
596 uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400597{
Richard Henderson54795542020-09-06 16:21:32 -0700598 uint64_t res = do_constant_folding_2(op, x, y);
Richard Henderson67f84c92021-08-25 08:00:20 -0700599 if (type == TCG_TYPE_I32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200600 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400601 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400602 return res;
603}
604
Richard Henderson9519da72012-10-02 11:32:26 -0700605static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
606{
607 switch (c) {
608 case TCG_COND_EQ:
609 return x == y;
610 case TCG_COND_NE:
611 return x != y;
612 case TCG_COND_LT:
613 return (int32_t)x < (int32_t)y;
614 case TCG_COND_GE:
615 return (int32_t)x >= (int32_t)y;
616 case TCG_COND_LE:
617 return (int32_t)x <= (int32_t)y;
618 case TCG_COND_GT:
619 return (int32_t)x > (int32_t)y;
620 case TCG_COND_LTU:
621 return x < y;
622 case TCG_COND_GEU:
623 return x >= y;
624 case TCG_COND_LEU:
625 return x <= y;
626 case TCG_COND_GTU:
627 return x > y;
628 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700629 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700630 }
631}
632
633static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
634{
635 switch (c) {
636 case TCG_COND_EQ:
637 return x == y;
638 case TCG_COND_NE:
639 return x != y;
640 case TCG_COND_LT:
641 return (int64_t)x < (int64_t)y;
642 case TCG_COND_GE:
643 return (int64_t)x >= (int64_t)y;
644 case TCG_COND_LE:
645 return (int64_t)x <= (int64_t)y;
646 case TCG_COND_GT:
647 return (int64_t)x > (int64_t)y;
648 case TCG_COND_LTU:
649 return x < y;
650 case TCG_COND_GEU:
651 return x >= y;
652 case TCG_COND_LEU:
653 return x <= y;
654 case TCG_COND_GTU:
655 return x > y;
656 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700657 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700658 }
659}
660
661static bool do_constant_folding_cond_eq(TCGCond c)
662{
663 switch (c) {
664 case TCG_COND_GT:
665 case TCG_COND_LTU:
666 case TCG_COND_LT:
667 case TCG_COND_GTU:
668 case TCG_COND_NE:
669 return 0;
670 case TCG_COND_GE:
671 case TCG_COND_GEU:
672 case TCG_COND_LE:
673 case TCG_COND_LEU:
674 case TCG_COND_EQ:
675 return 1;
676 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700677 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700678 }
679}
680
Richard Henderson8d57bf12021-08-24 08:34:27 -0700681/*
682 * Return -1 if the condition can't be simplified,
683 * and the result of the condition (0 or 1) if it can.
684 */
Richard Henderson67f84c92021-08-25 08:00:20 -0700685static int do_constant_folding_cond(TCGType type, TCGArg x,
Richard Henderson8d57bf12021-08-24 08:34:27 -0700686 TCGArg y, TCGCond c)
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200687{
Richard Henderson63490392017-06-20 13:43:15 -0700688 if (arg_is_const(x) && arg_is_const(y)) {
Alex Bennée9becc362022-02-09 11:21:42 +0000689 uint64_t xv = arg_info(x)->val;
690 uint64_t yv = arg_info(y)->val;
691
Richard Henderson67f84c92021-08-25 08:00:20 -0700692 switch (type) {
693 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100694 return do_constant_folding_cond_32(xv, yv, c);
Richard Henderson67f84c92021-08-25 08:00:20 -0700695 case TCG_TYPE_I64:
696 return do_constant_folding_cond_64(xv, yv, c);
697 default:
698 /* Only scalar comparisons are optimizable */
699 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200700 }
Richard Henderson63490392017-06-20 13:43:15 -0700701 } else if (args_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700702 return do_constant_folding_cond_eq(c);
Richard Henderson27cdb852023-10-23 11:38:00 -0700703 } else if (arg_is_const_val(y, 0)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200704 switch (c) {
705 case TCG_COND_LTU:
706 return 0;
707 case TCG_COND_GEU:
708 return 1;
709 default:
Richard Henderson8d57bf12021-08-24 08:34:27 -0700710 return -1;
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200711 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200712 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700713 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200714}
715
Richard Henderson8d57bf12021-08-24 08:34:27 -0700716/*
717 * Return -1 if the condition can't be simplified,
718 * and the result of the condition (0 or 1) if it can.
719 */
720static int do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
Richard Henderson6c4382f2012-10-02 11:32:27 -0700721{
722 TCGArg al = p1[0], ah = p1[1];
723 TCGArg bl = p2[0], bh = p2[1];
724
Richard Henderson63490392017-06-20 13:43:15 -0700725 if (arg_is_const(bl) && arg_is_const(bh)) {
726 tcg_target_ulong blv = arg_info(bl)->val;
727 tcg_target_ulong bhv = arg_info(bh)->val;
728 uint64_t b = deposit64(blv, 32, 32, bhv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700729
Richard Henderson63490392017-06-20 13:43:15 -0700730 if (arg_is_const(al) && arg_is_const(ah)) {
731 tcg_target_ulong alv = arg_info(al)->val;
732 tcg_target_ulong ahv = arg_info(ah)->val;
733 uint64_t a = deposit64(alv, 32, 32, ahv);
Richard Henderson6c4382f2012-10-02 11:32:27 -0700734 return do_constant_folding_cond_64(a, b, c);
735 }
736 if (b == 0) {
737 switch (c) {
738 case TCG_COND_LTU:
739 return 0;
740 case TCG_COND_GEU:
741 return 1;
742 default:
743 break;
744 }
745 }
746 }
Richard Henderson63490392017-06-20 13:43:15 -0700747 if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700748 return do_constant_folding_cond_eq(c);
749 }
Richard Henderson8d57bf12021-08-24 08:34:27 -0700750 return -1;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700751}
752
Richard Henderson7a2f7082021-08-26 07:06:39 -0700753/**
754 * swap_commutative:
755 * @dest: TCGArg of the destination argument, or NO_DEST.
756 * @p1: first paired argument
757 * @p2: second paired argument
758 *
759 * If *@p1 is a constant and *@p2 is not, swap.
760 * If *@p2 matches @dest, swap.
761 * Return true if a swap was performed.
762 */
763
764#define NO_DEST temp_arg(NULL)
765
Richard Henderson24c9ae42012-10-02 11:32:21 -0700766static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
767{
768 TCGArg a1 = *p1, a2 = *p2;
769 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700770 sum += arg_is_const(a1);
771 sum -= arg_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700772
773 /* Prefer the constant in second argument, and then the form
774 op a, a, b, which is better handled on non-RISC hosts. */
775 if (sum > 0 || (sum == 0 && dest == a2)) {
776 *p1 = a2;
777 *p2 = a1;
778 return true;
779 }
780 return false;
781}
782
Richard Henderson0bfcb862012-10-02 11:32:23 -0700783static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
784{
785 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700786 sum += arg_is_const(p1[0]);
787 sum += arg_is_const(p1[1]);
788 sum -= arg_is_const(p2[0]);
789 sum -= arg_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700790 if (sum > 0) {
791 TCGArg t;
792 t = p1[0], p1[0] = p2[0], p2[0] = t;
793 t = p1[1], p1[1] = p2[1], p2[1] = t;
794 return true;
795 }
796 return false;
797}
798
Richard Henderson246c4b72023-10-24 16:36:50 -0700799static int do_constant_folding_cond1(OptContext *ctx, TCGArg dest,
800 TCGArg *p1, TCGArg *p2, TCGArg *pcond)
801{
802 TCGCond cond;
803 bool swap;
804 int r;
805
806 swap = swap_commutative(dest, p1, p2);
807 cond = *pcond;
808 if (swap) {
809 *pcond = cond = tcg_swap_cond(cond);
810 }
811
812 r = do_constant_folding_cond(ctx->type, *p1, *p2, cond);
813 return r;
814}
815
Richard Hendersone2577ea2021-08-24 08:00:48 -0700816static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
817{
818 for (int i = 0; i < nb_args; i++) {
819 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000820 init_ts_info(ctx, ts);
Richard Hendersone2577ea2021-08-24 08:00:48 -0700821 }
822}
823
Richard Henderson8774dde2021-08-24 08:04:47 -0700824static void copy_propagate(OptContext *ctx, TCGOp *op,
825 int nb_oargs, int nb_iargs)
826{
Richard Henderson8774dde2021-08-24 08:04:47 -0700827 for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
828 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000829 if (ts_is_copy(ts)) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700830 op->args[i] = temp_arg(find_better_copy(ts));
Richard Henderson8774dde2021-08-24 08:04:47 -0700831 }
832 }
833}
834
Richard Henderson137f1f42021-08-24 08:49:25 -0700835static void finish_folding(OptContext *ctx, TCGOp *op)
836{
837 const TCGOpDef *def = &tcg_op_defs[op->opc];
838 int i, nb_oargs;
839
840 /*
Richard Hendersond97f8f32023-10-16 19:10:42 -0700841 * We only optimize extended basic blocks. If the opcode ends a BB
842 * and is not a conditional branch, reset all temp data.
Richard Henderson137f1f42021-08-24 08:49:25 -0700843 */
844 if (def->flags & TCG_OPF_BB_END) {
Richard Henderson137f1f42021-08-24 08:49:25 -0700845 ctx->prev_mb = NULL;
Richard Hendersond97f8f32023-10-16 19:10:42 -0700846 if (!(def->flags & TCG_OPF_COND_BRANCH)) {
847 memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
Richard Hendersonab84dc32023-08-23 23:04:24 -0700848 remove_mem_copy_all(ctx);
Richard Hendersond97f8f32023-10-16 19:10:42 -0700849 }
Richard Henderson137f1f42021-08-24 08:49:25 -0700850 return;
851 }
852
853 nb_oargs = def->nb_oargs;
854 for (i = 0; i < nb_oargs; i++) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700855 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson986cac12023-01-09 13:59:35 -0800856 reset_ts(ctx, ts);
Richard Henderson137f1f42021-08-24 08:49:25 -0700857 /*
Richard Henderson57fe5c62021-08-26 12:04:46 -0700858 * Save the corresponding known-zero/sign bits mask for the
Richard Henderson137f1f42021-08-24 08:49:25 -0700859 * first output argument (only one supported so far).
860 */
861 if (i == 0) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700862 ts_info(ts)->z_mask = ctx->z_mask;
863 ts_info(ts)->s_mask = ctx->s_mask;
Richard Henderson137f1f42021-08-24 08:49:25 -0700864 }
865 }
866}
867
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700868/*
869 * The fold_* functions return true when processing is complete,
870 * usually by folding the operation to a constant or to a copy,
871 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
872 * like collect information about the value produced, for use in
873 * optimizing a subsequent operation.
874 *
875 * These first fold_* functions are all helpers, used by other
876 * folders for more specific operations.
877 */
878
879static bool fold_const1(OptContext *ctx, TCGOp *op)
880{
881 if (arg_is_const(op->args[1])) {
882 uint64_t t;
883
884 t = arg_info(op->args[1])->val;
Richard Henderson67f84c92021-08-25 08:00:20 -0700885 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700886 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
887 }
888 return false;
889}
890
891static bool fold_const2(OptContext *ctx, TCGOp *op)
892{
893 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
894 uint64_t t1 = arg_info(op->args[1])->val;
895 uint64_t t2 = arg_info(op->args[2])->val;
896
Richard Henderson67f84c92021-08-25 08:00:20 -0700897 t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700898 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
899 }
900 return false;
901}
902
Richard Hendersonc578ff12021-12-16 06:07:25 -0800903static bool fold_commutative(OptContext *ctx, TCGOp *op)
904{
905 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
906 return false;
907}
908
Richard Henderson7a2f7082021-08-26 07:06:39 -0700909static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
910{
911 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
912 return fold_const2(ctx, op);
913}
914
Richard Hendersonfae450b2021-08-25 22:42:19 -0700915static bool fold_masks(OptContext *ctx, TCGOp *op)
916{
917 uint64_t a_mask = ctx->a_mask;
918 uint64_t z_mask = ctx->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700919 uint64_t s_mask = ctx->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -0700920
921 /*
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700922 * 32-bit ops generate 32-bit results, which for the purpose of
923 * simplifying tcg are sign-extended. Certainly that's how we
924 * represent our constants elsewhere. Note that the bits will
925 * be reset properly for a 64-bit value when encountering the
926 * type changing opcodes.
Richard Hendersonfae450b2021-08-25 22:42:19 -0700927 */
928 if (ctx->type == TCG_TYPE_I32) {
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700929 a_mask = (int32_t)a_mask;
930 z_mask = (int32_t)z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700931 s_mask |= MAKE_64BIT_MASK(32, 32);
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700932 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700933 ctx->s_mask = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -0700934 }
935
936 if (z_mask == 0) {
937 return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
938 }
939 if (a_mask == 0) {
940 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
941 }
942 return false;
943}
944
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700945/*
946 * Convert @op to NOT, if NOT is supported by the host.
947 * Return true f the conversion is successful, which will still
948 * indicate that the processing is complete.
949 */
950static bool fold_not(OptContext *ctx, TCGOp *op);
951static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
952{
953 TCGOpcode not_op;
954 bool have_not;
955
956 switch (ctx->type) {
957 case TCG_TYPE_I32:
958 not_op = INDEX_op_not_i32;
959 have_not = TCG_TARGET_HAS_not_i32;
960 break;
961 case TCG_TYPE_I64:
962 not_op = INDEX_op_not_i64;
963 have_not = TCG_TARGET_HAS_not_i64;
964 break;
965 case TCG_TYPE_V64:
966 case TCG_TYPE_V128:
967 case TCG_TYPE_V256:
968 not_op = INDEX_op_not_vec;
969 have_not = TCG_TARGET_HAS_not_vec;
970 break;
971 default:
972 g_assert_not_reached();
973 }
974 if (have_not) {
975 op->opc = not_op;
976 op->args[1] = op->args[idx];
977 return fold_not(ctx, op);
978 }
979 return false;
980}
981
Richard Hendersonda48e272021-08-25 20:42:04 -0700982/* If the binary operation has first argument @i, fold to @i. */
983static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
984{
Richard Henderson27cdb852023-10-23 11:38:00 -0700985 if (arg_is_const_val(op->args[1], i)) {
Richard Hendersonda48e272021-08-25 20:42:04 -0700986 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
987 }
988 return false;
989}
990
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700991/* If the binary operation has first argument @i, fold to NOT. */
992static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
993{
Richard Henderson27cdb852023-10-23 11:38:00 -0700994 if (arg_is_const_val(op->args[1], i)) {
Richard Henderson0e0a32b2021-08-24 13:18:01 -0700995 return fold_to_not(ctx, op, 2);
996 }
997 return false;
998}
999
Richard Hendersone8679952021-08-25 13:19:52 -07001000/* If the binary operation has second argument @i, fold to @i. */
1001static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1002{
Richard Henderson27cdb852023-10-23 11:38:00 -07001003 if (arg_is_const_val(op->args[2], i)) {
Richard Hendersone8679952021-08-25 13:19:52 -07001004 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1005 }
1006 return false;
1007}
1008
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001009/* If the binary operation has second argument @i, fold to identity. */
1010static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
1011{
Richard Henderson27cdb852023-10-23 11:38:00 -07001012 if (arg_is_const_val(op->args[2], i)) {
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001013 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1014 }
1015 return false;
1016}
1017
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001018/* If the binary operation has second argument @i, fold to NOT. */
1019static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
1020{
Richard Henderson27cdb852023-10-23 11:38:00 -07001021 if (arg_is_const_val(op->args[2], i)) {
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001022 return fold_to_not(ctx, op, 1);
1023 }
1024 return false;
1025}
1026
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001027/* If the binary operation has both arguments equal, fold to @i. */
1028static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1029{
1030 if (args_are_copies(op->args[1], op->args[2])) {
1031 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1032 }
1033 return false;
1034}
1035
Richard Hendersonca7bb042021-08-25 13:14:21 -07001036/* If the binary operation has both arguments equal, fold to identity. */
1037static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
1038{
1039 if (args_are_copies(op->args[1], op->args[2])) {
1040 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1041 }
1042 return false;
1043}
1044
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001045/*
1046 * These outermost fold_<op> functions are sorted alphabetically.
Richard Hendersonca7bb042021-08-25 13:14:21 -07001047 *
1048 * The ordering of the transformations should be:
1049 * 1) those that produce a constant
1050 * 2) those that produce a copy
1051 * 3) those that produce information about the result value.
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001052 */
1053
1054static bool fold_add(OptContext *ctx, TCGOp *op)
1055{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001056 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001057 fold_xi_to_x(ctx, op, 0)) {
1058 return true;
1059 }
1060 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001061}
1062
Richard Hendersonc578ff12021-12-16 06:07:25 -08001063/* We cannot as yet do_constant_folding with vectors. */
1064static bool fold_add_vec(OptContext *ctx, TCGOp *op)
1065{
1066 if (fold_commutative(ctx, op) ||
1067 fold_xi_to_x(ctx, op, 0)) {
1068 return true;
1069 }
1070 return false;
1071}
1072
Richard Henderson9531c072021-08-26 06:51:39 -07001073static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001074{
Richard Hendersonf2457572023-10-25 18:39:44 -07001075 bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]);
1076 bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]);
1077
1078 if (a_const && b_const) {
Richard Henderson9531c072021-08-26 06:51:39 -07001079 uint64_t al = arg_info(op->args[2])->val;
1080 uint64_t ah = arg_info(op->args[3])->val;
1081 uint64_t bl = arg_info(op->args[4])->val;
1082 uint64_t bh = arg_info(op->args[5])->val;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001083 TCGArg rl, rh;
Richard Henderson9531c072021-08-26 06:51:39 -07001084 TCGOp *op2;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001085
Richard Henderson9531c072021-08-26 06:51:39 -07001086 if (ctx->type == TCG_TYPE_I32) {
1087 uint64_t a = deposit64(al, 32, 32, ah);
1088 uint64_t b = deposit64(bl, 32, 32, bh);
1089
1090 if (add) {
1091 a += b;
1092 } else {
1093 a -= b;
1094 }
1095
1096 al = sextract64(a, 0, 32);
1097 ah = sextract64(a, 32, 32);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001098 } else {
Richard Henderson9531c072021-08-26 06:51:39 -07001099 Int128 a = int128_make128(al, ah);
1100 Int128 b = int128_make128(bl, bh);
1101
1102 if (add) {
1103 a = int128_add(a, b);
1104 } else {
1105 a = int128_sub(a, b);
1106 }
1107
1108 al = int128_getlo(a);
1109 ah = int128_gethi(a);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001110 }
1111
1112 rl = op->args[0];
1113 rh = op->args[1];
Richard Henderson9531c072021-08-26 06:51:39 -07001114
1115 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01001116 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson9531c072021-08-26 06:51:39 -07001117
1118 tcg_opt_gen_movi(ctx, op, rl, al);
1119 tcg_opt_gen_movi(ctx, op2, rh, ah);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001120 return true;
1121 }
Richard Hendersonf2457572023-10-25 18:39:44 -07001122
1123 /* Fold sub2 r,x,i to add2 r,x,-i */
1124 if (!add && b_const) {
1125 uint64_t bl = arg_info(op->args[4])->val;
1126 uint64_t bh = arg_info(op->args[5])->val;
1127
1128 /* Negate the two parts without assembling and disassembling. */
1129 bl = -bl;
1130 bh = ~bh + !bl;
1131
1132 op->opc = (ctx->type == TCG_TYPE_I32
1133 ? INDEX_op_add2_i32 : INDEX_op_add2_i64);
1134 op->args[4] = arg_new_constant(ctx, bl);
1135 op->args[5] = arg_new_constant(ctx, bh);
1136 }
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001137 return false;
1138}
1139
Richard Henderson9531c072021-08-26 06:51:39 -07001140static bool fold_add2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001141{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001142 /* Note that the high and low parts may be independently swapped. */
1143 swap_commutative(op->args[0], &op->args[2], &op->args[4]);
1144 swap_commutative(op->args[1], &op->args[3], &op->args[5]);
1145
Richard Henderson9531c072021-08-26 06:51:39 -07001146 return fold_addsub2(ctx, op, true);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001147}
1148
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001149static bool fold_and(OptContext *ctx, TCGOp *op)
1150{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001151 uint64_t z1, z2;
1152
Richard Henderson7a2f7082021-08-26 07:06:39 -07001153 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001154 fold_xi_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001155 fold_xi_to_x(ctx, op, -1) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001156 fold_xx_to_x(ctx, op)) {
1157 return true;
1158 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001159
1160 z1 = arg_info(op->args[1])->z_mask;
1161 z2 = arg_info(op->args[2])->z_mask;
1162 ctx->z_mask = z1 & z2;
1163
1164 /*
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001165 * Sign repetitions are perforce all identical, whether they are 1 or 0.
1166 * Bitwise operations preserve the relative quantity of the repetitions.
1167 */
1168 ctx->s_mask = arg_info(op->args[1])->s_mask
1169 & arg_info(op->args[2])->s_mask;
1170
1171 /*
Richard Hendersonfae450b2021-08-25 22:42:19 -07001172 * Known-zeros does not imply known-ones. Therefore unless
1173 * arg2 is constant, we can't infer affected bits from it.
1174 */
1175 if (arg_is_const(op->args[2])) {
1176 ctx->a_mask = z1 & ~z2;
1177 }
1178
1179 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001180}
1181
1182static bool fold_andc(OptContext *ctx, TCGOp *op)
1183{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001184 uint64_t z1;
1185
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001186 if (fold_const2(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001187 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001188 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001189 fold_ix_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001190 return true;
1191 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001192
1193 z1 = arg_info(op->args[1])->z_mask;
1194
1195 /*
1196 * Known-zeros does not imply known-ones. Therefore unless
1197 * arg2 is constant, we can't infer anything from it.
1198 */
1199 if (arg_is_const(op->args[2])) {
1200 uint64_t z2 = ~arg_info(op->args[2])->z_mask;
1201 ctx->a_mask = z1 & ~z2;
1202 z1 &= z2;
1203 }
1204 ctx->z_mask = z1;
1205
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001206 ctx->s_mask = arg_info(op->args[1])->s_mask
1207 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001208 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001209}
1210
Richard Henderson079b0802021-08-24 09:30:59 -07001211static bool fold_brcond(OptContext *ctx, TCGOp *op)
1212{
Richard Henderson246c4b72023-10-24 16:36:50 -07001213 int i = do_constant_folding_cond1(ctx, NO_DEST, &op->args[0],
1214 &op->args[1], &op->args[2]);
Richard Henderson079b0802021-08-24 09:30:59 -07001215 if (i == 0) {
1216 tcg_op_remove(ctx->tcg, op);
1217 return true;
1218 }
1219 if (i > 0) {
1220 op->opc = INDEX_op_br;
1221 op->args[0] = op->args[3];
1222 }
1223 return false;
1224}
1225
Richard Henderson764d2ab2021-08-24 09:22:11 -07001226static bool fold_brcond2(OptContext *ctx, TCGOp *op)
1227{
1228 TCGCond cond = op->args[4];
Richard Henderson764d2ab2021-08-24 09:22:11 -07001229 TCGArg label = op->args[5];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001230 int i, inv = 0;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001231
Richard Henderson7a2f7082021-08-26 07:06:39 -07001232 if (swap_commutative2(&op->args[0], &op->args[2])) {
1233 op->args[4] = cond = tcg_swap_cond(cond);
1234 }
1235
1236 i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond);
Richard Henderson764d2ab2021-08-24 09:22:11 -07001237 if (i >= 0) {
1238 goto do_brcond_const;
1239 }
1240
1241 switch (cond) {
1242 case TCG_COND_LT:
1243 case TCG_COND_GE:
1244 /*
1245 * Simplify LT/GE comparisons vs zero to a single compare
1246 * vs the high word of the input.
1247 */
Richard Henderson27cdb852023-10-23 11:38:00 -07001248 if (arg_is_const_val(op->args[2], 0) &&
1249 arg_is_const_val(op->args[3], 0)) {
Richard Henderson764d2ab2021-08-24 09:22:11 -07001250 goto do_brcond_high;
1251 }
1252 break;
1253
1254 case TCG_COND_NE:
1255 inv = 1;
1256 QEMU_FALLTHROUGH;
1257 case TCG_COND_EQ:
1258 /*
1259 * Simplify EQ/NE comparisons where one of the pairs
1260 * can be simplified.
1261 */
Richard Henderson67f84c92021-08-25 08:00:20 -07001262 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001263 op->args[2], cond);
1264 switch (i ^ inv) {
1265 case 0:
1266 goto do_brcond_const;
1267 case 1:
1268 goto do_brcond_high;
1269 }
1270
Richard Henderson67f84c92021-08-25 08:00:20 -07001271 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001272 op->args[3], cond);
1273 switch (i ^ inv) {
1274 case 0:
1275 goto do_brcond_const;
1276 case 1:
1277 op->opc = INDEX_op_brcond_i32;
1278 op->args[1] = op->args[2];
1279 op->args[2] = cond;
1280 op->args[3] = label;
1281 break;
1282 }
1283 break;
1284
1285 default:
1286 break;
1287
1288 do_brcond_high:
1289 op->opc = INDEX_op_brcond_i32;
1290 op->args[0] = op->args[1];
1291 op->args[1] = op->args[3];
1292 op->args[2] = cond;
1293 op->args[3] = label;
1294 break;
1295
1296 do_brcond_const:
1297 if (i == 0) {
1298 tcg_op_remove(ctx->tcg, op);
1299 return true;
1300 }
1301 op->opc = INDEX_op_br;
1302 op->args[0] = label;
1303 break;
1304 }
1305 return false;
1306}
1307
Richard Henderson09bacdc2021-08-24 11:58:12 -07001308static bool fold_bswap(OptContext *ctx, TCGOp *op)
1309{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001310 uint64_t z_mask, s_mask, sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001311
Richard Henderson09bacdc2021-08-24 11:58:12 -07001312 if (arg_is_const(op->args[1])) {
1313 uint64_t t = arg_info(op->args[1])->val;
1314
Richard Henderson67f84c92021-08-25 08:00:20 -07001315 t = do_constant_folding(op->opc, ctx->type, t, op->args[2]);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001316 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1317 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001318
1319 z_mask = arg_info(op->args[1])->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001320
Richard Hendersonfae450b2021-08-25 22:42:19 -07001321 switch (op->opc) {
1322 case INDEX_op_bswap16_i32:
1323 case INDEX_op_bswap16_i64:
1324 z_mask = bswap16(z_mask);
1325 sign = INT16_MIN;
1326 break;
1327 case INDEX_op_bswap32_i32:
1328 case INDEX_op_bswap32_i64:
1329 z_mask = bswap32(z_mask);
1330 sign = INT32_MIN;
1331 break;
1332 case INDEX_op_bswap64_i64:
1333 z_mask = bswap64(z_mask);
1334 sign = INT64_MIN;
1335 break;
1336 default:
1337 g_assert_not_reached();
1338 }
Richard Henderson57fe5c62021-08-26 12:04:46 -07001339 s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001340
1341 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1342 case TCG_BSWAP_OZ:
1343 break;
1344 case TCG_BSWAP_OS:
1345 /* If the sign bit may be 1, force all the bits above to 1. */
1346 if (z_mask & sign) {
1347 z_mask |= sign;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001348 s_mask = sign << 1;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001349 }
1350 break;
1351 default:
1352 /* The high bits are undefined: force all bits above the sign to 1. */
1353 z_mask |= sign << 1;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001354 s_mask = 0;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001355 break;
1356 }
1357 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001358 ctx->s_mask = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001359
1360 return fold_masks(ctx, op);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001361}
1362
Richard Henderson5cf32be2021-08-24 08:17:08 -07001363static bool fold_call(OptContext *ctx, TCGOp *op)
1364{
1365 TCGContext *s = ctx->tcg;
1366 int nb_oargs = TCGOP_CALLO(op);
1367 int nb_iargs = TCGOP_CALLI(op);
1368 int flags, i;
1369
1370 init_arguments(ctx, op, nb_oargs + nb_iargs);
1371 copy_propagate(ctx, op, nb_oargs, nb_iargs);
1372
1373 /* If the function reads or writes globals, reset temp data. */
1374 flags = tcg_call_flags(op);
1375 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1376 int nb_globals = s->nb_globals;
1377
1378 for (i = 0; i < nb_globals; i++) {
1379 if (test_bit(i, ctx->temps_used.l)) {
Richard Henderson986cac12023-01-09 13:59:35 -08001380 reset_ts(ctx, &ctx->tcg->temps[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001381 }
1382 }
1383 }
1384
Richard Hendersonab84dc32023-08-23 23:04:24 -07001385 /* If the function has side effects, reset mem data. */
1386 if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) {
1387 remove_mem_copy_all(ctx);
1388 }
1389
Richard Henderson5cf32be2021-08-24 08:17:08 -07001390 /* Reset temp data for outputs. */
1391 for (i = 0; i < nb_oargs; i++) {
Richard Henderson986cac12023-01-09 13:59:35 -08001392 reset_temp(ctx, op->args[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001393 }
1394
1395 /* Stop optimizing MB across calls. */
1396 ctx->prev_mb = NULL;
1397 return true;
1398}
1399
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001400static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1401{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001402 uint64_t z_mask;
1403
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001404 if (arg_is_const(op->args[1])) {
1405 uint64_t t = arg_info(op->args[1])->val;
1406
1407 if (t != 0) {
Richard Henderson67f84c92021-08-25 08:00:20 -07001408 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001409 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1410 }
1411 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1412 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001413
1414 switch (ctx->type) {
1415 case TCG_TYPE_I32:
1416 z_mask = 31;
1417 break;
1418 case TCG_TYPE_I64:
1419 z_mask = 63;
1420 break;
1421 default:
1422 g_assert_not_reached();
1423 }
1424 ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask;
Richard Henderson2b9d0c52021-08-26 13:24:17 -07001425 ctx->s_mask = smask_from_zmask(ctx->z_mask);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001426 return false;
1427}
1428
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001429static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1430{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001431 if (fold_const1(ctx, op)) {
1432 return true;
1433 }
1434
1435 switch (ctx->type) {
1436 case TCG_TYPE_I32:
1437 ctx->z_mask = 32 | 31;
1438 break;
1439 case TCG_TYPE_I64:
1440 ctx->z_mask = 64 | 63;
1441 break;
1442 default:
1443 g_assert_not_reached();
1444 }
Richard Henderson2b9d0c52021-08-26 13:24:17 -07001445 ctx->s_mask = smask_from_zmask(ctx->z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001446 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001447}
1448
Richard Henderson1b1907b2021-08-24 10:47:04 -07001449static bool fold_deposit(OptContext *ctx, TCGOp *op)
1450{
Richard Henderson8f7a8402023-08-13 11:03:05 -07001451 TCGOpcode and_opc;
1452
Richard Henderson1b1907b2021-08-24 10:47:04 -07001453 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1454 uint64_t t1 = arg_info(op->args[1])->val;
1455 uint64_t t2 = arg_info(op->args[2])->val;
1456
1457 t1 = deposit64(t1, op->args[3], op->args[4], t2);
1458 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1459 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001460
Richard Henderson8f7a8402023-08-13 11:03:05 -07001461 switch (ctx->type) {
1462 case TCG_TYPE_I32:
1463 and_opc = INDEX_op_and_i32;
1464 break;
1465 case TCG_TYPE_I64:
1466 and_opc = INDEX_op_and_i64;
1467 break;
1468 default:
1469 g_assert_not_reached();
1470 }
1471
1472 /* Inserting a value into zero at offset 0. */
Richard Henderson27cdb852023-10-23 11:38:00 -07001473 if (arg_is_const_val(op->args[1], 0) && op->args[3] == 0) {
Richard Henderson8f7a8402023-08-13 11:03:05 -07001474 uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
1475
1476 op->opc = and_opc;
1477 op->args[1] = op->args[2];
Richard Henderson26aac972023-10-23 12:31:57 -07001478 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001479 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1480 return false;
1481 }
1482
1483 /* Inserting zero into a value. */
Richard Henderson27cdb852023-10-23 11:38:00 -07001484 if (arg_is_const_val(op->args[2], 0)) {
Richard Henderson8f7a8402023-08-13 11:03:05 -07001485 uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
1486
1487 op->opc = and_opc;
Richard Henderson26aac972023-10-23 12:31:57 -07001488 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001489 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1490 return false;
1491 }
1492
Richard Hendersonfae450b2021-08-25 22:42:19 -07001493 ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
1494 op->args[3], op->args[4],
1495 arg_info(op->args[2])->z_mask);
Richard Henderson1b1907b2021-08-24 10:47:04 -07001496 return false;
1497}
1498
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001499static bool fold_divide(OptContext *ctx, TCGOp *op)
1500{
Richard Henderson2f9d9a32021-10-25 11:30:14 -07001501 if (fold_const2(ctx, op) ||
1502 fold_xi_to_x(ctx, op, 1)) {
1503 return true;
1504 }
1505 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001506}
1507
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07001508static bool fold_dup(OptContext *ctx, TCGOp *op)
1509{
1510 if (arg_is_const(op->args[1])) {
1511 uint64_t t = arg_info(op->args[1])->val;
1512 t = dup_const(TCGOP_VECE(op), t);
1513 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1514 }
1515 return false;
1516}
1517
1518static bool fold_dup2(OptContext *ctx, TCGOp *op)
1519{
1520 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1521 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1522 arg_info(op->args[2])->val);
1523 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1524 }
1525
1526 if (args_are_copies(op->args[1], op->args[2])) {
1527 op->opc = INDEX_op_dup_vec;
1528 TCGOP_VECE(op) = MO_32;
1529 }
1530 return false;
1531}
1532
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001533static bool fold_eqv(OptContext *ctx, TCGOp *op)
1534{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001535 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001536 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001537 fold_xi_to_not(ctx, op, 0)) {
1538 return true;
1539 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001540
1541 ctx->s_mask = arg_info(op->args[1])->s_mask
1542 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001543 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001544}
1545
Richard Hendersonb6617c82021-08-24 10:44:53 -07001546static bool fold_extract(OptContext *ctx, TCGOp *op)
1547{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001548 uint64_t z_mask_old, z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001549 int pos = op->args[2];
1550 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07001551
Richard Hendersonb6617c82021-08-24 10:44:53 -07001552 if (arg_is_const(op->args[1])) {
1553 uint64_t t;
1554
1555 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001556 t = extract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001557 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1558 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001559
1560 z_mask_old = arg_info(op->args[1])->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001561 z_mask = extract64(z_mask_old, pos, len);
1562 if (pos == 0) {
Richard Hendersonfae450b2021-08-25 22:42:19 -07001563 ctx->a_mask = z_mask_old ^ z_mask;
1564 }
1565 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001566 ctx->s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001567
1568 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001569}
1570
Richard Hendersondcd08992021-08-24 10:41:39 -07001571static bool fold_extract2(OptContext *ctx, TCGOp *op)
1572{
1573 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1574 uint64_t v1 = arg_info(op->args[1])->val;
1575 uint64_t v2 = arg_info(op->args[2])->val;
1576 int shr = op->args[3];
1577
1578 if (op->opc == INDEX_op_extract2_i64) {
1579 v1 >>= shr;
1580 v2 <<= 64 - shr;
1581 } else {
1582 v1 = (uint32_t)v1 >> shr;
Richard Henderson225bec02021-11-09 23:17:59 +01001583 v2 = (uint64_t)((int32_t)v2 << (32 - shr));
Richard Hendersondcd08992021-08-24 10:41:39 -07001584 }
1585 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1586 }
1587 return false;
1588}
1589
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001590static bool fold_exts(OptContext *ctx, TCGOp *op)
1591{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001592 uint64_t s_mask_old, s_mask, z_mask, sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001593 bool type_change = false;
1594
1595 if (fold_const1(ctx, op)) {
1596 return true;
1597 }
1598
Richard Henderson57fe5c62021-08-26 12:04:46 -07001599 z_mask = arg_info(op->args[1])->z_mask;
1600 s_mask = arg_info(op->args[1])->s_mask;
1601 s_mask_old = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001602
1603 switch (op->opc) {
1604 CASE_OP_32_64(ext8s):
1605 sign = INT8_MIN;
1606 z_mask = (uint8_t)z_mask;
1607 break;
1608 CASE_OP_32_64(ext16s):
1609 sign = INT16_MIN;
1610 z_mask = (uint16_t)z_mask;
1611 break;
1612 case INDEX_op_ext_i32_i64:
1613 type_change = true;
1614 QEMU_FALLTHROUGH;
1615 case INDEX_op_ext32s_i64:
1616 sign = INT32_MIN;
1617 z_mask = (uint32_t)z_mask;
1618 break;
1619 default:
1620 g_assert_not_reached();
1621 }
1622
1623 if (z_mask & sign) {
1624 z_mask |= sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001625 }
Richard Henderson57fe5c62021-08-26 12:04:46 -07001626 s_mask |= sign << 1;
1627
Richard Hendersonfae450b2021-08-25 22:42:19 -07001628 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001629 ctx->s_mask = s_mask;
1630 if (!type_change) {
1631 ctx->a_mask = s_mask & ~s_mask_old;
1632 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001633
1634 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001635}
1636
1637static bool fold_extu(OptContext *ctx, TCGOp *op)
1638{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001639 uint64_t z_mask_old, z_mask;
1640 bool type_change = false;
1641
1642 if (fold_const1(ctx, op)) {
1643 return true;
1644 }
1645
1646 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1647
1648 switch (op->opc) {
1649 CASE_OP_32_64(ext8u):
1650 z_mask = (uint8_t)z_mask;
1651 break;
1652 CASE_OP_32_64(ext16u):
1653 z_mask = (uint16_t)z_mask;
1654 break;
1655 case INDEX_op_extrl_i64_i32:
1656 case INDEX_op_extu_i32_i64:
1657 type_change = true;
1658 QEMU_FALLTHROUGH;
1659 case INDEX_op_ext32u_i64:
1660 z_mask = (uint32_t)z_mask;
1661 break;
1662 case INDEX_op_extrh_i64_i32:
1663 type_change = true;
1664 z_mask >>= 32;
1665 break;
1666 default:
1667 g_assert_not_reached();
1668 }
1669
1670 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001671 ctx->s_mask = smask_from_zmask(z_mask);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001672 if (!type_change) {
1673 ctx->a_mask = z_mask_old ^ z_mask;
1674 }
1675 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001676}
1677
Richard Henderson3eefdf22021-08-25 11:06:43 -07001678static bool fold_mb(OptContext *ctx, TCGOp *op)
1679{
1680 /* Eliminate duplicate and redundant fence instructions. */
1681 if (ctx->prev_mb) {
1682 /*
1683 * Merge two barriers of the same type into one,
1684 * or a weaker barrier into a stronger one,
1685 * or two weaker barriers into a stronger one.
1686 * mb X; mb Y => mb X|Y
1687 * mb; strl => mb; st
1688 * ldaq; mb => ld; mb
1689 * ldaq; strl => ld; mb; st
1690 * Other combinations are also merged into a strong
1691 * barrier. This is stricter than specified but for
1692 * the purposes of TCG is better than not optimizing.
1693 */
1694 ctx->prev_mb->args[0] |= op->args[0];
1695 tcg_op_remove(ctx->tcg, op);
1696 } else {
1697 ctx->prev_mb = op;
1698 }
1699 return true;
1700}
1701
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001702static bool fold_mov(OptContext *ctx, TCGOp *op)
1703{
1704 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1705}
1706
Richard Henderson0c310a32021-08-24 10:37:24 -07001707static bool fold_movcond(OptContext *ctx, TCGOp *op)
1708{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001709 int i;
Richard Henderson0c310a32021-08-24 10:37:24 -07001710
Richard Henderson7a2f7082021-08-26 07:06:39 -07001711 /*
1712 * Canonicalize the "false" input reg to match the destination reg so
1713 * that the tcg backend can implement a "move if true" operation.
1714 */
1715 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
Richard Henderson246c4b72023-10-24 16:36:50 -07001716 op->args[5] = tcg_invert_cond(op->args[5]);
Richard Henderson7a2f7082021-08-26 07:06:39 -07001717 }
1718
Richard Henderson246c4b72023-10-24 16:36:50 -07001719 i = do_constant_folding_cond1(ctx, NO_DEST, &op->args[1],
1720 &op->args[2], &op->args[5]);
Richard Henderson0c310a32021-08-24 10:37:24 -07001721 if (i >= 0) {
1722 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1723 }
1724
Richard Hendersonfae450b2021-08-25 22:42:19 -07001725 ctx->z_mask = arg_info(op->args[3])->z_mask
1726 | arg_info(op->args[4])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001727 ctx->s_mask = arg_info(op->args[3])->s_mask
1728 & arg_info(op->args[4])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001729
Richard Henderson0c310a32021-08-24 10:37:24 -07001730 if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
1731 uint64_t tv = arg_info(op->args[3])->val;
1732 uint64_t fv = arg_info(op->args[4])->val;
Richard Henderson36355022023-08-04 23:24:04 +00001733 TCGOpcode opc, negopc = 0;
Richard Henderson246c4b72023-10-24 16:36:50 -07001734 TCGCond cond = op->args[5];
Richard Henderson0c310a32021-08-24 10:37:24 -07001735
Richard Henderson67f84c92021-08-25 08:00:20 -07001736 switch (ctx->type) {
1737 case TCG_TYPE_I32:
1738 opc = INDEX_op_setcond_i32;
Richard Henderson36355022023-08-04 23:24:04 +00001739 if (TCG_TARGET_HAS_negsetcond_i32) {
1740 negopc = INDEX_op_negsetcond_i32;
1741 }
1742 tv = (int32_t)tv;
1743 fv = (int32_t)fv;
Richard Henderson67f84c92021-08-25 08:00:20 -07001744 break;
1745 case TCG_TYPE_I64:
1746 opc = INDEX_op_setcond_i64;
Richard Henderson36355022023-08-04 23:24:04 +00001747 if (TCG_TARGET_HAS_negsetcond_i64) {
1748 negopc = INDEX_op_negsetcond_i64;
1749 }
Richard Henderson67f84c92021-08-25 08:00:20 -07001750 break;
1751 default:
1752 g_assert_not_reached();
1753 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001754
1755 if (tv == 1 && fv == 0) {
1756 op->opc = opc;
1757 op->args[3] = cond;
1758 } else if (fv == 1 && tv == 0) {
1759 op->opc = opc;
1760 op->args[3] = tcg_invert_cond(cond);
Richard Henderson36355022023-08-04 23:24:04 +00001761 } else if (negopc) {
1762 if (tv == -1 && fv == 0) {
1763 op->opc = negopc;
1764 op->args[3] = cond;
1765 } else if (fv == -1 && tv == 0) {
1766 op->opc = negopc;
1767 op->args[3] = tcg_invert_cond(cond);
1768 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001769 }
1770 }
1771 return false;
1772}
1773
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001774static bool fold_mul(OptContext *ctx, TCGOp *op)
1775{
Richard Hendersone8679952021-08-25 13:19:52 -07001776 if (fold_const2(ctx, op) ||
Richard Henderson5b5cf472021-10-25 11:19:14 -07001777 fold_xi_to_i(ctx, op, 0) ||
1778 fold_xi_to_x(ctx, op, 1)) {
Richard Hendersone8679952021-08-25 13:19:52 -07001779 return true;
1780 }
1781 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001782}
1783
1784static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
1785{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001786 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001787 fold_xi_to_i(ctx, op, 0)) {
1788 return true;
1789 }
1790 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001791}
1792
Richard Henderson407112b2021-08-26 06:33:04 -07001793static bool fold_multiply2(OptContext *ctx, TCGOp *op)
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001794{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001795 swap_commutative(op->args[0], &op->args[2], &op->args[3]);
1796
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001797 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
Richard Henderson407112b2021-08-26 06:33:04 -07001798 uint64_t a = arg_info(op->args[2])->val;
1799 uint64_t b = arg_info(op->args[3])->val;
1800 uint64_t h, l;
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001801 TCGArg rl, rh;
Richard Henderson407112b2021-08-26 06:33:04 -07001802 TCGOp *op2;
1803
1804 switch (op->opc) {
1805 case INDEX_op_mulu2_i32:
1806 l = (uint64_t)(uint32_t)a * (uint32_t)b;
1807 h = (int32_t)(l >> 32);
1808 l = (int32_t)l;
1809 break;
1810 case INDEX_op_muls2_i32:
1811 l = (int64_t)(int32_t)a * (int32_t)b;
1812 h = l >> 32;
1813 l = (int32_t)l;
1814 break;
1815 case INDEX_op_mulu2_i64:
1816 mulu64(&l, &h, a, b);
1817 break;
1818 case INDEX_op_muls2_i64:
1819 muls64(&l, &h, a, b);
1820 break;
1821 default:
1822 g_assert_not_reached();
1823 }
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001824
1825 rl = op->args[0];
1826 rh = op->args[1];
Richard Henderson407112b2021-08-26 06:33:04 -07001827
1828 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01001829 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson407112b2021-08-26 06:33:04 -07001830
1831 tcg_opt_gen_movi(ctx, op, rl, l);
1832 tcg_opt_gen_movi(ctx, op2, rh, h);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001833 return true;
1834 }
1835 return false;
1836}
1837
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001838static bool fold_nand(OptContext *ctx, TCGOp *op)
1839{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001840 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001841 fold_xi_to_not(ctx, op, -1)) {
1842 return true;
1843 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001844
1845 ctx->s_mask = arg_info(op->args[1])->s_mask
1846 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001847 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001848}
1849
1850static bool fold_neg(OptContext *ctx, TCGOp *op)
1851{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001852 uint64_t z_mask;
1853
Richard Henderson9caca882021-08-24 13:30:32 -07001854 if (fold_const1(ctx, op)) {
1855 return true;
1856 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001857
1858 /* Set to 1 all bits to the left of the rightmost. */
1859 z_mask = arg_info(op->args[1])->z_mask;
1860 ctx->z_mask = -(z_mask & -z_mask);
1861
Richard Henderson9caca882021-08-24 13:30:32 -07001862 /*
1863 * Because of fold_sub_to_neg, we want to always return true,
1864 * via finish_folding.
1865 */
1866 finish_folding(ctx, op);
1867 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001868}
1869
1870static bool fold_nor(OptContext *ctx, TCGOp *op)
1871{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001872 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001873 fold_xi_to_not(ctx, op, 0)) {
1874 return true;
1875 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001876
1877 ctx->s_mask = arg_info(op->args[1])->s_mask
1878 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001879 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001880}
1881
1882static bool fold_not(OptContext *ctx, TCGOp *op)
1883{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001884 if (fold_const1(ctx, op)) {
1885 return true;
1886 }
1887
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001888 ctx->s_mask = arg_info(op->args[1])->s_mask;
1889
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001890 /* Because of fold_to_not, we want to always return true, via finish. */
1891 finish_folding(ctx, op);
1892 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001893}
1894
1895static bool fold_or(OptContext *ctx, TCGOp *op)
1896{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001897 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001898 fold_xi_to_x(ctx, op, 0) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001899 fold_xx_to_x(ctx, op)) {
1900 return true;
1901 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001902
1903 ctx->z_mask = arg_info(op->args[1])->z_mask
1904 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001905 ctx->s_mask = arg_info(op->args[1])->s_mask
1906 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001907 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001908}
1909
1910static bool fold_orc(OptContext *ctx, TCGOp *op)
1911{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001912 if (fold_const2(ctx, op) ||
Richard Henderson4e858d92021-08-26 07:31:13 -07001913 fold_xx_to_i(ctx, op, -1) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001914 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001915 fold_ix_to_not(ctx, op, 0)) {
1916 return true;
1917 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001918
1919 ctx->s_mask = arg_info(op->args[1])->s_mask
1920 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001921 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001922}
1923
Richard Henderson3eefdf22021-08-25 11:06:43 -07001924static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
1925{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001926 const TCGOpDef *def = &tcg_op_defs[op->opc];
1927 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
1928 MemOp mop = get_memop(oi);
1929 int width = 8 * memop_size(mop);
1930
Richard Henderson57fe5c62021-08-26 12:04:46 -07001931 if (width < 64) {
1932 ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width);
1933 if (!(mop & MO_SIGN)) {
1934 ctx->z_mask = MAKE_64BIT_MASK(0, width);
1935 ctx->s_mask <<= 1;
1936 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001937 }
1938
Richard Henderson3eefdf22021-08-25 11:06:43 -07001939 /* Opcodes that touch guest memory stop the mb optimization. */
1940 ctx->prev_mb = NULL;
1941 return false;
1942}
1943
1944static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
1945{
1946 /* Opcodes that touch guest memory stop the mb optimization. */
1947 ctx->prev_mb = NULL;
1948 return false;
1949}
1950
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001951static bool fold_remainder(OptContext *ctx, TCGOp *op)
1952{
Richard Henderson267c17e2021-10-25 11:30:33 -07001953 if (fold_const2(ctx, op) ||
1954 fold_xx_to_i(ctx, op, 0)) {
1955 return true;
1956 }
1957 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001958}
1959
Richard Hendersonc63ff552021-08-24 09:35:30 -07001960static bool fold_setcond(OptContext *ctx, TCGOp *op)
1961{
Richard Henderson246c4b72023-10-24 16:36:50 -07001962 int i = do_constant_folding_cond1(ctx, op->args[0], &op->args[1],
1963 &op->args[2], &op->args[3]);
Richard Hendersonc63ff552021-08-24 09:35:30 -07001964 if (i >= 0) {
1965 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1966 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001967
1968 ctx->z_mask = 1;
Richard Henderson275d7d82021-08-26 13:20:39 -07001969 ctx->s_mask = smask_from_zmask(1);
Richard Hendersonc63ff552021-08-24 09:35:30 -07001970 return false;
1971}
1972
Richard Henderson36355022023-08-04 23:24:04 +00001973static bool fold_negsetcond(OptContext *ctx, TCGOp *op)
1974{
Richard Henderson246c4b72023-10-24 16:36:50 -07001975 int i = do_constant_folding_cond1(ctx, op->args[0], &op->args[1],
1976 &op->args[2], &op->args[3]);
Richard Henderson36355022023-08-04 23:24:04 +00001977 if (i >= 0) {
1978 return tcg_opt_gen_movi(ctx, op, op->args[0], -i);
1979 }
1980
1981 /* Value is {0,-1} so all bits are repetitions of the sign. */
1982 ctx->s_mask = -1;
1983 return false;
1984}
1985
1986
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001987static bool fold_setcond2(OptContext *ctx, TCGOp *op)
1988{
1989 TCGCond cond = op->args[5];
Richard Henderson7a2f7082021-08-26 07:06:39 -07001990 int i, inv = 0;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001991
Richard Henderson7a2f7082021-08-26 07:06:39 -07001992 if (swap_commutative2(&op->args[1], &op->args[3])) {
1993 op->args[5] = cond = tcg_swap_cond(cond);
1994 }
1995
1996 i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond);
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07001997 if (i >= 0) {
1998 goto do_setcond_const;
1999 }
2000
2001 switch (cond) {
2002 case TCG_COND_LT:
2003 case TCG_COND_GE:
2004 /*
2005 * Simplify LT/GE comparisons vs zero to a single compare
2006 * vs the high word of the input.
2007 */
Richard Henderson27cdb852023-10-23 11:38:00 -07002008 if (arg_is_const_val(op->args[3], 0) &&
2009 arg_is_const_val(op->args[4], 0)) {
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002010 goto do_setcond_high;
2011 }
2012 break;
2013
2014 case TCG_COND_NE:
2015 inv = 1;
2016 QEMU_FALLTHROUGH;
2017 case TCG_COND_EQ:
2018 /*
2019 * Simplify EQ/NE comparisons where one of the pairs
2020 * can be simplified.
2021 */
Richard Henderson67f84c92021-08-25 08:00:20 -07002022 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002023 op->args[3], cond);
2024 switch (i ^ inv) {
2025 case 0:
2026 goto do_setcond_const;
2027 case 1:
2028 goto do_setcond_high;
2029 }
2030
Richard Henderson67f84c92021-08-25 08:00:20 -07002031 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002032 op->args[4], cond);
2033 switch (i ^ inv) {
2034 case 0:
2035 goto do_setcond_const;
2036 case 1:
2037 op->args[2] = op->args[3];
2038 op->args[3] = cond;
2039 op->opc = INDEX_op_setcond_i32;
2040 break;
2041 }
2042 break;
2043
2044 default:
2045 break;
2046
2047 do_setcond_high:
2048 op->args[1] = op->args[2];
2049 op->args[2] = op->args[4];
2050 op->args[3] = cond;
2051 op->opc = INDEX_op_setcond_i32;
2052 break;
2053 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002054
2055 ctx->z_mask = 1;
Richard Henderson275d7d82021-08-26 13:20:39 -07002056 ctx->s_mask = smask_from_zmask(1);
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002057 return false;
2058
2059 do_setcond_const:
2060 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2061}
2062
Richard Hendersonb6617c82021-08-24 10:44:53 -07002063static bool fold_sextract(OptContext *ctx, TCGOp *op)
2064{
Richard Henderson57fe5c62021-08-26 12:04:46 -07002065 uint64_t z_mask, s_mask, s_mask_old;
2066 int pos = op->args[2];
2067 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07002068
Richard Hendersonb6617c82021-08-24 10:44:53 -07002069 if (arg_is_const(op->args[1])) {
2070 uint64_t t;
2071
2072 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002073 t = sextract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002074 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
2075 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002076
Richard Henderson57fe5c62021-08-26 12:04:46 -07002077 z_mask = arg_info(op->args[1])->z_mask;
2078 z_mask = sextract64(z_mask, pos, len);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002079 ctx->z_mask = z_mask;
2080
Richard Henderson57fe5c62021-08-26 12:04:46 -07002081 s_mask_old = arg_info(op->args[1])->s_mask;
2082 s_mask = sextract64(s_mask_old, pos, len);
2083 s_mask |= MAKE_64BIT_MASK(len, 64 - len);
2084 ctx->s_mask = s_mask;
2085
2086 if (pos == 0) {
2087 ctx->a_mask = s_mask & ~s_mask_old;
2088 }
2089
Richard Hendersonfae450b2021-08-25 22:42:19 -07002090 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002091}
2092
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002093static bool fold_shift(OptContext *ctx, TCGOp *op)
2094{
Richard Henderson93a967f2021-08-26 13:24:59 -07002095 uint64_t s_mask, z_mask, sign;
2096
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002097 if (fold_const2(ctx, op) ||
Richard Hendersonda48e272021-08-25 20:42:04 -07002098 fold_ix_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002099 fold_xi_to_x(ctx, op, 0)) {
2100 return true;
2101 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002102
Richard Henderson93a967f2021-08-26 13:24:59 -07002103 s_mask = arg_info(op->args[1])->s_mask;
2104 z_mask = arg_info(op->args[1])->z_mask;
2105
Richard Hendersonfae450b2021-08-25 22:42:19 -07002106 if (arg_is_const(op->args[2])) {
Richard Henderson93a967f2021-08-26 13:24:59 -07002107 int sh = arg_info(op->args[2])->val;
2108
2109 ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
2110
2111 s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
2112 ctx->s_mask = smask_from_smask(s_mask);
2113
Richard Hendersonfae450b2021-08-25 22:42:19 -07002114 return fold_masks(ctx, op);
2115 }
Richard Henderson93a967f2021-08-26 13:24:59 -07002116
2117 switch (op->opc) {
2118 CASE_OP_32_64(sar):
2119 /*
2120 * Arithmetic right shift will not reduce the number of
2121 * input sign repetitions.
2122 */
2123 ctx->s_mask = s_mask;
2124 break;
2125 CASE_OP_32_64(shr):
2126 /*
2127 * If the sign bit is known zero, then logical right shift
2128 * will not reduced the number of input sign repetitions.
2129 */
2130 sign = (s_mask & -s_mask) >> 1;
2131 if (!(z_mask & sign)) {
2132 ctx->s_mask = s_mask;
2133 }
2134 break;
2135 default:
2136 break;
2137 }
2138
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002139 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002140}
2141
Richard Henderson9caca882021-08-24 13:30:32 -07002142static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
2143{
2144 TCGOpcode neg_op;
2145 bool have_neg;
2146
2147 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
2148 return false;
2149 }
2150
2151 switch (ctx->type) {
2152 case TCG_TYPE_I32:
2153 neg_op = INDEX_op_neg_i32;
Richard Hendersonb701f192023-10-25 21:14:04 -07002154 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002155 break;
2156 case TCG_TYPE_I64:
2157 neg_op = INDEX_op_neg_i64;
Richard Hendersonb701f192023-10-25 21:14:04 -07002158 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002159 break;
2160 case TCG_TYPE_V64:
2161 case TCG_TYPE_V128:
2162 case TCG_TYPE_V256:
2163 neg_op = INDEX_op_neg_vec;
2164 have_neg = (TCG_TARGET_HAS_neg_vec &&
2165 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
2166 break;
2167 default:
2168 g_assert_not_reached();
2169 }
2170 if (have_neg) {
2171 op->opc = neg_op;
2172 op->args[1] = op->args[2];
2173 return fold_neg(ctx, op);
2174 }
2175 return false;
2176}
2177
Richard Hendersonc578ff12021-12-16 06:07:25 -08002178/* We cannot as yet do_constant_folding with vectors. */
2179static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002180{
Richard Hendersonc578ff12021-12-16 06:07:25 -08002181 if (fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002182 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson9caca882021-08-24 13:30:32 -07002183 fold_sub_to_neg(ctx, op)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002184 return true;
2185 }
2186 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002187}
2188
Richard Hendersonc578ff12021-12-16 06:07:25 -08002189static bool fold_sub(OptContext *ctx, TCGOp *op)
2190{
Richard Henderson6334a962023-10-25 18:39:43 -07002191 if (fold_const2(ctx, op) || fold_sub_vec(ctx, op)) {
2192 return true;
2193 }
2194
2195 /* Fold sub r,x,i to add r,x,-i */
2196 if (arg_is_const(op->args[2])) {
2197 uint64_t val = arg_info(op->args[2])->val;
2198
2199 op->opc = (ctx->type == TCG_TYPE_I32
2200 ? INDEX_op_add_i32 : INDEX_op_add_i64);
2201 op->args[2] = arg_new_constant(ctx, -val);
2202 }
2203 return false;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002204}
2205
Richard Henderson9531c072021-08-26 06:51:39 -07002206static bool fold_sub2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002207{
Richard Henderson9531c072021-08-26 06:51:39 -07002208 return fold_addsub2(ctx, op, false);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002209}
2210
Richard Hendersonfae450b2021-08-25 22:42:19 -07002211static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
2212{
2213 /* We can't do any folding with a load, but we can record bits. */
2214 switch (op->opc) {
Richard Henderson57fe5c62021-08-26 12:04:46 -07002215 CASE_OP_32_64(ld8s):
2216 ctx->s_mask = MAKE_64BIT_MASK(8, 56);
2217 break;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002218 CASE_OP_32_64(ld8u):
2219 ctx->z_mask = MAKE_64BIT_MASK(0, 8);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002220 ctx->s_mask = MAKE_64BIT_MASK(9, 55);
2221 break;
2222 CASE_OP_32_64(ld16s):
2223 ctx->s_mask = MAKE_64BIT_MASK(16, 48);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002224 break;
2225 CASE_OP_32_64(ld16u):
2226 ctx->z_mask = MAKE_64BIT_MASK(0, 16);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002227 ctx->s_mask = MAKE_64BIT_MASK(17, 47);
2228 break;
2229 case INDEX_op_ld32s_i64:
2230 ctx->s_mask = MAKE_64BIT_MASK(32, 32);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002231 break;
2232 case INDEX_op_ld32u_i64:
2233 ctx->z_mask = MAKE_64BIT_MASK(0, 32);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002234 ctx->s_mask = MAKE_64BIT_MASK(33, 31);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002235 break;
2236 default:
2237 g_assert_not_reached();
2238 }
2239 return false;
2240}
2241
Richard Hendersonab84dc32023-08-23 23:04:24 -07002242static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op)
2243{
2244 TCGTemp *dst, *src;
2245 intptr_t ofs;
2246 TCGType type;
2247
2248 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2249 return false;
2250 }
2251
2252 type = ctx->type;
2253 ofs = op->args[2];
2254 dst = arg_temp(op->args[0]);
2255 src = find_mem_copy_for(ctx, type, ofs);
2256 if (src && src->base_type == type) {
2257 return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src));
2258 }
2259
2260 reset_ts(ctx, dst);
2261 record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1);
2262 return true;
2263}
2264
2265static bool fold_tcg_st(OptContext *ctx, TCGOp *op)
2266{
2267 intptr_t ofs = op->args[2];
2268 intptr_t lm1;
2269
2270 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2271 remove_mem_copy_all(ctx);
2272 return false;
2273 }
2274
2275 switch (op->opc) {
2276 CASE_OP_32_64(st8):
2277 lm1 = 0;
2278 break;
2279 CASE_OP_32_64(st16):
2280 lm1 = 1;
2281 break;
2282 case INDEX_op_st32_i64:
2283 case INDEX_op_st_i32:
2284 lm1 = 3;
2285 break;
2286 case INDEX_op_st_i64:
2287 lm1 = 7;
2288 break;
2289 case INDEX_op_st_vec:
2290 lm1 = tcg_type_size(ctx->type) - 1;
2291 break;
2292 default:
2293 g_assert_not_reached();
2294 }
2295 remove_mem_copy_in(ctx, ofs, ofs + lm1);
2296 return false;
2297}
2298
2299static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op)
2300{
2301 TCGTemp *src;
2302 intptr_t ofs, last;
2303 TCGType type;
2304
2305 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2306 fold_tcg_st(ctx, op);
2307 return false;
2308 }
2309
2310 src = arg_temp(op->args[0]);
2311 ofs = op->args[2];
2312 type = ctx->type;
Richard Henderson3eaadae2023-08-23 23:13:06 -07002313
2314 /*
2315 * Eliminate duplicate stores of a constant.
2316 * This happens frequently when the target ISA zero-extends.
2317 */
2318 if (ts_is_const(src)) {
2319 TCGTemp *prev = find_mem_copy_for(ctx, type, ofs);
2320 if (src == prev) {
2321 tcg_op_remove(ctx->tcg, op);
2322 return true;
2323 }
2324 }
2325
Richard Hendersonab84dc32023-08-23 23:04:24 -07002326 last = ofs + tcg_type_size(type) - 1;
2327 remove_mem_copy_in(ctx, ofs, last);
2328 record_mem_copy(ctx, type, src, ofs, last);
2329 return false;
2330}
2331
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002332static bool fold_xor(OptContext *ctx, TCGOp *op)
2333{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002334 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002335 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002336 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002337 fold_xi_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002338 return true;
2339 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002340
2341 ctx->z_mask = arg_info(op->args[1])->z_mask
2342 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002343 ctx->s_mask = arg_info(op->args[1])->s_mask
2344 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002345 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002346}
2347
Kirill Batuzov22613af2011-07-07 16:37:13 +04002348/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +02002349void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002350{
Richard Henderson5cf32be2021-08-24 08:17:08 -07002351 int nb_temps, i;
Richard Hendersond0ed5152021-08-24 07:38:39 -07002352 TCGOp *op, *op_next;
Richard Hendersondc849882021-08-24 07:13:45 -07002353 OptContext ctx = { .tcg = s };
Richard Henderson5d8f5362012-09-21 10:13:38 -07002354
Richard Hendersonab84dc32023-08-23 23:04:24 -07002355 QSIMPLEQ_INIT(&ctx.mem_free);
2356
Kirill Batuzov22613af2011-07-07 16:37:13 +04002357 /* Array VALS has an element for each temp.
2358 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02002359 If this temp is a copy of other ones then the other copies are
2360 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002361
2362 nb_temps = s->nb_temps;
Richard Henderson8f17a972020-03-30 19:52:02 -07002363 for (i = 0; i < nb_temps; ++i) {
2364 s->temps[i].state_ptr = NULL;
2365 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002366
Richard Henderson15fa08f2017-11-02 15:19:14 +01002367 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002368 TCGOpcode opc = op->opc;
Richard Henderson5cf32be2021-08-24 08:17:08 -07002369 const TCGOpDef *def;
Richard Henderson404a1482021-08-24 11:08:21 -07002370 bool done = false;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002371
Richard Henderson5cf32be2021-08-24 08:17:08 -07002372 /* Calls are special. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002373 if (opc == INDEX_op_call) {
Richard Henderson5cf32be2021-08-24 08:17:08 -07002374 fold_call(&ctx, op);
2375 continue;
Richard Hendersoncf066672014-03-22 20:06:52 -07002376 }
Richard Henderson5cf32be2021-08-24 08:17:08 -07002377
2378 def = &tcg_op_defs[opc];
Richard Hendersonec5d4cb2021-08-24 08:20:27 -07002379 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
2380 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
Kirill Batuzov22613af2011-07-07 16:37:13 +04002381
Richard Henderson67f84c92021-08-25 08:00:20 -07002382 /* Pre-compute the type of the operation. */
2383 if (def->flags & TCG_OPF_VECTOR) {
2384 ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
2385 } else if (def->flags & TCG_OPF_64BIT) {
2386 ctx.type = TCG_TYPE_I64;
2387 } else {
2388 ctx.type = TCG_TYPE_I32;
2389 }
2390
Richard Henderson57fe5c62021-08-26 12:04:46 -07002391 /* Assume all bits affected, no bits known zero, no sign reps. */
Richard Hendersonfae450b2021-08-25 22:42:19 -07002392 ctx.a_mask = -1;
2393 ctx.z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002394 ctx.s_mask = 0;
Paolo Bonzini633f6502013-01-11 15:42:53 -08002395
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002396 /*
2397 * Process each opcode.
2398 * Sorted alphabetically by opcode as much as possible.
2399 */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002400 switch (opc) {
Richard Hendersonc578ff12021-12-16 06:07:25 -08002401 CASE_OP_32_64(add):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002402 done = fold_add(&ctx, op);
2403 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002404 case INDEX_op_add_vec:
2405 done = fold_add_vec(&ctx, op);
2406 break;
Richard Henderson9531c072021-08-26 06:51:39 -07002407 CASE_OP_32_64(add2):
2408 done = fold_add2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002409 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002410 CASE_OP_32_64_VEC(and):
2411 done = fold_and(&ctx, op);
2412 break;
2413 CASE_OP_32_64_VEC(andc):
2414 done = fold_andc(&ctx, op);
2415 break;
Richard Henderson079b0802021-08-24 09:30:59 -07002416 CASE_OP_32_64(brcond):
2417 done = fold_brcond(&ctx, op);
2418 break;
Richard Henderson764d2ab2021-08-24 09:22:11 -07002419 case INDEX_op_brcond2_i32:
2420 done = fold_brcond2(&ctx, op);
2421 break;
Richard Henderson09bacdc2021-08-24 11:58:12 -07002422 CASE_OP_32_64(bswap16):
2423 CASE_OP_32_64(bswap32):
2424 case INDEX_op_bswap64_i64:
2425 done = fold_bswap(&ctx, op);
2426 break;
Richard Henderson30dd0bf2021-08-24 10:51:34 -07002427 CASE_OP_32_64(clz):
2428 CASE_OP_32_64(ctz):
2429 done = fold_count_zeros(&ctx, op);
2430 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002431 CASE_OP_32_64(ctpop):
2432 done = fold_ctpop(&ctx, op);
2433 break;
Richard Henderson1b1907b2021-08-24 10:47:04 -07002434 CASE_OP_32_64(deposit):
2435 done = fold_deposit(&ctx, op);
2436 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002437 CASE_OP_32_64(div):
2438 CASE_OP_32_64(divu):
2439 done = fold_divide(&ctx, op);
2440 break;
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07002441 case INDEX_op_dup_vec:
2442 done = fold_dup(&ctx, op);
2443 break;
2444 case INDEX_op_dup2_vec:
2445 done = fold_dup2(&ctx, op);
2446 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002447 CASE_OP_32_64_VEC(eqv):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002448 done = fold_eqv(&ctx, op);
2449 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07002450 CASE_OP_32_64(extract):
2451 done = fold_extract(&ctx, op);
2452 break;
Richard Hendersondcd08992021-08-24 10:41:39 -07002453 CASE_OP_32_64(extract2):
2454 done = fold_extract2(&ctx, op);
2455 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002456 CASE_OP_32_64(ext8s):
2457 CASE_OP_32_64(ext16s):
2458 case INDEX_op_ext32s_i64:
2459 case INDEX_op_ext_i32_i64:
2460 done = fold_exts(&ctx, op);
2461 break;
2462 CASE_OP_32_64(ext8u):
2463 CASE_OP_32_64(ext16u):
2464 case INDEX_op_ext32u_i64:
2465 case INDEX_op_extu_i32_i64:
2466 case INDEX_op_extrl_i64_i32:
2467 case INDEX_op_extrh_i64_i32:
2468 done = fold_extu(&ctx, op);
2469 break;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002470 CASE_OP_32_64(ld8s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002471 CASE_OP_32_64(ld8u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002472 CASE_OP_32_64(ld16s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002473 CASE_OP_32_64(ld16u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002474 case INDEX_op_ld32s_i64:
Richard Hendersonfae450b2021-08-25 22:42:19 -07002475 case INDEX_op_ld32u_i64:
2476 done = fold_tcg_ld(&ctx, op);
2477 break;
Richard Hendersonab84dc32023-08-23 23:04:24 -07002478 case INDEX_op_ld_i32:
2479 case INDEX_op_ld_i64:
2480 case INDEX_op_ld_vec:
2481 done = fold_tcg_ld_memcopy(&ctx, op);
2482 break;
2483 CASE_OP_32_64(st8):
2484 CASE_OP_32_64(st16):
2485 case INDEX_op_st32_i64:
2486 done = fold_tcg_st(&ctx, op);
2487 break;
2488 case INDEX_op_st_i32:
2489 case INDEX_op_st_i64:
2490 case INDEX_op_st_vec:
2491 done = fold_tcg_st_memcopy(&ctx, op);
2492 break;
Richard Henderson3eefdf22021-08-25 11:06:43 -07002493 case INDEX_op_mb:
2494 done = fold_mb(&ctx, op);
2495 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002496 CASE_OP_32_64_VEC(mov):
2497 done = fold_mov(&ctx, op);
2498 break;
Richard Henderson0c310a32021-08-24 10:37:24 -07002499 CASE_OP_32_64(movcond):
2500 done = fold_movcond(&ctx, op);
2501 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002502 CASE_OP_32_64(mul):
2503 done = fold_mul(&ctx, op);
2504 break;
2505 CASE_OP_32_64(mulsh):
2506 CASE_OP_32_64(muluh):
2507 done = fold_mul_highpart(&ctx, op);
2508 break;
Richard Henderson407112b2021-08-26 06:33:04 -07002509 CASE_OP_32_64(muls2):
2510 CASE_OP_32_64(mulu2):
2511 done = fold_multiply2(&ctx, op);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07002512 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002513 CASE_OP_32_64_VEC(nand):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002514 done = fold_nand(&ctx, op);
2515 break;
2516 CASE_OP_32_64(neg):
2517 done = fold_neg(&ctx, op);
2518 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002519 CASE_OP_32_64_VEC(nor):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002520 done = fold_nor(&ctx, op);
2521 break;
2522 CASE_OP_32_64_VEC(not):
2523 done = fold_not(&ctx, op);
2524 break;
2525 CASE_OP_32_64_VEC(or):
2526 done = fold_or(&ctx, op);
2527 break;
2528 CASE_OP_32_64_VEC(orc):
2529 done = fold_orc(&ctx, op);
2530 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07002531 case INDEX_op_qemu_ld_a32_i32:
2532 case INDEX_op_qemu_ld_a64_i32:
2533 case INDEX_op_qemu_ld_a32_i64:
2534 case INDEX_op_qemu_ld_a64_i64:
2535 case INDEX_op_qemu_ld_a32_i128:
2536 case INDEX_op_qemu_ld_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07002537 done = fold_qemu_ld(&ctx, op);
2538 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07002539 case INDEX_op_qemu_st8_a32_i32:
2540 case INDEX_op_qemu_st8_a64_i32:
2541 case INDEX_op_qemu_st_a32_i32:
2542 case INDEX_op_qemu_st_a64_i32:
2543 case INDEX_op_qemu_st_a32_i64:
2544 case INDEX_op_qemu_st_a64_i64:
2545 case INDEX_op_qemu_st_a32_i128:
2546 case INDEX_op_qemu_st_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07002547 done = fold_qemu_st(&ctx, op);
2548 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002549 CASE_OP_32_64(rem):
2550 CASE_OP_32_64(remu):
2551 done = fold_remainder(&ctx, op);
2552 break;
2553 CASE_OP_32_64(rotl):
2554 CASE_OP_32_64(rotr):
2555 CASE_OP_32_64(sar):
2556 CASE_OP_32_64(shl):
2557 CASE_OP_32_64(shr):
2558 done = fold_shift(&ctx, op);
2559 break;
Richard Hendersonc63ff552021-08-24 09:35:30 -07002560 CASE_OP_32_64(setcond):
2561 done = fold_setcond(&ctx, op);
2562 break;
Richard Henderson36355022023-08-04 23:24:04 +00002563 CASE_OP_32_64(negsetcond):
2564 done = fold_negsetcond(&ctx, op);
2565 break;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002566 case INDEX_op_setcond2_i32:
2567 done = fold_setcond2(&ctx, op);
2568 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07002569 CASE_OP_32_64(sextract):
2570 done = fold_sextract(&ctx, op);
2571 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002572 CASE_OP_32_64(sub):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002573 done = fold_sub(&ctx, op);
2574 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002575 case INDEX_op_sub_vec:
2576 done = fold_sub_vec(&ctx, op);
2577 break;
Richard Henderson9531c072021-08-26 06:51:39 -07002578 CASE_OP_32_64(sub2):
2579 done = fold_sub2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002580 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002581 CASE_OP_32_64_VEC(xor):
2582 done = fold_xor(&ctx, op);
Richard Hendersonb10f3832021-08-23 22:30:17 -07002583 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002584 default:
2585 break;
Richard Hendersonb10f3832021-08-23 22:30:17 -07002586 }
2587
Richard Henderson404a1482021-08-24 11:08:21 -07002588 if (!done) {
2589 finish_folding(&ctx, op);
2590 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002591 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002592}