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" |
Richard Henderson | 93280b6 | 2025-01-08 22:51:55 +0100 | [diff] [blame] | 31 | #include "tcg-has.h" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 32 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 33 | #define CASE_OP_32_64(x) \ |
| 34 | glue(glue(case INDEX_op_, x), _i32): \ |
| 35 | glue(glue(case INDEX_op_, x), _i64) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 36 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 37 | #define CASE_OP_32_64_VEC(x) \ |
| 38 | glue(glue(case INDEX_op_, x), _i32): \ |
| 39 | glue(glue(case INDEX_op_, x), _i64): \ |
| 40 | glue(glue(case INDEX_op_, x), _vec) |
| 41 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 42 | typedef struct MemCopyInfo { |
| 43 | IntervalTreeNode itree; |
| 44 | QSIMPLEQ_ENTRY (MemCopyInfo) next; |
| 45 | TCGTemp *ts; |
| 46 | TCGType type; |
| 47 | } MemCopyInfo; |
| 48 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 49 | typedef struct TempOptInfo { |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 50 | bool is_const; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 51 | TCGTemp *prev_copy; |
| 52 | TCGTemp *next_copy; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 53 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy; |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 54 | uint64_t val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 55 | 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] | 56 | 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] | 57 | } TempOptInfo; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 58 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 59 | typedef struct OptContext { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 60 | TCGContext *tcg; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 61 | TCGOp *prev_mb; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 62 | TCGTempSet temps_used; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 63 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 64 | IntervalTreeRoot mem_copy; |
| 65 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_free; |
| 66 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 67 | /* In flight values from optimization. */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 68 | TCGType type; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 69 | } OptContext; |
| 70 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 71 | static inline TempOptInfo *ts_info(TCGTemp *ts) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 72 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 73 | return ts->state_ptr; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 76 | static inline TempOptInfo *arg_info(TCGArg arg) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 77 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 78 | return ts_info(arg_temp(arg)); |
| 79 | } |
| 80 | |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 81 | static inline bool ti_is_const(TempOptInfo *ti) |
| 82 | { |
| 83 | return ti->is_const; |
| 84 | } |
| 85 | |
| 86 | static inline uint64_t ti_const_val(TempOptInfo *ti) |
| 87 | { |
| 88 | return ti->val; |
| 89 | } |
| 90 | |
| 91 | static inline bool ti_is_const_val(TempOptInfo *ti, uint64_t val) |
| 92 | { |
| 93 | return ti_is_const(ti) && ti_const_val(ti) == val; |
| 94 | } |
| 95 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 96 | static inline bool ts_is_const(TCGTemp *ts) |
| 97 | { |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 98 | return ti_is_const(ts_info(ts)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 101 | static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val) |
| 102 | { |
Richard Henderson | e1b6c14 | 2024-12-22 10:26:14 -0800 | [diff] [blame] | 103 | return ti_is_const_val(ts_info(ts), val); |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 106 | static inline bool arg_is_const(TCGArg arg) |
| 107 | { |
| 108 | return ts_is_const(arg_temp(arg)); |
| 109 | } |
| 110 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 111 | static inline bool arg_is_const_val(TCGArg arg, uint64_t val) |
| 112 | { |
| 113 | return ts_is_const_val(arg_temp(arg), val); |
| 114 | } |
| 115 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 116 | static inline bool ts_is_copy(TCGTemp *ts) |
| 117 | { |
| 118 | return ts_info(ts)->next_copy != ts; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 121 | static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b) |
| 122 | { |
| 123 | return a->kind < b->kind ? b : a; |
| 124 | } |
| 125 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 126 | /* Initialize and activate a temporary. */ |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 127 | static void init_ts_info(OptContext *ctx, TCGTemp *ts) |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 128 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 129 | size_t idx = temp_idx(ts); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 130 | TempOptInfo *ti; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 131 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 132 | if (test_bit(idx, ctx->temps_used.l)) { |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 133 | return; |
| 134 | } |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 135 | set_bit(idx, ctx->temps_used.l); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 136 | |
| 137 | ti = ts->state_ptr; |
| 138 | if (ti == NULL) { |
| 139 | ti = tcg_malloc(sizeof(TempOptInfo)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 140 | ts->state_ptr = ti; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ti->next_copy = ts; |
| 144 | ti->prev_copy = ts; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 145 | QSIMPLEQ_INIT(&ti->mem_copy); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 146 | if (ts->kind == TEMP_CONST) { |
| 147 | ti->is_const = true; |
| 148 | ti->val = ts->val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 149 | ti->z_mask = ts->val; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 150 | ti->s_mask = INT64_MIN >> clrsb64(ts->val); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 151 | } else { |
| 152 | ti->is_const = false; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 153 | ti->z_mask = -1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 154 | ti->s_mask = 0; |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 158 | static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l) |
| 159 | { |
| 160 | IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l); |
| 161 | return r ? container_of(r, MemCopyInfo, itree) : NULL; |
| 162 | } |
| 163 | |
| 164 | static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l) |
| 165 | { |
| 166 | IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l); |
| 167 | return r ? container_of(r, MemCopyInfo, itree) : NULL; |
| 168 | } |
| 169 | |
| 170 | static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc) |
| 171 | { |
| 172 | TCGTemp *ts = mc->ts; |
| 173 | TempOptInfo *ti = ts_info(ts); |
| 174 | |
| 175 | interval_tree_remove(&mc->itree, &ctx->mem_copy); |
| 176 | QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next); |
| 177 | QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next); |
| 178 | } |
| 179 | |
| 180 | static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l) |
| 181 | { |
| 182 | while (true) { |
| 183 | MemCopyInfo *mc = mem_copy_first(ctx, s, l); |
| 184 | if (!mc) { |
| 185 | break; |
| 186 | } |
| 187 | remove_mem_copy(ctx, mc); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void remove_mem_copy_all(OptContext *ctx) |
| 192 | { |
| 193 | remove_mem_copy_in(ctx, 0, -1); |
| 194 | tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy)); |
| 195 | } |
| 196 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 197 | static TCGTemp *find_better_copy(TCGTemp *ts) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 198 | { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 199 | TCGTemp *i, *ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 200 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 201 | /* If this is already readonly, we can't do better. */ |
| 202 | if (temp_readonly(ts)) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 203 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 206 | ret = ts; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 207 | 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] | 208 | ret = cmp_better_copy(ret, i); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 209 | } |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 210 | return ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 213 | static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts) |
| 214 | { |
| 215 | TempOptInfo *si = ts_info(src_ts); |
| 216 | TempOptInfo *di = ts_info(dst_ts); |
| 217 | MemCopyInfo *mc; |
| 218 | |
| 219 | QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) { |
| 220 | tcg_debug_assert(mc->ts == src_ts); |
| 221 | mc->ts = dst_ts; |
| 222 | } |
| 223 | QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy); |
| 224 | } |
| 225 | |
| 226 | /* Reset TEMP's state, possibly removing the temp for the list of copies. */ |
| 227 | static void reset_ts(OptContext *ctx, TCGTemp *ts) |
| 228 | { |
| 229 | TempOptInfo *ti = ts_info(ts); |
| 230 | TCGTemp *pts = ti->prev_copy; |
| 231 | TCGTemp *nts = ti->next_copy; |
| 232 | TempOptInfo *pi = ts_info(pts); |
| 233 | TempOptInfo *ni = ts_info(nts); |
| 234 | |
| 235 | ni->prev_copy = ti->prev_copy; |
| 236 | pi->next_copy = ti->next_copy; |
| 237 | ti->next_copy = ts; |
| 238 | ti->prev_copy = ts; |
| 239 | ti->is_const = false; |
| 240 | ti->z_mask = -1; |
| 241 | ti->s_mask = 0; |
| 242 | |
| 243 | if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) { |
| 244 | if (ts == nts) { |
| 245 | /* Last temp copy being removed, the mem copies die. */ |
| 246 | MemCopyInfo *mc; |
| 247 | QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) { |
| 248 | interval_tree_remove(&mc->itree, &ctx->mem_copy); |
| 249 | } |
| 250 | QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy); |
| 251 | } else { |
| 252 | move_mem_copies(find_better_copy(nts), ts); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static void reset_temp(OptContext *ctx, TCGArg arg) |
| 258 | { |
| 259 | reset_ts(ctx, arg_temp(arg)); |
| 260 | } |
| 261 | |
| 262 | static void record_mem_copy(OptContext *ctx, TCGType type, |
| 263 | TCGTemp *ts, intptr_t start, intptr_t last) |
| 264 | { |
| 265 | MemCopyInfo *mc; |
| 266 | TempOptInfo *ti; |
| 267 | |
| 268 | mc = QSIMPLEQ_FIRST(&ctx->mem_free); |
| 269 | if (mc) { |
| 270 | QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next); |
| 271 | } else { |
| 272 | mc = tcg_malloc(sizeof(*mc)); |
| 273 | } |
| 274 | |
| 275 | memset(mc, 0, sizeof(*mc)); |
| 276 | mc->itree.start = start; |
| 277 | mc->itree.last = last; |
| 278 | mc->type = type; |
| 279 | interval_tree_insert(&mc->itree, &ctx->mem_copy); |
| 280 | |
| 281 | ts = find_better_copy(ts); |
| 282 | ti = ts_info(ts); |
| 283 | mc->ts = ts; |
| 284 | QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next); |
| 285 | } |
| 286 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 287 | static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 288 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 289 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 290 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 291 | if (ts1 == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 295 | if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 299 | for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { |
| 300 | if (i == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 308 | static bool args_are_copies(TCGArg arg1, TCGArg arg2) |
| 309 | { |
| 310 | return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); |
| 311 | } |
| 312 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 313 | static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s) |
| 314 | { |
| 315 | MemCopyInfo *mc; |
| 316 | |
| 317 | for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) { |
| 318 | if (mc->itree.start == s && mc->type == type) { |
| 319 | return find_better_copy(mc->ts); |
| 320 | } |
| 321 | } |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 325 | static TCGArg arg_new_constant(OptContext *ctx, uint64_t val) |
| 326 | { |
| 327 | TCGType type = ctx->type; |
| 328 | TCGTemp *ts; |
| 329 | |
| 330 | if (type == TCG_TYPE_I32) { |
| 331 | val = (int32_t)val; |
| 332 | } |
| 333 | |
| 334 | ts = tcg_constant_internal(type, val); |
| 335 | init_ts_info(ctx, ts); |
| 336 | |
| 337 | return temp_arg(ts); |
| 338 | } |
| 339 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 340 | static TCGArg arg_new_temp(OptContext *ctx) |
| 341 | { |
| 342 | TCGTemp *ts = tcg_temp_new_internal(ctx->type, TEMP_EBB); |
| 343 | init_ts_info(ctx, ts); |
| 344 | return temp_arg(ts); |
| 345 | } |
| 346 | |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 347 | static TCGOp *opt_insert_after(OptContext *ctx, TCGOp *op, |
| 348 | TCGOpcode opc, unsigned narg) |
| 349 | { |
Richard Henderson | cf5c9f6 | 2025-01-21 20:34:41 -0800 | [diff] [blame] | 350 | return tcg_op_insert_after(ctx->tcg, op, opc, ctx->type, narg); |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static TCGOp *opt_insert_before(OptContext *ctx, TCGOp *op, |
| 354 | TCGOpcode opc, unsigned narg) |
| 355 | { |
Richard Henderson | cf5c9f6 | 2025-01-21 20:34:41 -0800 | [diff] [blame] | 356 | return tcg_op_insert_before(ctx->tcg, op, opc, ctx->type, narg); |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 359 | 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] | 360 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 361 | TCGTemp *dst_ts = arg_temp(dst); |
| 362 | TCGTemp *src_ts = arg_temp(src); |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 363 | TempOptInfo *di; |
| 364 | TempOptInfo *si; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 365 | TCGOpcode new_op; |
| 366 | |
| 367 | if (ts_are_copies(dst_ts, src_ts)) { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 368 | tcg_op_remove(ctx->tcg, op); |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 369 | return true; |
Aurelien Jarno | 5365718 | 2015-06-04 21:53:25 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 372 | reset_ts(ctx, dst_ts); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 373 | di = ts_info(dst_ts); |
| 374 | si = ts_info(src_ts); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 375 | |
| 376 | switch (ctx->type) { |
| 377 | case TCG_TYPE_I32: |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 378 | case TCG_TYPE_I64: |
Richard Henderson | b570126 | 2024-12-28 15:58:24 -0800 | [diff] [blame] | 379 | new_op = INDEX_op_mov; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 380 | break; |
| 381 | case TCG_TYPE_V64: |
| 382 | case TCG_TYPE_V128: |
| 383 | case TCG_TYPE_V256: |
Richard Henderson | 4d87221 | 2025-01-02 19:43:06 -0800 | [diff] [blame] | 384 | /* TCGOP_TYPE and TCGOP_VECE remain unchanged. */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 385 | new_op = INDEX_op_mov_vec; |
| 386 | break; |
| 387 | default: |
| 388 | g_assert_not_reached(); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 389 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 390 | op->opc = new_op; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 391 | op->args[0] = dst; |
| 392 | op->args[1] = src; |
Richard Henderson | a62f6f5 | 2014-05-22 10:59:12 -0700 | [diff] [blame] | 393 | |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 394 | di->z_mask = si->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 395 | di->s_mask = si->s_mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 396 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 397 | if (src_ts->type == dst_ts->type) { |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 398 | TempOptInfo *ni = ts_info(si->next_copy); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 399 | |
| 400 | di->next_copy = si->next_copy; |
| 401 | di->prev_copy = src_ts; |
| 402 | ni->prev_copy = dst_ts; |
| 403 | si->next_copy = dst_ts; |
| 404 | di->is_const = si->is_const; |
| 405 | di->val = si->val; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 406 | |
| 407 | if (!QSIMPLEQ_EMPTY(&si->mem_copy) |
| 408 | && cmp_better_copy(src_ts, dst_ts) == dst_ts) { |
| 409 | move_mem_copies(dst_ts, src_ts); |
| 410 | } |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 411 | } |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 412 | return true; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 413 | } |
| 414 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 415 | static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op, |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 416 | TCGArg dst, uint64_t val) |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 417 | { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 418 | /* Convert movi to mov with constant temp. */ |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 419 | 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] | 420 | } |
| 421 | |
Richard Henderson | aa28c9e | 2025-01-07 10:36:24 -0800 | [diff] [blame] | 422 | static uint64_t do_constant_folding_2(TCGOpcode op, TCGType type, |
| 423 | uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 424 | { |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 425 | uint64_t l64, h64; |
| 426 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 427 | switch (op) { |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 428 | case INDEX_op_add: |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 429 | return x + y; |
| 430 | |
Richard Henderson | 60f34f5 | 2025-01-06 22:06:32 -0800 | [diff] [blame] | 431 | case INDEX_op_sub: |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 432 | return x - y; |
| 433 | |
Richard Henderson | d2c3eca | 2025-01-07 09:32:18 -0800 | [diff] [blame] | 434 | case INDEX_op_mul: |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 435 | return x * y; |
| 436 | |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 437 | case INDEX_op_and: |
| 438 | case INDEX_op_and_vec: |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 439 | return x & y; |
| 440 | |
Richard Henderson | 49bd751 | 2025-01-06 14:00:40 -0800 | [diff] [blame] | 441 | case INDEX_op_or: |
| 442 | case INDEX_op_or_vec: |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 443 | return x | y; |
| 444 | |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 445 | case INDEX_op_xor: |
| 446 | case INDEX_op_xor_vec: |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 447 | return x ^ y; |
| 448 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 449 | case INDEX_op_shl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 450 | return (uint32_t)x << (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 451 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 452 | case INDEX_op_shl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 453 | return (uint64_t)x << (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 454 | |
| 455 | case INDEX_op_shr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 456 | return (uint32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 457 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 458 | case INDEX_op_shr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 459 | return (uint64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 460 | |
| 461 | case INDEX_op_sar_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 462 | return (int32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 463 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 464 | case INDEX_op_sar_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 465 | return (int64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 466 | |
| 467 | case INDEX_op_rotr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 468 | return ror32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 469 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 470 | case INDEX_op_rotr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 471 | return ror64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 472 | |
| 473 | case INDEX_op_rotl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 474 | return rol32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 475 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 476 | case INDEX_op_rotl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 477 | return rol64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 478 | |
Richard Henderson | 5c62d37 | 2025-01-06 23:46:47 -0800 | [diff] [blame] | 479 | case INDEX_op_not: |
| 480 | case INDEX_op_not_vec: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 481 | return ~x; |
| 482 | |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 483 | case INDEX_op_neg: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 484 | return -x; |
| 485 | |
Richard Henderson | 46f96bf | 2025-01-06 12:37:02 -0800 | [diff] [blame] | 486 | case INDEX_op_andc: |
| 487 | case INDEX_op_andc_vec: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 488 | return x & ~y; |
| 489 | |
Richard Henderson | 6aba25e | 2025-01-06 14:46:26 -0800 | [diff] [blame] | 490 | case INDEX_op_orc: |
| 491 | case INDEX_op_orc_vec: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 492 | return x | ~y; |
| 493 | |
Richard Henderson | 5c0968a | 2025-01-06 15:47:53 -0800 | [diff] [blame] | 494 | case INDEX_op_eqv: |
| 495 | case INDEX_op_eqv_vec: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 496 | return ~(x ^ y); |
| 497 | |
Richard Henderson | 59379a4 | 2025-01-06 20:32:54 -0800 | [diff] [blame] | 498 | case INDEX_op_nand: |
| 499 | case INDEX_op_nand_vec: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 500 | return ~(x & y); |
| 501 | |
Richard Henderson | 3a8c4e9 | 2025-01-06 21:02:17 -0800 | [diff] [blame] | 502 | case INDEX_op_nor: |
| 503 | case INDEX_op_nor_vec: |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 504 | return ~(x | y); |
| 505 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 506 | case INDEX_op_clz_i32: |
| 507 | return (uint32_t)x ? clz32(x) : y; |
| 508 | |
| 509 | case INDEX_op_clz_i64: |
| 510 | return x ? clz64(x) : y; |
| 511 | |
| 512 | case INDEX_op_ctz_i32: |
| 513 | return (uint32_t)x ? ctz32(x) : y; |
| 514 | |
| 515 | case INDEX_op_ctz_i64: |
| 516 | return x ? ctz64(x) : y; |
| 517 | |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 518 | case INDEX_op_ctpop_i32: |
| 519 | return ctpop32(x); |
| 520 | |
| 521 | case INDEX_op_ctpop_i64: |
| 522 | return ctpop64(x); |
| 523 | |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 524 | CASE_OP_32_64(bswap16): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 525 | x = bswap16(x); |
| 526 | return y & TCG_BSWAP_OS ? (int16_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 527 | |
| 528 | CASE_OP_32_64(bswap32): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 529 | x = bswap32(x); |
| 530 | return y & TCG_BSWAP_OS ? (int32_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 531 | |
| 532 | case INDEX_op_bswap64_i64: |
| 533 | return bswap64(x); |
| 534 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 535 | case INDEX_op_ext_i32_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 536 | return (int32_t)x; |
| 537 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 538 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 539 | case INDEX_op_extrl_i64_i32: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 540 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 541 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 542 | case INDEX_op_extrh_i64_i32: |
| 543 | return (uint64_t)x >> 32; |
| 544 | |
Richard Henderson | aa28c9e | 2025-01-07 10:36:24 -0800 | [diff] [blame] | 545 | case INDEX_op_muluh: |
| 546 | if (type == TCG_TYPE_I32) { |
| 547 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; |
| 548 | } |
| 549 | mulu64(&l64, &h64, x, y); |
| 550 | return h64; |
| 551 | |
Richard Henderson | c742824 | 2025-01-07 11:19:29 -0800 | [diff] [blame] | 552 | case INDEX_op_mulsh: |
| 553 | if (type == TCG_TYPE_I32) { |
| 554 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; |
| 555 | } |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 556 | muls64(&l64, &h64, x, y); |
| 557 | return h64; |
| 558 | |
Richard Henderson | b2c514f | 2025-01-07 13:22:56 -0800 | [diff] [blame] | 559 | case INDEX_op_divs: |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 560 | /* Avoid crashing on divide by zero, otherwise undefined. */ |
Richard Henderson | b2c514f | 2025-01-07 13:22:56 -0800 | [diff] [blame] | 561 | if (type == TCG_TYPE_I32) { |
| 562 | return (int32_t)x / ((int32_t)y ? : 1); |
| 563 | } |
| 564 | return (int64_t)x / ((int64_t)y ? : 1); |
| 565 | |
Richard Henderson | 961b80a | 2025-01-07 14:27:19 -0800 | [diff] [blame] | 566 | case INDEX_op_divu: |
| 567 | if (type == TCG_TYPE_I32) { |
| 568 | return (uint32_t)x / ((uint32_t)y ? : 1); |
| 569 | } |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 570 | return (uint64_t)x / ((uint64_t)y ? : 1); |
| 571 | |
Richard Henderson | 9a6bc18 | 2025-01-07 19:00:51 -0800 | [diff] [blame] | 572 | case INDEX_op_rems: |
| 573 | if (type == TCG_TYPE_I32) { |
| 574 | return (int32_t)x % ((int32_t)y ? : 1); |
| 575 | } |
| 576 | return (int64_t)x % ((int64_t)y ? : 1); |
| 577 | |
Richard Henderson | cd9acd2 | 2025-01-07 20:25:14 -0800 | [diff] [blame^] | 578 | case INDEX_op_remu: |
| 579 | if (type == TCG_TYPE_I32) { |
| 580 | return (uint32_t)x % ((uint32_t)y ? : 1); |
| 581 | } |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 582 | return (uint64_t)x % ((uint64_t)y ? : 1); |
| 583 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 584 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 585 | g_assert_not_reached(); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 589 | static uint64_t do_constant_folding(TCGOpcode op, TCGType type, |
| 590 | uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 591 | { |
Richard Henderson | aa28c9e | 2025-01-07 10:36:24 -0800 | [diff] [blame] | 592 | uint64_t res = do_constant_folding_2(op, type, x, y); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 593 | if (type == TCG_TYPE_I32) { |
Aurelien Jarno | 29f3ff8 | 2015-07-10 18:03:31 +0200 | [diff] [blame] | 594 | res = (int32_t)res; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 595 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 596 | return res; |
| 597 | } |
| 598 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 599 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 600 | { |
| 601 | switch (c) { |
| 602 | case TCG_COND_EQ: |
| 603 | return x == y; |
| 604 | case TCG_COND_NE: |
| 605 | return x != y; |
| 606 | case TCG_COND_LT: |
| 607 | return (int32_t)x < (int32_t)y; |
| 608 | case TCG_COND_GE: |
| 609 | return (int32_t)x >= (int32_t)y; |
| 610 | case TCG_COND_LE: |
| 611 | return (int32_t)x <= (int32_t)y; |
| 612 | case TCG_COND_GT: |
| 613 | return (int32_t)x > (int32_t)y; |
| 614 | case TCG_COND_LTU: |
| 615 | return x < y; |
| 616 | case TCG_COND_GEU: |
| 617 | return x >= y; |
| 618 | case TCG_COND_LEU: |
| 619 | return x <= y; |
| 620 | case TCG_COND_GTU: |
| 621 | return x > y; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 622 | case TCG_COND_TSTEQ: |
| 623 | return (x & y) == 0; |
| 624 | case TCG_COND_TSTNE: |
| 625 | return (x & y) != 0; |
| 626 | case TCG_COND_ALWAYS: |
| 627 | case TCG_COND_NEVER: |
| 628 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 629 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 630 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) |
| 634 | { |
| 635 | switch (c) { |
| 636 | case TCG_COND_EQ: |
| 637 | return x == y; |
| 638 | case TCG_COND_NE: |
| 639 | return x != y; |
| 640 | case TCG_COND_LT: |
| 641 | return (int64_t)x < (int64_t)y; |
| 642 | case TCG_COND_GE: |
| 643 | return (int64_t)x >= (int64_t)y; |
| 644 | case TCG_COND_LE: |
| 645 | return (int64_t)x <= (int64_t)y; |
| 646 | case TCG_COND_GT: |
| 647 | return (int64_t)x > (int64_t)y; |
| 648 | case TCG_COND_LTU: |
| 649 | return x < y; |
| 650 | case TCG_COND_GEU: |
| 651 | return x >= y; |
| 652 | case TCG_COND_LEU: |
| 653 | return x <= y; |
| 654 | case TCG_COND_GTU: |
| 655 | return x > y; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 656 | case TCG_COND_TSTEQ: |
| 657 | return (x & y) == 0; |
| 658 | case TCG_COND_TSTNE: |
| 659 | return (x & y) != 0; |
| 660 | case TCG_COND_ALWAYS: |
| 661 | case TCG_COND_NEVER: |
| 662 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 663 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 664 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 667 | static int do_constant_folding_cond_eq(TCGCond c) |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 668 | { |
| 669 | switch (c) { |
| 670 | case TCG_COND_GT: |
| 671 | case TCG_COND_LTU: |
| 672 | case TCG_COND_LT: |
| 673 | case TCG_COND_GTU: |
| 674 | case TCG_COND_NE: |
| 675 | return 0; |
| 676 | case TCG_COND_GE: |
| 677 | case TCG_COND_GEU: |
| 678 | case TCG_COND_LE: |
| 679 | case TCG_COND_LEU: |
| 680 | case TCG_COND_EQ: |
| 681 | return 1; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 682 | case TCG_COND_TSTEQ: |
| 683 | case TCG_COND_TSTNE: |
| 684 | return -1; |
| 685 | case TCG_COND_ALWAYS: |
| 686 | case TCG_COND_NEVER: |
| 687 | break; |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 688 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 689 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Return -1 if the condition can't be simplified, |
| 694 | * and the result of the condition (0 or 1) if it can. |
| 695 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 696 | static int do_constant_folding_cond(TCGType type, TCGArg x, |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 697 | TCGArg y, TCGCond c) |
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 | if (arg_is_const(x) && arg_is_const(y)) { |
Alex Bennée | 9becc36 | 2022-02-09 11:21:42 +0000 | [diff] [blame] | 700 | uint64_t xv = arg_info(x)->val; |
| 701 | uint64_t yv = arg_info(y)->val; |
| 702 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 703 | switch (type) { |
| 704 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 705 | return do_constant_folding_cond_32(xv, yv, c); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 706 | case TCG_TYPE_I64: |
| 707 | return do_constant_folding_cond_64(xv, yv, c); |
| 708 | default: |
| 709 | /* Only scalar comparisons are optimizable */ |
| 710 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 711 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 712 | } else if (args_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 713 | return do_constant_folding_cond_eq(c); |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 714 | } else if (arg_is_const_val(y, 0)) { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 715 | switch (c) { |
| 716 | case TCG_COND_LTU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 717 | case TCG_COND_TSTNE: |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 718 | return 0; |
| 719 | case TCG_COND_GEU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 720 | case TCG_COND_TSTEQ: |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 721 | return 1; |
| 722 | default: |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 723 | return -1; |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 724 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 725 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 726 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 729 | /** |
| 730 | * swap_commutative: |
| 731 | * @dest: TCGArg of the destination argument, or NO_DEST. |
| 732 | * @p1: first paired argument |
| 733 | * @p2: second paired argument |
| 734 | * |
| 735 | * If *@p1 is a constant and *@p2 is not, swap. |
| 736 | * If *@p2 matches @dest, swap. |
| 737 | * Return true if a swap was performed. |
| 738 | */ |
| 739 | |
| 740 | #define NO_DEST temp_arg(NULL) |
| 741 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 742 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 743 | { |
| 744 | TCGArg a1 = *p1, a2 = *p2; |
| 745 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 746 | sum += arg_is_const(a1); |
| 747 | sum -= arg_is_const(a2); |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 748 | |
| 749 | /* Prefer the constant in second argument, and then the form |
| 750 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 751 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 752 | *p1 = a2; |
| 753 | *p2 = a1; |
| 754 | return true; |
| 755 | } |
| 756 | return false; |
| 757 | } |
| 758 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 759 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 760 | { |
| 761 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 762 | sum += arg_is_const(p1[0]); |
| 763 | sum += arg_is_const(p1[1]); |
| 764 | sum -= arg_is_const(p2[0]); |
| 765 | sum -= arg_is_const(p2[1]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 766 | if (sum > 0) { |
| 767 | TCGArg t; |
| 768 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 769 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 770 | return true; |
| 771 | } |
| 772 | return false; |
| 773 | } |
| 774 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 775 | /* |
| 776 | * Return -1 if the condition can't be simplified, |
| 777 | * and the result of the condition (0 or 1) if it can. |
| 778 | */ |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 779 | static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest, |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 780 | TCGArg *p1, TCGArg *p2, TCGArg *pcond) |
| 781 | { |
| 782 | TCGCond cond; |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 783 | TempOptInfo *i1; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 784 | bool swap; |
| 785 | int r; |
| 786 | |
| 787 | swap = swap_commutative(dest, p1, p2); |
| 788 | cond = *pcond; |
| 789 | if (swap) { |
| 790 | *pcond = cond = tcg_swap_cond(cond); |
| 791 | } |
| 792 | |
| 793 | r = do_constant_folding_cond(ctx->type, *p1, *p2, cond); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 794 | if (r >= 0) { |
| 795 | return r; |
| 796 | } |
| 797 | if (!is_tst_cond(cond)) { |
| 798 | return -1; |
| 799 | } |
| 800 | |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 801 | i1 = arg_info(*p1); |
| 802 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 803 | /* |
| 804 | * TSTNE x,x -> NE x,0 |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 805 | * TSTNE x,i -> NE x,0 if i includes all nonzero bits of x |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 806 | */ |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 807 | if (args_are_copies(*p1, *p2) || |
| 808 | (arg_is_const(*p2) && (i1->z_mask & ~arg_info(*p2)->val) == 0)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 809 | *p2 = arg_new_constant(ctx, 0); |
| 810 | *pcond = tcg_tst_eqne_cond(cond); |
| 811 | return -1; |
| 812 | } |
| 813 | |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 814 | /* TSTNE x,i -> LT x,0 if i only includes sign bit copies */ |
| 815 | if (arg_is_const(*p2) && (arg_info(*p2)->val & ~i1->s_mask) == 0) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 816 | *p2 = arg_new_constant(ctx, 0); |
| 817 | *pcond = tcg_tst_ltge_cond(cond); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 818 | return -1; |
| 819 | } |
| 820 | |
| 821 | /* Expand to AND with a temporary if no backend support. */ |
| 822 | if (!TCG_TARGET_HAS_tst) { |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 823 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 824 | TCGArg tmp = arg_new_temp(ctx); |
| 825 | |
| 826 | op2->args[0] = tmp; |
| 827 | op2->args[1] = *p1; |
| 828 | op2->args[2] = *p2; |
| 829 | |
| 830 | *p1 = tmp; |
| 831 | *p2 = arg_new_constant(ctx, 0); |
| 832 | *pcond = tcg_tst_eqne_cond(cond); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 833 | } |
| 834 | return -1; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 837 | static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args) |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 838 | { |
| 839 | TCGArg al, ah, bl, bh; |
| 840 | TCGCond c; |
| 841 | bool swap; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 842 | int r; |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 843 | |
| 844 | swap = swap_commutative2(args, args + 2); |
| 845 | c = args[4]; |
| 846 | if (swap) { |
| 847 | args[4] = c = tcg_swap_cond(c); |
| 848 | } |
| 849 | |
| 850 | al = args[0]; |
| 851 | ah = args[1]; |
| 852 | bl = args[2]; |
| 853 | bh = args[3]; |
| 854 | |
| 855 | if (arg_is_const(bl) && arg_is_const(bh)) { |
| 856 | tcg_target_ulong blv = arg_info(bl)->val; |
| 857 | tcg_target_ulong bhv = arg_info(bh)->val; |
| 858 | uint64_t b = deposit64(blv, 32, 32, bhv); |
| 859 | |
| 860 | if (arg_is_const(al) && arg_is_const(ah)) { |
| 861 | tcg_target_ulong alv = arg_info(al)->val; |
| 862 | tcg_target_ulong ahv = arg_info(ah)->val; |
| 863 | uint64_t a = deposit64(alv, 32, 32, ahv); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 864 | |
| 865 | r = do_constant_folding_cond_64(a, b, c); |
| 866 | if (r >= 0) { |
| 867 | return r; |
| 868 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 869 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 870 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 871 | if (b == 0) { |
| 872 | switch (c) { |
| 873 | case TCG_COND_LTU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 874 | case TCG_COND_TSTNE: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 875 | return 0; |
| 876 | case TCG_COND_GEU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 877 | case TCG_COND_TSTEQ: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 878 | return 1; |
| 879 | default: |
| 880 | break; |
| 881 | } |
| 882 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 883 | |
| 884 | /* TSTNE x,-1 -> NE x,0 */ |
| 885 | if (b == -1 && is_tst_cond(c)) { |
| 886 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 887 | args[4] = tcg_tst_eqne_cond(c); |
| 888 | return -1; |
| 889 | } |
| 890 | |
| 891 | /* TSTNE x,sign -> LT x,0 */ |
| 892 | if (b == INT64_MIN && is_tst_cond(c)) { |
| 893 | /* bl must be 0, so copy that to bh */ |
| 894 | args[3] = bl; |
| 895 | args[4] = tcg_tst_ltge_cond(c); |
| 896 | return -1; |
| 897 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 898 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 899 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 900 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 901 | r = do_constant_folding_cond_eq(c); |
| 902 | if (r >= 0) { |
| 903 | return r; |
| 904 | } |
| 905 | |
| 906 | /* TSTNE x,x -> NE x,0 */ |
| 907 | if (is_tst_cond(c)) { |
| 908 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 909 | args[4] = tcg_tst_eqne_cond(c); |
| 910 | return -1; |
| 911 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 912 | } |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 913 | |
| 914 | /* Expand to AND with a temporary if no backend support. */ |
| 915 | if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) { |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 916 | TCGOp *op1 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
| 917 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 918 | TCGArg t1 = arg_new_temp(ctx); |
| 919 | TCGArg t2 = arg_new_temp(ctx); |
| 920 | |
| 921 | op1->args[0] = t1; |
| 922 | op1->args[1] = al; |
| 923 | op1->args[2] = bl; |
| 924 | op2->args[0] = t2; |
| 925 | op2->args[1] = ah; |
| 926 | op2->args[2] = bh; |
| 927 | |
| 928 | args[0] = t1; |
| 929 | args[1] = t2; |
| 930 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 931 | args[4] = tcg_tst_eqne_cond(c); |
| 932 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 933 | return -1; |
| 934 | } |
| 935 | |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 936 | static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args) |
| 937 | { |
| 938 | for (int i = 0; i < nb_args; i++) { |
| 939 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 940 | init_ts_info(ctx, ts); |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 944 | static void copy_propagate(OptContext *ctx, TCGOp *op, |
| 945 | int nb_oargs, int nb_iargs) |
| 946 | { |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 947 | for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
| 948 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 949 | if (ts_is_copy(ts)) { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 950 | op->args[i] = temp_arg(find_better_copy(ts)); |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 955 | static void finish_bb(OptContext *ctx) |
| 956 | { |
| 957 | /* We only optimize memory barriers across basic blocks. */ |
| 958 | ctx->prev_mb = NULL; |
| 959 | } |
| 960 | |
| 961 | static void finish_ebb(OptContext *ctx) |
| 962 | { |
| 963 | finish_bb(ctx); |
| 964 | /* We only optimize across extended basic blocks. */ |
| 965 | memset(&ctx->temps_used, 0, sizeof(ctx->temps_used)); |
| 966 | remove_mem_copy_all(ctx); |
| 967 | } |
| 968 | |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 969 | static bool finish_folding(OptContext *ctx, TCGOp *op) |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 970 | { |
| 971 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 972 | int i, nb_oargs; |
| 973 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 974 | nb_oargs = def->nb_oargs; |
| 975 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 976 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 977 | reset_ts(ctx, ts); |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 978 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 979 | return true; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 982 | /* |
| 983 | * The fold_* functions return true when processing is complete, |
| 984 | * usually by folding the operation to a constant or to a copy, |
| 985 | * and calling tcg_opt_gen_{mov,movi}. They may do other things, |
| 986 | * like collect information about the value produced, for use in |
| 987 | * optimizing a subsequent operation. |
| 988 | * |
| 989 | * These first fold_* functions are all helpers, used by other |
| 990 | * folders for more specific operations. |
| 991 | */ |
| 992 | |
| 993 | static bool fold_const1(OptContext *ctx, TCGOp *op) |
| 994 | { |
| 995 | if (arg_is_const(op->args[1])) { |
| 996 | uint64_t t; |
| 997 | |
| 998 | t = arg_info(op->args[1])->val; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 999 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1000 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1001 | } |
| 1002 | return false; |
| 1003 | } |
| 1004 | |
| 1005 | static bool fold_const2(OptContext *ctx, TCGOp *op) |
| 1006 | { |
| 1007 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1008 | uint64_t t1 = arg_info(op->args[1])->val; |
| 1009 | uint64_t t2 = arg_info(op->args[2])->val; |
| 1010 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1011 | t1 = do_constant_folding(op->opc, ctx->type, t1, t2); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1012 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 1013 | } |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1017 | static bool fold_commutative(OptContext *ctx, TCGOp *op) |
| 1018 | { |
| 1019 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1023 | static bool fold_const2_commutative(OptContext *ctx, TCGOp *op) |
| 1024 | { |
| 1025 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 1026 | return fold_const2(ctx, op); |
| 1027 | } |
| 1028 | |
Richard Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1029 | /* |
| 1030 | * Record "zero" and "sign" masks for the single output of @op. |
| 1031 | * See TempOptInfo definition of z_mask and s_mask. |
| 1032 | * If z_mask allows, fold the output to constant zero. |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1033 | * The passed s_mask may be augmented by z_mask. |
Richard Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1034 | */ |
| 1035 | static bool fold_masks_zs(OptContext *ctx, TCGOp *op, |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1036 | uint64_t z_mask, int64_t s_mask) |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1037 | { |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1038 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 1039 | TCGTemp *ts; |
| 1040 | TempOptInfo *ti; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1041 | int rep; |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1042 | |
| 1043 | /* Only single-output opcodes are supported here. */ |
| 1044 | tcg_debug_assert(def->nb_oargs == 1); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1047 | * 32-bit ops generate 32-bit results, which for the purpose of |
| 1048 | * simplifying tcg are sign-extended. Certainly that's how we |
| 1049 | * represent our constants elsewhere. Note that the bits will |
| 1050 | * be reset properly for a 64-bit value when encountering the |
| 1051 | * type changing opcodes. |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1052 | */ |
| 1053 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1054 | z_mask = (int32_t)z_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1055 | s_mask |= INT32_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (z_mask == 0) { |
| 1059 | return tcg_opt_gen_movi(ctx, op, op->args[0], 0); |
| 1060 | } |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1061 | |
| 1062 | ts = arg_temp(op->args[0]); |
| 1063 | reset_ts(ctx, ts); |
| 1064 | |
| 1065 | ti = ts_info(ts); |
| 1066 | ti->z_mask = z_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1067 | |
| 1068 | /* Canonicalize s_mask and incorporate data from z_mask. */ |
| 1069 | rep = clz64(~s_mask); |
| 1070 | rep = MAX(rep, clz64(z_mask)); |
| 1071 | rep = MAX(rep - 1, 0); |
| 1072 | ti->s_mask = INT64_MIN >> rep; |
| 1073 | |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1074 | return true; |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1077 | static bool fold_masks_z(OptContext *ctx, TCGOp *op, uint64_t z_mask) |
| 1078 | { |
| 1079 | return fold_masks_zs(ctx, op, z_mask, 0); |
| 1080 | } |
| 1081 | |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1082 | static bool fold_masks_s(OptContext *ctx, TCGOp *op, uint64_t s_mask) |
| 1083 | { |
| 1084 | return fold_masks_zs(ctx, op, -1, s_mask); |
| 1085 | } |
| 1086 | |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1087 | /* |
| 1088 | * An "affected" mask bit is 0 if and only if the result is identical |
| 1089 | * to the first input. Thus if the entire mask is 0, the operation |
| 1090 | * is equivalent to a copy. |
| 1091 | */ |
| 1092 | static bool fold_affected_mask(OptContext *ctx, TCGOp *op, uint64_t a_mask) |
| 1093 | { |
| 1094 | if (ctx->type == TCG_TYPE_I32) { |
| 1095 | a_mask = (uint32_t)a_mask; |
| 1096 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1097 | if (a_mask == 0) { |
| 1098 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1099 | } |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Convert @op to NOT, if NOT is supported by the host. |
| 1105 | * Return true f the conversion is successful, which will still |
| 1106 | * indicate that the processing is complete. |
| 1107 | */ |
| 1108 | static bool fold_not(OptContext *ctx, TCGOp *op); |
| 1109 | static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx) |
| 1110 | { |
| 1111 | TCGOpcode not_op; |
| 1112 | bool have_not; |
| 1113 | |
| 1114 | switch (ctx->type) { |
| 1115 | case TCG_TYPE_I32: |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1116 | case TCG_TYPE_I64: |
Richard Henderson | 5c62d37 | 2025-01-06 23:46:47 -0800 | [diff] [blame] | 1117 | not_op = INDEX_op_not; |
| 1118 | have_not = tcg_op_supported(INDEX_op_not, ctx->type, 0); |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1119 | break; |
| 1120 | case TCG_TYPE_V64: |
| 1121 | case TCG_TYPE_V128: |
| 1122 | case TCG_TYPE_V256: |
| 1123 | not_op = INDEX_op_not_vec; |
| 1124 | have_not = TCG_TARGET_HAS_not_vec; |
| 1125 | break; |
| 1126 | default: |
| 1127 | g_assert_not_reached(); |
| 1128 | } |
| 1129 | if (have_not) { |
| 1130 | op->opc = not_op; |
| 1131 | op->args[1] = op->args[idx]; |
| 1132 | return fold_not(ctx, op); |
| 1133 | } |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1137 | /* If the binary operation has first argument @i, fold to @i. */ |
| 1138 | static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1139 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1140 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1141 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1142 | } |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1146 | /* If the binary operation has first argument @i, fold to NOT. */ |
| 1147 | static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1148 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1149 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1150 | return fold_to_not(ctx, op, 2); |
| 1151 | } |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1155 | /* If the binary operation has second argument @i, fold to @i. */ |
| 1156 | static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1157 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1158 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1159 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1160 | } |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1164 | /* If the binary operation has second argument @i, fold to identity. */ |
| 1165 | static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1166 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1167 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1168 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1169 | } |
| 1170 | return false; |
| 1171 | } |
| 1172 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1173 | /* If the binary operation has second argument @i, fold to NOT. */ |
| 1174 | static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1175 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1176 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1177 | return fold_to_not(ctx, op, 1); |
| 1178 | } |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1182 | /* If the binary operation has both arguments equal, fold to @i. */ |
| 1183 | static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1184 | { |
| 1185 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1186 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1187 | } |
| 1188 | return false; |
| 1189 | } |
| 1190 | |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1191 | /* If the binary operation has both arguments equal, fold to identity. */ |
| 1192 | static bool fold_xx_to_x(OptContext *ctx, TCGOp *op) |
| 1193 | { |
| 1194 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1195 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1196 | } |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1200 | /* |
| 1201 | * These outermost fold_<op> functions are sorted alphabetically. |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1202 | * |
| 1203 | * The ordering of the transformations should be: |
| 1204 | * 1) those that produce a constant |
| 1205 | * 2) those that produce a copy |
| 1206 | * 3) those that produce information about the result value. |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1207 | */ |
| 1208 | |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1209 | static bool fold_or(OptContext *ctx, TCGOp *op); |
| 1210 | static bool fold_orc(OptContext *ctx, TCGOp *op); |
| 1211 | static bool fold_xor(OptContext *ctx, TCGOp *op); |
| 1212 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1213 | static bool fold_add(OptContext *ctx, TCGOp *op) |
| 1214 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1215 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1216 | fold_xi_to_x(ctx, op, 0)) { |
| 1217 | return true; |
| 1218 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1219 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1222 | /* We cannot as yet do_constant_folding with vectors. */ |
| 1223 | static bool fold_add_vec(OptContext *ctx, TCGOp *op) |
| 1224 | { |
| 1225 | if (fold_commutative(ctx, op) || |
| 1226 | fold_xi_to_x(ctx, op, 0)) { |
| 1227 | return true; |
| 1228 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1229 | return finish_folding(ctx, op); |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1232 | static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1233 | { |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1234 | bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]); |
| 1235 | bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]); |
| 1236 | |
| 1237 | if (a_const && b_const) { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1238 | uint64_t al = arg_info(op->args[2])->val; |
| 1239 | uint64_t ah = arg_info(op->args[3])->val; |
| 1240 | uint64_t bl = arg_info(op->args[4])->val; |
| 1241 | uint64_t bh = arg_info(op->args[5])->val; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1242 | TCGArg rl, rh; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1243 | TCGOp *op2; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1244 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1245 | if (ctx->type == TCG_TYPE_I32) { |
| 1246 | uint64_t a = deposit64(al, 32, 32, ah); |
| 1247 | uint64_t b = deposit64(bl, 32, 32, bh); |
| 1248 | |
| 1249 | if (add) { |
| 1250 | a += b; |
| 1251 | } else { |
| 1252 | a -= b; |
| 1253 | } |
| 1254 | |
| 1255 | al = sextract64(a, 0, 32); |
| 1256 | ah = sextract64(a, 32, 32); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1257 | } else { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1258 | Int128 a = int128_make128(al, ah); |
| 1259 | Int128 b = int128_make128(bl, bh); |
| 1260 | |
| 1261 | if (add) { |
| 1262 | a = int128_add(a, b); |
| 1263 | } else { |
| 1264 | a = int128_sub(a, b); |
| 1265 | } |
| 1266 | |
| 1267 | al = int128_getlo(a); |
| 1268 | ah = int128_gethi(a); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | rl = op->args[0]; |
| 1272 | rh = op->args[1]; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 1275 | op2 = opt_insert_before(ctx, op, 0, 2); |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1276 | |
| 1277 | tcg_opt_gen_movi(ctx, op, rl, al); |
| 1278 | tcg_opt_gen_movi(ctx, op2, rh, ah); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1281 | |
| 1282 | /* Fold sub2 r,x,i to add2 r,x,-i */ |
| 1283 | if (!add && b_const) { |
| 1284 | uint64_t bl = arg_info(op->args[4])->val; |
| 1285 | uint64_t bh = arg_info(op->args[5])->val; |
| 1286 | |
| 1287 | /* Negate the two parts without assembling and disassembling. */ |
| 1288 | bl = -bl; |
| 1289 | bh = ~bh + !bl; |
| 1290 | |
| 1291 | op->opc = (ctx->type == TCG_TYPE_I32 |
| 1292 | ? INDEX_op_add2_i32 : INDEX_op_add2_i64); |
| 1293 | op->args[4] = arg_new_constant(ctx, bl); |
| 1294 | op->args[5] = arg_new_constant(ctx, bh); |
| 1295 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1296 | return finish_folding(ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1299 | static bool fold_add2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1300 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1301 | /* Note that the high and low parts may be independently swapped. */ |
| 1302 | swap_commutative(op->args[0], &op->args[2], &op->args[4]); |
| 1303 | swap_commutative(op->args[1], &op->args[3], &op->args[5]); |
| 1304 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1305 | return fold_addsub2(ctx, op, true); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1308 | static bool fold_and(OptContext *ctx, TCGOp *op) |
| 1309 | { |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1310 | uint64_t z1, z2, z_mask, s_mask; |
| 1311 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1312 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1313 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1314 | fold_xi_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1315 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1316 | fold_xx_to_x(ctx, op)) { |
| 1317 | return true; |
| 1318 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1319 | |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1320 | t1 = arg_info(op->args[1]); |
| 1321 | t2 = arg_info(op->args[2]); |
| 1322 | z1 = t1->z_mask; |
| 1323 | z2 = t2->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1324 | |
| 1325 | /* |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1326 | * Known-zeros does not imply known-ones. Therefore unless |
| 1327 | * arg2 is constant, we can't infer affected bits from it. |
| 1328 | */ |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1329 | if (ti_is_const(t2) && fold_affected_mask(ctx, op, z1 & ~z2)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1330 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1333 | z_mask = z1 & z2; |
| 1334 | |
| 1335 | /* |
| 1336 | * Sign repetitions are perforce all identical, whether they are 1 or 0. |
| 1337 | * Bitwise operations preserve the relative quantity of the repetitions. |
| 1338 | */ |
| 1339 | s_mask = t1->s_mask & t2->s_mask; |
| 1340 | |
| 1341 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | static bool fold_andc(OptContext *ctx, TCGOp *op) |
| 1345 | { |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1346 | uint64_t z_mask, s_mask; |
| 1347 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1348 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1349 | if (fold_const2(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1350 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1351 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1352 | fold_ix_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1353 | return true; |
| 1354 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1355 | |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1356 | t1 = arg_info(op->args[1]); |
| 1357 | t2 = arg_info(op->args[2]); |
| 1358 | z_mask = t1->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1359 | |
Richard Henderson | 899281c | 2023-11-15 11:18:55 -0800 | [diff] [blame] | 1360 | if (ti_is_const(t2)) { |
| 1361 | /* Fold andc r,x,i to and r,x,~i. */ |
| 1362 | switch (ctx->type) { |
| 1363 | case TCG_TYPE_I32: |
| 1364 | case TCG_TYPE_I64: |
| 1365 | op->opc = INDEX_op_and; |
| 1366 | break; |
| 1367 | case TCG_TYPE_V64: |
| 1368 | case TCG_TYPE_V128: |
| 1369 | case TCG_TYPE_V256: |
| 1370 | op->opc = INDEX_op_and_vec; |
| 1371 | break; |
| 1372 | default: |
| 1373 | g_assert_not_reached(); |
| 1374 | } |
| 1375 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 1376 | return fold_and(ctx, op); |
| 1377 | } |
| 1378 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * Known-zeros does not imply known-ones. Therefore unless |
| 1381 | * arg2 is constant, we can't infer anything from it. |
| 1382 | */ |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1383 | if (ti_is_const(t2)) { |
| 1384 | uint64_t v2 = ti_const_val(t2); |
| 1385 | if (fold_affected_mask(ctx, op, z_mask & v2)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1386 | return true; |
| 1387 | } |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1388 | z_mask &= ~v2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1389 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1390 | |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1391 | s_mask = t1->s_mask & t2->s_mask; |
| 1392 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1395 | static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op) |
| 1396 | { |
| 1397 | /* If true and false values are the same, eliminate the cmp. */ |
| 1398 | if (args_are_copies(op->args[2], op->args[3])) { |
| 1399 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1400 | } |
| 1401 | |
| 1402 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { |
| 1403 | uint64_t tv = arg_info(op->args[2])->val; |
| 1404 | uint64_t fv = arg_info(op->args[3])->val; |
| 1405 | |
| 1406 | if (tv == -1 && fv == 0) { |
| 1407 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1408 | } |
| 1409 | if (tv == 0 && fv == -1) { |
| 1410 | if (TCG_TARGET_HAS_not_vec) { |
| 1411 | op->opc = INDEX_op_not_vec; |
| 1412 | return fold_not(ctx, op); |
| 1413 | } else { |
| 1414 | op->opc = INDEX_op_xor_vec; |
| 1415 | op->args[2] = arg_new_constant(ctx, -1); |
| 1416 | return fold_xor(ctx, op); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | if (arg_is_const(op->args[2])) { |
| 1421 | uint64_t tv = arg_info(op->args[2])->val; |
| 1422 | if (tv == -1) { |
| 1423 | op->opc = INDEX_op_or_vec; |
| 1424 | op->args[2] = op->args[3]; |
| 1425 | return fold_or(ctx, op); |
| 1426 | } |
| 1427 | if (tv == 0 && TCG_TARGET_HAS_andc_vec) { |
| 1428 | op->opc = INDEX_op_andc_vec; |
| 1429 | op->args[2] = op->args[1]; |
| 1430 | op->args[1] = op->args[3]; |
| 1431 | return fold_andc(ctx, op); |
| 1432 | } |
| 1433 | } |
| 1434 | if (arg_is_const(op->args[3])) { |
| 1435 | uint64_t fv = arg_info(op->args[3])->val; |
| 1436 | if (fv == 0) { |
| 1437 | op->opc = INDEX_op_and_vec; |
| 1438 | return fold_and(ctx, op); |
| 1439 | } |
| 1440 | if (fv == -1 && TCG_TARGET_HAS_orc_vec) { |
| 1441 | op->opc = INDEX_op_orc_vec; |
| 1442 | op->args[2] = op->args[1]; |
| 1443 | op->args[1] = op->args[3]; |
| 1444 | return fold_orc(ctx, op); |
| 1445 | } |
| 1446 | } |
| 1447 | return finish_folding(ctx, op); |
| 1448 | } |
| 1449 | |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1450 | static bool fold_brcond(OptContext *ctx, TCGOp *op) |
| 1451 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1452 | 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] | 1453 | &op->args[1], &op->args[2]); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1454 | if (i == 0) { |
| 1455 | tcg_op_remove(ctx->tcg, op); |
| 1456 | return true; |
| 1457 | } |
| 1458 | if (i > 0) { |
| 1459 | op->opc = INDEX_op_br; |
| 1460 | op->args[0] = op->args[3]; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1461 | finish_ebb(ctx); |
| 1462 | } else { |
| 1463 | finish_bb(ctx); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1464 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1465 | return true; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1468 | static bool fold_brcond2(OptContext *ctx, TCGOp *op) |
| 1469 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1470 | TCGCond cond; |
| 1471 | TCGArg label; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1472 | int i, inv = 0; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1473 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1474 | i = do_constant_folding_cond2(ctx, op, &op->args[0]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1475 | cond = op->args[4]; |
| 1476 | label = op->args[5]; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1477 | if (i >= 0) { |
| 1478 | goto do_brcond_const; |
| 1479 | } |
| 1480 | |
| 1481 | switch (cond) { |
| 1482 | case TCG_COND_LT: |
| 1483 | case TCG_COND_GE: |
| 1484 | /* |
| 1485 | * Simplify LT/GE comparisons vs zero to a single compare |
| 1486 | * vs the high word of the input. |
| 1487 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1488 | if (arg_is_const_val(op->args[2], 0) && |
| 1489 | arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1490 | goto do_brcond_high; |
| 1491 | } |
| 1492 | break; |
| 1493 | |
| 1494 | case TCG_COND_NE: |
| 1495 | inv = 1; |
| 1496 | QEMU_FALLTHROUGH; |
| 1497 | case TCG_COND_EQ: |
| 1498 | /* |
| 1499 | * Simplify EQ/NE comparisons where one of the pairs |
| 1500 | * can be simplified. |
| 1501 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1502 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1503 | op->args[2], cond); |
| 1504 | switch (i ^ inv) { |
| 1505 | case 0: |
| 1506 | goto do_brcond_const; |
| 1507 | case 1: |
| 1508 | goto do_brcond_high; |
| 1509 | } |
| 1510 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1511 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1512 | op->args[3], cond); |
| 1513 | switch (i ^ inv) { |
| 1514 | case 0: |
| 1515 | goto do_brcond_const; |
| 1516 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1517 | goto do_brcond_low; |
| 1518 | } |
| 1519 | break; |
| 1520 | |
| 1521 | case TCG_COND_TSTEQ: |
| 1522 | case TCG_COND_TSTNE: |
| 1523 | if (arg_is_const_val(op->args[2], 0)) { |
| 1524 | goto do_brcond_high; |
| 1525 | } |
| 1526 | if (arg_is_const_val(op->args[3], 0)) { |
| 1527 | goto do_brcond_low; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1528 | } |
| 1529 | break; |
| 1530 | |
| 1531 | default: |
| 1532 | break; |
| 1533 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1534 | do_brcond_low: |
| 1535 | op->opc = INDEX_op_brcond_i32; |
| 1536 | op->args[1] = op->args[2]; |
| 1537 | op->args[2] = cond; |
| 1538 | op->args[3] = label; |
| 1539 | return fold_brcond(ctx, op); |
| 1540 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1541 | do_brcond_high: |
| 1542 | op->opc = INDEX_op_brcond_i32; |
| 1543 | op->args[0] = op->args[1]; |
| 1544 | op->args[1] = op->args[3]; |
| 1545 | op->args[2] = cond; |
| 1546 | op->args[3] = label; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1547 | return fold_brcond(ctx, op); |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1548 | |
| 1549 | do_brcond_const: |
| 1550 | if (i == 0) { |
| 1551 | tcg_op_remove(ctx->tcg, op); |
| 1552 | return true; |
| 1553 | } |
| 1554 | op->opc = INDEX_op_br; |
| 1555 | op->args[0] = label; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1556 | finish_ebb(ctx); |
| 1557 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1558 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1559 | |
| 1560 | finish_bb(ctx); |
| 1561 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1564 | static bool fold_bswap(OptContext *ctx, TCGOp *op) |
| 1565 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1566 | uint64_t z_mask, s_mask, sign; |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1567 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1568 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1569 | if (ti_is_const(t1)) { |
| 1570 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1571 | do_constant_folding(op->opc, ctx->type, |
| 1572 | ti_const_val(t1), |
| 1573 | op->args[2])); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1574 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1575 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1576 | z_mask = t1->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1577 | switch (op->opc) { |
| 1578 | case INDEX_op_bswap16_i32: |
| 1579 | case INDEX_op_bswap16_i64: |
| 1580 | z_mask = bswap16(z_mask); |
| 1581 | sign = INT16_MIN; |
| 1582 | break; |
| 1583 | case INDEX_op_bswap32_i32: |
| 1584 | case INDEX_op_bswap32_i64: |
| 1585 | z_mask = bswap32(z_mask); |
| 1586 | sign = INT32_MIN; |
| 1587 | break; |
| 1588 | case INDEX_op_bswap64_i64: |
| 1589 | z_mask = bswap64(z_mask); |
| 1590 | sign = INT64_MIN; |
| 1591 | break; |
| 1592 | default: |
| 1593 | g_assert_not_reached(); |
| 1594 | } |
| 1595 | |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1596 | s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1597 | switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) { |
| 1598 | case TCG_BSWAP_OZ: |
| 1599 | break; |
| 1600 | case TCG_BSWAP_OS: |
| 1601 | /* If the sign bit may be 1, force all the bits above to 1. */ |
| 1602 | if (z_mask & sign) { |
| 1603 | z_mask |= sign; |
| 1604 | } |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1605 | /* The value and therefore s_mask is explicitly sign-extended. */ |
| 1606 | s_mask = sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1607 | break; |
| 1608 | default: |
| 1609 | /* The high bits are undefined: force all bits above the sign to 1. */ |
| 1610 | z_mask |= sign << 1; |
| 1611 | break; |
| 1612 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1613 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1614 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1617 | static bool fold_call(OptContext *ctx, TCGOp *op) |
| 1618 | { |
| 1619 | TCGContext *s = ctx->tcg; |
| 1620 | int nb_oargs = TCGOP_CALLO(op); |
| 1621 | int nb_iargs = TCGOP_CALLI(op); |
| 1622 | int flags, i; |
| 1623 | |
| 1624 | init_arguments(ctx, op, nb_oargs + nb_iargs); |
| 1625 | copy_propagate(ctx, op, nb_oargs, nb_iargs); |
| 1626 | |
| 1627 | /* If the function reads or writes globals, reset temp data. */ |
| 1628 | flags = tcg_call_flags(op); |
| 1629 | if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { |
| 1630 | int nb_globals = s->nb_globals; |
| 1631 | |
| 1632 | for (i = 0; i < nb_globals; i++) { |
| 1633 | if (test_bit(i, ctx->temps_used.l)) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1634 | reset_ts(ctx, &ctx->tcg->temps[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 1639 | /* If the function has side effects, reset mem data. */ |
| 1640 | if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) { |
| 1641 | remove_mem_copy_all(ctx); |
| 1642 | } |
| 1643 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1644 | /* Reset temp data for outputs. */ |
| 1645 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1646 | reset_temp(ctx, op->args[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* Stop optimizing MB across calls. */ |
| 1650 | ctx->prev_mb = NULL; |
| 1651 | return true; |
| 1652 | } |
| 1653 | |
Richard Henderson | 29f6586 | 2024-12-09 14:09:49 -0600 | [diff] [blame] | 1654 | static bool fold_cmp_vec(OptContext *ctx, TCGOp *op) |
| 1655 | { |
| 1656 | /* Canonicalize the comparison to put immediate second. */ |
| 1657 | if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { |
| 1658 | op->args[3] = tcg_swap_cond(op->args[3]); |
| 1659 | } |
| 1660 | return finish_folding(ctx, op); |
| 1661 | } |
| 1662 | |
| 1663 | static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op) |
| 1664 | { |
| 1665 | /* If true and false values are the same, eliminate the cmp. */ |
| 1666 | if (args_are_copies(op->args[3], op->args[4])) { |
| 1667 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); |
| 1668 | } |
| 1669 | |
| 1670 | /* Canonicalize the comparison to put immediate second. */ |
| 1671 | if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { |
| 1672 | op->args[5] = tcg_swap_cond(op->args[5]); |
| 1673 | } |
| 1674 | /* |
| 1675 | * Canonicalize the "false" input reg to match the destination, |
| 1676 | * so that the tcg backend can implement "move if true". |
| 1677 | */ |
| 1678 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
| 1679 | op->args[5] = tcg_invert_cond(op->args[5]); |
| 1680 | } |
| 1681 | return finish_folding(ctx, op); |
| 1682 | } |
| 1683 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1684 | static bool fold_count_zeros(OptContext *ctx, TCGOp *op) |
| 1685 | { |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1686 | uint64_t z_mask, s_mask; |
| 1687 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 1688 | TempOptInfo *t2 = arg_info(op->args[2]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1689 | |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1690 | if (ti_is_const(t1)) { |
| 1691 | uint64_t t = ti_const_val(t1); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1692 | |
| 1693 | if (t != 0) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1694 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1695 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1696 | } |
| 1697 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1698 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1699 | |
| 1700 | switch (ctx->type) { |
| 1701 | case TCG_TYPE_I32: |
| 1702 | z_mask = 31; |
| 1703 | break; |
| 1704 | case TCG_TYPE_I64: |
| 1705 | z_mask = 63; |
| 1706 | break; |
| 1707 | default: |
| 1708 | g_assert_not_reached(); |
| 1709 | } |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1710 | s_mask = ~z_mask; |
| 1711 | z_mask |= t2->z_mask; |
| 1712 | s_mask &= t2->s_mask; |
| 1713 | |
| 1714 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1717 | static bool fold_ctpop(OptContext *ctx, TCGOp *op) |
| 1718 | { |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1719 | uint64_t z_mask; |
| 1720 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1721 | if (fold_const1(ctx, op)) { |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
| 1725 | switch (ctx->type) { |
| 1726 | case TCG_TYPE_I32: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1727 | z_mask = 32 | 31; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1728 | break; |
| 1729 | case TCG_TYPE_I64: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1730 | z_mask = 64 | 63; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | default: |
| 1733 | g_assert_not_reached(); |
| 1734 | } |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1735 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1738 | static bool fold_deposit(OptContext *ctx, TCGOp *op) |
| 1739 | { |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1740 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 1741 | TempOptInfo *t2 = arg_info(op->args[2]); |
| 1742 | int ofs = op->args[3]; |
| 1743 | int len = op->args[4]; |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1744 | int width = 8 * tcg_type_size(ctx->type); |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1745 | uint64_t z_mask, s_mask; |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1746 | |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1747 | if (ti_is_const(t1) && ti_is_const(t2)) { |
| 1748 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1749 | deposit64(ti_const_val(t1), ofs, len, |
| 1750 | ti_const_val(t2))); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1751 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1752 | |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1753 | /* Inserting a value into zero at offset 0. */ |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1754 | if (ti_is_const_val(t1, 0) && ofs == 0) { |
| 1755 | uint64_t mask = MAKE_64BIT_MASK(0, len); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1756 | |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1757 | op->opc = INDEX_op_and; |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1758 | op->args[1] = op->args[2]; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1759 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1760 | return fold_and(ctx, op); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /* Inserting zero into a value. */ |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1764 | if (ti_is_const_val(t2, 0)) { |
| 1765 | uint64_t mask = deposit64(-1, ofs, len, 0); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1766 | |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1767 | op->opc = INDEX_op_and; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1768 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1769 | return fold_and(ctx, op); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1772 | /* The s_mask from the top portion of the deposit is still valid. */ |
| 1773 | if (ofs + len == width) { |
| 1774 | s_mask = t2->s_mask << ofs; |
| 1775 | } else { |
| 1776 | s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len); |
| 1777 | } |
| 1778 | |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1779 | z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask); |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1780 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1783 | static bool fold_divide(OptContext *ctx, TCGOp *op) |
| 1784 | { |
Richard Henderson | 2f9d9a3 | 2021-10-25 11:30:14 -0700 | [diff] [blame] | 1785 | if (fold_const2(ctx, op) || |
| 1786 | fold_xi_to_x(ctx, op, 1)) { |
| 1787 | return true; |
| 1788 | } |
Richard Henderson | 3d5ec80 | 2024-12-08 19:59:15 -0600 | [diff] [blame] | 1789 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1792 | static bool fold_dup(OptContext *ctx, TCGOp *op) |
| 1793 | { |
| 1794 | if (arg_is_const(op->args[1])) { |
| 1795 | uint64_t t = arg_info(op->args[1])->val; |
| 1796 | t = dup_const(TCGOP_VECE(op), t); |
| 1797 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1798 | } |
Richard Henderson | e089d69 | 2024-12-08 20:00:51 -0600 | [diff] [blame] | 1799 | return finish_folding(ctx, op); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | static bool fold_dup2(OptContext *ctx, TCGOp *op) |
| 1803 | { |
| 1804 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1805 | uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32, |
| 1806 | arg_info(op->args[2])->val); |
| 1807 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1808 | } |
| 1809 | |
| 1810 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1811 | op->opc = INDEX_op_dup_vec; |
| 1812 | TCGOP_VECE(op) = MO_32; |
| 1813 | } |
Richard Henderson | e089d69 | 2024-12-08 20:00:51 -0600 | [diff] [blame] | 1814 | return finish_folding(ctx, op); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1817 | static bool fold_eqv(OptContext *ctx, TCGOp *op) |
| 1818 | { |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1819 | uint64_t s_mask; |
Richard Henderson | 46c68d7 | 2023-11-15 11:51:28 -0800 | [diff] [blame] | 1820 | TempOptInfo *t1, *t2; |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1821 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1822 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1823 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1824 | fold_xi_to_not(ctx, op, 0)) { |
| 1825 | return true; |
| 1826 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1827 | |
Richard Henderson | 46c68d7 | 2023-11-15 11:51:28 -0800 | [diff] [blame] | 1828 | t2 = arg_info(op->args[2]); |
| 1829 | if (ti_is_const(t2)) { |
| 1830 | /* Fold eqv r,x,i to xor r,x,~i. */ |
| 1831 | switch (ctx->type) { |
| 1832 | case TCG_TYPE_I32: |
| 1833 | case TCG_TYPE_I64: |
| 1834 | op->opc = INDEX_op_xor; |
| 1835 | break; |
| 1836 | case TCG_TYPE_V64: |
| 1837 | case TCG_TYPE_V128: |
| 1838 | case TCG_TYPE_V256: |
| 1839 | op->opc = INDEX_op_xor_vec; |
| 1840 | break; |
| 1841 | default: |
| 1842 | g_assert_not_reached(); |
| 1843 | } |
| 1844 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 1845 | return fold_xor(ctx, op); |
| 1846 | } |
| 1847 | |
| 1848 | t1 = arg_info(op->args[1]); |
| 1849 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1850 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1853 | static bool fold_extract(OptContext *ctx, TCGOp *op) |
| 1854 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1855 | uint64_t z_mask_old, z_mask; |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1856 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1857 | int pos = op->args[2]; |
| 1858 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1859 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1860 | if (ti_is_const(t1)) { |
| 1861 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1862 | extract64(ti_const_val(t1), pos, len)); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1863 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1864 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1865 | z_mask_old = t1->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1866 | z_mask = extract64(z_mask_old, pos, len); |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1867 | if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) { |
| 1868 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1869 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1870 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1871 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1874 | static bool fold_extract2(OptContext *ctx, TCGOp *op) |
| 1875 | { |
| 1876 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1877 | uint64_t v1 = arg_info(op->args[1])->val; |
| 1878 | uint64_t v2 = arg_info(op->args[2])->val; |
| 1879 | int shr = op->args[3]; |
| 1880 | |
| 1881 | if (op->opc == INDEX_op_extract2_i64) { |
| 1882 | v1 >>= shr; |
| 1883 | v2 <<= 64 - shr; |
| 1884 | } else { |
| 1885 | v1 = (uint32_t)v1 >> shr; |
Richard Henderson | 225bec0 | 2021-11-09 23:17:59 +0100 | [diff] [blame] | 1886 | v2 = (uint64_t)((int32_t)v2 << (32 - shr)); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1887 | } |
| 1888 | return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); |
| 1889 | } |
Richard Henderson | c9df99e | 2024-12-08 20:06:42 -0600 | [diff] [blame] | 1890 | return finish_folding(ctx, op); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1893 | static bool fold_exts(OptContext *ctx, TCGOp *op) |
| 1894 | { |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 1895 | uint64_t s_mask, z_mask; |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 1896 | TempOptInfo *t1; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1897 | |
| 1898 | if (fold_const1(ctx, op)) { |
| 1899 | return true; |
| 1900 | } |
| 1901 | |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 1902 | t1 = arg_info(op->args[1]); |
| 1903 | z_mask = t1->z_mask; |
| 1904 | s_mask = t1->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1905 | |
| 1906 | switch (op->opc) { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1907 | case INDEX_op_ext_i32_i64: |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 1908 | s_mask |= INT32_MIN; |
| 1909 | z_mask = (int32_t)z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1910 | break; |
| 1911 | default: |
| 1912 | g_assert_not_reached(); |
| 1913 | } |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 1914 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static bool fold_extu(OptContext *ctx, TCGOp *op) |
| 1918 | { |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 1919 | uint64_t z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1920 | |
| 1921 | if (fold_const1(ctx, op)) { |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 1925 | z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1926 | switch (op->opc) { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1927 | case INDEX_op_extrl_i64_i32: |
| 1928 | case INDEX_op_extu_i32_i64: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1929 | z_mask = (uint32_t)z_mask; |
| 1930 | break; |
| 1931 | case INDEX_op_extrh_i64_i32: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1932 | z_mask >>= 32; |
| 1933 | break; |
| 1934 | default: |
| 1935 | g_assert_not_reached(); |
| 1936 | } |
Richard Henderson | 08abe29 | 2024-12-08 20:11:44 -0600 | [diff] [blame] | 1937 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1940 | static bool fold_mb(OptContext *ctx, TCGOp *op) |
| 1941 | { |
| 1942 | /* Eliminate duplicate and redundant fence instructions. */ |
| 1943 | if (ctx->prev_mb) { |
| 1944 | /* |
| 1945 | * Merge two barriers of the same type into one, |
| 1946 | * or a weaker barrier into a stronger one, |
| 1947 | * or two weaker barriers into a stronger one. |
| 1948 | * mb X; mb Y => mb X|Y |
| 1949 | * mb; strl => mb; st |
| 1950 | * ldaq; mb => ld; mb |
| 1951 | * ldaq; strl => ld; mb; st |
| 1952 | * Other combinations are also merged into a strong |
| 1953 | * barrier. This is stricter than specified but for |
| 1954 | * the purposes of TCG is better than not optimizing. |
| 1955 | */ |
| 1956 | ctx->prev_mb->args[0] |= op->args[0]; |
| 1957 | tcg_op_remove(ctx->tcg, op); |
| 1958 | } else { |
| 1959 | ctx->prev_mb = op; |
| 1960 | } |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1964 | static bool fold_mov(OptContext *ctx, TCGOp *op) |
| 1965 | { |
| 1966 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1967 | } |
| 1968 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1969 | static bool fold_movcond(OptContext *ctx, TCGOp *op) |
| 1970 | { |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 1971 | uint64_t z_mask, s_mask; |
| 1972 | TempOptInfo *tt, *ft; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1973 | int i; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1974 | |
Richard Henderson | 141125e | 2024-09-06 21:00:10 -0700 | [diff] [blame] | 1975 | /* If true and false values are the same, eliminate the cmp. */ |
| 1976 | if (args_are_copies(op->args[3], op->args[4])) { |
| 1977 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); |
| 1978 | } |
| 1979 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1980 | /* |
| 1981 | * Canonicalize the "false" input reg to match the destination reg so |
| 1982 | * that the tcg backend can implement a "move if true" operation. |
| 1983 | */ |
| 1984 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1985 | op->args[5] = tcg_invert_cond(op->args[5]); |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1988 | i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 1989 | &op->args[2], &op->args[5]); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1990 | if (i >= 0) { |
| 1991 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); |
| 1992 | } |
| 1993 | |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 1994 | tt = arg_info(op->args[3]); |
| 1995 | ft = arg_info(op->args[4]); |
| 1996 | z_mask = tt->z_mask | ft->z_mask; |
| 1997 | s_mask = tt->s_mask & ft->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1998 | |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 1999 | if (ti_is_const(tt) && ti_is_const(ft)) { |
| 2000 | uint64_t tv = ti_const_val(tt); |
| 2001 | uint64_t fv = ti_const_val(ft); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2002 | TCGOpcode opc, negopc = 0; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2003 | TCGCond cond = op->args[5]; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2004 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2005 | switch (ctx->type) { |
| 2006 | case TCG_TYPE_I32: |
| 2007 | opc = INDEX_op_setcond_i32; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2008 | if (TCG_TARGET_HAS_negsetcond_i32) { |
| 2009 | negopc = INDEX_op_negsetcond_i32; |
| 2010 | } |
| 2011 | tv = (int32_t)tv; |
| 2012 | fv = (int32_t)fv; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2013 | break; |
| 2014 | case TCG_TYPE_I64: |
| 2015 | opc = INDEX_op_setcond_i64; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2016 | if (TCG_TARGET_HAS_negsetcond_i64) { |
| 2017 | negopc = INDEX_op_negsetcond_i64; |
| 2018 | } |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2019 | break; |
| 2020 | default: |
| 2021 | g_assert_not_reached(); |
| 2022 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2023 | |
| 2024 | if (tv == 1 && fv == 0) { |
| 2025 | op->opc = opc; |
| 2026 | op->args[3] = cond; |
| 2027 | } else if (fv == 1 && tv == 0) { |
| 2028 | op->opc = opc; |
| 2029 | op->args[3] = tcg_invert_cond(cond); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2030 | } else if (negopc) { |
| 2031 | if (tv == -1 && fv == 0) { |
| 2032 | op->opc = negopc; |
| 2033 | op->args[3] = cond; |
| 2034 | } else if (fv == -1 && tv == 0) { |
| 2035 | op->opc = negopc; |
| 2036 | op->args[3] = tcg_invert_cond(cond); |
| 2037 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2038 | } |
| 2039 | } |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 2040 | |
| 2041 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2044 | static bool fold_mul(OptContext *ctx, TCGOp *op) |
| 2045 | { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2046 | if (fold_const2(ctx, op) || |
Richard Henderson | 5b5cf47 | 2021-10-25 11:19:14 -0700 | [diff] [blame] | 2047 | fold_xi_to_i(ctx, op, 0) || |
| 2048 | fold_xi_to_x(ctx, op, 1)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2049 | return true; |
| 2050 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2051 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) |
| 2055 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2056 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2057 | fold_xi_to_i(ctx, op, 0)) { |
| 2058 | return true; |
| 2059 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2060 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2063 | static bool fold_multiply2(OptContext *ctx, TCGOp *op) |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2064 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2065 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
| 2066 | |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2067 | 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] | 2068 | uint64_t a = arg_info(op->args[2])->val; |
| 2069 | uint64_t b = arg_info(op->args[3])->val; |
| 2070 | uint64_t h, l; |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2071 | TCGArg rl, rh; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2072 | TCGOp *op2; |
| 2073 | |
| 2074 | switch (op->opc) { |
| 2075 | case INDEX_op_mulu2_i32: |
| 2076 | l = (uint64_t)(uint32_t)a * (uint32_t)b; |
| 2077 | h = (int32_t)(l >> 32); |
| 2078 | l = (int32_t)l; |
| 2079 | break; |
| 2080 | case INDEX_op_muls2_i32: |
| 2081 | l = (int64_t)(int32_t)a * (int32_t)b; |
| 2082 | h = l >> 32; |
| 2083 | l = (int32_t)l; |
| 2084 | break; |
| 2085 | case INDEX_op_mulu2_i64: |
| 2086 | mulu64(&l, &h, a, b); |
| 2087 | break; |
| 2088 | case INDEX_op_muls2_i64: |
| 2089 | muls64(&l, &h, a, b); |
| 2090 | break; |
| 2091 | default: |
| 2092 | g_assert_not_reached(); |
| 2093 | } |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2094 | |
| 2095 | rl = op->args[0]; |
| 2096 | rh = op->args[1]; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2097 | |
| 2098 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 2099 | op2 = opt_insert_before(ctx, op, 0, 2); |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2100 | |
| 2101 | tcg_opt_gen_movi(ctx, op, rl, l); |
| 2102 | tcg_opt_gen_movi(ctx, op2, rh, h); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2103 | return true; |
| 2104 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2105 | return finish_folding(ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2108 | static bool fold_nand(OptContext *ctx, TCGOp *op) |
| 2109 | { |
Richard Henderson | fa3168e | 2024-12-08 20:20:40 -0600 | [diff] [blame] | 2110 | uint64_t s_mask; |
| 2111 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2112 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2113 | fold_xi_to_not(ctx, op, -1)) { |
| 2114 | return true; |
| 2115 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2116 | |
Richard Henderson | fa3168e | 2024-12-08 20:20:40 -0600 | [diff] [blame] | 2117 | s_mask = arg_info(op->args[1])->s_mask |
| 2118 | & arg_info(op->args[2])->s_mask; |
| 2119 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2122 | static bool fold_neg_no_const(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2123 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2124 | /* Set to 1 all bits to the left of the rightmost. */ |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2125 | uint64_t z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | d151fd3 | 2024-12-08 20:23:11 -0600 | [diff] [blame] | 2126 | z_mask = -(z_mask & -z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2127 | |
Richard Henderson | d151fd3 | 2024-12-08 20:23:11 -0600 | [diff] [blame] | 2128 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2131 | static bool fold_neg(OptContext *ctx, TCGOp *op) |
| 2132 | { |
| 2133 | return fold_const1(ctx, op) || fold_neg_no_const(ctx, op); |
| 2134 | } |
| 2135 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2136 | static bool fold_nor(OptContext *ctx, TCGOp *op) |
| 2137 | { |
Richard Henderson | 2b7b695 | 2024-12-08 20:25:21 -0600 | [diff] [blame] | 2138 | uint64_t s_mask; |
| 2139 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2140 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2141 | fold_xi_to_not(ctx, op, 0)) { |
| 2142 | return true; |
| 2143 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2144 | |
Richard Henderson | 2b7b695 | 2024-12-08 20:25:21 -0600 | [diff] [blame] | 2145 | s_mask = arg_info(op->args[1])->s_mask |
| 2146 | & arg_info(op->args[2])->s_mask; |
| 2147 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | static bool fold_not(OptContext *ctx, TCGOp *op) |
| 2151 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2152 | if (fold_const1(ctx, op)) { |
| 2153 | return true; |
| 2154 | } |
Richard Henderson | 608e75f | 2024-12-08 20:27:02 -0600 | [diff] [blame] | 2155 | return fold_masks_s(ctx, op, arg_info(op->args[1])->s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | static bool fold_or(OptContext *ctx, TCGOp *op) |
| 2159 | { |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2160 | uint64_t z_mask, s_mask; |
| 2161 | TempOptInfo *t1, *t2; |
| 2162 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2163 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2164 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 2165 | fold_xx_to_x(ctx, op)) { |
| 2166 | return true; |
| 2167 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2168 | |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2169 | t1 = arg_info(op->args[1]); |
| 2170 | t2 = arg_info(op->args[2]); |
| 2171 | z_mask = t1->z_mask | t2->z_mask; |
| 2172 | s_mask = t1->s_mask & t2->s_mask; |
| 2173 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | static bool fold_orc(OptContext *ctx, TCGOp *op) |
| 2177 | { |
Richard Henderson | 54e26b2 | 2024-12-08 20:30:20 -0600 | [diff] [blame] | 2178 | uint64_t s_mask; |
Richard Henderson | 50e40ec | 2024-12-10 08:13:10 -0600 | [diff] [blame] | 2179 | TempOptInfo *t1, *t2; |
Richard Henderson | 54e26b2 | 2024-12-08 20:30:20 -0600 | [diff] [blame] | 2180 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2181 | if (fold_const2(ctx, op) || |
Richard Henderson | 4e858d9 | 2021-08-26 07:31:13 -0700 | [diff] [blame] | 2182 | fold_xx_to_i(ctx, op, -1) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2183 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2184 | fold_ix_to_not(ctx, op, 0)) { |
| 2185 | return true; |
| 2186 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2187 | |
Richard Henderson | 50e40ec | 2024-12-10 08:13:10 -0600 | [diff] [blame] | 2188 | t2 = arg_info(op->args[2]); |
| 2189 | if (ti_is_const(t2)) { |
| 2190 | /* Fold orc r,x,i to or r,x,~i. */ |
| 2191 | switch (ctx->type) { |
| 2192 | case TCG_TYPE_I32: |
| 2193 | case TCG_TYPE_I64: |
| 2194 | op->opc = INDEX_op_or; |
| 2195 | break; |
| 2196 | case TCG_TYPE_V64: |
| 2197 | case TCG_TYPE_V128: |
| 2198 | case TCG_TYPE_V256: |
| 2199 | op->opc = INDEX_op_or_vec; |
| 2200 | break; |
| 2201 | default: |
| 2202 | g_assert_not_reached(); |
| 2203 | } |
| 2204 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 2205 | return fold_or(ctx, op); |
| 2206 | } |
| 2207 | |
| 2208 | t1 = arg_info(op->args[1]); |
| 2209 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | 54e26b2 | 2024-12-08 20:30:20 -0600 | [diff] [blame] | 2210 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2213 | static bool fold_qemu_ld_1reg(OptContext *ctx, TCGOp *op) |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2214 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2215 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 2216 | MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs]; |
| 2217 | MemOp mop = get_memop(oi); |
| 2218 | int width = 8 * memop_size(mop); |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2219 | uint64_t z_mask = -1, s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2220 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2221 | if (width < 64) { |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 2222 | if (mop & MO_SIGN) { |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2223 | s_mask = MAKE_64BIT_MASK(width - 1, 64 - (width - 1)); |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 2224 | } else { |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2225 | z_mask = MAKE_64BIT_MASK(0, width); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2226 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2229 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2230 | ctx->prev_mb = NULL; |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2231 | |
| 2232 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
| 2233 | } |
| 2234 | |
| 2235 | static bool fold_qemu_ld_2reg(OptContext *ctx, TCGOp *op) |
| 2236 | { |
| 2237 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2238 | ctx->prev_mb = NULL; |
| 2239 | return finish_folding(ctx, op); |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | static bool fold_qemu_st(OptContext *ctx, TCGOp *op) |
| 2243 | { |
| 2244 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2245 | ctx->prev_mb = NULL; |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2246 | return true; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2249 | static bool fold_remainder(OptContext *ctx, TCGOp *op) |
| 2250 | { |
Richard Henderson | 267c17e | 2021-10-25 11:30:33 -0700 | [diff] [blame] | 2251 | if (fold_const2(ctx, op) || |
| 2252 | fold_xx_to_i(ctx, op, 0)) { |
| 2253 | return true; |
| 2254 | } |
Richard Henderson | f9e3934 | 2024-12-08 20:36:50 -0600 | [diff] [blame] | 2255 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2258 | /* Return 1 if finished, -1 if simplified, 0 if unchanged. */ |
| 2259 | static int fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg) |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2260 | { |
| 2261 | uint64_t a_zmask, b_val; |
| 2262 | TCGCond cond; |
| 2263 | |
| 2264 | if (!arg_is_const(op->args[2])) { |
| 2265 | return false; |
| 2266 | } |
| 2267 | |
| 2268 | a_zmask = arg_info(op->args[1])->z_mask; |
| 2269 | b_val = arg_info(op->args[2])->val; |
| 2270 | cond = op->args[3]; |
| 2271 | |
| 2272 | if (ctx->type == TCG_TYPE_I32) { |
| 2273 | a_zmask = (uint32_t)a_zmask; |
| 2274 | b_val = (uint32_t)b_val; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * A with only low bits set vs B with high bits set means that A < B. |
| 2279 | */ |
| 2280 | if (a_zmask < b_val) { |
| 2281 | bool inv = false; |
| 2282 | |
| 2283 | switch (cond) { |
| 2284 | case TCG_COND_NE: |
| 2285 | case TCG_COND_LEU: |
| 2286 | case TCG_COND_LTU: |
| 2287 | inv = true; |
| 2288 | /* fall through */ |
| 2289 | case TCG_COND_GTU: |
| 2290 | case TCG_COND_GEU: |
| 2291 | case TCG_COND_EQ: |
| 2292 | return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv); |
| 2293 | default: |
| 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * A with only lsb set is already boolean. |
| 2300 | */ |
| 2301 | if (a_zmask <= 1) { |
| 2302 | bool convert = false; |
| 2303 | bool inv = false; |
| 2304 | |
| 2305 | switch (cond) { |
| 2306 | case TCG_COND_EQ: |
| 2307 | inv = true; |
| 2308 | /* fall through */ |
| 2309 | case TCG_COND_NE: |
| 2310 | convert = (b_val == 0); |
| 2311 | break; |
| 2312 | case TCG_COND_LTU: |
| 2313 | case TCG_COND_TSTEQ: |
| 2314 | inv = true; |
| 2315 | /* fall through */ |
| 2316 | case TCG_COND_GEU: |
| 2317 | case TCG_COND_TSTNE: |
| 2318 | convert = (b_val == 1); |
| 2319 | break; |
| 2320 | default: |
| 2321 | break; |
| 2322 | } |
| 2323 | if (convert) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2324 | if (!inv && !neg) { |
| 2325 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 2326 | } |
| 2327 | |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2328 | if (!inv) { |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2329 | op->opc = INDEX_op_neg; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2330 | } else if (neg) { |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 2331 | op->opc = INDEX_op_add; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2332 | op->args[2] = arg_new_constant(ctx, -1); |
| 2333 | } else { |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 2334 | op->opc = INDEX_op_xor; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2335 | op->args[2] = arg_new_constant(ctx, 1); |
| 2336 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2337 | return -1; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2338 | } |
| 2339 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2340 | return 0; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2341 | } |
| 2342 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2343 | static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg) |
| 2344 | { |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2345 | TCGOpcode shr_opc; |
Paolo Bonzini | ff20281 | 2024-02-28 12:06:41 +0100 | [diff] [blame] | 2346 | TCGOpcode uext_opc = 0, sext_opc = 0; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2347 | TCGCond cond = op->args[3]; |
| 2348 | TCGArg ret, src1, src2; |
| 2349 | TCGOp *op2; |
| 2350 | uint64_t val; |
| 2351 | int sh; |
| 2352 | bool inv; |
| 2353 | |
| 2354 | if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) { |
| 2355 | return; |
| 2356 | } |
| 2357 | |
| 2358 | src2 = op->args[2]; |
| 2359 | val = arg_info(src2)->val; |
| 2360 | if (!is_power_of_2(val)) { |
| 2361 | return; |
| 2362 | } |
| 2363 | sh = ctz64(val); |
| 2364 | |
| 2365 | switch (ctx->type) { |
| 2366 | case TCG_TYPE_I32: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2367 | shr_opc = INDEX_op_shr_i32; |
Richard Henderson | 4bce752 | 2024-12-25 18:55:45 -0800 | [diff] [blame] | 2368 | if (TCG_TARGET_extract_valid(TCG_TYPE_I32, sh, 1)) { |
Richard Henderson | c334de1 | 2024-12-26 00:43:19 -0800 | [diff] [blame] | 2369 | uext_opc = INDEX_op_extract_i32; |
Richard Henderson | 4bce752 | 2024-12-25 18:55:45 -0800 | [diff] [blame] | 2370 | } |
| 2371 | if (TCG_TARGET_sextract_valid(TCG_TYPE_I32, sh, 1)) { |
Richard Henderson | c334de1 | 2024-12-26 00:43:19 -0800 | [diff] [blame] | 2372 | sext_opc = INDEX_op_sextract_i32; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2373 | } |
| 2374 | break; |
| 2375 | case TCG_TYPE_I64: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2376 | shr_opc = INDEX_op_shr_i64; |
Richard Henderson | 4bce752 | 2024-12-25 18:55:45 -0800 | [diff] [blame] | 2377 | if (TCG_TARGET_extract_valid(TCG_TYPE_I64, sh, 1)) { |
Richard Henderson | c334de1 | 2024-12-26 00:43:19 -0800 | [diff] [blame] | 2378 | uext_opc = INDEX_op_extract_i64; |
Richard Henderson | 4bce752 | 2024-12-25 18:55:45 -0800 | [diff] [blame] | 2379 | } |
| 2380 | if (TCG_TARGET_sextract_valid(TCG_TYPE_I64, sh, 1)) { |
Richard Henderson | c334de1 | 2024-12-26 00:43:19 -0800 | [diff] [blame] | 2381 | sext_opc = INDEX_op_sextract_i64; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2382 | } |
| 2383 | break; |
| 2384 | default: |
| 2385 | g_assert_not_reached(); |
| 2386 | } |
| 2387 | |
| 2388 | ret = op->args[0]; |
| 2389 | src1 = op->args[1]; |
| 2390 | inv = cond == TCG_COND_TSTEQ; |
| 2391 | |
| 2392 | if (sh && sext_opc && neg && !inv) { |
| 2393 | op->opc = sext_opc; |
| 2394 | op->args[1] = src1; |
| 2395 | op->args[2] = sh; |
| 2396 | op->args[3] = 1; |
| 2397 | return; |
| 2398 | } else if (sh && uext_opc) { |
| 2399 | op->opc = uext_opc; |
| 2400 | op->args[1] = src1; |
| 2401 | op->args[2] = sh; |
| 2402 | op->args[3] = 1; |
| 2403 | } else { |
| 2404 | if (sh) { |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 2405 | op2 = opt_insert_before(ctx, op, shr_opc, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2406 | op2->args[0] = ret; |
| 2407 | op2->args[1] = src1; |
| 2408 | op2->args[2] = arg_new_constant(ctx, sh); |
| 2409 | src1 = ret; |
| 2410 | } |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 2411 | op->opc = INDEX_op_and; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2412 | op->args[1] = src1; |
| 2413 | op->args[2] = arg_new_constant(ctx, 1); |
| 2414 | } |
| 2415 | |
| 2416 | if (neg && inv) { |
Richard Henderson | 93a9ddb | 2025-01-06 22:06:08 -0800 | [diff] [blame] | 2417 | op2 = opt_insert_after(ctx, op, INDEX_op_add, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2418 | op2->args[0] = ret; |
| 2419 | op2->args[1] = ret; |
Richard Henderson | 93a9ddb | 2025-01-06 22:06:08 -0800 | [diff] [blame] | 2420 | op2->args[2] = arg_new_constant(ctx, -1); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2421 | } else if (inv) { |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 2422 | op2 = opt_insert_after(ctx, op, INDEX_op_xor, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2423 | op2->args[0] = ret; |
| 2424 | op2->args[1] = ret; |
| 2425 | op2->args[2] = arg_new_constant(ctx, 1); |
| 2426 | } else if (neg) { |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2427 | op2 = opt_insert_after(ctx, op, INDEX_op_neg, 2); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2428 | op2->args[0] = ret; |
| 2429 | op2->args[1] = ret; |
| 2430 | } |
| 2431 | } |
| 2432 | |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2433 | static bool fold_setcond(OptContext *ctx, TCGOp *op) |
| 2434 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2435 | 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] | 2436 | &op->args[2], &op->args[3]); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2437 | if (i >= 0) { |
| 2438 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2439 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2440 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2441 | i = fold_setcond_zmask(ctx, op, false); |
| 2442 | if (i > 0) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2443 | return true; |
| 2444 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2445 | if (i == 0) { |
| 2446 | fold_setcond_tst_pow2(ctx, op, false); |
| 2447 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2448 | |
Richard Henderson | 2c8a283 | 2024-12-08 20:50:37 -0600 | [diff] [blame] | 2449 | return fold_masks_z(ctx, op, 1); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2452 | static bool fold_negsetcond(OptContext *ctx, TCGOp *op) |
| 2453 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2454 | 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] | 2455 | &op->args[2], &op->args[3]); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2456 | if (i >= 0) { |
| 2457 | return tcg_opt_gen_movi(ctx, op, op->args[0], -i); |
| 2458 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2459 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2460 | i = fold_setcond_zmask(ctx, op, true); |
| 2461 | if (i > 0) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2462 | return true; |
| 2463 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2464 | if (i == 0) { |
| 2465 | fold_setcond_tst_pow2(ctx, op, true); |
| 2466 | } |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2467 | |
| 2468 | /* Value is {0,-1} so all bits are repetitions of the sign. */ |
Richard Henderson | 081cf08 | 2024-12-08 20:50:58 -0600 | [diff] [blame] | 2469 | return fold_masks_s(ctx, op, -1); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2472 | static bool fold_setcond2(OptContext *ctx, TCGOp *op) |
| 2473 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2474 | TCGCond cond; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2475 | int i, inv = 0; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2476 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2477 | i = do_constant_folding_cond2(ctx, op, &op->args[1]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2478 | cond = op->args[5]; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2479 | if (i >= 0) { |
| 2480 | goto do_setcond_const; |
| 2481 | } |
| 2482 | |
| 2483 | switch (cond) { |
| 2484 | case TCG_COND_LT: |
| 2485 | case TCG_COND_GE: |
| 2486 | /* |
| 2487 | * Simplify LT/GE comparisons vs zero to a single compare |
| 2488 | * vs the high word of the input. |
| 2489 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 2490 | if (arg_is_const_val(op->args[3], 0) && |
| 2491 | arg_is_const_val(op->args[4], 0)) { |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2492 | goto do_setcond_high; |
| 2493 | } |
| 2494 | break; |
| 2495 | |
| 2496 | case TCG_COND_NE: |
| 2497 | inv = 1; |
| 2498 | QEMU_FALLTHROUGH; |
| 2499 | case TCG_COND_EQ: |
| 2500 | /* |
| 2501 | * Simplify EQ/NE comparisons where one of the pairs |
| 2502 | * can be simplified. |
| 2503 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2504 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2505 | op->args[3], cond); |
| 2506 | switch (i ^ inv) { |
| 2507 | case 0: |
| 2508 | goto do_setcond_const; |
| 2509 | case 1: |
| 2510 | goto do_setcond_high; |
| 2511 | } |
| 2512 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2513 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2514 | op->args[4], cond); |
| 2515 | switch (i ^ inv) { |
| 2516 | case 0: |
| 2517 | goto do_setcond_const; |
| 2518 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2519 | goto do_setcond_low; |
| 2520 | } |
| 2521 | break; |
| 2522 | |
| 2523 | case TCG_COND_TSTEQ: |
| 2524 | case TCG_COND_TSTNE: |
Richard Henderson | a71d9df | 2024-06-30 19:46:23 -0700 | [diff] [blame] | 2525 | if (arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2526 | goto do_setcond_high; |
| 2527 | } |
| 2528 | if (arg_is_const_val(op->args[4], 0)) { |
| 2529 | goto do_setcond_low; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2530 | } |
| 2531 | break; |
| 2532 | |
| 2533 | default: |
| 2534 | break; |
| 2535 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2536 | do_setcond_low: |
| 2537 | op->args[2] = op->args[3]; |
| 2538 | op->args[3] = cond; |
| 2539 | op->opc = INDEX_op_setcond_i32; |
| 2540 | return fold_setcond(ctx, op); |
| 2541 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2542 | do_setcond_high: |
| 2543 | op->args[1] = op->args[2]; |
| 2544 | op->args[2] = op->args[4]; |
| 2545 | op->args[3] = cond; |
| 2546 | op->opc = INDEX_op_setcond_i32; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2547 | return fold_setcond(ctx, op); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2548 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2549 | |
Richard Henderson | a53502c | 2024-12-08 20:56:36 -0600 | [diff] [blame] | 2550 | return fold_masks_z(ctx, op, 1); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2551 | |
| 2552 | do_setcond_const: |
| 2553 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2554 | } |
| 2555 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2556 | static bool fold_sextract(OptContext *ctx, TCGOp *op) |
| 2557 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2558 | uint64_t z_mask, s_mask, s_mask_old; |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2559 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2560 | int pos = op->args[2]; |
| 2561 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2562 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2563 | if (ti_is_const(t1)) { |
| 2564 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 2565 | sextract64(ti_const_val(t1), pos, len)); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2566 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2567 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2568 | s_mask_old = t1->s_mask; |
| 2569 | s_mask = s_mask_old >> pos; |
| 2570 | s_mask |= -1ull << (len - 1); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2571 | |
Richard Henderson | aa9e050 | 2024-12-21 22:03:53 -0800 | [diff] [blame] | 2572 | if (pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 2573 | return true; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2576 | z_mask = sextract64(t1->z_mask, pos, len); |
| 2577 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2580 | static bool fold_shift(OptContext *ctx, TCGOp *op) |
| 2581 | { |
Richard Henderson | 4ed2ba3 | 2024-12-19 19:38:54 -0800 | [diff] [blame] | 2582 | uint64_t s_mask, z_mask; |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2583 | TempOptInfo *t1, *t2; |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2584 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2585 | if (fold_const2(ctx, op) || |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 2586 | fold_ix_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2587 | fold_xi_to_x(ctx, op, 0)) { |
| 2588 | return true; |
| 2589 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2590 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2591 | t1 = arg_info(op->args[1]); |
| 2592 | t2 = arg_info(op->args[2]); |
| 2593 | s_mask = t1->s_mask; |
| 2594 | z_mask = t1->z_mask; |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2595 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2596 | if (ti_is_const(t2)) { |
| 2597 | int sh = ti_const_val(t2); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2598 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2599 | z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2600 | s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2601 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2602 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2603 | } |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2604 | |
| 2605 | switch (op->opc) { |
| 2606 | CASE_OP_32_64(sar): |
| 2607 | /* |
| 2608 | * Arithmetic right shift will not reduce the number of |
| 2609 | * input sign repetitions. |
| 2610 | */ |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2611 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2612 | CASE_OP_32_64(shr): |
| 2613 | /* |
| 2614 | * If the sign bit is known zero, then logical right shift |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2615 | * will not reduce the number of input sign repetitions. |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2616 | */ |
Richard Henderson | 4ed2ba3 | 2024-12-19 19:38:54 -0800 | [diff] [blame] | 2617 | if (~z_mask & -s_mask) { |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2618 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2619 | } |
| 2620 | break; |
| 2621 | default: |
| 2622 | break; |
| 2623 | } |
| 2624 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2625 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2628 | static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op) |
| 2629 | { |
| 2630 | TCGOpcode neg_op; |
| 2631 | bool have_neg; |
| 2632 | |
| 2633 | if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) { |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
| 2637 | switch (ctx->type) { |
| 2638 | case TCG_TYPE_I32: |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2639 | case TCG_TYPE_I64: |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2640 | neg_op = INDEX_op_neg; |
Richard Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2641 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2642 | break; |
| 2643 | case TCG_TYPE_V64: |
| 2644 | case TCG_TYPE_V128: |
| 2645 | case TCG_TYPE_V256: |
| 2646 | neg_op = INDEX_op_neg_vec; |
| 2647 | have_neg = (TCG_TARGET_HAS_neg_vec && |
| 2648 | tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0); |
| 2649 | break; |
| 2650 | default: |
| 2651 | g_assert_not_reached(); |
| 2652 | } |
| 2653 | if (have_neg) { |
| 2654 | op->opc = neg_op; |
| 2655 | op->args[1] = op->args[2]; |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2656 | return fold_neg_no_const(ctx, op); |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2657 | } |
| 2658 | return false; |
| 2659 | } |
| 2660 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2661 | /* We cannot as yet do_constant_folding with vectors. */ |
| 2662 | static bool fold_sub_vec(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2663 | { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2664 | if (fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2665 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2666 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2667 | return true; |
| 2668 | } |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2669 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2672 | static bool fold_sub(OptContext *ctx, TCGOp *op) |
| 2673 | { |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2674 | if (fold_const2(ctx, op) || |
| 2675 | fold_xx_to_i(ctx, op, 0) || |
| 2676 | fold_xi_to_x(ctx, op, 0) || |
| 2677 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2678 | return true; |
| 2679 | } |
| 2680 | |
| 2681 | /* Fold sub r,x,i to add r,x,-i */ |
| 2682 | if (arg_is_const(op->args[2])) { |
| 2683 | uint64_t val = arg_info(op->args[2])->val; |
| 2684 | |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 2685 | op->opc = INDEX_op_add; |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2686 | op->args[2] = arg_new_constant(ctx, -val); |
| 2687 | } |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2688 | return finish_folding(ctx, op); |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2691 | static bool fold_sub2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2692 | { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2693 | return fold_addsub2(ctx, op, false); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2696 | static bool fold_tcg_ld(OptContext *ctx, TCGOp *op) |
| 2697 | { |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2698 | uint64_t z_mask = -1, s_mask = 0; |
| 2699 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2700 | /* We can't do any folding with a load, but we can record bits. */ |
| 2701 | switch (op->opc) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2702 | CASE_OP_32_64(ld8s): |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2703 | s_mask = INT8_MIN; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2704 | break; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2705 | CASE_OP_32_64(ld8u): |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2706 | z_mask = MAKE_64BIT_MASK(0, 8); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2707 | break; |
| 2708 | CASE_OP_32_64(ld16s): |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2709 | s_mask = INT16_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2710 | break; |
| 2711 | CASE_OP_32_64(ld16u): |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2712 | z_mask = MAKE_64BIT_MASK(0, 16); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2713 | break; |
| 2714 | case INDEX_op_ld32s_i64: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2715 | s_mask = INT32_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2716 | break; |
| 2717 | case INDEX_op_ld32u_i64: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2718 | z_mask = MAKE_64BIT_MASK(0, 32); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2719 | break; |
| 2720 | default: |
| 2721 | g_assert_not_reached(); |
| 2722 | } |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2723 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2726 | static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op) |
| 2727 | { |
| 2728 | TCGTemp *dst, *src; |
| 2729 | intptr_t ofs; |
| 2730 | TCGType type; |
| 2731 | |
| 2732 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
Richard Henderson | 0fb5b75 | 2024-12-09 09:44:40 -0600 | [diff] [blame] | 2733 | return finish_folding(ctx, op); |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | type = ctx->type; |
| 2737 | ofs = op->args[2]; |
| 2738 | dst = arg_temp(op->args[0]); |
| 2739 | src = find_mem_copy_for(ctx, type, ofs); |
| 2740 | if (src && src->base_type == type) { |
| 2741 | return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src)); |
| 2742 | } |
| 2743 | |
| 2744 | reset_ts(ctx, dst); |
| 2745 | record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1); |
| 2746 | return true; |
| 2747 | } |
| 2748 | |
| 2749 | static bool fold_tcg_st(OptContext *ctx, TCGOp *op) |
| 2750 | { |
| 2751 | intptr_t ofs = op->args[2]; |
| 2752 | intptr_t lm1; |
| 2753 | |
| 2754 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
| 2755 | remove_mem_copy_all(ctx); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2756 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | switch (op->opc) { |
| 2760 | CASE_OP_32_64(st8): |
| 2761 | lm1 = 0; |
| 2762 | break; |
| 2763 | CASE_OP_32_64(st16): |
| 2764 | lm1 = 1; |
| 2765 | break; |
| 2766 | case INDEX_op_st32_i64: |
| 2767 | case INDEX_op_st_i32: |
| 2768 | lm1 = 3; |
| 2769 | break; |
| 2770 | case INDEX_op_st_i64: |
| 2771 | lm1 = 7; |
| 2772 | break; |
| 2773 | case INDEX_op_st_vec: |
| 2774 | lm1 = tcg_type_size(ctx->type) - 1; |
| 2775 | break; |
| 2776 | default: |
| 2777 | g_assert_not_reached(); |
| 2778 | } |
| 2779 | remove_mem_copy_in(ctx, ofs, ofs + lm1); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2780 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op) |
| 2784 | { |
| 2785 | TCGTemp *src; |
| 2786 | intptr_t ofs, last; |
| 2787 | TCGType type; |
| 2788 | |
| 2789 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2790 | return fold_tcg_st(ctx, op); |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | src = arg_temp(op->args[0]); |
| 2794 | ofs = op->args[2]; |
| 2795 | type = ctx->type; |
Richard Henderson | 3eaadae | 2023-08-23 23:13:06 -0700 | [diff] [blame] | 2796 | |
| 2797 | /* |
| 2798 | * Eliminate duplicate stores of a constant. |
| 2799 | * This happens frequently when the target ISA zero-extends. |
| 2800 | */ |
| 2801 | if (ts_is_const(src)) { |
| 2802 | TCGTemp *prev = find_mem_copy_for(ctx, type, ofs); |
| 2803 | if (src == prev) { |
| 2804 | tcg_op_remove(ctx->tcg, op); |
| 2805 | return true; |
| 2806 | } |
| 2807 | } |
| 2808 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2809 | last = ofs + tcg_type_size(type) - 1; |
| 2810 | remove_mem_copy_in(ctx, ofs, last); |
| 2811 | record_mem_copy(ctx, type, src, ofs, last); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2812 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2815 | static bool fold_xor(OptContext *ctx, TCGOp *op) |
| 2816 | { |
Richard Henderson | c890fd7 | 2024-12-08 21:39:01 -0600 | [diff] [blame] | 2817 | uint64_t z_mask, s_mask; |
| 2818 | TempOptInfo *t1, *t2; |
| 2819 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2820 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2821 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2822 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2823 | fold_xi_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2824 | return true; |
| 2825 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2826 | |
Richard Henderson | c890fd7 | 2024-12-08 21:39:01 -0600 | [diff] [blame] | 2827 | t1 = arg_info(op->args[1]); |
| 2828 | t2 = arg_info(op->args[2]); |
| 2829 | z_mask = t1->z_mask | t2->z_mask; |
| 2830 | s_mask = t1->s_mask & t2->s_mask; |
| 2831 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2834 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 2835 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2836 | { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2837 | int nb_temps, i; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 2838 | TCGOp *op, *op_next; |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 2839 | OptContext ctx = { .tcg = s }; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 2840 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2841 | QSIMPLEQ_INIT(&ctx.mem_free); |
| 2842 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2843 | /* Array VALS has an element for each temp. |
| 2844 | 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] | 2845 | If this temp is a copy of other ones then the other copies are |
| 2846 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2847 | |
| 2848 | nb_temps = s->nb_temps; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 2849 | for (i = 0; i < nb_temps; ++i) { |
| 2850 | s->temps[i].state_ptr = NULL; |
| 2851 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2852 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 2853 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2854 | TCGOpcode opc = op->opc; |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2855 | const TCGOpDef *def; |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 2856 | bool done = false; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2857 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2858 | /* Calls are special. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2859 | if (opc == INDEX_op_call) { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2860 | fold_call(&ctx, op); |
| 2861 | continue; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 2862 | } |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2863 | |
| 2864 | def = &tcg_op_defs[opc]; |
Richard Henderson | ec5d4cb | 2021-08-24 08:20:27 -0700 | [diff] [blame] | 2865 | init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); |
| 2866 | copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2867 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2868 | /* Pre-compute the type of the operation. */ |
Richard Henderson | 4d87221 | 2025-01-02 19:43:06 -0800 | [diff] [blame] | 2869 | ctx.type = TCGOP_TYPE(op); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2870 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2871 | /* |
| 2872 | * Process each opcode. |
| 2873 | * Sorted alphabetically by opcode as much as possible. |
| 2874 | */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2875 | switch (opc) { |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 2876 | case INDEX_op_add: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2877 | done = fold_add(&ctx, op); |
| 2878 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2879 | case INDEX_op_add_vec: |
| 2880 | done = fold_add_vec(&ctx, op); |
| 2881 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2882 | CASE_OP_32_64(add2): |
| 2883 | done = fold_add2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2884 | break; |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 2885 | case INDEX_op_and: |
| 2886 | case INDEX_op_and_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2887 | done = fold_and(&ctx, op); |
| 2888 | break; |
Richard Henderson | 46f96bf | 2025-01-06 12:37:02 -0800 | [diff] [blame] | 2889 | case INDEX_op_andc: |
| 2890 | case INDEX_op_andc_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2891 | done = fold_andc(&ctx, op); |
| 2892 | break; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 2893 | CASE_OP_32_64(brcond): |
| 2894 | done = fold_brcond(&ctx, op); |
| 2895 | break; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 2896 | case INDEX_op_brcond2_i32: |
| 2897 | done = fold_brcond2(&ctx, op); |
| 2898 | break; |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 2899 | CASE_OP_32_64(bswap16): |
| 2900 | CASE_OP_32_64(bswap32): |
| 2901 | case INDEX_op_bswap64_i64: |
| 2902 | done = fold_bswap(&ctx, op); |
| 2903 | break; |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 2904 | CASE_OP_32_64(clz): |
| 2905 | CASE_OP_32_64(ctz): |
| 2906 | done = fold_count_zeros(&ctx, op); |
| 2907 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2908 | CASE_OP_32_64(ctpop): |
| 2909 | done = fold_ctpop(&ctx, op); |
| 2910 | break; |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 2911 | CASE_OP_32_64(deposit): |
| 2912 | done = fold_deposit(&ctx, op); |
| 2913 | break; |
Richard Henderson | b2c514f | 2025-01-07 13:22:56 -0800 | [diff] [blame] | 2914 | case INDEX_op_divs: |
Richard Henderson | 961b80a | 2025-01-07 14:27:19 -0800 | [diff] [blame] | 2915 | case INDEX_op_divu: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2916 | done = fold_divide(&ctx, op); |
| 2917 | break; |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 2918 | case INDEX_op_dup_vec: |
| 2919 | done = fold_dup(&ctx, op); |
| 2920 | break; |
| 2921 | case INDEX_op_dup2_vec: |
| 2922 | done = fold_dup2(&ctx, op); |
| 2923 | break; |
Richard Henderson | 5c0968a | 2025-01-06 15:47:53 -0800 | [diff] [blame] | 2924 | case INDEX_op_eqv: |
| 2925 | case INDEX_op_eqv_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2926 | done = fold_eqv(&ctx, op); |
| 2927 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2928 | CASE_OP_32_64(extract): |
| 2929 | done = fold_extract(&ctx, op); |
| 2930 | break; |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 2931 | CASE_OP_32_64(extract2): |
| 2932 | done = fold_extract2(&ctx, op); |
| 2933 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2934 | case INDEX_op_ext_i32_i64: |
| 2935 | done = fold_exts(&ctx, op); |
| 2936 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 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 | b570126 | 2024-12-28 15:58:24 -0800 | [diff] [blame] | 2968 | case INDEX_op_mov: |
| 2969 | case INDEX_op_mov_vec: |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2970 | done = fold_mov(&ctx, op); |
| 2971 | break; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2972 | CASE_OP_32_64(movcond): |
| 2973 | done = fold_movcond(&ctx, op); |
| 2974 | break; |
Richard Henderson | d2c3eca | 2025-01-07 09:32:18 -0800 | [diff] [blame] | 2975 | case INDEX_op_mul: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2976 | done = fold_mul(&ctx, op); |
| 2977 | break; |
Richard Henderson | c742824 | 2025-01-07 11:19:29 -0800 | [diff] [blame] | 2978 | case INDEX_op_mulsh: |
Richard Henderson | aa28c9e | 2025-01-07 10:36:24 -0800 | [diff] [blame] | 2979 | case INDEX_op_muluh: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2980 | done = fold_mul_highpart(&ctx, op); |
| 2981 | break; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2982 | CASE_OP_32_64(muls2): |
| 2983 | CASE_OP_32_64(mulu2): |
| 2984 | done = fold_multiply2(&ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2985 | break; |
Richard Henderson | 59379a4 | 2025-01-06 20:32:54 -0800 | [diff] [blame] | 2986 | case INDEX_op_nand: |
| 2987 | case INDEX_op_nand_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2988 | done = fold_nand(&ctx, op); |
| 2989 | break; |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2990 | case INDEX_op_neg: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2991 | done = fold_neg(&ctx, op); |
| 2992 | break; |
Richard Henderson | 3a8c4e9 | 2025-01-06 21:02:17 -0800 | [diff] [blame] | 2993 | case INDEX_op_nor: |
| 2994 | case INDEX_op_nor_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2995 | done = fold_nor(&ctx, op); |
| 2996 | break; |
Richard Henderson | 5c62d37 | 2025-01-06 23:46:47 -0800 | [diff] [blame] | 2997 | case INDEX_op_not: |
| 2998 | case INDEX_op_not_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2999 | done = fold_not(&ctx, op); |
| 3000 | break; |
Richard Henderson | 49bd751 | 2025-01-06 14:00:40 -0800 | [diff] [blame] | 3001 | case INDEX_op_or: |
| 3002 | case INDEX_op_or_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3003 | done = fold_or(&ctx, op); |
| 3004 | break; |
Richard Henderson | 6aba25e | 2025-01-06 14:46:26 -0800 | [diff] [blame] | 3005 | case INDEX_op_orc: |
| 3006 | case INDEX_op_orc_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3007 | done = fold_orc(&ctx, op); |
| 3008 | break; |
Richard Henderson | 50b7a19 | 2025-02-04 13:46:09 -0800 | [diff] [blame] | 3009 | case INDEX_op_qemu_ld_i32: |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 3010 | done = fold_qemu_ld_1reg(&ctx, op); |
| 3011 | break; |
Richard Henderson | 50b7a19 | 2025-02-04 13:46:09 -0800 | [diff] [blame] | 3012 | case INDEX_op_qemu_ld_i64: |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 3013 | if (TCG_TARGET_REG_BITS == 64) { |
| 3014 | done = fold_qemu_ld_1reg(&ctx, op); |
| 3015 | break; |
| 3016 | } |
| 3017 | QEMU_FALLTHROUGH; |
Richard Henderson | 50b7a19 | 2025-02-04 13:46:09 -0800 | [diff] [blame] | 3018 | case INDEX_op_qemu_ld_i128: |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 3019 | done = fold_qemu_ld_2reg(&ctx, op); |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3020 | break; |
Richard Henderson | 50b7a19 | 2025-02-04 13:46:09 -0800 | [diff] [blame] | 3021 | case INDEX_op_qemu_st8_i32: |
| 3022 | case INDEX_op_qemu_st_i32: |
| 3023 | case INDEX_op_qemu_st_i64: |
| 3024 | case INDEX_op_qemu_st_i128: |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3025 | done = fold_qemu_st(&ctx, op); |
| 3026 | break; |
Richard Henderson | 9a6bc18 | 2025-01-07 19:00:51 -0800 | [diff] [blame] | 3027 | case INDEX_op_rems: |
Richard Henderson | cd9acd2 | 2025-01-07 20:25:14 -0800 | [diff] [blame^] | 3028 | case INDEX_op_remu: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3029 | done = fold_remainder(&ctx, op); |
| 3030 | break; |
| 3031 | CASE_OP_32_64(rotl): |
| 3032 | CASE_OP_32_64(rotr): |
| 3033 | CASE_OP_32_64(sar): |
| 3034 | CASE_OP_32_64(shl): |
| 3035 | CASE_OP_32_64(shr): |
| 3036 | done = fold_shift(&ctx, op); |
| 3037 | break; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 3038 | CASE_OP_32_64(setcond): |
| 3039 | done = fold_setcond(&ctx, op); |
| 3040 | break; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 3041 | CASE_OP_32_64(negsetcond): |
| 3042 | done = fold_negsetcond(&ctx, op); |
| 3043 | break; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 3044 | case INDEX_op_setcond2_i32: |
| 3045 | done = fold_setcond2(&ctx, op); |
| 3046 | break; |
Richard Henderson | 1f10654 | 2024-09-06 12:22:41 -0700 | [diff] [blame] | 3047 | case INDEX_op_cmp_vec: |
| 3048 | done = fold_cmp_vec(&ctx, op); |
| 3049 | break; |
| 3050 | case INDEX_op_cmpsel_vec: |
| 3051 | done = fold_cmpsel_vec(&ctx, op); |
| 3052 | break; |
Richard Henderson | e58b977 | 2024-09-06 22:30:01 -0700 | [diff] [blame] | 3053 | case INDEX_op_bitsel_vec: |
| 3054 | done = fold_bitsel_vec(&ctx, op); |
| 3055 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 3056 | CASE_OP_32_64(sextract): |
| 3057 | done = fold_sextract(&ctx, op); |
| 3058 | break; |
Richard Henderson | 60f34f5 | 2025-01-06 22:06:32 -0800 | [diff] [blame] | 3059 | case INDEX_op_sub: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3060 | done = fold_sub(&ctx, op); |
| 3061 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 3062 | case INDEX_op_sub_vec: |
| 3063 | done = fold_sub_vec(&ctx, op); |
| 3064 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 3065 | CASE_OP_32_64(sub2): |
| 3066 | done = fold_sub2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 3067 | break; |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 3068 | case INDEX_op_xor: |
| 3069 | case INDEX_op_xor_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3070 | done = fold_xor(&ctx, op); |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3071 | break; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 3072 | case INDEX_op_set_label: |
| 3073 | case INDEX_op_br: |
| 3074 | case INDEX_op_exit_tb: |
| 3075 | case INDEX_op_goto_tb: |
| 3076 | case INDEX_op_goto_ptr: |
| 3077 | finish_ebb(&ctx); |
| 3078 | done = true; |
| 3079 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3080 | default: |
Richard Henderson | 0ae5642 | 2024-12-08 21:42:53 -0600 | [diff] [blame] | 3081 | done = finish_folding(&ctx, op); |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3082 | break; |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3083 | } |
Richard Henderson | 0ae5642 | 2024-12-08 21:42:53 -0600 | [diff] [blame] | 3084 | tcg_debug_assert(done); |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3085 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3086 | } |