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