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 | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 787 | static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest, |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 788 | TCGArg *p1, TCGArg *p2, TCGArg *pcond) |
| 789 | { |
| 790 | TCGCond cond; |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 791 | TempOptInfo *i1; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 792 | bool swap; |
| 793 | int r; |
| 794 | |
| 795 | swap = swap_commutative(dest, p1, p2); |
| 796 | cond = *pcond; |
| 797 | if (swap) { |
| 798 | *pcond = cond = tcg_swap_cond(cond); |
| 799 | } |
| 800 | |
| 801 | r = do_constant_folding_cond(ctx->type, *p1, *p2, cond); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 802 | if (r >= 0) { |
| 803 | return r; |
| 804 | } |
| 805 | if (!is_tst_cond(cond)) { |
| 806 | return -1; |
| 807 | } |
| 808 | |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 809 | i1 = arg_info(*p1); |
| 810 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 811 | /* |
| 812 | * TSTNE x,x -> NE x,0 |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 813 | * 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] | 814 | */ |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 815 | if (args_are_copies(*p1, *p2) || |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 816 | (arg_is_const(*p2) && (i1->z_mask & ~arg_const_val(*p2)) == 0)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 817 | *p2 = arg_new_constant(ctx, 0); |
| 818 | *pcond = tcg_tst_eqne_cond(cond); |
| 819 | return -1; |
| 820 | } |
| 821 | |
Paolo Bonzini | 3502062 | 2024-01-22 10:48:11 +0100 | [diff] [blame] | 822 | /* 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] | 823 | 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] | 824 | *p2 = arg_new_constant(ctx, 0); |
| 825 | *pcond = tcg_tst_ltge_cond(cond); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | /* Expand to AND with a temporary if no backend support. */ |
| 830 | if (!TCG_TARGET_HAS_tst) { |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 831 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 832 | TCGArg tmp = arg_new_temp(ctx); |
| 833 | |
| 834 | op2->args[0] = tmp; |
| 835 | op2->args[1] = *p1; |
| 836 | op2->args[2] = *p2; |
| 837 | |
| 838 | *p1 = tmp; |
| 839 | *p2 = arg_new_constant(ctx, 0); |
| 840 | *pcond = tcg_tst_eqne_cond(cond); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 841 | } |
| 842 | return -1; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 845 | static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args) |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 846 | { |
| 847 | TCGArg al, ah, bl, bh; |
| 848 | TCGCond c; |
| 849 | bool swap; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 850 | int r; |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 851 | |
| 852 | swap = swap_commutative2(args, args + 2); |
| 853 | c = args[4]; |
| 854 | if (swap) { |
| 855 | args[4] = c = tcg_swap_cond(c); |
| 856 | } |
| 857 | |
| 858 | al = args[0]; |
| 859 | ah = args[1]; |
| 860 | bl = args[2]; |
| 861 | bh = args[3]; |
| 862 | |
| 863 | if (arg_is_const(bl) && arg_is_const(bh)) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 864 | tcg_target_ulong blv = arg_const_val(bl); |
| 865 | tcg_target_ulong bhv = arg_const_val(bh); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 866 | uint64_t b = deposit64(blv, 32, 32, bhv); |
| 867 | |
| 868 | if (arg_is_const(al) && arg_is_const(ah)) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 869 | tcg_target_ulong alv = arg_const_val(al); |
| 870 | tcg_target_ulong ahv = arg_const_val(ah); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 871 | uint64_t a = deposit64(alv, 32, 32, ahv); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 872 | |
| 873 | r = do_constant_folding_cond_64(a, b, c); |
| 874 | if (r >= 0) { |
| 875 | return r; |
| 876 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 877 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 878 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 879 | if (b == 0) { |
| 880 | switch (c) { |
| 881 | case TCG_COND_LTU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 882 | case TCG_COND_TSTNE: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 883 | return 0; |
| 884 | case TCG_COND_GEU: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 885 | case TCG_COND_TSTEQ: |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 886 | return 1; |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 891 | |
| 892 | /* TSTNE x,-1 -> NE x,0 */ |
| 893 | if (b == -1 && is_tst_cond(c)) { |
| 894 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 895 | args[4] = tcg_tst_eqne_cond(c); |
| 896 | return -1; |
| 897 | } |
| 898 | |
| 899 | /* TSTNE x,sign -> LT x,0 */ |
| 900 | if (b == INT64_MIN && is_tst_cond(c)) { |
| 901 | /* bl must be 0, so copy that to bh */ |
| 902 | args[3] = bl; |
| 903 | args[4] = tcg_tst_ltge_cond(c); |
| 904 | return -1; |
| 905 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 906 | } |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 907 | |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 908 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 909 | r = do_constant_folding_cond_eq(c); |
| 910 | if (r >= 0) { |
| 911 | return r; |
| 912 | } |
| 913 | |
| 914 | /* TSTNE x,x -> NE x,0 */ |
| 915 | if (is_tst_cond(c)) { |
| 916 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 917 | args[4] = tcg_tst_eqne_cond(c); |
| 918 | return -1; |
| 919 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 920 | } |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 921 | |
| 922 | /* Expand to AND with a temporary if no backend support. */ |
| 923 | if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) { |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 924 | TCGOp *op1 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
| 925 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 926 | TCGArg t1 = arg_new_temp(ctx); |
| 927 | TCGArg t2 = arg_new_temp(ctx); |
| 928 | |
| 929 | op1->args[0] = t1; |
| 930 | op1->args[1] = al; |
| 931 | op1->args[2] = bl; |
| 932 | op2->args[0] = t2; |
| 933 | op2->args[1] = ah; |
| 934 | op2->args[2] = bh; |
| 935 | |
| 936 | args[0] = t1; |
| 937 | args[1] = t2; |
| 938 | args[3] = args[2] = arg_new_constant(ctx, 0); |
| 939 | args[4] = tcg_tst_eqne_cond(c); |
| 940 | } |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 941 | return -1; |
| 942 | } |
| 943 | |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 944 | static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args) |
| 945 | { |
| 946 | for (int i = 0; i < nb_args; i++) { |
| 947 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 948 | init_ts_info(ctx, ts); |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 952 | static void copy_propagate(OptContext *ctx, TCGOp *op, |
| 953 | int nb_oargs, int nb_iargs) |
| 954 | { |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 955 | for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
| 956 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 957 | if (ts_is_copy(ts)) { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 958 | op->args[i] = temp_arg(find_better_copy(ts)); |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 963 | static void finish_bb(OptContext *ctx) |
| 964 | { |
| 965 | /* We only optimize memory barriers across basic blocks. */ |
| 966 | ctx->prev_mb = NULL; |
| 967 | } |
| 968 | |
| 969 | static void finish_ebb(OptContext *ctx) |
| 970 | { |
| 971 | finish_bb(ctx); |
| 972 | /* We only optimize across extended basic blocks. */ |
| 973 | memset(&ctx->temps_used, 0, sizeof(ctx->temps_used)); |
| 974 | remove_mem_copy_all(ctx); |
| 975 | } |
| 976 | |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 977 | static bool finish_folding(OptContext *ctx, TCGOp *op) |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 978 | { |
| 979 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 980 | int i, nb_oargs; |
| 981 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 982 | nb_oargs = def->nb_oargs; |
| 983 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 984 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 985 | reset_ts(ctx, ts); |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 986 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 987 | return true; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 990 | /* |
| 991 | * The fold_* functions return true when processing is complete, |
| 992 | * usually by folding the operation to a constant or to a copy, |
| 993 | * and calling tcg_opt_gen_{mov,movi}. They may do other things, |
| 994 | * like collect information about the value produced, for use in |
| 995 | * optimizing a subsequent operation. |
| 996 | * |
| 997 | * These first fold_* functions are all helpers, used by other |
| 998 | * folders for more specific operations. |
| 999 | */ |
| 1000 | |
| 1001 | static bool fold_const1(OptContext *ctx, TCGOp *op) |
| 1002 | { |
| 1003 | if (arg_is_const(op->args[1])) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 1004 | uint64_t t = arg_const_val(op->args[1]); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1005 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1006 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1007 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1008 | } |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | static bool fold_const2(OptContext *ctx, TCGOp *op) |
| 1013 | { |
| 1014 | 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] | 1015 | uint64_t t1 = arg_const_val(op->args[1]); |
| 1016 | uint64_t t2 = arg_const_val(op->args[2]); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1017 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1018 | t1 = do_constant_folding(op->opc, ctx->type, t1, t2); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1019 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 1020 | } |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1024 | static bool fold_commutative(OptContext *ctx, TCGOp *op) |
| 1025 | { |
| 1026 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1030 | static bool fold_const2_commutative(OptContext *ctx, TCGOp *op) |
| 1031 | { |
| 1032 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 1033 | return fold_const2(ctx, op); |
| 1034 | } |
| 1035 | |
Richard Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1036 | /* |
| 1037 | * Record "zero" and "sign" masks for the single output of @op. |
| 1038 | * See TempOptInfo definition of z_mask and s_mask. |
| 1039 | * If z_mask allows, fold the output to constant zero. |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1040 | * The passed s_mask may be augmented by z_mask. |
Richard Henderson | d582b14 | 2024-12-19 10:43:26 -0800 | [diff] [blame] | 1041 | */ |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1042 | static bool fold_masks_zosa(OptContext *ctx, TCGOp *op, uint64_t z_mask, |
| 1043 | uint64_t o_mask, int64_t s_mask, uint64_t a_mask) |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1044 | { |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1045 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 1046 | TCGTemp *ts; |
| 1047 | TempOptInfo *ti; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1048 | int rep; |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1049 | |
| 1050 | /* Only single-output opcodes are supported here. */ |
| 1051 | tcg_debug_assert(def->nb_oargs == 1); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1052 | |
| 1053 | /* |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1054 | * 32-bit ops generate 32-bit results, which for the purpose of |
| 1055 | * simplifying tcg are sign-extended. Certainly that's how we |
| 1056 | * represent our constants elsewhere. Note that the bits will |
| 1057 | * be reset properly for a 64-bit value when encountering the |
| 1058 | * type changing opcodes. |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1059 | */ |
| 1060 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 1061 | z_mask = (int32_t)z_mask; |
Richard Henderson | 56f15f6 | 2024-12-22 15:07:31 -0800 | [diff] [blame] | 1062 | o_mask = (int32_t)o_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1063 | s_mask |= INT32_MIN; |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1064 | a_mask = (uint32_t)a_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Richard Henderson | 56f15f6 | 2024-12-22 15:07:31 -0800 | [diff] [blame] | 1067 | /* Bits that are known 1 and bits that are known 0 must not overlap. */ |
| 1068 | tcg_debug_assert((o_mask & ~z_mask) == 0); |
| 1069 | |
| 1070 | /* All bits that are not known zero are known one is a constant. */ |
| 1071 | if (z_mask == o_mask) { |
| 1072 | return tcg_opt_gen_movi(ctx, op, op->args[0], o_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1073 | } |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1074 | |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1075 | /* If no bits are affected, the operation devolves to a copy. */ |
| 1076 | if (a_mask == 0) { |
| 1077 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1078 | } |
| 1079 | |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1080 | ts = arg_temp(op->args[0]); |
| 1081 | reset_ts(ctx, ts); |
| 1082 | |
| 1083 | ti = ts_info(ts); |
| 1084 | ti->z_mask = z_mask; |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1085 | |
| 1086 | /* Canonicalize s_mask and incorporate data from z_mask. */ |
| 1087 | rep = clz64(~s_mask); |
| 1088 | rep = MAX(rep, clz64(z_mask)); |
Richard Henderson | 56f15f6 | 2024-12-22 15:07:31 -0800 | [diff] [blame] | 1089 | rep = MAX(rep, clz64(~o_mask)); |
Richard Henderson | 6d70ddc | 2024-12-21 21:08:10 -0800 | [diff] [blame] | 1090 | rep = MAX(rep - 1, 0); |
| 1091 | ti->s_mask = INT64_MIN >> rep; |
| 1092 | |
Richard Henderson | 56e06ec | 2024-12-08 18:26:48 -0600 | [diff] [blame] | 1093 | return true; |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Richard Henderson | 33fceba | 2024-12-10 08:26:56 -0600 | [diff] [blame] | 1096 | static bool fold_masks_zos(OptContext *ctx, TCGOp *op, |
| 1097 | uint64_t z_mask, uint64_t o_mask, uint64_t s_mask) |
| 1098 | { |
| 1099 | return fold_masks_zosa(ctx, op, z_mask, o_mask, s_mask, -1); |
| 1100 | } |
| 1101 | |
Richard Henderson | 56f15f6 | 2024-12-22 15:07:31 -0800 | [diff] [blame] | 1102 | static bool fold_masks_zs(OptContext *ctx, TCGOp *op, |
| 1103 | uint64_t z_mask, uint64_t s_mask) |
| 1104 | { |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1105 | return fold_masks_zosa(ctx, op, z_mask, 0, s_mask, -1); |
Richard Henderson | 56f15f6 | 2024-12-22 15:07:31 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1108 | static bool fold_masks_z(OptContext *ctx, TCGOp *op, uint64_t z_mask) |
| 1109 | { |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1110 | return fold_masks_zosa(ctx, op, z_mask, 0, 0, -1); |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1111 | } |
| 1112 | |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1113 | static bool fold_masks_s(OptContext *ctx, TCGOp *op, uint64_t s_mask) |
| 1114 | { |
Richard Henderson | 9e397cc | 2025-06-02 14:11:51 +0100 | [diff] [blame] | 1115 | return fold_masks_zosa(ctx, op, -1, 0, s_mask, -1); |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1118 | /* |
| 1119 | * An "affected" mask bit is 0 if and only if the result is identical |
| 1120 | * to the first input. Thus if the entire mask is 0, the operation |
| 1121 | * is equivalent to a copy. |
| 1122 | */ |
| 1123 | static bool fold_affected_mask(OptContext *ctx, TCGOp *op, uint64_t a_mask) |
| 1124 | { |
| 1125 | if (ctx->type == TCG_TYPE_I32) { |
| 1126 | a_mask = (uint32_t)a_mask; |
| 1127 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1128 | if (a_mask == 0) { |
| 1129 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1134 | /* |
| 1135 | * Convert @op to NOT, if NOT is supported by the host. |
| 1136 | * Return true f the conversion is successful, which will still |
| 1137 | * indicate that the processing is complete. |
| 1138 | */ |
| 1139 | static bool fold_not(OptContext *ctx, TCGOp *op); |
| 1140 | static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx) |
| 1141 | { |
| 1142 | TCGOpcode not_op; |
| 1143 | bool have_not; |
| 1144 | |
| 1145 | switch (ctx->type) { |
| 1146 | case TCG_TYPE_I32: |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1147 | case TCG_TYPE_I64: |
Richard Henderson | 5c62d37 | 2025-01-06 23:46:47 -0800 | [diff] [blame] | 1148 | not_op = INDEX_op_not; |
| 1149 | have_not = tcg_op_supported(INDEX_op_not, ctx->type, 0); |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1150 | break; |
| 1151 | case TCG_TYPE_V64: |
| 1152 | case TCG_TYPE_V128: |
| 1153 | case TCG_TYPE_V256: |
| 1154 | not_op = INDEX_op_not_vec; |
| 1155 | have_not = TCG_TARGET_HAS_not_vec; |
| 1156 | break; |
| 1157 | default: |
| 1158 | g_assert_not_reached(); |
| 1159 | } |
| 1160 | if (have_not) { |
| 1161 | op->opc = not_op; |
| 1162 | op->args[1] = op->args[idx]; |
| 1163 | return fold_not(ctx, op); |
| 1164 | } |
| 1165 | return false; |
| 1166 | } |
| 1167 | |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1168 | /* If the binary operation has first argument @i, fold to @i. */ |
| 1169 | static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1170 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1171 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1172 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1173 | } |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1177 | /* If the binary operation has first argument @i, fold to NOT. */ |
| 1178 | static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1179 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1180 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1181 | return fold_to_not(ctx, op, 2); |
| 1182 | } |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1186 | /* If the binary operation has second argument @i, fold to @i. */ |
| 1187 | static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1188 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1189 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1190 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1191 | } |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1195 | /* If the binary operation has second argument @i, fold to identity. */ |
| 1196 | static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1197 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1198 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1199 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1200 | } |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1204 | /* If the binary operation has second argument @i, fold to NOT. */ |
| 1205 | static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1206 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1207 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1208 | return fold_to_not(ctx, op, 1); |
| 1209 | } |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1213 | /* If the binary operation has both arguments equal, fold to @i. */ |
| 1214 | static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1215 | { |
| 1216 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1217 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1218 | } |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1222 | /* If the binary operation has both arguments equal, fold to identity. */ |
| 1223 | static bool fold_xx_to_x(OptContext *ctx, TCGOp *op) |
| 1224 | { |
| 1225 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1226 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1227 | } |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1231 | /* |
| 1232 | * These outermost fold_<op> functions are sorted alphabetically. |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1233 | * |
| 1234 | * The ordering of the transformations should be: |
| 1235 | * 1) those that produce a constant |
| 1236 | * 2) those that produce a copy |
| 1237 | * 3) those that produce information about the result value. |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1238 | */ |
| 1239 | |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 1240 | static bool fold_addco(OptContext *ctx, TCGOp *op); |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1241 | static bool fold_or(OptContext *ctx, TCGOp *op); |
| 1242 | static bool fold_orc(OptContext *ctx, TCGOp *op); |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 1243 | static bool fold_subbo(OptContext *ctx, TCGOp *op); |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1244 | static bool fold_xor(OptContext *ctx, TCGOp *op); |
| 1245 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1246 | static bool fold_add(OptContext *ctx, TCGOp *op) |
| 1247 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1248 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1249 | fold_xi_to_x(ctx, op, 0)) { |
| 1250 | return true; |
| 1251 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1252 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1255 | /* We cannot as yet do_constant_folding with vectors. */ |
| 1256 | static bool fold_add_vec(OptContext *ctx, TCGOp *op) |
| 1257 | { |
| 1258 | if (fold_commutative(ctx, op) || |
| 1259 | fold_xi_to_x(ctx, op, 0)) { |
| 1260 | return true; |
| 1261 | } |
Richard Henderson | f3ed3cf | 2024-12-08 18:39:47 -0600 | [diff] [blame] | 1262 | return finish_folding(ctx, op); |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 1265 | static void squash_prev_carryout(OptContext *ctx, TCGOp *op) |
| 1266 | { |
| 1267 | TempOptInfo *t2; |
| 1268 | |
| 1269 | op = QTAILQ_PREV(op, link); |
| 1270 | switch (op->opc) { |
| 1271 | case INDEX_op_addco: |
| 1272 | op->opc = INDEX_op_add; |
| 1273 | fold_add(ctx, op); |
| 1274 | break; |
| 1275 | case INDEX_op_addcio: |
| 1276 | op->opc = INDEX_op_addci; |
| 1277 | break; |
| 1278 | case INDEX_op_addc1o: |
| 1279 | op->opc = INDEX_op_add; |
| 1280 | t2 = arg_info(op->args[2]); |
| 1281 | if (ti_is_const(t2)) { |
| 1282 | op->args[2] = arg_new_constant(ctx, ti_const_val(t2) + 1); |
| 1283 | /* Perform other constant folding, if needed. */ |
| 1284 | fold_add(ctx, op); |
| 1285 | } else { |
| 1286 | TCGArg ret = op->args[0]; |
| 1287 | op = opt_insert_after(ctx, op, INDEX_op_add, 3); |
| 1288 | op->args[0] = ret; |
| 1289 | op->args[1] = ret; |
| 1290 | op->args[2] = arg_new_constant(ctx, 1); |
| 1291 | } |
| 1292 | break; |
| 1293 | default: |
| 1294 | g_assert_not_reached(); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | static bool fold_addci(OptContext *ctx, TCGOp *op) |
Richard Henderson | 76f4278 | 2025-01-14 13:58:39 -0800 | [diff] [blame] | 1299 | { |
| 1300 | fold_commutative(ctx, op); |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 1301 | |
| 1302 | if (ctx->carry_state < 0) { |
| 1303 | return finish_folding(ctx, op); |
| 1304 | } |
| 1305 | |
| 1306 | squash_prev_carryout(ctx, op); |
| 1307 | op->opc = INDEX_op_add; |
| 1308 | |
| 1309 | if (ctx->carry_state > 0) { |
| 1310 | TempOptInfo *t2 = arg_info(op->args[2]); |
| 1311 | |
| 1312 | /* |
| 1313 | * Propagate the known carry-in into a constant, if possible. |
| 1314 | * Otherwise emit a second add +1. |
| 1315 | */ |
| 1316 | if (ti_is_const(t2)) { |
| 1317 | op->args[2] = arg_new_constant(ctx, ti_const_val(t2) + 1); |
| 1318 | } else { |
| 1319 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_add, 3); |
| 1320 | |
| 1321 | op2->args[0] = op->args[0]; |
| 1322 | op2->args[1] = op->args[1]; |
| 1323 | op2->args[2] = op->args[2]; |
| 1324 | fold_add(ctx, op2); |
| 1325 | |
| 1326 | op->args[1] = op->args[0]; |
| 1327 | op->args[2] = arg_new_constant(ctx, 1); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | ctx->carry_state = -1; |
| 1332 | return fold_add(ctx, op); |
| 1333 | } |
| 1334 | |
| 1335 | static bool fold_addcio(OptContext *ctx, TCGOp *op) |
| 1336 | { |
| 1337 | TempOptInfo *t1, *t2; |
| 1338 | int carry_out = -1; |
| 1339 | uint64_t sum, max; |
| 1340 | |
| 1341 | fold_commutative(ctx, op); |
| 1342 | t1 = arg_info(op->args[1]); |
| 1343 | t2 = arg_info(op->args[2]); |
| 1344 | |
| 1345 | /* |
| 1346 | * The z_mask value is >= the maximum value that can be represented |
| 1347 | * with the known zero bits. So adding the z_mask values will not |
| 1348 | * overflow if and only if the true values cannot overflow. |
| 1349 | */ |
| 1350 | if (!uadd64_overflow(t1->z_mask, t2->z_mask, &sum) && |
| 1351 | !uadd64_overflow(sum, ctx->carry_state != 0, &sum)) { |
| 1352 | carry_out = 0; |
| 1353 | } |
| 1354 | |
| 1355 | if (ctx->carry_state < 0) { |
| 1356 | ctx->carry_state = carry_out; |
| 1357 | return finish_folding(ctx, op); |
| 1358 | } |
| 1359 | |
| 1360 | squash_prev_carryout(ctx, op); |
| 1361 | if (ctx->carry_state == 0) { |
| 1362 | goto do_addco; |
| 1363 | } |
| 1364 | |
| 1365 | /* Propagate the known carry-in into a constant, if possible. */ |
| 1366 | max = ctx->type == TCG_TYPE_I32 ? UINT32_MAX : UINT64_MAX; |
| 1367 | if (ti_is_const(t2)) { |
| 1368 | uint64_t v = ti_const_val(t2) & max; |
| 1369 | if (v < max) { |
| 1370 | op->args[2] = arg_new_constant(ctx, v + 1); |
| 1371 | goto do_addco; |
| 1372 | } |
| 1373 | /* max + known carry in produces known carry out. */ |
| 1374 | carry_out = 1; |
| 1375 | } |
| 1376 | if (ti_is_const(t1)) { |
| 1377 | uint64_t v = ti_const_val(t1) & max; |
| 1378 | if (v < max) { |
| 1379 | op->args[1] = arg_new_constant(ctx, v + 1); |
| 1380 | goto do_addco; |
| 1381 | } |
| 1382 | carry_out = 1; |
| 1383 | } |
| 1384 | |
| 1385 | /* Adjust the opcode to remember the known carry-in. */ |
| 1386 | op->opc = INDEX_op_addc1o; |
| 1387 | ctx->carry_state = carry_out; |
| 1388 | return finish_folding(ctx, op); |
| 1389 | |
| 1390 | do_addco: |
| 1391 | op->opc = INDEX_op_addco; |
| 1392 | return fold_addco(ctx, op); |
| 1393 | } |
| 1394 | |
| 1395 | static bool fold_addco(OptContext *ctx, TCGOp *op) |
| 1396 | { |
| 1397 | TempOptInfo *t1, *t2; |
| 1398 | int carry_out = -1; |
| 1399 | uint64_t ign; |
| 1400 | |
| 1401 | fold_commutative(ctx, op); |
| 1402 | t1 = arg_info(op->args[1]); |
| 1403 | t2 = arg_info(op->args[2]); |
| 1404 | |
| 1405 | if (ti_is_const(t2)) { |
| 1406 | uint64_t v2 = ti_const_val(t2); |
| 1407 | |
| 1408 | if (ti_is_const(t1)) { |
| 1409 | uint64_t v1 = ti_const_val(t1); |
| 1410 | /* Given sign-extension of z_mask for I32, we need not truncate. */ |
| 1411 | carry_out = uadd64_overflow(v1, v2, &ign); |
| 1412 | } else if (v2 == 0) { |
| 1413 | carry_out = 0; |
| 1414 | } |
| 1415 | } else { |
| 1416 | /* |
| 1417 | * The z_mask value is >= the maximum value that can be represented |
| 1418 | * with the known zero bits. So adding the z_mask values will not |
| 1419 | * overflow if and only if the true values cannot overflow. |
| 1420 | */ |
| 1421 | if (!uadd64_overflow(t1->z_mask, t2->z_mask, &ign)) { |
| 1422 | carry_out = 0; |
| 1423 | } |
| 1424 | } |
| 1425 | ctx->carry_state = carry_out; |
Richard Henderson | 76f4278 | 2025-01-14 13:58:39 -0800 | [diff] [blame] | 1426 | return finish_folding(ctx, op); |
| 1427 | } |
| 1428 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1429 | static bool fold_and(OptContext *ctx, TCGOp *op) |
| 1430 | { |
Richard Henderson | 1e2edf8 | 2024-12-09 16:48:36 -0600 | [diff] [blame] | 1431 | uint64_t z_mask, o_mask, s_mask, a_mask; |
Richard Henderson | 1ca7372 | 2024-12-08 18:47:15 -0600 | [diff] [blame] | 1432 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1433 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1434 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1435 | fold_xi_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1436 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1437 | fold_xx_to_x(ctx, op)) { |
| 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 | |
| 1456 | 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] | 1457 | } |
| 1458 | |
| 1459 | static bool fold_andc(OptContext *ctx, TCGOp *op) |
| 1460 | { |
Richard Henderson | d4d441e | 2024-12-22 16:08:42 -0800 | [diff] [blame] | 1461 | uint64_t z_mask, o_mask, s_mask, a_mask; |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1462 | TempOptInfo *t1, *t2; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1463 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1464 | if (fold_const2(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1465 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1466 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1467 | fold_ix_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1468 | return true; |
| 1469 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1470 | |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1471 | t1 = arg_info(op->args[1]); |
| 1472 | t2 = arg_info(op->args[2]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1473 | |
Richard Henderson | 899281c | 2023-11-15 11:18:55 -0800 | [diff] [blame] | 1474 | if (ti_is_const(t2)) { |
| 1475 | /* Fold andc r,x,i to and r,x,~i. */ |
| 1476 | switch (ctx->type) { |
| 1477 | case TCG_TYPE_I32: |
| 1478 | case TCG_TYPE_I64: |
| 1479 | op->opc = INDEX_op_and; |
| 1480 | break; |
| 1481 | case TCG_TYPE_V64: |
| 1482 | case TCG_TYPE_V128: |
| 1483 | case TCG_TYPE_V256: |
| 1484 | op->opc = INDEX_op_and_vec; |
| 1485 | break; |
| 1486 | default: |
| 1487 | g_assert_not_reached(); |
| 1488 | } |
| 1489 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 1490 | return fold_and(ctx, op); |
| 1491 | } |
| 1492 | |
Richard Henderson | d4d441e | 2024-12-22 16:08:42 -0800 | [diff] [blame] | 1493 | z_mask = t1->z_mask & ~t2->o_mask; |
| 1494 | o_mask = t1->o_mask & ~t2->z_mask; |
Richard Henderson | 21e2b5f | 2024-12-08 18:56:55 -0600 | [diff] [blame] | 1495 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | d4d441e | 2024-12-22 16:08:42 -0800 | [diff] [blame] | 1496 | |
| 1497 | /* Affected bits are those not known zero, masked by those known zero. */ |
| 1498 | a_mask = t1->z_mask & t2->z_mask; |
| 1499 | |
| 1500 | 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] | 1501 | } |
| 1502 | |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1503 | static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op) |
| 1504 | { |
| 1505 | /* If true and false values are the same, eliminate the cmp. */ |
| 1506 | if (args_are_copies(op->args[2], op->args[3])) { |
| 1507 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1508 | } |
| 1509 | |
| 1510 | 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] | 1511 | uint64_t tv = arg_const_val(op->args[2]); |
| 1512 | uint64_t fv = arg_const_val(op->args[3]); |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1513 | |
| 1514 | if (tv == -1 && fv == 0) { |
| 1515 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1516 | } |
| 1517 | if (tv == 0 && fv == -1) { |
| 1518 | if (TCG_TARGET_HAS_not_vec) { |
| 1519 | op->opc = INDEX_op_not_vec; |
| 1520 | return fold_not(ctx, op); |
| 1521 | } else { |
| 1522 | op->opc = INDEX_op_xor_vec; |
| 1523 | op->args[2] = arg_new_constant(ctx, -1); |
| 1524 | return fold_xor(ctx, op); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | if (arg_is_const(op->args[2])) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 1529 | uint64_t tv = arg_const_val(op->args[2]); |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1530 | if (tv == -1) { |
| 1531 | op->opc = INDEX_op_or_vec; |
| 1532 | op->args[2] = op->args[3]; |
| 1533 | return fold_or(ctx, op); |
| 1534 | } |
| 1535 | if (tv == 0 && TCG_TARGET_HAS_andc_vec) { |
| 1536 | op->opc = INDEX_op_andc_vec; |
| 1537 | op->args[2] = op->args[1]; |
| 1538 | op->args[1] = op->args[3]; |
| 1539 | return fold_andc(ctx, op); |
| 1540 | } |
| 1541 | } |
| 1542 | if (arg_is_const(op->args[3])) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 1543 | uint64_t fv = arg_const_val(op->args[3]); |
Richard Henderson | 7d3c63a | 2024-12-09 14:06:08 -0600 | [diff] [blame] | 1544 | if (fv == 0) { |
| 1545 | op->opc = INDEX_op_and_vec; |
| 1546 | return fold_and(ctx, op); |
| 1547 | } |
| 1548 | if (fv == -1 && TCG_TARGET_HAS_orc_vec) { |
| 1549 | op->opc = INDEX_op_orc_vec; |
| 1550 | op->args[2] = op->args[1]; |
| 1551 | op->args[1] = op->args[3]; |
| 1552 | return fold_orc(ctx, op); |
| 1553 | } |
| 1554 | } |
| 1555 | return finish_folding(ctx, op); |
| 1556 | } |
| 1557 | |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1558 | static bool fold_brcond(OptContext *ctx, TCGOp *op) |
| 1559 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1560 | 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] | 1561 | &op->args[1], &op->args[2]); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1562 | if (i == 0) { |
| 1563 | tcg_op_remove(ctx->tcg, op); |
| 1564 | return true; |
| 1565 | } |
| 1566 | if (i > 0) { |
| 1567 | op->opc = INDEX_op_br; |
| 1568 | op->args[0] = op->args[3]; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1569 | finish_ebb(ctx); |
| 1570 | } else { |
| 1571 | finish_bb(ctx); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1572 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1573 | return true; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1576 | static bool fold_brcond2(OptContext *ctx, TCGOp *op) |
| 1577 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1578 | TCGCond cond; |
| 1579 | TCGArg label; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1580 | int i, inv = 0; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1581 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 1582 | i = do_constant_folding_cond2(ctx, op, &op->args[0]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 1583 | cond = op->args[4]; |
| 1584 | label = op->args[5]; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1585 | if (i >= 0) { |
| 1586 | goto do_brcond_const; |
| 1587 | } |
| 1588 | |
| 1589 | switch (cond) { |
| 1590 | case TCG_COND_LT: |
| 1591 | case TCG_COND_GE: |
| 1592 | /* |
| 1593 | * Simplify LT/GE comparisons vs zero to a single compare |
| 1594 | * vs the high word of the input. |
| 1595 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1596 | if (arg_is_const_val(op->args[2], 0) && |
| 1597 | arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1598 | goto do_brcond_high; |
| 1599 | } |
| 1600 | break; |
| 1601 | |
| 1602 | case TCG_COND_NE: |
| 1603 | inv = 1; |
| 1604 | QEMU_FALLTHROUGH; |
| 1605 | case TCG_COND_EQ: |
| 1606 | /* |
| 1607 | * Simplify EQ/NE comparisons where one of the pairs |
| 1608 | * can be simplified. |
| 1609 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1610 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1611 | op->args[2], cond); |
| 1612 | switch (i ^ inv) { |
| 1613 | case 0: |
| 1614 | goto do_brcond_const; |
| 1615 | case 1: |
| 1616 | goto do_brcond_high; |
| 1617 | } |
| 1618 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1619 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1620 | op->args[3], cond); |
| 1621 | switch (i ^ inv) { |
| 1622 | case 0: |
| 1623 | goto do_brcond_const; |
| 1624 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1625 | goto do_brcond_low; |
| 1626 | } |
| 1627 | break; |
| 1628 | |
| 1629 | case TCG_COND_TSTEQ: |
| 1630 | case TCG_COND_TSTNE: |
| 1631 | if (arg_is_const_val(op->args[2], 0)) { |
| 1632 | goto do_brcond_high; |
| 1633 | } |
| 1634 | if (arg_is_const_val(op->args[3], 0)) { |
| 1635 | goto do_brcond_low; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1636 | } |
| 1637 | break; |
| 1638 | |
| 1639 | default: |
| 1640 | break; |
| 1641 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1642 | do_brcond_low: |
Richard Henderson | b6d69fc | 2025-01-10 11:49:22 -0800 | [diff] [blame] | 1643 | op->opc = INDEX_op_brcond; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1644 | op->args[1] = op->args[2]; |
| 1645 | op->args[2] = cond; |
| 1646 | op->args[3] = label; |
| 1647 | return fold_brcond(ctx, op); |
| 1648 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1649 | do_brcond_high: |
Richard Henderson | b6d69fc | 2025-01-10 11:49:22 -0800 | [diff] [blame] | 1650 | op->opc = INDEX_op_brcond; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1651 | op->args[0] = op->args[1]; |
| 1652 | op->args[1] = op->args[3]; |
| 1653 | op->args[2] = cond; |
| 1654 | op->args[3] = label; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 1655 | return fold_brcond(ctx, op); |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1656 | |
| 1657 | do_brcond_const: |
| 1658 | if (i == 0) { |
| 1659 | tcg_op_remove(ctx->tcg, op); |
| 1660 | return true; |
| 1661 | } |
| 1662 | op->opc = INDEX_op_br; |
| 1663 | op->args[0] = label; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1664 | finish_ebb(ctx); |
| 1665 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1666 | } |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 1667 | |
| 1668 | finish_bb(ctx); |
| 1669 | return true; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1672 | static bool fold_bswap(OptContext *ctx, TCGOp *op) |
| 1673 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1674 | uint64_t z_mask, s_mask, sign; |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1675 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1676 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1677 | if (ti_is_const(t1)) { |
| 1678 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1679 | do_constant_folding(op->opc, ctx->type, |
| 1680 | ti_const_val(t1), |
| 1681 | op->args[2])); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1682 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1683 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1684 | z_mask = t1->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1685 | switch (op->opc) { |
Richard Henderson | 0dd07ee | 2025-01-10 18:51:16 -0800 | [diff] [blame] | 1686 | case INDEX_op_bswap16: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1687 | z_mask = bswap16(z_mask); |
| 1688 | sign = INT16_MIN; |
| 1689 | break; |
Richard Henderson | 7498d88 | 2025-01-10 19:53:51 -0800 | [diff] [blame] | 1690 | case INDEX_op_bswap32: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1691 | z_mask = bswap32(z_mask); |
| 1692 | sign = INT32_MIN; |
| 1693 | break; |
Richard Henderson | 3ad5d4c | 2025-01-10 21:54:44 -0800 | [diff] [blame] | 1694 | case INDEX_op_bswap64: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1695 | z_mask = bswap64(z_mask); |
| 1696 | sign = INT64_MIN; |
| 1697 | break; |
| 1698 | default: |
| 1699 | g_assert_not_reached(); |
| 1700 | } |
| 1701 | |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 1702 | s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1703 | switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) { |
| 1704 | case TCG_BSWAP_OZ: |
| 1705 | break; |
| 1706 | case TCG_BSWAP_OS: |
| 1707 | /* If the sign bit may be 1, force all the bits above to 1. */ |
| 1708 | if (z_mask & sign) { |
| 1709 | z_mask |= sign; |
| 1710 | } |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1711 | /* The value and therefore s_mask is explicitly sign-extended. */ |
| 1712 | s_mask = sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | default: |
| 1715 | /* The high bits are undefined: force all bits above the sign to 1. */ |
| 1716 | z_mask |= sign << 1; |
| 1717 | break; |
| 1718 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1719 | |
Richard Henderson | c1e7b98 | 2024-12-08 19:42:20 -0600 | [diff] [blame] | 1720 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1723 | static bool fold_call(OptContext *ctx, TCGOp *op) |
| 1724 | { |
| 1725 | TCGContext *s = ctx->tcg; |
| 1726 | int nb_oargs = TCGOP_CALLO(op); |
| 1727 | int nb_iargs = TCGOP_CALLI(op); |
| 1728 | int flags, i; |
| 1729 | |
| 1730 | init_arguments(ctx, op, nb_oargs + nb_iargs); |
| 1731 | copy_propagate(ctx, op, nb_oargs, nb_iargs); |
| 1732 | |
| 1733 | /* If the function reads or writes globals, reset temp data. */ |
| 1734 | flags = tcg_call_flags(op); |
| 1735 | if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { |
| 1736 | int nb_globals = s->nb_globals; |
| 1737 | |
| 1738 | for (i = 0; i < nb_globals; i++) { |
| 1739 | if (test_bit(i, ctx->temps_used.l)) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1740 | reset_ts(ctx, &ctx->tcg->temps[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 1745 | /* If the function has side effects, reset mem data. */ |
| 1746 | if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) { |
| 1747 | remove_mem_copy_all(ctx); |
| 1748 | } |
| 1749 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1750 | /* Reset temp data for outputs. */ |
| 1751 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1752 | reset_temp(ctx, op->args[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /* Stop optimizing MB across calls. */ |
| 1756 | ctx->prev_mb = NULL; |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
Richard Henderson | 29f6586 | 2024-12-09 14:09:49 -0600 | [diff] [blame] | 1760 | static bool fold_cmp_vec(OptContext *ctx, TCGOp *op) |
| 1761 | { |
| 1762 | /* Canonicalize the comparison to put immediate second. */ |
| 1763 | if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { |
| 1764 | op->args[3] = tcg_swap_cond(op->args[3]); |
| 1765 | } |
| 1766 | return finish_folding(ctx, op); |
| 1767 | } |
| 1768 | |
| 1769 | static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op) |
| 1770 | { |
| 1771 | /* If true and false values are the same, eliminate the cmp. */ |
| 1772 | if (args_are_copies(op->args[3], op->args[4])) { |
| 1773 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); |
| 1774 | } |
| 1775 | |
| 1776 | /* Canonicalize the comparison to put immediate second. */ |
| 1777 | if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { |
| 1778 | op->args[5] = tcg_swap_cond(op->args[5]); |
| 1779 | } |
| 1780 | /* |
| 1781 | * Canonicalize the "false" input reg to match the destination, |
| 1782 | * so that the tcg backend can implement "move if true". |
| 1783 | */ |
| 1784 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
| 1785 | op->args[5] = tcg_invert_cond(op->args[5]); |
| 1786 | } |
| 1787 | return finish_folding(ctx, op); |
| 1788 | } |
| 1789 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1790 | static bool fold_count_zeros(OptContext *ctx, TCGOp *op) |
| 1791 | { |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1792 | uint64_t z_mask, s_mask; |
| 1793 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 1794 | TempOptInfo *t2 = arg_info(op->args[2]); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1795 | |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1796 | if (ti_is_const(t1)) { |
| 1797 | uint64_t t = ti_const_val(t1); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1798 | |
| 1799 | if (t != 0) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1800 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1801 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1802 | } |
| 1803 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1804 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1805 | |
| 1806 | switch (ctx->type) { |
| 1807 | case TCG_TYPE_I32: |
| 1808 | z_mask = 31; |
| 1809 | break; |
| 1810 | case TCG_TYPE_I64: |
| 1811 | z_mask = 63; |
| 1812 | break; |
| 1813 | default: |
| 1814 | g_assert_not_reached(); |
| 1815 | } |
Richard Henderson | ce1d663 | 2024-12-08 19:47:51 -0600 | [diff] [blame] | 1816 | s_mask = ~z_mask; |
| 1817 | z_mask |= t2->z_mask; |
| 1818 | s_mask &= t2->s_mask; |
| 1819 | |
| 1820 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1823 | static bool fold_ctpop(OptContext *ctx, TCGOp *op) |
| 1824 | { |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1825 | uint64_t z_mask; |
| 1826 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1827 | if (fold_const1(ctx, op)) { |
| 1828 | return true; |
| 1829 | } |
| 1830 | |
| 1831 | switch (ctx->type) { |
| 1832 | case TCG_TYPE_I32: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1833 | z_mask = 32 | 31; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1834 | break; |
| 1835 | case TCG_TYPE_I64: |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1836 | z_mask = 64 | 63; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1837 | break; |
| 1838 | default: |
| 1839 | g_assert_not_reached(); |
| 1840 | } |
Richard Henderson | 81be07f | 2024-12-08 19:49:17 -0600 | [diff] [blame] | 1841 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1844 | static bool fold_deposit(OptContext *ctx, TCGOp *op) |
| 1845 | { |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1846 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 1847 | TempOptInfo *t2 = arg_info(op->args[2]); |
| 1848 | int ofs = op->args[3]; |
| 1849 | int len = op->args[4]; |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1850 | int width = 8 * tcg_type_size(ctx->type); |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1851 | uint64_t z_mask, s_mask; |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1852 | |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1853 | if (ti_is_const(t1) && ti_is_const(t2)) { |
| 1854 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1855 | deposit64(ti_const_val(t1), ofs, len, |
| 1856 | ti_const_val(t2))); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1857 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1858 | |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1859 | /* Inserting a value into zero at offset 0. */ |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1860 | if (ti_is_const_val(t1, 0) && ofs == 0) { |
| 1861 | uint64_t mask = MAKE_64BIT_MASK(0, len); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1862 | |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1863 | op->opc = INDEX_op_and; |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1864 | op->args[1] = op->args[2]; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1865 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1866 | return fold_and(ctx, op); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /* Inserting zero into a value. */ |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1870 | if (ti_is_const_val(t2, 0)) { |
| 1871 | uint64_t mask = deposit64(-1, ofs, len, 0); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1872 | |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 1873 | op->opc = INDEX_op_and; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1874 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1875 | return fold_and(ctx, op); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1878 | /* The s_mask from the top portion of the deposit is still valid. */ |
| 1879 | if (ofs + len == width) { |
| 1880 | s_mask = t2->s_mask << ofs; |
| 1881 | } else { |
| 1882 | s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len); |
| 1883 | } |
| 1884 | |
Richard Henderson | c7739ab | 2024-12-08 19:57:28 -0600 | [diff] [blame] | 1885 | z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask); |
Richard Henderson | edb832c | 2024-12-19 17:56:05 -0800 | [diff] [blame] | 1886 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1889 | static bool fold_divide(OptContext *ctx, TCGOp *op) |
| 1890 | { |
Richard Henderson | 2f9d9a3 | 2021-10-25 11:30:14 -0700 | [diff] [blame] | 1891 | if (fold_const2(ctx, op) || |
| 1892 | fold_xi_to_x(ctx, op, 1)) { |
| 1893 | return true; |
| 1894 | } |
Richard Henderson | 3d5ec80 | 2024-12-08 19:59:15 -0600 | [diff] [blame] | 1895 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1898 | static bool fold_dup(OptContext *ctx, TCGOp *op) |
| 1899 | { |
| 1900 | if (arg_is_const(op->args[1])) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 1901 | uint64_t t = arg_const_val(op->args[1]); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1902 | t = dup_const(TCGOP_VECE(op), t); |
| 1903 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1904 | } |
Richard Henderson | e089d69 | 2024-12-08 20:00:51 -0600 | [diff] [blame] | 1905 | return finish_folding(ctx, op); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | static bool fold_dup2(OptContext *ctx, TCGOp *op) |
| 1909 | { |
| 1910 | 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] | 1911 | uint64_t t = deposit64(arg_const_val(op->args[1]), 32, 32, |
| 1912 | arg_const_val(op->args[2])); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1913 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1914 | } |
| 1915 | |
| 1916 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1917 | op->opc = INDEX_op_dup_vec; |
| 1918 | TCGOP_VECE(op) = MO_32; |
| 1919 | } |
Richard Henderson | e089d69 | 2024-12-08 20:00:51 -0600 | [diff] [blame] | 1920 | return finish_folding(ctx, op); |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1923 | static bool fold_eqv(OptContext *ctx, TCGOp *op) |
| 1924 | { |
Richard Henderson | 33fceba | 2024-12-10 08:26:56 -0600 | [diff] [blame] | 1925 | uint64_t z_mask, o_mask, s_mask; |
Richard Henderson | 46c68d7 | 2023-11-15 11:51:28 -0800 | [diff] [blame] | 1926 | TempOptInfo *t1, *t2; |
Richard Henderson | ef6be62 | 2024-12-08 20:03:15 -0600 | [diff] [blame] | 1927 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1928 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1929 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1930 | fold_xi_to_not(ctx, op, 0)) { |
| 1931 | return true; |
| 1932 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1933 | |
Richard Henderson | 46c68d7 | 2023-11-15 11:51:28 -0800 | [diff] [blame] | 1934 | t2 = arg_info(op->args[2]); |
| 1935 | if (ti_is_const(t2)) { |
| 1936 | /* Fold eqv r,x,i to xor r,x,~i. */ |
| 1937 | switch (ctx->type) { |
| 1938 | case TCG_TYPE_I32: |
| 1939 | case TCG_TYPE_I64: |
| 1940 | op->opc = INDEX_op_xor; |
| 1941 | break; |
| 1942 | case TCG_TYPE_V64: |
| 1943 | case TCG_TYPE_V128: |
| 1944 | case TCG_TYPE_V256: |
| 1945 | op->opc = INDEX_op_xor_vec; |
| 1946 | break; |
| 1947 | default: |
| 1948 | g_assert_not_reached(); |
| 1949 | } |
| 1950 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 1951 | return fold_xor(ctx, op); |
| 1952 | } |
| 1953 | |
| 1954 | t1 = arg_info(op->args[1]); |
Richard Henderson | 33fceba | 2024-12-10 08:26:56 -0600 | [diff] [blame] | 1955 | |
| 1956 | z_mask = (t1->z_mask | ~t2->o_mask) & (t2->z_mask | ~t1->o_mask); |
| 1957 | 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] | 1958 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | 33fceba | 2024-12-10 08:26:56 -0600 | [diff] [blame] | 1959 | |
| 1960 | return fold_masks_zos(ctx, op, z_mask, o_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1963 | static bool fold_extract(OptContext *ctx, TCGOp *op) |
| 1964 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1965 | uint64_t z_mask_old, z_mask; |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1966 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1967 | int pos = op->args[2]; |
| 1968 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1969 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1970 | if (ti_is_const(t1)) { |
| 1971 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 1972 | extract64(ti_const_val(t1), pos, len)); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1973 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1974 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1975 | z_mask_old = t1->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1976 | z_mask = extract64(z_mask_old, pos, len); |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 1977 | if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) { |
| 1978 | return true; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1979 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1980 | |
Richard Henderson | b6cd00f | 2024-12-08 20:05:11 -0600 | [diff] [blame] | 1981 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1984 | static bool fold_extract2(OptContext *ctx, TCGOp *op) |
| 1985 | { |
| 1986 | 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] | 1987 | uint64_t v1 = arg_const_val(op->args[1]); |
| 1988 | uint64_t v2 = arg_const_val(op->args[2]); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1989 | int shr = op->args[3]; |
| 1990 | |
Richard Henderson | 61d6a87 | 2025-01-12 21:40:43 -0800 | [diff] [blame] | 1991 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1992 | v1 = (uint32_t)v1 >> shr; |
Richard Henderson | 225bec0 | 2021-11-09 23:17:59 +0100 | [diff] [blame] | 1993 | v2 = (uint64_t)((int32_t)v2 << (32 - shr)); |
Richard Henderson | 61d6a87 | 2025-01-12 21:40:43 -0800 | [diff] [blame] | 1994 | } else { |
| 1995 | v1 >>= shr; |
| 1996 | v2 <<= 64 - shr; |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1997 | } |
| 1998 | return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); |
| 1999 | } |
Richard Henderson | c9df99e | 2024-12-08 20:06:42 -0600 | [diff] [blame] | 2000 | return finish_folding(ctx, op); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2003 | static bool fold_exts(OptContext *ctx, TCGOp *op) |
| 2004 | { |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 2005 | uint64_t s_mask, z_mask; |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 2006 | TempOptInfo *t1; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2007 | |
| 2008 | if (fold_const1(ctx, op)) { |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 2012 | t1 = arg_info(op->args[1]); |
| 2013 | z_mask = t1->z_mask; |
| 2014 | s_mask = t1->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2015 | |
| 2016 | switch (op->opc) { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2017 | case INDEX_op_ext_i32_i64: |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 2018 | s_mask |= INT32_MIN; |
| 2019 | z_mask = (int32_t)z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2020 | break; |
| 2021 | default: |
| 2022 | g_assert_not_reached(); |
| 2023 | } |
Richard Henderson | a962192 | 2024-12-08 20:08:46 -0600 | [diff] [blame] | 2024 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | static bool fold_extu(OptContext *ctx, TCGOp *op) |
| 2028 | { |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 2029 | uint64_t z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2030 | |
| 2031 | if (fold_const1(ctx, op)) { |
| 2032 | return true; |
| 2033 | } |
| 2034 | |
Richard Henderson | 48e8de6 | 2024-12-26 12:01:57 -0800 | [diff] [blame] | 2035 | z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2036 | switch (op->opc) { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2037 | case INDEX_op_extrl_i64_i32: |
| 2038 | case INDEX_op_extu_i32_i64: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2039 | z_mask = (uint32_t)z_mask; |
| 2040 | break; |
| 2041 | case INDEX_op_extrh_i64_i32: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2042 | z_mask >>= 32; |
| 2043 | break; |
| 2044 | default: |
| 2045 | g_assert_not_reached(); |
| 2046 | } |
Richard Henderson | 08abe29 | 2024-12-08 20:11:44 -0600 | [diff] [blame] | 2047 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2050 | static bool fold_mb(OptContext *ctx, TCGOp *op) |
| 2051 | { |
| 2052 | /* Eliminate duplicate and redundant fence instructions. */ |
| 2053 | if (ctx->prev_mb) { |
| 2054 | /* |
| 2055 | * Merge two barriers of the same type into one, |
| 2056 | * or a weaker barrier into a stronger one, |
| 2057 | * or two weaker barriers into a stronger one. |
| 2058 | * mb X; mb Y => mb X|Y |
| 2059 | * mb; strl => mb; st |
| 2060 | * ldaq; mb => ld; mb |
| 2061 | * ldaq; strl => ld; mb; st |
| 2062 | * Other combinations are also merged into a strong |
| 2063 | * barrier. This is stricter than specified but for |
| 2064 | * the purposes of TCG is better than not optimizing. |
| 2065 | */ |
| 2066 | ctx->prev_mb->args[0] |= op->args[0]; |
| 2067 | tcg_op_remove(ctx->tcg, op); |
| 2068 | } else { |
| 2069 | ctx->prev_mb = op; |
| 2070 | } |
| 2071 | return true; |
| 2072 | } |
| 2073 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2074 | static bool fold_mov(OptContext *ctx, TCGOp *op) |
| 2075 | { |
| 2076 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 2077 | } |
| 2078 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2079 | static bool fold_movcond(OptContext *ctx, TCGOp *op) |
| 2080 | { |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 2081 | uint64_t z_mask, s_mask; |
| 2082 | TempOptInfo *tt, *ft; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2083 | int i; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2084 | |
Richard Henderson | 141125e | 2024-09-06 21:00:10 -0700 | [diff] [blame] | 2085 | /* If true and false values are the same, eliminate the cmp. */ |
| 2086 | if (args_are_copies(op->args[3], op->args[4])) { |
| 2087 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); |
| 2088 | } |
| 2089 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2090 | /* |
| 2091 | * Canonicalize the "false" input reg to match the destination reg so |
| 2092 | * that the tcg backend can implement a "move if true" operation. |
| 2093 | */ |
| 2094 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2095 | op->args[5] = tcg_invert_cond(op->args[5]); |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2098 | i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1], |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2099 | &op->args[2], &op->args[5]); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2100 | if (i >= 0) { |
| 2101 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); |
| 2102 | } |
| 2103 | |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 2104 | tt = arg_info(op->args[3]); |
| 2105 | ft = arg_info(op->args[4]); |
| 2106 | z_mask = tt->z_mask | ft->z_mask; |
| 2107 | s_mask = tt->s_mask & ft->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2108 | |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 2109 | if (ti_is_const(tt) && ti_is_const(ft)) { |
| 2110 | uint64_t tv = ti_const_val(tt); |
| 2111 | uint64_t fv = ti_const_val(ft); |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame] | 2112 | TCGCond cond = op->args[5]; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2113 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2114 | if (tv == 1 && fv == 0) { |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2115 | op->opc = INDEX_op_setcond; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2116 | op->args[3] = cond; |
| 2117 | } else if (fv == 1 && tv == 0) { |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2118 | op->opc = INDEX_op_setcond; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2119 | op->args[3] = tcg_invert_cond(cond); |
Richard Henderson | f791458 | 2025-01-09 12:48:21 -0800 | [diff] [blame] | 2120 | } else if (tv == -1 && fv == 0) { |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2121 | op->opc = INDEX_op_negsetcond; |
Richard Henderson | f791458 | 2025-01-09 12:48:21 -0800 | [diff] [blame] | 2122 | op->args[3] = cond; |
| 2123 | } else if (fv == -1 && tv == 0) { |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2124 | op->opc = INDEX_op_negsetcond; |
Richard Henderson | f791458 | 2025-01-09 12:48:21 -0800 | [diff] [blame] | 2125 | op->args[3] = tcg_invert_cond(cond); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2126 | } |
| 2127 | } |
Richard Henderson | 3220278 | 2024-12-08 20:16:38 -0600 | [diff] [blame] | 2128 | |
| 2129 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2132 | static bool fold_mul(OptContext *ctx, TCGOp *op) |
| 2133 | { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2134 | if (fold_const2(ctx, op) || |
Richard Henderson | 5b5cf47 | 2021-10-25 11:19:14 -0700 | [diff] [blame] | 2135 | fold_xi_to_i(ctx, op, 0) || |
| 2136 | fold_xi_to_x(ctx, op, 1)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2137 | return true; |
| 2138 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2139 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) |
| 2143 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2144 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 2145 | fold_xi_to_i(ctx, op, 0)) { |
| 2146 | return true; |
| 2147 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2148 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2151 | static bool fold_multiply2(OptContext *ctx, TCGOp *op) |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2152 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2153 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
| 2154 | |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2155 | 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] | 2156 | uint64_t a = arg_const_val(op->args[2]); |
| 2157 | uint64_t b = arg_const_val(op->args[3]); |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2158 | uint64_t h, l; |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2159 | TCGArg rl, rh; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2160 | TCGOp *op2; |
| 2161 | |
| 2162 | switch (op->opc) { |
Richard Henderson | d776198 | 2025-01-09 09:11:53 -0800 | [diff] [blame] | 2163 | case INDEX_op_mulu2: |
| 2164 | if (ctx->type == TCG_TYPE_I32) { |
| 2165 | l = (uint64_t)(uint32_t)a * (uint32_t)b; |
| 2166 | h = (int32_t)(l >> 32); |
| 2167 | l = (int32_t)l; |
| 2168 | } else { |
| 2169 | mulu64(&l, &h, a, b); |
| 2170 | } |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2171 | break; |
Richard Henderson | bfe9648 | 2025-01-09 07:24:32 -0800 | [diff] [blame] | 2172 | case INDEX_op_muls2: |
| 2173 | if (ctx->type == TCG_TYPE_I32) { |
| 2174 | l = (int64_t)(int32_t)a * (int32_t)b; |
| 2175 | h = l >> 32; |
| 2176 | l = (int32_t)l; |
| 2177 | } else { |
| 2178 | muls64(&l, &h, a, b); |
| 2179 | } |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2180 | break; |
| 2181 | default: |
| 2182 | g_assert_not_reached(); |
| 2183 | } |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2184 | |
| 2185 | rl = op->args[0]; |
| 2186 | rh = op->args[1]; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2187 | |
| 2188 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Richard Henderson | a3c1c57 | 2025-04-21 11:05:29 -0700 | [diff] [blame] | 2189 | op2 = opt_insert_before(ctx, op, 0, 2); |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2190 | |
| 2191 | tcg_opt_gen_movi(ctx, op, rl, l); |
| 2192 | tcg_opt_gen_movi(ctx, op2, rh, h); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2193 | return true; |
| 2194 | } |
Richard Henderson | cd9c583 | 2024-12-08 20:18:02 -0600 | [diff] [blame] | 2195 | return finish_folding(ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2198 | static bool fold_nand(OptContext *ctx, TCGOp *op) |
| 2199 | { |
Richard Henderson | 16559c3 | 2024-12-09 18:13:15 -0600 | [diff] [blame] | 2200 | uint64_t z_mask, o_mask, s_mask; |
| 2201 | TempOptInfo *t1, *t2; |
Richard Henderson | fa3168e | 2024-12-08 20:20:40 -0600 | [diff] [blame] | 2202 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2203 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2204 | fold_xi_to_not(ctx, op, -1)) { |
| 2205 | return true; |
| 2206 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2207 | |
Richard Henderson | 16559c3 | 2024-12-09 18:13:15 -0600 | [diff] [blame] | 2208 | t1 = arg_info(op->args[1]); |
| 2209 | t2 = arg_info(op->args[2]); |
| 2210 | |
| 2211 | z_mask = ~(t1->o_mask & t2->o_mask); |
| 2212 | o_mask = ~(t1->z_mask & t2->z_mask); |
| 2213 | s_mask = t1->s_mask & t2->s_mask; |
| 2214 | |
| 2215 | return fold_masks_zos(ctx, op, z_mask, o_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2218 | static bool fold_neg_no_const(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2219 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2220 | /* Set to 1 all bits to the left of the rightmost. */ |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2221 | uint64_t z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | d151fd3 | 2024-12-08 20:23:11 -0600 | [diff] [blame] | 2222 | z_mask = -(z_mask & -z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2223 | |
Richard Henderson | d151fd3 | 2024-12-08 20:23:11 -0600 | [diff] [blame] | 2224 | return fold_masks_z(ctx, op, z_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2227 | static bool fold_neg(OptContext *ctx, TCGOp *op) |
| 2228 | { |
| 2229 | return fold_const1(ctx, op) || fold_neg_no_const(ctx, op); |
| 2230 | } |
| 2231 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2232 | static bool fold_nor(OptContext *ctx, TCGOp *op) |
| 2233 | { |
Richard Henderson | 682d6d5 | 2024-12-09 21:13:02 -0600 | [diff] [blame] | 2234 | uint64_t z_mask, o_mask, s_mask; |
| 2235 | TempOptInfo *t1, *t2; |
Richard Henderson | 2b7b695 | 2024-12-08 20:25:21 -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, 0)) { |
| 2239 | return true; |
| 2240 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2241 | |
Richard Henderson | 682d6d5 | 2024-12-09 21:13:02 -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 | |
| 2252 | static bool fold_not(OptContext *ctx, TCGOp *op) |
| 2253 | { |
Richard Henderson | d89504b | 2024-12-09 21:15:37 -0600 | [diff] [blame] | 2254 | TempOptInfo *t1; |
| 2255 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2256 | if (fold_const1(ctx, op)) { |
| 2257 | return true; |
| 2258 | } |
Richard Henderson | d89504b | 2024-12-09 21:15:37 -0600 | [diff] [blame] | 2259 | |
| 2260 | t1 = arg_info(op->args[1]); |
| 2261 | 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] | 2262 | } |
| 2263 | |
| 2264 | static bool fold_or(OptContext *ctx, TCGOp *op) |
| 2265 | { |
Richard Henderson | 84b399d | 2024-12-09 21:35:53 -0600 | [diff] [blame] | 2266 | uint64_t z_mask, o_mask, s_mask, a_mask; |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2267 | TempOptInfo *t1, *t2; |
| 2268 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2269 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2270 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 2271 | fold_xx_to_x(ctx, op)) { |
| 2272 | return true; |
| 2273 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2274 | |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2275 | t1 = arg_info(op->args[1]); |
| 2276 | t2 = arg_info(op->args[2]); |
Richard Henderson | 84b399d | 2024-12-09 21:35:53 -0600 | [diff] [blame] | 2277 | |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2278 | z_mask = t1->z_mask | t2->z_mask; |
Richard Henderson | 84b399d | 2024-12-09 21:35:53 -0600 | [diff] [blame] | 2279 | o_mask = t1->o_mask | t2->o_mask; |
Richard Henderson | 83b1ba3 | 2024-12-08 20:28:59 -0600 | [diff] [blame] | 2280 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | 84b399d | 2024-12-09 21:35:53 -0600 | [diff] [blame] | 2281 | |
| 2282 | /* Affected bits are those not known one, masked by those known zero. */ |
| 2283 | a_mask = ~t1->o_mask & t2->z_mask; |
| 2284 | |
| 2285 | 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] | 2286 | } |
| 2287 | |
| 2288 | static bool fold_orc(OptContext *ctx, TCGOp *op) |
| 2289 | { |
Richard Henderson | cc4033e | 2024-12-09 22:22:27 -0600 | [diff] [blame] | 2290 | uint64_t z_mask, o_mask, s_mask, a_mask; |
Richard Henderson | 50e40ec | 2024-12-10 08:13:10 -0600 | [diff] [blame] | 2291 | TempOptInfo *t1, *t2; |
Richard Henderson | 54e26b2 | 2024-12-08 20:30:20 -0600 | [diff] [blame] | 2292 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2293 | if (fold_const2(ctx, op) || |
Richard Henderson | 4e858d9 | 2021-08-26 07:31:13 -0700 | [diff] [blame] | 2294 | fold_xx_to_i(ctx, op, -1) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2295 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2296 | fold_ix_to_not(ctx, op, 0)) { |
| 2297 | return true; |
| 2298 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2299 | |
Richard Henderson | 50e40ec | 2024-12-10 08:13:10 -0600 | [diff] [blame] | 2300 | t2 = arg_info(op->args[2]); |
| 2301 | if (ti_is_const(t2)) { |
| 2302 | /* Fold orc r,x,i to or r,x,~i. */ |
| 2303 | switch (ctx->type) { |
| 2304 | case TCG_TYPE_I32: |
| 2305 | case TCG_TYPE_I64: |
| 2306 | op->opc = INDEX_op_or; |
| 2307 | break; |
| 2308 | case TCG_TYPE_V64: |
| 2309 | case TCG_TYPE_V128: |
| 2310 | case TCG_TYPE_V256: |
| 2311 | op->opc = INDEX_op_or_vec; |
| 2312 | break; |
| 2313 | default: |
| 2314 | g_assert_not_reached(); |
| 2315 | } |
| 2316 | op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); |
| 2317 | return fold_or(ctx, op); |
| 2318 | } |
| 2319 | |
| 2320 | t1 = arg_info(op->args[1]); |
Richard Henderson | cc4033e | 2024-12-09 22:22:27 -0600 | [diff] [blame] | 2321 | |
| 2322 | z_mask = t1->z_mask | ~t2->o_mask; |
| 2323 | o_mask = t1->o_mask | ~t2->z_mask; |
Richard Henderson | 50e40ec | 2024-12-10 08:13:10 -0600 | [diff] [blame] | 2324 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | cc4033e | 2024-12-09 22:22:27 -0600 | [diff] [blame] | 2325 | |
| 2326 | /* Affected bits are those not known one, masked by those known one. */ |
| 2327 | a_mask = ~t1->o_mask & t2->o_mask; |
| 2328 | |
| 2329 | 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] | 2330 | } |
| 2331 | |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2332 | static bool fold_qemu_ld_1reg(OptContext *ctx, TCGOp *op) |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2333 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2334 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 2335 | MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs]; |
| 2336 | MemOp mop = get_memop(oi); |
| 2337 | int width = 8 * memop_size(mop); |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2338 | uint64_t z_mask = -1, s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2339 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2340 | if (width < 64) { |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 2341 | if (mop & MO_SIGN) { |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2342 | s_mask = MAKE_64BIT_MASK(width - 1, 64 - (width - 1)); |
Richard Henderson | 75c3bf3 | 2024-12-19 10:50:40 -0800 | [diff] [blame] | 2343 | } else { |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2344 | z_mask = MAKE_64BIT_MASK(0, width); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2345 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2348 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2349 | ctx->prev_mb = NULL; |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 2350 | |
| 2351 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
| 2352 | } |
| 2353 | |
| 2354 | static bool fold_qemu_ld_2reg(OptContext *ctx, TCGOp *op) |
| 2355 | { |
| 2356 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2357 | ctx->prev_mb = NULL; |
| 2358 | return finish_folding(ctx, op); |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static bool fold_qemu_st(OptContext *ctx, TCGOp *op) |
| 2362 | { |
| 2363 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 2364 | ctx->prev_mb = NULL; |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2365 | return true; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2368 | static bool fold_remainder(OptContext *ctx, TCGOp *op) |
| 2369 | { |
Richard Henderson | 267c17e | 2021-10-25 11:30:33 -0700 | [diff] [blame] | 2370 | if (fold_const2(ctx, op) || |
| 2371 | fold_xx_to_i(ctx, op, 0)) { |
| 2372 | return true; |
| 2373 | } |
Richard Henderson | f9e3934 | 2024-12-08 20:36:50 -0600 | [diff] [blame] | 2374 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2377 | /* Return 1 if finished, -1 if simplified, 0 if unchanged. */ |
| 2378 | static int fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg) |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2379 | { |
| 2380 | uint64_t a_zmask, b_val; |
| 2381 | TCGCond cond; |
| 2382 | |
| 2383 | if (!arg_is_const(op->args[2])) { |
| 2384 | return false; |
| 2385 | } |
| 2386 | |
| 2387 | a_zmask = arg_info(op->args[1])->z_mask; |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 2388 | b_val = arg_const_val(op->args[2]); |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2389 | cond = op->args[3]; |
| 2390 | |
| 2391 | if (ctx->type == TCG_TYPE_I32) { |
| 2392 | a_zmask = (uint32_t)a_zmask; |
| 2393 | b_val = (uint32_t)b_val; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * A with only low bits set vs B with high bits set means that A < B. |
| 2398 | */ |
| 2399 | if (a_zmask < b_val) { |
| 2400 | bool inv = false; |
| 2401 | |
| 2402 | switch (cond) { |
| 2403 | case TCG_COND_NE: |
| 2404 | case TCG_COND_LEU: |
| 2405 | case TCG_COND_LTU: |
| 2406 | inv = true; |
| 2407 | /* fall through */ |
| 2408 | case TCG_COND_GTU: |
| 2409 | case TCG_COND_GEU: |
| 2410 | case TCG_COND_EQ: |
| 2411 | return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv); |
| 2412 | default: |
| 2413 | break; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * A with only lsb set is already boolean. |
| 2419 | */ |
| 2420 | if (a_zmask <= 1) { |
| 2421 | bool convert = false; |
| 2422 | bool inv = false; |
| 2423 | |
| 2424 | switch (cond) { |
| 2425 | case TCG_COND_EQ: |
| 2426 | inv = true; |
| 2427 | /* fall through */ |
| 2428 | case TCG_COND_NE: |
| 2429 | convert = (b_val == 0); |
| 2430 | break; |
| 2431 | case TCG_COND_LTU: |
| 2432 | case TCG_COND_TSTEQ: |
| 2433 | inv = true; |
| 2434 | /* fall through */ |
| 2435 | case TCG_COND_GEU: |
| 2436 | case TCG_COND_TSTNE: |
| 2437 | convert = (b_val == 1); |
| 2438 | break; |
| 2439 | default: |
| 2440 | break; |
| 2441 | } |
| 2442 | if (convert) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2443 | if (!inv && !neg) { |
| 2444 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 2445 | } |
| 2446 | |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2447 | if (!inv) { |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2448 | op->opc = INDEX_op_neg; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2449 | } else if (neg) { |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 2450 | op->opc = INDEX_op_add; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2451 | op->args[2] = arg_new_constant(ctx, -1); |
| 2452 | } else { |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 2453 | op->opc = INDEX_op_xor; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2454 | op->args[2] = arg_new_constant(ctx, 1); |
| 2455 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2456 | return -1; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2457 | } |
| 2458 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2459 | return 0; |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2460 | } |
| 2461 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2462 | static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg) |
| 2463 | { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2464 | TCGCond cond = op->args[3]; |
| 2465 | TCGArg ret, src1, src2; |
| 2466 | TCGOp *op2; |
| 2467 | uint64_t val; |
| 2468 | int sh; |
| 2469 | bool inv; |
| 2470 | |
| 2471 | if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) { |
| 2472 | return; |
| 2473 | } |
| 2474 | |
| 2475 | src2 = op->args[2]; |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 2476 | val = arg_const_val(src2); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2477 | if (!is_power_of_2(val)) { |
| 2478 | return; |
| 2479 | } |
| 2480 | sh = ctz64(val); |
| 2481 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2482 | ret = op->args[0]; |
| 2483 | src1 = op->args[1]; |
| 2484 | inv = cond == TCG_COND_TSTEQ; |
| 2485 | |
Richard Henderson | fa361ee | 2025-01-12 11:50:09 -0800 | [diff] [blame] | 2486 | if (sh && neg && !inv && TCG_TARGET_sextract_valid(ctx->type, sh, 1)) { |
| 2487 | op->opc = INDEX_op_sextract; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2488 | op->args[1] = src1; |
| 2489 | op->args[2] = sh; |
| 2490 | op->args[3] = 1; |
| 2491 | return; |
Richard Henderson | 07d5d50 | 2025-01-11 09:01:46 -0800 | [diff] [blame] | 2492 | } else if (sh && TCG_TARGET_extract_valid(ctx->type, sh, 1)) { |
| 2493 | op->opc = INDEX_op_extract; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2494 | op->args[1] = src1; |
| 2495 | op->args[2] = sh; |
| 2496 | op->args[3] = 1; |
| 2497 | } else { |
| 2498 | if (sh) { |
Richard Henderson | 74dbd36 | 2025-01-07 22:52:10 -0800 | [diff] [blame] | 2499 | op2 = opt_insert_before(ctx, op, INDEX_op_shr, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2500 | op2->args[0] = ret; |
| 2501 | op2->args[1] = src1; |
| 2502 | op2->args[2] = arg_new_constant(ctx, sh); |
| 2503 | src1 = ret; |
| 2504 | } |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 2505 | op->opc = INDEX_op_and; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2506 | op->args[1] = src1; |
| 2507 | op->args[2] = arg_new_constant(ctx, 1); |
| 2508 | } |
| 2509 | |
| 2510 | if (neg && inv) { |
Richard Henderson | 93a9ddb | 2025-01-06 22:06:08 -0800 | [diff] [blame] | 2511 | op2 = opt_insert_after(ctx, op, INDEX_op_add, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2512 | op2->args[0] = ret; |
| 2513 | op2->args[1] = ret; |
Richard Henderson | 93a9ddb | 2025-01-06 22:06:08 -0800 | [diff] [blame] | 2514 | op2->args[2] = arg_new_constant(ctx, -1); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2515 | } else if (inv) { |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 2516 | op2 = opt_insert_after(ctx, op, INDEX_op_xor, 3); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2517 | op2->args[0] = ret; |
| 2518 | op2->args[1] = ret; |
| 2519 | op2->args[2] = arg_new_constant(ctx, 1); |
| 2520 | } else if (neg) { |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2521 | op2 = opt_insert_after(ctx, op, INDEX_op_neg, 2); |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2522 | op2->args[0] = ret; |
| 2523 | op2->args[1] = ret; |
| 2524 | } |
| 2525 | } |
| 2526 | |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2527 | static bool fold_setcond(OptContext *ctx, TCGOp *op) |
| 2528 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2529 | 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] | 2530 | &op->args[2], &op->args[3]); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2531 | if (i >= 0) { |
| 2532 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2533 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2534 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2535 | i = fold_setcond_zmask(ctx, op, false); |
| 2536 | if (i > 0) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2537 | return true; |
| 2538 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2539 | if (i == 0) { |
| 2540 | fold_setcond_tst_pow2(ctx, op, false); |
| 2541 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2542 | |
Richard Henderson | 2c8a283 | 2024-12-08 20:50:37 -0600 | [diff] [blame] | 2543 | return fold_masks_z(ctx, op, 1); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2546 | static bool fold_negsetcond(OptContext *ctx, TCGOp *op) |
| 2547 | { |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2548 | 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] | 2549 | &op->args[2], &op->args[3]); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2550 | if (i >= 0) { |
| 2551 | return tcg_opt_gen_movi(ctx, op, op->args[0], -i); |
| 2552 | } |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2553 | |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2554 | i = fold_setcond_zmask(ctx, op, true); |
| 2555 | if (i > 0) { |
Richard Henderson | 8d65cda | 2024-03-26 16:00:40 -1000 | [diff] [blame] | 2556 | return true; |
| 2557 | } |
Richard Henderson | 95eb229 | 2024-12-08 20:47:59 -0600 | [diff] [blame] | 2558 | if (i == 0) { |
| 2559 | fold_setcond_tst_pow2(ctx, op, true); |
| 2560 | } |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2561 | |
| 2562 | /* Value is {0,-1} so all bits are repetitions of the sign. */ |
Richard Henderson | 081cf08 | 2024-12-08 20:50:58 -0600 | [diff] [blame] | 2563 | return fold_masks_s(ctx, op, -1); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2566 | static bool fold_setcond2(OptContext *ctx, TCGOp *op) |
| 2567 | { |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2568 | TCGCond cond; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2569 | int i, inv = 0; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2570 | |
Richard Henderson | fb04ab7 | 2024-01-10 18:21:58 +1100 | [diff] [blame] | 2571 | i = do_constant_folding_cond2(ctx, op, &op->args[1]); |
Richard Henderson | 7e64b11 | 2023-10-24 16:53:56 -0700 | [diff] [blame] | 2572 | cond = op->args[5]; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2573 | if (i >= 0) { |
| 2574 | goto do_setcond_const; |
| 2575 | } |
| 2576 | |
| 2577 | switch (cond) { |
| 2578 | case TCG_COND_LT: |
| 2579 | case TCG_COND_GE: |
| 2580 | /* |
| 2581 | * Simplify LT/GE comparisons vs zero to a single compare |
| 2582 | * vs the high word of the input. |
| 2583 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 2584 | if (arg_is_const_val(op->args[3], 0) && |
| 2585 | arg_is_const_val(op->args[4], 0)) { |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2586 | goto do_setcond_high; |
| 2587 | } |
| 2588 | break; |
| 2589 | |
| 2590 | case TCG_COND_NE: |
| 2591 | inv = 1; |
| 2592 | QEMU_FALLTHROUGH; |
| 2593 | case TCG_COND_EQ: |
| 2594 | /* |
| 2595 | * Simplify EQ/NE comparisons where one of the pairs |
| 2596 | * can be simplified. |
| 2597 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2598 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2599 | op->args[3], cond); |
| 2600 | switch (i ^ inv) { |
| 2601 | case 0: |
| 2602 | goto do_setcond_const; |
| 2603 | case 1: |
| 2604 | goto do_setcond_high; |
| 2605 | } |
| 2606 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2607 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2608 | op->args[4], cond); |
| 2609 | switch (i ^ inv) { |
| 2610 | case 0: |
| 2611 | goto do_setcond_const; |
| 2612 | case 1: |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2613 | goto do_setcond_low; |
| 2614 | } |
| 2615 | break; |
| 2616 | |
| 2617 | case TCG_COND_TSTEQ: |
| 2618 | case TCG_COND_TSTNE: |
Richard Henderson | a71d9df | 2024-06-30 19:46:23 -0700 | [diff] [blame] | 2619 | if (arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2620 | goto do_setcond_high; |
| 2621 | } |
| 2622 | if (arg_is_const_val(op->args[4], 0)) { |
| 2623 | goto do_setcond_low; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2624 | } |
| 2625 | break; |
| 2626 | |
| 2627 | default: |
| 2628 | break; |
| 2629 | |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2630 | do_setcond_low: |
| 2631 | op->args[2] = op->args[3]; |
| 2632 | op->args[3] = cond; |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2633 | op->opc = INDEX_op_setcond; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2634 | return fold_setcond(ctx, op); |
| 2635 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2636 | do_setcond_high: |
| 2637 | op->args[1] = op->args[2]; |
| 2638 | op->args[2] = op->args[4]; |
| 2639 | op->args[3] = cond; |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 2640 | op->opc = INDEX_op_setcond; |
Richard Henderson | ceb9ee0 | 2023-10-23 23:44:27 -0700 | [diff] [blame] | 2641 | return fold_setcond(ctx, op); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2642 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2643 | |
Richard Henderson | a53502c | 2024-12-08 20:56:36 -0600 | [diff] [blame] | 2644 | return fold_masks_z(ctx, op, 1); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2645 | |
| 2646 | do_setcond_const: |
| 2647 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2648 | } |
| 2649 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2650 | static bool fold_sextract(OptContext *ctx, TCGOp *op) |
| 2651 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2652 | uint64_t z_mask, s_mask, s_mask_old; |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2653 | TempOptInfo *t1 = arg_info(op->args[1]); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2654 | int pos = op->args[2]; |
| 2655 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2656 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2657 | if (ti_is_const(t1)) { |
| 2658 | return tcg_opt_gen_movi(ctx, op, op->args[0], |
| 2659 | sextract64(ti_const_val(t1), pos, len)); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2660 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2661 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2662 | s_mask_old = t1->s_mask; |
| 2663 | s_mask = s_mask_old >> pos; |
| 2664 | s_mask |= -1ull << (len - 1); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2665 | |
Richard Henderson | aa9e050 | 2024-12-21 22:03:53 -0800 | [diff] [blame] | 2666 | if (pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) { |
Richard Henderson | 045ace3 | 2024-12-19 10:33:51 -0800 | [diff] [blame] | 2667 | return true; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Richard Henderson | baff507 | 2024-12-08 21:09:30 -0600 | [diff] [blame] | 2670 | z_mask = sextract64(t1->z_mask, pos, len); |
| 2671 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2674 | static bool fold_shift(OptContext *ctx, TCGOp *op) |
| 2675 | { |
Richard Henderson | 4ed2ba3 | 2024-12-19 19:38:54 -0800 | [diff] [blame] | 2676 | uint64_t s_mask, z_mask; |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2677 | TempOptInfo *t1, *t2; |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2678 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2679 | if (fold_const2(ctx, op) || |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 2680 | fold_ix_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2681 | fold_xi_to_x(ctx, op, 0)) { |
| 2682 | return true; |
| 2683 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2684 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2685 | t1 = arg_info(op->args[1]); |
| 2686 | t2 = arg_info(op->args[2]); |
| 2687 | s_mask = t1->s_mask; |
| 2688 | z_mask = t1->z_mask; |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2689 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2690 | if (ti_is_const(t2)) { |
| 2691 | int sh = ti_const_val(t2); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2692 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2693 | z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2694 | s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2695 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2696 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2697 | } |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2698 | |
| 2699 | switch (op->opc) { |
Richard Henderson | 3949f36 | 2025-01-08 08:05:18 -0800 | [diff] [blame] | 2700 | case INDEX_op_sar: |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2701 | /* |
| 2702 | * Arithmetic right shift will not reduce the number of |
| 2703 | * input sign repetitions. |
| 2704 | */ |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2705 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 74dbd36 | 2025-01-07 22:52:10 -0800 | [diff] [blame] | 2706 | case INDEX_op_shr: |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2707 | /* |
| 2708 | * If the sign bit is known zero, then logical right shift |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2709 | * will not reduce the number of input sign repetitions. |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2710 | */ |
Richard Henderson | 4ed2ba3 | 2024-12-19 19:38:54 -0800 | [diff] [blame] | 2711 | if (~z_mask & -s_mask) { |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2712 | return fold_masks_s(ctx, op, s_mask); |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2713 | } |
| 2714 | break; |
| 2715 | default: |
| 2716 | break; |
| 2717 | } |
| 2718 | |
Richard Henderson | 4e9ce6a | 2024-12-08 21:13:41 -0600 | [diff] [blame] | 2719 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2722 | static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op) |
| 2723 | { |
| 2724 | TCGOpcode neg_op; |
| 2725 | bool have_neg; |
| 2726 | |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 2727 | if (!arg_is_const_val(op->args[1], 0)) { |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2728 | return false; |
| 2729 | } |
| 2730 | |
| 2731 | switch (ctx->type) { |
| 2732 | case TCG_TYPE_I32: |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2733 | case TCG_TYPE_I64: |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 2734 | neg_op = INDEX_op_neg; |
Richard Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2735 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2736 | break; |
| 2737 | case TCG_TYPE_V64: |
| 2738 | case TCG_TYPE_V128: |
| 2739 | case TCG_TYPE_V256: |
| 2740 | neg_op = INDEX_op_neg_vec; |
| 2741 | have_neg = (TCG_TARGET_HAS_neg_vec && |
| 2742 | tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0); |
| 2743 | break; |
| 2744 | default: |
| 2745 | g_assert_not_reached(); |
| 2746 | } |
| 2747 | if (have_neg) { |
| 2748 | op->opc = neg_op; |
| 2749 | op->args[1] = op->args[2]; |
Richard Henderson | e25fe88 | 2024-04-04 20:53:50 +0000 | [diff] [blame] | 2750 | return fold_neg_no_const(ctx, op); |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2751 | } |
| 2752 | return false; |
| 2753 | } |
| 2754 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2755 | /* We cannot as yet do_constant_folding with vectors. */ |
| 2756 | static bool fold_sub_vec(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2757 | { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2758 | if (fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2759 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2760 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2761 | return true; |
| 2762 | } |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2763 | return finish_folding(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2766 | static bool fold_sub(OptContext *ctx, TCGOp *op) |
| 2767 | { |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2768 | if (fold_const2(ctx, op) || |
| 2769 | fold_xx_to_i(ctx, op, 0) || |
| 2770 | fold_xi_to_x(ctx, op, 0) || |
| 2771 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2772 | return true; |
| 2773 | } |
| 2774 | |
| 2775 | /* Fold sub r,x,i to add r,x,-i */ |
| 2776 | if (arg_is_const(op->args[2])) { |
Richard Henderson | c1fa1b3 | 2025-02-17 15:17:47 -0800 | [diff] [blame] | 2777 | uint64_t val = arg_const_val(op->args[2]); |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2778 | |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 2779 | op->opc = INDEX_op_add; |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2780 | op->args[2] = arg_new_constant(ctx, -val); |
| 2781 | } |
Richard Henderson | fe1d007 | 2024-12-08 21:15:22 -0600 | [diff] [blame] | 2782 | return finish_folding(ctx, op); |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 2785 | static void squash_prev_borrowout(OptContext *ctx, TCGOp *op) |
| 2786 | { |
| 2787 | TempOptInfo *t2; |
| 2788 | |
| 2789 | op = QTAILQ_PREV(op, link); |
| 2790 | switch (op->opc) { |
| 2791 | case INDEX_op_subbo: |
| 2792 | op->opc = INDEX_op_sub; |
| 2793 | fold_sub(ctx, op); |
| 2794 | break; |
| 2795 | case INDEX_op_subbio: |
| 2796 | op->opc = INDEX_op_subbi; |
| 2797 | break; |
| 2798 | case INDEX_op_subb1o: |
| 2799 | t2 = arg_info(op->args[2]); |
| 2800 | if (ti_is_const(t2)) { |
| 2801 | op->opc = INDEX_op_add; |
| 2802 | op->args[2] = arg_new_constant(ctx, -(ti_const_val(t2) + 1)); |
| 2803 | /* Perform other constant folding, if needed. */ |
| 2804 | fold_add(ctx, op); |
| 2805 | } else { |
| 2806 | TCGArg ret = op->args[0]; |
| 2807 | op->opc = INDEX_op_sub; |
| 2808 | op = opt_insert_after(ctx, op, INDEX_op_add, 3); |
| 2809 | op->args[0] = ret; |
| 2810 | op->args[1] = ret; |
| 2811 | op->args[2] = arg_new_constant(ctx, -1); |
| 2812 | } |
| 2813 | break; |
| 2814 | default: |
| 2815 | g_assert_not_reached(); |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | static bool fold_subbi(OptContext *ctx, TCGOp *op) |
| 2820 | { |
| 2821 | TempOptInfo *t2; |
| 2822 | int borrow_in = ctx->carry_state; |
| 2823 | |
| 2824 | if (borrow_in < 0) { |
| 2825 | return finish_folding(ctx, op); |
| 2826 | } |
| 2827 | ctx->carry_state = -1; |
| 2828 | |
| 2829 | squash_prev_borrowout(ctx, op); |
| 2830 | if (borrow_in == 0) { |
| 2831 | op->opc = INDEX_op_sub; |
| 2832 | return fold_sub(ctx, op); |
| 2833 | } |
| 2834 | |
| 2835 | /* |
| 2836 | * Propagate the known carry-in into any constant, then negate to |
| 2837 | * transform from sub to add. If there is no constant, emit a |
| 2838 | * separate add -1. |
| 2839 | */ |
| 2840 | t2 = arg_info(op->args[2]); |
| 2841 | if (ti_is_const(t2)) { |
| 2842 | op->args[2] = arg_new_constant(ctx, -(ti_const_val(t2) + 1)); |
| 2843 | } else { |
| 2844 | TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_sub, 3); |
| 2845 | |
| 2846 | op2->args[0] = op->args[0]; |
| 2847 | op2->args[1] = op->args[1]; |
| 2848 | op2->args[2] = op->args[2]; |
| 2849 | fold_sub(ctx, op2); |
| 2850 | |
| 2851 | op->args[1] = op->args[0]; |
| 2852 | op->args[2] = arg_new_constant(ctx, -1); |
| 2853 | } |
| 2854 | op->opc = INDEX_op_add; |
| 2855 | return fold_add(ctx, op); |
| 2856 | } |
| 2857 | |
| 2858 | static bool fold_subbio(OptContext *ctx, TCGOp *op) |
| 2859 | { |
| 2860 | TempOptInfo *t1, *t2; |
| 2861 | int borrow_out = -1; |
| 2862 | |
| 2863 | if (ctx->carry_state < 0) { |
| 2864 | return finish_folding(ctx, op); |
| 2865 | } |
| 2866 | |
| 2867 | squash_prev_borrowout(ctx, op); |
| 2868 | if (ctx->carry_state == 0) { |
| 2869 | goto do_subbo; |
| 2870 | } |
| 2871 | |
| 2872 | t1 = arg_info(op->args[1]); |
| 2873 | t2 = arg_info(op->args[2]); |
| 2874 | |
| 2875 | /* Propagate the known borrow-in into a constant, if possible. */ |
| 2876 | if (ti_is_const(t2)) { |
| 2877 | uint64_t max = ctx->type == TCG_TYPE_I32 ? UINT32_MAX : UINT64_MAX; |
| 2878 | uint64_t v = ti_const_val(t2) & max; |
| 2879 | |
| 2880 | if (v < max) { |
| 2881 | op->args[2] = arg_new_constant(ctx, v + 1); |
| 2882 | goto do_subbo; |
| 2883 | } |
| 2884 | /* subtracting max + 1 produces known borrow out. */ |
| 2885 | borrow_out = 1; |
| 2886 | } |
| 2887 | if (ti_is_const(t1)) { |
| 2888 | uint64_t v = ti_const_val(t1); |
| 2889 | if (v != 0) { |
| 2890 | op->args[2] = arg_new_constant(ctx, v - 1); |
| 2891 | goto do_subbo; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | /* Adjust the opcode to remember the known carry-in. */ |
| 2896 | op->opc = INDEX_op_subb1o; |
| 2897 | ctx->carry_state = borrow_out; |
| 2898 | return finish_folding(ctx, op); |
| 2899 | |
| 2900 | do_subbo: |
| 2901 | op->opc = INDEX_op_subbo; |
| 2902 | return fold_subbo(ctx, op); |
| 2903 | } |
| 2904 | |
| 2905 | static bool fold_subbo(OptContext *ctx, TCGOp *op) |
| 2906 | { |
| 2907 | TempOptInfo *t1 = arg_info(op->args[1]); |
| 2908 | TempOptInfo *t2 = arg_info(op->args[2]); |
| 2909 | int borrow_out = -1; |
| 2910 | |
| 2911 | if (ti_is_const(t2)) { |
| 2912 | uint64_t v2 = ti_const_val(t2); |
| 2913 | if (v2 == 0) { |
| 2914 | borrow_out = 0; |
| 2915 | } else if (ti_is_const(t1)) { |
| 2916 | uint64_t v1 = ti_const_val(t1); |
| 2917 | borrow_out = v1 < v2; |
| 2918 | } |
| 2919 | } |
| 2920 | ctx->carry_state = borrow_out; |
| 2921 | return finish_folding(ctx, op); |
| 2922 | } |
| 2923 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2924 | static bool fold_tcg_ld(OptContext *ctx, TCGOp *op) |
| 2925 | { |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2926 | uint64_t z_mask = -1, s_mask = 0; |
| 2927 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2928 | /* We can't do any folding with a load, but we can record bits. */ |
| 2929 | switch (op->opc) { |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2930 | case INDEX_op_ld8s: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2931 | s_mask = INT8_MIN; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2932 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2933 | case INDEX_op_ld8u: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2934 | z_mask = MAKE_64BIT_MASK(0, 8); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2935 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2936 | case INDEX_op_ld16s: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2937 | s_mask = INT16_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2938 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2939 | case INDEX_op_ld16u: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2940 | z_mask = MAKE_64BIT_MASK(0, 16); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2941 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2942 | case INDEX_op_ld32s: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2943 | s_mask = INT32_MIN; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2944 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 2945 | case INDEX_op_ld32u: |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2946 | z_mask = MAKE_64BIT_MASK(0, 32); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2947 | break; |
| 2948 | default: |
| 2949 | g_assert_not_reached(); |
| 2950 | } |
Richard Henderson | d33e0f0 | 2024-12-09 08:53:20 -0600 | [diff] [blame] | 2951 | return fold_masks_zs(ctx, op, z_mask, s_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2954 | static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op) |
| 2955 | { |
| 2956 | TCGTemp *dst, *src; |
| 2957 | intptr_t ofs; |
| 2958 | TCGType type; |
| 2959 | |
| 2960 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
Richard Henderson | 0fb5b75 | 2024-12-09 09:44:40 -0600 | [diff] [blame] | 2961 | return finish_folding(ctx, op); |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | type = ctx->type; |
| 2965 | ofs = op->args[2]; |
| 2966 | dst = arg_temp(op->args[0]); |
| 2967 | src = find_mem_copy_for(ctx, type, ofs); |
| 2968 | if (src && src->base_type == type) { |
| 2969 | return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src)); |
| 2970 | } |
| 2971 | |
| 2972 | reset_ts(ctx, dst); |
| 2973 | record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1); |
| 2974 | return true; |
| 2975 | } |
| 2976 | |
| 2977 | static bool fold_tcg_st(OptContext *ctx, TCGOp *op) |
| 2978 | { |
| 2979 | intptr_t ofs = op->args[2]; |
| 2980 | intptr_t lm1; |
| 2981 | |
| 2982 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
| 2983 | remove_mem_copy_all(ctx); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 2984 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | switch (op->opc) { |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 2988 | case INDEX_op_st8: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2989 | lm1 = 0; |
| 2990 | break; |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 2991 | case INDEX_op_st16: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2992 | lm1 = 1; |
| 2993 | break; |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 2994 | case INDEX_op_st32: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2995 | lm1 = 3; |
| 2996 | break; |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 2997 | case INDEX_op_st: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2998 | case INDEX_op_st_vec: |
| 2999 | lm1 = tcg_type_size(ctx->type) - 1; |
| 3000 | break; |
| 3001 | default: |
| 3002 | g_assert_not_reached(); |
| 3003 | } |
| 3004 | remove_mem_copy_in(ctx, ofs, ofs + lm1); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 3005 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op) |
| 3009 | { |
| 3010 | TCGTemp *src; |
| 3011 | intptr_t ofs, last; |
| 3012 | TCGType type; |
| 3013 | |
| 3014 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 3015 | return fold_tcg_st(ctx, op); |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | src = arg_temp(op->args[0]); |
| 3019 | ofs = op->args[2]; |
| 3020 | type = ctx->type; |
Richard Henderson | 3eaadae | 2023-08-23 23:13:06 -0700 | [diff] [blame] | 3021 | |
| 3022 | /* |
| 3023 | * Eliminate duplicate stores of a constant. |
| 3024 | * This happens frequently when the target ISA zero-extends. |
| 3025 | */ |
| 3026 | if (ts_is_const(src)) { |
| 3027 | TCGTemp *prev = find_mem_copy_for(ctx, type, ofs); |
| 3028 | if (src == prev) { |
| 3029 | tcg_op_remove(ctx->tcg, op); |
| 3030 | return true; |
| 3031 | } |
| 3032 | } |
| 3033 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3034 | last = ofs + tcg_type_size(type) - 1; |
| 3035 | remove_mem_copy_in(ctx, ofs, last); |
| 3036 | record_mem_copy(ctx, type, src, ofs, last); |
Richard Henderson | 082b3ef | 2024-12-08 20:34:57 -0600 | [diff] [blame] | 3037 | return true; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3040 | static bool fold_xor(OptContext *ctx, TCGOp *op) |
| 3041 | { |
Richard Henderson | 787190e | 2024-12-10 08:39:56 -0600 | [diff] [blame^] | 3042 | uint64_t z_mask, o_mask, s_mask; |
Richard Henderson | c890fd7 | 2024-12-08 21:39:01 -0600 | [diff] [blame] | 3043 | TempOptInfo *t1, *t2; |
| 3044 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 3045 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 3046 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 3047 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 3048 | fold_xi_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 3049 | return true; |
| 3050 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 3051 | |
Richard Henderson | c890fd7 | 2024-12-08 21:39:01 -0600 | [diff] [blame] | 3052 | t1 = arg_info(op->args[1]); |
| 3053 | t2 = arg_info(op->args[2]); |
Richard Henderson | 787190e | 2024-12-10 08:39:56 -0600 | [diff] [blame^] | 3054 | |
| 3055 | z_mask = (t1->z_mask | t2->z_mask) & ~(t1->o_mask & t2->o_mask); |
| 3056 | 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] | 3057 | s_mask = t1->s_mask & t2->s_mask; |
Richard Henderson | 787190e | 2024-12-10 08:39:56 -0600 | [diff] [blame^] | 3058 | |
| 3059 | return fold_masks_zos(ctx, op, z_mask, o_mask, s_mask); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 3062 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 3063 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3064 | { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 3065 | int nb_temps, i; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 3066 | TCGOp *op, *op_next; |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 3067 | OptContext ctx = { .tcg = s }; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 3068 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3069 | QSIMPLEQ_INIT(&ctx.mem_free); |
| 3070 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 3071 | /* Array VALS has an element for each temp. |
| 3072 | 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] | 3073 | If this temp is a copy of other ones then the other copies are |
| 3074 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3075 | |
| 3076 | nb_temps = s->nb_temps; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 3077 | for (i = 0; i < nb_temps; ++i) { |
| 3078 | s->temps[i].state_ptr = NULL; |
| 3079 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3080 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 3081 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 3082 | TCGOpcode opc = op->opc; |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 3083 | const TCGOpDef *def; |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 3084 | bool done = false; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 3085 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 3086 | /* Calls are special. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 3087 | if (opc == INDEX_op_call) { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 3088 | fold_call(&ctx, op); |
| 3089 | continue; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 3090 | } |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 3091 | |
| 3092 | def = &tcg_op_defs[opc]; |
Richard Henderson | ec5d4cb | 2021-08-24 08:20:27 -0700 | [diff] [blame] | 3093 | init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); |
| 3094 | copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 3095 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 3096 | /* Pre-compute the type of the operation. */ |
Richard Henderson | 4d87221 | 2025-01-02 19:43:06 -0800 | [diff] [blame] | 3097 | ctx.type = TCGOP_TYPE(op); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 3098 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3099 | /* |
| 3100 | * Process each opcode. |
| 3101 | * Sorted alphabetically by opcode as much as possible. |
| 3102 | */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 3103 | switch (opc) { |
Richard Henderson | 79602f6 | 2025-01-06 09:11:39 -0800 | [diff] [blame] | 3104 | case INDEX_op_add: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3105 | done = fold_add(&ctx, op); |
| 3106 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 3107 | case INDEX_op_add_vec: |
| 3108 | done = fold_add_vec(&ctx, op); |
| 3109 | break; |
Richard Henderson | 76f4278 | 2025-01-14 13:58:39 -0800 | [diff] [blame] | 3110 | case INDEX_op_addci: |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 3111 | done = fold_addci(&ctx, op); |
| 3112 | break; |
Richard Henderson | 76f4278 | 2025-01-14 13:58:39 -0800 | [diff] [blame] | 3113 | case INDEX_op_addcio: |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 3114 | done = fold_addcio(&ctx, op); |
| 3115 | break; |
| 3116 | case INDEX_op_addco: |
| 3117 | done = fold_addco(&ctx, op); |
Richard Henderson | 76f4278 | 2025-01-14 13:58:39 -0800 | [diff] [blame] | 3118 | break; |
Richard Henderson | c3b920b | 2025-01-06 10:32:44 -0800 | [diff] [blame] | 3119 | case INDEX_op_and: |
| 3120 | case INDEX_op_and_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3121 | done = fold_and(&ctx, op); |
| 3122 | break; |
Richard Henderson | 46f96bf | 2025-01-06 12:37:02 -0800 | [diff] [blame] | 3123 | case INDEX_op_andc: |
| 3124 | case INDEX_op_andc_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3125 | done = fold_andc(&ctx, op); |
| 3126 | break; |
Richard Henderson | b6d69fc | 2025-01-10 11:49:22 -0800 | [diff] [blame] | 3127 | case INDEX_op_brcond: |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 3128 | done = fold_brcond(&ctx, op); |
| 3129 | break; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 3130 | case INDEX_op_brcond2_i32: |
| 3131 | done = fold_brcond2(&ctx, op); |
| 3132 | break; |
Richard Henderson | 0dd07ee | 2025-01-10 18:51:16 -0800 | [diff] [blame] | 3133 | case INDEX_op_bswap16: |
Richard Henderson | 7498d88 | 2025-01-10 19:53:51 -0800 | [diff] [blame] | 3134 | case INDEX_op_bswap32: |
Richard Henderson | 3ad5d4c | 2025-01-10 21:54:44 -0800 | [diff] [blame] | 3135 | case INDEX_op_bswap64: |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 3136 | done = fold_bswap(&ctx, op); |
| 3137 | break; |
Richard Henderson | 5a5bb0a | 2025-01-08 16:12:46 -0800 | [diff] [blame] | 3138 | case INDEX_op_clz: |
Richard Henderson | c96447d | 2025-01-08 17:07:01 -0800 | [diff] [blame] | 3139 | case INDEX_op_ctz: |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 3140 | done = fold_count_zeros(&ctx, op); |
| 3141 | break; |
Richard Henderson | 97218ae | 2025-01-08 18:37:43 -0800 | [diff] [blame] | 3142 | case INDEX_op_ctpop: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3143 | done = fold_ctpop(&ctx, op); |
| 3144 | break; |
Richard Henderson | 4d137ff | 2025-01-12 20:48:57 -0800 | [diff] [blame] | 3145 | case INDEX_op_deposit: |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 3146 | done = fold_deposit(&ctx, op); |
| 3147 | break; |
Richard Henderson | b2c514f | 2025-01-07 13:22:56 -0800 | [diff] [blame] | 3148 | case INDEX_op_divs: |
Richard Henderson | 961b80a | 2025-01-07 14:27:19 -0800 | [diff] [blame] | 3149 | case INDEX_op_divu: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3150 | done = fold_divide(&ctx, op); |
| 3151 | break; |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 3152 | case INDEX_op_dup_vec: |
| 3153 | done = fold_dup(&ctx, op); |
| 3154 | break; |
| 3155 | case INDEX_op_dup2_vec: |
| 3156 | done = fold_dup2(&ctx, op); |
| 3157 | break; |
Richard Henderson | 5c0968a | 2025-01-06 15:47:53 -0800 | [diff] [blame] | 3158 | case INDEX_op_eqv: |
| 3159 | case INDEX_op_eqv_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3160 | done = fold_eqv(&ctx, op); |
| 3161 | break; |
Richard Henderson | 07d5d50 | 2025-01-11 09:01:46 -0800 | [diff] [blame] | 3162 | case INDEX_op_extract: |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 3163 | done = fold_extract(&ctx, op); |
| 3164 | break; |
Richard Henderson | 61d6a87 | 2025-01-12 21:40:43 -0800 | [diff] [blame] | 3165 | case INDEX_op_extract2: |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 3166 | done = fold_extract2(&ctx, op); |
| 3167 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3168 | case INDEX_op_ext_i32_i64: |
| 3169 | done = fold_exts(&ctx, op); |
| 3170 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3171 | case INDEX_op_extu_i32_i64: |
| 3172 | case INDEX_op_extrl_i64_i32: |
| 3173 | case INDEX_op_extrh_i64_i32: |
| 3174 | done = fold_extu(&ctx, op); |
| 3175 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 3176 | case INDEX_op_ld8s: |
| 3177 | case INDEX_op_ld8u: |
| 3178 | case INDEX_op_ld16s: |
| 3179 | case INDEX_op_ld16u: |
| 3180 | case INDEX_op_ld32s: |
| 3181 | case INDEX_op_ld32u: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 3182 | done = fold_tcg_ld(&ctx, op); |
| 3183 | break; |
Richard Henderson | e996804 | 2025-01-21 21:47:16 -0800 | [diff] [blame] | 3184 | case INDEX_op_ld: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3185 | case INDEX_op_ld_vec: |
| 3186 | done = fold_tcg_ld_memcopy(&ctx, op); |
| 3187 | break; |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 3188 | case INDEX_op_st8: |
| 3189 | case INDEX_op_st16: |
| 3190 | case INDEX_op_st32: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3191 | done = fold_tcg_st(&ctx, op); |
| 3192 | break; |
Richard Henderson | a28f151 | 2025-01-22 13:28:55 -0800 | [diff] [blame] | 3193 | case INDEX_op_st: |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 3194 | case INDEX_op_st_vec: |
| 3195 | done = fold_tcg_st_memcopy(&ctx, op); |
| 3196 | break; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3197 | case INDEX_op_mb: |
| 3198 | done = fold_mb(&ctx, op); |
| 3199 | break; |
Richard Henderson | b570126 | 2024-12-28 15:58:24 -0800 | [diff] [blame] | 3200 | case INDEX_op_mov: |
| 3201 | case INDEX_op_mov_vec: |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3202 | done = fold_mov(&ctx, op); |
| 3203 | break; |
Richard Henderson | ea46c4b | 2025-01-10 13:41:25 -0800 | [diff] [blame] | 3204 | case INDEX_op_movcond: |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 3205 | done = fold_movcond(&ctx, op); |
| 3206 | break; |
Richard Henderson | d2c3eca | 2025-01-07 09:32:18 -0800 | [diff] [blame] | 3207 | case INDEX_op_mul: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3208 | done = fold_mul(&ctx, op); |
| 3209 | break; |
Richard Henderson | c742824 | 2025-01-07 11:19:29 -0800 | [diff] [blame] | 3210 | case INDEX_op_mulsh: |
Richard Henderson | aa28c9e | 2025-01-07 10:36:24 -0800 | [diff] [blame] | 3211 | case INDEX_op_muluh: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3212 | done = fold_mul_highpart(&ctx, op); |
| 3213 | break; |
Richard Henderson | bfe9648 | 2025-01-09 07:24:32 -0800 | [diff] [blame] | 3214 | case INDEX_op_muls2: |
Richard Henderson | d776198 | 2025-01-09 09:11:53 -0800 | [diff] [blame] | 3215 | case INDEX_op_mulu2: |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 3216 | done = fold_multiply2(&ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 3217 | break; |
Richard Henderson | 59379a4 | 2025-01-06 20:32:54 -0800 | [diff] [blame] | 3218 | case INDEX_op_nand: |
| 3219 | case INDEX_op_nand_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3220 | done = fold_nand(&ctx, op); |
| 3221 | break; |
Richard Henderson | 6971358 | 2025-01-06 22:48:57 -0800 | [diff] [blame] | 3222 | case INDEX_op_neg: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3223 | done = fold_neg(&ctx, op); |
| 3224 | break; |
Richard Henderson | 3a8c4e9 | 2025-01-06 21:02:17 -0800 | [diff] [blame] | 3225 | case INDEX_op_nor: |
| 3226 | case INDEX_op_nor_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3227 | done = fold_nor(&ctx, op); |
| 3228 | break; |
Richard Henderson | 5c62d37 | 2025-01-06 23:46:47 -0800 | [diff] [blame] | 3229 | case INDEX_op_not: |
| 3230 | case INDEX_op_not_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3231 | done = fold_not(&ctx, op); |
| 3232 | break; |
Richard Henderson | 49bd751 | 2025-01-06 14:00:40 -0800 | [diff] [blame] | 3233 | case INDEX_op_or: |
| 3234 | case INDEX_op_or_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3235 | done = fold_or(&ctx, op); |
| 3236 | break; |
Richard Henderson | 6aba25e | 2025-01-06 14:46:26 -0800 | [diff] [blame] | 3237 | case INDEX_op_orc: |
| 3238 | case INDEX_op_orc_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3239 | done = fold_orc(&ctx, op); |
| 3240 | break; |
Richard Henderson | aae2456 | 2025-02-09 12:55:15 -0800 | [diff] [blame] | 3241 | case INDEX_op_qemu_ld: |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 3242 | done = fold_qemu_ld_1reg(&ctx, op); |
| 3243 | break; |
Richard Henderson | aae2456 | 2025-02-09 12:55:15 -0800 | [diff] [blame] | 3244 | case INDEX_op_qemu_ld2: |
Richard Henderson | 6813be9 | 2024-12-08 20:33:30 -0600 | [diff] [blame] | 3245 | done = fold_qemu_ld_2reg(&ctx, op); |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3246 | break; |
Richard Henderson | aae2456 | 2025-02-09 12:55:15 -0800 | [diff] [blame] | 3247 | case INDEX_op_qemu_st: |
| 3248 | case INDEX_op_qemu_st2: |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 3249 | done = fold_qemu_st(&ctx, op); |
| 3250 | break; |
Richard Henderson | 9a6bc18 | 2025-01-07 19:00:51 -0800 | [diff] [blame] | 3251 | case INDEX_op_rems: |
Richard Henderson | cd9acd2 | 2025-01-07 20:25:14 -0800 | [diff] [blame] | 3252 | case INDEX_op_remu: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3253 | done = fold_remainder(&ctx, op); |
| 3254 | break; |
Richard Henderson | 005a87e | 2025-01-08 10:42:16 -0800 | [diff] [blame] | 3255 | case INDEX_op_rotl: |
| 3256 | case INDEX_op_rotr: |
Richard Henderson | 3949f36 | 2025-01-08 08:05:18 -0800 | [diff] [blame] | 3257 | case INDEX_op_sar: |
Richard Henderson | 6ca5945 | 2025-01-07 21:50:04 -0800 | [diff] [blame] | 3258 | case INDEX_op_shl: |
Richard Henderson | 74dbd36 | 2025-01-07 22:52:10 -0800 | [diff] [blame] | 3259 | case INDEX_op_shr: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3260 | done = fold_shift(&ctx, op); |
| 3261 | break; |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 3262 | case INDEX_op_setcond: |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 3263 | done = fold_setcond(&ctx, op); |
| 3264 | break; |
Richard Henderson | a363e1e | 2025-01-10 09:26:44 -0800 | [diff] [blame] | 3265 | case INDEX_op_negsetcond: |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 3266 | done = fold_negsetcond(&ctx, op); |
| 3267 | break; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 3268 | case INDEX_op_setcond2_i32: |
| 3269 | done = fold_setcond2(&ctx, op); |
| 3270 | break; |
Richard Henderson | 1f10654 | 2024-09-06 12:22:41 -0700 | [diff] [blame] | 3271 | case INDEX_op_cmp_vec: |
| 3272 | done = fold_cmp_vec(&ctx, op); |
| 3273 | break; |
| 3274 | case INDEX_op_cmpsel_vec: |
| 3275 | done = fold_cmpsel_vec(&ctx, op); |
| 3276 | break; |
Richard Henderson | e58b977 | 2024-09-06 22:30:01 -0700 | [diff] [blame] | 3277 | case INDEX_op_bitsel_vec: |
| 3278 | done = fold_bitsel_vec(&ctx, op); |
| 3279 | break; |
Richard Henderson | fa361ee | 2025-01-12 11:50:09 -0800 | [diff] [blame] | 3280 | case INDEX_op_sextract: |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 3281 | done = fold_sextract(&ctx, op); |
| 3282 | break; |
Richard Henderson | 60f34f5 | 2025-01-06 22:06:32 -0800 | [diff] [blame] | 3283 | case INDEX_op_sub: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3284 | done = fold_sub(&ctx, op); |
| 3285 | break; |
Richard Henderson | aeb3514 | 2025-01-14 18:28:15 -0800 | [diff] [blame] | 3286 | case INDEX_op_subbi: |
| 3287 | done = fold_subbi(&ctx, op); |
| 3288 | break; |
| 3289 | case INDEX_op_subbio: |
| 3290 | done = fold_subbio(&ctx, op); |
| 3291 | break; |
| 3292 | case INDEX_op_subbo: |
| 3293 | done = fold_subbo(&ctx, op); |
| 3294 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 3295 | case INDEX_op_sub_vec: |
| 3296 | done = fold_sub_vec(&ctx, op); |
| 3297 | break; |
Richard Henderson | fffd3dc | 2025-01-06 15:18:35 -0800 | [diff] [blame] | 3298 | case INDEX_op_xor: |
| 3299 | case INDEX_op_xor_vec: |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 3300 | done = fold_xor(&ctx, op); |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3301 | break; |
Richard Henderson | 1526855 | 2024-12-08 07:45:11 -0600 | [diff] [blame] | 3302 | case INDEX_op_set_label: |
| 3303 | case INDEX_op_br: |
| 3304 | case INDEX_op_exit_tb: |
| 3305 | case INDEX_op_goto_tb: |
| 3306 | case INDEX_op_goto_ptr: |
| 3307 | finish_ebb(&ctx); |
| 3308 | done = true; |
| 3309 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3310 | default: |
Richard Henderson | 0ae5642 | 2024-12-08 21:42:53 -0600 | [diff] [blame] | 3311 | done = finish_folding(&ctx, op); |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 3312 | break; |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 3313 | } |
Richard Henderson | 0ae5642 | 2024-12-08 21:42:53 -0600 | [diff] [blame] | 3314 | tcg_debug_assert(done); |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3315 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 3316 | } |