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