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