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