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" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 31 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 32 | #define CASE_OP_32_64(x) \ |
| 33 | glue(glue(case INDEX_op_, x), _i32): \ |
| 34 | glue(glue(case INDEX_op_, x), _i64) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 35 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 36 | #define CASE_OP_32_64_VEC(x) \ |
| 37 | glue(glue(case INDEX_op_, x), _i32): \ |
| 38 | glue(glue(case INDEX_op_, x), _i64): \ |
| 39 | glue(glue(case INDEX_op_, x), _vec) |
| 40 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 41 | typedef struct MemCopyInfo { |
| 42 | IntervalTreeNode itree; |
| 43 | QSIMPLEQ_ENTRY (MemCopyInfo) next; |
| 44 | TCGTemp *ts; |
| 45 | TCGType type; |
| 46 | } MemCopyInfo; |
| 47 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 48 | typedef struct TempOptInfo { |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 49 | bool is_const; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 50 | TCGTemp *prev_copy; |
| 51 | TCGTemp *next_copy; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 52 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy; |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 53 | uint64_t val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 54 | uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */ |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 55 | uint64_t s_mask; /* a left-aligned mask of clrsb(value) bits. */ |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 56 | } TempOptInfo; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 57 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 58 | typedef struct OptContext { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 59 | TCGContext *tcg; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 60 | TCGOp *prev_mb; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 61 | TCGTempSet temps_used; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 62 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 63 | IntervalTreeRoot mem_copy; |
| 64 | QSIMPLEQ_HEAD(, MemCopyInfo) mem_free; |
| 65 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 66 | /* In flight values from optimization. */ |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 67 | uint64_t a_mask; /* mask bit is 0 iff value identical to first input */ |
| 68 | uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */ |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 69 | uint64_t s_mask; /* mask of clrsb(value) bits */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 70 | TCGType type; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 71 | } OptContext; |
| 72 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 73 | /* Calculate the smask for a specific value. */ |
| 74 | static uint64_t smask_from_value(uint64_t value) |
| 75 | { |
| 76 | int rep = clrsb64(value); |
| 77 | return ~(~0ull >> rep); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Calculate the smask for a given set of known-zeros. |
| 82 | * If there are lots of zeros on the left, we can consider the remainder |
| 83 | * an unsigned field, and thus the corresponding signed field is one bit |
| 84 | * larger. |
| 85 | */ |
| 86 | static uint64_t smask_from_zmask(uint64_t zmask) |
| 87 | { |
| 88 | /* |
| 89 | * Only the 0 bits are significant for zmask, thus the msb itself |
| 90 | * must be zero, else we have no sign information. |
| 91 | */ |
| 92 | int rep = clz64(zmask); |
| 93 | if (rep == 0) { |
| 94 | return 0; |
| 95 | } |
| 96 | rep -= 1; |
| 97 | return ~(~0ull >> rep); |
| 98 | } |
| 99 | |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Recreate a properly left-aligned smask after manipulation. |
| 102 | * Some bit-shuffling, particularly shifts and rotates, may |
| 103 | * retain sign bits on the left, but may scatter disconnected |
| 104 | * sign bits on the right. Retain only what remains to the left. |
| 105 | */ |
| 106 | static uint64_t smask_from_smask(int64_t smask) |
| 107 | { |
| 108 | /* Only the 1 bits are significant for smask */ |
| 109 | return smask_from_zmask(~smask); |
| 110 | } |
| 111 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 112 | static inline TempOptInfo *ts_info(TCGTemp *ts) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 113 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 114 | return ts->state_ptr; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 117 | static inline TempOptInfo *arg_info(TCGArg arg) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 118 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 119 | return ts_info(arg_temp(arg)); |
| 120 | } |
| 121 | |
| 122 | static inline bool ts_is_const(TCGTemp *ts) |
| 123 | { |
| 124 | return ts_info(ts)->is_const; |
| 125 | } |
| 126 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 127 | static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val) |
| 128 | { |
| 129 | TempOptInfo *ti = ts_info(ts); |
| 130 | return ti->is_const && ti->val == val; |
| 131 | } |
| 132 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 133 | static inline bool arg_is_const(TCGArg arg) |
| 134 | { |
| 135 | return ts_is_const(arg_temp(arg)); |
| 136 | } |
| 137 | |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 138 | static inline bool arg_is_const_val(TCGArg arg, uint64_t val) |
| 139 | { |
| 140 | return ts_is_const_val(arg_temp(arg), val); |
| 141 | } |
| 142 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 143 | static inline bool ts_is_copy(TCGTemp *ts) |
| 144 | { |
| 145 | return ts_info(ts)->next_copy != ts; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 148 | static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b) |
| 149 | { |
| 150 | return a->kind < b->kind ? b : a; |
| 151 | } |
| 152 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 153 | /* Initialize and activate a temporary. */ |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 154 | static void init_ts_info(OptContext *ctx, TCGTemp *ts) |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 155 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 156 | size_t idx = temp_idx(ts); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 157 | TempOptInfo *ti; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 158 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 159 | if (test_bit(idx, ctx->temps_used.l)) { |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 162 | set_bit(idx, ctx->temps_used.l); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 163 | |
| 164 | ti = ts->state_ptr; |
| 165 | if (ti == NULL) { |
| 166 | ti = tcg_malloc(sizeof(TempOptInfo)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 167 | ts->state_ptr = ti; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ti->next_copy = ts; |
| 171 | ti->prev_copy = ts; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 172 | QSIMPLEQ_INIT(&ti->mem_copy); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 173 | if (ts->kind == TEMP_CONST) { |
| 174 | ti->is_const = true; |
| 175 | ti->val = ts->val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 176 | ti->z_mask = ts->val; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 177 | ti->s_mask = smask_from_value(ts->val); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 178 | } else { |
| 179 | ti->is_const = false; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 180 | ti->z_mask = -1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 181 | ti->s_mask = 0; |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 185 | static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l) |
| 186 | { |
| 187 | IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l); |
| 188 | return r ? container_of(r, MemCopyInfo, itree) : NULL; |
| 189 | } |
| 190 | |
| 191 | static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l) |
| 192 | { |
| 193 | IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l); |
| 194 | return r ? container_of(r, MemCopyInfo, itree) : NULL; |
| 195 | } |
| 196 | |
| 197 | static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc) |
| 198 | { |
| 199 | TCGTemp *ts = mc->ts; |
| 200 | TempOptInfo *ti = ts_info(ts); |
| 201 | |
| 202 | interval_tree_remove(&mc->itree, &ctx->mem_copy); |
| 203 | QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next); |
| 204 | QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next); |
| 205 | } |
| 206 | |
| 207 | static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l) |
| 208 | { |
| 209 | while (true) { |
| 210 | MemCopyInfo *mc = mem_copy_first(ctx, s, l); |
| 211 | if (!mc) { |
| 212 | break; |
| 213 | } |
| 214 | remove_mem_copy(ctx, mc); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void remove_mem_copy_all(OptContext *ctx) |
| 219 | { |
| 220 | remove_mem_copy_in(ctx, 0, -1); |
| 221 | tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy)); |
| 222 | } |
| 223 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 224 | static TCGTemp *find_better_copy(TCGTemp *ts) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 225 | { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 226 | TCGTemp *i, *ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 227 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 228 | /* If this is already readonly, we can't do better. */ |
| 229 | if (temp_readonly(ts)) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 230 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 233 | ret = ts; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 234 | 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] | 235 | ret = cmp_better_copy(ret, i); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 236 | } |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 237 | return ret; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 240 | static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts) |
| 241 | { |
| 242 | TempOptInfo *si = ts_info(src_ts); |
| 243 | TempOptInfo *di = ts_info(dst_ts); |
| 244 | MemCopyInfo *mc; |
| 245 | |
| 246 | QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) { |
| 247 | tcg_debug_assert(mc->ts == src_ts); |
| 248 | mc->ts = dst_ts; |
| 249 | } |
| 250 | QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy); |
| 251 | } |
| 252 | |
| 253 | /* Reset TEMP's state, possibly removing the temp for the list of copies. */ |
| 254 | static void reset_ts(OptContext *ctx, TCGTemp *ts) |
| 255 | { |
| 256 | TempOptInfo *ti = ts_info(ts); |
| 257 | TCGTemp *pts = ti->prev_copy; |
| 258 | TCGTemp *nts = ti->next_copy; |
| 259 | TempOptInfo *pi = ts_info(pts); |
| 260 | TempOptInfo *ni = ts_info(nts); |
| 261 | |
| 262 | ni->prev_copy = ti->prev_copy; |
| 263 | pi->next_copy = ti->next_copy; |
| 264 | ti->next_copy = ts; |
| 265 | ti->prev_copy = ts; |
| 266 | ti->is_const = false; |
| 267 | ti->z_mask = -1; |
| 268 | ti->s_mask = 0; |
| 269 | |
| 270 | if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) { |
| 271 | if (ts == nts) { |
| 272 | /* Last temp copy being removed, the mem copies die. */ |
| 273 | MemCopyInfo *mc; |
| 274 | QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) { |
| 275 | interval_tree_remove(&mc->itree, &ctx->mem_copy); |
| 276 | } |
| 277 | QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy); |
| 278 | } else { |
| 279 | move_mem_copies(find_better_copy(nts), ts); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static void reset_temp(OptContext *ctx, TCGArg arg) |
| 285 | { |
| 286 | reset_ts(ctx, arg_temp(arg)); |
| 287 | } |
| 288 | |
| 289 | static void record_mem_copy(OptContext *ctx, TCGType type, |
| 290 | TCGTemp *ts, intptr_t start, intptr_t last) |
| 291 | { |
| 292 | MemCopyInfo *mc; |
| 293 | TempOptInfo *ti; |
| 294 | |
| 295 | mc = QSIMPLEQ_FIRST(&ctx->mem_free); |
| 296 | if (mc) { |
| 297 | QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next); |
| 298 | } else { |
| 299 | mc = tcg_malloc(sizeof(*mc)); |
| 300 | } |
| 301 | |
| 302 | memset(mc, 0, sizeof(*mc)); |
| 303 | mc->itree.start = start; |
| 304 | mc->itree.last = last; |
| 305 | mc->type = type; |
| 306 | interval_tree_insert(&mc->itree, &ctx->mem_copy); |
| 307 | |
| 308 | ts = find_better_copy(ts); |
| 309 | ti = ts_info(ts); |
| 310 | mc->ts = ts; |
| 311 | QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next); |
| 312 | } |
| 313 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 314 | static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 315 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 316 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 317 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 318 | if (ts1 == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 322 | if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 326 | for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { |
| 327 | if (i == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 335 | static bool args_are_copies(TCGArg arg1, TCGArg arg2) |
| 336 | { |
| 337 | return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); |
| 338 | } |
| 339 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 340 | static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s) |
| 341 | { |
| 342 | MemCopyInfo *mc; |
| 343 | |
| 344 | for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) { |
| 345 | if (mc->itree.start == s && mc->type == type) { |
| 346 | return find_better_copy(mc->ts); |
| 347 | } |
| 348 | } |
| 349 | return NULL; |
| 350 | } |
| 351 | |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 352 | static TCGArg arg_new_constant(OptContext *ctx, uint64_t val) |
| 353 | { |
| 354 | TCGType type = ctx->type; |
| 355 | TCGTemp *ts; |
| 356 | |
| 357 | if (type == TCG_TYPE_I32) { |
| 358 | val = (int32_t)val; |
| 359 | } |
| 360 | |
| 361 | ts = tcg_constant_internal(type, val); |
| 362 | init_ts_info(ctx, ts); |
| 363 | |
| 364 | return temp_arg(ts); |
| 365 | } |
| 366 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 367 | 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] | 368 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 369 | TCGTemp *dst_ts = arg_temp(dst); |
| 370 | TCGTemp *src_ts = arg_temp(src); |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 371 | TempOptInfo *di; |
| 372 | TempOptInfo *si; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 373 | TCGOpcode new_op; |
| 374 | |
| 375 | if (ts_are_copies(dst_ts, src_ts)) { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 376 | tcg_op_remove(ctx->tcg, op); |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 377 | return true; |
Aurelien Jarno | 5365718 | 2015-06-04 21:53:25 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 380 | reset_ts(ctx, dst_ts); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 381 | di = ts_info(dst_ts); |
| 382 | si = ts_info(src_ts); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 383 | |
| 384 | switch (ctx->type) { |
| 385 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 386 | new_op = INDEX_op_mov_i32; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 387 | break; |
| 388 | case TCG_TYPE_I64: |
| 389 | new_op = INDEX_op_mov_i64; |
| 390 | break; |
| 391 | case TCG_TYPE_V64: |
| 392 | case TCG_TYPE_V128: |
| 393 | case TCG_TYPE_V256: |
| 394 | /* TCGOP_VECL and TCGOP_VECE remain unchanged. */ |
| 395 | new_op = INDEX_op_mov_vec; |
| 396 | break; |
| 397 | default: |
| 398 | g_assert_not_reached(); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 399 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 400 | op->opc = new_op; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 401 | op->args[0] = dst; |
| 402 | op->args[1] = src; |
Richard Henderson | a62f6f5 | 2014-05-22 10:59:12 -0700 | [diff] [blame] | 403 | |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 404 | di->z_mask = si->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 405 | di->s_mask = si->s_mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 406 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 407 | if (src_ts->type == dst_ts->type) { |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 408 | TempOptInfo *ni = ts_info(si->next_copy); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 409 | |
| 410 | di->next_copy = si->next_copy; |
| 411 | di->prev_copy = src_ts; |
| 412 | ni->prev_copy = dst_ts; |
| 413 | si->next_copy = dst_ts; |
| 414 | di->is_const = si->is_const; |
| 415 | di->val = si->val; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 416 | |
| 417 | if (!QSIMPLEQ_EMPTY(&si->mem_copy) |
| 418 | && cmp_better_copy(src_ts, dst_ts) == dst_ts) { |
| 419 | move_mem_copies(dst_ts, src_ts); |
| 420 | } |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 421 | } |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 422 | return true; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 423 | } |
| 424 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 425 | static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op, |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 426 | TCGArg dst, uint64_t val) |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 427 | { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 428 | /* Convert movi to mov with constant temp. */ |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 429 | 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] | 430 | } |
| 431 | |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 432 | static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 433 | { |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 434 | uint64_t l64, h64; |
| 435 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 436 | switch (op) { |
| 437 | CASE_OP_32_64(add): |
| 438 | return x + y; |
| 439 | |
| 440 | CASE_OP_32_64(sub): |
| 441 | return x - y; |
| 442 | |
| 443 | CASE_OP_32_64(mul): |
| 444 | return x * y; |
| 445 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 446 | CASE_OP_32_64_VEC(and): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 447 | return x & y; |
| 448 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 449 | CASE_OP_32_64_VEC(or): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 450 | return x | y; |
| 451 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 452 | CASE_OP_32_64_VEC(xor): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 453 | return x ^ y; |
| 454 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 455 | case INDEX_op_shl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 456 | return (uint32_t)x << (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 457 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 458 | case INDEX_op_shl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 459 | return (uint64_t)x << (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 460 | |
| 461 | case INDEX_op_shr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 462 | return (uint32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 463 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 464 | case INDEX_op_shr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 465 | return (uint64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 466 | |
| 467 | case INDEX_op_sar_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 468 | return (int32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 469 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 470 | case INDEX_op_sar_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 471 | return (int64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 472 | |
| 473 | case INDEX_op_rotr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 474 | return ror32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 475 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 476 | case INDEX_op_rotr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 477 | return ror64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 478 | |
| 479 | case INDEX_op_rotl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 480 | return rol32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 481 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 482 | case INDEX_op_rotl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 483 | return rol64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 484 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 485 | CASE_OP_32_64_VEC(not): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 486 | return ~x; |
| 487 | |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 488 | CASE_OP_32_64(neg): |
| 489 | return -x; |
| 490 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 491 | CASE_OP_32_64_VEC(andc): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 492 | return x & ~y; |
| 493 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 494 | CASE_OP_32_64_VEC(orc): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 495 | return x | ~y; |
| 496 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 497 | CASE_OP_32_64_VEC(eqv): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 498 | return ~(x ^ y); |
| 499 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 500 | CASE_OP_32_64_VEC(nand): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 501 | return ~(x & y); |
| 502 | |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 503 | CASE_OP_32_64_VEC(nor): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 504 | return ~(x | y); |
| 505 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 506 | case INDEX_op_clz_i32: |
| 507 | return (uint32_t)x ? clz32(x) : y; |
| 508 | |
| 509 | case INDEX_op_clz_i64: |
| 510 | return x ? clz64(x) : y; |
| 511 | |
| 512 | case INDEX_op_ctz_i32: |
| 513 | return (uint32_t)x ? ctz32(x) : y; |
| 514 | |
| 515 | case INDEX_op_ctz_i64: |
| 516 | return x ? ctz64(x) : y; |
| 517 | |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 518 | case INDEX_op_ctpop_i32: |
| 519 | return ctpop32(x); |
| 520 | |
| 521 | case INDEX_op_ctpop_i64: |
| 522 | return ctpop64(x); |
| 523 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 524 | CASE_OP_32_64(ext8s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 525 | return (int8_t)x; |
| 526 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 527 | CASE_OP_32_64(ext16s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 528 | return (int16_t)x; |
| 529 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 530 | CASE_OP_32_64(ext8u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 531 | return (uint8_t)x; |
| 532 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 533 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 534 | return (uint16_t)x; |
| 535 | |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 536 | CASE_OP_32_64(bswap16): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 537 | x = bswap16(x); |
| 538 | return y & TCG_BSWAP_OS ? (int16_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 539 | |
| 540 | CASE_OP_32_64(bswap32): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 541 | x = bswap32(x); |
| 542 | return y & TCG_BSWAP_OS ? (int32_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 543 | |
| 544 | case INDEX_op_bswap64_i64: |
| 545 | return bswap64(x); |
| 546 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 547 | case INDEX_op_ext_i32_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 548 | case INDEX_op_ext32s_i64: |
| 549 | return (int32_t)x; |
| 550 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 551 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 552 | case INDEX_op_extrl_i64_i32: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 553 | case INDEX_op_ext32u_i64: |
| 554 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 555 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 556 | case INDEX_op_extrh_i64_i32: |
| 557 | return (uint64_t)x >> 32; |
| 558 | |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 559 | case INDEX_op_muluh_i32: |
| 560 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; |
| 561 | case INDEX_op_mulsh_i32: |
| 562 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; |
| 563 | |
| 564 | case INDEX_op_muluh_i64: |
| 565 | mulu64(&l64, &h64, x, y); |
| 566 | return h64; |
| 567 | case INDEX_op_mulsh_i64: |
| 568 | muls64(&l64, &h64, x, y); |
| 569 | return h64; |
| 570 | |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 571 | case INDEX_op_div_i32: |
| 572 | /* Avoid crashing on divide by zero, otherwise undefined. */ |
| 573 | return (int32_t)x / ((int32_t)y ? : 1); |
| 574 | case INDEX_op_divu_i32: |
| 575 | return (uint32_t)x / ((uint32_t)y ? : 1); |
| 576 | case INDEX_op_div_i64: |
| 577 | return (int64_t)x / ((int64_t)y ? : 1); |
| 578 | case INDEX_op_divu_i64: |
| 579 | return (uint64_t)x / ((uint64_t)y ? : 1); |
| 580 | |
| 581 | case INDEX_op_rem_i32: |
| 582 | return (int32_t)x % ((int32_t)y ? : 1); |
| 583 | case INDEX_op_remu_i32: |
| 584 | return (uint32_t)x % ((uint32_t)y ? : 1); |
| 585 | case INDEX_op_rem_i64: |
| 586 | return (int64_t)x % ((int64_t)y ? : 1); |
| 587 | case INDEX_op_remu_i64: |
| 588 | return (uint64_t)x % ((uint64_t)y ? : 1); |
| 589 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 590 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 591 | g_assert_not_reached(); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 595 | static uint64_t do_constant_folding(TCGOpcode op, TCGType type, |
| 596 | uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 597 | { |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 598 | uint64_t res = do_constant_folding_2(op, x, y); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 599 | if (type == TCG_TYPE_I32) { |
Aurelien Jarno | 29f3ff8 | 2015-07-10 18:03:31 +0200 | [diff] [blame] | 600 | res = (int32_t)res; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 601 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 602 | return res; |
| 603 | } |
| 604 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 605 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 606 | { |
| 607 | switch (c) { |
| 608 | case TCG_COND_EQ: |
| 609 | return x == y; |
| 610 | case TCG_COND_NE: |
| 611 | return x != y; |
| 612 | case TCG_COND_LT: |
| 613 | return (int32_t)x < (int32_t)y; |
| 614 | case TCG_COND_GE: |
| 615 | return (int32_t)x >= (int32_t)y; |
| 616 | case TCG_COND_LE: |
| 617 | return (int32_t)x <= (int32_t)y; |
| 618 | case TCG_COND_GT: |
| 619 | return (int32_t)x > (int32_t)y; |
| 620 | case TCG_COND_LTU: |
| 621 | return x < y; |
| 622 | case TCG_COND_GEU: |
| 623 | return x >= y; |
| 624 | case TCG_COND_LEU: |
| 625 | return x <= y; |
| 626 | case TCG_COND_GTU: |
| 627 | return x > y; |
| 628 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 629 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) |
| 634 | { |
| 635 | switch (c) { |
| 636 | case TCG_COND_EQ: |
| 637 | return x == y; |
| 638 | case TCG_COND_NE: |
| 639 | return x != y; |
| 640 | case TCG_COND_LT: |
| 641 | return (int64_t)x < (int64_t)y; |
| 642 | case TCG_COND_GE: |
| 643 | return (int64_t)x >= (int64_t)y; |
| 644 | case TCG_COND_LE: |
| 645 | return (int64_t)x <= (int64_t)y; |
| 646 | case TCG_COND_GT: |
| 647 | return (int64_t)x > (int64_t)y; |
| 648 | case TCG_COND_LTU: |
| 649 | return x < y; |
| 650 | case TCG_COND_GEU: |
| 651 | return x >= y; |
| 652 | case TCG_COND_LEU: |
| 653 | return x <= y; |
| 654 | case TCG_COND_GTU: |
| 655 | return x > y; |
| 656 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 657 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | static bool do_constant_folding_cond_eq(TCGCond c) |
| 662 | { |
| 663 | switch (c) { |
| 664 | case TCG_COND_GT: |
| 665 | case TCG_COND_LTU: |
| 666 | case TCG_COND_LT: |
| 667 | case TCG_COND_GTU: |
| 668 | case TCG_COND_NE: |
| 669 | return 0; |
| 670 | case TCG_COND_GE: |
| 671 | case TCG_COND_GEU: |
| 672 | case TCG_COND_LE: |
| 673 | case TCG_COND_LEU: |
| 674 | case TCG_COND_EQ: |
| 675 | return 1; |
| 676 | default: |
Richard Henderson | 732e89f | 2023-04-05 12:09:14 -0700 | [diff] [blame] | 677 | g_assert_not_reached(); |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 681 | /* |
| 682 | * Return -1 if the condition can't be simplified, |
| 683 | * and the result of the condition (0 or 1) if it can. |
| 684 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 685 | static int do_constant_folding_cond(TCGType type, TCGArg x, |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 686 | TCGArg y, TCGCond c) |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 687 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 688 | if (arg_is_const(x) && arg_is_const(y)) { |
Alex Bennée | 9becc36 | 2022-02-09 11:21:42 +0000 | [diff] [blame] | 689 | uint64_t xv = arg_info(x)->val; |
| 690 | uint64_t yv = arg_info(y)->val; |
| 691 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 692 | switch (type) { |
| 693 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 694 | return do_constant_folding_cond_32(xv, yv, c); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 695 | case TCG_TYPE_I64: |
| 696 | return do_constant_folding_cond_64(xv, yv, c); |
| 697 | default: |
| 698 | /* Only scalar comparisons are optimizable */ |
| 699 | return -1; |
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 | } else if (args_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 702 | return do_constant_folding_cond_eq(c); |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 703 | } else if (arg_is_const_val(y, 0)) { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 704 | switch (c) { |
| 705 | case TCG_COND_LTU: |
| 706 | return 0; |
| 707 | case TCG_COND_GEU: |
| 708 | return 1; |
| 709 | default: |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 710 | return -1; |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 711 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 712 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 713 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 716 | /* |
| 717 | * Return -1 if the condition can't be simplified, |
| 718 | * and the result of the condition (0 or 1) if it can. |
| 719 | */ |
| 720 | static int do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c) |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 721 | { |
| 722 | TCGArg al = p1[0], ah = p1[1]; |
| 723 | TCGArg bl = p2[0], bh = p2[1]; |
| 724 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 725 | if (arg_is_const(bl) && arg_is_const(bh)) { |
| 726 | tcg_target_ulong blv = arg_info(bl)->val; |
| 727 | tcg_target_ulong bhv = arg_info(bh)->val; |
| 728 | uint64_t b = deposit64(blv, 32, 32, bhv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 729 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 730 | if (arg_is_const(al) && arg_is_const(ah)) { |
| 731 | tcg_target_ulong alv = arg_info(al)->val; |
| 732 | tcg_target_ulong ahv = arg_info(ah)->val; |
| 733 | uint64_t a = deposit64(alv, 32, 32, ahv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 734 | return do_constant_folding_cond_64(a, b, c); |
| 735 | } |
| 736 | if (b == 0) { |
| 737 | switch (c) { |
| 738 | case TCG_COND_LTU: |
| 739 | return 0; |
| 740 | case TCG_COND_GEU: |
| 741 | return 1; |
| 742 | default: |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 747 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 748 | return do_constant_folding_cond_eq(c); |
| 749 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 750 | return -1; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 753 | /** |
| 754 | * swap_commutative: |
| 755 | * @dest: TCGArg of the destination argument, or NO_DEST. |
| 756 | * @p1: first paired argument |
| 757 | * @p2: second paired argument |
| 758 | * |
| 759 | * If *@p1 is a constant and *@p2 is not, swap. |
| 760 | * If *@p2 matches @dest, swap. |
| 761 | * Return true if a swap was performed. |
| 762 | */ |
| 763 | |
| 764 | #define NO_DEST temp_arg(NULL) |
| 765 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 766 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 767 | { |
| 768 | TCGArg a1 = *p1, a2 = *p2; |
| 769 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 770 | sum += arg_is_const(a1); |
| 771 | sum -= arg_is_const(a2); |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 772 | |
| 773 | /* Prefer the constant in second argument, and then the form |
| 774 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 775 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 776 | *p1 = a2; |
| 777 | *p2 = a1; |
| 778 | return true; |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 783 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 784 | { |
| 785 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 786 | sum += arg_is_const(p1[0]); |
| 787 | sum += arg_is_const(p1[1]); |
| 788 | sum -= arg_is_const(p2[0]); |
| 789 | sum -= arg_is_const(p2[1]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 790 | if (sum > 0) { |
| 791 | TCGArg t; |
| 792 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 793 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 794 | return true; |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 799 | static int do_constant_folding_cond1(OptContext *ctx, TCGArg dest, |
| 800 | TCGArg *p1, TCGArg *p2, TCGArg *pcond) |
| 801 | { |
| 802 | TCGCond cond; |
| 803 | bool swap; |
| 804 | int r; |
| 805 | |
| 806 | swap = swap_commutative(dest, p1, p2); |
| 807 | cond = *pcond; |
| 808 | if (swap) { |
| 809 | *pcond = cond = tcg_swap_cond(cond); |
| 810 | } |
| 811 | |
| 812 | r = do_constant_folding_cond(ctx->type, *p1, *p2, cond); |
| 813 | return r; |
| 814 | } |
| 815 | |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 816 | static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args) |
| 817 | { |
| 818 | for (int i = 0; i < nb_args; i++) { |
| 819 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 820 | init_ts_info(ctx, ts); |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 824 | static void copy_propagate(OptContext *ctx, TCGOp *op, |
| 825 | int nb_oargs, int nb_iargs) |
| 826 | { |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 827 | for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
| 828 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 39004a7 | 2022-11-11 10:09:37 +1000 | [diff] [blame] | 829 | if (ts_is_copy(ts)) { |
Richard Henderson | 9f75e52 | 2023-11-02 13:37:46 -0700 | [diff] [blame] | 830 | op->args[i] = temp_arg(find_better_copy(ts)); |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 835 | static void finish_folding(OptContext *ctx, TCGOp *op) |
| 836 | { |
| 837 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 838 | int i, nb_oargs; |
| 839 | |
| 840 | /* |
Richard Henderson | d97f8f3 | 2023-10-16 19:10:42 -0700 | [diff] [blame] | 841 | * We only optimize extended basic blocks. If the opcode ends a BB |
| 842 | * and is not a conditional branch, reset all temp data. |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 843 | */ |
| 844 | if (def->flags & TCG_OPF_BB_END) { |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 845 | ctx->prev_mb = NULL; |
Richard Henderson | d97f8f3 | 2023-10-16 19:10:42 -0700 | [diff] [blame] | 846 | if (!(def->flags & TCG_OPF_COND_BRANCH)) { |
| 847 | memset(&ctx->temps_used, 0, sizeof(ctx->temps_used)); |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 848 | remove_mem_copy_all(ctx); |
Richard Henderson | d97f8f3 | 2023-10-16 19:10:42 -0700 | [diff] [blame] | 849 | } |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 850 | return; |
| 851 | } |
| 852 | |
| 853 | nb_oargs = def->nb_oargs; |
| 854 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 855 | TCGTemp *ts = arg_temp(op->args[i]); |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 856 | reset_ts(ctx, ts); |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 857 | /* |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 858 | * Save the corresponding known-zero/sign bits mask for the |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 859 | * first output argument (only one supported so far). |
| 860 | */ |
| 861 | if (i == 0) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 862 | ts_info(ts)->z_mask = ctx->z_mask; |
| 863 | ts_info(ts)->s_mask = ctx->s_mask; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 868 | /* |
| 869 | * The fold_* functions return true when processing is complete, |
| 870 | * usually by folding the operation to a constant or to a copy, |
| 871 | * and calling tcg_opt_gen_{mov,movi}. They may do other things, |
| 872 | * like collect information about the value produced, for use in |
| 873 | * optimizing a subsequent operation. |
| 874 | * |
| 875 | * These first fold_* functions are all helpers, used by other |
| 876 | * folders for more specific operations. |
| 877 | */ |
| 878 | |
| 879 | static bool fold_const1(OptContext *ctx, TCGOp *op) |
| 880 | { |
| 881 | if (arg_is_const(op->args[1])) { |
| 882 | uint64_t t; |
| 883 | |
| 884 | t = arg_info(op->args[1])->val; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 885 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 886 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | static bool fold_const2(OptContext *ctx, TCGOp *op) |
| 892 | { |
| 893 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 894 | uint64_t t1 = arg_info(op->args[1])->val; |
| 895 | uint64_t t2 = arg_info(op->args[2])->val; |
| 896 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 897 | t1 = do_constant_folding(op->opc, ctx->type, t1, t2); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 898 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 899 | } |
| 900 | return false; |
| 901 | } |
| 902 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 903 | static bool fold_commutative(OptContext *ctx, TCGOp *op) |
| 904 | { |
| 905 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 906 | return false; |
| 907 | } |
| 908 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 909 | static bool fold_const2_commutative(OptContext *ctx, TCGOp *op) |
| 910 | { |
| 911 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 912 | return fold_const2(ctx, op); |
| 913 | } |
| 914 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 915 | static bool fold_masks(OptContext *ctx, TCGOp *op) |
| 916 | { |
| 917 | uint64_t a_mask = ctx->a_mask; |
| 918 | uint64_t z_mask = ctx->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 919 | uint64_t s_mask = ctx->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 920 | |
| 921 | /* |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 922 | * 32-bit ops generate 32-bit results, which for the purpose of |
| 923 | * simplifying tcg are sign-extended. Certainly that's how we |
| 924 | * represent our constants elsewhere. Note that the bits will |
| 925 | * be reset properly for a 64-bit value when encountering the |
| 926 | * type changing opcodes. |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 927 | */ |
| 928 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 929 | a_mask = (int32_t)a_mask; |
| 930 | z_mask = (int32_t)z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 931 | s_mask |= MAKE_64BIT_MASK(32, 32); |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 932 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 933 | ctx->s_mask = s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | if (z_mask == 0) { |
| 937 | return tcg_opt_gen_movi(ctx, op, op->args[0], 0); |
| 938 | } |
| 939 | if (a_mask == 0) { |
| 940 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 941 | } |
| 942 | return false; |
| 943 | } |
| 944 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 945 | /* |
| 946 | * Convert @op to NOT, if NOT is supported by the host. |
| 947 | * Return true f the conversion is successful, which will still |
| 948 | * indicate that the processing is complete. |
| 949 | */ |
| 950 | static bool fold_not(OptContext *ctx, TCGOp *op); |
| 951 | static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx) |
| 952 | { |
| 953 | TCGOpcode not_op; |
| 954 | bool have_not; |
| 955 | |
| 956 | switch (ctx->type) { |
| 957 | case TCG_TYPE_I32: |
| 958 | not_op = INDEX_op_not_i32; |
| 959 | have_not = TCG_TARGET_HAS_not_i32; |
| 960 | break; |
| 961 | case TCG_TYPE_I64: |
| 962 | not_op = INDEX_op_not_i64; |
| 963 | have_not = TCG_TARGET_HAS_not_i64; |
| 964 | break; |
| 965 | case TCG_TYPE_V64: |
| 966 | case TCG_TYPE_V128: |
| 967 | case TCG_TYPE_V256: |
| 968 | not_op = INDEX_op_not_vec; |
| 969 | have_not = TCG_TARGET_HAS_not_vec; |
| 970 | break; |
| 971 | default: |
| 972 | g_assert_not_reached(); |
| 973 | } |
| 974 | if (have_not) { |
| 975 | op->opc = not_op; |
| 976 | op->args[1] = op->args[idx]; |
| 977 | return fold_not(ctx, op); |
| 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 982 | /* If the binary operation has first argument @i, fold to @i. */ |
| 983 | static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 984 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 985 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 986 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 987 | } |
| 988 | return false; |
| 989 | } |
| 990 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 991 | /* If the binary operation has first argument @i, fold to NOT. */ |
| 992 | static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 993 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 994 | if (arg_is_const_val(op->args[1], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 995 | return fold_to_not(ctx, op, 2); |
| 996 | } |
| 997 | return false; |
| 998 | } |
| 999 | |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1000 | /* If the binary operation has second argument @i, fold to @i. */ |
| 1001 | static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1002 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1003 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1004 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1005 | } |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1009 | /* If the binary operation has second argument @i, fold to identity. */ |
| 1010 | static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1011 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1012 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1013 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1014 | } |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1018 | /* If the binary operation has second argument @i, fold to NOT. */ |
| 1019 | static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1020 | { |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1021 | if (arg_is_const_val(op->args[2], i)) { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1022 | return fold_to_not(ctx, op, 1); |
| 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1027 | /* If the binary operation has both arguments equal, fold to @i. */ |
| 1028 | static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 1029 | { |
| 1030 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1031 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1032 | } |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1036 | /* If the binary operation has both arguments equal, fold to identity. */ |
| 1037 | static bool fold_xx_to_x(OptContext *ctx, TCGOp *op) |
| 1038 | { |
| 1039 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1040 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1041 | } |
| 1042 | return false; |
| 1043 | } |
| 1044 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1045 | /* |
| 1046 | * These outermost fold_<op> functions are sorted alphabetically. |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1047 | * |
| 1048 | * The ordering of the transformations should be: |
| 1049 | * 1) those that produce a constant |
| 1050 | * 2) those that produce a copy |
| 1051 | * 3) those that produce information about the result value. |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1052 | */ |
| 1053 | |
| 1054 | static bool fold_add(OptContext *ctx, TCGOp *op) |
| 1055 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1056 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1057 | fold_xi_to_x(ctx, op, 0)) { |
| 1058 | return true; |
| 1059 | } |
| 1060 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 1063 | /* We cannot as yet do_constant_folding with vectors. */ |
| 1064 | static bool fold_add_vec(OptContext *ctx, TCGOp *op) |
| 1065 | { |
| 1066 | if (fold_commutative(ctx, op) || |
| 1067 | fold_xi_to_x(ctx, op, 0)) { |
| 1068 | return true; |
| 1069 | } |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1073 | static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1074 | { |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1075 | bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]); |
| 1076 | bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]); |
| 1077 | |
| 1078 | if (a_const && b_const) { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1079 | uint64_t al = arg_info(op->args[2])->val; |
| 1080 | uint64_t ah = arg_info(op->args[3])->val; |
| 1081 | uint64_t bl = arg_info(op->args[4])->val; |
| 1082 | uint64_t bh = arg_info(op->args[5])->val; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1083 | TCGArg rl, rh; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1084 | TCGOp *op2; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1085 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1086 | if (ctx->type == TCG_TYPE_I32) { |
| 1087 | uint64_t a = deposit64(al, 32, 32, ah); |
| 1088 | uint64_t b = deposit64(bl, 32, 32, bh); |
| 1089 | |
| 1090 | if (add) { |
| 1091 | a += b; |
| 1092 | } else { |
| 1093 | a -= b; |
| 1094 | } |
| 1095 | |
| 1096 | al = sextract64(a, 0, 32); |
| 1097 | ah = sextract64(a, 32, 32); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1098 | } else { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1099 | Int128 a = int128_make128(al, ah); |
| 1100 | Int128 b = int128_make128(bl, bh); |
| 1101 | |
| 1102 | if (add) { |
| 1103 | a = int128_add(a, b); |
| 1104 | } else { |
| 1105 | a = int128_sub(a, b); |
| 1106 | } |
| 1107 | |
| 1108 | al = int128_getlo(a); |
| 1109 | ah = int128_gethi(a); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | rl = op->args[0]; |
| 1113 | rh = op->args[1]; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1114 | |
| 1115 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Philippe Mathieu-Daudé | d447894 | 2022-12-18 22:18:31 +0100 | [diff] [blame] | 1116 | op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2); |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1117 | |
| 1118 | tcg_opt_gen_movi(ctx, op, rl, al); |
| 1119 | tcg_opt_gen_movi(ctx, op2, rh, ah); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1120 | return true; |
| 1121 | } |
Richard Henderson | f245757 | 2023-10-25 18:39:44 -0700 | [diff] [blame] | 1122 | |
| 1123 | /* Fold sub2 r,x,i to add2 r,x,-i */ |
| 1124 | if (!add && b_const) { |
| 1125 | uint64_t bl = arg_info(op->args[4])->val; |
| 1126 | uint64_t bh = arg_info(op->args[5])->val; |
| 1127 | |
| 1128 | /* Negate the two parts without assembling and disassembling. */ |
| 1129 | bl = -bl; |
| 1130 | bh = ~bh + !bl; |
| 1131 | |
| 1132 | op->opc = (ctx->type == TCG_TYPE_I32 |
| 1133 | ? INDEX_op_add2_i32 : INDEX_op_add2_i64); |
| 1134 | op->args[4] = arg_new_constant(ctx, bl); |
| 1135 | op->args[5] = arg_new_constant(ctx, bh); |
| 1136 | } |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
| 1139 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1140 | static bool fold_add2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1141 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1142 | /* Note that the high and low parts may be independently swapped. */ |
| 1143 | swap_commutative(op->args[0], &op->args[2], &op->args[4]); |
| 1144 | swap_commutative(op->args[1], &op->args[3], &op->args[5]); |
| 1145 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1146 | return fold_addsub2(ctx, op, true); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1149 | static bool fold_and(OptContext *ctx, TCGOp *op) |
| 1150 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1151 | uint64_t z1, z2; |
| 1152 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1153 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1154 | fold_xi_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1155 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1156 | fold_xx_to_x(ctx, op)) { |
| 1157 | return true; |
| 1158 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1159 | |
| 1160 | z1 = arg_info(op->args[1])->z_mask; |
| 1161 | z2 = arg_info(op->args[2])->z_mask; |
| 1162 | ctx->z_mask = z1 & z2; |
| 1163 | |
| 1164 | /* |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1165 | * Sign repetitions are perforce all identical, whether they are 1 or 0. |
| 1166 | * Bitwise operations preserve the relative quantity of the repetitions. |
| 1167 | */ |
| 1168 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1169 | & arg_info(op->args[2])->s_mask; |
| 1170 | |
| 1171 | /* |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1172 | * Known-zeros does not imply known-ones. Therefore unless |
| 1173 | * arg2 is constant, we can't infer affected bits from it. |
| 1174 | */ |
| 1175 | if (arg_is_const(op->args[2])) { |
| 1176 | ctx->a_mask = z1 & ~z2; |
| 1177 | } |
| 1178 | |
| 1179 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | static bool fold_andc(OptContext *ctx, TCGOp *op) |
| 1183 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1184 | uint64_t z1; |
| 1185 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1186 | if (fold_const2(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1187 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1188 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1189 | fold_ix_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1190 | return true; |
| 1191 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1192 | |
| 1193 | z1 = arg_info(op->args[1])->z_mask; |
| 1194 | |
| 1195 | /* |
| 1196 | * Known-zeros does not imply known-ones. Therefore unless |
| 1197 | * arg2 is constant, we can't infer anything from it. |
| 1198 | */ |
| 1199 | if (arg_is_const(op->args[2])) { |
| 1200 | uint64_t z2 = ~arg_info(op->args[2])->z_mask; |
| 1201 | ctx->a_mask = z1 & ~z2; |
| 1202 | z1 &= z2; |
| 1203 | } |
| 1204 | ctx->z_mask = z1; |
| 1205 | |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1206 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1207 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1208 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1211 | static bool fold_brcond(OptContext *ctx, TCGOp *op) |
| 1212 | { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1213 | int i = do_constant_folding_cond1(ctx, NO_DEST, &op->args[0], |
| 1214 | &op->args[1], &op->args[2]); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1215 | if (i == 0) { |
| 1216 | tcg_op_remove(ctx->tcg, op); |
| 1217 | return true; |
| 1218 | } |
| 1219 | if (i > 0) { |
| 1220 | op->opc = INDEX_op_br; |
| 1221 | op->args[0] = op->args[3]; |
| 1222 | } |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1226 | static bool fold_brcond2(OptContext *ctx, TCGOp *op) |
| 1227 | { |
| 1228 | TCGCond cond = op->args[4]; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1229 | TCGArg label = op->args[5]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1230 | int i, inv = 0; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1231 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1232 | if (swap_commutative2(&op->args[0], &op->args[2])) { |
| 1233 | op->args[4] = cond = tcg_swap_cond(cond); |
| 1234 | } |
| 1235 | |
| 1236 | i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond); |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1237 | if (i >= 0) { |
| 1238 | goto do_brcond_const; |
| 1239 | } |
| 1240 | |
| 1241 | switch (cond) { |
| 1242 | case TCG_COND_LT: |
| 1243 | case TCG_COND_GE: |
| 1244 | /* |
| 1245 | * Simplify LT/GE comparisons vs zero to a single compare |
| 1246 | * vs the high word of the input. |
| 1247 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1248 | if (arg_is_const_val(op->args[2], 0) && |
| 1249 | arg_is_const_val(op->args[3], 0)) { |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1250 | goto do_brcond_high; |
| 1251 | } |
| 1252 | break; |
| 1253 | |
| 1254 | case TCG_COND_NE: |
| 1255 | inv = 1; |
| 1256 | QEMU_FALLTHROUGH; |
| 1257 | case TCG_COND_EQ: |
| 1258 | /* |
| 1259 | * Simplify EQ/NE comparisons where one of the pairs |
| 1260 | * can be simplified. |
| 1261 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1262 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1263 | op->args[2], cond); |
| 1264 | switch (i ^ inv) { |
| 1265 | case 0: |
| 1266 | goto do_brcond_const; |
| 1267 | case 1: |
| 1268 | goto do_brcond_high; |
| 1269 | } |
| 1270 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1271 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1272 | op->args[3], cond); |
| 1273 | switch (i ^ inv) { |
| 1274 | case 0: |
| 1275 | goto do_brcond_const; |
| 1276 | case 1: |
| 1277 | op->opc = INDEX_op_brcond_i32; |
| 1278 | op->args[1] = op->args[2]; |
| 1279 | op->args[2] = cond; |
| 1280 | op->args[3] = label; |
| 1281 | break; |
| 1282 | } |
| 1283 | break; |
| 1284 | |
| 1285 | default: |
| 1286 | break; |
| 1287 | |
| 1288 | do_brcond_high: |
| 1289 | op->opc = INDEX_op_brcond_i32; |
| 1290 | op->args[0] = op->args[1]; |
| 1291 | op->args[1] = op->args[3]; |
| 1292 | op->args[2] = cond; |
| 1293 | op->args[3] = label; |
| 1294 | break; |
| 1295 | |
| 1296 | do_brcond_const: |
| 1297 | if (i == 0) { |
| 1298 | tcg_op_remove(ctx->tcg, op); |
| 1299 | return true; |
| 1300 | } |
| 1301 | op->opc = INDEX_op_br; |
| 1302 | op->args[0] = label; |
| 1303 | break; |
| 1304 | } |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1308 | static bool fold_bswap(OptContext *ctx, TCGOp *op) |
| 1309 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1310 | uint64_t z_mask, s_mask, sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1311 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1312 | if (arg_is_const(op->args[1])) { |
| 1313 | uint64_t t = arg_info(op->args[1])->val; |
| 1314 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1315 | t = do_constant_folding(op->opc, ctx->type, t, op->args[2]); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1316 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1317 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1318 | |
| 1319 | z_mask = arg_info(op->args[1])->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1320 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1321 | switch (op->opc) { |
| 1322 | case INDEX_op_bswap16_i32: |
| 1323 | case INDEX_op_bswap16_i64: |
| 1324 | z_mask = bswap16(z_mask); |
| 1325 | sign = INT16_MIN; |
| 1326 | break; |
| 1327 | case INDEX_op_bswap32_i32: |
| 1328 | case INDEX_op_bswap32_i64: |
| 1329 | z_mask = bswap32(z_mask); |
| 1330 | sign = INT32_MIN; |
| 1331 | break; |
| 1332 | case INDEX_op_bswap64_i64: |
| 1333 | z_mask = bswap64(z_mask); |
| 1334 | sign = INT64_MIN; |
| 1335 | break; |
| 1336 | default: |
| 1337 | g_assert_not_reached(); |
| 1338 | } |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1339 | s_mask = smask_from_zmask(z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1340 | |
| 1341 | switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) { |
| 1342 | case TCG_BSWAP_OZ: |
| 1343 | break; |
| 1344 | case TCG_BSWAP_OS: |
| 1345 | /* If the sign bit may be 1, force all the bits above to 1. */ |
| 1346 | if (z_mask & sign) { |
| 1347 | z_mask |= sign; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1348 | s_mask = sign << 1; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1349 | } |
| 1350 | break; |
| 1351 | default: |
| 1352 | /* The high bits are undefined: force all bits above the sign to 1. */ |
| 1353 | z_mask |= sign << 1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1354 | s_mask = 0; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1355 | break; |
| 1356 | } |
| 1357 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1358 | ctx->s_mask = s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1359 | |
| 1360 | return fold_masks(ctx, op); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1363 | static bool fold_call(OptContext *ctx, TCGOp *op) |
| 1364 | { |
| 1365 | TCGContext *s = ctx->tcg; |
| 1366 | int nb_oargs = TCGOP_CALLO(op); |
| 1367 | int nb_iargs = TCGOP_CALLI(op); |
| 1368 | int flags, i; |
| 1369 | |
| 1370 | init_arguments(ctx, op, nb_oargs + nb_iargs); |
| 1371 | copy_propagate(ctx, op, nb_oargs, nb_iargs); |
| 1372 | |
| 1373 | /* If the function reads or writes globals, reset temp data. */ |
| 1374 | flags = tcg_call_flags(op); |
| 1375 | if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { |
| 1376 | int nb_globals = s->nb_globals; |
| 1377 | |
| 1378 | for (i = 0; i < nb_globals; i++) { |
| 1379 | if (test_bit(i, ctx->temps_used.l)) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1380 | reset_ts(ctx, &ctx->tcg->temps[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 1385 | /* If the function has side effects, reset mem data. */ |
| 1386 | if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) { |
| 1387 | remove_mem_copy_all(ctx); |
| 1388 | } |
| 1389 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1390 | /* Reset temp data for outputs. */ |
| 1391 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | 986cac1 | 2023-01-09 13:59:35 -0800 | [diff] [blame] | 1392 | reset_temp(ctx, op->args[i]); |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /* Stop optimizing MB across calls. */ |
| 1396 | ctx->prev_mb = NULL; |
| 1397 | return true; |
| 1398 | } |
| 1399 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1400 | static bool fold_count_zeros(OptContext *ctx, TCGOp *op) |
| 1401 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1402 | uint64_t z_mask; |
| 1403 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1404 | if (arg_is_const(op->args[1])) { |
| 1405 | uint64_t t = arg_info(op->args[1])->val; |
| 1406 | |
| 1407 | if (t != 0) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1408 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1409 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1410 | } |
| 1411 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1412 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1413 | |
| 1414 | switch (ctx->type) { |
| 1415 | case TCG_TYPE_I32: |
| 1416 | z_mask = 31; |
| 1417 | break; |
| 1418 | case TCG_TYPE_I64: |
| 1419 | z_mask = 63; |
| 1420 | break; |
| 1421 | default: |
| 1422 | g_assert_not_reached(); |
| 1423 | } |
| 1424 | ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask; |
Richard Henderson | 2b9d0c5 | 2021-08-26 13:24:17 -0700 | [diff] [blame] | 1425 | ctx->s_mask = smask_from_zmask(ctx->z_mask); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
| 1428 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1429 | static bool fold_ctpop(OptContext *ctx, TCGOp *op) |
| 1430 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1431 | if (fold_const1(ctx, op)) { |
| 1432 | return true; |
| 1433 | } |
| 1434 | |
| 1435 | switch (ctx->type) { |
| 1436 | case TCG_TYPE_I32: |
| 1437 | ctx->z_mask = 32 | 31; |
| 1438 | break; |
| 1439 | case TCG_TYPE_I64: |
| 1440 | ctx->z_mask = 64 | 63; |
| 1441 | break; |
| 1442 | default: |
| 1443 | g_assert_not_reached(); |
| 1444 | } |
Richard Henderson | 2b9d0c5 | 2021-08-26 13:24:17 -0700 | [diff] [blame] | 1445 | ctx->s_mask = smask_from_zmask(ctx->z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1446 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1449 | static bool fold_deposit(OptContext *ctx, TCGOp *op) |
| 1450 | { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1451 | TCGOpcode and_opc; |
| 1452 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1453 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1454 | uint64_t t1 = arg_info(op->args[1])->val; |
| 1455 | uint64_t t2 = arg_info(op->args[2])->val; |
| 1456 | |
| 1457 | t1 = deposit64(t1, op->args[3], op->args[4], t2); |
| 1458 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 1459 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1460 | |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1461 | switch (ctx->type) { |
| 1462 | case TCG_TYPE_I32: |
| 1463 | and_opc = INDEX_op_and_i32; |
| 1464 | break; |
| 1465 | case TCG_TYPE_I64: |
| 1466 | and_opc = INDEX_op_and_i64; |
| 1467 | break; |
| 1468 | default: |
| 1469 | g_assert_not_reached(); |
| 1470 | } |
| 1471 | |
| 1472 | /* Inserting a value into zero at offset 0. */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1473 | if (arg_is_const_val(op->args[1], 0) && op->args[3] == 0) { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1474 | uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]); |
| 1475 | |
| 1476 | op->opc = and_opc; |
| 1477 | op->args[1] = op->args[2]; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1478 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1479 | ctx->z_mask = mask & arg_info(op->args[1])->z_mask; |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | /* Inserting zero into a value. */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 1484 | if (arg_is_const_val(op->args[2], 0)) { |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1485 | uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0); |
| 1486 | |
| 1487 | op->opc = and_opc; |
Richard Henderson | 26aac97 | 2023-10-23 12:31:57 -0700 | [diff] [blame] | 1488 | op->args[2] = arg_new_constant(ctx, mask); |
Richard Henderson | 8f7a840 | 2023-08-13 11:03:05 -0700 | [diff] [blame] | 1489 | ctx->z_mask = mask & arg_info(op->args[1])->z_mask; |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1493 | ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask, |
| 1494 | op->args[3], op->args[4], |
| 1495 | arg_info(op->args[2])->z_mask); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1499 | static bool fold_divide(OptContext *ctx, TCGOp *op) |
| 1500 | { |
Richard Henderson | 2f9d9a3 | 2021-10-25 11:30:14 -0700 | [diff] [blame] | 1501 | if (fold_const2(ctx, op) || |
| 1502 | fold_xi_to_x(ctx, op, 1)) { |
| 1503 | return true; |
| 1504 | } |
| 1505 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1508 | static bool fold_dup(OptContext *ctx, TCGOp *op) |
| 1509 | { |
| 1510 | if (arg_is_const(op->args[1])) { |
| 1511 | uint64_t t = arg_info(op->args[1])->val; |
| 1512 | t = dup_const(TCGOP_VECE(op), t); |
| 1513 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1514 | } |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
| 1518 | static bool fold_dup2(OptContext *ctx, TCGOp *op) |
| 1519 | { |
| 1520 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1521 | uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32, |
| 1522 | arg_info(op->args[2])->val); |
| 1523 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1524 | } |
| 1525 | |
| 1526 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1527 | op->opc = INDEX_op_dup_vec; |
| 1528 | TCGOP_VECE(op) = MO_32; |
| 1529 | } |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1533 | static bool fold_eqv(OptContext *ctx, TCGOp *op) |
| 1534 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1535 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1536 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1537 | fold_xi_to_not(ctx, op, 0)) { |
| 1538 | return true; |
| 1539 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1540 | |
| 1541 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1542 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1543 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1546 | static bool fold_extract(OptContext *ctx, TCGOp *op) |
| 1547 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1548 | uint64_t z_mask_old, z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1549 | int pos = op->args[2]; |
| 1550 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1551 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1552 | if (arg_is_const(op->args[1])) { |
| 1553 | uint64_t t; |
| 1554 | |
| 1555 | t = arg_info(op->args[1])->val; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1556 | t = extract64(t, pos, len); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1557 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1558 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1559 | |
| 1560 | z_mask_old = arg_info(op->args[1])->z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1561 | z_mask = extract64(z_mask_old, pos, len); |
| 1562 | if (pos == 0) { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1563 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1564 | } |
| 1565 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1566 | ctx->s_mask = smask_from_zmask(z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1567 | |
| 1568 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1571 | static bool fold_extract2(OptContext *ctx, TCGOp *op) |
| 1572 | { |
| 1573 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1574 | uint64_t v1 = arg_info(op->args[1])->val; |
| 1575 | uint64_t v2 = arg_info(op->args[2])->val; |
| 1576 | int shr = op->args[3]; |
| 1577 | |
| 1578 | if (op->opc == INDEX_op_extract2_i64) { |
| 1579 | v1 >>= shr; |
| 1580 | v2 <<= 64 - shr; |
| 1581 | } else { |
| 1582 | v1 = (uint32_t)v1 >> shr; |
Richard Henderson | 225bec0 | 2021-11-09 23:17:59 +0100 | [diff] [blame] | 1583 | v2 = (uint64_t)((int32_t)v2 << (32 - shr)); |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1584 | } |
| 1585 | return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); |
| 1586 | } |
| 1587 | return false; |
| 1588 | } |
| 1589 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1590 | static bool fold_exts(OptContext *ctx, TCGOp *op) |
| 1591 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1592 | uint64_t s_mask_old, s_mask, z_mask, sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1593 | bool type_change = false; |
| 1594 | |
| 1595 | if (fold_const1(ctx, op)) { |
| 1596 | return true; |
| 1597 | } |
| 1598 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1599 | z_mask = arg_info(op->args[1])->z_mask; |
| 1600 | s_mask = arg_info(op->args[1])->s_mask; |
| 1601 | s_mask_old = s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1602 | |
| 1603 | switch (op->opc) { |
| 1604 | CASE_OP_32_64(ext8s): |
| 1605 | sign = INT8_MIN; |
| 1606 | z_mask = (uint8_t)z_mask; |
| 1607 | break; |
| 1608 | CASE_OP_32_64(ext16s): |
| 1609 | sign = INT16_MIN; |
| 1610 | z_mask = (uint16_t)z_mask; |
| 1611 | break; |
| 1612 | case INDEX_op_ext_i32_i64: |
| 1613 | type_change = true; |
| 1614 | QEMU_FALLTHROUGH; |
| 1615 | case INDEX_op_ext32s_i64: |
| 1616 | sign = INT32_MIN; |
| 1617 | z_mask = (uint32_t)z_mask; |
| 1618 | break; |
| 1619 | default: |
| 1620 | g_assert_not_reached(); |
| 1621 | } |
| 1622 | |
| 1623 | if (z_mask & sign) { |
| 1624 | z_mask |= sign; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1625 | } |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1626 | s_mask |= sign << 1; |
| 1627 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1628 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1629 | ctx->s_mask = s_mask; |
| 1630 | if (!type_change) { |
| 1631 | ctx->a_mask = s_mask & ~s_mask_old; |
| 1632 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1633 | |
| 1634 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static bool fold_extu(OptContext *ctx, TCGOp *op) |
| 1638 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1639 | uint64_t z_mask_old, z_mask; |
| 1640 | bool type_change = false; |
| 1641 | |
| 1642 | if (fold_const1(ctx, op)) { |
| 1643 | return true; |
| 1644 | } |
| 1645 | |
| 1646 | z_mask_old = z_mask = arg_info(op->args[1])->z_mask; |
| 1647 | |
| 1648 | switch (op->opc) { |
| 1649 | CASE_OP_32_64(ext8u): |
| 1650 | z_mask = (uint8_t)z_mask; |
| 1651 | break; |
| 1652 | CASE_OP_32_64(ext16u): |
| 1653 | z_mask = (uint16_t)z_mask; |
| 1654 | break; |
| 1655 | case INDEX_op_extrl_i64_i32: |
| 1656 | case INDEX_op_extu_i32_i64: |
| 1657 | type_change = true; |
| 1658 | QEMU_FALLTHROUGH; |
| 1659 | case INDEX_op_ext32u_i64: |
| 1660 | z_mask = (uint32_t)z_mask; |
| 1661 | break; |
| 1662 | case INDEX_op_extrh_i64_i32: |
| 1663 | type_change = true; |
| 1664 | z_mask >>= 32; |
| 1665 | break; |
| 1666 | default: |
| 1667 | g_assert_not_reached(); |
| 1668 | } |
| 1669 | |
| 1670 | ctx->z_mask = z_mask; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1671 | ctx->s_mask = smask_from_zmask(z_mask); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1672 | if (!type_change) { |
| 1673 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1674 | } |
| 1675 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1678 | static bool fold_mb(OptContext *ctx, TCGOp *op) |
| 1679 | { |
| 1680 | /* Eliminate duplicate and redundant fence instructions. */ |
| 1681 | if (ctx->prev_mb) { |
| 1682 | /* |
| 1683 | * Merge two barriers of the same type into one, |
| 1684 | * or a weaker barrier into a stronger one, |
| 1685 | * or two weaker barriers into a stronger one. |
| 1686 | * mb X; mb Y => mb X|Y |
| 1687 | * mb; strl => mb; st |
| 1688 | * ldaq; mb => ld; mb |
| 1689 | * ldaq; strl => ld; mb; st |
| 1690 | * Other combinations are also merged into a strong |
| 1691 | * barrier. This is stricter than specified but for |
| 1692 | * the purposes of TCG is better than not optimizing. |
| 1693 | */ |
| 1694 | ctx->prev_mb->args[0] |= op->args[0]; |
| 1695 | tcg_op_remove(ctx->tcg, op); |
| 1696 | } else { |
| 1697 | ctx->prev_mb = op; |
| 1698 | } |
| 1699 | return true; |
| 1700 | } |
| 1701 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1702 | static bool fold_mov(OptContext *ctx, TCGOp *op) |
| 1703 | { |
| 1704 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1705 | } |
| 1706 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1707 | static bool fold_movcond(OptContext *ctx, TCGOp *op) |
| 1708 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1709 | int i; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1710 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1711 | /* |
| 1712 | * Canonicalize the "false" input reg to match the destination reg so |
| 1713 | * that the tcg backend can implement a "move if true" operation. |
| 1714 | */ |
| 1715 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1716 | op->args[5] = tcg_invert_cond(op->args[5]); |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1719 | i = do_constant_folding_cond1(ctx, NO_DEST, &op->args[1], |
| 1720 | &op->args[2], &op->args[5]); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1721 | if (i >= 0) { |
| 1722 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); |
| 1723 | } |
| 1724 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1725 | ctx->z_mask = arg_info(op->args[3])->z_mask |
| 1726 | | arg_info(op->args[4])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1727 | ctx->s_mask = arg_info(op->args[3])->s_mask |
| 1728 | & arg_info(op->args[4])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1729 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1730 | if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) { |
| 1731 | uint64_t tv = arg_info(op->args[3])->val; |
| 1732 | uint64_t fv = arg_info(op->args[4])->val; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1733 | TCGOpcode opc, negopc = 0; |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1734 | TCGCond cond = op->args[5]; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1735 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1736 | switch (ctx->type) { |
| 1737 | case TCG_TYPE_I32: |
| 1738 | opc = INDEX_op_setcond_i32; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1739 | if (TCG_TARGET_HAS_negsetcond_i32) { |
| 1740 | negopc = INDEX_op_negsetcond_i32; |
| 1741 | } |
| 1742 | tv = (int32_t)tv; |
| 1743 | fv = (int32_t)fv; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1744 | break; |
| 1745 | case TCG_TYPE_I64: |
| 1746 | opc = INDEX_op_setcond_i64; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1747 | if (TCG_TARGET_HAS_negsetcond_i64) { |
| 1748 | negopc = INDEX_op_negsetcond_i64; |
| 1749 | } |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1750 | break; |
| 1751 | default: |
| 1752 | g_assert_not_reached(); |
| 1753 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1754 | |
| 1755 | if (tv == 1 && fv == 0) { |
| 1756 | op->opc = opc; |
| 1757 | op->args[3] = cond; |
| 1758 | } else if (fv == 1 && tv == 0) { |
| 1759 | op->opc = opc; |
| 1760 | op->args[3] = tcg_invert_cond(cond); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1761 | } else if (negopc) { |
| 1762 | if (tv == -1 && fv == 0) { |
| 1763 | op->opc = negopc; |
| 1764 | op->args[3] = cond; |
| 1765 | } else if (fv == -1 && tv == 0) { |
| 1766 | op->opc = negopc; |
| 1767 | op->args[3] = tcg_invert_cond(cond); |
| 1768 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1774 | static bool fold_mul(OptContext *ctx, TCGOp *op) |
| 1775 | { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1776 | if (fold_const2(ctx, op) || |
Richard Henderson | 5b5cf47 | 2021-10-25 11:19:14 -0700 | [diff] [blame] | 1777 | fold_xi_to_i(ctx, op, 0) || |
| 1778 | fold_xi_to_x(ctx, op, 1)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1779 | return true; |
| 1780 | } |
| 1781 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) |
| 1785 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1786 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1787 | fold_xi_to_i(ctx, op, 0)) { |
| 1788 | return true; |
| 1789 | } |
| 1790 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1793 | static bool fold_multiply2(OptContext *ctx, TCGOp *op) |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1794 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1795 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
| 1796 | |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1797 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1798 | uint64_t a = arg_info(op->args[2])->val; |
| 1799 | uint64_t b = arg_info(op->args[3])->val; |
| 1800 | uint64_t h, l; |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1801 | TCGArg rl, rh; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1802 | TCGOp *op2; |
| 1803 | |
| 1804 | switch (op->opc) { |
| 1805 | case INDEX_op_mulu2_i32: |
| 1806 | l = (uint64_t)(uint32_t)a * (uint32_t)b; |
| 1807 | h = (int32_t)(l >> 32); |
| 1808 | l = (int32_t)l; |
| 1809 | break; |
| 1810 | case INDEX_op_muls2_i32: |
| 1811 | l = (int64_t)(int32_t)a * (int32_t)b; |
| 1812 | h = l >> 32; |
| 1813 | l = (int32_t)l; |
| 1814 | break; |
| 1815 | case INDEX_op_mulu2_i64: |
| 1816 | mulu64(&l, &h, a, b); |
| 1817 | break; |
| 1818 | case INDEX_op_muls2_i64: |
| 1819 | muls64(&l, &h, a, b); |
| 1820 | break; |
| 1821 | default: |
| 1822 | g_assert_not_reached(); |
| 1823 | } |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1824 | |
| 1825 | rl = op->args[0]; |
| 1826 | rh = op->args[1]; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1827 | |
| 1828 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
Philippe Mathieu-Daudé | d447894 | 2022-12-18 22:18:31 +0100 | [diff] [blame] | 1829 | op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2); |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1830 | |
| 1831 | tcg_opt_gen_movi(ctx, op, rl, l); |
| 1832 | tcg_opt_gen_movi(ctx, op2, rh, h); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1833 | return true; |
| 1834 | } |
| 1835 | return false; |
| 1836 | } |
| 1837 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1838 | static bool fold_nand(OptContext *ctx, TCGOp *op) |
| 1839 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1840 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1841 | fold_xi_to_not(ctx, op, -1)) { |
| 1842 | return true; |
| 1843 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1844 | |
| 1845 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1846 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1847 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | static bool fold_neg(OptContext *ctx, TCGOp *op) |
| 1851 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1852 | uint64_t z_mask; |
| 1853 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1854 | if (fold_const1(ctx, op)) { |
| 1855 | return true; |
| 1856 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1857 | |
| 1858 | /* Set to 1 all bits to the left of the rightmost. */ |
| 1859 | z_mask = arg_info(op->args[1])->z_mask; |
| 1860 | ctx->z_mask = -(z_mask & -z_mask); |
| 1861 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1862 | /* |
| 1863 | * Because of fold_sub_to_neg, we want to always return true, |
| 1864 | * via finish_folding. |
| 1865 | */ |
| 1866 | finish_folding(ctx, op); |
| 1867 | return true; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static bool fold_nor(OptContext *ctx, TCGOp *op) |
| 1871 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1872 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1873 | fold_xi_to_not(ctx, op, 0)) { |
| 1874 | return true; |
| 1875 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1876 | |
| 1877 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1878 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1879 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static bool fold_not(OptContext *ctx, TCGOp *op) |
| 1883 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1884 | if (fold_const1(ctx, op)) { |
| 1885 | return true; |
| 1886 | } |
| 1887 | |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1888 | ctx->s_mask = arg_info(op->args[1])->s_mask; |
| 1889 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1890 | /* Because of fold_to_not, we want to always return true, via finish. */ |
| 1891 | finish_folding(ctx, op); |
| 1892 | return true; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | static bool fold_or(OptContext *ctx, TCGOp *op) |
| 1896 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1897 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1898 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1899 | fold_xx_to_x(ctx, op)) { |
| 1900 | return true; |
| 1901 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1902 | |
| 1903 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 1904 | | arg_info(op->args[2])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1905 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1906 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1907 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | static bool fold_orc(OptContext *ctx, TCGOp *op) |
| 1911 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1912 | if (fold_const2(ctx, op) || |
Richard Henderson | 4e858d9 | 2021-08-26 07:31:13 -0700 | [diff] [blame] | 1913 | fold_xx_to_i(ctx, op, -1) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1914 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1915 | fold_ix_to_not(ctx, op, 0)) { |
| 1916 | return true; |
| 1917 | } |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 1918 | |
| 1919 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 1920 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1921 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1924 | static bool fold_qemu_ld(OptContext *ctx, TCGOp *op) |
| 1925 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1926 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 1927 | MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs]; |
| 1928 | MemOp mop = get_memop(oi); |
| 1929 | int width = 8 * memop_size(mop); |
| 1930 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 1931 | if (width < 64) { |
| 1932 | ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width); |
| 1933 | if (!(mop & MO_SIGN)) { |
| 1934 | ctx->z_mask = MAKE_64BIT_MASK(0, width); |
| 1935 | ctx->s_mask <<= 1; |
| 1936 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1939 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 1940 | ctx->prev_mb = NULL; |
| 1941 | return false; |
| 1942 | } |
| 1943 | |
| 1944 | static bool fold_qemu_st(OptContext *ctx, TCGOp *op) |
| 1945 | { |
| 1946 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 1947 | ctx->prev_mb = NULL; |
| 1948 | return false; |
| 1949 | } |
| 1950 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1951 | static bool fold_remainder(OptContext *ctx, TCGOp *op) |
| 1952 | { |
Richard Henderson | 267c17e | 2021-10-25 11:30:33 -0700 | [diff] [blame] | 1953 | if (fold_const2(ctx, op) || |
| 1954 | fold_xx_to_i(ctx, op, 0)) { |
| 1955 | return true; |
| 1956 | } |
| 1957 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1960 | static bool fold_setcond(OptContext *ctx, TCGOp *op) |
| 1961 | { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1962 | int i = do_constant_folding_cond1(ctx, op->args[0], &op->args[1], |
| 1963 | &op->args[2], &op->args[3]); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1964 | if (i >= 0) { |
| 1965 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1966 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1967 | |
| 1968 | ctx->z_mask = 1; |
Richard Henderson | 275d7d8 | 2021-08-26 13:20:39 -0700 | [diff] [blame] | 1969 | ctx->s_mask = smask_from_zmask(1); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1970 | return false; |
| 1971 | } |
| 1972 | |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1973 | static bool fold_negsetcond(OptContext *ctx, TCGOp *op) |
| 1974 | { |
Richard Henderson | 246c4b7 | 2023-10-24 16:36:50 -0700 | [diff] [blame^] | 1975 | int i = do_constant_folding_cond1(ctx, op->args[0], &op->args[1], |
| 1976 | &op->args[2], &op->args[3]); |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 1977 | if (i >= 0) { |
| 1978 | return tcg_opt_gen_movi(ctx, op, op->args[0], -i); |
| 1979 | } |
| 1980 | |
| 1981 | /* Value is {0,-1} so all bits are repetitions of the sign. */ |
| 1982 | ctx->s_mask = -1; |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
| 1986 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1987 | static bool fold_setcond2(OptContext *ctx, TCGOp *op) |
| 1988 | { |
| 1989 | TCGCond cond = op->args[5]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1990 | int i, inv = 0; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1991 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1992 | if (swap_commutative2(&op->args[1], &op->args[3])) { |
| 1993 | op->args[5] = cond = tcg_swap_cond(cond); |
| 1994 | } |
| 1995 | |
| 1996 | i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1997 | if (i >= 0) { |
| 1998 | goto do_setcond_const; |
| 1999 | } |
| 2000 | |
| 2001 | switch (cond) { |
| 2002 | case TCG_COND_LT: |
| 2003 | case TCG_COND_GE: |
| 2004 | /* |
| 2005 | * Simplify LT/GE comparisons vs zero to a single compare |
| 2006 | * vs the high word of the input. |
| 2007 | */ |
Richard Henderson | 27cdb85 | 2023-10-23 11:38:00 -0700 | [diff] [blame] | 2008 | if (arg_is_const_val(op->args[3], 0) && |
| 2009 | arg_is_const_val(op->args[4], 0)) { |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2010 | goto do_setcond_high; |
| 2011 | } |
| 2012 | break; |
| 2013 | |
| 2014 | case TCG_COND_NE: |
| 2015 | inv = 1; |
| 2016 | QEMU_FALLTHROUGH; |
| 2017 | case TCG_COND_EQ: |
| 2018 | /* |
| 2019 | * Simplify EQ/NE comparisons where one of the pairs |
| 2020 | * can be simplified. |
| 2021 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2022 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2023 | op->args[3], cond); |
| 2024 | switch (i ^ inv) { |
| 2025 | case 0: |
| 2026 | goto do_setcond_const; |
| 2027 | case 1: |
| 2028 | goto do_setcond_high; |
| 2029 | } |
| 2030 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2031 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2032 | op->args[4], cond); |
| 2033 | switch (i ^ inv) { |
| 2034 | case 0: |
| 2035 | goto do_setcond_const; |
| 2036 | case 1: |
| 2037 | op->args[2] = op->args[3]; |
| 2038 | op->args[3] = cond; |
| 2039 | op->opc = INDEX_op_setcond_i32; |
| 2040 | break; |
| 2041 | } |
| 2042 | break; |
| 2043 | |
| 2044 | default: |
| 2045 | break; |
| 2046 | |
| 2047 | do_setcond_high: |
| 2048 | op->args[1] = op->args[2]; |
| 2049 | op->args[2] = op->args[4]; |
| 2050 | op->args[3] = cond; |
| 2051 | op->opc = INDEX_op_setcond_i32; |
| 2052 | break; |
| 2053 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2054 | |
| 2055 | ctx->z_mask = 1; |
Richard Henderson | 275d7d8 | 2021-08-26 13:20:39 -0700 | [diff] [blame] | 2056 | ctx->s_mask = smask_from_zmask(1); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2057 | return false; |
| 2058 | |
| 2059 | do_setcond_const: |
| 2060 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 2061 | } |
| 2062 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2063 | static bool fold_sextract(OptContext *ctx, TCGOp *op) |
| 2064 | { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2065 | uint64_t z_mask, s_mask, s_mask_old; |
| 2066 | int pos = op->args[2]; |
| 2067 | int len = op->args[3]; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2068 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2069 | if (arg_is_const(op->args[1])) { |
| 2070 | uint64_t t; |
| 2071 | |
| 2072 | t = arg_info(op->args[1])->val; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2073 | t = sextract64(t, pos, len); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2074 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 2075 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2076 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2077 | z_mask = arg_info(op->args[1])->z_mask; |
| 2078 | z_mask = sextract64(z_mask, pos, len); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2079 | ctx->z_mask = z_mask; |
| 2080 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2081 | s_mask_old = arg_info(op->args[1])->s_mask; |
| 2082 | s_mask = sextract64(s_mask_old, pos, len); |
| 2083 | s_mask |= MAKE_64BIT_MASK(len, 64 - len); |
| 2084 | ctx->s_mask = s_mask; |
| 2085 | |
| 2086 | if (pos == 0) { |
| 2087 | ctx->a_mask = s_mask & ~s_mask_old; |
| 2088 | } |
| 2089 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2090 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2093 | static bool fold_shift(OptContext *ctx, TCGOp *op) |
| 2094 | { |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2095 | uint64_t s_mask, z_mask, sign; |
| 2096 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2097 | if (fold_const2(ctx, op) || |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 2098 | fold_ix_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2099 | fold_xi_to_x(ctx, op, 0)) { |
| 2100 | return true; |
| 2101 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2102 | |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2103 | s_mask = arg_info(op->args[1])->s_mask; |
| 2104 | z_mask = arg_info(op->args[1])->z_mask; |
| 2105 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2106 | if (arg_is_const(op->args[2])) { |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2107 | int sh = arg_info(op->args[2])->val; |
| 2108 | |
| 2109 | ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh); |
| 2110 | |
| 2111 | s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh); |
| 2112 | ctx->s_mask = smask_from_smask(s_mask); |
| 2113 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2114 | return fold_masks(ctx, op); |
| 2115 | } |
Richard Henderson | 93a967f | 2021-08-26 13:24:59 -0700 | [diff] [blame] | 2116 | |
| 2117 | switch (op->opc) { |
| 2118 | CASE_OP_32_64(sar): |
| 2119 | /* |
| 2120 | * Arithmetic right shift will not reduce the number of |
| 2121 | * input sign repetitions. |
| 2122 | */ |
| 2123 | ctx->s_mask = s_mask; |
| 2124 | break; |
| 2125 | CASE_OP_32_64(shr): |
| 2126 | /* |
| 2127 | * If the sign bit is known zero, then logical right shift |
| 2128 | * will not reduced the number of input sign repetitions. |
| 2129 | */ |
| 2130 | sign = (s_mask & -s_mask) >> 1; |
| 2131 | if (!(z_mask & sign)) { |
| 2132 | ctx->s_mask = s_mask; |
| 2133 | } |
| 2134 | break; |
| 2135 | default: |
| 2136 | break; |
| 2137 | } |
| 2138 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2139 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2142 | static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op) |
| 2143 | { |
| 2144 | TCGOpcode neg_op; |
| 2145 | bool have_neg; |
| 2146 | |
| 2147 | if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) { |
| 2148 | return false; |
| 2149 | } |
| 2150 | |
| 2151 | switch (ctx->type) { |
| 2152 | case TCG_TYPE_I32: |
| 2153 | neg_op = INDEX_op_neg_i32; |
Richard Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2154 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2155 | break; |
| 2156 | case TCG_TYPE_I64: |
| 2157 | neg_op = INDEX_op_neg_i64; |
Richard Henderson | b701f19 | 2023-10-25 21:14:04 -0700 | [diff] [blame] | 2158 | have_neg = true; |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2159 | break; |
| 2160 | case TCG_TYPE_V64: |
| 2161 | case TCG_TYPE_V128: |
| 2162 | case TCG_TYPE_V256: |
| 2163 | neg_op = INDEX_op_neg_vec; |
| 2164 | have_neg = (TCG_TARGET_HAS_neg_vec && |
| 2165 | tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0); |
| 2166 | break; |
| 2167 | default: |
| 2168 | g_assert_not_reached(); |
| 2169 | } |
| 2170 | if (have_neg) { |
| 2171 | op->opc = neg_op; |
| 2172 | op->args[1] = op->args[2]; |
| 2173 | return fold_neg(ctx, op); |
| 2174 | } |
| 2175 | return false; |
| 2176 | } |
| 2177 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2178 | /* We cannot as yet do_constant_folding with vectors. */ |
| 2179 | static bool fold_sub_vec(OptContext *ctx, TCGOp *op) |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2180 | { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2181 | if (fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2182 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 2183 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2184 | return true; |
| 2185 | } |
| 2186 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2189 | static bool fold_sub(OptContext *ctx, TCGOp *op) |
| 2190 | { |
Richard Henderson | 6334a96 | 2023-10-25 18:39:43 -0700 | [diff] [blame] | 2191 | if (fold_const2(ctx, op) || fold_sub_vec(ctx, op)) { |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | /* Fold sub r,x,i to add r,x,-i */ |
| 2196 | if (arg_is_const(op->args[2])) { |
| 2197 | uint64_t val = arg_info(op->args[2])->val; |
| 2198 | |
| 2199 | op->opc = (ctx->type == TCG_TYPE_I32 |
| 2200 | ? INDEX_op_add_i32 : INDEX_op_add_i64); |
| 2201 | op->args[2] = arg_new_constant(ctx, -val); |
| 2202 | } |
| 2203 | return false; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2206 | static bool fold_sub2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2207 | { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2208 | return fold_addsub2(ctx, op, false); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2211 | static bool fold_tcg_ld(OptContext *ctx, TCGOp *op) |
| 2212 | { |
| 2213 | /* We can't do any folding with a load, but we can record bits. */ |
| 2214 | switch (op->opc) { |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2215 | CASE_OP_32_64(ld8s): |
| 2216 | ctx->s_mask = MAKE_64BIT_MASK(8, 56); |
| 2217 | break; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2218 | CASE_OP_32_64(ld8u): |
| 2219 | ctx->z_mask = MAKE_64BIT_MASK(0, 8); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2220 | ctx->s_mask = MAKE_64BIT_MASK(9, 55); |
| 2221 | break; |
| 2222 | CASE_OP_32_64(ld16s): |
| 2223 | ctx->s_mask = MAKE_64BIT_MASK(16, 48); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2224 | break; |
| 2225 | CASE_OP_32_64(ld16u): |
| 2226 | ctx->z_mask = MAKE_64BIT_MASK(0, 16); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2227 | ctx->s_mask = MAKE_64BIT_MASK(17, 47); |
| 2228 | break; |
| 2229 | case INDEX_op_ld32s_i64: |
| 2230 | ctx->s_mask = MAKE_64BIT_MASK(32, 32); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2231 | break; |
| 2232 | case INDEX_op_ld32u_i64: |
| 2233 | ctx->z_mask = MAKE_64BIT_MASK(0, 32); |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2234 | ctx->s_mask = MAKE_64BIT_MASK(33, 31); |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2235 | break; |
| 2236 | default: |
| 2237 | g_assert_not_reached(); |
| 2238 | } |
| 2239 | return false; |
| 2240 | } |
| 2241 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2242 | static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op) |
| 2243 | { |
| 2244 | TCGTemp *dst, *src; |
| 2245 | intptr_t ofs; |
| 2246 | TCGType type; |
| 2247 | |
| 2248 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
| 2249 | return false; |
| 2250 | } |
| 2251 | |
| 2252 | type = ctx->type; |
| 2253 | ofs = op->args[2]; |
| 2254 | dst = arg_temp(op->args[0]); |
| 2255 | src = find_mem_copy_for(ctx, type, ofs); |
| 2256 | if (src && src->base_type == type) { |
| 2257 | return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src)); |
| 2258 | } |
| 2259 | |
| 2260 | reset_ts(ctx, dst); |
| 2261 | record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1); |
| 2262 | return true; |
| 2263 | } |
| 2264 | |
| 2265 | static bool fold_tcg_st(OptContext *ctx, TCGOp *op) |
| 2266 | { |
| 2267 | intptr_t ofs = op->args[2]; |
| 2268 | intptr_t lm1; |
| 2269 | |
| 2270 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
| 2271 | remove_mem_copy_all(ctx); |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | switch (op->opc) { |
| 2276 | CASE_OP_32_64(st8): |
| 2277 | lm1 = 0; |
| 2278 | break; |
| 2279 | CASE_OP_32_64(st16): |
| 2280 | lm1 = 1; |
| 2281 | break; |
| 2282 | case INDEX_op_st32_i64: |
| 2283 | case INDEX_op_st_i32: |
| 2284 | lm1 = 3; |
| 2285 | break; |
| 2286 | case INDEX_op_st_i64: |
| 2287 | lm1 = 7; |
| 2288 | break; |
| 2289 | case INDEX_op_st_vec: |
| 2290 | lm1 = tcg_type_size(ctx->type) - 1; |
| 2291 | break; |
| 2292 | default: |
| 2293 | g_assert_not_reached(); |
| 2294 | } |
| 2295 | remove_mem_copy_in(ctx, ofs, ofs + lm1); |
| 2296 | return false; |
| 2297 | } |
| 2298 | |
| 2299 | static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op) |
| 2300 | { |
| 2301 | TCGTemp *src; |
| 2302 | intptr_t ofs, last; |
| 2303 | TCGType type; |
| 2304 | |
| 2305 | if (op->args[1] != tcgv_ptr_arg(tcg_env)) { |
| 2306 | fold_tcg_st(ctx, op); |
| 2307 | return false; |
| 2308 | } |
| 2309 | |
| 2310 | src = arg_temp(op->args[0]); |
| 2311 | ofs = op->args[2]; |
| 2312 | type = ctx->type; |
Richard Henderson | 3eaadae | 2023-08-23 23:13:06 -0700 | [diff] [blame] | 2313 | |
| 2314 | /* |
| 2315 | * Eliminate duplicate stores of a constant. |
| 2316 | * This happens frequently when the target ISA zero-extends. |
| 2317 | */ |
| 2318 | if (ts_is_const(src)) { |
| 2319 | TCGTemp *prev = find_mem_copy_for(ctx, type, ofs); |
| 2320 | if (src == prev) { |
| 2321 | tcg_op_remove(ctx->tcg, op); |
| 2322 | return true; |
| 2323 | } |
| 2324 | } |
| 2325 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2326 | last = ofs + tcg_type_size(type) - 1; |
| 2327 | remove_mem_copy_in(ctx, ofs, last); |
| 2328 | record_mem_copy(ctx, type, src, ofs, last); |
| 2329 | return false; |
| 2330 | } |
| 2331 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2332 | static bool fold_xor(OptContext *ctx, TCGOp *op) |
| 2333 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 2334 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2335 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 2336 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 2337 | fold_xi_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 2338 | return true; |
| 2339 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2340 | |
| 2341 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 2342 | | arg_info(op->args[2])->z_mask; |
Richard Henderson | 3f2b1f8 | 2021-08-26 13:08:54 -0700 | [diff] [blame] | 2343 | ctx->s_mask = arg_info(op->args[1])->s_mask |
| 2344 | & arg_info(op->args[2])->s_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2345 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2348 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 2349 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2350 | { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2351 | int nb_temps, i; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 2352 | TCGOp *op, *op_next; |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 2353 | OptContext ctx = { .tcg = s }; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 2354 | |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2355 | QSIMPLEQ_INIT(&ctx.mem_free); |
| 2356 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2357 | /* Array VALS has an element for each temp. |
| 2358 | 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] | 2359 | If this temp is a copy of other ones then the other copies are |
| 2360 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2361 | |
| 2362 | nb_temps = s->nb_temps; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 2363 | for (i = 0; i < nb_temps; ++i) { |
| 2364 | s->temps[i].state_ptr = NULL; |
| 2365 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2366 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 2367 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2368 | TCGOpcode opc = op->opc; |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2369 | const TCGOpDef *def; |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 2370 | bool done = false; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2371 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2372 | /* Calls are special. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2373 | if (opc == INDEX_op_call) { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2374 | fold_call(&ctx, op); |
| 2375 | continue; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 2376 | } |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 2377 | |
| 2378 | def = &tcg_op_defs[opc]; |
Richard Henderson | ec5d4cb | 2021-08-24 08:20:27 -0700 | [diff] [blame] | 2379 | init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); |
| 2380 | copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 2381 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 2382 | /* Pre-compute the type of the operation. */ |
| 2383 | if (def->flags & TCG_OPF_VECTOR) { |
| 2384 | ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op); |
| 2385 | } else if (def->flags & TCG_OPF_64BIT) { |
| 2386 | ctx.type = TCG_TYPE_I64; |
| 2387 | } else { |
| 2388 | ctx.type = TCG_TYPE_I32; |
| 2389 | } |
| 2390 | |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2391 | /* Assume all bits affected, no bits known zero, no sign reps. */ |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2392 | ctx.a_mask = -1; |
| 2393 | ctx.z_mask = -1; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2394 | ctx.s_mask = 0; |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 2395 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2396 | /* |
| 2397 | * Process each opcode. |
| 2398 | * Sorted alphabetically by opcode as much as possible. |
| 2399 | */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 2400 | switch (opc) { |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2401 | CASE_OP_32_64(add): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2402 | done = fold_add(&ctx, op); |
| 2403 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2404 | case INDEX_op_add_vec: |
| 2405 | done = fold_add_vec(&ctx, op); |
| 2406 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2407 | CASE_OP_32_64(add2): |
| 2408 | done = fold_add2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2409 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2410 | CASE_OP_32_64_VEC(and): |
| 2411 | done = fold_and(&ctx, op); |
| 2412 | break; |
| 2413 | CASE_OP_32_64_VEC(andc): |
| 2414 | done = fold_andc(&ctx, op); |
| 2415 | break; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 2416 | CASE_OP_32_64(brcond): |
| 2417 | done = fold_brcond(&ctx, op); |
| 2418 | break; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 2419 | case INDEX_op_brcond2_i32: |
| 2420 | done = fold_brcond2(&ctx, op); |
| 2421 | break; |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 2422 | CASE_OP_32_64(bswap16): |
| 2423 | CASE_OP_32_64(bswap32): |
| 2424 | case INDEX_op_bswap64_i64: |
| 2425 | done = fold_bswap(&ctx, op); |
| 2426 | break; |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 2427 | CASE_OP_32_64(clz): |
| 2428 | CASE_OP_32_64(ctz): |
| 2429 | done = fold_count_zeros(&ctx, op); |
| 2430 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2431 | CASE_OP_32_64(ctpop): |
| 2432 | done = fold_ctpop(&ctx, op); |
| 2433 | break; |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 2434 | CASE_OP_32_64(deposit): |
| 2435 | done = fold_deposit(&ctx, op); |
| 2436 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2437 | CASE_OP_32_64(div): |
| 2438 | CASE_OP_32_64(divu): |
| 2439 | done = fold_divide(&ctx, op); |
| 2440 | break; |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 2441 | case INDEX_op_dup_vec: |
| 2442 | done = fold_dup(&ctx, op); |
| 2443 | break; |
| 2444 | case INDEX_op_dup2_vec: |
| 2445 | done = fold_dup2(&ctx, op); |
| 2446 | break; |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2447 | CASE_OP_32_64_VEC(eqv): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2448 | done = fold_eqv(&ctx, op); |
| 2449 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2450 | CASE_OP_32_64(extract): |
| 2451 | done = fold_extract(&ctx, op); |
| 2452 | break; |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 2453 | CASE_OP_32_64(extract2): |
| 2454 | done = fold_extract2(&ctx, op); |
| 2455 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2456 | CASE_OP_32_64(ext8s): |
| 2457 | CASE_OP_32_64(ext16s): |
| 2458 | case INDEX_op_ext32s_i64: |
| 2459 | case INDEX_op_ext_i32_i64: |
| 2460 | done = fold_exts(&ctx, op); |
| 2461 | break; |
| 2462 | CASE_OP_32_64(ext8u): |
| 2463 | CASE_OP_32_64(ext16u): |
| 2464 | case INDEX_op_ext32u_i64: |
| 2465 | case INDEX_op_extu_i32_i64: |
| 2466 | case INDEX_op_extrl_i64_i32: |
| 2467 | case INDEX_op_extrh_i64_i32: |
| 2468 | done = fold_extu(&ctx, op); |
| 2469 | break; |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2470 | CASE_OP_32_64(ld8s): |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2471 | CASE_OP_32_64(ld8u): |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2472 | CASE_OP_32_64(ld16s): |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2473 | CASE_OP_32_64(ld16u): |
Richard Henderson | 57fe5c6 | 2021-08-26 12:04:46 -0700 | [diff] [blame] | 2474 | case INDEX_op_ld32s_i64: |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 2475 | case INDEX_op_ld32u_i64: |
| 2476 | done = fold_tcg_ld(&ctx, op); |
| 2477 | break; |
Richard Henderson | ab84dc3 | 2023-08-23 23:04:24 -0700 | [diff] [blame] | 2478 | case INDEX_op_ld_i32: |
| 2479 | case INDEX_op_ld_i64: |
| 2480 | case INDEX_op_ld_vec: |
| 2481 | done = fold_tcg_ld_memcopy(&ctx, op); |
| 2482 | break; |
| 2483 | CASE_OP_32_64(st8): |
| 2484 | CASE_OP_32_64(st16): |
| 2485 | case INDEX_op_st32_i64: |
| 2486 | done = fold_tcg_st(&ctx, op); |
| 2487 | break; |
| 2488 | case INDEX_op_st_i32: |
| 2489 | case INDEX_op_st_i64: |
| 2490 | case INDEX_op_st_vec: |
| 2491 | done = fold_tcg_st_memcopy(&ctx, op); |
| 2492 | break; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2493 | case INDEX_op_mb: |
| 2494 | done = fold_mb(&ctx, op); |
| 2495 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2496 | CASE_OP_32_64_VEC(mov): |
| 2497 | done = fold_mov(&ctx, op); |
| 2498 | break; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 2499 | CASE_OP_32_64(movcond): |
| 2500 | done = fold_movcond(&ctx, op); |
| 2501 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2502 | CASE_OP_32_64(mul): |
| 2503 | done = fold_mul(&ctx, op); |
| 2504 | break; |
| 2505 | CASE_OP_32_64(mulsh): |
| 2506 | CASE_OP_32_64(muluh): |
| 2507 | done = fold_mul_highpart(&ctx, op); |
| 2508 | break; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 2509 | CASE_OP_32_64(muls2): |
| 2510 | CASE_OP_32_64(mulu2): |
| 2511 | done = fold_multiply2(&ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 2512 | break; |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2513 | CASE_OP_32_64_VEC(nand): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2514 | done = fold_nand(&ctx, op); |
| 2515 | break; |
| 2516 | CASE_OP_32_64(neg): |
| 2517 | done = fold_neg(&ctx, op); |
| 2518 | break; |
Richard Henderson | ed52347 | 2021-12-16 11:17:46 -0800 | [diff] [blame] | 2519 | CASE_OP_32_64_VEC(nor): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2520 | done = fold_nor(&ctx, op); |
| 2521 | break; |
| 2522 | CASE_OP_32_64_VEC(not): |
| 2523 | done = fold_not(&ctx, op); |
| 2524 | break; |
| 2525 | CASE_OP_32_64_VEC(or): |
| 2526 | done = fold_or(&ctx, op); |
| 2527 | break; |
| 2528 | CASE_OP_32_64_VEC(orc): |
| 2529 | done = fold_orc(&ctx, op); |
| 2530 | break; |
Richard Henderson | fecccfc | 2023-05-16 20:07:20 -0700 | [diff] [blame] | 2531 | case INDEX_op_qemu_ld_a32_i32: |
| 2532 | case INDEX_op_qemu_ld_a64_i32: |
| 2533 | case INDEX_op_qemu_ld_a32_i64: |
| 2534 | case INDEX_op_qemu_ld_a64_i64: |
| 2535 | case INDEX_op_qemu_ld_a32_i128: |
| 2536 | case INDEX_op_qemu_ld_a64_i128: |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2537 | done = fold_qemu_ld(&ctx, op); |
| 2538 | break; |
Richard Henderson | fecccfc | 2023-05-16 20:07:20 -0700 | [diff] [blame] | 2539 | case INDEX_op_qemu_st8_a32_i32: |
| 2540 | case INDEX_op_qemu_st8_a64_i32: |
| 2541 | case INDEX_op_qemu_st_a32_i32: |
| 2542 | case INDEX_op_qemu_st_a64_i32: |
| 2543 | case INDEX_op_qemu_st_a32_i64: |
| 2544 | case INDEX_op_qemu_st_a64_i64: |
| 2545 | case INDEX_op_qemu_st_a32_i128: |
| 2546 | case INDEX_op_qemu_st_a64_i128: |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2547 | done = fold_qemu_st(&ctx, op); |
| 2548 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2549 | CASE_OP_32_64(rem): |
| 2550 | CASE_OP_32_64(remu): |
| 2551 | done = fold_remainder(&ctx, op); |
| 2552 | break; |
| 2553 | CASE_OP_32_64(rotl): |
| 2554 | CASE_OP_32_64(rotr): |
| 2555 | CASE_OP_32_64(sar): |
| 2556 | CASE_OP_32_64(shl): |
| 2557 | CASE_OP_32_64(shr): |
| 2558 | done = fold_shift(&ctx, op); |
| 2559 | break; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2560 | CASE_OP_32_64(setcond): |
| 2561 | done = fold_setcond(&ctx, op); |
| 2562 | break; |
Richard Henderson | 3635502 | 2023-08-04 23:24:04 +0000 | [diff] [blame] | 2563 | CASE_OP_32_64(negsetcond): |
| 2564 | done = fold_negsetcond(&ctx, op); |
| 2565 | break; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2566 | case INDEX_op_setcond2_i32: |
| 2567 | done = fold_setcond2(&ctx, op); |
| 2568 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2569 | CASE_OP_32_64(sextract): |
| 2570 | done = fold_sextract(&ctx, op); |
| 2571 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2572 | CASE_OP_32_64(sub): |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2573 | done = fold_sub(&ctx, op); |
| 2574 | break; |
Richard Henderson | c578ff1 | 2021-12-16 06:07:25 -0800 | [diff] [blame] | 2575 | case INDEX_op_sub_vec: |
| 2576 | done = fold_sub_vec(&ctx, op); |
| 2577 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2578 | CASE_OP_32_64(sub2): |
| 2579 | done = fold_sub2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2580 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2581 | CASE_OP_32_64_VEC(xor): |
| 2582 | done = fold_xor(&ctx, op); |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 2583 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2584 | default: |
| 2585 | break; |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 2588 | if (!done) { |
| 2589 | finish_folding(&ctx, op); |
| 2590 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2591 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2592 | } |