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