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