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