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