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" |
Philippe Mathieu-Daudé | dcb32f1 | 2020-01-01 12:23:00 +0100 | [diff] [blame] | 28 | #include "tcg/tcg-op.h" |
Richard Henderson | 9016390 | 2021-03-18 10:21:45 -0600 | [diff] [blame] | 29 | #include "tcg-internal.h" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 30 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 31 | #define CASE_OP_32_64(x) \ |
| 32 | glue(glue(case INDEX_op_, x), _i32): \ |
| 33 | glue(glue(case INDEX_op_, x), _i64) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 34 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 35 | #define CASE_OP_32_64_VEC(x) \ |
| 36 | glue(glue(case INDEX_op_, x), _i32): \ |
| 37 | glue(glue(case INDEX_op_, x), _i64): \ |
| 38 | glue(glue(case INDEX_op_, x), _vec) |
| 39 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 40 | typedef struct TempOptInfo { |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 41 | bool is_const; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 42 | TCGTemp *prev_copy; |
| 43 | TCGTemp *next_copy; |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 44 | uint64_t val; |
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 | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 46 | } TempOptInfo; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 47 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 48 | typedef struct OptContext { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 49 | TCGContext *tcg; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 50 | TCGOp *prev_mb; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 51 | TCGTempSet temps_used; |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 52 | |
| 53 | /* In flight values from optimization. */ |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 54 | uint64_t a_mask; /* mask bit is 0 iff value identical to first input */ |
| 55 | uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 56 | TCGType type; |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 57 | } OptContext; |
| 58 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 59 | static inline TempOptInfo *ts_info(TCGTemp *ts) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 60 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 61 | return ts->state_ptr; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 64 | static inline TempOptInfo *arg_info(TCGArg arg) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 65 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 66 | return ts_info(arg_temp(arg)); |
| 67 | } |
| 68 | |
| 69 | static inline bool ts_is_const(TCGTemp *ts) |
| 70 | { |
| 71 | return ts_info(ts)->is_const; |
| 72 | } |
| 73 | |
| 74 | static inline bool arg_is_const(TCGArg arg) |
| 75 | { |
| 76 | return ts_is_const(arg_temp(arg)); |
| 77 | } |
| 78 | |
| 79 | static inline bool ts_is_copy(TCGTemp *ts) |
| 80 | { |
| 81 | return ts_info(ts)->next_copy != ts; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 84 | /* Reset TEMP's state, possibly removing the temp for the list of copies. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 85 | static void reset_ts(TCGTemp *ts) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 86 | { |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 87 | TempOptInfo *ti = ts_info(ts); |
| 88 | TempOptInfo *pi = ts_info(ti->prev_copy); |
| 89 | TempOptInfo *ni = ts_info(ti->next_copy); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 90 | |
| 91 | ni->prev_copy = ti->prev_copy; |
| 92 | pi->next_copy = ti->next_copy; |
| 93 | ti->next_copy = ts; |
| 94 | ti->prev_copy = ts; |
| 95 | ti->is_const = false; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 96 | ti->z_mask = -1; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void reset_temp(TCGArg arg) |
| 100 | { |
| 101 | reset_ts(arg_temp(arg)); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 102 | } |
| 103 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 104 | /* Initialize and activate a temporary. */ |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 105 | static void init_ts_info(OptContext *ctx, TCGTemp *ts) |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 106 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 107 | size_t idx = temp_idx(ts); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 108 | TempOptInfo *ti; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 109 | |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 110 | if (test_bit(idx, ctx->temps_used.l)) { |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 113 | set_bit(idx, ctx->temps_used.l); |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 114 | |
| 115 | ti = ts->state_ptr; |
| 116 | if (ti == NULL) { |
| 117 | ti = tcg_malloc(sizeof(TempOptInfo)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 118 | ts->state_ptr = ti; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | ti->next_copy = ts; |
| 122 | ti->prev_copy = ts; |
| 123 | if (ts->kind == TEMP_CONST) { |
| 124 | ti->is_const = true; |
| 125 | ti->val = ts->val; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 126 | ti->z_mask = ts->val; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 127 | } else { |
| 128 | ti->is_const = false; |
Richard Henderson | b1fde41 | 2021-08-23 13:07:49 -0700 | [diff] [blame] | 129 | ti->z_mask = -1; |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 133 | static TCGTemp *find_better_copy(TCGContext *s, TCGTemp *ts) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 134 | { |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 135 | TCGTemp *i, *g, *l; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 136 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 137 | /* If this is already readonly, we can't do better. */ |
| 138 | if (temp_readonly(ts)) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 139 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 142 | g = l = NULL; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 143 | for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) { |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 144 | if (temp_readonly(i)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 145 | return i; |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 146 | } else if (i->kind > ts->kind) { |
| 147 | if (i->kind == TEMP_GLOBAL) { |
| 148 | g = i; |
| 149 | } else if (i->kind == TEMP_LOCAL) { |
| 150 | l = i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Richard Henderson | 4c868ce | 2020-04-23 09:02:23 -0700 | [diff] [blame] | 155 | /* If we didn't find a better representation, return the same temp. */ |
| 156 | return g ? g : l ? l : ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 159 | static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 160 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 161 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 162 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 163 | if (ts1 == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 167 | if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 171 | for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { |
| 172 | if (i == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 180 | static bool args_are_copies(TCGArg arg1, TCGArg arg2) |
| 181 | { |
| 182 | return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); |
| 183 | } |
| 184 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 185 | 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] | 186 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 187 | TCGTemp *dst_ts = arg_temp(dst); |
| 188 | TCGTemp *src_ts = arg_temp(src); |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 189 | TempOptInfo *di; |
| 190 | TempOptInfo *si; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 191 | TCGOpcode new_op; |
| 192 | |
| 193 | if (ts_are_copies(dst_ts, src_ts)) { |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 194 | tcg_op_remove(ctx->tcg, op); |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 195 | return true; |
Aurelien Jarno | 5365718 | 2015-06-04 21:53:25 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 198 | reset_ts(dst_ts); |
| 199 | di = ts_info(dst_ts); |
| 200 | si = ts_info(src_ts); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 201 | |
| 202 | switch (ctx->type) { |
| 203 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 204 | new_op = INDEX_op_mov_i32; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 205 | break; |
| 206 | case TCG_TYPE_I64: |
| 207 | new_op = INDEX_op_mov_i64; |
| 208 | break; |
| 209 | case TCG_TYPE_V64: |
| 210 | case TCG_TYPE_V128: |
| 211 | case TCG_TYPE_V256: |
| 212 | /* TCGOP_VECL and TCGOP_VECE remain unchanged. */ |
| 213 | new_op = INDEX_op_mov_vec; |
| 214 | break; |
| 215 | default: |
| 216 | g_assert_not_reached(); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 217 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 218 | op->opc = new_op; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 219 | op->args[0] = dst; |
| 220 | op->args[1] = src; |
Richard Henderson | a62f6f5 | 2014-05-22 10:59:12 -0700 | [diff] [blame] | 221 | |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 222 | di->z_mask = si->z_mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 223 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 224 | if (src_ts->type == dst_ts->type) { |
Richard Henderson | 6fcb98e | 2020-03-30 17:44:30 -0700 | [diff] [blame] | 225 | TempOptInfo *ni = ts_info(si->next_copy); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 226 | |
| 227 | di->next_copy = si->next_copy; |
| 228 | di->prev_copy = src_ts; |
| 229 | ni->prev_copy = dst_ts; |
| 230 | si->next_copy = dst_ts; |
| 231 | di->is_const = si->is_const; |
| 232 | di->val = si->val; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 233 | } |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 234 | return true; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 235 | } |
| 236 | |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 237 | static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op, |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 238 | TCGArg dst, uint64_t val) |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 239 | { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 240 | TCGTemp *tv; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 241 | |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 242 | if (ctx->type == TCG_TYPE_I32) { |
| 243 | val = (int32_t)val; |
| 244 | } |
| 245 | |
| 246 | /* Convert movi to mov with constant temp. */ |
| 247 | tv = tcg_constant_internal(ctx->type, val); |
Richard Henderson | 3b3f847 | 2021-08-23 22:06:31 -0700 | [diff] [blame] | 248 | init_ts_info(ctx, tv); |
Richard Henderson | 6b99d5b | 2021-08-24 10:57:56 -0700 | [diff] [blame] | 249 | return tcg_opt_gen_mov(ctx, op, dst, temp_arg(tv)); |
Richard Henderson | 8fe35e0 | 2020-03-30 20:42:43 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 252 | static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 253 | { |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 254 | uint64_t l64, h64; |
| 255 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 256 | switch (op) { |
| 257 | CASE_OP_32_64(add): |
| 258 | return x + y; |
| 259 | |
| 260 | CASE_OP_32_64(sub): |
| 261 | return x - y; |
| 262 | |
| 263 | CASE_OP_32_64(mul): |
| 264 | return x * y; |
| 265 | |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 266 | CASE_OP_32_64(and): |
| 267 | return x & y; |
| 268 | |
| 269 | CASE_OP_32_64(or): |
| 270 | return x | y; |
| 271 | |
| 272 | CASE_OP_32_64(xor): |
| 273 | return x ^ y; |
| 274 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 275 | case INDEX_op_shl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 276 | return (uint32_t)x << (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 277 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 278 | case INDEX_op_shl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 279 | return (uint64_t)x << (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 280 | |
| 281 | case INDEX_op_shr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 282 | return (uint32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 283 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 284 | case INDEX_op_shr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 285 | return (uint64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 286 | |
| 287 | case INDEX_op_sar_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 288 | return (int32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 289 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 290 | case INDEX_op_sar_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 291 | return (int64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 292 | |
| 293 | case INDEX_op_rotr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 294 | return ror32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 295 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 296 | case INDEX_op_rotr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 297 | return ror64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 298 | |
| 299 | case INDEX_op_rotl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 300 | return rol32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 301 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 302 | case INDEX_op_rotl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 303 | return rol64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 304 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 305 | CASE_OP_32_64(not): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 306 | return ~x; |
| 307 | |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 308 | CASE_OP_32_64(neg): |
| 309 | return -x; |
| 310 | |
| 311 | CASE_OP_32_64(andc): |
| 312 | return x & ~y; |
| 313 | |
| 314 | CASE_OP_32_64(orc): |
| 315 | return x | ~y; |
| 316 | |
| 317 | CASE_OP_32_64(eqv): |
| 318 | return ~(x ^ y); |
| 319 | |
| 320 | CASE_OP_32_64(nand): |
| 321 | return ~(x & y); |
| 322 | |
| 323 | CASE_OP_32_64(nor): |
| 324 | return ~(x | y); |
| 325 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 326 | case INDEX_op_clz_i32: |
| 327 | return (uint32_t)x ? clz32(x) : y; |
| 328 | |
| 329 | case INDEX_op_clz_i64: |
| 330 | return x ? clz64(x) : y; |
| 331 | |
| 332 | case INDEX_op_ctz_i32: |
| 333 | return (uint32_t)x ? ctz32(x) : y; |
| 334 | |
| 335 | case INDEX_op_ctz_i64: |
| 336 | return x ? ctz64(x) : y; |
| 337 | |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 338 | case INDEX_op_ctpop_i32: |
| 339 | return ctpop32(x); |
| 340 | |
| 341 | case INDEX_op_ctpop_i64: |
| 342 | return ctpop64(x); |
| 343 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 344 | CASE_OP_32_64(ext8s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 345 | return (int8_t)x; |
| 346 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 347 | CASE_OP_32_64(ext16s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 348 | return (int16_t)x; |
| 349 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 350 | CASE_OP_32_64(ext8u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 351 | return (uint8_t)x; |
| 352 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 353 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 354 | return (uint16_t)x; |
| 355 | |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 356 | CASE_OP_32_64(bswap16): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 357 | x = bswap16(x); |
| 358 | return y & TCG_BSWAP_OS ? (int16_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 359 | |
| 360 | CASE_OP_32_64(bswap32): |
Richard Henderson | 0b76ff8 | 2021-06-13 13:04:00 -0700 | [diff] [blame] | 361 | x = bswap32(x); |
| 362 | return y & TCG_BSWAP_OS ? (int32_t)x : x; |
Richard Henderson | 6498594 | 2018-11-20 08:53:34 +0100 | [diff] [blame] | 363 | |
| 364 | case INDEX_op_bswap64_i64: |
| 365 | return bswap64(x); |
| 366 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 367 | case INDEX_op_ext_i32_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 368 | case INDEX_op_ext32s_i64: |
| 369 | return (int32_t)x; |
| 370 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 371 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 372 | case INDEX_op_extrl_i64_i32: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 373 | case INDEX_op_ext32u_i64: |
| 374 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 375 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 376 | case INDEX_op_extrh_i64_i32: |
| 377 | return (uint64_t)x >> 32; |
| 378 | |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 379 | case INDEX_op_muluh_i32: |
| 380 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; |
| 381 | case INDEX_op_mulsh_i32: |
| 382 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; |
| 383 | |
| 384 | case INDEX_op_muluh_i64: |
| 385 | mulu64(&l64, &h64, x, y); |
| 386 | return h64; |
| 387 | case INDEX_op_mulsh_i64: |
| 388 | muls64(&l64, &h64, x, y); |
| 389 | return h64; |
| 390 | |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 391 | case INDEX_op_div_i32: |
| 392 | /* Avoid crashing on divide by zero, otherwise undefined. */ |
| 393 | return (int32_t)x / ((int32_t)y ? : 1); |
| 394 | case INDEX_op_divu_i32: |
| 395 | return (uint32_t)x / ((uint32_t)y ? : 1); |
| 396 | case INDEX_op_div_i64: |
| 397 | return (int64_t)x / ((int64_t)y ? : 1); |
| 398 | case INDEX_op_divu_i64: |
| 399 | return (uint64_t)x / ((uint64_t)y ? : 1); |
| 400 | |
| 401 | case INDEX_op_rem_i32: |
| 402 | return (int32_t)x % ((int32_t)y ? : 1); |
| 403 | case INDEX_op_remu_i32: |
| 404 | return (uint32_t)x % ((uint32_t)y ? : 1); |
| 405 | case INDEX_op_rem_i64: |
| 406 | return (int64_t)x % ((int64_t)y ? : 1); |
| 407 | case INDEX_op_remu_i64: |
| 408 | return (uint64_t)x % ((uint64_t)y ? : 1); |
| 409 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 410 | default: |
| 411 | fprintf(stderr, |
| 412 | "Unrecognized operation %d in do_constant_folding.\n", op); |
| 413 | tcg_abort(); |
| 414 | } |
| 415 | } |
| 416 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 417 | static uint64_t do_constant_folding(TCGOpcode op, TCGType type, |
| 418 | uint64_t x, uint64_t y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 419 | { |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 420 | uint64_t res = do_constant_folding_2(op, x, y); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 421 | if (type == TCG_TYPE_I32) { |
Aurelien Jarno | 29f3ff8 | 2015-07-10 18:03:31 +0200 | [diff] [blame] | 422 | res = (int32_t)res; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 423 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 424 | return res; |
| 425 | } |
| 426 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 427 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 428 | { |
| 429 | switch (c) { |
| 430 | case TCG_COND_EQ: |
| 431 | return x == y; |
| 432 | case TCG_COND_NE: |
| 433 | return x != y; |
| 434 | case TCG_COND_LT: |
| 435 | return (int32_t)x < (int32_t)y; |
| 436 | case TCG_COND_GE: |
| 437 | return (int32_t)x >= (int32_t)y; |
| 438 | case TCG_COND_LE: |
| 439 | return (int32_t)x <= (int32_t)y; |
| 440 | case TCG_COND_GT: |
| 441 | return (int32_t)x > (int32_t)y; |
| 442 | case TCG_COND_LTU: |
| 443 | return x < y; |
| 444 | case TCG_COND_GEU: |
| 445 | return x >= y; |
| 446 | case TCG_COND_LEU: |
| 447 | return x <= y; |
| 448 | case TCG_COND_GTU: |
| 449 | return x > y; |
| 450 | default: |
| 451 | tcg_abort(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) |
| 456 | { |
| 457 | switch (c) { |
| 458 | case TCG_COND_EQ: |
| 459 | return x == y; |
| 460 | case TCG_COND_NE: |
| 461 | return x != y; |
| 462 | case TCG_COND_LT: |
| 463 | return (int64_t)x < (int64_t)y; |
| 464 | case TCG_COND_GE: |
| 465 | return (int64_t)x >= (int64_t)y; |
| 466 | case TCG_COND_LE: |
| 467 | return (int64_t)x <= (int64_t)y; |
| 468 | case TCG_COND_GT: |
| 469 | return (int64_t)x > (int64_t)y; |
| 470 | case TCG_COND_LTU: |
| 471 | return x < y; |
| 472 | case TCG_COND_GEU: |
| 473 | return x >= y; |
| 474 | case TCG_COND_LEU: |
| 475 | return x <= y; |
| 476 | case TCG_COND_GTU: |
| 477 | return x > y; |
| 478 | default: |
| 479 | tcg_abort(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static bool do_constant_folding_cond_eq(TCGCond c) |
| 484 | { |
| 485 | switch (c) { |
| 486 | case TCG_COND_GT: |
| 487 | case TCG_COND_LTU: |
| 488 | case TCG_COND_LT: |
| 489 | case TCG_COND_GTU: |
| 490 | case TCG_COND_NE: |
| 491 | return 0; |
| 492 | case TCG_COND_GE: |
| 493 | case TCG_COND_GEU: |
| 494 | case TCG_COND_LE: |
| 495 | case TCG_COND_LEU: |
| 496 | case TCG_COND_EQ: |
| 497 | return 1; |
| 498 | default: |
| 499 | tcg_abort(); |
| 500 | } |
| 501 | } |
| 502 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 503 | /* |
| 504 | * Return -1 if the condition can't be simplified, |
| 505 | * and the result of the condition (0 or 1) if it can. |
| 506 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 507 | static int do_constant_folding_cond(TCGType type, TCGArg x, |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 508 | TCGArg y, TCGCond c) |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 509 | { |
Richard Henderson | 5479554 | 2020-09-06 16:21:32 -0700 | [diff] [blame] | 510 | uint64_t xv = arg_info(x)->val; |
| 511 | uint64_t yv = arg_info(y)->val; |
| 512 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 513 | if (arg_is_const(x) && arg_is_const(y)) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 514 | switch (type) { |
| 515 | case TCG_TYPE_I32: |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 516 | return do_constant_folding_cond_32(xv, yv, c); |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 517 | case TCG_TYPE_I64: |
| 518 | return do_constant_folding_cond_64(xv, yv, c); |
| 519 | default: |
| 520 | /* Only scalar comparisons are optimizable */ |
| 521 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 522 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 523 | } else if (args_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 524 | return do_constant_folding_cond_eq(c); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 525 | } else if (arg_is_const(y) && yv == 0) { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 526 | switch (c) { |
| 527 | case TCG_COND_LTU: |
| 528 | return 0; |
| 529 | case TCG_COND_GEU: |
| 530 | return 1; |
| 531 | default: |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 532 | return -1; |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 533 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 534 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 535 | return -1; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 538 | /* |
| 539 | * Return -1 if the condition can't be simplified, |
| 540 | * and the result of the condition (0 or 1) if it can. |
| 541 | */ |
| 542 | static int do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c) |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 543 | { |
| 544 | TCGArg al = p1[0], ah = p1[1]; |
| 545 | TCGArg bl = p2[0], bh = p2[1]; |
| 546 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 547 | if (arg_is_const(bl) && arg_is_const(bh)) { |
| 548 | tcg_target_ulong blv = arg_info(bl)->val; |
| 549 | tcg_target_ulong bhv = arg_info(bh)->val; |
| 550 | uint64_t b = deposit64(blv, 32, 32, bhv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 551 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 552 | if (arg_is_const(al) && arg_is_const(ah)) { |
| 553 | tcg_target_ulong alv = arg_info(al)->val; |
| 554 | tcg_target_ulong ahv = arg_info(ah)->val; |
| 555 | uint64_t a = deposit64(alv, 32, 32, ahv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 556 | return do_constant_folding_cond_64(a, b, c); |
| 557 | } |
| 558 | if (b == 0) { |
| 559 | switch (c) { |
| 560 | case TCG_COND_LTU: |
| 561 | return 0; |
| 562 | case TCG_COND_GEU: |
| 563 | return 1; |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 569 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 570 | return do_constant_folding_cond_eq(c); |
| 571 | } |
Richard Henderson | 8d57bf1 | 2021-08-24 08:34:27 -0700 | [diff] [blame] | 572 | return -1; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 575 | /** |
| 576 | * swap_commutative: |
| 577 | * @dest: TCGArg of the destination argument, or NO_DEST. |
| 578 | * @p1: first paired argument |
| 579 | * @p2: second paired argument |
| 580 | * |
| 581 | * If *@p1 is a constant and *@p2 is not, swap. |
| 582 | * If *@p2 matches @dest, swap. |
| 583 | * Return true if a swap was performed. |
| 584 | */ |
| 585 | |
| 586 | #define NO_DEST temp_arg(NULL) |
| 587 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 588 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 589 | { |
| 590 | TCGArg a1 = *p1, a2 = *p2; |
| 591 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 592 | sum += arg_is_const(a1); |
| 593 | sum -= arg_is_const(a2); |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 594 | |
| 595 | /* Prefer the constant in second argument, and then the form |
| 596 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 597 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 598 | *p1 = a2; |
| 599 | *p2 = a1; |
| 600 | return true; |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 605 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 606 | { |
| 607 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 608 | sum += arg_is_const(p1[0]); |
| 609 | sum += arg_is_const(p1[1]); |
| 610 | sum -= arg_is_const(p2[0]); |
| 611 | sum -= arg_is_const(p2[1]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 612 | if (sum > 0) { |
| 613 | TCGArg t; |
| 614 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 615 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 616 | return true; |
| 617 | } |
| 618 | return false; |
| 619 | } |
| 620 | |
Richard Henderson | e2577ea | 2021-08-24 08:00:48 -0700 | [diff] [blame] | 621 | static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args) |
| 622 | { |
| 623 | for (int i = 0; i < nb_args; i++) { |
| 624 | TCGTemp *ts = arg_temp(op->args[i]); |
| 625 | if (ts) { |
| 626 | init_ts_info(ctx, ts); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Richard Henderson | 8774dde | 2021-08-24 08:04:47 -0700 | [diff] [blame] | 631 | static void copy_propagate(OptContext *ctx, TCGOp *op, |
| 632 | int nb_oargs, int nb_iargs) |
| 633 | { |
| 634 | TCGContext *s = ctx->tcg; |
| 635 | |
| 636 | for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
| 637 | TCGTemp *ts = arg_temp(op->args[i]); |
| 638 | if (ts && ts_is_copy(ts)) { |
| 639 | op->args[i] = temp_arg(find_better_copy(s, ts)); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Richard Henderson | 137f1f4 | 2021-08-24 08:49:25 -0700 | [diff] [blame] | 644 | static void finish_folding(OptContext *ctx, TCGOp *op) |
| 645 | { |
| 646 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 647 | int i, nb_oargs; |
| 648 | |
| 649 | /* |
| 650 | * For an opcode that ends a BB, reset all temp data. |
| 651 | * We do no cross-BB optimization. |
| 652 | */ |
| 653 | if (def->flags & TCG_OPF_BB_END) { |
| 654 | memset(&ctx->temps_used, 0, sizeof(ctx->temps_used)); |
| 655 | ctx->prev_mb = NULL; |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | nb_oargs = def->nb_oargs; |
| 660 | for (i = 0; i < nb_oargs; i++) { |
| 661 | reset_temp(op->args[i]); |
| 662 | /* |
| 663 | * Save the corresponding known-zero bits mask for the |
| 664 | * first output argument (only one supported so far). |
| 665 | */ |
| 666 | if (i == 0) { |
| 667 | arg_info(op->args[i])->z_mask = ctx->z_mask; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 672 | /* |
| 673 | * The fold_* functions return true when processing is complete, |
| 674 | * usually by folding the operation to a constant or to a copy, |
| 675 | * and calling tcg_opt_gen_{mov,movi}. They may do other things, |
| 676 | * like collect information about the value produced, for use in |
| 677 | * optimizing a subsequent operation. |
| 678 | * |
| 679 | * These first fold_* functions are all helpers, used by other |
| 680 | * folders for more specific operations. |
| 681 | */ |
| 682 | |
| 683 | static bool fold_const1(OptContext *ctx, TCGOp *op) |
| 684 | { |
| 685 | if (arg_is_const(op->args[1])) { |
| 686 | uint64_t t; |
| 687 | |
| 688 | t = arg_info(op->args[1])->val; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 689 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 690 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | static bool fold_const2(OptContext *ctx, TCGOp *op) |
| 696 | { |
| 697 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 698 | uint64_t t1 = arg_info(op->args[1])->val; |
| 699 | uint64_t t2 = arg_info(op->args[2])->val; |
| 700 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 701 | t1 = do_constant_folding(op->opc, ctx->type, t1, t2); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 702 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 703 | } |
| 704 | return false; |
| 705 | } |
| 706 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 707 | static bool fold_const2_commutative(OptContext *ctx, TCGOp *op) |
| 708 | { |
| 709 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
| 710 | return fold_const2(ctx, op); |
| 711 | } |
| 712 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 713 | static bool fold_masks(OptContext *ctx, TCGOp *op) |
| 714 | { |
| 715 | uint64_t a_mask = ctx->a_mask; |
| 716 | uint64_t z_mask = ctx->z_mask; |
| 717 | |
| 718 | /* |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 719 | * 32-bit ops generate 32-bit results, which for the purpose of |
| 720 | * simplifying tcg are sign-extended. Certainly that's how we |
| 721 | * represent our constants elsewhere. Note that the bits will |
| 722 | * be reset properly for a 64-bit value when encountering the |
| 723 | * type changing opcodes. |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 724 | */ |
| 725 | if (ctx->type == TCG_TYPE_I32) { |
Richard Henderson | faa2e10 | 2021-08-26 09:03:59 -0700 | [diff] [blame] | 726 | a_mask = (int32_t)a_mask; |
| 727 | z_mask = (int32_t)z_mask; |
| 728 | ctx->z_mask = z_mask; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | if (z_mask == 0) { |
| 732 | return tcg_opt_gen_movi(ctx, op, op->args[0], 0); |
| 733 | } |
| 734 | if (a_mask == 0) { |
| 735 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 736 | } |
| 737 | return false; |
| 738 | } |
| 739 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 740 | /* |
| 741 | * Convert @op to NOT, if NOT is supported by the host. |
| 742 | * Return true f the conversion is successful, which will still |
| 743 | * indicate that the processing is complete. |
| 744 | */ |
| 745 | static bool fold_not(OptContext *ctx, TCGOp *op); |
| 746 | static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx) |
| 747 | { |
| 748 | TCGOpcode not_op; |
| 749 | bool have_not; |
| 750 | |
| 751 | switch (ctx->type) { |
| 752 | case TCG_TYPE_I32: |
| 753 | not_op = INDEX_op_not_i32; |
| 754 | have_not = TCG_TARGET_HAS_not_i32; |
| 755 | break; |
| 756 | case TCG_TYPE_I64: |
| 757 | not_op = INDEX_op_not_i64; |
| 758 | have_not = TCG_TARGET_HAS_not_i64; |
| 759 | break; |
| 760 | case TCG_TYPE_V64: |
| 761 | case TCG_TYPE_V128: |
| 762 | case TCG_TYPE_V256: |
| 763 | not_op = INDEX_op_not_vec; |
| 764 | have_not = TCG_TARGET_HAS_not_vec; |
| 765 | break; |
| 766 | default: |
| 767 | g_assert_not_reached(); |
| 768 | } |
| 769 | if (have_not) { |
| 770 | op->opc = not_op; |
| 771 | op->args[1] = op->args[idx]; |
| 772 | return fold_not(ctx, op); |
| 773 | } |
| 774 | return false; |
| 775 | } |
| 776 | |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 777 | /* If the binary operation has first argument @i, fold to @i. */ |
| 778 | static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 779 | { |
| 780 | if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) { |
| 781 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 782 | } |
| 783 | return false; |
| 784 | } |
| 785 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 786 | /* If the binary operation has first argument @i, fold to NOT. */ |
| 787 | static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 788 | { |
| 789 | if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) { |
| 790 | return fold_to_not(ctx, op, 2); |
| 791 | } |
| 792 | return false; |
| 793 | } |
| 794 | |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 795 | /* If the binary operation has second argument @i, fold to @i. */ |
| 796 | static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 797 | { |
| 798 | if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) { |
| 799 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 804 | /* If the binary operation has second argument @i, fold to identity. */ |
| 805 | static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) |
| 806 | { |
| 807 | if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) { |
| 808 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 809 | } |
| 810 | return false; |
| 811 | } |
| 812 | |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 813 | /* If the binary operation has second argument @i, fold to NOT. */ |
| 814 | static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) |
| 815 | { |
| 816 | if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) { |
| 817 | return fold_to_not(ctx, op, 1); |
| 818 | } |
| 819 | return false; |
| 820 | } |
| 821 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 822 | /* If the binary operation has both arguments equal, fold to @i. */ |
| 823 | static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i) |
| 824 | { |
| 825 | if (args_are_copies(op->args[1], op->args[2])) { |
| 826 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 827 | } |
| 828 | return false; |
| 829 | } |
| 830 | |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 831 | /* If the binary operation has both arguments equal, fold to identity. */ |
| 832 | static bool fold_xx_to_x(OptContext *ctx, TCGOp *op) |
| 833 | { |
| 834 | if (args_are_copies(op->args[1], op->args[2])) { |
| 835 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 836 | } |
| 837 | return false; |
| 838 | } |
| 839 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 840 | /* |
| 841 | * These outermost fold_<op> functions are sorted alphabetically. |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 842 | * |
| 843 | * The ordering of the transformations should be: |
| 844 | * 1) those that produce a constant |
| 845 | * 2) those that produce a copy |
| 846 | * 3) those that produce information about the result value. |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 847 | */ |
| 848 | |
| 849 | static bool fold_add(OptContext *ctx, TCGOp *op) |
| 850 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 851 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 852 | fold_xi_to_x(ctx, op, 0)) { |
| 853 | return true; |
| 854 | } |
| 855 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 858 | static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 859 | { |
| 860 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3]) && |
| 861 | arg_is_const(op->args[4]) && arg_is_const(op->args[5])) { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 862 | uint64_t al = arg_info(op->args[2])->val; |
| 863 | uint64_t ah = arg_info(op->args[3])->val; |
| 864 | uint64_t bl = arg_info(op->args[4])->val; |
| 865 | uint64_t bh = arg_info(op->args[5])->val; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 866 | TCGArg rl, rh; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 867 | TCGOp *op2; |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 868 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 869 | if (ctx->type == TCG_TYPE_I32) { |
| 870 | uint64_t a = deposit64(al, 32, 32, ah); |
| 871 | uint64_t b = deposit64(bl, 32, 32, bh); |
| 872 | |
| 873 | if (add) { |
| 874 | a += b; |
| 875 | } else { |
| 876 | a -= b; |
| 877 | } |
| 878 | |
| 879 | al = sextract64(a, 0, 32); |
| 880 | ah = sextract64(a, 32, 32); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 881 | } else { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 882 | Int128 a = int128_make128(al, ah); |
| 883 | Int128 b = int128_make128(bl, bh); |
| 884 | |
| 885 | if (add) { |
| 886 | a = int128_add(a, b); |
| 887 | } else { |
| 888 | a = int128_sub(a, b); |
| 889 | } |
| 890 | |
| 891 | al = int128_getlo(a); |
| 892 | ah = int128_gethi(a); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | rl = op->args[0]; |
| 896 | rh = op->args[1]; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 897 | |
| 898 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
| 899 | op2 = tcg_op_insert_before(ctx->tcg, op, 0); |
| 900 | |
| 901 | tcg_opt_gen_movi(ctx, op, rl, al); |
| 902 | tcg_opt_gen_movi(ctx, op2, rh, ah); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 908 | static bool fold_add2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 909 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 910 | /* Note that the high and low parts may be independently swapped. */ |
| 911 | swap_commutative(op->args[0], &op->args[2], &op->args[4]); |
| 912 | swap_commutative(op->args[1], &op->args[3], &op->args[5]); |
| 913 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 914 | return fold_addsub2(ctx, op, true); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 917 | static bool fold_and(OptContext *ctx, TCGOp *op) |
| 918 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 919 | uint64_t z1, z2; |
| 920 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 921 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 922 | fold_xi_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 923 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 924 | fold_xx_to_x(ctx, op)) { |
| 925 | return true; |
| 926 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 927 | |
| 928 | z1 = arg_info(op->args[1])->z_mask; |
| 929 | z2 = arg_info(op->args[2])->z_mask; |
| 930 | ctx->z_mask = z1 & z2; |
| 931 | |
| 932 | /* |
| 933 | * Known-zeros does not imply known-ones. Therefore unless |
| 934 | * arg2 is constant, we can't infer affected bits from it. |
| 935 | */ |
| 936 | if (arg_is_const(op->args[2])) { |
| 937 | ctx->a_mask = z1 & ~z2; |
| 938 | } |
| 939 | |
| 940 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static bool fold_andc(OptContext *ctx, TCGOp *op) |
| 944 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 945 | uint64_t z1; |
| 946 | |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 947 | if (fold_const2(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 948 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 949 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 950 | fold_ix_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 951 | return true; |
| 952 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 953 | |
| 954 | z1 = arg_info(op->args[1])->z_mask; |
| 955 | |
| 956 | /* |
| 957 | * Known-zeros does not imply known-ones. Therefore unless |
| 958 | * arg2 is constant, we can't infer anything from it. |
| 959 | */ |
| 960 | if (arg_is_const(op->args[2])) { |
| 961 | uint64_t z2 = ~arg_info(op->args[2])->z_mask; |
| 962 | ctx->a_mask = z1 & ~z2; |
| 963 | z1 &= z2; |
| 964 | } |
| 965 | ctx->z_mask = z1; |
| 966 | |
| 967 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 970 | static bool fold_brcond(OptContext *ctx, TCGOp *op) |
| 971 | { |
| 972 | TCGCond cond = op->args[2]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 973 | int i; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 974 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 975 | if (swap_commutative(NO_DEST, &op->args[0], &op->args[1])) { |
| 976 | op->args[2] = cond = tcg_swap_cond(cond); |
| 977 | } |
| 978 | |
| 979 | i = do_constant_folding_cond(ctx->type, op->args[0], op->args[1], cond); |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 980 | if (i == 0) { |
| 981 | tcg_op_remove(ctx->tcg, op); |
| 982 | return true; |
| 983 | } |
| 984 | if (i > 0) { |
| 985 | op->opc = INDEX_op_br; |
| 986 | op->args[0] = op->args[3]; |
| 987 | } |
| 988 | return false; |
| 989 | } |
| 990 | |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 991 | static bool fold_brcond2(OptContext *ctx, TCGOp *op) |
| 992 | { |
| 993 | TCGCond cond = op->args[4]; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 994 | TCGArg label = op->args[5]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 995 | int i, inv = 0; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 996 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 997 | if (swap_commutative2(&op->args[0], &op->args[2])) { |
| 998 | op->args[4] = cond = tcg_swap_cond(cond); |
| 999 | } |
| 1000 | |
| 1001 | i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond); |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1002 | if (i >= 0) { |
| 1003 | goto do_brcond_const; |
| 1004 | } |
| 1005 | |
| 1006 | switch (cond) { |
| 1007 | case TCG_COND_LT: |
| 1008 | case TCG_COND_GE: |
| 1009 | /* |
| 1010 | * Simplify LT/GE comparisons vs zero to a single compare |
| 1011 | * vs the high word of the input. |
| 1012 | */ |
| 1013 | if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == 0 && |
| 1014 | arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0) { |
| 1015 | goto do_brcond_high; |
| 1016 | } |
| 1017 | break; |
| 1018 | |
| 1019 | case TCG_COND_NE: |
| 1020 | inv = 1; |
| 1021 | QEMU_FALLTHROUGH; |
| 1022 | case TCG_COND_EQ: |
| 1023 | /* |
| 1024 | * Simplify EQ/NE comparisons where one of the pairs |
| 1025 | * can be simplified. |
| 1026 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1027 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1028 | op->args[2], cond); |
| 1029 | switch (i ^ inv) { |
| 1030 | case 0: |
| 1031 | goto do_brcond_const; |
| 1032 | case 1: |
| 1033 | goto do_brcond_high; |
| 1034 | } |
| 1035 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1036 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1037 | op->args[3], cond); |
| 1038 | switch (i ^ inv) { |
| 1039 | case 0: |
| 1040 | goto do_brcond_const; |
| 1041 | case 1: |
| 1042 | op->opc = INDEX_op_brcond_i32; |
| 1043 | op->args[1] = op->args[2]; |
| 1044 | op->args[2] = cond; |
| 1045 | op->args[3] = label; |
| 1046 | break; |
| 1047 | } |
| 1048 | break; |
| 1049 | |
| 1050 | default: |
| 1051 | break; |
| 1052 | |
| 1053 | do_brcond_high: |
| 1054 | op->opc = INDEX_op_brcond_i32; |
| 1055 | op->args[0] = op->args[1]; |
| 1056 | op->args[1] = op->args[3]; |
| 1057 | op->args[2] = cond; |
| 1058 | op->args[3] = label; |
| 1059 | break; |
| 1060 | |
| 1061 | do_brcond_const: |
| 1062 | if (i == 0) { |
| 1063 | tcg_op_remove(ctx->tcg, op); |
| 1064 | return true; |
| 1065 | } |
| 1066 | op->opc = INDEX_op_br; |
| 1067 | op->args[0] = label; |
| 1068 | break; |
| 1069 | } |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1073 | static bool fold_bswap(OptContext *ctx, TCGOp *op) |
| 1074 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1075 | uint64_t z_mask, sign; |
| 1076 | |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1077 | if (arg_is_const(op->args[1])) { |
| 1078 | uint64_t t = arg_info(op->args[1])->val; |
| 1079 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1080 | t = do_constant_folding(op->opc, ctx->type, t, op->args[2]); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1081 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1082 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1083 | |
| 1084 | z_mask = arg_info(op->args[1])->z_mask; |
| 1085 | switch (op->opc) { |
| 1086 | case INDEX_op_bswap16_i32: |
| 1087 | case INDEX_op_bswap16_i64: |
| 1088 | z_mask = bswap16(z_mask); |
| 1089 | sign = INT16_MIN; |
| 1090 | break; |
| 1091 | case INDEX_op_bswap32_i32: |
| 1092 | case INDEX_op_bswap32_i64: |
| 1093 | z_mask = bswap32(z_mask); |
| 1094 | sign = INT32_MIN; |
| 1095 | break; |
| 1096 | case INDEX_op_bswap64_i64: |
| 1097 | z_mask = bswap64(z_mask); |
| 1098 | sign = INT64_MIN; |
| 1099 | break; |
| 1100 | default: |
| 1101 | g_assert_not_reached(); |
| 1102 | } |
| 1103 | |
| 1104 | switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) { |
| 1105 | case TCG_BSWAP_OZ: |
| 1106 | break; |
| 1107 | case TCG_BSWAP_OS: |
| 1108 | /* If the sign bit may be 1, force all the bits above to 1. */ |
| 1109 | if (z_mask & sign) { |
| 1110 | z_mask |= sign; |
| 1111 | } |
| 1112 | break; |
| 1113 | default: |
| 1114 | /* The high bits are undefined: force all bits above the sign to 1. */ |
| 1115 | z_mask |= sign << 1; |
| 1116 | break; |
| 1117 | } |
| 1118 | ctx->z_mask = z_mask; |
| 1119 | |
| 1120 | return fold_masks(ctx, op); |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1123 | static bool fold_call(OptContext *ctx, TCGOp *op) |
| 1124 | { |
| 1125 | TCGContext *s = ctx->tcg; |
| 1126 | int nb_oargs = TCGOP_CALLO(op); |
| 1127 | int nb_iargs = TCGOP_CALLI(op); |
| 1128 | int flags, i; |
| 1129 | |
| 1130 | init_arguments(ctx, op, nb_oargs + nb_iargs); |
| 1131 | copy_propagate(ctx, op, nb_oargs, nb_iargs); |
| 1132 | |
| 1133 | /* If the function reads or writes globals, reset temp data. */ |
| 1134 | flags = tcg_call_flags(op); |
| 1135 | if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { |
| 1136 | int nb_globals = s->nb_globals; |
| 1137 | |
| 1138 | for (i = 0; i < nb_globals; i++) { |
| 1139 | if (test_bit(i, ctx->temps_used.l)) { |
| 1140 | reset_ts(&ctx->tcg->temps[i]); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | /* Reset temp data for outputs. */ |
| 1146 | for (i = 0; i < nb_oargs; i++) { |
| 1147 | reset_temp(op->args[i]); |
| 1148 | } |
| 1149 | |
| 1150 | /* Stop optimizing MB across calls. */ |
| 1151 | ctx->prev_mb = NULL; |
| 1152 | return true; |
| 1153 | } |
| 1154 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1155 | static bool fold_count_zeros(OptContext *ctx, TCGOp *op) |
| 1156 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1157 | uint64_t z_mask; |
| 1158 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1159 | if (arg_is_const(op->args[1])) { |
| 1160 | uint64_t t = arg_info(op->args[1])->val; |
| 1161 | |
| 1162 | if (t != 0) { |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1163 | t = do_constant_folding(op->opc, ctx->type, t, 0); |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1164 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1165 | } |
| 1166 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); |
| 1167 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1168 | |
| 1169 | switch (ctx->type) { |
| 1170 | case TCG_TYPE_I32: |
| 1171 | z_mask = 31; |
| 1172 | break; |
| 1173 | case TCG_TYPE_I64: |
| 1174 | z_mask = 63; |
| 1175 | break; |
| 1176 | default: |
| 1177 | g_assert_not_reached(); |
| 1178 | } |
| 1179 | ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask; |
| 1180 | |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1181 | return false; |
| 1182 | } |
| 1183 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1184 | static bool fold_ctpop(OptContext *ctx, TCGOp *op) |
| 1185 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1186 | if (fold_const1(ctx, op)) { |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
| 1190 | switch (ctx->type) { |
| 1191 | case TCG_TYPE_I32: |
| 1192 | ctx->z_mask = 32 | 31; |
| 1193 | break; |
| 1194 | case TCG_TYPE_I64: |
| 1195 | ctx->z_mask = 64 | 63; |
| 1196 | break; |
| 1197 | default: |
| 1198 | g_assert_not_reached(); |
| 1199 | } |
| 1200 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1203 | static bool fold_deposit(OptContext *ctx, TCGOp *op) |
| 1204 | { |
| 1205 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1206 | uint64_t t1 = arg_info(op->args[1])->val; |
| 1207 | uint64_t t2 = arg_info(op->args[2])->val; |
| 1208 | |
| 1209 | t1 = deposit64(t1, op->args[3], op->args[4], t2); |
| 1210 | return tcg_opt_gen_movi(ctx, op, op->args[0], t1); |
| 1211 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1212 | |
| 1213 | ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask, |
| 1214 | op->args[3], op->args[4], |
| 1215 | arg_info(op->args[2])->z_mask); |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1216 | return false; |
| 1217 | } |
| 1218 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1219 | static bool fold_divide(OptContext *ctx, TCGOp *op) |
| 1220 | { |
Richard Henderson | 2f9d9a3 | 2021-10-25 11:30:14 -0700 | [diff] [blame] | 1221 | if (fold_const2(ctx, op) || |
| 1222 | fold_xi_to_x(ctx, op, 1)) { |
| 1223 | return true; |
| 1224 | } |
| 1225 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1228 | static bool fold_dup(OptContext *ctx, TCGOp *op) |
| 1229 | { |
| 1230 | if (arg_is_const(op->args[1])) { |
| 1231 | uint64_t t = arg_info(op->args[1])->val; |
| 1232 | t = dup_const(TCGOP_VECE(op), t); |
| 1233 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1234 | } |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | static bool fold_dup2(OptContext *ctx, TCGOp *op) |
| 1239 | { |
| 1240 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1241 | uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32, |
| 1242 | arg_info(op->args[2])->val); |
| 1243 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1244 | } |
| 1245 | |
| 1246 | if (args_are_copies(op->args[1], op->args[2])) { |
| 1247 | op->opc = INDEX_op_dup_vec; |
| 1248 | TCGOP_VECE(op) = MO_32; |
| 1249 | } |
| 1250 | return false; |
| 1251 | } |
| 1252 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1253 | static bool fold_eqv(OptContext *ctx, TCGOp *op) |
| 1254 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1255 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1256 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1257 | fold_xi_to_not(ctx, op, 0)) { |
| 1258 | return true; |
| 1259 | } |
| 1260 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1263 | static bool fold_extract(OptContext *ctx, TCGOp *op) |
| 1264 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1265 | uint64_t z_mask_old, z_mask; |
| 1266 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1267 | if (arg_is_const(op->args[1])) { |
| 1268 | uint64_t t; |
| 1269 | |
| 1270 | t = arg_info(op->args[1])->val; |
| 1271 | t = extract64(t, op->args[2], op->args[3]); |
| 1272 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1273 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1274 | |
| 1275 | z_mask_old = arg_info(op->args[1])->z_mask; |
| 1276 | z_mask = extract64(z_mask_old, op->args[2], op->args[3]); |
| 1277 | if (op->args[2] == 0) { |
| 1278 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1279 | } |
| 1280 | ctx->z_mask = z_mask; |
| 1281 | |
| 1282 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1285 | static bool fold_extract2(OptContext *ctx, TCGOp *op) |
| 1286 | { |
| 1287 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1288 | uint64_t v1 = arg_info(op->args[1])->val; |
| 1289 | uint64_t v2 = arg_info(op->args[2])->val; |
| 1290 | int shr = op->args[3]; |
| 1291 | |
| 1292 | if (op->opc == INDEX_op_extract2_i64) { |
| 1293 | v1 >>= shr; |
| 1294 | v2 <<= 64 - shr; |
| 1295 | } else { |
| 1296 | v1 = (uint32_t)v1 >> shr; |
| 1297 | v2 = (int32_t)v2 << (32 - shr); |
| 1298 | } |
| 1299 | return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); |
| 1300 | } |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1304 | static bool fold_exts(OptContext *ctx, TCGOp *op) |
| 1305 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1306 | uint64_t z_mask_old, z_mask, sign; |
| 1307 | bool type_change = false; |
| 1308 | |
| 1309 | if (fold_const1(ctx, op)) { |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
| 1313 | z_mask_old = z_mask = arg_info(op->args[1])->z_mask; |
| 1314 | |
| 1315 | switch (op->opc) { |
| 1316 | CASE_OP_32_64(ext8s): |
| 1317 | sign = INT8_MIN; |
| 1318 | z_mask = (uint8_t)z_mask; |
| 1319 | break; |
| 1320 | CASE_OP_32_64(ext16s): |
| 1321 | sign = INT16_MIN; |
| 1322 | z_mask = (uint16_t)z_mask; |
| 1323 | break; |
| 1324 | case INDEX_op_ext_i32_i64: |
| 1325 | type_change = true; |
| 1326 | QEMU_FALLTHROUGH; |
| 1327 | case INDEX_op_ext32s_i64: |
| 1328 | sign = INT32_MIN; |
| 1329 | z_mask = (uint32_t)z_mask; |
| 1330 | break; |
| 1331 | default: |
| 1332 | g_assert_not_reached(); |
| 1333 | } |
| 1334 | |
| 1335 | if (z_mask & sign) { |
| 1336 | z_mask |= sign; |
| 1337 | } else if (!type_change) { |
| 1338 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1339 | } |
| 1340 | ctx->z_mask = z_mask; |
| 1341 | |
| 1342 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static bool fold_extu(OptContext *ctx, TCGOp *op) |
| 1346 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1347 | uint64_t z_mask_old, z_mask; |
| 1348 | bool type_change = false; |
| 1349 | |
| 1350 | if (fold_const1(ctx, op)) { |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | z_mask_old = z_mask = arg_info(op->args[1])->z_mask; |
| 1355 | |
| 1356 | switch (op->opc) { |
| 1357 | CASE_OP_32_64(ext8u): |
| 1358 | z_mask = (uint8_t)z_mask; |
| 1359 | break; |
| 1360 | CASE_OP_32_64(ext16u): |
| 1361 | z_mask = (uint16_t)z_mask; |
| 1362 | break; |
| 1363 | case INDEX_op_extrl_i64_i32: |
| 1364 | case INDEX_op_extu_i32_i64: |
| 1365 | type_change = true; |
| 1366 | QEMU_FALLTHROUGH; |
| 1367 | case INDEX_op_ext32u_i64: |
| 1368 | z_mask = (uint32_t)z_mask; |
| 1369 | break; |
| 1370 | case INDEX_op_extrh_i64_i32: |
| 1371 | type_change = true; |
| 1372 | z_mask >>= 32; |
| 1373 | break; |
| 1374 | default: |
| 1375 | g_assert_not_reached(); |
| 1376 | } |
| 1377 | |
| 1378 | ctx->z_mask = z_mask; |
| 1379 | if (!type_change) { |
| 1380 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1381 | } |
| 1382 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1385 | static bool fold_mb(OptContext *ctx, TCGOp *op) |
| 1386 | { |
| 1387 | /* Eliminate duplicate and redundant fence instructions. */ |
| 1388 | if (ctx->prev_mb) { |
| 1389 | /* |
| 1390 | * Merge two barriers of the same type into one, |
| 1391 | * or a weaker barrier into a stronger one, |
| 1392 | * or two weaker barriers into a stronger one. |
| 1393 | * mb X; mb Y => mb X|Y |
| 1394 | * mb; strl => mb; st |
| 1395 | * ldaq; mb => ld; mb |
| 1396 | * ldaq; strl => ld; mb; st |
| 1397 | * Other combinations are also merged into a strong |
| 1398 | * barrier. This is stricter than specified but for |
| 1399 | * the purposes of TCG is better than not optimizing. |
| 1400 | */ |
| 1401 | ctx->prev_mb->args[0] |= op->args[0]; |
| 1402 | tcg_op_remove(ctx->tcg, op); |
| 1403 | } else { |
| 1404 | ctx->prev_mb = op; |
| 1405 | } |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1409 | static bool fold_mov(OptContext *ctx, TCGOp *op) |
| 1410 | { |
| 1411 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); |
| 1412 | } |
| 1413 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1414 | static bool fold_movcond(OptContext *ctx, TCGOp *op) |
| 1415 | { |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1416 | TCGCond cond = op->args[5]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1417 | int i; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1418 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1419 | if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { |
| 1420 | op->args[5] = cond = tcg_swap_cond(cond); |
| 1421 | } |
| 1422 | /* |
| 1423 | * Canonicalize the "false" input reg to match the destination reg so |
| 1424 | * that the tcg backend can implement a "move if true" operation. |
| 1425 | */ |
| 1426 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
| 1427 | op->args[5] = cond = tcg_invert_cond(cond); |
| 1428 | } |
| 1429 | |
| 1430 | i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond); |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1431 | if (i >= 0) { |
| 1432 | return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); |
| 1433 | } |
| 1434 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1435 | ctx->z_mask = arg_info(op->args[3])->z_mask |
| 1436 | | arg_info(op->args[4])->z_mask; |
| 1437 | |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1438 | if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) { |
| 1439 | uint64_t tv = arg_info(op->args[3])->val; |
| 1440 | uint64_t fv = arg_info(op->args[4])->val; |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1441 | TCGOpcode opc; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1442 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1443 | switch (ctx->type) { |
| 1444 | case TCG_TYPE_I32: |
| 1445 | opc = INDEX_op_setcond_i32; |
| 1446 | break; |
| 1447 | case TCG_TYPE_I64: |
| 1448 | opc = INDEX_op_setcond_i64; |
| 1449 | break; |
| 1450 | default: |
| 1451 | g_assert_not_reached(); |
| 1452 | } |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1453 | |
| 1454 | if (tv == 1 && fv == 0) { |
| 1455 | op->opc = opc; |
| 1456 | op->args[3] = cond; |
| 1457 | } else if (fv == 1 && tv == 0) { |
| 1458 | op->opc = opc; |
| 1459 | op->args[3] = tcg_invert_cond(cond); |
| 1460 | } |
| 1461 | } |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1465 | static bool fold_mul(OptContext *ctx, TCGOp *op) |
| 1466 | { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1467 | if (fold_const2(ctx, op) || |
Richard Henderson | 5b5cf47 | 2021-10-25 11:19:14 -0700 | [diff] [blame] | 1468 | fold_xi_to_i(ctx, op, 0) || |
| 1469 | fold_xi_to_x(ctx, op, 1)) { |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) |
| 1476 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1477 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | e867995 | 2021-08-25 13:19:52 -0700 | [diff] [blame] | 1478 | fold_xi_to_i(ctx, op, 0)) { |
| 1479 | return true; |
| 1480 | } |
| 1481 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1484 | static bool fold_multiply2(OptContext *ctx, TCGOp *op) |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1485 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1486 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
| 1487 | |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1488 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1489 | uint64_t a = arg_info(op->args[2])->val; |
| 1490 | uint64_t b = arg_info(op->args[3])->val; |
| 1491 | uint64_t h, l; |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1492 | TCGArg rl, rh; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1493 | TCGOp *op2; |
| 1494 | |
| 1495 | switch (op->opc) { |
| 1496 | case INDEX_op_mulu2_i32: |
| 1497 | l = (uint64_t)(uint32_t)a * (uint32_t)b; |
| 1498 | h = (int32_t)(l >> 32); |
| 1499 | l = (int32_t)l; |
| 1500 | break; |
| 1501 | case INDEX_op_muls2_i32: |
| 1502 | l = (int64_t)(int32_t)a * (int32_t)b; |
| 1503 | h = l >> 32; |
| 1504 | l = (int32_t)l; |
| 1505 | break; |
| 1506 | case INDEX_op_mulu2_i64: |
| 1507 | mulu64(&l, &h, a, b); |
| 1508 | break; |
| 1509 | case INDEX_op_muls2_i64: |
| 1510 | muls64(&l, &h, a, b); |
| 1511 | break; |
| 1512 | default: |
| 1513 | g_assert_not_reached(); |
| 1514 | } |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1515 | |
| 1516 | rl = op->args[0]; |
| 1517 | rh = op->args[1]; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1518 | |
| 1519 | /* The proper opcode is supplied by tcg_opt_gen_mov. */ |
| 1520 | op2 = tcg_op_insert_before(ctx->tcg, op, 0); |
| 1521 | |
| 1522 | tcg_opt_gen_movi(ctx, op, rl, l); |
| 1523 | tcg_opt_gen_movi(ctx, op2, rh, h); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1524 | return true; |
| 1525 | } |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1529 | static bool fold_nand(OptContext *ctx, TCGOp *op) |
| 1530 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1531 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1532 | fold_xi_to_not(ctx, op, -1)) { |
| 1533 | return true; |
| 1534 | } |
| 1535 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static bool fold_neg(OptContext *ctx, TCGOp *op) |
| 1539 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1540 | uint64_t z_mask; |
| 1541 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1542 | if (fold_const1(ctx, op)) { |
| 1543 | return true; |
| 1544 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* Set to 1 all bits to the left of the rightmost. */ |
| 1547 | z_mask = arg_info(op->args[1])->z_mask; |
| 1548 | ctx->z_mask = -(z_mask & -z_mask); |
| 1549 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1550 | /* |
| 1551 | * Because of fold_sub_to_neg, we want to always return true, |
| 1552 | * via finish_folding. |
| 1553 | */ |
| 1554 | finish_folding(ctx, op); |
| 1555 | return true; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | static bool fold_nor(OptContext *ctx, TCGOp *op) |
| 1559 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1560 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1561 | fold_xi_to_not(ctx, op, 0)) { |
| 1562 | return true; |
| 1563 | } |
| 1564 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static bool fold_not(OptContext *ctx, TCGOp *op) |
| 1568 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1569 | if (fold_const1(ctx, op)) { |
| 1570 | return true; |
| 1571 | } |
| 1572 | |
| 1573 | /* Because of fold_to_not, we want to always return true, via finish. */ |
| 1574 | finish_folding(ctx, op); |
| 1575 | return true; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static bool fold_or(OptContext *ctx, TCGOp *op) |
| 1579 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1580 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1581 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | ca7bb04 | 2021-08-25 13:14:21 -0700 | [diff] [blame] | 1582 | fold_xx_to_x(ctx, op)) { |
| 1583 | return true; |
| 1584 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1585 | |
| 1586 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 1587 | | arg_info(op->args[2])->z_mask; |
| 1588 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static bool fold_orc(OptContext *ctx, TCGOp *op) |
| 1592 | { |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1593 | if (fold_const2(ctx, op) || |
Richard Henderson | 4e858d9 | 2021-08-26 07:31:13 -0700 | [diff] [blame] | 1594 | fold_xx_to_i(ctx, op, -1) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1595 | fold_xi_to_x(ctx, op, -1) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1596 | fold_ix_to_not(ctx, op, 0)) { |
| 1597 | return true; |
| 1598 | } |
| 1599 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1602 | static bool fold_qemu_ld(OptContext *ctx, TCGOp *op) |
| 1603 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1604 | const TCGOpDef *def = &tcg_op_defs[op->opc]; |
| 1605 | MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs]; |
| 1606 | MemOp mop = get_memop(oi); |
| 1607 | int width = 8 * memop_size(mop); |
| 1608 | |
| 1609 | if (!(mop & MO_SIGN) && width < 64) { |
| 1610 | ctx->z_mask = MAKE_64BIT_MASK(0, width); |
| 1611 | } |
| 1612 | |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1613 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 1614 | ctx->prev_mb = NULL; |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
| 1618 | static bool fold_qemu_st(OptContext *ctx, TCGOp *op) |
| 1619 | { |
| 1620 | /* Opcodes that touch guest memory stop the mb optimization. */ |
| 1621 | ctx->prev_mb = NULL; |
| 1622 | return false; |
| 1623 | } |
| 1624 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1625 | static bool fold_remainder(OptContext *ctx, TCGOp *op) |
| 1626 | { |
Richard Henderson | 267c17e | 2021-10-25 11:30:33 -0700 | [diff] [blame^] | 1627 | if (fold_const2(ctx, op) || |
| 1628 | fold_xx_to_i(ctx, op, 0)) { |
| 1629 | return true; |
| 1630 | } |
| 1631 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1634 | static bool fold_setcond(OptContext *ctx, TCGOp *op) |
| 1635 | { |
| 1636 | TCGCond cond = op->args[3]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1637 | int i; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1638 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1639 | if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) { |
| 1640 | op->args[3] = cond = tcg_swap_cond(cond); |
| 1641 | } |
| 1642 | |
| 1643 | i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond); |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1644 | if (i >= 0) { |
| 1645 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1646 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1647 | |
| 1648 | ctx->z_mask = 1; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 1649 | return false; |
| 1650 | } |
| 1651 | |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1652 | static bool fold_setcond2(OptContext *ctx, TCGOp *op) |
| 1653 | { |
| 1654 | TCGCond cond = op->args[5]; |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1655 | int i, inv = 0; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1656 | |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1657 | if (swap_commutative2(&op->args[1], &op->args[3])) { |
| 1658 | op->args[5] = cond = tcg_swap_cond(cond); |
| 1659 | } |
| 1660 | |
| 1661 | i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond); |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1662 | if (i >= 0) { |
| 1663 | goto do_setcond_const; |
| 1664 | } |
| 1665 | |
| 1666 | switch (cond) { |
| 1667 | case TCG_COND_LT: |
| 1668 | case TCG_COND_GE: |
| 1669 | /* |
| 1670 | * Simplify LT/GE comparisons vs zero to a single compare |
| 1671 | * vs the high word of the input. |
| 1672 | */ |
| 1673 | if (arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0 && |
| 1674 | arg_is_const(op->args[4]) && arg_info(op->args[4])->val == 0) { |
| 1675 | goto do_setcond_high; |
| 1676 | } |
| 1677 | break; |
| 1678 | |
| 1679 | case TCG_COND_NE: |
| 1680 | inv = 1; |
| 1681 | QEMU_FALLTHROUGH; |
| 1682 | case TCG_COND_EQ: |
| 1683 | /* |
| 1684 | * Simplify EQ/NE comparisons where one of the pairs |
| 1685 | * can be simplified. |
| 1686 | */ |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1687 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1688 | op->args[3], cond); |
| 1689 | switch (i ^ inv) { |
| 1690 | case 0: |
| 1691 | goto do_setcond_const; |
| 1692 | case 1: |
| 1693 | goto do_setcond_high; |
| 1694 | } |
| 1695 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1696 | i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1697 | op->args[4], cond); |
| 1698 | switch (i ^ inv) { |
| 1699 | case 0: |
| 1700 | goto do_setcond_const; |
| 1701 | case 1: |
| 1702 | op->args[2] = op->args[3]; |
| 1703 | op->args[3] = cond; |
| 1704 | op->opc = INDEX_op_setcond_i32; |
| 1705 | break; |
| 1706 | } |
| 1707 | break; |
| 1708 | |
| 1709 | default: |
| 1710 | break; |
| 1711 | |
| 1712 | do_setcond_high: |
| 1713 | op->args[1] = op->args[2]; |
| 1714 | op->args[2] = op->args[4]; |
| 1715 | op->args[3] = cond; |
| 1716 | op->opc = INDEX_op_setcond_i32; |
| 1717 | break; |
| 1718 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1719 | |
| 1720 | ctx->z_mask = 1; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 1721 | return false; |
| 1722 | |
| 1723 | do_setcond_const: |
| 1724 | return tcg_opt_gen_movi(ctx, op, op->args[0], i); |
| 1725 | } |
| 1726 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1727 | static bool fold_sextract(OptContext *ctx, TCGOp *op) |
| 1728 | { |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1729 | int64_t z_mask_old, z_mask; |
| 1730 | |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1731 | if (arg_is_const(op->args[1])) { |
| 1732 | uint64_t t; |
| 1733 | |
| 1734 | t = arg_info(op->args[1])->val; |
| 1735 | t = sextract64(t, op->args[2], op->args[3]); |
| 1736 | return tcg_opt_gen_movi(ctx, op, op->args[0], t); |
| 1737 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1738 | |
| 1739 | z_mask_old = arg_info(op->args[1])->z_mask; |
| 1740 | z_mask = sextract64(z_mask_old, op->args[2], op->args[3]); |
| 1741 | if (op->args[2] == 0 && z_mask >= 0) { |
| 1742 | ctx->a_mask = z_mask_old ^ z_mask; |
| 1743 | } |
| 1744 | ctx->z_mask = z_mask; |
| 1745 | |
| 1746 | return fold_masks(ctx, op); |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1749 | static bool fold_shift(OptContext *ctx, TCGOp *op) |
| 1750 | { |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1751 | if (fold_const2(ctx, op) || |
Richard Henderson | da48e27 | 2021-08-25 20:42:04 -0700 | [diff] [blame] | 1752 | fold_ix_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1753 | fold_xi_to_x(ctx, op, 0)) { |
| 1754 | return true; |
| 1755 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1756 | |
| 1757 | if (arg_is_const(op->args[2])) { |
| 1758 | ctx->z_mask = do_constant_folding(op->opc, ctx->type, |
| 1759 | arg_info(op->args[1])->z_mask, |
| 1760 | arg_info(op->args[2])->val); |
| 1761 | return fold_masks(ctx, op); |
| 1762 | } |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1763 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1766 | static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op) |
| 1767 | { |
| 1768 | TCGOpcode neg_op; |
| 1769 | bool have_neg; |
| 1770 | |
| 1771 | if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) { |
| 1772 | return false; |
| 1773 | } |
| 1774 | |
| 1775 | switch (ctx->type) { |
| 1776 | case TCG_TYPE_I32: |
| 1777 | neg_op = INDEX_op_neg_i32; |
| 1778 | have_neg = TCG_TARGET_HAS_neg_i32; |
| 1779 | break; |
| 1780 | case TCG_TYPE_I64: |
| 1781 | neg_op = INDEX_op_neg_i64; |
| 1782 | have_neg = TCG_TARGET_HAS_neg_i64; |
| 1783 | break; |
| 1784 | case TCG_TYPE_V64: |
| 1785 | case TCG_TYPE_V128: |
| 1786 | case TCG_TYPE_V256: |
| 1787 | neg_op = INDEX_op_neg_vec; |
| 1788 | have_neg = (TCG_TARGET_HAS_neg_vec && |
| 1789 | tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0); |
| 1790 | break; |
| 1791 | default: |
| 1792 | g_assert_not_reached(); |
| 1793 | } |
| 1794 | if (have_neg) { |
| 1795 | op->opc = neg_op; |
| 1796 | op->args[1] = op->args[2]; |
| 1797 | return fold_neg(ctx, op); |
| 1798 | } |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1802 | static bool fold_sub(OptContext *ctx, TCGOp *op) |
| 1803 | { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1804 | if (fold_const2(ctx, op) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1805 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1806 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 9caca88 | 2021-08-24 13:30:32 -0700 | [diff] [blame] | 1807 | fold_sub_to_neg(ctx, op)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1808 | return true; |
| 1809 | } |
| 1810 | return false; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1813 | static bool fold_sub2(OptContext *ctx, TCGOp *op) |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1814 | { |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1815 | return fold_addsub2(ctx, op, false); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1818 | static bool fold_tcg_ld(OptContext *ctx, TCGOp *op) |
| 1819 | { |
| 1820 | /* We can't do any folding with a load, but we can record bits. */ |
| 1821 | switch (op->opc) { |
| 1822 | CASE_OP_32_64(ld8u): |
| 1823 | ctx->z_mask = MAKE_64BIT_MASK(0, 8); |
| 1824 | break; |
| 1825 | CASE_OP_32_64(ld16u): |
| 1826 | ctx->z_mask = MAKE_64BIT_MASK(0, 16); |
| 1827 | break; |
| 1828 | case INDEX_op_ld32u_i64: |
| 1829 | ctx->z_mask = MAKE_64BIT_MASK(0, 32); |
| 1830 | break; |
| 1831 | default: |
| 1832 | g_assert_not_reached(); |
| 1833 | } |
| 1834 | return false; |
| 1835 | } |
| 1836 | |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1837 | static bool fold_xor(OptContext *ctx, TCGOp *op) |
| 1838 | { |
Richard Henderson | 7a2f708 | 2021-08-26 07:06:39 -0700 | [diff] [blame] | 1839 | if (fold_const2_commutative(ctx, op) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1840 | fold_xx_to_i(ctx, op, 0) || |
Richard Henderson | a63ce0e | 2021-08-25 20:28:53 -0700 | [diff] [blame] | 1841 | fold_xi_to_x(ctx, op, 0) || |
Richard Henderson | 0e0a32b | 2021-08-24 13:18:01 -0700 | [diff] [blame] | 1842 | fold_xi_to_not(ctx, op, -1)) { |
Richard Henderson | cbe42fb | 2021-08-25 13:02:00 -0700 | [diff] [blame] | 1843 | return true; |
| 1844 | } |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1845 | |
| 1846 | ctx->z_mask = arg_info(op->args[1])->z_mask |
| 1847 | | arg_info(op->args[2])->z_mask; |
| 1848 | return fold_masks(ctx, op); |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1851 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 1852 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1853 | { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1854 | int nb_temps, i; |
Richard Henderson | d0ed515 | 2021-08-24 07:38:39 -0700 | [diff] [blame] | 1855 | TCGOp *op, *op_next; |
Richard Henderson | dc84988 | 2021-08-24 07:13:45 -0700 | [diff] [blame] | 1856 | OptContext ctx = { .tcg = s }; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 1857 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1858 | /* Array VALS has an element for each temp. |
| 1859 | 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] | 1860 | If this temp is a copy of other ones then the other copies are |
| 1861 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1862 | |
| 1863 | nb_temps = s->nb_temps; |
Richard Henderson | 8f17a97 | 2020-03-30 19:52:02 -0700 | [diff] [blame] | 1864 | for (i = 0; i < nb_temps; ++i) { |
| 1865 | s->temps[i].state_ptr = NULL; |
| 1866 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1867 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 1868 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1869 | TCGOpcode opc = op->opc; |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1870 | const TCGOpDef *def; |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 1871 | bool done = false; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1872 | |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1873 | /* Calls are special. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1874 | if (opc == INDEX_op_call) { |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1875 | fold_call(&ctx, op); |
| 1876 | continue; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 1877 | } |
Richard Henderson | 5cf32be | 2021-08-24 08:17:08 -0700 | [diff] [blame] | 1878 | |
| 1879 | def = &tcg_op_defs[opc]; |
Richard Henderson | ec5d4cb | 2021-08-24 08:20:27 -0700 | [diff] [blame] | 1880 | init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); |
| 1881 | copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1882 | |
Richard Henderson | 67f84c9 | 2021-08-25 08:00:20 -0700 | [diff] [blame] | 1883 | /* Pre-compute the type of the operation. */ |
| 1884 | if (def->flags & TCG_OPF_VECTOR) { |
| 1885 | ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op); |
| 1886 | } else if (def->flags & TCG_OPF_64BIT) { |
| 1887 | ctx.type = TCG_TYPE_I64; |
| 1888 | } else { |
| 1889 | ctx.type = TCG_TYPE_I32; |
| 1890 | } |
| 1891 | |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1892 | /* Assume all bits affected, and no bits known zero. */ |
| 1893 | ctx.a_mask = -1; |
| 1894 | ctx.z_mask = -1; |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 1895 | |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1896 | /* |
| 1897 | * Process each opcode. |
| 1898 | * Sorted alphabetically by opcode as much as possible. |
| 1899 | */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1900 | switch (opc) { |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1901 | CASE_OP_32_64_VEC(add): |
| 1902 | done = fold_add(&ctx, op); |
| 1903 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 1904 | CASE_OP_32_64(add2): |
| 1905 | done = fold_add2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 1906 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1907 | CASE_OP_32_64_VEC(and): |
| 1908 | done = fold_and(&ctx, op); |
| 1909 | break; |
| 1910 | CASE_OP_32_64_VEC(andc): |
| 1911 | done = fold_andc(&ctx, op); |
| 1912 | break; |
Richard Henderson | 079b080 | 2021-08-24 09:30:59 -0700 | [diff] [blame] | 1913 | CASE_OP_32_64(brcond): |
| 1914 | done = fold_brcond(&ctx, op); |
| 1915 | break; |
Richard Henderson | 764d2ab | 2021-08-24 09:22:11 -0700 | [diff] [blame] | 1916 | case INDEX_op_brcond2_i32: |
| 1917 | done = fold_brcond2(&ctx, op); |
| 1918 | break; |
Richard Henderson | 09bacdc | 2021-08-24 11:58:12 -0700 | [diff] [blame] | 1919 | CASE_OP_32_64(bswap16): |
| 1920 | CASE_OP_32_64(bswap32): |
| 1921 | case INDEX_op_bswap64_i64: |
| 1922 | done = fold_bswap(&ctx, op); |
| 1923 | break; |
Richard Henderson | 30dd0bf | 2021-08-24 10:51:34 -0700 | [diff] [blame] | 1924 | CASE_OP_32_64(clz): |
| 1925 | CASE_OP_32_64(ctz): |
| 1926 | done = fold_count_zeros(&ctx, op); |
| 1927 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1928 | CASE_OP_32_64(ctpop): |
| 1929 | done = fold_ctpop(&ctx, op); |
| 1930 | break; |
Richard Henderson | 1b1907b | 2021-08-24 10:47:04 -0700 | [diff] [blame] | 1931 | CASE_OP_32_64(deposit): |
| 1932 | done = fold_deposit(&ctx, op); |
| 1933 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1934 | CASE_OP_32_64(div): |
| 1935 | CASE_OP_32_64(divu): |
| 1936 | done = fold_divide(&ctx, op); |
| 1937 | break; |
Richard Henderson | 8cdb3fc | 2021-08-24 12:06:33 -0700 | [diff] [blame] | 1938 | case INDEX_op_dup_vec: |
| 1939 | done = fold_dup(&ctx, op); |
| 1940 | break; |
| 1941 | case INDEX_op_dup2_vec: |
| 1942 | done = fold_dup2(&ctx, op); |
| 1943 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1944 | CASE_OP_32_64(eqv): |
| 1945 | done = fold_eqv(&ctx, op); |
| 1946 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 1947 | CASE_OP_32_64(extract): |
| 1948 | done = fold_extract(&ctx, op); |
| 1949 | break; |
Richard Henderson | dcd0899 | 2021-08-24 10:41:39 -0700 | [diff] [blame] | 1950 | CASE_OP_32_64(extract2): |
| 1951 | done = fold_extract2(&ctx, op); |
| 1952 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1953 | CASE_OP_32_64(ext8s): |
| 1954 | CASE_OP_32_64(ext16s): |
| 1955 | case INDEX_op_ext32s_i64: |
| 1956 | case INDEX_op_ext_i32_i64: |
| 1957 | done = fold_exts(&ctx, op); |
| 1958 | break; |
| 1959 | CASE_OP_32_64(ext8u): |
| 1960 | CASE_OP_32_64(ext16u): |
| 1961 | case INDEX_op_ext32u_i64: |
| 1962 | case INDEX_op_extu_i32_i64: |
| 1963 | case INDEX_op_extrl_i64_i32: |
| 1964 | case INDEX_op_extrh_i64_i32: |
| 1965 | done = fold_extu(&ctx, op); |
| 1966 | break; |
Richard Henderson | fae450b | 2021-08-25 22:42:19 -0700 | [diff] [blame] | 1967 | CASE_OP_32_64(ld8u): |
| 1968 | CASE_OP_32_64(ld16u): |
| 1969 | case INDEX_op_ld32u_i64: |
| 1970 | done = fold_tcg_ld(&ctx, op); |
| 1971 | break; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 1972 | case INDEX_op_mb: |
| 1973 | done = fold_mb(&ctx, op); |
| 1974 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 1975 | CASE_OP_32_64_VEC(mov): |
| 1976 | done = fold_mov(&ctx, op); |
| 1977 | break; |
Richard Henderson | 0c310a3 | 2021-08-24 10:37:24 -0700 | [diff] [blame] | 1978 | CASE_OP_32_64(movcond): |
| 1979 | done = fold_movcond(&ctx, op); |
| 1980 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1981 | CASE_OP_32_64(mul): |
| 1982 | done = fold_mul(&ctx, op); |
| 1983 | break; |
| 1984 | CASE_OP_32_64(mulsh): |
| 1985 | CASE_OP_32_64(muluh): |
| 1986 | done = fold_mul_highpart(&ctx, op); |
| 1987 | break; |
Richard Henderson | 407112b | 2021-08-26 06:33:04 -0700 | [diff] [blame] | 1988 | CASE_OP_32_64(muls2): |
| 1989 | CASE_OP_32_64(mulu2): |
| 1990 | done = fold_multiply2(&ctx, op); |
Richard Henderson | 6b8ac0d | 2021-08-24 10:24:12 -0700 | [diff] [blame] | 1991 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 1992 | CASE_OP_32_64(nand): |
| 1993 | done = fold_nand(&ctx, op); |
| 1994 | break; |
| 1995 | CASE_OP_32_64(neg): |
| 1996 | done = fold_neg(&ctx, op); |
| 1997 | break; |
| 1998 | CASE_OP_32_64(nor): |
| 1999 | done = fold_nor(&ctx, op); |
| 2000 | break; |
| 2001 | CASE_OP_32_64_VEC(not): |
| 2002 | done = fold_not(&ctx, op); |
| 2003 | break; |
| 2004 | CASE_OP_32_64_VEC(or): |
| 2005 | done = fold_or(&ctx, op); |
| 2006 | break; |
| 2007 | CASE_OP_32_64_VEC(orc): |
| 2008 | done = fold_orc(&ctx, op); |
| 2009 | break; |
Richard Henderson | 3eefdf2 | 2021-08-25 11:06:43 -0700 | [diff] [blame] | 2010 | case INDEX_op_qemu_ld_i32: |
| 2011 | case INDEX_op_qemu_ld_i64: |
| 2012 | done = fold_qemu_ld(&ctx, op); |
| 2013 | break; |
| 2014 | case INDEX_op_qemu_st_i32: |
| 2015 | case INDEX_op_qemu_st8_i32: |
| 2016 | case INDEX_op_qemu_st_i64: |
| 2017 | done = fold_qemu_st(&ctx, op); |
| 2018 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2019 | CASE_OP_32_64(rem): |
| 2020 | CASE_OP_32_64(remu): |
| 2021 | done = fold_remainder(&ctx, op); |
| 2022 | break; |
| 2023 | CASE_OP_32_64(rotl): |
| 2024 | CASE_OP_32_64(rotr): |
| 2025 | CASE_OP_32_64(sar): |
| 2026 | CASE_OP_32_64(shl): |
| 2027 | CASE_OP_32_64(shr): |
| 2028 | done = fold_shift(&ctx, op); |
| 2029 | break; |
Richard Henderson | c63ff55 | 2021-08-24 09:35:30 -0700 | [diff] [blame] | 2030 | CASE_OP_32_64(setcond): |
| 2031 | done = fold_setcond(&ctx, op); |
| 2032 | break; |
Richard Henderson | bc47b1a | 2021-08-24 09:09:35 -0700 | [diff] [blame] | 2033 | case INDEX_op_setcond2_i32: |
| 2034 | done = fold_setcond2(&ctx, op); |
| 2035 | break; |
Richard Henderson | b6617c8 | 2021-08-24 10:44:53 -0700 | [diff] [blame] | 2036 | CASE_OP_32_64(sextract): |
| 2037 | done = fold_sextract(&ctx, op); |
| 2038 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2039 | CASE_OP_32_64_VEC(sub): |
| 2040 | done = fold_sub(&ctx, op); |
| 2041 | break; |
Richard Henderson | 9531c07 | 2021-08-26 06:51:39 -0700 | [diff] [blame] | 2042 | CASE_OP_32_64(sub2): |
| 2043 | done = fold_sub2(&ctx, op); |
Richard Henderson | e3f7dc2 | 2021-08-24 10:30:38 -0700 | [diff] [blame] | 2044 | break; |
Richard Henderson | 2f9f08b | 2021-08-25 12:03:48 -0700 | [diff] [blame] | 2045 | CASE_OP_32_64_VEC(xor): |
| 2046 | done = fold_xor(&ctx, op); |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 2047 | break; |
Richard Henderson | 2cfac7f | 2021-08-25 13:05:43 -0700 | [diff] [blame] | 2048 | default: |
| 2049 | break; |
Richard Henderson | b10f383 | 2021-08-23 22:30:17 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Richard Henderson | 404a148 | 2021-08-24 11:08:21 -0700 | [diff] [blame] | 2052 | if (!done) { |
| 2053 | finish_folding(&ctx, op); |
| 2054 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2055 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 2056 | } |