Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Optimizations for Tiny Code Generator for QEMU |
| 3 | * |
| 4 | * Copyright (c) 2010 Samsung Electronics. |
| 5 | * Contributed by Kirill Batuzov <batuzovk@ispras.ru> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
Peter Maydell | 757e725 | 2016-01-26 18:17:08 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 27 | #include "qemu/int128.h" |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 28 | #include "qemu/interval-tree.h" |
Richard Henderson | ad3d0e4 | 2023-03-28 18:17:24 -0700 | [diff] [blame] | 29 | #include "tcg/tcg-op-common.h" |
Richard Henderson | 9016390 | 2021-03-18 10:21:45 -0600 | [diff] [blame] | 30 | #include "tcg-internal.h" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 31 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 32 | #define CASE_OP_32_64(x) \ |
| 33 | glue(glue(case INDEX_op_, x), _i32): \ |
| 34 | glue(glue(case INDEX_op_, x), _i64) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 35 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 36 | #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 Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 41 | typedef struct MemCopyInfo { |
| 42 | IntervalTreeNode itree; |
| 43 | QSIMPLEQ_ENTRY (MemCopyInfo) next; |
| 44 | TCGTemp *ts; |
| 45 | TCGType type; |
| 46 | } MemCopyInfo; |
| 47 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 48 | typedef struct TempOptInfo { |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 49 | bool is_const; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 50 | TCGTemp *prev_copy; |
| 51 | TCGTemp *next_copy; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 52 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy; |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 53 | uint64_t val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 54 | uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */ |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 55 | uint64_t s_mask; /* mask bit is 1 if value bit matches msb */ |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 56 | } TempOptInfo; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 57 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 58 | typedef struct OptContext { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 59 | TCGContext *tcg; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 60 | TCGOp *prev_mb; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 61 | TCGTempSet temps_used; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 62 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 63 | IntervalTreeRoot mem_copy; |
| 64 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_free; |
| 65 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 66 | /* In flight values from optimization. */ |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 67 | uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */ |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 68 | uint64_t s_mask; /* mask bit is 1 if value bit matches msb */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 69 | TCGType type; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 70 | } OptContext; |
| 71 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 72 | static inline TempOptInfo *ts_info(TCGTemp *ts) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 73 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 74 | return ts->state_ptr; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 77 | static inline TempOptInfo *arg_info(TCGArg arg) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 78 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 79 | return ts_info(arg_temp(arg)); |
| 80 | } |
| 81 | |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 82 | static inline bool ti_is_const(TempOptInfo *ti) |
| 83 | { |
| 84 | return ti->is_const; |
| 85 | } |
| 86 | |
| 87 | static inline uint64_t ti_const_val(TempOptInfo *ti) |
| 88 | { |
| 89 | return ti->val; |
| 90 | } |
| 91 | |
| 92 | static 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 Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 97 | static inline bool ts_is_const(TCGTemp *ts) |
| 98 | { |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 99 | return ti_is_const(ts_info(ts)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 102 | static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val) |
| 103 | { |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 104 | return ti_is_const_val(ts_info(ts), val); |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 107 | static inline bool arg_is_const(TCGArg arg) |
| 108 | { |
| 109 | return ts_is_const(arg_temp(arg)); |
| 110 | } |
| 111 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 112 | static 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 Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 117 | static inline bool ts_is_copy(TCGTemp *ts) |
| 118 | { |
| 119 | return ts_info(ts)->next_copy != ts; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 122 | static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b) |
| 123 | { |
| 124 | return a->kind < b->kind ? b : a; |
| 125 | } |
| 126 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 127 | /* Initialize and activate a temporary. */ |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 128 | static void init_ts_info(OptContext *ctx, TCGTemp *ts) |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 129 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 130 | size_t idx = temp_idx(ts); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 131 | TempOptInfo *ti; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 132 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 133 | if (test_bit(idx, ctx->temps_used.l)) { |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 136 | set_bit(idx, ctx->temps_used.l); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 137 | |
| 138 | ti = ts->state_ptr; |
| 139 | if (ti == NULL) { |
| 140 | ti = tcg_malloc(sizeof(TempOptInfo)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 141 | ts->state_ptr = ti; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ti->next_copy = ts; |
| 145 | ti->prev_copy = ts; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 146 | QSIMPLEQ_INIT(&ti->mem_copy); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 147 | if (ts->kind == TEMP_CONST) { |
| 148 | ti->is_const = true; |
| 149 | ti->val = ts->val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 150 | ti->z_mask = ts->val; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 151 | ti->s_mask = INT64_MIN >> clrsb64(ts->val); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 152 | } else { |
| 153 | ti->is_const = false; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 154 | ti->z_mask = -1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 155 | ti->s_mask = 0; |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 159 | static 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 | |
| 165 | static 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 | |
| 171 | static 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 | |
| 181 | static 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 | |
| 192 | static 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 Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 198 | static TCGTemp *find_better_copy(TCGTemp *ts) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 199 | { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 200 | TCGTemp *i, *ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 201 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 202 | /* If this is already readonly, we can't do better. */ |
| 203 | if (temp_readonly(ts)) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 204 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 207 | ret = ts; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 208 | for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 209 | ret = cmp_better_copy(ret, i); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 210 | } |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 211 | return ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 214 | static 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. */ |
| 228 | static 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 | |
| 258 | static void reset_temp(OptContext *ctx, TCGArg arg) |
| 259 | { |
| 260 | reset_ts(ctx, arg_temp(arg)); |
| 261 | } |
| 262 | |
| 263 | static 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 Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 288 | static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 289 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 290 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 291 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 292 | if (ts1 == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 296 | if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 300 | for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { |
| 301 | if (i == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return false; |
| 307 | } |
| 308 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 309 | static bool args_are_copies(TCGArg arg1, TCGArg arg2) |
| 310 | { |
| 311 | return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); |
| 312 | } |
| 313 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 314 | static 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 Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 326 | static 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 Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 341 | static 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 Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 348 | static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 349 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 350 | TCGTemp *dst_ts = arg_temp(dst); |
| 351 | TCGTemp *src_ts = arg_temp(src); |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 352 | TempOptInfo *di; |
| 353 | TempOptInfo *si; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 354 | TCGOpcode new_op; |
| 355 | |
| 356 | if (ts_are_copies(dst_ts, src_ts)) { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 357 | tcg_op_remove(ctx->tcg, op); |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 358 | return true; |
Aurelien Jarno | 5365718 | 2015-06-04 21:53:25 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 361 | reset_ts(ctx, dst_ts); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 362 | di = ts_info(dst_ts); |
| 363 | si = ts_info(src_ts); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 364 | |
| 365 | switch (ctx->type) { |
| 366 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 367 | new_op = INDEX_op_mov_i32; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 368 | 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 Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 380 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 381 | op->opc = new_op; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 382 | op->args[0] = dst; |
| 383 | op->args[1] = src; |
Richard Henderson | a62f6f5 | 2014-05-22 10:59:12 -0700 | [diff] [blame] | 384 | |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 385 | di->z_mask = si->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 386 | di->s_mask = si->s_mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 387 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 388 | if (src_ts->type == dst_ts->type) { |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 389 | TempOptInfo *ni = ts_info(si->next_copy); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 390 | |
| 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 Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 397 | |
| 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 Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 402 | } |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 403 | return true; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 404 | } |
| 405 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 406 | static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op, |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 407 | TCGArg dst, uint64_t val) |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 408 | { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 409 | /* Convert movi to mov with constant temp. */ |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 410 | return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val)); |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 413 | static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 414 | { |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 415 | uint64_t l64, h64; |
| 416 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 417 | 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 Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 427 | CASE_OP_32_64_VEC(and): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 428 | return x & y; |
| 429 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 430 | CASE_OP_32_64_VEC(or): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 431 | return x | y; |
| 432 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 433 | CASE_OP_32_64_VEC(xor): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 434 | return x ^ y; |
| 435 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 436 | case INDEX_op_shl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 437 | return (uint32_t)x << (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 438 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 439 | case INDEX_op_shl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 440 | return (uint64_t)x << (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 441 | |
| 442 | case INDEX_op_shr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 443 | return (uint32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 444 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 445 | case INDEX_op_shr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 446 | return (uint64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 447 | |
| 448 | case INDEX_op_sar_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 449 | return (int32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 450 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 451 | case INDEX_op_sar_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 452 | return (int64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 453 | |
| 454 | case INDEX_op_rotr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 455 | return ror32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 456 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 457 | case INDEX_op_rotr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 458 | return ror64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 459 | |
| 460 | case INDEX_op_rotl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 461 | return rol32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 462 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 463 | case INDEX_op_rotl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 464 | return rol64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 465 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 466 | CASE_OP_32_64_VEC(not): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 467 | return ~x; |
| 468 | |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 469 | CASE_OP_32_64(neg): |
| 470 | return -x; |
| 471 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 472 | CASE_OP_32_64_VEC(andc): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 473 | return x & ~y; |
| 474 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 475 | CASE_OP_32_64_VEC(orc): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 476 | return x | ~y; |
| 477 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 478 | CASE_OP_32_64_VEC(eqv): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 479 | return ~(x ^ y); |
| 480 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 481 | CASE_OP_32_64_VEC(nand): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 482 | return ~(x & y); |
| 483 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 484 | CASE_OP_32_64_VEC(nor): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 485 | return ~(x | y); |
| 486 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 487 | 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 Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 499 | case INDEX_op_ctpop_i32: |
| 500 | return ctpop32(x); |
| 501 | |
| 502 | case INDEX_op_ctpop_i64: |
| 503 | return ctpop64(x); |
| 504 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 505 | CASE_OP_32_64(ext8s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 506 | return (int8_t)x; |
| 507 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 508 | CASE_OP_32_64(ext16s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 509 | return (int16_t)x; |
| 510 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 511 | CASE_OP_32_64(ext8u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 512 | return (uint8_t)x; |
| 513 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 514 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 515 | return (uint16_t)x; |
| 516 | |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 517 | CASE_OP_32_64(bswap16): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 518 | x = bswap16(x); |
| 519 | return y & TCG_BSWAP_OS ? (int16_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 520 | |
| 521 | CASE_OP_32_64(bswap32): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 522 | x = bswap32(x); |
| 523 | return y & TCG_BSWAP_OS ? (int32_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 524 | |
| 525 | case INDEX_op_bswap64_i64: |
| 526 | return bswap64(x); |
| 527 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 528 | case INDEX_op_ext_i32_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 529 | case INDEX_op_ext32s_i64: |
| 530 | return (int32_t)x; |
| 531 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 532 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 533 | case INDEX_op_extrl_i64_i32: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 534 | case INDEX_op_ext32u_i64: |
| 535 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 536 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 537 | case INDEX_op_extrh_i64_i32: |
| 538 | return (uint64_t)x >> 32; |
| 539 | |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 540 | case INDEX_op_muluh_i32: |
| 541 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; |
| 542 | case INDEX_op_mulsh_i32: |
| 543 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; |
| 544 | |
| 545 | case INDEX_op_muluh_i64: |
| 546 | mulu64(&l64, &h64, x, y); |
| 547 | return h64; |
| 548 | case INDEX_op_mulsh_i64: |
| 549 | muls64(&l64, &h64, x, y); |
| 550 | return h64; |
| 551 | |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 552 | case INDEX_op_div_i32: |
| 553 | /* Avoid crashing on divide by zero, otherwise undefined. */ |
| 554 | return (int32_t)x / ((int32_t)y ? : 1); |
| 555 | case INDEX_op_divu_i32: |
| 556 | return (uint32_t)x / ((uint32_t)y ? : 1); |
| 557 | case INDEX_op_div_i64: |
| 558 | return (int64_t)x / ((int64_t)y ? : 1); |
| 559 | case INDEX_op_divu_i64: |
| 560 | return (uint64_t)x / ((uint64_t)y ? : 1); |
| 561 | |
| 562 | case INDEX_op_rem_i32: |
| 563 | return (int32_t)x % ((int32_t)y ? : 1); |
| 564 | case INDEX_op_remu_i32: |
| 565 | return (uint32_t)x % ((uint32_t)y ? : 1); |
| 566 | case INDEX_op_rem_i64: |
| 567 | return (int64_t)x % ((int64_t)y ? : 1); |
| 568 | case INDEX_op_remu_i64: |
| 569 | return (uint64_t)x % ((uint64_t)y ? : 1); |
| 570 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 571 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 572 | g_assert_not_reached(); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 576 | static uint64_t do_constant_folding(TCGOpcode op, TCGType type, |
| 577 | uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 578 | { |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 579 | uint64_t res = do_constant_folding_2(op, x, y); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 580 | if (type == TCG_TYPE_I32) { |
Aurelien Jarno | 29f3ff8 | 2015-07-10 18:03:31 +0200 | [diff] [blame] | 581 | res = (int32_t)res; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 582 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 583 | return res; |
| 584 | } |
| 585 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 586 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 587 | { |
| 588 | switch (c) { |
| 589 | case TCG_COND_EQ: |
| 590 | return x == y; |
| 591 | case TCG_COND_NE: |
| 592 | return x != y; |
| 593 | case TCG_COND_LT: |
| 594 | return (int32_t)x < (int32_t)y; |
| 595 | case TCG_COND_GE: |
| 596 | return (int32_t)x >= (int32_t)y; |
| 597 | case TCG_COND_LE: |
| 598 | return (int32_t)x <= (int32_t)y; |
| 599 | case TCG_COND_GT: |
| 600 | return (int32_t)x > (int32_t)y; |
| 601 | case TCG_COND_LTU: |
| 602 | return x < y; |
| 603 | case TCG_COND_GEU: |
| 604 | return x >= y; |
| 605 | case TCG_COND_LEU: |
| 606 | return x <= y; |
| 607 | case TCG_COND_GTU: |
| 608 | return x > y; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 609 | case TCG_COND_TSTEQ: |
| 610 | return (x & y) == 0; |
| 611 | case TCG_COND_TSTNE: |
| 612 | return (x & y) != 0; |
| 613 | case TCG_COND_ALWAYS: |
| 614 | case TCG_COND_NEVER: |
| 615 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 616 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 617 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) |
| 621 | { |
| 622 | switch (c) { |
| 623 | case TCG_COND_EQ: |
| 624 | return x == y; |
| 625 | case TCG_COND_NE: |
| 626 | return x != y; |
| 627 | case TCG_COND_LT: |
| 628 | return (int64_t)x < (int64_t)y; |
| 629 | case TCG_COND_GE: |
| 630 | return (int64_t)x >= (int64_t)y; |
| 631 | case TCG_COND_LE: |
| 632 | return (int64_t)x <= (int64_t)y; |
| 633 | case TCG_COND_GT: |
| 634 | return (int64_t)x > (int64_t)y; |
| 635 | case TCG_COND_LTU: |
| 636 | return x < y; |
| 637 | case TCG_COND_GEU: |
| 638 | return x >= y; |
| 639 | case TCG_COND_LEU: |
| 640 | return x <= y; |
| 641 | case TCG_COND_GTU: |
| 642 | return x > y; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 643 | case TCG_COND_TSTEQ: |
| 644 | return (x & y) == 0; |
| 645 | case TCG_COND_TSTNE: |
| 646 | return (x & y) != 0; |
| 647 | case TCG_COND_ALWAYS: |
| 648 | case TCG_COND_NEVER: |
| 649 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 650 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 651 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 654 | static int do_constant_folding_cond_eq(TCGCond c) |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 655 | { |
| 656 | switch (c) { |
| 657 | case TCG_COND_GT: |
| 658 | case TCG_COND_LTU: |
| 659 | case TCG_COND_LT: |
| 660 | case TCG_COND_GTU: |
| 661 | case TCG_COND_NE: |
| 662 | return 0; |
| 663 | case TCG_COND_GE: |
| 664 | case TCG_COND_GEU: |
| 665 | case TCG_COND_LE: |
| 666 | case TCG_COND_LEU: |
| 667 | case TCG_COND_EQ: |
| 668 | return 1; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 669 | case TCG_COND_TSTEQ: |
| 670 | case TCG_COND_TSTNE: |
| 671 | return -1; |
| 672 | case TCG_COND_ALWAYS: |
| 673 | case TCG_COND_NEVER: |
| 674 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 675 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 676 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 679 | /* |
| 680 | * Return -1 if the condition can't be simplified, |
| 681 | * and the result of the condition (0 or 1) if it can. |
| 682 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 683 | static int do_constant_folding_cond(TCGType type, TCGArg x, |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 684 | TCGArg y, TCGCond c) |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 685 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 686 | if (arg_is_const(x) && arg_is_const(y)) { |
Alex Bennée | 9becc36 | 2022-02-09 11:21:42 +0000 | [diff] [blame] | 687 | uint64_t xv = arg_info(x)->val; |
| 688 | uint64_t yv = arg_info(y)->val; |
| 689 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 690 | switch (type) { |
| 691 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 692 | return do_constant_folding_cond_32(xv, yv, c); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 693 | case TCG_TYPE_I64: |
| 694 | return do_constant_folding_cond_64(xv, yv, c); |
| 695 | default: |
| 696 | /* Only scalar comparisons are optimizable */ |
| 697 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 698 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 699 | } else if (args_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 700 | return do_constant_folding_cond_eq(c); |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 701 | } else if (arg_is_const_val(y, 0)) { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 702 | switch (c) { |
| 703 | case TCG_COND_LTU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 704 | case TCG_COND_TSTNE: |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 705 | return 0; |
| 706 | case TCG_COND_GEU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 707 | case TCG_COND_TSTEQ: |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 708 | return 1; |
| 709 | default: |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 710 | return -1; |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 711 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 712 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 713 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 716 | /** |
| 717 | * swap_commutative: |
| 718 | * @dest: TCGArg of the destination argument, or NO_DEST. |
| 719 | * @p1: first paired argument |
| 720 | * @p2: second paired argument |
| 721 | * |
| 722 | * If *@p1 is a constant and *@p2 is not, swap. |
| 723 | * If *@p2 matches @dest, swap. |
| 724 | * Return true if a swap was performed. |
| 725 | */ |
| 726 | |
| 727 | #define NO_DEST temp_arg(NULL) |
| 728 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 729 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 730 | { |
| 731 | TCGArg a1 = *p1, a2 = *p2; |
| 732 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 733 | sum += arg_is_const(a1); |
| 734 | sum -= arg_is_const(a2); |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 735 | |
| 736 | /* Prefer the constant in second argument, and then the form |
| 737 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 738 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 739 | *p1 = a2; |
| 740 | *p2 = a1; |
| 741 | return true; |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 746 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 747 | { |
| 748 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 749 | sum += arg_is_const(p1[0]); |
| 750 | sum += arg_is_const(p1[1]); |
| 751 | sum -= arg_is_const(p2[0]); |
| 752 | sum -= arg_is_const(p2[1]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 753 | if (sum > 0) { |
| 754 | TCGArg t; |
| 755 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 756 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 757 | return true; |
| 758 | } |
| 759 | return false; |
| 760 | } |
| 761 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 762 | /* |
| 763 | * Return -1 if the condition can't be simplified, |
| 764 | * and the result of the condition (0 or 1) if it can. |
| 765 | */ |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 766 | static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest, |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 767 | 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 780 | 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 Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 802 | 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 819 | } |
| 820 | return -1; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 823 | static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args) |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 824 | { |
| 825 | TCGArg al, ah, bl, bh; |
| 826 | TCGCond c; |
| 827 | bool swap; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 828 | int r; |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 829 | |
| 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 850 | |
| 851 | r = do_constant_folding_cond_64(a, b, c); |
| 852 | if (r >= 0) { |
| 853 | return r; |
| 854 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 855 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 856 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 857 | if (b == 0) { |
| 858 | switch (c) { |
| 859 | case TCG_COND_LTU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 860 | case TCG_COND_TSTNE: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 861 | return 0; |
| 862 | case TCG_COND_GEU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 863 | case TCG_COND_TSTEQ: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 864 | return 1; |
| 865 | default: |
| 866 | break; |
| 867 | } |
| 868 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 869 | |
| 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 Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 884 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 885 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 886 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 887 | 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 Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 898 | } |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 899 | |
| 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 Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 919 | return -1; |
| 920 | } |
| 921 | |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 922 | static 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 Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 926 | init_ts_info(ctx, ts); |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 930 | static void copy_propagate(OptContext *ctx, TCGOp *op, |
| 931 | int nb_oargs, int nb_iargs) |
| 932 | { |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 933 | for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
| 934 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 935 | if (ts_is_copy(ts)) { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 936 | op->args[i] = temp_arg(find_better_copy(ts)); |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 941 | static void finish_bb(OptContext *ctx) |
| 942 | { |
| 943 | /* We only optimize memory barriers across basic blocks. */ |
| 944 | ctx->prev_mb = NULL; |
| 945 | } |
| 946 | |
| 947 | static 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 Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 955 | static bool finish_folding(OptContext *ctx, TCGOp *op) |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 956 | { |
| 957 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 958 | int i, nb_oargs; |
| 959 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 960 | nb_oargs = def->nb_oargs; |
| 961 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 962 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 963 | reset_ts(ctx, ts); |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 964 | /* |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 965 | * Save the corresponding known-zero/sign bits mask for the |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 966 | * first output argument (only one supported so far). |
| 967 | */ |
| 968 | if (i == 0) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 969 | ts_info(ts)->z_mask = ctx->z_mask; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 972 | return true; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 975 | /* |
| 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 | |
| 986 | static 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 992 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 993 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 994 | } |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | static 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1004 | t1 = do_constant_folding(op->opc, ctx->type, t1, t2); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1005 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 1006 | } |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1010 | static 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 Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1016 | static 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 Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1022 | /* |
| 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 Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1026 | * The passed s_mask may be augmented by z_mask. |
Richard Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1027 | */ |
| 1028 | static bool fold_masks_zs(OptContext *ctx, TCGOp *op, |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1029 | uint64_t z_mask, int64_t s_mask) |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1030 | { |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1031 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 1032 | TCGTemp *ts; |
| 1033 | TempOptInfo *ti; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1034 | int rep; |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1035 | |
| 1036 | /* Only single-output opcodes are supported here. */ |
| 1037 | tcg_debug_assert(def->nb_oargs == 1); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1038 | |
| 1039 | /* |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1040 | * 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1045 | */ |
| 1046 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1047 | z_mask = (int32_t)z_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1048 | s_mask |= INT32_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | if (z_mask == 0) { |
| 1052 | return tcg_opt_gen_movi(ctx, op, op->args[0], 0); |
| 1053 | } |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1054 | |
| 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 Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1060 | |
| 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 Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1067 | return true; |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame^] | 1070 | static 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 Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1075 | static bool fold_masks(OptContext *ctx, TCGOp *op) |
| 1076 | { |
| 1077 | return fold_masks_zs(ctx, op, ctx->z_mask, ctx->s_mask); |
| 1078 | } |
| 1079 | |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1080 | /* |
| 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 | */ |
| 1085 | static 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1090 | 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 Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1096 | /* |
| 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 | */ |
| 1101 | static bool fold_not(OptContext *ctx, TCGOp *op); |
| 1102 | static 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 Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1133 | /* If the binary operation has first argument @i, fold to @i. */ |
| 1134 | static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1135 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1136 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1137 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1138 | } |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1142 | /* If the binary operation has first argument @i, fold to NOT. */ |
| 1143 | static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1144 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1145 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1146 | return fold_to_not(ctx, op, 2); |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1151 | /* If the binary operation has second argument @i, fold to @i. */ |
| 1152 | static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1153 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1154 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1155 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1156 | } |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1160 | /* If the binary operation has second argument @i, fold to identity. */ |
| 1161 | static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1162 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1163 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1164 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1165 | } |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1169 | /* If the binary operation has second argument @i, fold to NOT. */ |
| 1170 | static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1171 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1172 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1173 | return fold_to_not(ctx, op, 1); |
| 1174 | } |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1178 | /* If the binary operation has both arguments equal, fold to @i. */ |
| 1179 | static 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 Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1187 | /* If the binary operation has both arguments equal, fold to identity. */ |
| 1188 | static 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1196 | /* |
| 1197 | * These outermost fold_<op> functions are sorted alphabetically. |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1198 | * |
| 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1203 | */ |
| 1204 | |
| 1205 | static bool fold_add(OptContext *ctx, TCGOp *op) |
| 1206 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1207 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1208 | fold_xi_to_x(ctx, op, 0)) { |
| 1209 | return true; |
| 1210 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1211 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1214 | /* We cannot as yet do_constant_folding with vectors. */ |
| 1215 | static 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 Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1221 | return finish_folding(ctx, op); |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1224 | static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1225 | { |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1226 | 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 Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1230 | 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 Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1234 | TCGArg rl, rh; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1235 | TCGOp *op2; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1236 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1237 | 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 Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1249 | } else { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1250 | 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 Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | rl = op->args[0]; |
| 1264 | rh = op->args[1]; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1265 | |
| 1266 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Philippe Mathieu-Daudé | d447894 | 2022-12-18 22:18:31 +0100 | [diff] [blame] | 1267 | op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2); |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1268 | |
| 1269 | tcg_opt_gen_movi(ctx, op, rl, al); |
| 1270 | tcg_opt_gen_movi(ctx, op2, rh, ah); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1273 | |
| 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 Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1288 | return finish_folding(ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1291 | static bool fold_add2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1292 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1293 | /* 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 Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1297 | return fold_addsub2(ctx, op, true); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1300 | static bool fold_and(OptContext *ctx, TCGOp *op) |
| 1301 | { |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1302 | uint64_t z1, z2, z_mask, s_mask; |
| 1303 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1304 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1305 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1306 | fold_xi_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1307 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1308 | fold_xx_to_x(ctx, op)) { |
| 1309 | return true; |
| 1310 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1311 | |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1312 | t1 = arg_info(op->args[1]); |
| 1313 | t2 = arg_info(op->args[2]); |
| 1314 | z1 = t1->z_mask; |
| 1315 | z2 = t2->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1318 | * Known-zeros does not imply known-ones. Therefore unless |
| 1319 | * arg2 is constant, we can't infer affected bits from it. |
| 1320 | */ |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1321 | if (ti_is_const(t2) && fold_affected_mask(ctx, op, z1 & ~z2)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1322 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1325 | 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static bool fold_andc(OptContext *ctx, TCGOp *op) |
| 1337 | { |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1338 | uint64_t z_mask, s_mask; |
| 1339 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1340 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1341 | if (fold_const2(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1342 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1343 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1344 | fold_ix_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1345 | return true; |
| 1346 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1347 | |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1348 | t1 = arg_info(op->args[1]); |
| 1349 | t2 = arg_info(op->args[2]); |
| 1350 | z_mask = t1->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1351 | |
| 1352 | /* |
| 1353 | * Known-zeros does not imply known-ones. Therefore unless |
| 1354 | * arg2 is constant, we can't infer anything from it. |
| 1355 | */ |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1356 | if (ti_is_const(t2)) { |
| 1357 | uint64_t v2 = ti_const_val(t2); |
| 1358 | if (fold_affected_mask(ctx, op, z_mask & v2)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1359 | return true; |
| 1360 | } |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1361 | z_mask &= ~v2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1362 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1363 | |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1364 | s_mask = t1->s_mask & t2->s_mask; |
| 1365 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1368 | static bool fold_brcond(OptContext *ctx, TCGOp *op) |
| 1369 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1370 | int i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[0], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1371 | &op->args[1], &op->args[2]); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1372 | 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 Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1379 | finish_ebb(ctx); |
| 1380 | } else { |
| 1381 | finish_bb(ctx); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1382 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1383 | return true; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1386 | static bool fold_brcond2(OptContext *ctx, TCGOp *op) |
| 1387 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1388 | TCGCond cond; |
| 1389 | TCGArg label; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1390 | int i, inv = 0; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1391 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1392 | i = do_constant_folding_cond2(ctx, op, &op->args[0]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1393 | cond = op->args[4]; |
| 1394 | label = op->args[5]; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1395 | 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 Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1406 | if (arg_is_const_val(op->args[2], 0) && |
| 1407 | arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1408 | 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1420 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1421 | 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1429 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1430 | op->args[3], cond); |
| 1431 | switch (i ^ inv) { |
| 1432 | case 0: |
| 1433 | goto do_brcond_const; |
| 1434 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1435 | 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 Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1446 | } |
| 1447 | break; |
| 1448 | |
| 1449 | default: |
| 1450 | break; |
| 1451 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1452 | 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 Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1459 | 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1465 | return fold_brcond(ctx, op); |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1466 | |
| 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 Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1474 | finish_ebb(ctx); |
| 1475 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1476 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1477 | |
| 1478 | finish_bb(ctx); |
| 1479 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1482 | static bool fold_bswap(OptContext *ctx, TCGOp *op) |
| 1483 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1484 | uint64_t z_mask, s_mask, sign; |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1485 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1486 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1487 | 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 Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1492 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1493 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1494 | z_mask = t1->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1495 | 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 Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1514 | s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1515 | 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 Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1523 | /* The value and therefore s_mask is explicitly sign-extended. */ |
| 1524 | s_mask = sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1525 | 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1531 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1532 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1535 | static 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 Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1552 | reset_ts(ctx, &ctx->tcg->temps[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 1557 | /* 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 Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1562 | /* Reset temp data for outputs. */ |
| 1563 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1564 | reset_temp(ctx, op->args[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /* Stop optimizing MB across calls. */ |
| 1568 | ctx->prev_mb = NULL; |
| 1569 | return true; |
| 1570 | } |
| 1571 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1572 | static bool fold_count_zeros(OptContext *ctx, TCGOp *op) |
| 1573 | { |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1574 | uint64_t z_mask, s_mask; |
| 1575 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 1576 | TempOptInfo *t2 = arg_info(op->args[2]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1577 | |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1578 | if (ti_is_const(t1)) { |
| 1579 | uint64_t t = ti_const_val(t1); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1580 | |
| 1581 | if (t != 0) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1582 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1583 | 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1587 | |
| 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 Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1598 | 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 Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1605 | static bool fold_ctpop(OptContext *ctx, TCGOp *op) |
| 1606 | { |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame^] | 1607 | uint64_t z_mask; |
| 1608 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1609 | if (fold_const1(ctx, op)) { |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
| 1613 | switch (ctx->type) { |
| 1614 | case TCG_TYPE_I32: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame^] | 1615 | z_mask = 32 | 31; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1616 | break; |
| 1617 | case TCG_TYPE_I64: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame^] | 1618 | z_mask = 64 | 63; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1619 | break; |
| 1620 | default: |
| 1621 | g_assert_not_reached(); |
| 1622 | } |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame^] | 1623 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1626 | static bool fold_deposit(OptContext *ctx, TCGOp *op) |
| 1627 | { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1628 | TCGOpcode and_opc; |
| 1629 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1630 | 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1637 | |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1638 | 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 Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1650 | if (arg_is_const_val(op->args[1], 0) && op->args[3] == 0) { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1651 | uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]); |
| 1652 | |
| 1653 | op->opc = and_opc; |
| 1654 | op->args[1] = op->args[2]; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1655 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1656 | ctx->z_mask = mask & arg_info(op->args[1])->z_mask; |
| 1657 | return false; |
| 1658 | } |
| 1659 | |
| 1660 | /* Inserting zero into a value. */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1661 | if (arg_is_const_val(op->args[2], 0)) { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1662 | uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0); |
| 1663 | |
| 1664 | op->opc = and_opc; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1665 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1666 | ctx->z_mask = mask & arg_info(op->args[1])->z_mask; |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1670 | 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 Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
| 1675 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1676 | static bool fold_divide(OptContext *ctx, TCGOp *op) |
| 1677 | { |
Richard Henderson | 2f9d9a3 | 2021-10-25 11:30:14 -0700 | [diff] [blame] | 1678 | if (fold_const2(ctx, op) || |
| 1679 | fold_xi_to_x(ctx, op, 1)) { |
| 1680 | return true; |
| 1681 | } |
| 1682 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1685 | static 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 | |
| 1695 | static 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1710 | static bool fold_eqv(OptContext *ctx, TCGOp *op) |
| 1711 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1712 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1713 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1714 | fold_xi_to_not(ctx, op, 0)) { |
| 1715 | return true; |
| 1716 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1717 | |
| 1718 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1719 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1720 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1723 | static bool fold_extract(OptContext *ctx, TCGOp *op) |
| 1724 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1725 | uint64_t z_mask_old, z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1726 | int pos = op->args[2]; |
| 1727 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1728 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1729 | if (arg_is_const(op->args[1])) { |
| 1730 | uint64_t t; |
| 1731 | |
| 1732 | t = arg_info(op->args[1])->val; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1733 | t = extract64(t, pos, len); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1734 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1735 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1736 | |
| 1737 | z_mask_old = arg_info(op->args[1])->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1738 | z_mask = extract64(z_mask_old, pos, len); |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1739 | if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) { |
| 1740 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1741 | } |
| 1742 | ctx->z_mask = z_mask; |
| 1743 | |
| 1744 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1747 | static 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 Henderson | 225bec0 | 2021-11-09 23:17:59 +0100 | [diff] [blame] | 1759 | v2 = (uint64_t)((int32_t)v2 << (32 - shr)); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1760 | } |
| 1761 | return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); |
| 1762 | } |
| 1763 | return false; |
| 1764 | } |
| 1765 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1766 | static bool fold_exts(OptContext *ctx, TCGOp *op) |
| 1767 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1768 | uint64_t s_mask_old, s_mask, z_mask, sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1769 | bool type_change = false; |
| 1770 | |
| 1771 | if (fold_const1(ctx, op)) { |
| 1772 | return true; |
| 1773 | } |
| 1774 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1775 | 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1778 | |
| 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 Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1801 | } |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1802 | s_mask |= sign << 1; |
| 1803 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1804 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1805 | ctx->s_mask = s_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1806 | if (0 && !type_change && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1807 | return true; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1808 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1809 | |
| 1810 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static bool fold_extu(OptContext *ctx, TCGOp *op) |
| 1814 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1815 | 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 Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1847 | if (!type_change && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) { |
| 1848 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1849 | } |
| 1850 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1853 | static 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 Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1877 | static 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 Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1882 | static bool fold_movcond(OptContext *ctx, TCGOp *op) |
| 1883 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1884 | int i; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1885 | |
Richard Henderson | 141125e | 2024-09-06 21:00:10 -0700 | [diff] [blame] | 1886 | /* 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 Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1891 | /* |
| 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 Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1896 | op->args[5] = tcg_invert_cond(op->args[5]); |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1899 | i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1900 | &op->args[2], &op->args[5]); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1901 | if (i >= 0) { |
| 1902 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); |
| 1903 | } |
| 1904 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1905 | ctx->z_mask = arg_info(op->args[3])->z_mask |
| 1906 | | arg_info(op->args[4])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1907 | ctx->s_mask = arg_info(op->args[3])->s_mask |
| 1908 | & arg_info(op->args[4])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1909 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1910 | 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 Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1913 | TCGOpcode opc, negopc = 0; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1914 | TCGCond cond = op->args[5]; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1915 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1916 | switch (ctx->type) { |
| 1917 | case TCG_TYPE_I32: |
| 1918 | opc = INDEX_op_setcond_i32; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1919 | if (TCG_TARGET_HAS_negsetcond_i32) { |
| 1920 | negopc = INDEX_op_negsetcond_i32; |
| 1921 | } |
| 1922 | tv = (int32_t)tv; |
| 1923 | fv = (int32_t)fv; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1924 | break; |
| 1925 | case TCG_TYPE_I64: |
| 1926 | opc = INDEX_op_setcond_i64; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1927 | if (TCG_TARGET_HAS_negsetcond_i64) { |
| 1928 | negopc = INDEX_op_negsetcond_i64; |
| 1929 | } |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1930 | break; |
| 1931 | default: |
| 1932 | g_assert_not_reached(); |
| 1933 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1934 | |
| 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 Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1941 | } 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 Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1954 | static bool fold_mul(OptContext *ctx, TCGOp *op) |
| 1955 | { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1956 | if (fold_const2(ctx, op) || |
Richard Henderson | 5b5cf47 | 2021-10-25 11:19:14 -0700 | [diff] [blame] | 1957 | fold_xi_to_i(ctx, op, 0) || |
| 1958 | fold_xi_to_x(ctx, op, 1)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1959 | return true; |
| 1960 | } |
| 1961 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) |
| 1965 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1966 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1967 | fold_xi_to_i(ctx, op, 0)) { |
| 1968 | return true; |
| 1969 | } |
| 1970 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1973 | static bool fold_multiply2(OptContext *ctx, TCGOp *op) |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1974 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1975 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
| 1976 | |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1977 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1978 | 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 Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1981 | TCGArg rl, rh; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1982 | 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 Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2004 | |
| 2005 | rl = op->args[0]; |
| 2006 | rh = op->args[1]; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2007 | |
| 2008 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Philippe Mathieu-Daudé | d447894 | 2022-12-18 22:18:31 +0100 | [diff] [blame] | 2009 | op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2); |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2010 | |
| 2011 | tcg_opt_gen_movi(ctx, op, rl, l); |
| 2012 | tcg_opt_gen_movi(ctx, op2, rh, h); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2013 | return true; |
| 2014 | } |
| 2015 | return false; |
| 2016 | } |
| 2017 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2018 | static bool fold_nand(OptContext *ctx, TCGOp *op) |
| 2019 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2020 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2021 | fold_xi_to_not(ctx, op, -1)) { |
| 2022 | return true; |
| 2023 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2024 | |
| 2025 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2026 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2027 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2030 | static bool fold_neg_no_const(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2031 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2032 | /* Set to 1 all bits to the left of the rightmost. */ |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2033 | uint64_t z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2034 | ctx->z_mask = -(z_mask & -z_mask); |
| 2035 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2036 | /* |
| 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2044 | static bool fold_neg(OptContext *ctx, TCGOp *op) |
| 2045 | { |
| 2046 | return fold_const1(ctx, op) || fold_neg_no_const(ctx, op); |
| 2047 | } |
| 2048 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2049 | static bool fold_nor(OptContext *ctx, TCGOp *op) |
| 2050 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2051 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2052 | fold_xi_to_not(ctx, op, 0)) { |
| 2053 | return true; |
| 2054 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2055 | |
| 2056 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2057 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2058 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | static bool fold_not(OptContext *ctx, TCGOp *op) |
| 2062 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2063 | if (fold_const1(ctx, op)) { |
| 2064 | return true; |
| 2065 | } |
| 2066 | |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2067 | ctx->s_mask = arg_info(op->args[1])->s_mask; |
| 2068 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2069 | /* Because of fold_to_not, we want to always return true, via finish. */ |
| 2070 | finish_folding(ctx, op); |
| 2071 | return true; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static bool fold_or(OptContext *ctx, TCGOp *op) |
| 2075 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2076 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2077 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 2078 | fold_xx_to_x(ctx, op)) { |
| 2079 | return true; |
| 2080 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2081 | |
| 2082 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 2083 | | arg_info(op->args[2])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2084 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2085 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2086 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | static bool fold_orc(OptContext *ctx, TCGOp *op) |
| 2090 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2091 | if (fold_const2(ctx, op) || |
Richard Henderson | 4e858d9 | 2021-08-26 07:31:13 -0700 | [diff] [blame] | 2092 | fold_xx_to_i(ctx, op, -1) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2093 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2094 | fold_ix_to_not(ctx, op, 0)) { |
| 2095 | return true; |
| 2096 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2097 | |
| 2098 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2099 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2100 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2103 | static bool fold_qemu_ld(OptContext *ctx, TCGOp *op) |
| 2104 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2105 | 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 Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2110 | if (width < 64) { |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 2111 | if (mop & MO_SIGN) { |
| 2112 | ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width); |
| 2113 | } else { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2114 | ctx->z_mask = MAKE_64BIT_MASK(0, width); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2115 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2118 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2119 | ctx->prev_mb = NULL; |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
| 2123 | static 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2130 | static bool fold_remainder(OptContext *ctx, TCGOp *op) |
| 2131 | { |
Richard Henderson | 267c17e | 2021-10-25 11:30:33 -0700 | [diff] [blame] | 2132 | if (fold_const2(ctx, op) || |
| 2133 | fold_xx_to_i(ctx, op, 0)) { |
| 2134 | return true; |
| 2135 | } |
| 2136 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2139 | static 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2241 | static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg) |
| 2242 | { |
Paolo Bonzini | ff20281 | 2024-02-28 12:06:41 +0100 | [diff] [blame] | 2243 | TCGOpcode and_opc, sub_opc, xor_opc, neg_opc, shr_opc; |
| 2244 | TCGOpcode uext_opc = 0, sext_opc = 0; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2245 | 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 Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2335 | static bool fold_setcond(OptContext *ctx, TCGOp *op) |
| 2336 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2337 | int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2338 | &op->args[2], &op->args[3]); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2339 | if (i >= 0) { |
| 2340 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2341 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2342 | |
| 2343 | if (fold_setcond_zmask(ctx, op, false)) { |
| 2344 | return true; |
| 2345 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2346 | fold_setcond_tst_pow2(ctx, op, false); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2347 | |
| 2348 | ctx->z_mask = 1; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2349 | return false; |
| 2350 | } |
| 2351 | |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2352 | static bool fold_negsetcond(OptContext *ctx, TCGOp *op) |
| 2353 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2354 | int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2355 | &op->args[2], &op->args[3]); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2356 | if (i >= 0) { |
| 2357 | return tcg_opt_gen_movi(ctx, op, op->args[0], -i); |
| 2358 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2359 | |
| 2360 | if (fold_setcond_zmask(ctx, op, true)) { |
| 2361 | return true; |
| 2362 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2363 | fold_setcond_tst_pow2(ctx, op, true); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* Value is {0,-1} so all bits are repetitions of the sign. */ |
| 2366 | ctx->s_mask = -1; |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2370 | static bool fold_setcond2(OptContext *ctx, TCGOp *op) |
| 2371 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2372 | TCGCond cond; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2373 | int i, inv = 0; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2374 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2375 | i = do_constant_folding_cond2(ctx, op, &op->args[1]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2376 | cond = op->args[5]; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2377 | 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 Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 2388 | if (arg_is_const_val(op->args[3], 0) && |
| 2389 | arg_is_const_val(op->args[4], 0)) { |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2390 | 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2402 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2403 | 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 Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2411 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2412 | op->args[4], cond); |
| 2413 | switch (i ^ inv) { |
| 2414 | case 0: |
| 2415 | goto do_setcond_const; |
| 2416 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2417 | goto do_setcond_low; |
| 2418 | } |
| 2419 | break; |
| 2420 | |
| 2421 | case TCG_COND_TSTEQ: |
| 2422 | case TCG_COND_TSTNE: |
Richard Henderson | a71d9df | 2024-06-30 19:46:23 -0700 | [diff] [blame] | 2423 | if (arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2424 | goto do_setcond_high; |
| 2425 | } |
| 2426 | if (arg_is_const_val(op->args[4], 0)) { |
| 2427 | goto do_setcond_low; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2428 | } |
| 2429 | break; |
| 2430 | |
| 2431 | default: |
| 2432 | break; |
| 2433 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2434 | 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 Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2440 | 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 Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2445 | return fold_setcond(ctx, op); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2446 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2447 | |
| 2448 | ctx->z_mask = 1; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2449 | return false; |
| 2450 | |
| 2451 | do_setcond_const: |
| 2452 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2453 | } |
| 2454 | |
Richard Henderson | 1f10654 | 2024-09-06 12:22:41 -0700 | [diff] [blame] | 2455 | static 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 | |
| 2464 | static 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 Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2485 | static bool fold_sextract(OptContext *ctx, TCGOp *op) |
| 2486 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2487 | uint64_t z_mask, s_mask, s_mask_old; |
| 2488 | int pos = op->args[2]; |
| 2489 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2490 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2491 | if (arg_is_const(op->args[1])) { |
| 2492 | uint64_t t; |
| 2493 | |
| 2494 | t = arg_info(op->args[1])->val; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2495 | t = sextract64(t, pos, len); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2496 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 2497 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2498 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2499 | z_mask = arg_info(op->args[1])->z_mask; |
| 2500 | z_mask = sextract64(z_mask, pos, len); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2501 | ctx->z_mask = z_mask; |
| 2502 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2503 | 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 Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 2508 | if (0 && pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 2509 | return true; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2512 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2515 | static bool fold_shift(OptContext *ctx, TCGOp *op) |
| 2516 | { |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2517 | uint64_t s_mask, z_mask, sign; |
| 2518 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2519 | if (fold_const2(ctx, op) || |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 2520 | fold_ix_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2521 | fold_xi_to_x(ctx, op, 0)) { |
| 2522 | return true; |
| 2523 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2524 | |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2525 | s_mask = arg_info(op->args[1])->s_mask; |
| 2526 | z_mask = arg_info(op->args[1])->z_mask; |
| 2527 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2528 | if (arg_is_const(op->args[2])) { |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2529 | 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 Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2534 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2535 | return fold_masks(ctx, op); |
| 2536 | } |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2537 | |
| 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 Henderson | 2911e9b | 2024-03-26 11:21:38 -1000 | [diff] [blame] | 2552 | if (sign && !(z_mask & sign)) { |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2553 | ctx->s_mask = s_mask; |
| 2554 | } |
| 2555 | break; |
| 2556 | default: |
| 2557 | break; |
| 2558 | } |
| 2559 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2560 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2563 | static 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 Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2575 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2576 | break; |
| 2577 | case TCG_TYPE_I64: |
| 2578 | neg_op = INDEX_op_neg_i64; |
Richard Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2579 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2580 | 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 Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2594 | return fold_neg_no_const(ctx, op); |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2595 | } |
| 2596 | return false; |
| 2597 | } |
| 2598 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2599 | /* We cannot as yet do_constant_folding with vectors. */ |
| 2600 | static bool fold_sub_vec(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2601 | { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2602 | if (fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2603 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2604 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2605 | return true; |
| 2606 | } |
| 2607 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2610 | static bool fold_sub(OptContext *ctx, TCGOp *op) |
| 2611 | { |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2612 | 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 Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2627 | static bool fold_sub2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2628 | { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2629 | return fold_addsub2(ctx, op, false); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2632 | static 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 Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2636 | CASE_OP_32_64(ld8s): |
| 2637 | ctx->s_mask = MAKE_64BIT_MASK(8, 56); |
| 2638 | break; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2639 | CASE_OP_32_64(ld8u): |
| 2640 | ctx->z_mask = MAKE_64BIT_MASK(0, 8); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2641 | break; |
| 2642 | CASE_OP_32_64(ld16s): |
| 2643 | ctx->s_mask = MAKE_64BIT_MASK(16, 48); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2644 | break; |
| 2645 | CASE_OP_32_64(ld16u): |
| 2646 | ctx->z_mask = MAKE_64BIT_MASK(0, 16); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2647 | break; |
| 2648 | case INDEX_op_ld32s_i64: |
| 2649 | ctx->s_mask = MAKE_64BIT_MASK(32, 32); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2650 | 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 Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2660 | static 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 | |
| 2683 | static 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 | |
| 2717 | static 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 Henderson | 3eaadae | 2023-08-23 23:13:06 -0700 | [diff] [blame] | 2731 | |
| 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 Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2744 | 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 Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2750 | static bool fold_xor(OptContext *ctx, TCGOp *op) |
| 2751 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2752 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2753 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2754 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2755 | fold_xi_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2756 | return true; |
| 2757 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2758 | |
| 2759 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 2760 | | arg_info(op->args[2])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2761 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2762 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2763 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Richard Henderson | e58b977 | 2024-09-06 22:30:01 -0700 | [diff] [blame] | 2766 | static 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 Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2821 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 2822 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2823 | { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2824 | int nb_temps, i; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 2825 | TCGOp *op, *op_next; |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 2826 | OptContext ctx = { .tcg = s }; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 2827 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2828 | QSIMPLEQ_INIT(&ctx.mem_free); |
| 2829 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2830 | /* Array VALS has an element for each temp. |
| 2831 | If this temp holds a constant then its value is kept in VALS' element. |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 2832 | If this temp is a copy of other ones then the other copies are |
| 2833 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2834 | |
| 2835 | nb_temps = s->nb_temps; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 2836 | for (i = 0; i < nb_temps; ++i) { |
| 2837 | s->temps[i].state_ptr = NULL; |
| 2838 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2839 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 2840 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2841 | TCGOpcode opc = op->opc; |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2842 | const TCGOpDef *def; |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 2843 | bool done = false; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2844 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2845 | /* Calls are special. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2846 | if (opc == INDEX_op_call) { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2847 | fold_call(&ctx, op); |
| 2848 | continue; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 2849 | } |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2850 | |
| 2851 | def = &tcg_op_defs[opc]; |
Richard Henderson | ec5d4cb | 2021-08-24 08:20:27 -0700 | [diff] [blame] | 2852 | init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); |
| 2853 | copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2854 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2855 | /* 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 Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2864 | /* Assume all bits affected, no bits known zero, no sign reps. */ |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2865 | ctx.z_mask = -1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2866 | ctx.s_mask = 0; |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 2867 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2868 | /* |
| 2869 | * Process each opcode. |
| 2870 | * Sorted alphabetically by opcode as much as possible. |
| 2871 | */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2872 | switch (opc) { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2873 | CASE_OP_32_64(add): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2874 | done = fold_add(&ctx, op); |
| 2875 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2876 | case INDEX_op_add_vec: |
| 2877 | done = fold_add_vec(&ctx, op); |
| 2878 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2879 | CASE_OP_32_64(add2): |
| 2880 | done = fold_add2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2881 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2882 | 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 Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 2888 | CASE_OP_32_64(brcond): |
| 2889 | done = fold_brcond(&ctx, op); |
| 2890 | break; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 2891 | case INDEX_op_brcond2_i32: |
| 2892 | done = fold_brcond2(&ctx, op); |
| 2893 | break; |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 2894 | 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 Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 2899 | CASE_OP_32_64(clz): |
| 2900 | CASE_OP_32_64(ctz): |
| 2901 | done = fold_count_zeros(&ctx, op); |
| 2902 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2903 | CASE_OP_32_64(ctpop): |
| 2904 | done = fold_ctpop(&ctx, op); |
| 2905 | break; |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 2906 | CASE_OP_32_64(deposit): |
| 2907 | done = fold_deposit(&ctx, op); |
| 2908 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2909 | CASE_OP_32_64(div): |
| 2910 | CASE_OP_32_64(divu): |
| 2911 | done = fold_divide(&ctx, op); |
| 2912 | break; |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 2913 | 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 Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2919 | CASE_OP_32_64_VEC(eqv): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2920 | done = fold_eqv(&ctx, op); |
| 2921 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2922 | CASE_OP_32_64(extract): |
| 2923 | done = fold_extract(&ctx, op); |
| 2924 | break; |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 2925 | CASE_OP_32_64(extract2): |
| 2926 | done = fold_extract2(&ctx, op); |
| 2927 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2928 | 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 Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2942 | CASE_OP_32_64(ld8s): |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2943 | CASE_OP_32_64(ld8u): |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2944 | CASE_OP_32_64(ld16s): |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2945 | CASE_OP_32_64(ld16u): |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2946 | case INDEX_op_ld32s_i64: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2947 | case INDEX_op_ld32u_i64: |
| 2948 | done = fold_tcg_ld(&ctx, op); |
| 2949 | break; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2950 | 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 Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2965 | case INDEX_op_mb: |
| 2966 | done = fold_mb(&ctx, op); |
| 2967 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2968 | CASE_OP_32_64_VEC(mov): |
| 2969 | done = fold_mov(&ctx, op); |
| 2970 | break; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2971 | CASE_OP_32_64(movcond): |
| 2972 | done = fold_movcond(&ctx, op); |
| 2973 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2974 | 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 Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2981 | CASE_OP_32_64(muls2): |
| 2982 | CASE_OP_32_64(mulu2): |
| 2983 | done = fold_multiply2(&ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2984 | break; |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2985 | CASE_OP_32_64_VEC(nand): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2986 | done = fold_nand(&ctx, op); |
| 2987 | break; |
| 2988 | CASE_OP_32_64(neg): |
| 2989 | done = fold_neg(&ctx, op); |
| 2990 | break; |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2991 | CASE_OP_32_64_VEC(nor): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2992 | 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 Henderson | fecccfc | 2023-05-16 20:07:20 -0700 | [diff] [blame] | 3003 | 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 Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3009 | done = fold_qemu_ld(&ctx, op); |
| 3010 | break; |
Richard Henderson | fecccfc | 2023-05-16 20:07:20 -0700 | [diff] [blame] | 3011 | 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 Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3019 | done = fold_qemu_st(&ctx, op); |
| 3020 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3021 | 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 Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 3032 | CASE_OP_32_64(setcond): |
| 3033 | done = fold_setcond(&ctx, op); |
| 3034 | break; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 3035 | CASE_OP_32_64(negsetcond): |
| 3036 | done = fold_negsetcond(&ctx, op); |
| 3037 | break; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 3038 | case INDEX_op_setcond2_i32: |
| 3039 | done = fold_setcond2(&ctx, op); |
| 3040 | break; |
Richard Henderson | 1f10654 | 2024-09-06 12:22:41 -0700 | [diff] [blame] | 3041 | 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 Henderson | e58b977 | 2024-09-06 22:30:01 -0700 | [diff] [blame] | 3047 | case INDEX_op_bitsel_vec: |
| 3048 | done = fold_bitsel_vec(&ctx, op); |
| 3049 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 3050 | CASE_OP_32_64(sextract): |
| 3051 | done = fold_sextract(&ctx, op); |
| 3052 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 3053 | CASE_OP_32_64(sub): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3054 | done = fold_sub(&ctx, op); |
| 3055 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 3056 | case INDEX_op_sub_vec: |
| 3057 | done = fold_sub_vec(&ctx, op); |
| 3058 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 3059 | CASE_OP_32_64(sub2): |
| 3060 | done = fold_sub2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 3061 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3062 | CASE_OP_32_64_VEC(xor): |
| 3063 | done = fold_xor(&ctx, op); |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3064 | break; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 3065 | 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 Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3073 | default: |
| 3074 | break; |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 3077 | if (!done) { |
| 3078 | finish_folding(&ctx, op); |
| 3079 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3080 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3081 | } |