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