blob: 2f5030c899a21cead439e785cbd7ea7cbf9b105c [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 Henderson6d70ddc2024-12-21 21:08:10 -080055 uint64_t s_mask; /* mask bit is 1 if value bit matches msb */
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 z_mask; /* mask bit is 0 iff value bit is 0 */
Richard Henderson6d70ddc2024-12-21 21:08:10 -080068 uint64_t s_mask; /* mask bit is 1 if value bit matches msb */
Richard Henderson67f84c92021-08-25 08:00:20 -070069 TCGType type;
Richard Henderson3b3f8472021-08-23 22:06:31 -070070} OptContext;
71
Richard Henderson6fcb98e2020-03-30 17:44:30 -070072static inline TempOptInfo *ts_info(TCGTemp *ts)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020073{
Richard Henderson63490392017-06-20 13:43:15 -070074 return ts->state_ptr;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020075}
76
Richard Henderson6fcb98e2020-03-30 17:44:30 -070077static inline TempOptInfo *arg_info(TCGArg arg)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020078{
Richard Henderson63490392017-06-20 13:43:15 -070079 return ts_info(arg_temp(arg));
80}
81
Richard Hendersone1b6c142024-12-22 10:26:14 -080082static inline bool ti_is_const(TempOptInfo *ti)
83{
84 return ti->is_const;
85}
86
87static inline uint64_t ti_const_val(TempOptInfo *ti)
88{
89 return ti->val;
90}
91
92static inline bool ti_is_const_val(TempOptInfo *ti, uint64_t val)
93{
94 return ti_is_const(ti) && ti_const_val(ti) == val;
95}
96
Richard Henderson63490392017-06-20 13:43:15 -070097static inline bool ts_is_const(TCGTemp *ts)
98{
Richard Hendersone1b6c142024-12-22 10:26:14 -080099 return ti_is_const(ts_info(ts));
Richard Henderson63490392017-06-20 13:43:15 -0700100}
101
Richard Henderson27cdb852023-10-23 11:38:00 -0700102static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val)
103{
Richard Hendersone1b6c142024-12-22 10:26:14 -0800104 return ti_is_const_val(ts_info(ts), val);
Richard Henderson27cdb852023-10-23 11:38:00 -0700105}
106
Richard Henderson63490392017-06-20 13:43:15 -0700107static inline bool arg_is_const(TCGArg arg)
108{
109 return ts_is_const(arg_temp(arg));
110}
111
Richard Henderson27cdb852023-10-23 11:38:00 -0700112static inline bool arg_is_const_val(TCGArg arg, uint64_t val)
113{
114 return ts_is_const_val(arg_temp(arg), val);
115}
116
Richard Henderson63490392017-06-20 13:43:15 -0700117static inline bool ts_is_copy(TCGTemp *ts)
118{
119 return ts_info(ts)->next_copy != ts;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200120}
121
Richard Henderson9f75e522023-11-02 13:37:46 -0700122static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b)
123{
124 return a->kind < b->kind ? b : a;
125}
126
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200127/* Initialize and activate a temporary. */
Richard Henderson3b3f8472021-08-23 22:06:31 -0700128static void init_ts_info(OptContext *ctx, TCGTemp *ts)
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200129{
Richard Henderson63490392017-06-20 13:43:15 -0700130 size_t idx = temp_idx(ts);
Richard Henderson8f17a972020-03-30 19:52:02 -0700131 TempOptInfo *ti;
Richard Henderson63490392017-06-20 13:43:15 -0700132
Richard Henderson3b3f8472021-08-23 22:06:31 -0700133 if (test_bit(idx, ctx->temps_used.l)) {
Richard Henderson8f17a972020-03-30 19:52:02 -0700134 return;
135 }
Richard Henderson3b3f8472021-08-23 22:06:31 -0700136 set_bit(idx, ctx->temps_used.l);
Richard Henderson8f17a972020-03-30 19:52:02 -0700137
138 ti = ts->state_ptr;
139 if (ti == NULL) {
140 ti = tcg_malloc(sizeof(TempOptInfo));
Richard Henderson63490392017-06-20 13:43:15 -0700141 ts->state_ptr = ti;
Richard Henderson8f17a972020-03-30 19:52:02 -0700142 }
143
144 ti->next_copy = ts;
145 ti->prev_copy = ts;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700146 QSIMPLEQ_INIT(&ti->mem_copy);
Richard Henderson8f17a972020-03-30 19:52:02 -0700147 if (ts->kind == TEMP_CONST) {
148 ti->is_const = true;
149 ti->val = ts->val;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700150 ti->z_mask = ts->val;
Richard Henderson6d70ddc2024-12-21 21:08:10 -0800151 ti->s_mask = INT64_MIN >> clrsb64(ts->val);
Richard Henderson8f17a972020-03-30 19:52:02 -0700152 } else {
153 ti->is_const = false;
Richard Hendersonb1fde412021-08-23 13:07:49 -0700154 ti->z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700155 ti->s_mask = 0;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200156 }
157}
158
Richard Hendersonab84dc32023-08-23 23:04:24 -0700159static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l)
160{
161 IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l);
162 return r ? container_of(r, MemCopyInfo, itree) : NULL;
163}
164
165static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l)
166{
167 IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l);
168 return r ? container_of(r, MemCopyInfo, itree) : NULL;
169}
170
171static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc)
172{
173 TCGTemp *ts = mc->ts;
174 TempOptInfo *ti = ts_info(ts);
175
176 interval_tree_remove(&mc->itree, &ctx->mem_copy);
177 QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next);
178 QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next);
179}
180
181static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l)
182{
183 while (true) {
184 MemCopyInfo *mc = mem_copy_first(ctx, s, l);
185 if (!mc) {
186 break;
187 }
188 remove_mem_copy(ctx, mc);
189 }
190}
191
192static void remove_mem_copy_all(OptContext *ctx)
193{
194 remove_mem_copy_in(ctx, 0, -1);
195 tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy));
196}
197
Richard Henderson9f75e522023-11-02 13:37:46 -0700198static TCGTemp *find_better_copy(TCGTemp *ts)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200199{
Richard Henderson9f75e522023-11-02 13:37:46 -0700200 TCGTemp *i, *ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200201
Richard Henderson4c868ce2020-04-23 09:02:23 -0700202 /* If this is already readonly, we can't do better. */
203 if (temp_readonly(ts)) {
Richard Henderson63490392017-06-20 13:43:15 -0700204 return ts;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200205 }
206
Richard Henderson9f75e522023-11-02 13:37:46 -0700207 ret = ts;
Richard Henderson63490392017-06-20 13:43:15 -0700208 for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700209 ret = cmp_better_copy(ret, i);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200210 }
Richard Henderson9f75e522023-11-02 13:37:46 -0700211 return ret;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200212}
213
Richard Hendersonab84dc32023-08-23 23:04:24 -0700214static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts)
215{
216 TempOptInfo *si = ts_info(src_ts);
217 TempOptInfo *di = ts_info(dst_ts);
218 MemCopyInfo *mc;
219
220 QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) {
221 tcg_debug_assert(mc->ts == src_ts);
222 mc->ts = dst_ts;
223 }
224 QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy);
225}
226
227/* Reset TEMP's state, possibly removing the temp for the list of copies. */
228static void reset_ts(OptContext *ctx, TCGTemp *ts)
229{
230 TempOptInfo *ti = ts_info(ts);
231 TCGTemp *pts = ti->prev_copy;
232 TCGTemp *nts = ti->next_copy;
233 TempOptInfo *pi = ts_info(pts);
234 TempOptInfo *ni = ts_info(nts);
235
236 ni->prev_copy = ti->prev_copy;
237 pi->next_copy = ti->next_copy;
238 ti->next_copy = ts;
239 ti->prev_copy = ts;
240 ti->is_const = false;
241 ti->z_mask = -1;
242 ti->s_mask = 0;
243
244 if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) {
245 if (ts == nts) {
246 /* Last temp copy being removed, the mem copies die. */
247 MemCopyInfo *mc;
248 QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) {
249 interval_tree_remove(&mc->itree, &ctx->mem_copy);
250 }
251 QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy);
252 } else {
253 move_mem_copies(find_better_copy(nts), ts);
254 }
255 }
256}
257
258static void reset_temp(OptContext *ctx, TCGArg arg)
259{
260 reset_ts(ctx, arg_temp(arg));
261}
262
263static void record_mem_copy(OptContext *ctx, TCGType type,
264 TCGTemp *ts, intptr_t start, intptr_t last)
265{
266 MemCopyInfo *mc;
267 TempOptInfo *ti;
268
269 mc = QSIMPLEQ_FIRST(&ctx->mem_free);
270 if (mc) {
271 QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next);
272 } else {
273 mc = tcg_malloc(sizeof(*mc));
274 }
275
276 memset(mc, 0, sizeof(*mc));
277 mc->itree.start = start;
278 mc->itree.last = last;
279 mc->type = type;
280 interval_tree_insert(&mc->itree, &ctx->mem_copy);
281
282 ts = find_better_copy(ts);
283 ti = ts_info(ts);
284 mc->ts = ts;
285 QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next);
286}
287
Richard Henderson63490392017-06-20 13:43:15 -0700288static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200289{
Richard Henderson63490392017-06-20 13:43:15 -0700290 TCGTemp *i;
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200291
Richard Henderson63490392017-06-20 13:43:15 -0700292 if (ts1 == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200293 return true;
294 }
295
Richard Henderson63490392017-06-20 13:43:15 -0700296 if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200297 return false;
298 }
299
Richard Henderson63490392017-06-20 13:43:15 -0700300 for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
301 if (i == ts2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200302 return true;
303 }
304 }
305
306 return false;
307}
308
Richard Henderson63490392017-06-20 13:43:15 -0700309static bool args_are_copies(TCGArg arg1, TCGArg arg2)
310{
311 return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
312}
313
Richard Hendersonab84dc32023-08-23 23:04:24 -0700314static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s)
315{
316 MemCopyInfo *mc;
317
318 for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) {
319 if (mc->itree.start == s && mc->type == type) {
320 return find_better_copy(mc->ts);
321 }
322 }
323 return NULL;
324}
325
Richard Henderson26aac972023-10-23 12:31:57 -0700326static TCGArg arg_new_constant(OptContext *ctx, uint64_t val)
327{
328 TCGType type = ctx->type;
329 TCGTemp *ts;
330
331 if (type == TCG_TYPE_I32) {
332 val = (int32_t)val;
333 }
334
335 ts = tcg_constant_internal(type, val);
336 init_ts_info(ctx, ts);
337
338 return temp_arg(ts);
339}
340
Richard Hendersonfb04ab72024-01-10 18:21:58 +1100341static TCGArg arg_new_temp(OptContext *ctx)
342{
343 TCGTemp *ts = tcg_temp_new_internal(ctx->type, TEMP_EBB);
344 init_ts_info(ctx, ts);
345 return temp_arg(ts);
346}
347
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700348static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400349{
Richard Henderson63490392017-06-20 13:43:15 -0700350 TCGTemp *dst_ts = arg_temp(dst);
351 TCGTemp *src_ts = arg_temp(src);
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700352 TempOptInfo *di;
353 TempOptInfo *si;
Richard Henderson63490392017-06-20 13:43:15 -0700354 TCGOpcode new_op;
355
356 if (ts_are_copies(dst_ts, src_ts)) {
Richard Hendersondc849882021-08-24 07:13:45 -0700357 tcg_op_remove(ctx->tcg, op);
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700358 return true;
Aurelien Jarno53657182015-06-04 21:53:25 +0200359 }
360
Richard Henderson986cac12023-01-09 13:59:35 -0800361 reset_ts(ctx, dst_ts);
Richard Henderson63490392017-06-20 13:43:15 -0700362 di = ts_info(dst_ts);
363 si = ts_info(src_ts);
Richard Henderson67f84c92021-08-25 08:00:20 -0700364
365 switch (ctx->type) {
366 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100367 new_op = INDEX_op_mov_i32;
Richard Henderson67f84c92021-08-25 08:00:20 -0700368 break;
369 case TCG_TYPE_I64:
370 new_op = INDEX_op_mov_i64;
371 break;
372 case TCG_TYPE_V64:
373 case TCG_TYPE_V128:
374 case TCG_TYPE_V256:
375 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
376 new_op = INDEX_op_mov_vec;
377 break;
378 default:
379 g_assert_not_reached();
Richard Henderson170ba882017-11-22 09:07:11 +0100380 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700381 op->opc = new_op;
Richard Henderson63490392017-06-20 13:43:15 -0700382 op->args[0] = dst;
383 op->args[1] = src;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700384
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700385 di->z_mask = si->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -0700386 di->s_mask = si->s_mask;
Richard Henderson24666ba2014-05-22 11:14:10 -0700387
Richard Henderson63490392017-06-20 13:43:15 -0700388 if (src_ts->type == dst_ts->type) {
Richard Henderson6fcb98e2020-03-30 17:44:30 -0700389 TempOptInfo *ni = ts_info(si->next_copy);
Richard Henderson63490392017-06-20 13:43:15 -0700390
391 di->next_copy = si->next_copy;
392 di->prev_copy = src_ts;
393 ni->prev_copy = dst_ts;
394 si->next_copy = dst_ts;
395 di->is_const = si->is_const;
396 di->val = si->val;
Richard Hendersonab84dc32023-08-23 23:04:24 -0700397
398 if (!QSIMPLEQ_EMPTY(&si->mem_copy)
399 && cmp_better_copy(src_ts, dst_ts) == dst_ts) {
400 move_mem_copies(dst_ts, src_ts);
401 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800402 }
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700403 return true;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400404}
405
Richard Henderson6b99d5b2021-08-24 10:57:56 -0700406static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
Richard Hendersondc849882021-08-24 07:13:45 -0700407 TCGArg dst, uint64_t val)
Richard Henderson8fe35e02020-03-30 20:42:43 -0700408{
Richard Hendersonfaa2e102021-08-26 09:03:59 -0700409 /* Convert movi to mov with constant temp. */
Richard Henderson26aac972023-10-23 12:31:57 -0700410 return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val));
Richard Henderson8fe35e02020-03-30 20:42:43 -0700411}
412
Richard Henderson54795542020-09-06 16:21:32 -0700413static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400414{
Richard Henderson03271522013-08-14 14:35:56 -0700415 uint64_t l64, h64;
416
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400417 switch (op) {
418 CASE_OP_32_64(add):
419 return x + y;
420
421 CASE_OP_32_64(sub):
422 return x - y;
423
424 CASE_OP_32_64(mul):
425 return x * y;
426
Richard Hendersonc578ff12021-12-16 06:07:25 -0800427 CASE_OP_32_64_VEC(and):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400428 return x & y;
429
Richard Hendersonc578ff12021-12-16 06:07:25 -0800430 CASE_OP_32_64_VEC(or):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400431 return x | y;
432
Richard Hendersonc578ff12021-12-16 06:07:25 -0800433 CASE_OP_32_64_VEC(xor):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400434 return x ^ y;
435
Kirill Batuzov55c09752011-07-07 16:37:16 +0400436 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700437 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400438
Kirill Batuzov55c09752011-07-07 16:37:16 +0400439 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700440 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400441
442 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700443 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400444
Kirill Batuzov55c09752011-07-07 16:37:16 +0400445 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700446 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400447
448 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700449 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400450
Kirill Batuzov55c09752011-07-07 16:37:16 +0400451 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700452 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400453
454 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700455 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400456
Kirill Batuzov55c09752011-07-07 16:37:16 +0400457 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700458 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400459
460 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700461 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400462
Kirill Batuzov55c09752011-07-07 16:37:16 +0400463 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700464 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400465
Richard Hendersonc578ff12021-12-16 06:07:25 -0800466 CASE_OP_32_64_VEC(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400467 return ~x;
468
Richard Hendersoncb25c802011-08-17 14:11:47 -0700469 CASE_OP_32_64(neg):
470 return -x;
471
Richard Hendersonc578ff12021-12-16 06:07:25 -0800472 CASE_OP_32_64_VEC(andc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700473 return x & ~y;
474
Richard Hendersonc578ff12021-12-16 06:07:25 -0800475 CASE_OP_32_64_VEC(orc):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700476 return x | ~y;
477
Richard Hendersoned523472021-12-16 11:17:46 -0800478 CASE_OP_32_64_VEC(eqv):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700479 return ~(x ^ y);
480
Richard Hendersoned523472021-12-16 11:17:46 -0800481 CASE_OP_32_64_VEC(nand):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700482 return ~(x & y);
483
Richard Hendersoned523472021-12-16 11:17:46 -0800484 CASE_OP_32_64_VEC(nor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700485 return ~(x | y);
486
Richard Henderson0e28d002016-11-16 09:23:28 +0100487 case INDEX_op_clz_i32:
488 return (uint32_t)x ? clz32(x) : y;
489
490 case INDEX_op_clz_i64:
491 return x ? clz64(x) : y;
492
493 case INDEX_op_ctz_i32:
494 return (uint32_t)x ? ctz32(x) : y;
495
496 case INDEX_op_ctz_i64:
497 return x ? ctz64(x) : y;
498
Richard Hendersona768e4e2016-11-21 11:13:39 +0100499 case INDEX_op_ctpop_i32:
500 return ctpop32(x);
501
502 case INDEX_op_ctpop_i64:
503 return ctpop64(x);
504
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700505 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400506 return (int8_t)x;
507
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700508 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400509 return (int16_t)x;
510
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700511 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400512 return (uint8_t)x;
513
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700514 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400515 return (uint16_t)x;
516
Richard Henderson64985942018-11-20 08:53:34 +0100517 CASE_OP_32_64(bswap16):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700518 x = bswap16(x);
519 return y & TCG_BSWAP_OS ? (int16_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100520
521 CASE_OP_32_64(bswap32):
Richard Henderson0b76ff82021-06-13 13:04:00 -0700522 x = bswap32(x);
523 return y & TCG_BSWAP_OS ? (int32_t)x : x;
Richard Henderson64985942018-11-20 08:53:34 +0100524
525 case INDEX_op_bswap64_i64:
526 return bswap64(x);
527
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200528 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400529 case INDEX_op_ext32s_i64:
530 return (int32_t)x;
531
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200532 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700533 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400534 case INDEX_op_ext32u_i64:
535 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400536
Richard Henderson609ad702015-07-24 07:16:00 -0700537 case INDEX_op_extrh_i64_i32:
538 return (uint64_t)x >> 32;
539
Richard Henderson03271522013-08-14 14:35:56 -0700540 case INDEX_op_muluh_i32:
541 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
542 case INDEX_op_mulsh_i32:
543 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
544
545 case INDEX_op_muluh_i64:
546 mulu64(&l64, &h64, x, y);
547 return h64;
548 case INDEX_op_mulsh_i64:
549 muls64(&l64, &h64, x, y);
550 return h64;
551
Richard Henderson01547f72013-08-14 15:22:46 -0700552 case INDEX_op_div_i32:
553 /* Avoid crashing on divide by zero, otherwise undefined. */
554 return (int32_t)x / ((int32_t)y ? : 1);
555 case INDEX_op_divu_i32:
556 return (uint32_t)x / ((uint32_t)y ? : 1);
557 case INDEX_op_div_i64:
558 return (int64_t)x / ((int64_t)y ? : 1);
559 case INDEX_op_divu_i64:
560 return (uint64_t)x / ((uint64_t)y ? : 1);
561
562 case INDEX_op_rem_i32:
563 return (int32_t)x % ((int32_t)y ? : 1);
564 case INDEX_op_remu_i32:
565 return (uint32_t)x % ((uint32_t)y ? : 1);
566 case INDEX_op_rem_i64:
567 return (int64_t)x % ((int64_t)y ? : 1);
568 case INDEX_op_remu_i64:
569 return (uint64_t)x % ((uint64_t)y ? : 1);
570
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400571 default:
Richard Henderson732e89f2023-04-05 12:09:14 -0700572 g_assert_not_reached();
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400573 }
574}
575
Richard Henderson67f84c92021-08-25 08:00:20 -0700576static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
577 uint64_t x, uint64_t y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400578{
Richard Henderson54795542020-09-06 16:21:32 -0700579 uint64_t res = do_constant_folding_2(op, x, y);
Richard Henderson67f84c92021-08-25 08:00:20 -0700580 if (type == TCG_TYPE_I32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200581 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400582 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400583 return res;
584}
585
Richard Henderson9519da72012-10-02 11:32:26 -0700586static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
587{
588 switch (c) {
589 case TCG_COND_EQ:
590 return x == y;
591 case TCG_COND_NE:
592 return x != y;
593 case TCG_COND_LT:
594 return (int32_t)x < (int32_t)y;
595 case TCG_COND_GE:
596 return (int32_t)x >= (int32_t)y;
597 case TCG_COND_LE:
598 return (int32_t)x <= (int32_t)y;
599 case TCG_COND_GT:
600 return (int32_t)x > (int32_t)y;
601 case TCG_COND_LTU:
602 return x < y;
603 case TCG_COND_GEU:
604 return x >= y;
605 case TCG_COND_LEU:
606 return x <= y;
607 case TCG_COND_GTU:
608 return x > y;
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700609 case TCG_COND_TSTEQ:
610 return (x & y) == 0;
611 case TCG_COND_TSTNE:
612 return (x & y) != 0;
613 case TCG_COND_ALWAYS:
614 case TCG_COND_NEVER:
615 break;
Richard Henderson9519da72012-10-02 11:32:26 -0700616 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700617 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700618}
619
620static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
621{
622 switch (c) {
623 case TCG_COND_EQ:
624 return x == y;
625 case TCG_COND_NE:
626 return x != y;
627 case TCG_COND_LT:
628 return (int64_t)x < (int64_t)y;
629 case TCG_COND_GE:
630 return (int64_t)x >= (int64_t)y;
631 case TCG_COND_LE:
632 return (int64_t)x <= (int64_t)y;
633 case TCG_COND_GT:
634 return (int64_t)x > (int64_t)y;
635 case TCG_COND_LTU:
636 return x < y;
637 case TCG_COND_GEU:
638 return x >= y;
639 case TCG_COND_LEU:
640 return x <= y;
641 case TCG_COND_GTU:
642 return x > y;
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700643 case TCG_COND_TSTEQ:
644 return (x & y) == 0;
645 case TCG_COND_TSTNE:
646 return (x & y) != 0;
647 case TCG_COND_ALWAYS:
648 case TCG_COND_NEVER:
649 break;
Richard Henderson9519da72012-10-02 11:32:26 -0700650 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700651 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700652}
653
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700654static int do_constant_folding_cond_eq(TCGCond c)
Richard Henderson9519da72012-10-02 11:32:26 -0700655{
656 switch (c) {
657 case TCG_COND_GT:
658 case TCG_COND_LTU:
659 case TCG_COND_LT:
660 case TCG_COND_GTU:
661 case TCG_COND_NE:
662 return 0;
663 case TCG_COND_GE:
664 case TCG_COND_GEU:
665 case TCG_COND_LE:
666 case TCG_COND_LEU:
667 case TCG_COND_EQ:
668 return 1;
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700669 case TCG_COND_TSTEQ:
670 case TCG_COND_TSTNE:
671 return -1;
672 case TCG_COND_ALWAYS:
673 case TCG_COND_NEVER:
674 break;
Richard Henderson9519da72012-10-02 11:32:26 -0700675 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700676 g_assert_not_reached();
Richard Henderson9519da72012-10-02 11:32:26 -0700677}
678
Richard Henderson8d57bf12021-08-24 08:34:27 -0700679/*
680 * Return -1 if the condition can't be simplified,
681 * and the result of the condition (0 or 1) if it can.
682 */
Richard Henderson67f84c92021-08-25 08:00:20 -0700683static int do_constant_folding_cond(TCGType type, TCGArg x,
Richard Henderson8d57bf12021-08-24 08:34:27 -0700684 TCGArg y, TCGCond c)
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200685{
Richard Henderson63490392017-06-20 13:43:15 -0700686 if (arg_is_const(x) && arg_is_const(y)) {
Alex Bennée9becc362022-02-09 11:21:42 +0000687 uint64_t xv = arg_info(x)->val;
688 uint64_t yv = arg_info(y)->val;
689
Richard Henderson67f84c92021-08-25 08:00:20 -0700690 switch (type) {
691 case TCG_TYPE_I32:
Richard Henderson170ba882017-11-22 09:07:11 +0100692 return do_constant_folding_cond_32(xv, yv, c);
Richard Henderson67f84c92021-08-25 08:00:20 -0700693 case TCG_TYPE_I64:
694 return do_constant_folding_cond_64(xv, yv, c);
695 default:
696 /* Only scalar comparisons are optimizable */
697 return -1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200698 }
Richard Henderson63490392017-06-20 13:43:15 -0700699 } else if (args_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700700 return do_constant_folding_cond_eq(c);
Richard Henderson27cdb852023-10-23 11:38:00 -0700701 } else if (arg_is_const_val(y, 0)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200702 switch (c) {
703 case TCG_COND_LTU:
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700704 case TCG_COND_TSTNE:
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200705 return 0;
706 case TCG_COND_GEU:
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700707 case TCG_COND_TSTEQ:
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200708 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 Henderson7a2f7082021-08-26 07:06:39 -0700716/**
717 * swap_commutative:
718 * @dest: TCGArg of the destination argument, or NO_DEST.
719 * @p1: first paired argument
720 * @p2: second paired argument
721 *
722 * If *@p1 is a constant and *@p2 is not, swap.
723 * If *@p2 matches @dest, swap.
724 * Return true if a swap was performed.
725 */
726
727#define NO_DEST temp_arg(NULL)
728
Richard Henderson24c9ae42012-10-02 11:32:21 -0700729static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
730{
731 TCGArg a1 = *p1, a2 = *p2;
732 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700733 sum += arg_is_const(a1);
734 sum -= arg_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700735
736 /* Prefer the constant in second argument, and then the form
737 op a, a, b, which is better handled on non-RISC hosts. */
738 if (sum > 0 || (sum == 0 && dest == a2)) {
739 *p1 = a2;
740 *p2 = a1;
741 return true;
742 }
743 return false;
744}
745
Richard Henderson0bfcb862012-10-02 11:32:23 -0700746static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
747{
748 int sum = 0;
Richard Henderson63490392017-06-20 13:43:15 -0700749 sum += arg_is_const(p1[0]);
750 sum += arg_is_const(p1[1]);
751 sum -= arg_is_const(p2[0]);
752 sum -= arg_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700753 if (sum > 0) {
754 TCGArg t;
755 t = p1[0], p1[0] = p2[0], p2[0] = t;
756 t = p1[1], p1[1] = p2[1], p2[1] = t;
757 return true;
758 }
759 return false;
760}
761
Richard Henderson7e64b112023-10-24 16:53:56 -0700762/*
763 * Return -1 if the condition can't be simplified,
764 * and the result of the condition (0 or 1) if it can.
765 */
Richard Hendersonfb04ab72024-01-10 18:21:58 +1100766static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest,
Richard Henderson246c4b72023-10-24 16:36:50 -0700767 TCGArg *p1, TCGArg *p2, TCGArg *pcond)
768{
769 TCGCond cond;
770 bool swap;
771 int r;
772
773 swap = swap_commutative(dest, p1, p2);
774 cond = *pcond;
775 if (swap) {
776 *pcond = cond = tcg_swap_cond(cond);
777 }
778
779 r = do_constant_folding_cond(ctx->type, *p1, *p2, cond);
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700780 if (r >= 0) {
781 return r;
782 }
783 if (!is_tst_cond(cond)) {
784 return -1;
785 }
786
787 /*
788 * TSTNE x,x -> NE x,0
789 * TSTNE x,-1 -> NE x,0
790 */
791 if (args_are_copies(*p1, *p2) || arg_is_const_val(*p2, -1)) {
792 *p2 = arg_new_constant(ctx, 0);
793 *pcond = tcg_tst_eqne_cond(cond);
794 return -1;
795 }
796
797 /* TSTNE x,sign -> LT x,0 */
798 if (arg_is_const_val(*p2, (ctx->type == TCG_TYPE_I32
799 ? INT32_MIN : INT64_MIN))) {
800 *p2 = arg_new_constant(ctx, 0);
801 *pcond = tcg_tst_ltge_cond(cond);
Richard Hendersonfb04ab72024-01-10 18:21:58 +1100802 return -1;
803 }
804
805 /* Expand to AND with a temporary if no backend support. */
806 if (!TCG_TARGET_HAS_tst) {
807 TCGOpcode and_opc = (ctx->type == TCG_TYPE_I32
808 ? INDEX_op_and_i32 : INDEX_op_and_i64);
809 TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, and_opc, 3);
810 TCGArg tmp = arg_new_temp(ctx);
811
812 op2->args[0] = tmp;
813 op2->args[1] = *p1;
814 op2->args[2] = *p2;
815
816 *p1 = tmp;
817 *p2 = arg_new_constant(ctx, 0);
818 *pcond = tcg_tst_eqne_cond(cond);
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700819 }
820 return -1;
Richard Henderson246c4b72023-10-24 16:36:50 -0700821}
822
Richard Hendersonfb04ab72024-01-10 18:21:58 +1100823static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args)
Richard Henderson7e64b112023-10-24 16:53:56 -0700824{
825 TCGArg al, ah, bl, bh;
826 TCGCond c;
827 bool swap;
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700828 int r;
Richard Henderson7e64b112023-10-24 16:53:56 -0700829
830 swap = swap_commutative2(args, args + 2);
831 c = args[4];
832 if (swap) {
833 args[4] = c = tcg_swap_cond(c);
834 }
835
836 al = args[0];
837 ah = args[1];
838 bl = args[2];
839 bh = args[3];
840
841 if (arg_is_const(bl) && arg_is_const(bh)) {
842 tcg_target_ulong blv = arg_info(bl)->val;
843 tcg_target_ulong bhv = arg_info(bh)->val;
844 uint64_t b = deposit64(blv, 32, 32, bhv);
845
846 if (arg_is_const(al) && arg_is_const(ah)) {
847 tcg_target_ulong alv = arg_info(al)->val;
848 tcg_target_ulong ahv = arg_info(ah)->val;
849 uint64_t a = deposit64(alv, 32, 32, ahv);
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700850
851 r = do_constant_folding_cond_64(a, b, c);
852 if (r >= 0) {
853 return r;
854 }
Richard Henderson7e64b112023-10-24 16:53:56 -0700855 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700856
Richard Henderson7e64b112023-10-24 16:53:56 -0700857 if (b == 0) {
858 switch (c) {
859 case TCG_COND_LTU:
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700860 case TCG_COND_TSTNE:
Richard Henderson7e64b112023-10-24 16:53:56 -0700861 return 0;
862 case TCG_COND_GEU:
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700863 case TCG_COND_TSTEQ:
Richard Henderson7e64b112023-10-24 16:53:56 -0700864 return 1;
865 default:
866 break;
867 }
868 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700869
870 /* TSTNE x,-1 -> NE x,0 */
871 if (b == -1 && is_tst_cond(c)) {
872 args[3] = args[2] = arg_new_constant(ctx, 0);
873 args[4] = tcg_tst_eqne_cond(c);
874 return -1;
875 }
876
877 /* TSTNE x,sign -> LT x,0 */
878 if (b == INT64_MIN && is_tst_cond(c)) {
879 /* bl must be 0, so copy that to bh */
880 args[3] = bl;
881 args[4] = tcg_tst_ltge_cond(c);
882 return -1;
883 }
Richard Henderson7e64b112023-10-24 16:53:56 -0700884 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700885
Richard Henderson7e64b112023-10-24 16:53:56 -0700886 if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
Richard Hendersonceb9ee02023-10-23 23:44:27 -0700887 r = do_constant_folding_cond_eq(c);
888 if (r >= 0) {
889 return r;
890 }
891
892 /* TSTNE x,x -> NE x,0 */
893 if (is_tst_cond(c)) {
894 args[3] = args[2] = arg_new_constant(ctx, 0);
895 args[4] = tcg_tst_eqne_cond(c);
896 return -1;
897 }
Richard Henderson7e64b112023-10-24 16:53:56 -0700898 }
Richard Hendersonfb04ab72024-01-10 18:21:58 +1100899
900 /* Expand to AND with a temporary if no backend support. */
901 if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) {
902 TCGOp *op1 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_and_i32, 3);
903 TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_and_i32, 3);
904 TCGArg t1 = arg_new_temp(ctx);
905 TCGArg t2 = arg_new_temp(ctx);
906
907 op1->args[0] = t1;
908 op1->args[1] = al;
909 op1->args[2] = bl;
910 op2->args[0] = t2;
911 op2->args[1] = ah;
912 op2->args[2] = bh;
913
914 args[0] = t1;
915 args[1] = t2;
916 args[3] = args[2] = arg_new_constant(ctx, 0);
917 args[4] = tcg_tst_eqne_cond(c);
918 }
Richard Henderson7e64b112023-10-24 16:53:56 -0700919 return -1;
920}
921
Richard Hendersone2577ea2021-08-24 08:00:48 -0700922static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
923{
924 for (int i = 0; i < nb_args; i++) {
925 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000926 init_ts_info(ctx, ts);
Richard Hendersone2577ea2021-08-24 08:00:48 -0700927 }
928}
929
Richard Henderson8774dde2021-08-24 08:04:47 -0700930static void copy_propagate(OptContext *ctx, TCGOp *op,
931 int nb_oargs, int nb_iargs)
932{
Richard Henderson8774dde2021-08-24 08:04:47 -0700933 for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
934 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +1000935 if (ts_is_copy(ts)) {
Richard Henderson9f75e522023-11-02 13:37:46 -0700936 op->args[i] = temp_arg(find_better_copy(ts));
Richard Henderson8774dde2021-08-24 08:04:47 -0700937 }
938 }
939}
940
Richard Henderson15268552024-12-08 07:45:11 -0600941static void finish_bb(OptContext *ctx)
942{
943 /* We only optimize memory barriers across basic blocks. */
944 ctx->prev_mb = NULL;
945}
946
947static void finish_ebb(OptContext *ctx)
948{
949 finish_bb(ctx);
950 /* We only optimize across extended basic blocks. */
951 memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
952 remove_mem_copy_all(ctx);
953}
954
Richard Hendersonf3ed3cf2024-12-08 18:39:47 -0600955static bool finish_folding(OptContext *ctx, TCGOp *op)
Richard Henderson137f1f42021-08-24 08:49:25 -0700956{
957 const TCGOpDef *def = &tcg_op_defs[op->opc];
958 int i, nb_oargs;
959
Richard Henderson137f1f42021-08-24 08:49:25 -0700960 nb_oargs = def->nb_oargs;
961 for (i = 0; i < nb_oargs; i++) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700962 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson986cac12023-01-09 13:59:35 -0800963 reset_ts(ctx, ts);
Richard Henderson137f1f42021-08-24 08:49:25 -0700964 /*
Richard Henderson57fe5c62021-08-26 12:04:46 -0700965 * Save the corresponding known-zero/sign bits mask for the
Richard Henderson137f1f42021-08-24 08:49:25 -0700966 * first output argument (only one supported so far).
967 */
968 if (i == 0) {
Richard Henderson57fe5c62021-08-26 12:04:46 -0700969 ts_info(ts)->z_mask = ctx->z_mask;
Richard Henderson137f1f42021-08-24 08:49:25 -0700970 }
971 }
Richard Hendersonf3ed3cf2024-12-08 18:39:47 -0600972 return true;
Richard Henderson137f1f42021-08-24 08:49:25 -0700973}
974
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700975/*
976 * The fold_* functions return true when processing is complete,
977 * usually by folding the operation to a constant or to a copy,
978 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
979 * like collect information about the value produced, for use in
980 * optimizing a subsequent operation.
981 *
982 * These first fold_* functions are all helpers, used by other
983 * folders for more specific operations.
984 */
985
986static bool fold_const1(OptContext *ctx, TCGOp *op)
987{
988 if (arg_is_const(op->args[1])) {
989 uint64_t t;
990
991 t = arg_info(op->args[1])->val;
Richard Henderson67f84c92021-08-25 08:00:20 -0700992 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson2f9f08b2021-08-25 12:03:48 -0700993 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
994 }
995 return false;
996}
997
998static bool fold_const2(OptContext *ctx, TCGOp *op)
999{
1000 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1001 uint64_t t1 = arg_info(op->args[1])->val;
1002 uint64_t t2 = arg_info(op->args[2])->val;
1003
Richard Henderson67f84c92021-08-25 08:00:20 -07001004 t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001005 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1006 }
1007 return false;
1008}
1009
Richard Hendersonc578ff12021-12-16 06:07:25 -08001010static bool fold_commutative(OptContext *ctx, TCGOp *op)
1011{
1012 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
1013 return false;
1014}
1015
Richard Henderson7a2f7082021-08-26 07:06:39 -07001016static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
1017{
1018 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
1019 return fold_const2(ctx, op);
1020}
1021
Richard Hendersond582b142024-12-19 10:43:26 -08001022/*
1023 * Record "zero" and "sign" masks for the single output of @op.
1024 * See TempOptInfo definition of z_mask and s_mask.
1025 * If z_mask allows, fold the output to constant zero.
Richard Henderson75c3bf32024-12-19 10:50:40 -08001026 * The passed s_mask may be augmented by z_mask.
Richard Hendersond582b142024-12-19 10:43:26 -08001027 */
1028static bool fold_masks_zs(OptContext *ctx, TCGOp *op,
Richard Henderson6d70ddc2024-12-21 21:08:10 -08001029 uint64_t z_mask, int64_t s_mask)
Richard Hendersonfae450b2021-08-25 22:42:19 -07001030{
Richard Henderson56e06ec2024-12-08 18:26:48 -06001031 const TCGOpDef *def = &tcg_op_defs[op->opc];
1032 TCGTemp *ts;
1033 TempOptInfo *ti;
Richard Henderson6d70ddc2024-12-21 21:08:10 -08001034 int rep;
Richard Henderson56e06ec2024-12-08 18:26:48 -06001035
1036 /* Only single-output opcodes are supported here. */
1037 tcg_debug_assert(def->nb_oargs == 1);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001038
1039 /*
Richard Hendersonfaa2e102021-08-26 09:03:59 -07001040 * 32-bit ops generate 32-bit results, which for the purpose of
1041 * simplifying tcg are sign-extended. Certainly that's how we
1042 * represent our constants elsewhere. Note that the bits will
1043 * be reset properly for a 64-bit value when encountering the
1044 * type changing opcodes.
Richard Hendersonfae450b2021-08-25 22:42:19 -07001045 */
1046 if (ctx->type == TCG_TYPE_I32) {
Richard Hendersonfaa2e102021-08-26 09:03:59 -07001047 z_mask = (int32_t)z_mask;
Richard Henderson6d70ddc2024-12-21 21:08:10 -08001048 s_mask |= INT32_MIN;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001049 }
1050
1051 if (z_mask == 0) {
1052 return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
1053 }
Richard Henderson56e06ec2024-12-08 18:26:48 -06001054
1055 ts = arg_temp(op->args[0]);
1056 reset_ts(ctx, ts);
1057
1058 ti = ts_info(ts);
1059 ti->z_mask = z_mask;
Richard Henderson6d70ddc2024-12-21 21:08:10 -08001060
1061 /* Canonicalize s_mask and incorporate data from z_mask. */
1062 rep = clz64(~s_mask);
1063 rep = MAX(rep, clz64(z_mask));
1064 rep = MAX(rep - 1, 0);
1065 ti->s_mask = INT64_MIN >> rep;
1066
Richard Henderson56e06ec2024-12-08 18:26:48 -06001067 return true;
Richard Henderson045ace32024-12-19 10:33:51 -08001068}
1069
Richard Henderson81be07f2024-12-08 19:49:17 -06001070static bool fold_masks_z(OptContext *ctx, TCGOp *op, uint64_t z_mask)
1071{
1072 return fold_masks_zs(ctx, op, z_mask, 0);
1073}
1074
Richard Hendersond582b142024-12-19 10:43:26 -08001075static bool fold_masks(OptContext *ctx, TCGOp *op)
1076{
1077 return fold_masks_zs(ctx, op, ctx->z_mask, ctx->s_mask);
1078}
1079
Richard Henderson045ace32024-12-19 10:33:51 -08001080/*
1081 * An "affected" mask bit is 0 if and only if the result is identical
1082 * to the first input. Thus if the entire mask is 0, the operation
1083 * is equivalent to a copy.
1084 */
1085static bool fold_affected_mask(OptContext *ctx, TCGOp *op, uint64_t a_mask)
1086{
1087 if (ctx->type == TCG_TYPE_I32) {
1088 a_mask = (uint32_t)a_mask;
1089 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001090 if (a_mask == 0) {
1091 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1092 }
1093 return false;
1094}
1095
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001096/*
1097 * Convert @op to NOT, if NOT is supported by the host.
1098 * Return true f the conversion is successful, which will still
1099 * indicate that the processing is complete.
1100 */
1101static bool fold_not(OptContext *ctx, TCGOp *op);
1102static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
1103{
1104 TCGOpcode not_op;
1105 bool have_not;
1106
1107 switch (ctx->type) {
1108 case TCG_TYPE_I32:
1109 not_op = INDEX_op_not_i32;
1110 have_not = TCG_TARGET_HAS_not_i32;
1111 break;
1112 case TCG_TYPE_I64:
1113 not_op = INDEX_op_not_i64;
1114 have_not = TCG_TARGET_HAS_not_i64;
1115 break;
1116 case TCG_TYPE_V64:
1117 case TCG_TYPE_V128:
1118 case TCG_TYPE_V256:
1119 not_op = INDEX_op_not_vec;
1120 have_not = TCG_TARGET_HAS_not_vec;
1121 break;
1122 default:
1123 g_assert_not_reached();
1124 }
1125 if (have_not) {
1126 op->opc = not_op;
1127 op->args[1] = op->args[idx];
1128 return fold_not(ctx, op);
1129 }
1130 return false;
1131}
1132
Richard Hendersonda48e272021-08-25 20:42:04 -07001133/* If the binary operation has first argument @i, fold to @i. */
1134static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1135{
Richard Henderson27cdb852023-10-23 11:38:00 -07001136 if (arg_is_const_val(op->args[1], i)) {
Richard Hendersonda48e272021-08-25 20:42:04 -07001137 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1138 }
1139 return false;
1140}
1141
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001142/* If the binary operation has first argument @i, fold to NOT. */
1143static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
1144{
Richard Henderson27cdb852023-10-23 11:38:00 -07001145 if (arg_is_const_val(op->args[1], i)) {
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001146 return fold_to_not(ctx, op, 2);
1147 }
1148 return false;
1149}
1150
Richard Hendersone8679952021-08-25 13:19:52 -07001151/* If the binary operation has second argument @i, fold to @i. */
1152static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1153{
Richard Henderson27cdb852023-10-23 11:38:00 -07001154 if (arg_is_const_val(op->args[2], i)) {
Richard Hendersone8679952021-08-25 13:19:52 -07001155 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1156 }
1157 return false;
1158}
1159
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001160/* If the binary operation has second argument @i, fold to identity. */
1161static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
1162{
Richard Henderson27cdb852023-10-23 11:38:00 -07001163 if (arg_is_const_val(op->args[2], i)) {
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001164 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1165 }
1166 return false;
1167}
1168
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001169/* If the binary operation has second argument @i, fold to NOT. */
1170static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
1171{
Richard Henderson27cdb852023-10-23 11:38:00 -07001172 if (arg_is_const_val(op->args[2], i)) {
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001173 return fold_to_not(ctx, op, 1);
1174 }
1175 return false;
1176}
1177
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001178/* If the binary operation has both arguments equal, fold to @i. */
1179static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
1180{
1181 if (args_are_copies(op->args[1], op->args[2])) {
1182 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1183 }
1184 return false;
1185}
1186
Richard Hendersonca7bb042021-08-25 13:14:21 -07001187/* If the binary operation has both arguments equal, fold to identity. */
1188static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
1189{
1190 if (args_are_copies(op->args[1], op->args[2])) {
1191 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1192 }
1193 return false;
1194}
1195
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001196/*
1197 * These outermost fold_<op> functions are sorted alphabetically.
Richard Hendersonca7bb042021-08-25 13:14:21 -07001198 *
1199 * The ordering of the transformations should be:
1200 * 1) those that produce a constant
1201 * 2) those that produce a copy
1202 * 3) those that produce information about the result value.
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001203 */
1204
1205static bool fold_add(OptContext *ctx, TCGOp *op)
1206{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001207 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001208 fold_xi_to_x(ctx, op, 0)) {
1209 return true;
1210 }
Richard Hendersonf3ed3cf2024-12-08 18:39:47 -06001211 return finish_folding(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001212}
1213
Richard Hendersonc578ff12021-12-16 06:07:25 -08001214/* We cannot as yet do_constant_folding with vectors. */
1215static bool fold_add_vec(OptContext *ctx, TCGOp *op)
1216{
1217 if (fold_commutative(ctx, op) ||
1218 fold_xi_to_x(ctx, op, 0)) {
1219 return true;
1220 }
Richard Hendersonf3ed3cf2024-12-08 18:39:47 -06001221 return finish_folding(ctx, op);
Richard Hendersonc578ff12021-12-16 06:07:25 -08001222}
1223
Richard Henderson9531c072021-08-26 06:51:39 -07001224static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001225{
Richard Hendersonf2457572023-10-25 18:39:44 -07001226 bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]);
1227 bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]);
1228
1229 if (a_const && b_const) {
Richard Henderson9531c072021-08-26 06:51:39 -07001230 uint64_t al = arg_info(op->args[2])->val;
1231 uint64_t ah = arg_info(op->args[3])->val;
1232 uint64_t bl = arg_info(op->args[4])->val;
1233 uint64_t bh = arg_info(op->args[5])->val;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001234 TCGArg rl, rh;
Richard Henderson9531c072021-08-26 06:51:39 -07001235 TCGOp *op2;
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001236
Richard Henderson9531c072021-08-26 06:51:39 -07001237 if (ctx->type == TCG_TYPE_I32) {
1238 uint64_t a = deposit64(al, 32, 32, ah);
1239 uint64_t b = deposit64(bl, 32, 32, bh);
1240
1241 if (add) {
1242 a += b;
1243 } else {
1244 a -= b;
1245 }
1246
1247 al = sextract64(a, 0, 32);
1248 ah = sextract64(a, 32, 32);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001249 } else {
Richard Henderson9531c072021-08-26 06:51:39 -07001250 Int128 a = int128_make128(al, ah);
1251 Int128 b = int128_make128(bl, bh);
1252
1253 if (add) {
1254 a = int128_add(a, b);
1255 } else {
1256 a = int128_sub(a, b);
1257 }
1258
1259 al = int128_getlo(a);
1260 ah = int128_gethi(a);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001261 }
1262
1263 rl = op->args[0];
1264 rh = op->args[1];
Richard Henderson9531c072021-08-26 06:51:39 -07001265
1266 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01001267 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson9531c072021-08-26 06:51:39 -07001268
1269 tcg_opt_gen_movi(ctx, op, rl, al);
1270 tcg_opt_gen_movi(ctx, op2, rh, ah);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001271 return true;
1272 }
Richard Hendersonf2457572023-10-25 18:39:44 -07001273
1274 /* Fold sub2 r,x,i to add2 r,x,-i */
1275 if (!add && b_const) {
1276 uint64_t bl = arg_info(op->args[4])->val;
1277 uint64_t bh = arg_info(op->args[5])->val;
1278
1279 /* Negate the two parts without assembling and disassembling. */
1280 bl = -bl;
1281 bh = ~bh + !bl;
1282
1283 op->opc = (ctx->type == TCG_TYPE_I32
1284 ? INDEX_op_add2_i32 : INDEX_op_add2_i64);
1285 op->args[4] = arg_new_constant(ctx, bl);
1286 op->args[5] = arg_new_constant(ctx, bh);
1287 }
Richard Hendersonf3ed3cf2024-12-08 18:39:47 -06001288 return finish_folding(ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001289}
1290
Richard Henderson9531c072021-08-26 06:51:39 -07001291static bool fold_add2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001292{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001293 /* Note that the high and low parts may be independently swapped. */
1294 swap_commutative(op->args[0], &op->args[2], &op->args[4]);
1295 swap_commutative(op->args[1], &op->args[3], &op->args[5]);
1296
Richard Henderson9531c072021-08-26 06:51:39 -07001297 return fold_addsub2(ctx, op, true);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07001298}
1299
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001300static bool fold_and(OptContext *ctx, TCGOp *op)
1301{
Richard Henderson1ca73722024-12-08 18:47:15 -06001302 uint64_t z1, z2, z_mask, s_mask;
1303 TempOptInfo *t1, *t2;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001304
Richard Henderson7a2f7082021-08-26 07:06:39 -07001305 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001306 fold_xi_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001307 fold_xi_to_x(ctx, op, -1) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07001308 fold_xx_to_x(ctx, op)) {
1309 return true;
1310 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001311
Richard Henderson1ca73722024-12-08 18:47:15 -06001312 t1 = arg_info(op->args[1]);
1313 t2 = arg_info(op->args[2]);
1314 z1 = t1->z_mask;
1315 z2 = t2->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001316
1317 /*
Richard Hendersonfae450b2021-08-25 22:42:19 -07001318 * Known-zeros does not imply known-ones. Therefore unless
1319 * arg2 is constant, we can't infer affected bits from it.
1320 */
Richard Henderson1ca73722024-12-08 18:47:15 -06001321 if (ti_is_const(t2) && fold_affected_mask(ctx, op, z1 & ~z2)) {
Richard Henderson045ace32024-12-19 10:33:51 -08001322 return true;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001323 }
1324
Richard Henderson1ca73722024-12-08 18:47:15 -06001325 z_mask = z1 & z2;
1326
1327 /*
1328 * Sign repetitions are perforce all identical, whether they are 1 or 0.
1329 * Bitwise operations preserve the relative quantity of the repetitions.
1330 */
1331 s_mask = t1->s_mask & t2->s_mask;
1332
1333 return fold_masks_zs(ctx, op, z_mask, s_mask);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001334}
1335
1336static bool fold_andc(OptContext *ctx, TCGOp *op)
1337{
Richard Henderson21e2b5f2024-12-08 18:56:55 -06001338 uint64_t z_mask, s_mask;
1339 TempOptInfo *t1, *t2;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001340
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001341 if (fold_const2(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001342 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001343 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001344 fold_ix_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07001345 return true;
1346 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001347
Richard Henderson21e2b5f2024-12-08 18:56:55 -06001348 t1 = arg_info(op->args[1]);
1349 t2 = arg_info(op->args[2]);
1350 z_mask = t1->z_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001351
1352 /*
1353 * Known-zeros does not imply known-ones. Therefore unless
1354 * arg2 is constant, we can't infer anything from it.
1355 */
Richard Henderson21e2b5f2024-12-08 18:56:55 -06001356 if (ti_is_const(t2)) {
1357 uint64_t v2 = ti_const_val(t2);
1358 if (fold_affected_mask(ctx, op, z_mask & v2)) {
Richard Henderson045ace32024-12-19 10:33:51 -08001359 return true;
1360 }
Richard Henderson21e2b5f2024-12-08 18:56:55 -06001361 z_mask &= ~v2;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001362 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001363
Richard Henderson21e2b5f2024-12-08 18:56:55 -06001364 s_mask = t1->s_mask & t2->s_mask;
1365 return fold_masks_zs(ctx, op, z_mask, s_mask);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001366}
1367
Richard Henderson079b0802021-08-24 09:30:59 -07001368static bool fold_brcond(OptContext *ctx, TCGOp *op)
1369{
Richard Hendersonfb04ab72024-01-10 18:21:58 +11001370 int i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[0],
Richard Henderson246c4b72023-10-24 16:36:50 -07001371 &op->args[1], &op->args[2]);
Richard Henderson079b0802021-08-24 09:30:59 -07001372 if (i == 0) {
1373 tcg_op_remove(ctx->tcg, op);
1374 return true;
1375 }
1376 if (i > 0) {
1377 op->opc = INDEX_op_br;
1378 op->args[0] = op->args[3];
Richard Henderson15268552024-12-08 07:45:11 -06001379 finish_ebb(ctx);
1380 } else {
1381 finish_bb(ctx);
Richard Henderson079b0802021-08-24 09:30:59 -07001382 }
Richard Henderson15268552024-12-08 07:45:11 -06001383 return true;
Richard Henderson079b0802021-08-24 09:30:59 -07001384}
1385
Richard Henderson764d2ab2021-08-24 09:22:11 -07001386static bool fold_brcond2(OptContext *ctx, TCGOp *op)
1387{
Richard Henderson7e64b112023-10-24 16:53:56 -07001388 TCGCond cond;
1389 TCGArg label;
Richard Henderson7a2f7082021-08-26 07:06:39 -07001390 int i, inv = 0;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001391
Richard Hendersonfb04ab72024-01-10 18:21:58 +11001392 i = do_constant_folding_cond2(ctx, op, &op->args[0]);
Richard Henderson7e64b112023-10-24 16:53:56 -07001393 cond = op->args[4];
1394 label = op->args[5];
Richard Henderson764d2ab2021-08-24 09:22:11 -07001395 if (i >= 0) {
1396 goto do_brcond_const;
1397 }
1398
1399 switch (cond) {
1400 case TCG_COND_LT:
1401 case TCG_COND_GE:
1402 /*
1403 * Simplify LT/GE comparisons vs zero to a single compare
1404 * vs the high word of the input.
1405 */
Richard Henderson27cdb852023-10-23 11:38:00 -07001406 if (arg_is_const_val(op->args[2], 0) &&
1407 arg_is_const_val(op->args[3], 0)) {
Richard Henderson764d2ab2021-08-24 09:22:11 -07001408 goto do_brcond_high;
1409 }
1410 break;
1411
1412 case TCG_COND_NE:
1413 inv = 1;
1414 QEMU_FALLTHROUGH;
1415 case TCG_COND_EQ:
1416 /*
1417 * Simplify EQ/NE comparisons where one of the pairs
1418 * can be simplified.
1419 */
Richard Henderson67f84c92021-08-25 08:00:20 -07001420 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001421 op->args[2], cond);
1422 switch (i ^ inv) {
1423 case 0:
1424 goto do_brcond_const;
1425 case 1:
1426 goto do_brcond_high;
1427 }
1428
Richard Henderson67f84c92021-08-25 08:00:20 -07001429 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Henderson764d2ab2021-08-24 09:22:11 -07001430 op->args[3], cond);
1431 switch (i ^ inv) {
1432 case 0:
1433 goto do_brcond_const;
1434 case 1:
Richard Hendersonceb9ee02023-10-23 23:44:27 -07001435 goto do_brcond_low;
1436 }
1437 break;
1438
1439 case TCG_COND_TSTEQ:
1440 case TCG_COND_TSTNE:
1441 if (arg_is_const_val(op->args[2], 0)) {
1442 goto do_brcond_high;
1443 }
1444 if (arg_is_const_val(op->args[3], 0)) {
1445 goto do_brcond_low;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001446 }
1447 break;
1448
1449 default:
1450 break;
1451
Richard Hendersonceb9ee02023-10-23 23:44:27 -07001452 do_brcond_low:
1453 op->opc = INDEX_op_brcond_i32;
1454 op->args[1] = op->args[2];
1455 op->args[2] = cond;
1456 op->args[3] = label;
1457 return fold_brcond(ctx, op);
1458
Richard Henderson764d2ab2021-08-24 09:22:11 -07001459 do_brcond_high:
1460 op->opc = INDEX_op_brcond_i32;
1461 op->args[0] = op->args[1];
1462 op->args[1] = op->args[3];
1463 op->args[2] = cond;
1464 op->args[3] = label;
Richard Hendersonceb9ee02023-10-23 23:44:27 -07001465 return fold_brcond(ctx, op);
Richard Henderson764d2ab2021-08-24 09:22:11 -07001466
1467 do_brcond_const:
1468 if (i == 0) {
1469 tcg_op_remove(ctx->tcg, op);
1470 return true;
1471 }
1472 op->opc = INDEX_op_br;
1473 op->args[0] = label;
Richard Henderson15268552024-12-08 07:45:11 -06001474 finish_ebb(ctx);
1475 return true;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001476 }
Richard Henderson15268552024-12-08 07:45:11 -06001477
1478 finish_bb(ctx);
1479 return true;
Richard Henderson764d2ab2021-08-24 09:22:11 -07001480}
1481
Richard Henderson09bacdc2021-08-24 11:58:12 -07001482static bool fold_bswap(OptContext *ctx, TCGOp *op)
1483{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001484 uint64_t z_mask, s_mask, sign;
Richard Hendersonc1e7b982024-12-08 19:42:20 -06001485 TempOptInfo *t1 = arg_info(op->args[1]);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001486
Richard Hendersonc1e7b982024-12-08 19:42:20 -06001487 if (ti_is_const(t1)) {
1488 return tcg_opt_gen_movi(ctx, op, op->args[0],
1489 do_constant_folding(op->opc, ctx->type,
1490 ti_const_val(t1),
1491 op->args[2]));
Richard Henderson09bacdc2021-08-24 11:58:12 -07001492 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001493
Richard Hendersonc1e7b982024-12-08 19:42:20 -06001494 z_mask = t1->z_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001495 switch (op->opc) {
1496 case INDEX_op_bswap16_i32:
1497 case INDEX_op_bswap16_i64:
1498 z_mask = bswap16(z_mask);
1499 sign = INT16_MIN;
1500 break;
1501 case INDEX_op_bswap32_i32:
1502 case INDEX_op_bswap32_i64:
1503 z_mask = bswap32(z_mask);
1504 sign = INT32_MIN;
1505 break;
1506 case INDEX_op_bswap64_i64:
1507 z_mask = bswap64(z_mask);
1508 sign = INT64_MIN;
1509 break;
1510 default:
1511 g_assert_not_reached();
1512 }
1513
Richard Henderson75c3bf32024-12-19 10:50:40 -08001514 s_mask = 0;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001515 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1516 case TCG_BSWAP_OZ:
1517 break;
1518 case TCG_BSWAP_OS:
1519 /* If the sign bit may be 1, force all the bits above to 1. */
1520 if (z_mask & sign) {
1521 z_mask |= sign;
1522 }
Richard Hendersonc1e7b982024-12-08 19:42:20 -06001523 /* The value and therefore s_mask is explicitly sign-extended. */
1524 s_mask = sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001525 break;
1526 default:
1527 /* The high bits are undefined: force all bits above the sign to 1. */
1528 z_mask |= sign << 1;
1529 break;
1530 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001531
Richard Hendersonc1e7b982024-12-08 19:42:20 -06001532 return fold_masks_zs(ctx, op, z_mask, s_mask);
Richard Henderson09bacdc2021-08-24 11:58:12 -07001533}
1534
Richard Henderson5cf32be2021-08-24 08:17:08 -07001535static bool fold_call(OptContext *ctx, TCGOp *op)
1536{
1537 TCGContext *s = ctx->tcg;
1538 int nb_oargs = TCGOP_CALLO(op);
1539 int nb_iargs = TCGOP_CALLI(op);
1540 int flags, i;
1541
1542 init_arguments(ctx, op, nb_oargs + nb_iargs);
1543 copy_propagate(ctx, op, nb_oargs, nb_iargs);
1544
1545 /* If the function reads or writes globals, reset temp data. */
1546 flags = tcg_call_flags(op);
1547 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1548 int nb_globals = s->nb_globals;
1549
1550 for (i = 0; i < nb_globals; i++) {
1551 if (test_bit(i, ctx->temps_used.l)) {
Richard Henderson986cac12023-01-09 13:59:35 -08001552 reset_ts(ctx, &ctx->tcg->temps[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001553 }
1554 }
1555 }
1556
Richard Hendersonab84dc32023-08-23 23:04:24 -07001557 /* If the function has side effects, reset mem data. */
1558 if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) {
1559 remove_mem_copy_all(ctx);
1560 }
1561
Richard Henderson5cf32be2021-08-24 08:17:08 -07001562 /* Reset temp data for outputs. */
1563 for (i = 0; i < nb_oargs; i++) {
Richard Henderson986cac12023-01-09 13:59:35 -08001564 reset_temp(ctx, op->args[i]);
Richard Henderson5cf32be2021-08-24 08:17:08 -07001565 }
1566
1567 /* Stop optimizing MB across calls. */
1568 ctx->prev_mb = NULL;
1569 return true;
1570}
1571
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001572static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1573{
Richard Hendersonce1d6632024-12-08 19:47:51 -06001574 uint64_t z_mask, s_mask;
1575 TempOptInfo *t1 = arg_info(op->args[1]);
1576 TempOptInfo *t2 = arg_info(op->args[2]);
Richard Hendersonfae450b2021-08-25 22:42:19 -07001577
Richard Hendersonce1d6632024-12-08 19:47:51 -06001578 if (ti_is_const(t1)) {
1579 uint64_t t = ti_const_val(t1);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001580
1581 if (t != 0) {
Richard Henderson67f84c92021-08-25 08:00:20 -07001582 t = do_constant_folding(op->opc, ctx->type, t, 0);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001583 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1584 }
1585 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1586 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001587
1588 switch (ctx->type) {
1589 case TCG_TYPE_I32:
1590 z_mask = 31;
1591 break;
1592 case TCG_TYPE_I64:
1593 z_mask = 63;
1594 break;
1595 default:
1596 g_assert_not_reached();
1597 }
Richard Hendersonce1d6632024-12-08 19:47:51 -06001598 s_mask = ~z_mask;
1599 z_mask |= t2->z_mask;
1600 s_mask &= t2->s_mask;
1601
1602 return fold_masks_zs(ctx, op, z_mask, s_mask);
Richard Henderson30dd0bf2021-08-24 10:51:34 -07001603}
1604
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001605static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1606{
Richard Henderson81be07f2024-12-08 19:49:17 -06001607 uint64_t z_mask;
1608
Richard Hendersonfae450b2021-08-25 22:42:19 -07001609 if (fold_const1(ctx, op)) {
1610 return true;
1611 }
1612
1613 switch (ctx->type) {
1614 case TCG_TYPE_I32:
Richard Henderson81be07f2024-12-08 19:49:17 -06001615 z_mask = 32 | 31;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001616 break;
1617 case TCG_TYPE_I64:
Richard Henderson81be07f2024-12-08 19:49:17 -06001618 z_mask = 64 | 63;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001619 break;
1620 default:
1621 g_assert_not_reached();
1622 }
Richard Henderson81be07f2024-12-08 19:49:17 -06001623 return fold_masks_z(ctx, op, z_mask);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001624}
1625
Richard Henderson1b1907b2021-08-24 10:47:04 -07001626static bool fold_deposit(OptContext *ctx, TCGOp *op)
1627{
Richard Henderson8f7a8402023-08-13 11:03:05 -07001628 TCGOpcode and_opc;
1629
Richard Henderson1b1907b2021-08-24 10:47:04 -07001630 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1631 uint64_t t1 = arg_info(op->args[1])->val;
1632 uint64_t t2 = arg_info(op->args[2])->val;
1633
1634 t1 = deposit64(t1, op->args[3], op->args[4], t2);
1635 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1636 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001637
Richard Henderson8f7a8402023-08-13 11:03:05 -07001638 switch (ctx->type) {
1639 case TCG_TYPE_I32:
1640 and_opc = INDEX_op_and_i32;
1641 break;
1642 case TCG_TYPE_I64:
1643 and_opc = INDEX_op_and_i64;
1644 break;
1645 default:
1646 g_assert_not_reached();
1647 }
1648
1649 /* Inserting a value into zero at offset 0. */
Richard Henderson27cdb852023-10-23 11:38:00 -07001650 if (arg_is_const_val(op->args[1], 0) && op->args[3] == 0) {
Richard Henderson8f7a8402023-08-13 11:03:05 -07001651 uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
1652
1653 op->opc = and_opc;
1654 op->args[1] = op->args[2];
Richard Henderson26aac972023-10-23 12:31:57 -07001655 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001656 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1657 return false;
1658 }
1659
1660 /* Inserting zero into a value. */
Richard Henderson27cdb852023-10-23 11:38:00 -07001661 if (arg_is_const_val(op->args[2], 0)) {
Richard Henderson8f7a8402023-08-13 11:03:05 -07001662 uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
1663
1664 op->opc = and_opc;
Richard Henderson26aac972023-10-23 12:31:57 -07001665 op->args[2] = arg_new_constant(ctx, mask);
Richard Henderson8f7a8402023-08-13 11:03:05 -07001666 ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
1667 return false;
1668 }
1669
Richard Hendersonfae450b2021-08-25 22:42:19 -07001670 ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
1671 op->args[3], op->args[4],
1672 arg_info(op->args[2])->z_mask);
Richard Henderson1b1907b2021-08-24 10:47:04 -07001673 return false;
1674}
1675
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001676static bool fold_divide(OptContext *ctx, TCGOp *op)
1677{
Richard Henderson2f9d9a32021-10-25 11:30:14 -07001678 if (fold_const2(ctx, op) ||
1679 fold_xi_to_x(ctx, op, 1)) {
1680 return true;
1681 }
1682 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001683}
1684
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07001685static bool fold_dup(OptContext *ctx, TCGOp *op)
1686{
1687 if (arg_is_const(op->args[1])) {
1688 uint64_t t = arg_info(op->args[1])->val;
1689 t = dup_const(TCGOP_VECE(op), t);
1690 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1691 }
1692 return false;
1693}
1694
1695static bool fold_dup2(OptContext *ctx, TCGOp *op)
1696{
1697 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1698 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1699 arg_info(op->args[2])->val);
1700 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1701 }
1702
1703 if (args_are_copies(op->args[1], op->args[2])) {
1704 op->opc = INDEX_op_dup_vec;
1705 TCGOP_VECE(op) = MO_32;
1706 }
1707 return false;
1708}
1709
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001710static bool fold_eqv(OptContext *ctx, TCGOp *op)
1711{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001712 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07001713 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001714 fold_xi_to_not(ctx, op, 0)) {
1715 return true;
1716 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001717
1718 ctx->s_mask = arg_info(op->args[1])->s_mask
1719 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07001720 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001721}
1722
Richard Hendersonb6617c82021-08-24 10:44:53 -07001723static bool fold_extract(OptContext *ctx, TCGOp *op)
1724{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001725 uint64_t z_mask_old, z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001726 int pos = op->args[2];
1727 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07001728
Richard Hendersonb6617c82021-08-24 10:44:53 -07001729 if (arg_is_const(op->args[1])) {
1730 uint64_t t;
1731
1732 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001733 t = extract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001734 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1735 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001736
1737 z_mask_old = arg_info(op->args[1])->z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001738 z_mask = extract64(z_mask_old, pos, len);
Richard Henderson045ace32024-12-19 10:33:51 -08001739 if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) {
1740 return true;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001741 }
1742 ctx->z_mask = z_mask;
1743
1744 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07001745}
1746
Richard Hendersondcd08992021-08-24 10:41:39 -07001747static bool fold_extract2(OptContext *ctx, TCGOp *op)
1748{
1749 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1750 uint64_t v1 = arg_info(op->args[1])->val;
1751 uint64_t v2 = arg_info(op->args[2])->val;
1752 int shr = op->args[3];
1753
1754 if (op->opc == INDEX_op_extract2_i64) {
1755 v1 >>= shr;
1756 v2 <<= 64 - shr;
1757 } else {
1758 v1 = (uint32_t)v1 >> shr;
Richard Henderson225bec02021-11-09 23:17:59 +01001759 v2 = (uint64_t)((int32_t)v2 << (32 - shr));
Richard Hendersondcd08992021-08-24 10:41:39 -07001760 }
1761 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1762 }
1763 return false;
1764}
1765
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001766static bool fold_exts(OptContext *ctx, TCGOp *op)
1767{
Richard Henderson57fe5c62021-08-26 12:04:46 -07001768 uint64_t s_mask_old, s_mask, z_mask, sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001769 bool type_change = false;
1770
1771 if (fold_const1(ctx, op)) {
1772 return true;
1773 }
1774
Richard Henderson57fe5c62021-08-26 12:04:46 -07001775 z_mask = arg_info(op->args[1])->z_mask;
1776 s_mask = arg_info(op->args[1])->s_mask;
1777 s_mask_old = s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001778
1779 switch (op->opc) {
1780 CASE_OP_32_64(ext8s):
1781 sign = INT8_MIN;
1782 z_mask = (uint8_t)z_mask;
1783 break;
1784 CASE_OP_32_64(ext16s):
1785 sign = INT16_MIN;
1786 z_mask = (uint16_t)z_mask;
1787 break;
1788 case INDEX_op_ext_i32_i64:
1789 type_change = true;
1790 QEMU_FALLTHROUGH;
1791 case INDEX_op_ext32s_i64:
1792 sign = INT32_MIN;
1793 z_mask = (uint32_t)z_mask;
1794 break;
1795 default:
1796 g_assert_not_reached();
1797 }
1798
1799 if (z_mask & sign) {
1800 z_mask |= sign;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001801 }
Richard Henderson57fe5c62021-08-26 12:04:46 -07001802 s_mask |= sign << 1;
1803
Richard Hendersonfae450b2021-08-25 22:42:19 -07001804 ctx->z_mask = z_mask;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001805 ctx->s_mask = s_mask;
Richard Henderson6d70ddc2024-12-21 21:08:10 -08001806 if (0 && !type_change && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) {
Richard Henderson045ace32024-12-19 10:33:51 -08001807 return true;
Richard Henderson57fe5c62021-08-26 12:04:46 -07001808 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07001809
1810 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001811}
1812
1813static bool fold_extu(OptContext *ctx, TCGOp *op)
1814{
Richard Hendersonfae450b2021-08-25 22:42:19 -07001815 uint64_t z_mask_old, z_mask;
1816 bool type_change = false;
1817
1818 if (fold_const1(ctx, op)) {
1819 return true;
1820 }
1821
1822 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1823
1824 switch (op->opc) {
1825 CASE_OP_32_64(ext8u):
1826 z_mask = (uint8_t)z_mask;
1827 break;
1828 CASE_OP_32_64(ext16u):
1829 z_mask = (uint16_t)z_mask;
1830 break;
1831 case INDEX_op_extrl_i64_i32:
1832 case INDEX_op_extu_i32_i64:
1833 type_change = true;
1834 QEMU_FALLTHROUGH;
1835 case INDEX_op_ext32u_i64:
1836 z_mask = (uint32_t)z_mask;
1837 break;
1838 case INDEX_op_extrh_i64_i32:
1839 type_change = true;
1840 z_mask >>= 32;
1841 break;
1842 default:
1843 g_assert_not_reached();
1844 }
1845
1846 ctx->z_mask = z_mask;
Richard Henderson045ace32024-12-19 10:33:51 -08001847 if (!type_change && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) {
1848 return true;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001849 }
1850 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001851}
1852
Richard Henderson3eefdf22021-08-25 11:06:43 -07001853static bool fold_mb(OptContext *ctx, TCGOp *op)
1854{
1855 /* Eliminate duplicate and redundant fence instructions. */
1856 if (ctx->prev_mb) {
1857 /*
1858 * Merge two barriers of the same type into one,
1859 * or a weaker barrier into a stronger one,
1860 * or two weaker barriers into a stronger one.
1861 * mb X; mb Y => mb X|Y
1862 * mb; strl => mb; st
1863 * ldaq; mb => ld; mb
1864 * ldaq; strl => ld; mb; st
1865 * Other combinations are also merged into a strong
1866 * barrier. This is stricter than specified but for
1867 * the purposes of TCG is better than not optimizing.
1868 */
1869 ctx->prev_mb->args[0] |= op->args[0];
1870 tcg_op_remove(ctx->tcg, op);
1871 } else {
1872 ctx->prev_mb = op;
1873 }
1874 return true;
1875}
1876
Richard Henderson2cfac7f2021-08-25 13:05:43 -07001877static bool fold_mov(OptContext *ctx, TCGOp *op)
1878{
1879 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1880}
1881
Richard Henderson0c310a32021-08-24 10:37:24 -07001882static bool fold_movcond(OptContext *ctx, TCGOp *op)
1883{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001884 int i;
Richard Henderson0c310a32021-08-24 10:37:24 -07001885
Richard Henderson141125e2024-09-06 21:00:10 -07001886 /* If true and false values are the same, eliminate the cmp. */
1887 if (args_are_copies(op->args[3], op->args[4])) {
1888 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
1889 }
1890
Richard Henderson7a2f7082021-08-26 07:06:39 -07001891 /*
1892 * Canonicalize the "false" input reg to match the destination reg so
1893 * that the tcg backend can implement a "move if true" operation.
1894 */
1895 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
Richard Henderson246c4b72023-10-24 16:36:50 -07001896 op->args[5] = tcg_invert_cond(op->args[5]);
Richard Henderson7a2f7082021-08-26 07:06:39 -07001897 }
1898
Richard Hendersonfb04ab72024-01-10 18:21:58 +11001899 i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1],
Richard Henderson246c4b72023-10-24 16:36:50 -07001900 &op->args[2], &op->args[5]);
Richard Henderson0c310a32021-08-24 10:37:24 -07001901 if (i >= 0) {
1902 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1903 }
1904
Richard Hendersonfae450b2021-08-25 22:42:19 -07001905 ctx->z_mask = arg_info(op->args[3])->z_mask
1906 | arg_info(op->args[4])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07001907 ctx->s_mask = arg_info(op->args[3])->s_mask
1908 & arg_info(op->args[4])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07001909
Richard Henderson0c310a32021-08-24 10:37:24 -07001910 if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
1911 uint64_t tv = arg_info(op->args[3])->val;
1912 uint64_t fv = arg_info(op->args[4])->val;
Richard Henderson36355022023-08-04 23:24:04 +00001913 TCGOpcode opc, negopc = 0;
Richard Henderson246c4b72023-10-24 16:36:50 -07001914 TCGCond cond = op->args[5];
Richard Henderson0c310a32021-08-24 10:37:24 -07001915
Richard Henderson67f84c92021-08-25 08:00:20 -07001916 switch (ctx->type) {
1917 case TCG_TYPE_I32:
1918 opc = INDEX_op_setcond_i32;
Richard Henderson36355022023-08-04 23:24:04 +00001919 if (TCG_TARGET_HAS_negsetcond_i32) {
1920 negopc = INDEX_op_negsetcond_i32;
1921 }
1922 tv = (int32_t)tv;
1923 fv = (int32_t)fv;
Richard Henderson67f84c92021-08-25 08:00:20 -07001924 break;
1925 case TCG_TYPE_I64:
1926 opc = INDEX_op_setcond_i64;
Richard Henderson36355022023-08-04 23:24:04 +00001927 if (TCG_TARGET_HAS_negsetcond_i64) {
1928 negopc = INDEX_op_negsetcond_i64;
1929 }
Richard Henderson67f84c92021-08-25 08:00:20 -07001930 break;
1931 default:
1932 g_assert_not_reached();
1933 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001934
1935 if (tv == 1 && fv == 0) {
1936 op->opc = opc;
1937 op->args[3] = cond;
1938 } else if (fv == 1 && tv == 0) {
1939 op->opc = opc;
1940 op->args[3] = tcg_invert_cond(cond);
Richard Henderson36355022023-08-04 23:24:04 +00001941 } else if (negopc) {
1942 if (tv == -1 && fv == 0) {
1943 op->opc = negopc;
1944 op->args[3] = cond;
1945 } else if (fv == -1 && tv == 0) {
1946 op->opc = negopc;
1947 op->args[3] = tcg_invert_cond(cond);
1948 }
Richard Henderson0c310a32021-08-24 10:37:24 -07001949 }
1950 }
1951 return false;
1952}
1953
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001954static bool fold_mul(OptContext *ctx, TCGOp *op)
1955{
Richard Hendersone8679952021-08-25 13:19:52 -07001956 if (fold_const2(ctx, op) ||
Richard Henderson5b5cf472021-10-25 11:19:14 -07001957 fold_xi_to_i(ctx, op, 0) ||
1958 fold_xi_to_x(ctx, op, 1)) {
Richard Hendersone8679952021-08-25 13:19:52 -07001959 return true;
1960 }
1961 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001962}
1963
1964static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
1965{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001966 if (fold_const2_commutative(ctx, op) ||
Richard Hendersone8679952021-08-25 13:19:52 -07001967 fold_xi_to_i(ctx, op, 0)) {
1968 return true;
1969 }
1970 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07001971}
1972
Richard Henderson407112b2021-08-26 06:33:04 -07001973static bool fold_multiply2(OptContext *ctx, TCGOp *op)
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001974{
Richard Henderson7a2f7082021-08-26 07:06:39 -07001975 swap_commutative(op->args[0], &op->args[2], &op->args[3]);
1976
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001977 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
Richard Henderson407112b2021-08-26 06:33:04 -07001978 uint64_t a = arg_info(op->args[2])->val;
1979 uint64_t b = arg_info(op->args[3])->val;
1980 uint64_t h, l;
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07001981 TCGArg rl, rh;
Richard Henderson407112b2021-08-26 06:33:04 -07001982 TCGOp *op2;
1983
1984 switch (op->opc) {
1985 case INDEX_op_mulu2_i32:
1986 l = (uint64_t)(uint32_t)a * (uint32_t)b;
1987 h = (int32_t)(l >> 32);
1988 l = (int32_t)l;
1989 break;
1990 case INDEX_op_muls2_i32:
1991 l = (int64_t)(int32_t)a * (int32_t)b;
1992 h = l >> 32;
1993 l = (int32_t)l;
1994 break;
1995 case INDEX_op_mulu2_i64:
1996 mulu64(&l, &h, a, b);
1997 break;
1998 case INDEX_op_muls2_i64:
1999 muls64(&l, &h, a, b);
2000 break;
2001 default:
2002 g_assert_not_reached();
2003 }
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07002004
2005 rl = op->args[0];
2006 rh = op->args[1];
Richard Henderson407112b2021-08-26 06:33:04 -07002007
2008 /* The proper opcode is supplied by tcg_opt_gen_mov. */
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01002009 op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
Richard Henderson407112b2021-08-26 06:33:04 -07002010
2011 tcg_opt_gen_movi(ctx, op, rl, l);
2012 tcg_opt_gen_movi(ctx, op2, rh, h);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07002013 return true;
2014 }
2015 return false;
2016}
2017
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002018static bool fold_nand(OptContext *ctx, TCGOp *op)
2019{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002020 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002021 fold_xi_to_not(ctx, op, -1)) {
2022 return true;
2023 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002024
2025 ctx->s_mask = arg_info(op->args[1])->s_mask
2026 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002027 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002028}
2029
Richard Hendersone25fe882024-04-04 20:53:50 +00002030static bool fold_neg_no_const(OptContext *ctx, TCGOp *op)
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002031{
Richard Hendersonfae450b2021-08-25 22:42:19 -07002032 /* Set to 1 all bits to the left of the rightmost. */
Richard Hendersone25fe882024-04-04 20:53:50 +00002033 uint64_t z_mask = arg_info(op->args[1])->z_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002034 ctx->z_mask = -(z_mask & -z_mask);
2035
Richard Henderson9caca882021-08-24 13:30:32 -07002036 /*
2037 * Because of fold_sub_to_neg, we want to always return true,
2038 * via finish_folding.
2039 */
2040 finish_folding(ctx, op);
2041 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002042}
2043
Richard Hendersone25fe882024-04-04 20:53:50 +00002044static bool fold_neg(OptContext *ctx, TCGOp *op)
2045{
2046 return fold_const1(ctx, op) || fold_neg_no_const(ctx, op);
2047}
2048
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002049static bool fold_nor(OptContext *ctx, TCGOp *op)
2050{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002051 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002052 fold_xi_to_not(ctx, op, 0)) {
2053 return true;
2054 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002055
2056 ctx->s_mask = arg_info(op->args[1])->s_mask
2057 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002058 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002059}
2060
2061static bool fold_not(OptContext *ctx, TCGOp *op)
2062{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002063 if (fold_const1(ctx, op)) {
2064 return true;
2065 }
2066
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002067 ctx->s_mask = arg_info(op->args[1])->s_mask;
2068
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002069 /* Because of fold_to_not, we want to always return true, via finish. */
2070 finish_folding(ctx, op);
2071 return true;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002072}
2073
2074static bool fold_or(OptContext *ctx, TCGOp *op)
2075{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002076 if (fold_const2_commutative(ctx, op) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002077 fold_xi_to_x(ctx, op, 0) ||
Richard Hendersonca7bb042021-08-25 13:14:21 -07002078 fold_xx_to_x(ctx, op)) {
2079 return true;
2080 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002081
2082 ctx->z_mask = arg_info(op->args[1])->z_mask
2083 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002084 ctx->s_mask = arg_info(op->args[1])->s_mask
2085 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002086 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002087}
2088
2089static bool fold_orc(OptContext *ctx, TCGOp *op)
2090{
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002091 if (fold_const2(ctx, op) ||
Richard Henderson4e858d92021-08-26 07:31:13 -07002092 fold_xx_to_i(ctx, op, -1) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002093 fold_xi_to_x(ctx, op, -1) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002094 fold_ix_to_not(ctx, op, 0)) {
2095 return true;
2096 }
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002097
2098 ctx->s_mask = arg_info(op->args[1])->s_mask
2099 & arg_info(op->args[2])->s_mask;
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002100 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002101}
2102
Richard Henderson3eefdf22021-08-25 11:06:43 -07002103static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
2104{
Richard Hendersonfae450b2021-08-25 22:42:19 -07002105 const TCGOpDef *def = &tcg_op_defs[op->opc];
2106 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
2107 MemOp mop = get_memop(oi);
2108 int width = 8 * memop_size(mop);
2109
Richard Henderson57fe5c62021-08-26 12:04:46 -07002110 if (width < 64) {
Richard Henderson75c3bf32024-12-19 10:50:40 -08002111 if (mop & MO_SIGN) {
2112 ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width);
2113 } else {
Richard Henderson57fe5c62021-08-26 12:04:46 -07002114 ctx->z_mask = MAKE_64BIT_MASK(0, width);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002115 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002116 }
2117
Richard Henderson3eefdf22021-08-25 11:06:43 -07002118 /* Opcodes that touch guest memory stop the mb optimization. */
2119 ctx->prev_mb = NULL;
2120 return false;
2121}
2122
2123static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
2124{
2125 /* Opcodes that touch guest memory stop the mb optimization. */
2126 ctx->prev_mb = NULL;
2127 return false;
2128}
2129
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002130static bool fold_remainder(OptContext *ctx, TCGOp *op)
2131{
Richard Henderson267c17e2021-10-25 11:30:33 -07002132 if (fold_const2(ctx, op) ||
2133 fold_xx_to_i(ctx, op, 0)) {
2134 return true;
2135 }
2136 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002137}
2138
Richard Henderson8d65cda2024-03-26 16:00:40 -10002139static bool fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg)
2140{
2141 uint64_t a_zmask, b_val;
2142 TCGCond cond;
2143
2144 if (!arg_is_const(op->args[2])) {
2145 return false;
2146 }
2147
2148 a_zmask = arg_info(op->args[1])->z_mask;
2149 b_val = arg_info(op->args[2])->val;
2150 cond = op->args[3];
2151
2152 if (ctx->type == TCG_TYPE_I32) {
2153 a_zmask = (uint32_t)a_zmask;
2154 b_val = (uint32_t)b_val;
2155 }
2156
2157 /*
2158 * A with only low bits set vs B with high bits set means that A < B.
2159 */
2160 if (a_zmask < b_val) {
2161 bool inv = false;
2162
2163 switch (cond) {
2164 case TCG_COND_NE:
2165 case TCG_COND_LEU:
2166 case TCG_COND_LTU:
2167 inv = true;
2168 /* fall through */
2169 case TCG_COND_GTU:
2170 case TCG_COND_GEU:
2171 case TCG_COND_EQ:
2172 return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv);
2173 default:
2174 break;
2175 }
2176 }
2177
2178 /*
2179 * A with only lsb set is already boolean.
2180 */
2181 if (a_zmask <= 1) {
2182 bool convert = false;
2183 bool inv = false;
2184
2185 switch (cond) {
2186 case TCG_COND_EQ:
2187 inv = true;
2188 /* fall through */
2189 case TCG_COND_NE:
2190 convert = (b_val == 0);
2191 break;
2192 case TCG_COND_LTU:
2193 case TCG_COND_TSTEQ:
2194 inv = true;
2195 /* fall through */
2196 case TCG_COND_GEU:
2197 case TCG_COND_TSTNE:
2198 convert = (b_val == 1);
2199 break;
2200 default:
2201 break;
2202 }
2203 if (convert) {
2204 TCGOpcode add_opc, xor_opc, neg_opc;
2205
2206 if (!inv && !neg) {
2207 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
2208 }
2209
2210 switch (ctx->type) {
2211 case TCG_TYPE_I32:
2212 add_opc = INDEX_op_add_i32;
2213 neg_opc = INDEX_op_neg_i32;
2214 xor_opc = INDEX_op_xor_i32;
2215 break;
2216 case TCG_TYPE_I64:
2217 add_opc = INDEX_op_add_i64;
2218 neg_opc = INDEX_op_neg_i64;
2219 xor_opc = INDEX_op_xor_i64;
2220 break;
2221 default:
2222 g_assert_not_reached();
2223 }
2224
2225 if (!inv) {
2226 op->opc = neg_opc;
2227 } else if (neg) {
2228 op->opc = add_opc;
2229 op->args[2] = arg_new_constant(ctx, -1);
2230 } else {
2231 op->opc = xor_opc;
2232 op->args[2] = arg_new_constant(ctx, 1);
2233 }
2234 return false;
2235 }
2236 }
2237
2238 return false;
2239}
2240
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002241static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg)
2242{
Paolo Bonziniff202812024-02-28 12:06:41 +01002243 TCGOpcode and_opc, sub_opc, xor_opc, neg_opc, shr_opc;
2244 TCGOpcode uext_opc = 0, sext_opc = 0;
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002245 TCGCond cond = op->args[3];
2246 TCGArg ret, src1, src2;
2247 TCGOp *op2;
2248 uint64_t val;
2249 int sh;
2250 bool inv;
2251
2252 if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) {
2253 return;
2254 }
2255
2256 src2 = op->args[2];
2257 val = arg_info(src2)->val;
2258 if (!is_power_of_2(val)) {
2259 return;
2260 }
2261 sh = ctz64(val);
2262
2263 switch (ctx->type) {
2264 case TCG_TYPE_I32:
2265 and_opc = INDEX_op_and_i32;
2266 sub_opc = INDEX_op_sub_i32;
2267 xor_opc = INDEX_op_xor_i32;
2268 shr_opc = INDEX_op_shr_i32;
2269 neg_opc = INDEX_op_neg_i32;
2270 if (TCG_TARGET_extract_i32_valid(sh, 1)) {
2271 uext_opc = TCG_TARGET_HAS_extract_i32 ? INDEX_op_extract_i32 : 0;
2272 sext_opc = TCG_TARGET_HAS_sextract_i32 ? INDEX_op_sextract_i32 : 0;
2273 }
2274 break;
2275 case TCG_TYPE_I64:
2276 and_opc = INDEX_op_and_i64;
2277 sub_opc = INDEX_op_sub_i64;
2278 xor_opc = INDEX_op_xor_i64;
2279 shr_opc = INDEX_op_shr_i64;
2280 neg_opc = INDEX_op_neg_i64;
2281 if (TCG_TARGET_extract_i64_valid(sh, 1)) {
2282 uext_opc = TCG_TARGET_HAS_extract_i64 ? INDEX_op_extract_i64 : 0;
2283 sext_opc = TCG_TARGET_HAS_sextract_i64 ? INDEX_op_sextract_i64 : 0;
2284 }
2285 break;
2286 default:
2287 g_assert_not_reached();
2288 }
2289
2290 ret = op->args[0];
2291 src1 = op->args[1];
2292 inv = cond == TCG_COND_TSTEQ;
2293
2294 if (sh && sext_opc && neg && !inv) {
2295 op->opc = sext_opc;
2296 op->args[1] = src1;
2297 op->args[2] = sh;
2298 op->args[3] = 1;
2299 return;
2300 } else if (sh && uext_opc) {
2301 op->opc = uext_opc;
2302 op->args[1] = src1;
2303 op->args[2] = sh;
2304 op->args[3] = 1;
2305 } else {
2306 if (sh) {
2307 op2 = tcg_op_insert_before(ctx->tcg, op, shr_opc, 3);
2308 op2->args[0] = ret;
2309 op2->args[1] = src1;
2310 op2->args[2] = arg_new_constant(ctx, sh);
2311 src1 = ret;
2312 }
2313 op->opc = and_opc;
2314 op->args[1] = src1;
2315 op->args[2] = arg_new_constant(ctx, 1);
2316 }
2317
2318 if (neg && inv) {
2319 op2 = tcg_op_insert_after(ctx->tcg, op, sub_opc, 3);
2320 op2->args[0] = ret;
2321 op2->args[1] = ret;
2322 op2->args[2] = arg_new_constant(ctx, 1);
2323 } else if (inv) {
2324 op2 = tcg_op_insert_after(ctx->tcg, op, xor_opc, 3);
2325 op2->args[0] = ret;
2326 op2->args[1] = ret;
2327 op2->args[2] = arg_new_constant(ctx, 1);
2328 } else if (neg) {
2329 op2 = tcg_op_insert_after(ctx->tcg, op, neg_opc, 2);
2330 op2->args[0] = ret;
2331 op2->args[1] = ret;
2332 }
2333}
2334
Richard Hendersonc63ff552021-08-24 09:35:30 -07002335static bool fold_setcond(OptContext *ctx, TCGOp *op)
2336{
Richard Hendersonfb04ab72024-01-10 18:21:58 +11002337 int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
Richard Henderson246c4b72023-10-24 16:36:50 -07002338 &op->args[2], &op->args[3]);
Richard Hendersonc63ff552021-08-24 09:35:30 -07002339 if (i >= 0) {
2340 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2341 }
Richard Henderson8d65cda2024-03-26 16:00:40 -10002342
2343 if (fold_setcond_zmask(ctx, op, false)) {
2344 return true;
2345 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002346 fold_setcond_tst_pow2(ctx, op, false);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002347
2348 ctx->z_mask = 1;
Richard Hendersonc63ff552021-08-24 09:35:30 -07002349 return false;
2350}
2351
Richard Henderson36355022023-08-04 23:24:04 +00002352static bool fold_negsetcond(OptContext *ctx, TCGOp *op)
2353{
Richard Hendersonfb04ab72024-01-10 18:21:58 +11002354 int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
Richard Henderson246c4b72023-10-24 16:36:50 -07002355 &op->args[2], &op->args[3]);
Richard Henderson36355022023-08-04 23:24:04 +00002356 if (i >= 0) {
2357 return tcg_opt_gen_movi(ctx, op, op->args[0], -i);
2358 }
Richard Henderson8d65cda2024-03-26 16:00:40 -10002359
2360 if (fold_setcond_zmask(ctx, op, true)) {
2361 return true;
2362 }
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002363 fold_setcond_tst_pow2(ctx, op, true);
Richard Henderson36355022023-08-04 23:24:04 +00002364
2365 /* Value is {0,-1} so all bits are repetitions of the sign. */
2366 ctx->s_mask = -1;
2367 return false;
2368}
2369
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002370static bool fold_setcond2(OptContext *ctx, TCGOp *op)
2371{
Richard Henderson7e64b112023-10-24 16:53:56 -07002372 TCGCond cond;
Richard Henderson7a2f7082021-08-26 07:06:39 -07002373 int i, inv = 0;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002374
Richard Hendersonfb04ab72024-01-10 18:21:58 +11002375 i = do_constant_folding_cond2(ctx, op, &op->args[1]);
Richard Henderson7e64b112023-10-24 16:53:56 -07002376 cond = op->args[5];
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002377 if (i >= 0) {
2378 goto do_setcond_const;
2379 }
2380
2381 switch (cond) {
2382 case TCG_COND_LT:
2383 case TCG_COND_GE:
2384 /*
2385 * Simplify LT/GE comparisons vs zero to a single compare
2386 * vs the high word of the input.
2387 */
Richard Henderson27cdb852023-10-23 11:38:00 -07002388 if (arg_is_const_val(op->args[3], 0) &&
2389 arg_is_const_val(op->args[4], 0)) {
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002390 goto do_setcond_high;
2391 }
2392 break;
2393
2394 case TCG_COND_NE:
2395 inv = 1;
2396 QEMU_FALLTHROUGH;
2397 case TCG_COND_EQ:
2398 /*
2399 * Simplify EQ/NE comparisons where one of the pairs
2400 * can be simplified.
2401 */
Richard Henderson67f84c92021-08-25 08:00:20 -07002402 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002403 op->args[3], cond);
2404 switch (i ^ inv) {
2405 case 0:
2406 goto do_setcond_const;
2407 case 1:
2408 goto do_setcond_high;
2409 }
2410
Richard Henderson67f84c92021-08-25 08:00:20 -07002411 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002412 op->args[4], cond);
2413 switch (i ^ inv) {
2414 case 0:
2415 goto do_setcond_const;
2416 case 1:
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002417 goto do_setcond_low;
2418 }
2419 break;
2420
2421 case TCG_COND_TSTEQ:
2422 case TCG_COND_TSTNE:
Richard Hendersona71d9df2024-06-30 19:46:23 -07002423 if (arg_is_const_val(op->args[3], 0)) {
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002424 goto do_setcond_high;
2425 }
2426 if (arg_is_const_val(op->args[4], 0)) {
2427 goto do_setcond_low;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002428 }
2429 break;
2430
2431 default:
2432 break;
2433
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002434 do_setcond_low:
2435 op->args[2] = op->args[3];
2436 op->args[3] = cond;
2437 op->opc = INDEX_op_setcond_i32;
2438 return fold_setcond(ctx, op);
2439
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002440 do_setcond_high:
2441 op->args[1] = op->args[2];
2442 op->args[2] = op->args[4];
2443 op->args[3] = cond;
2444 op->opc = INDEX_op_setcond_i32;
Richard Hendersonceb9ee02023-10-23 23:44:27 -07002445 return fold_setcond(ctx, op);
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002446 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002447
2448 ctx->z_mask = 1;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07002449 return false;
2450
2451 do_setcond_const:
2452 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
2453}
2454
Richard Henderson1f106542024-09-06 12:22:41 -07002455static bool fold_cmp_vec(OptContext *ctx, TCGOp *op)
2456{
2457 /* Canonicalize the comparison to put immediate second. */
2458 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
2459 op->args[3] = tcg_swap_cond(op->args[3]);
2460 }
2461 return false;
2462}
2463
2464static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op)
2465{
2466 /* If true and false values are the same, eliminate the cmp. */
2467 if (args_are_copies(op->args[3], op->args[4])) {
2468 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
2469 }
2470
2471 /* Canonicalize the comparison to put immediate second. */
2472 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
2473 op->args[5] = tcg_swap_cond(op->args[5]);
2474 }
2475 /*
2476 * Canonicalize the "false" input reg to match the destination,
2477 * so that the tcg backend can implement "move if true".
2478 */
2479 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
2480 op->args[5] = tcg_invert_cond(op->args[5]);
2481 }
2482 return false;
2483}
2484
Richard Hendersonb6617c82021-08-24 10:44:53 -07002485static bool fold_sextract(OptContext *ctx, TCGOp *op)
2486{
Richard Henderson57fe5c62021-08-26 12:04:46 -07002487 uint64_t z_mask, s_mask, s_mask_old;
2488 int pos = op->args[2];
2489 int len = op->args[3];
Richard Hendersonfae450b2021-08-25 22:42:19 -07002490
Richard Hendersonb6617c82021-08-24 10:44:53 -07002491 if (arg_is_const(op->args[1])) {
2492 uint64_t t;
2493
2494 t = arg_info(op->args[1])->val;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002495 t = sextract64(t, pos, len);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002496 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
2497 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002498
Richard Henderson57fe5c62021-08-26 12:04:46 -07002499 z_mask = arg_info(op->args[1])->z_mask;
2500 z_mask = sextract64(z_mask, pos, len);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002501 ctx->z_mask = z_mask;
2502
Richard Henderson57fe5c62021-08-26 12:04:46 -07002503 s_mask_old = arg_info(op->args[1])->s_mask;
2504 s_mask = sextract64(s_mask_old, pos, len);
2505 s_mask |= MAKE_64BIT_MASK(len, 64 - len);
2506 ctx->s_mask = s_mask;
2507
Richard Henderson6d70ddc2024-12-21 21:08:10 -08002508 if (0 && pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) {
Richard Henderson045ace32024-12-19 10:33:51 -08002509 return true;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002510 }
2511
Richard Hendersonfae450b2021-08-25 22:42:19 -07002512 return fold_masks(ctx, op);
Richard Hendersonb6617c82021-08-24 10:44:53 -07002513}
2514
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002515static bool fold_shift(OptContext *ctx, TCGOp *op)
2516{
Richard Henderson93a967f2021-08-26 13:24:59 -07002517 uint64_t s_mask, z_mask, sign;
2518
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002519 if (fold_const2(ctx, op) ||
Richard Hendersonda48e272021-08-25 20:42:04 -07002520 fold_ix_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002521 fold_xi_to_x(ctx, op, 0)) {
2522 return true;
2523 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002524
Richard Henderson93a967f2021-08-26 13:24:59 -07002525 s_mask = arg_info(op->args[1])->s_mask;
2526 z_mask = arg_info(op->args[1])->z_mask;
2527
Richard Hendersonfae450b2021-08-25 22:42:19 -07002528 if (arg_is_const(op->args[2])) {
Richard Henderson93a967f2021-08-26 13:24:59 -07002529 int sh = arg_info(op->args[2])->val;
2530
2531 ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
2532
2533 s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
Richard Henderson93a967f2021-08-26 13:24:59 -07002534
Richard Hendersonfae450b2021-08-25 22:42:19 -07002535 return fold_masks(ctx, op);
2536 }
Richard Henderson93a967f2021-08-26 13:24:59 -07002537
2538 switch (op->opc) {
2539 CASE_OP_32_64(sar):
2540 /*
2541 * Arithmetic right shift will not reduce the number of
2542 * input sign repetitions.
2543 */
2544 ctx->s_mask = s_mask;
2545 break;
2546 CASE_OP_32_64(shr):
2547 /*
2548 * If the sign bit is known zero, then logical right shift
2549 * will not reduced the number of input sign repetitions.
2550 */
2551 sign = (s_mask & -s_mask) >> 1;
Richard Henderson2911e9b2024-03-26 11:21:38 -10002552 if (sign && !(z_mask & sign)) {
Richard Henderson93a967f2021-08-26 13:24:59 -07002553 ctx->s_mask = s_mask;
2554 }
2555 break;
2556 default:
2557 break;
2558 }
2559
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002560 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002561}
2562
Richard Henderson9caca882021-08-24 13:30:32 -07002563static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
2564{
2565 TCGOpcode neg_op;
2566 bool have_neg;
2567
2568 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
2569 return false;
2570 }
2571
2572 switch (ctx->type) {
2573 case TCG_TYPE_I32:
2574 neg_op = INDEX_op_neg_i32;
Richard Hendersonb701f192023-10-25 21:14:04 -07002575 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002576 break;
2577 case TCG_TYPE_I64:
2578 neg_op = INDEX_op_neg_i64;
Richard Hendersonb701f192023-10-25 21:14:04 -07002579 have_neg = true;
Richard Henderson9caca882021-08-24 13:30:32 -07002580 break;
2581 case TCG_TYPE_V64:
2582 case TCG_TYPE_V128:
2583 case TCG_TYPE_V256:
2584 neg_op = INDEX_op_neg_vec;
2585 have_neg = (TCG_TARGET_HAS_neg_vec &&
2586 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
2587 break;
2588 default:
2589 g_assert_not_reached();
2590 }
2591 if (have_neg) {
2592 op->opc = neg_op;
2593 op->args[1] = op->args[2];
Richard Hendersone25fe882024-04-04 20:53:50 +00002594 return fold_neg_no_const(ctx, op);
Richard Henderson9caca882021-08-24 13:30:32 -07002595 }
2596 return false;
2597}
2598
Richard Hendersonc578ff12021-12-16 06:07:25 -08002599/* We cannot as yet do_constant_folding with vectors. */
2600static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002601{
Richard Hendersonc578ff12021-12-16 06:07:25 -08002602 if (fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002603 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson9caca882021-08-24 13:30:32 -07002604 fold_sub_to_neg(ctx, op)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002605 return true;
2606 }
2607 return false;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002608}
2609
Richard Hendersonc578ff12021-12-16 06:07:25 -08002610static bool fold_sub(OptContext *ctx, TCGOp *op)
2611{
Richard Henderson6334a962023-10-25 18:39:43 -07002612 if (fold_const2(ctx, op) || fold_sub_vec(ctx, op)) {
2613 return true;
2614 }
2615
2616 /* Fold sub r,x,i to add r,x,-i */
2617 if (arg_is_const(op->args[2])) {
2618 uint64_t val = arg_info(op->args[2])->val;
2619
2620 op->opc = (ctx->type == TCG_TYPE_I32
2621 ? INDEX_op_add_i32 : INDEX_op_add_i64);
2622 op->args[2] = arg_new_constant(ctx, -val);
2623 }
2624 return false;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002625}
2626
Richard Henderson9531c072021-08-26 06:51:39 -07002627static bool fold_sub2(OptContext *ctx, TCGOp *op)
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002628{
Richard Henderson9531c072021-08-26 06:51:39 -07002629 return fold_addsub2(ctx, op, false);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002630}
2631
Richard Hendersonfae450b2021-08-25 22:42:19 -07002632static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
2633{
2634 /* We can't do any folding with a load, but we can record bits. */
2635 switch (op->opc) {
Richard Henderson57fe5c62021-08-26 12:04:46 -07002636 CASE_OP_32_64(ld8s):
2637 ctx->s_mask = MAKE_64BIT_MASK(8, 56);
2638 break;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002639 CASE_OP_32_64(ld8u):
2640 ctx->z_mask = MAKE_64BIT_MASK(0, 8);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002641 break;
2642 CASE_OP_32_64(ld16s):
2643 ctx->s_mask = MAKE_64BIT_MASK(16, 48);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002644 break;
2645 CASE_OP_32_64(ld16u):
2646 ctx->z_mask = MAKE_64BIT_MASK(0, 16);
Richard Henderson57fe5c62021-08-26 12:04:46 -07002647 break;
2648 case INDEX_op_ld32s_i64:
2649 ctx->s_mask = MAKE_64BIT_MASK(32, 32);
Richard Hendersonfae450b2021-08-25 22:42:19 -07002650 break;
2651 case INDEX_op_ld32u_i64:
2652 ctx->z_mask = MAKE_64BIT_MASK(0, 32);
2653 break;
2654 default:
2655 g_assert_not_reached();
2656 }
2657 return false;
2658}
2659
Richard Hendersonab84dc32023-08-23 23:04:24 -07002660static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op)
2661{
2662 TCGTemp *dst, *src;
2663 intptr_t ofs;
2664 TCGType type;
2665
2666 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2667 return false;
2668 }
2669
2670 type = ctx->type;
2671 ofs = op->args[2];
2672 dst = arg_temp(op->args[0]);
2673 src = find_mem_copy_for(ctx, type, ofs);
2674 if (src && src->base_type == type) {
2675 return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src));
2676 }
2677
2678 reset_ts(ctx, dst);
2679 record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1);
2680 return true;
2681}
2682
2683static bool fold_tcg_st(OptContext *ctx, TCGOp *op)
2684{
2685 intptr_t ofs = op->args[2];
2686 intptr_t lm1;
2687
2688 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2689 remove_mem_copy_all(ctx);
2690 return false;
2691 }
2692
2693 switch (op->opc) {
2694 CASE_OP_32_64(st8):
2695 lm1 = 0;
2696 break;
2697 CASE_OP_32_64(st16):
2698 lm1 = 1;
2699 break;
2700 case INDEX_op_st32_i64:
2701 case INDEX_op_st_i32:
2702 lm1 = 3;
2703 break;
2704 case INDEX_op_st_i64:
2705 lm1 = 7;
2706 break;
2707 case INDEX_op_st_vec:
2708 lm1 = tcg_type_size(ctx->type) - 1;
2709 break;
2710 default:
2711 g_assert_not_reached();
2712 }
2713 remove_mem_copy_in(ctx, ofs, ofs + lm1);
2714 return false;
2715}
2716
2717static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op)
2718{
2719 TCGTemp *src;
2720 intptr_t ofs, last;
2721 TCGType type;
2722
2723 if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
2724 fold_tcg_st(ctx, op);
2725 return false;
2726 }
2727
2728 src = arg_temp(op->args[0]);
2729 ofs = op->args[2];
2730 type = ctx->type;
Richard Henderson3eaadae2023-08-23 23:13:06 -07002731
2732 /*
2733 * Eliminate duplicate stores of a constant.
2734 * This happens frequently when the target ISA zero-extends.
2735 */
2736 if (ts_is_const(src)) {
2737 TCGTemp *prev = find_mem_copy_for(ctx, type, ofs);
2738 if (src == prev) {
2739 tcg_op_remove(ctx->tcg, op);
2740 return true;
2741 }
2742 }
2743
Richard Hendersonab84dc32023-08-23 23:04:24 -07002744 last = ofs + tcg_type_size(type) - 1;
2745 remove_mem_copy_in(ctx, ofs, last);
2746 record_mem_copy(ctx, type, src, ofs, last);
2747 return false;
2748}
2749
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002750static bool fold_xor(OptContext *ctx, TCGOp *op)
2751{
Richard Henderson7a2f7082021-08-26 07:06:39 -07002752 if (fold_const2_commutative(ctx, op) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002753 fold_xx_to_i(ctx, op, 0) ||
Richard Hendersona63ce0e2021-08-25 20:28:53 -07002754 fold_xi_to_x(ctx, op, 0) ||
Richard Henderson0e0a32b2021-08-24 13:18:01 -07002755 fold_xi_to_not(ctx, op, -1)) {
Richard Hendersoncbe42fb2021-08-25 13:02:00 -07002756 return true;
2757 }
Richard Hendersonfae450b2021-08-25 22:42:19 -07002758
2759 ctx->z_mask = arg_info(op->args[1])->z_mask
2760 | arg_info(op->args[2])->z_mask;
Richard Henderson3f2b1f82021-08-26 13:08:54 -07002761 ctx->s_mask = arg_info(op->args[1])->s_mask
2762 & arg_info(op->args[2])->s_mask;
Richard Hendersonfae450b2021-08-25 22:42:19 -07002763 return fold_masks(ctx, op);
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002764}
2765
Richard Hendersone58b9772024-09-06 22:30:01 -07002766static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op)
2767{
2768 /* If true and false values are the same, eliminate the cmp. */
2769 if (args_are_copies(op->args[2], op->args[3])) {
2770 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
2771 }
2772
2773 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
2774 uint64_t tv = arg_info(op->args[2])->val;
2775 uint64_t fv = arg_info(op->args[3])->val;
2776
2777 if (tv == -1 && fv == 0) {
2778 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
2779 }
2780 if (tv == 0 && fv == -1) {
2781 if (TCG_TARGET_HAS_not_vec) {
2782 op->opc = INDEX_op_not_vec;
2783 return fold_not(ctx, op);
2784 } else {
2785 op->opc = INDEX_op_xor_vec;
2786 op->args[2] = arg_new_constant(ctx, -1);
2787 return fold_xor(ctx, op);
2788 }
2789 }
2790 }
2791 if (arg_is_const(op->args[2])) {
2792 uint64_t tv = arg_info(op->args[2])->val;
2793 if (tv == -1) {
2794 op->opc = INDEX_op_or_vec;
2795 op->args[2] = op->args[3];
2796 return fold_or(ctx, op);
2797 }
2798 if (tv == 0 && TCG_TARGET_HAS_andc_vec) {
2799 op->opc = INDEX_op_andc_vec;
2800 op->args[2] = op->args[1];
2801 op->args[1] = op->args[3];
2802 return fold_andc(ctx, op);
2803 }
2804 }
2805 if (arg_is_const(op->args[3])) {
2806 uint64_t fv = arg_info(op->args[3])->val;
2807 if (fv == 0) {
2808 op->opc = INDEX_op_and_vec;
2809 return fold_and(ctx, op);
2810 }
2811 if (fv == -1 && TCG_TARGET_HAS_orc_vec) {
2812 op->opc = INDEX_op_orc_vec;
2813 op->args[2] = op->args[1];
2814 op->args[1] = op->args[3];
2815 return fold_orc(ctx, op);
2816 }
2817 }
2818 return false;
2819}
2820
Kirill Batuzov22613af2011-07-07 16:37:13 +04002821/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +02002822void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002823{
Richard Henderson5cf32be2021-08-24 08:17:08 -07002824 int nb_temps, i;
Richard Hendersond0ed5152021-08-24 07:38:39 -07002825 TCGOp *op, *op_next;
Richard Hendersondc849882021-08-24 07:13:45 -07002826 OptContext ctx = { .tcg = s };
Richard Henderson5d8f5362012-09-21 10:13:38 -07002827
Richard Hendersonab84dc32023-08-23 23:04:24 -07002828 QSIMPLEQ_INIT(&ctx.mem_free);
2829
Kirill Batuzov22613af2011-07-07 16:37:13 +04002830 /* Array VALS has an element for each temp.
2831 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02002832 If this temp is a copy of other ones then the other copies are
2833 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002834
2835 nb_temps = s->nb_temps;
Richard Henderson8f17a972020-03-30 19:52:02 -07002836 for (i = 0; i < nb_temps; ++i) {
2837 s->temps[i].state_ptr = NULL;
2838 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04002839
Richard Henderson15fa08f2017-11-02 15:19:14 +01002840 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002841 TCGOpcode opc = op->opc;
Richard Henderson5cf32be2021-08-24 08:17:08 -07002842 const TCGOpDef *def;
Richard Henderson404a1482021-08-24 11:08:21 -07002843 bool done = false;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002844
Richard Henderson5cf32be2021-08-24 08:17:08 -07002845 /* Calls are special. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002846 if (opc == INDEX_op_call) {
Richard Henderson5cf32be2021-08-24 08:17:08 -07002847 fold_call(&ctx, op);
2848 continue;
Richard Hendersoncf066672014-03-22 20:06:52 -07002849 }
Richard Henderson5cf32be2021-08-24 08:17:08 -07002850
2851 def = &tcg_op_defs[opc];
Richard Hendersonec5d4cb2021-08-24 08:20:27 -07002852 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
2853 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
Kirill Batuzov22613af2011-07-07 16:37:13 +04002854
Richard Henderson67f84c92021-08-25 08:00:20 -07002855 /* Pre-compute the type of the operation. */
2856 if (def->flags & TCG_OPF_VECTOR) {
2857 ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
2858 } else if (def->flags & TCG_OPF_64BIT) {
2859 ctx.type = TCG_TYPE_I64;
2860 } else {
2861 ctx.type = TCG_TYPE_I32;
2862 }
2863
Richard Henderson57fe5c62021-08-26 12:04:46 -07002864 /* Assume all bits affected, no bits known zero, no sign reps. */
Richard Hendersonfae450b2021-08-25 22:42:19 -07002865 ctx.z_mask = -1;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002866 ctx.s_mask = 0;
Paolo Bonzini633f6502013-01-11 15:42:53 -08002867
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002868 /*
2869 * Process each opcode.
2870 * Sorted alphabetically by opcode as much as possible.
2871 */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002872 switch (opc) {
Richard Hendersonc578ff12021-12-16 06:07:25 -08002873 CASE_OP_32_64(add):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002874 done = fold_add(&ctx, op);
2875 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08002876 case INDEX_op_add_vec:
2877 done = fold_add_vec(&ctx, op);
2878 break;
Richard Henderson9531c072021-08-26 06:51:39 -07002879 CASE_OP_32_64(add2):
2880 done = fold_add2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07002881 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002882 CASE_OP_32_64_VEC(and):
2883 done = fold_and(&ctx, op);
2884 break;
2885 CASE_OP_32_64_VEC(andc):
2886 done = fold_andc(&ctx, op);
2887 break;
Richard Henderson079b0802021-08-24 09:30:59 -07002888 CASE_OP_32_64(brcond):
2889 done = fold_brcond(&ctx, op);
2890 break;
Richard Henderson764d2ab2021-08-24 09:22:11 -07002891 case INDEX_op_brcond2_i32:
2892 done = fold_brcond2(&ctx, op);
2893 break;
Richard Henderson09bacdc2021-08-24 11:58:12 -07002894 CASE_OP_32_64(bswap16):
2895 CASE_OP_32_64(bswap32):
2896 case INDEX_op_bswap64_i64:
2897 done = fold_bswap(&ctx, op);
2898 break;
Richard Henderson30dd0bf2021-08-24 10:51:34 -07002899 CASE_OP_32_64(clz):
2900 CASE_OP_32_64(ctz):
2901 done = fold_count_zeros(&ctx, op);
2902 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002903 CASE_OP_32_64(ctpop):
2904 done = fold_ctpop(&ctx, op);
2905 break;
Richard Henderson1b1907b2021-08-24 10:47:04 -07002906 CASE_OP_32_64(deposit):
2907 done = fold_deposit(&ctx, op);
2908 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002909 CASE_OP_32_64(div):
2910 CASE_OP_32_64(divu):
2911 done = fold_divide(&ctx, op);
2912 break;
Richard Henderson8cdb3fc2021-08-24 12:06:33 -07002913 case INDEX_op_dup_vec:
2914 done = fold_dup(&ctx, op);
2915 break;
2916 case INDEX_op_dup2_vec:
2917 done = fold_dup2(&ctx, op);
2918 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002919 CASE_OP_32_64_VEC(eqv):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002920 done = fold_eqv(&ctx, op);
2921 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07002922 CASE_OP_32_64(extract):
2923 done = fold_extract(&ctx, op);
2924 break;
Richard Hendersondcd08992021-08-24 10:41:39 -07002925 CASE_OP_32_64(extract2):
2926 done = fold_extract2(&ctx, op);
2927 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002928 CASE_OP_32_64(ext8s):
2929 CASE_OP_32_64(ext16s):
2930 case INDEX_op_ext32s_i64:
2931 case INDEX_op_ext_i32_i64:
2932 done = fold_exts(&ctx, op);
2933 break;
2934 CASE_OP_32_64(ext8u):
2935 CASE_OP_32_64(ext16u):
2936 case INDEX_op_ext32u_i64:
2937 case INDEX_op_extu_i32_i64:
2938 case INDEX_op_extrl_i64_i32:
2939 case INDEX_op_extrh_i64_i32:
2940 done = fold_extu(&ctx, op);
2941 break;
Richard Henderson57fe5c62021-08-26 12:04:46 -07002942 CASE_OP_32_64(ld8s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002943 CASE_OP_32_64(ld8u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002944 CASE_OP_32_64(ld16s):
Richard Hendersonfae450b2021-08-25 22:42:19 -07002945 CASE_OP_32_64(ld16u):
Richard Henderson57fe5c62021-08-26 12:04:46 -07002946 case INDEX_op_ld32s_i64:
Richard Hendersonfae450b2021-08-25 22:42:19 -07002947 case INDEX_op_ld32u_i64:
2948 done = fold_tcg_ld(&ctx, op);
2949 break;
Richard Hendersonab84dc32023-08-23 23:04:24 -07002950 case INDEX_op_ld_i32:
2951 case INDEX_op_ld_i64:
2952 case INDEX_op_ld_vec:
2953 done = fold_tcg_ld_memcopy(&ctx, op);
2954 break;
2955 CASE_OP_32_64(st8):
2956 CASE_OP_32_64(st16):
2957 case INDEX_op_st32_i64:
2958 done = fold_tcg_st(&ctx, op);
2959 break;
2960 case INDEX_op_st_i32:
2961 case INDEX_op_st_i64:
2962 case INDEX_op_st_vec:
2963 done = fold_tcg_st_memcopy(&ctx, op);
2964 break;
Richard Henderson3eefdf22021-08-25 11:06:43 -07002965 case INDEX_op_mb:
2966 done = fold_mb(&ctx, op);
2967 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07002968 CASE_OP_32_64_VEC(mov):
2969 done = fold_mov(&ctx, op);
2970 break;
Richard Henderson0c310a32021-08-24 10:37:24 -07002971 CASE_OP_32_64(movcond):
2972 done = fold_movcond(&ctx, op);
2973 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002974 CASE_OP_32_64(mul):
2975 done = fold_mul(&ctx, op);
2976 break;
2977 CASE_OP_32_64(mulsh):
2978 CASE_OP_32_64(muluh):
2979 done = fold_mul_highpart(&ctx, op);
2980 break;
Richard Henderson407112b2021-08-26 06:33:04 -07002981 CASE_OP_32_64(muls2):
2982 CASE_OP_32_64(mulu2):
2983 done = fold_multiply2(&ctx, op);
Richard Henderson6b8ac0d2021-08-24 10:24:12 -07002984 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002985 CASE_OP_32_64_VEC(nand):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002986 done = fold_nand(&ctx, op);
2987 break;
2988 CASE_OP_32_64(neg):
2989 done = fold_neg(&ctx, op);
2990 break;
Richard Hendersoned523472021-12-16 11:17:46 -08002991 CASE_OP_32_64_VEC(nor):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07002992 done = fold_nor(&ctx, op);
2993 break;
2994 CASE_OP_32_64_VEC(not):
2995 done = fold_not(&ctx, op);
2996 break;
2997 CASE_OP_32_64_VEC(or):
2998 done = fold_or(&ctx, op);
2999 break;
3000 CASE_OP_32_64_VEC(orc):
3001 done = fold_orc(&ctx, op);
3002 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07003003 case INDEX_op_qemu_ld_a32_i32:
3004 case INDEX_op_qemu_ld_a64_i32:
3005 case INDEX_op_qemu_ld_a32_i64:
3006 case INDEX_op_qemu_ld_a64_i64:
3007 case INDEX_op_qemu_ld_a32_i128:
3008 case INDEX_op_qemu_ld_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07003009 done = fold_qemu_ld(&ctx, op);
3010 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07003011 case INDEX_op_qemu_st8_a32_i32:
3012 case INDEX_op_qemu_st8_a64_i32:
3013 case INDEX_op_qemu_st_a32_i32:
3014 case INDEX_op_qemu_st_a64_i32:
3015 case INDEX_op_qemu_st_a32_i64:
3016 case INDEX_op_qemu_st_a64_i64:
3017 case INDEX_op_qemu_st_a32_i128:
3018 case INDEX_op_qemu_st_a64_i128:
Richard Henderson3eefdf22021-08-25 11:06:43 -07003019 done = fold_qemu_st(&ctx, op);
3020 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07003021 CASE_OP_32_64(rem):
3022 CASE_OP_32_64(remu):
3023 done = fold_remainder(&ctx, op);
3024 break;
3025 CASE_OP_32_64(rotl):
3026 CASE_OP_32_64(rotr):
3027 CASE_OP_32_64(sar):
3028 CASE_OP_32_64(shl):
3029 CASE_OP_32_64(shr):
3030 done = fold_shift(&ctx, op);
3031 break;
Richard Hendersonc63ff552021-08-24 09:35:30 -07003032 CASE_OP_32_64(setcond):
3033 done = fold_setcond(&ctx, op);
3034 break;
Richard Henderson36355022023-08-04 23:24:04 +00003035 CASE_OP_32_64(negsetcond):
3036 done = fold_negsetcond(&ctx, op);
3037 break;
Richard Hendersonbc47b1a2021-08-24 09:09:35 -07003038 case INDEX_op_setcond2_i32:
3039 done = fold_setcond2(&ctx, op);
3040 break;
Richard Henderson1f106542024-09-06 12:22:41 -07003041 case INDEX_op_cmp_vec:
3042 done = fold_cmp_vec(&ctx, op);
3043 break;
3044 case INDEX_op_cmpsel_vec:
3045 done = fold_cmpsel_vec(&ctx, op);
3046 break;
Richard Hendersone58b9772024-09-06 22:30:01 -07003047 case INDEX_op_bitsel_vec:
3048 done = fold_bitsel_vec(&ctx, op);
3049 break;
Richard Hendersonb6617c82021-08-24 10:44:53 -07003050 CASE_OP_32_64(sextract):
3051 done = fold_sextract(&ctx, op);
3052 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08003053 CASE_OP_32_64(sub):
Richard Henderson2f9f08b2021-08-25 12:03:48 -07003054 done = fold_sub(&ctx, op);
3055 break;
Richard Hendersonc578ff12021-12-16 06:07:25 -08003056 case INDEX_op_sub_vec:
3057 done = fold_sub_vec(&ctx, op);
3058 break;
Richard Henderson9531c072021-08-26 06:51:39 -07003059 CASE_OP_32_64(sub2):
3060 done = fold_sub2(&ctx, op);
Richard Hendersone3f7dc22021-08-24 10:30:38 -07003061 break;
Richard Henderson2f9f08b2021-08-25 12:03:48 -07003062 CASE_OP_32_64_VEC(xor):
3063 done = fold_xor(&ctx, op);
Richard Hendersonb10f3832021-08-23 22:30:17 -07003064 break;
Richard Henderson15268552024-12-08 07:45:11 -06003065 case INDEX_op_set_label:
3066 case INDEX_op_br:
3067 case INDEX_op_exit_tb:
3068 case INDEX_op_goto_tb:
3069 case INDEX_op_goto_ptr:
3070 finish_ebb(&ctx);
3071 done = true;
3072 break;
Richard Henderson2cfac7f2021-08-25 13:05:43 -07003073 default:
3074 break;
Richard Hendersonb10f3832021-08-23 22:30:17 -07003075 }
3076
Richard Henderson404a1482021-08-24 11:08:21 -07003077 if (!done) {
3078 finish_folding(&ctx, op);
3079 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04003080 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04003081}