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