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