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" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 27 | #include "qemu-common.h" |
Paolo Bonzini | 00f6da6 | 2016-03-15 13:16:36 +0100 | [diff] [blame] | 28 | #include "exec/cpu-common.h" |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 29 | #include "tcg-op.h" |
| 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 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 40 | struct tcg_temp_info { |
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; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 44 | tcg_target_ulong val; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 45 | tcg_target_ulong mask; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 46 | }; |
| 47 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 48 | static inline struct tcg_temp_info *ts_info(TCGTemp *ts) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 49 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 50 | return ts->state_ptr; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 53 | static inline struct tcg_temp_info *arg_info(TCGArg arg) |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 54 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 55 | return ts_info(arg_temp(arg)); |
| 56 | } |
| 57 | |
| 58 | static inline bool ts_is_const(TCGTemp *ts) |
| 59 | { |
| 60 | return ts_info(ts)->is_const; |
| 61 | } |
| 62 | |
| 63 | static inline bool arg_is_const(TCGArg arg) |
| 64 | { |
| 65 | return ts_is_const(arg_temp(arg)); |
| 66 | } |
| 67 | |
| 68 | static inline bool ts_is_copy(TCGTemp *ts) |
| 69 | { |
| 70 | return ts_info(ts)->next_copy != ts; |
Aurelien Jarno | d9c769c | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Aurelien Jarno | b41059d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 73 | /* 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] | 74 | static void reset_ts(TCGTemp *ts) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 75 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 76 | struct tcg_temp_info *ti = ts_info(ts); |
| 77 | struct tcg_temp_info *pi = ts_info(ti->prev_copy); |
| 78 | struct tcg_temp_info *ni = ts_info(ti->next_copy); |
| 79 | |
| 80 | ni->prev_copy = ti->prev_copy; |
| 81 | pi->next_copy = ti->next_copy; |
| 82 | ti->next_copy = ts; |
| 83 | ti->prev_copy = ts; |
| 84 | ti->is_const = false; |
| 85 | ti->mask = -1; |
| 86 | } |
| 87 | |
| 88 | static void reset_temp(TCGArg arg) |
| 89 | { |
| 90 | reset_ts(arg_temp(arg)); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 91 | } |
| 92 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 93 | /* Initialize and activate a temporary. */ |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 94 | static void init_ts_info(struct tcg_temp_info *infos, |
| 95 | TCGTempSet *temps_used, TCGTemp *ts) |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 96 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 97 | size_t idx = temp_idx(ts); |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 98 | if (!test_bit(idx, temps_used->l)) { |
| 99 | struct tcg_temp_info *ti = &infos[idx]; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 100 | |
| 101 | ts->state_ptr = ti; |
| 102 | ti->next_copy = ts; |
| 103 | ti->prev_copy = ts; |
| 104 | ti->is_const = false; |
| 105 | ti->mask = -1; |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 106 | set_bit(idx, temps_used->l); |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 110 | static void init_arg_info(struct tcg_temp_info *infos, |
| 111 | TCGTempSet *temps_used, TCGArg arg) |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 112 | { |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 113 | init_ts_info(infos, temps_used, arg_temp(arg)); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 116 | static TCGTemp *find_better_copy(TCGContext *s, TCGTemp *ts) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 117 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 118 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 119 | |
| 120 | /* If this is already a global, we can't do better. */ |
Richard Henderson | fa477d2 | 2016-11-02 11:20:15 -0600 | [diff] [blame] | 121 | if (ts->temp_global) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 122 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Search for a global first. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 126 | for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) { |
| 127 | if (i->temp_global) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 128 | return i; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* If it is a temp, search for a temp local. */ |
Richard Henderson | fa477d2 | 2016-11-02 11:20:15 -0600 | [diff] [blame] | 133 | if (!ts->temp_local) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 134 | for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) { |
| 135 | if (ts->temp_local) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 136 | return i; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* Failure to find a better representation, return the same temp. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 142 | return ts; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 145 | static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 146 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 147 | TCGTemp *i; |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 148 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 149 | if (ts1 == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 153 | if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 157 | for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { |
| 158 | if (i == ts2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return false; |
| 164 | } |
| 165 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 166 | static bool args_are_copies(TCGArg arg1, TCGArg arg2) |
| 167 | { |
| 168 | return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); |
| 169 | } |
| 170 | |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 171 | static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg dst, TCGArg val) |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 172 | { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 173 | const TCGOpDef *def; |
| 174 | TCGOpcode new_op; |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 175 | tcg_target_ulong mask; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 176 | struct tcg_temp_info *di = arg_info(dst); |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 177 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 178 | def = &tcg_op_defs[op->opc]; |
| 179 | if (def->flags & TCG_OPF_VECTOR) { |
| 180 | new_op = INDEX_op_dupi_vec; |
| 181 | } else if (def->flags & TCG_OPF_64BIT) { |
| 182 | new_op = INDEX_op_movi_i64; |
| 183 | } else { |
| 184 | new_op = INDEX_op_movi_i32; |
| 185 | } |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 186 | op->opc = new_op; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 187 | /* TCGOP_VECL and TCGOP_VECE remain unchanged. */ |
| 188 | op->args[0] = dst; |
| 189 | op->args[1] = val; |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 190 | |
| 191 | reset_temp(dst); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 192 | di->is_const = true; |
| 193 | di->val = val; |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 194 | mask = val; |
Aurelien Jarno | 9615212 | 2015-07-10 18:03:30 +0200 | [diff] [blame] | 195 | if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_movi_i32) { |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 196 | /* High bits of the destination are now garbage. */ |
| 197 | mask |= ~0xffffffffull; |
| 198 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 199 | di->mask = mask; |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 202 | static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg dst, TCGArg src) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 203 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 204 | TCGTemp *dst_ts = arg_temp(dst); |
| 205 | TCGTemp *src_ts = arg_temp(src); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 206 | const TCGOpDef *def; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 207 | struct tcg_temp_info *di; |
| 208 | struct tcg_temp_info *si; |
| 209 | tcg_target_ulong mask; |
| 210 | TCGOpcode new_op; |
| 211 | |
| 212 | if (ts_are_copies(dst_ts, src_ts)) { |
Aurelien Jarno | 5365718 | 2015-06-04 21:53:25 +0200 | [diff] [blame] | 213 | tcg_op_remove(s, op); |
| 214 | return; |
| 215 | } |
| 216 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 217 | reset_ts(dst_ts); |
| 218 | di = ts_info(dst_ts); |
| 219 | si = ts_info(src_ts); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 220 | def = &tcg_op_defs[op->opc]; |
| 221 | if (def->flags & TCG_OPF_VECTOR) { |
| 222 | new_op = INDEX_op_mov_vec; |
| 223 | } else if (def->flags & TCG_OPF_64BIT) { |
| 224 | new_op = INDEX_op_mov_i64; |
| 225 | } else { |
| 226 | new_op = INDEX_op_mov_i32; |
| 227 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 228 | op->opc = new_op; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 229 | /* TCGOP_VECL and TCGOP_VECE remain unchanged. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 230 | op->args[0] = dst; |
| 231 | op->args[1] = src; |
Richard Henderson | a62f6f5 | 2014-05-22 10:59:12 -0700 | [diff] [blame] | 232 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 233 | mask = si->mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 234 | if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) { |
| 235 | /* High bits of the destination are now garbage. */ |
| 236 | mask |= ~0xffffffffull; |
| 237 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 238 | di->mask = mask; |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 239 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 240 | if (src_ts->type == dst_ts->type) { |
| 241 | struct tcg_temp_info *ni = ts_info(si->next_copy); |
| 242 | |
| 243 | di->next_copy = si->next_copy; |
| 244 | di->prev_copy = src_ts; |
| 245 | ni->prev_copy = dst_ts; |
| 246 | si->next_copy = dst_ts; |
| 247 | di->is_const = si->is_const; |
| 248 | di->val = si->val; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 249 | } |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 250 | } |
| 251 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 252 | static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 253 | { |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 254 | uint64_t l64, h64; |
| 255 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 256 | switch (op) { |
| 257 | CASE_OP_32_64(add): |
| 258 | return x + y; |
| 259 | |
| 260 | CASE_OP_32_64(sub): |
| 261 | return x - y; |
| 262 | |
| 263 | CASE_OP_32_64(mul): |
| 264 | return x * y; |
| 265 | |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 266 | CASE_OP_32_64(and): |
| 267 | return x & y; |
| 268 | |
| 269 | CASE_OP_32_64(or): |
| 270 | return x | y; |
| 271 | |
| 272 | CASE_OP_32_64(xor): |
| 273 | return x ^ y; |
| 274 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 275 | case INDEX_op_shl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 276 | return (uint32_t)x << (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 277 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 278 | case INDEX_op_shl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 279 | return (uint64_t)x << (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 280 | |
| 281 | case INDEX_op_shr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 282 | return (uint32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 283 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 284 | case INDEX_op_shr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 285 | return (uint64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 286 | |
| 287 | case INDEX_op_sar_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 288 | return (int32_t)x >> (y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 289 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 290 | case INDEX_op_sar_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 291 | return (int64_t)x >> (y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 292 | |
| 293 | case INDEX_op_rotr_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 294 | return ror32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 295 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 296 | case INDEX_op_rotr_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 297 | return ror64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 298 | |
| 299 | case INDEX_op_rotl_i32: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 300 | return rol32(x, y & 31); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 301 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 302 | case INDEX_op_rotl_i64: |
Richard Henderson | 50c5c4d | 2014-03-18 07:45:39 -0700 | [diff] [blame] | 303 | return rol64(x, y & 63); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 304 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 305 | CASE_OP_32_64(not): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 306 | return ~x; |
| 307 | |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 308 | CASE_OP_32_64(neg): |
| 309 | return -x; |
| 310 | |
| 311 | CASE_OP_32_64(andc): |
| 312 | return x & ~y; |
| 313 | |
| 314 | CASE_OP_32_64(orc): |
| 315 | return x | ~y; |
| 316 | |
| 317 | CASE_OP_32_64(eqv): |
| 318 | return ~(x ^ y); |
| 319 | |
| 320 | CASE_OP_32_64(nand): |
| 321 | return ~(x & y); |
| 322 | |
| 323 | CASE_OP_32_64(nor): |
| 324 | return ~(x | y); |
| 325 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 326 | case INDEX_op_clz_i32: |
| 327 | return (uint32_t)x ? clz32(x) : y; |
| 328 | |
| 329 | case INDEX_op_clz_i64: |
| 330 | return x ? clz64(x) : y; |
| 331 | |
| 332 | case INDEX_op_ctz_i32: |
| 333 | return (uint32_t)x ? ctz32(x) : y; |
| 334 | |
| 335 | case INDEX_op_ctz_i64: |
| 336 | return x ? ctz64(x) : y; |
| 337 | |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 338 | case INDEX_op_ctpop_i32: |
| 339 | return ctpop32(x); |
| 340 | |
| 341 | case INDEX_op_ctpop_i64: |
| 342 | return ctpop64(x); |
| 343 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 344 | CASE_OP_32_64(ext8s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 345 | return (int8_t)x; |
| 346 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 347 | CASE_OP_32_64(ext16s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 348 | return (int16_t)x; |
| 349 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 350 | CASE_OP_32_64(ext8u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 351 | return (uint8_t)x; |
| 352 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 353 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 354 | return (uint16_t)x; |
| 355 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 356 | case INDEX_op_ext_i32_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 357 | case INDEX_op_ext32s_i64: |
| 358 | return (int32_t)x; |
| 359 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 360 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 361 | case INDEX_op_extrl_i64_i32: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 362 | case INDEX_op_ext32u_i64: |
| 363 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 364 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 365 | case INDEX_op_extrh_i64_i32: |
| 366 | return (uint64_t)x >> 32; |
| 367 | |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 368 | case INDEX_op_muluh_i32: |
| 369 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; |
| 370 | case INDEX_op_mulsh_i32: |
| 371 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; |
| 372 | |
| 373 | case INDEX_op_muluh_i64: |
| 374 | mulu64(&l64, &h64, x, y); |
| 375 | return h64; |
| 376 | case INDEX_op_mulsh_i64: |
| 377 | muls64(&l64, &h64, x, y); |
| 378 | return h64; |
| 379 | |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 380 | case INDEX_op_div_i32: |
| 381 | /* Avoid crashing on divide by zero, otherwise undefined. */ |
| 382 | return (int32_t)x / ((int32_t)y ? : 1); |
| 383 | case INDEX_op_divu_i32: |
| 384 | return (uint32_t)x / ((uint32_t)y ? : 1); |
| 385 | case INDEX_op_div_i64: |
| 386 | return (int64_t)x / ((int64_t)y ? : 1); |
| 387 | case INDEX_op_divu_i64: |
| 388 | return (uint64_t)x / ((uint64_t)y ? : 1); |
| 389 | |
| 390 | case INDEX_op_rem_i32: |
| 391 | return (int32_t)x % ((int32_t)y ? : 1); |
| 392 | case INDEX_op_remu_i32: |
| 393 | return (uint32_t)x % ((uint32_t)y ? : 1); |
| 394 | case INDEX_op_rem_i64: |
| 395 | return (int64_t)x % ((int64_t)y ? : 1); |
| 396 | case INDEX_op_remu_i64: |
| 397 | return (uint64_t)x % ((uint64_t)y ? : 1); |
| 398 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 399 | default: |
| 400 | fprintf(stderr, |
| 401 | "Unrecognized operation %d in do_constant_folding.\n", op); |
| 402 | tcg_abort(); |
| 403 | } |
| 404 | } |
| 405 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 406 | static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 407 | { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 408 | const TCGOpDef *def = &tcg_op_defs[op]; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 409 | TCGArg res = do_constant_folding_2(op, x, y); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 410 | if (!(def->flags & TCG_OPF_64BIT)) { |
Aurelien Jarno | 29f3ff8 | 2015-07-10 18:03:31 +0200 | [diff] [blame] | 411 | res = (int32_t)res; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 412 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 413 | return res; |
| 414 | } |
| 415 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 416 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 417 | { |
| 418 | switch (c) { |
| 419 | case TCG_COND_EQ: |
| 420 | return x == y; |
| 421 | case TCG_COND_NE: |
| 422 | return x != y; |
| 423 | case TCG_COND_LT: |
| 424 | return (int32_t)x < (int32_t)y; |
| 425 | case TCG_COND_GE: |
| 426 | return (int32_t)x >= (int32_t)y; |
| 427 | case TCG_COND_LE: |
| 428 | return (int32_t)x <= (int32_t)y; |
| 429 | case TCG_COND_GT: |
| 430 | return (int32_t)x > (int32_t)y; |
| 431 | case TCG_COND_LTU: |
| 432 | return x < y; |
| 433 | case TCG_COND_GEU: |
| 434 | return x >= y; |
| 435 | case TCG_COND_LEU: |
| 436 | return x <= y; |
| 437 | case TCG_COND_GTU: |
| 438 | return x > y; |
| 439 | default: |
| 440 | tcg_abort(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) |
| 445 | { |
| 446 | switch (c) { |
| 447 | case TCG_COND_EQ: |
| 448 | return x == y; |
| 449 | case TCG_COND_NE: |
| 450 | return x != y; |
| 451 | case TCG_COND_LT: |
| 452 | return (int64_t)x < (int64_t)y; |
| 453 | case TCG_COND_GE: |
| 454 | return (int64_t)x >= (int64_t)y; |
| 455 | case TCG_COND_LE: |
| 456 | return (int64_t)x <= (int64_t)y; |
| 457 | case TCG_COND_GT: |
| 458 | return (int64_t)x > (int64_t)y; |
| 459 | case TCG_COND_LTU: |
| 460 | return x < y; |
| 461 | case TCG_COND_GEU: |
| 462 | return x >= y; |
| 463 | case TCG_COND_LEU: |
| 464 | return x <= y; |
| 465 | case TCG_COND_GTU: |
| 466 | return x > y; |
| 467 | default: |
| 468 | tcg_abort(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static bool do_constant_folding_cond_eq(TCGCond c) |
| 473 | { |
| 474 | switch (c) { |
| 475 | case TCG_COND_GT: |
| 476 | case TCG_COND_LTU: |
| 477 | case TCG_COND_LT: |
| 478 | case TCG_COND_GTU: |
| 479 | case TCG_COND_NE: |
| 480 | return 0; |
| 481 | case TCG_COND_GE: |
| 482 | case TCG_COND_GEU: |
| 483 | case TCG_COND_LE: |
| 484 | case TCG_COND_LEU: |
| 485 | case TCG_COND_EQ: |
| 486 | return 1; |
| 487 | default: |
| 488 | tcg_abort(); |
| 489 | } |
| 490 | } |
| 491 | |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 492 | /* Return 2 if the condition can't be simplified, and the result |
| 493 | of the condition (0 or 1) if it can */ |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 494 | static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x, |
| 495 | TCGArg y, TCGCond c) |
| 496 | { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 497 | tcg_target_ulong xv = arg_info(x)->val; |
| 498 | tcg_target_ulong yv = arg_info(y)->val; |
| 499 | if (arg_is_const(x) && arg_is_const(y)) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 500 | const TCGOpDef *def = &tcg_op_defs[op]; |
| 501 | tcg_debug_assert(!(def->flags & TCG_OPF_VECTOR)); |
| 502 | if (def->flags & TCG_OPF_64BIT) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 503 | return do_constant_folding_cond_64(xv, yv, c); |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 504 | } else { |
| 505 | return do_constant_folding_cond_32(xv, yv, c); |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 506 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 507 | } else if (args_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 508 | return do_constant_folding_cond_eq(c); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 509 | } else if (arg_is_const(y) && yv == 0) { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 510 | switch (c) { |
| 511 | case TCG_COND_LTU: |
| 512 | return 0; |
| 513 | case TCG_COND_GEU: |
| 514 | return 1; |
| 515 | default: |
| 516 | return 2; |
| 517 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 518 | } |
Alex Bennée | 550276a | 2016-09-30 22:30:55 +0100 | [diff] [blame] | 519 | return 2; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 522 | /* Return 2 if the condition can't be simplified, and the result |
| 523 | of the condition (0 or 1) if it can */ |
| 524 | static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c) |
| 525 | { |
| 526 | TCGArg al = p1[0], ah = p1[1]; |
| 527 | TCGArg bl = p2[0], bh = p2[1]; |
| 528 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 529 | if (arg_is_const(bl) && arg_is_const(bh)) { |
| 530 | tcg_target_ulong blv = arg_info(bl)->val; |
| 531 | tcg_target_ulong bhv = arg_info(bh)->val; |
| 532 | uint64_t b = deposit64(blv, 32, 32, bhv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 533 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 534 | if (arg_is_const(al) && arg_is_const(ah)) { |
| 535 | tcg_target_ulong alv = arg_info(al)->val; |
| 536 | tcg_target_ulong ahv = arg_info(ah)->val; |
| 537 | uint64_t a = deposit64(alv, 32, 32, ahv); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 538 | return do_constant_folding_cond_64(a, b, c); |
| 539 | } |
| 540 | if (b == 0) { |
| 541 | switch (c) { |
| 542 | case TCG_COND_LTU: |
| 543 | return 0; |
| 544 | case TCG_COND_GEU: |
| 545 | return 1; |
| 546 | default: |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 551 | if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 552 | return do_constant_folding_cond_eq(c); |
| 553 | } |
| 554 | return 2; |
| 555 | } |
| 556 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 557 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 558 | { |
| 559 | TCGArg a1 = *p1, a2 = *p2; |
| 560 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 561 | sum += arg_is_const(a1); |
| 562 | sum -= arg_is_const(a2); |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 563 | |
| 564 | /* Prefer the constant in second argument, and then the form |
| 565 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 566 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 567 | *p1 = a2; |
| 568 | *p2 = a1; |
| 569 | return true; |
| 570 | } |
| 571 | return false; |
| 572 | } |
| 573 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 574 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 575 | { |
| 576 | int sum = 0; |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 577 | sum += arg_is_const(p1[0]); |
| 578 | sum += arg_is_const(p1[1]); |
| 579 | sum -= arg_is_const(p2[0]); |
| 580 | sum -= arg_is_const(p2[1]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 581 | if (sum > 0) { |
| 582 | TCGArg t; |
| 583 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 584 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 585 | return true; |
| 586 | } |
| 587 | return false; |
| 588 | } |
| 589 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 590 | /* Propagate constants and copies, fold constant expressions. */ |
Aurelien Jarno | 36e60ef | 2015-06-04 21:53:27 +0200 | [diff] [blame] | 591 | void tcg_optimize(TCGContext *s) |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 592 | { |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 593 | int nb_temps, nb_globals; |
| 594 | TCGOp *op, *op_next, *prev_mb = NULL; |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 595 | struct tcg_temp_info *infos; |
| 596 | TCGTempSet temps_used; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 597 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 598 | /* Array VALS has an element for each temp. |
| 599 | 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] | 600 | If this temp is a copy of other ones then the other copies are |
| 601 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 602 | |
| 603 | nb_temps = s->nb_temps; |
| 604 | nb_globals = s->nb_globals; |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 605 | bitmap_zero(temps_used.l, nb_temps); |
| 606 | infos = tcg_malloc(sizeof(struct tcg_temp_info) * nb_temps); |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 607 | |
Richard Henderson | 15fa08f | 2017-11-02 15:19:14 +0100 | [diff] [blame] | 608 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 609 | tcg_target_ulong mask, partmask, affected; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 610 | int nb_oargs, nb_iargs, i; |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 611 | TCGArg tmp; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 612 | TCGOpcode opc = op->opc; |
| 613 | const TCGOpDef *def = &tcg_op_defs[opc]; |
| 614 | |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 615 | /* Count the arguments, and initialize the temps that are |
| 616 | going to be used */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 617 | if (opc == INDEX_op_call) { |
Richard Henderson | cd9090a | 2017-11-14 13:02:51 +0100 | [diff] [blame] | 618 | nb_oargs = TCGOP_CALLO(op); |
| 619 | nb_iargs = TCGOP_CALLI(op); |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 620 | for (i = 0; i < nb_oargs + nb_iargs; i++) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 621 | TCGTemp *ts = arg_temp(op->args[i]); |
| 622 | if (ts) { |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 623 | init_ts_info(infos, &temps_used, ts); |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
Aurelien Jarno | 1ff8c54 | 2012-09-11 16:18:49 +0200 | [diff] [blame] | 626 | } else { |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 627 | nb_oargs = def->nb_oargs; |
| 628 | nb_iargs = def->nb_iargs; |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 629 | for (i = 0; i < nb_oargs + nb_iargs; i++) { |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 630 | init_arg_info(infos, &temps_used, op->args[i]); |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 631 | } |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* Do copy propagation */ |
| 635 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 636 | TCGTemp *ts = arg_temp(op->args[i]); |
| 637 | if (ts && ts_is_copy(ts)) { |
| 638 | op->args[i] = temp_arg(find_better_copy(s, ts)); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 642 | /* For commutative operations make constant second argument */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 643 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 644 | CASE_OP_32_64_VEC(add): |
| 645 | CASE_OP_32_64_VEC(mul): |
| 646 | CASE_OP_32_64_VEC(and): |
| 647 | CASE_OP_32_64_VEC(or): |
| 648 | CASE_OP_32_64_VEC(xor): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 649 | CASE_OP_32_64(eqv): |
| 650 | CASE_OP_32_64(nand): |
| 651 | CASE_OP_32_64(nor): |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 652 | CASE_OP_32_64(muluh): |
| 653 | CASE_OP_32_64(mulsh): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 654 | swap_commutative(op->args[0], &op->args[1], &op->args[2]); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 655 | break; |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 656 | CASE_OP_32_64(brcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 657 | if (swap_commutative(-1, &op->args[0], &op->args[1])) { |
| 658 | op->args[2] = tcg_swap_cond(op->args[2]); |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 659 | } |
| 660 | break; |
| 661 | CASE_OP_32_64(setcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 662 | if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) { |
| 663 | op->args[3] = tcg_swap_cond(op->args[3]); |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 664 | } |
| 665 | break; |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 666 | CASE_OP_32_64(movcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 667 | if (swap_commutative(-1, &op->args[1], &op->args[2])) { |
| 668 | op->args[5] = tcg_swap_cond(op->args[5]); |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 669 | } |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 670 | /* For movcond, we canonicalize the "false" input reg to match |
| 671 | the destination reg so that the tcg backend can implement |
| 672 | a "move if true" operation. */ |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 673 | if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { |
| 674 | op->args[5] = tcg_invert_cond(op->args[5]); |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 675 | } |
Richard Henderson | 1e484e6 | 2012-10-02 11:32:22 -0700 | [diff] [blame] | 676 | break; |
Richard Henderson | d7156f7 | 2013-02-19 23:51:52 -0800 | [diff] [blame] | 677 | CASE_OP_32_64(add2): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 678 | swap_commutative(op->args[0], &op->args[2], &op->args[4]); |
| 679 | swap_commutative(op->args[1], &op->args[3], &op->args[5]); |
Richard Henderson | 1e484e6 | 2012-10-02 11:32:22 -0700 | [diff] [blame] | 680 | break; |
Richard Henderson | d7156f7 | 2013-02-19 23:51:52 -0800 | [diff] [blame] | 681 | CASE_OP_32_64(mulu2): |
Richard Henderson | 4d3203f | 2013-02-19 23:51:53 -0800 | [diff] [blame] | 682 | CASE_OP_32_64(muls2): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 683 | swap_commutative(op->args[0], &op->args[2], &op->args[3]); |
Richard Henderson | 1414968 | 2012-10-02 11:32:30 -0700 | [diff] [blame] | 684 | break; |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 685 | case INDEX_op_brcond2_i32: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 686 | if (swap_commutative2(&op->args[0], &op->args[2])) { |
| 687 | op->args[4] = tcg_swap_cond(op->args[4]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 688 | } |
| 689 | break; |
| 690 | case INDEX_op_setcond2_i32: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 691 | if (swap_commutative2(&op->args[1], &op->args[3])) { |
| 692 | op->args[5] = tcg_swap_cond(op->args[5]); |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 693 | } |
| 694 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 695 | default: |
| 696 | break; |
| 697 | } |
| 698 | |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 699 | /* Simplify expressions for "shift/rot r, 0, a => movi r, 0", |
| 700 | and "sub r, 0, a => neg r, a" case. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 701 | switch (opc) { |
Aurelien Jarno | 01ee528 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 702 | CASE_OP_32_64(shl): |
| 703 | CASE_OP_32_64(shr): |
| 704 | CASE_OP_32_64(sar): |
| 705 | CASE_OP_32_64(rotl): |
| 706 | CASE_OP_32_64(rotr): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 707 | if (arg_is_const(op->args[1]) |
| 708 | && arg_info(op->args[1])->val == 0) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 709 | tcg_opt_gen_movi(s, op, op->args[0], 0); |
Aurelien Jarno | 01ee528 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 710 | continue; |
| 711 | } |
| 712 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 713 | CASE_OP_32_64_VEC(sub): |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 714 | { |
| 715 | TCGOpcode neg_op; |
| 716 | bool have_neg; |
| 717 | |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 718 | if (arg_is_const(op->args[2])) { |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 719 | /* Proceed with possible constant folding. */ |
| 720 | break; |
| 721 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 722 | if (opc == INDEX_op_sub_i32) { |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 723 | neg_op = INDEX_op_neg_i32; |
| 724 | have_neg = TCG_TARGET_HAS_neg_i32; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 725 | } else if (opc == INDEX_op_sub_i64) { |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 726 | neg_op = INDEX_op_neg_i64; |
| 727 | have_neg = TCG_TARGET_HAS_neg_i64; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 728 | } else { |
| 729 | neg_op = INDEX_op_neg_vec; |
| 730 | have_neg = TCG_TARGET_HAS_neg_vec; |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 731 | } |
| 732 | if (!have_neg) { |
| 733 | break; |
| 734 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 735 | if (arg_is_const(op->args[1]) |
| 736 | && arg_info(op->args[1])->val == 0) { |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 737 | op->opc = neg_op; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 738 | reset_temp(op->args[0]); |
| 739 | op->args[1] = op->args[2]; |
Richard Henderson | 2d49754 | 2013-03-21 09:13:33 -0700 | [diff] [blame] | 740 | continue; |
| 741 | } |
| 742 | } |
| 743 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 744 | CASE_OP_32_64_VEC(xor): |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 745 | CASE_OP_32_64(nand): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 746 | if (!arg_is_const(op->args[1]) |
| 747 | && arg_is_const(op->args[2]) |
| 748 | && arg_info(op->args[2])->val == -1) { |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 749 | i = 1; |
| 750 | goto try_not; |
| 751 | } |
| 752 | break; |
| 753 | CASE_OP_32_64(nor): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 754 | if (!arg_is_const(op->args[1]) |
| 755 | && arg_is_const(op->args[2]) |
| 756 | && arg_info(op->args[2])->val == 0) { |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 757 | i = 1; |
| 758 | goto try_not; |
| 759 | } |
| 760 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 761 | CASE_OP_32_64_VEC(andc): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 762 | if (!arg_is_const(op->args[2]) |
| 763 | && arg_is_const(op->args[1]) |
| 764 | && arg_info(op->args[1])->val == -1) { |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 765 | i = 2; |
| 766 | goto try_not; |
| 767 | } |
| 768 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 769 | CASE_OP_32_64_VEC(orc): |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 770 | CASE_OP_32_64(eqv): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 771 | if (!arg_is_const(op->args[2]) |
| 772 | && arg_is_const(op->args[1]) |
| 773 | && arg_info(op->args[1])->val == 0) { |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 774 | i = 2; |
| 775 | goto try_not; |
| 776 | } |
| 777 | break; |
| 778 | try_not: |
| 779 | { |
| 780 | TCGOpcode not_op; |
| 781 | bool have_not; |
| 782 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 783 | if (def->flags & TCG_OPF_VECTOR) { |
| 784 | not_op = INDEX_op_not_vec; |
| 785 | have_not = TCG_TARGET_HAS_not_vec; |
| 786 | } else if (def->flags & TCG_OPF_64BIT) { |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 787 | not_op = INDEX_op_not_i64; |
| 788 | have_not = TCG_TARGET_HAS_not_i64; |
| 789 | } else { |
| 790 | not_op = INDEX_op_not_i32; |
| 791 | have_not = TCG_TARGET_HAS_not_i32; |
| 792 | } |
| 793 | if (!have_not) { |
| 794 | break; |
| 795 | } |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 796 | op->opc = not_op; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 797 | reset_temp(op->args[0]); |
| 798 | op->args[1] = op->args[i]; |
Richard Henderson | e201b56 | 2014-01-28 13:15:38 -0800 | [diff] [blame] | 799 | continue; |
| 800 | } |
Aurelien Jarno | 01ee528 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 801 | default: |
| 802 | break; |
| 803 | } |
| 804 | |
Richard Henderson | 464a144 | 2014-01-31 07:42:11 -0600 | [diff] [blame] | 805 | /* Simplify expression for "op r, a, const => mov r, a" cases */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 806 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 807 | CASE_OP_32_64_VEC(add): |
| 808 | CASE_OP_32_64_VEC(sub): |
| 809 | CASE_OP_32_64_VEC(or): |
| 810 | CASE_OP_32_64_VEC(xor): |
| 811 | CASE_OP_32_64_VEC(andc): |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 812 | CASE_OP_32_64(shl): |
| 813 | CASE_OP_32_64(shr): |
| 814 | CASE_OP_32_64(sar): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 815 | CASE_OP_32_64(rotl): |
| 816 | CASE_OP_32_64(rotr): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 817 | if (!arg_is_const(op->args[1]) |
| 818 | && arg_is_const(op->args[2]) |
| 819 | && arg_info(op->args[2])->val == 0) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 820 | tcg_opt_gen_mov(s, op, op->args[0], op->args[1]); |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 821 | continue; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 822 | } |
| 823 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 824 | CASE_OP_32_64_VEC(and): |
| 825 | CASE_OP_32_64_VEC(orc): |
Richard Henderson | 464a144 | 2014-01-31 07:42:11 -0600 | [diff] [blame] | 826 | CASE_OP_32_64(eqv): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 827 | if (!arg_is_const(op->args[1]) |
| 828 | && arg_is_const(op->args[2]) |
| 829 | && arg_info(op->args[2])->val == -1) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 830 | tcg_opt_gen_mov(s, op, op->args[0], op->args[1]); |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 831 | continue; |
Richard Henderson | 464a144 | 2014-01-31 07:42:11 -0600 | [diff] [blame] | 832 | } |
| 833 | break; |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 834 | default: |
| 835 | break; |
| 836 | } |
| 837 | |
Aurelien Jarno | 3031244 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 838 | /* Simplify using known-zero bits. Currently only ops with a single |
| 839 | output argument is supported. */ |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 840 | mask = -1; |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 841 | affected = -1; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 842 | switch (opc) { |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 843 | CASE_OP_32_64(ext8s): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 844 | if ((arg_info(op->args[1])->mask & 0x80) != 0) { |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | CASE_OP_32_64(ext8u): |
| 848 | mask = 0xff; |
| 849 | goto and_const; |
| 850 | CASE_OP_32_64(ext16s): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 851 | if ((arg_info(op->args[1])->mask & 0x8000) != 0) { |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | CASE_OP_32_64(ext16u): |
| 855 | mask = 0xffff; |
| 856 | goto and_const; |
| 857 | case INDEX_op_ext32s_i64: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 858 | if ((arg_info(op->args[1])->mask & 0x80000000) != 0) { |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | case INDEX_op_ext32u_i64: |
| 862 | mask = 0xffffffffU; |
| 863 | goto and_const; |
| 864 | |
| 865 | CASE_OP_32_64(and): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 866 | mask = arg_info(op->args[2])->mask; |
| 867 | if (arg_is_const(op->args[2])) { |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 868 | and_const: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 869 | affected = arg_info(op->args[1])->mask & ~mask; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 870 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 871 | mask = arg_info(op->args[1])->mask & mask; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 872 | break; |
| 873 | |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 874 | case INDEX_op_ext_i32_i64: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 875 | if ((arg_info(op->args[1])->mask & 0x80000000) != 0) { |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | case INDEX_op_extu_i32_i64: |
| 879 | /* We do not compute affected as it is a size changing op. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 880 | mask = (uint32_t)arg_info(op->args[1])->mask; |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 881 | break; |
| 882 | |
Richard Henderson | 23ec69ed | 2014-01-28 12:03:24 -0800 | [diff] [blame] | 883 | CASE_OP_32_64(andc): |
| 884 | /* Known-zeros does not imply known-ones. Therefore unless |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 885 | op->args[2] is constant, we can't infer anything from it. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 886 | if (arg_is_const(op->args[2])) { |
| 887 | mask = ~arg_info(op->args[2])->mask; |
Richard Henderson | 23ec69ed | 2014-01-28 12:03:24 -0800 | [diff] [blame] | 888 | goto and_const; |
| 889 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 890 | /* But we certainly know nothing outside args[1] may be set. */ |
| 891 | mask = arg_info(op->args[1])->mask; |
Richard Henderson | 23ec69ed | 2014-01-28 12:03:24 -0800 | [diff] [blame] | 892 | break; |
| 893 | |
Aurelien Jarno | e46b225 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 894 | case INDEX_op_sar_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 895 | if (arg_is_const(op->args[2])) { |
| 896 | tmp = arg_info(op->args[2])->val & 31; |
| 897 | mask = (int32_t)arg_info(op->args[1])->mask >> tmp; |
Aurelien Jarno | e46b225 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 898 | } |
| 899 | break; |
| 900 | case INDEX_op_sar_i64: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 901 | if (arg_is_const(op->args[2])) { |
| 902 | tmp = arg_info(op->args[2])->val & 63; |
| 903 | mask = (int64_t)arg_info(op->args[1])->mask >> tmp; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 904 | } |
| 905 | break; |
| 906 | |
Aurelien Jarno | e46b225 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 907 | case INDEX_op_shr_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 908 | if (arg_is_const(op->args[2])) { |
| 909 | tmp = arg_info(op->args[2])->val & 31; |
| 910 | mask = (uint32_t)arg_info(op->args[1])->mask >> tmp; |
Aurelien Jarno | e46b225 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 911 | } |
| 912 | break; |
| 913 | case INDEX_op_shr_i64: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 914 | if (arg_is_const(op->args[2])) { |
| 915 | tmp = arg_info(op->args[2])->val & 63; |
| 916 | mask = (uint64_t)arg_info(op->args[1])->mask >> tmp; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 917 | } |
| 918 | break; |
| 919 | |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 920 | case INDEX_op_extrl_i64_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 921 | mask = (uint32_t)arg_info(op->args[1])->mask; |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 922 | break; |
| 923 | case INDEX_op_extrh_i64_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 924 | mask = (uint64_t)arg_info(op->args[1])->mask >> 32; |
Richard Henderson | 4bb7a41 | 2013-09-09 17:03:24 -0700 | [diff] [blame] | 925 | break; |
| 926 | |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 927 | CASE_OP_32_64(shl): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 928 | if (arg_is_const(op->args[2])) { |
| 929 | tmp = arg_info(op->args[2])->val & (TCG_TARGET_REG_BITS - 1); |
| 930 | mask = arg_info(op->args[1])->mask << tmp; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 931 | } |
| 932 | break; |
| 933 | |
| 934 | CASE_OP_32_64(neg): |
| 935 | /* Set to 1 all bits to the left of the rightmost. */ |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 936 | mask = -(arg_info(op->args[1])->mask |
| 937 | & -arg_info(op->args[1])->mask); |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 938 | break; |
| 939 | |
| 940 | CASE_OP_32_64(deposit): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 941 | mask = deposit64(arg_info(op->args[1])->mask, |
| 942 | op->args[3], op->args[4], |
| 943 | arg_info(op->args[2])->mask); |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 944 | break; |
| 945 | |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 946 | CASE_OP_32_64(extract): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 947 | mask = extract64(arg_info(op->args[1])->mask, |
| 948 | op->args[2], op->args[3]); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 949 | if (op->args[2] == 0) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 950 | affected = arg_info(op->args[1])->mask & ~mask; |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 951 | } |
| 952 | break; |
| 953 | CASE_OP_32_64(sextract): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 954 | mask = sextract64(arg_info(op->args[1])->mask, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 955 | op->args[2], op->args[3]); |
| 956 | if (op->args[2] == 0 && (tcg_target_long)mask >= 0) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 957 | affected = arg_info(op->args[1])->mask & ~mask; |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 958 | } |
| 959 | break; |
| 960 | |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 961 | CASE_OP_32_64(or): |
| 962 | CASE_OP_32_64(xor): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 963 | mask = arg_info(op->args[1])->mask | arg_info(op->args[2])->mask; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 964 | break; |
| 965 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 966 | case INDEX_op_clz_i32: |
| 967 | case INDEX_op_ctz_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 968 | mask = arg_info(op->args[2])->mask | 31; |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 969 | break; |
| 970 | |
| 971 | case INDEX_op_clz_i64: |
| 972 | case INDEX_op_ctz_i64: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 973 | mask = arg_info(op->args[2])->mask | 63; |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 974 | break; |
| 975 | |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 976 | case INDEX_op_ctpop_i32: |
| 977 | mask = 32 | 31; |
| 978 | break; |
| 979 | case INDEX_op_ctpop_i64: |
| 980 | mask = 64 | 63; |
| 981 | break; |
| 982 | |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 983 | CASE_OP_32_64(setcond): |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 984 | case INDEX_op_setcond2_i32: |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 985 | mask = 1; |
| 986 | break; |
| 987 | |
| 988 | CASE_OP_32_64(movcond): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 989 | mask = arg_info(op->args[3])->mask | arg_info(op->args[4])->mask; |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 990 | break; |
| 991 | |
Aurelien Jarno | c8d7027 | 2013-09-03 08:27:39 +0200 | [diff] [blame] | 992 | CASE_OP_32_64(ld8u): |
Aurelien Jarno | c8d7027 | 2013-09-03 08:27:39 +0200 | [diff] [blame] | 993 | mask = 0xff; |
| 994 | break; |
| 995 | CASE_OP_32_64(ld16u): |
Aurelien Jarno | c8d7027 | 2013-09-03 08:27:39 +0200 | [diff] [blame] | 996 | mask = 0xffff; |
| 997 | break; |
| 998 | case INDEX_op_ld32u_i64: |
Aurelien Jarno | c8d7027 | 2013-09-03 08:27:39 +0200 | [diff] [blame] | 999 | mask = 0xffffffffu; |
| 1000 | break; |
| 1001 | |
| 1002 | CASE_OP_32_64(qemu_ld): |
| 1003 | { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1004 | TCGMemOpIdx oi = op->args[nb_oargs + nb_iargs]; |
Richard Henderson | 59227d5 | 2015-05-12 11:51:44 -0700 | [diff] [blame] | 1005 | TCGMemOp mop = get_memop(oi); |
Aurelien Jarno | c8d7027 | 2013-09-03 08:27:39 +0200 | [diff] [blame] | 1006 | if (!(mop & MO_SIGN)) { |
| 1007 | mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1; |
| 1008 | } |
| 1009 | } |
| 1010 | break; |
| 1011 | |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 1012 | default: |
| 1013 | break; |
| 1014 | } |
| 1015 | |
Richard Henderson | bc8d688 | 2014-06-08 18:24:14 -0700 | [diff] [blame] | 1016 | /* 32-bit ops generate 32-bit results. For the result is zero test |
| 1017 | below, we can ignore high bits, but for further optimizations we |
| 1018 | need to record that the high bits contain garbage. */ |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 1019 | partmask = mask; |
Richard Henderson | bc8d688 | 2014-06-08 18:24:14 -0700 | [diff] [blame] | 1020 | if (!(def->flags & TCG_OPF_64BIT)) { |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 1021 | mask |= ~(tcg_target_ulong)0xffffffffu; |
| 1022 | partmask &= 0xffffffffu; |
| 1023 | affected &= 0xffffffffu; |
Aurelien Jarno | f096dc9 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Richard Henderson | 24666ba | 2014-05-22 11:14:10 -0700 | [diff] [blame] | 1026 | if (partmask == 0) { |
Aurelien Jarno | eabb7b9 | 2016-04-21 10:48:49 +0200 | [diff] [blame] | 1027 | tcg_debug_assert(nb_oargs == 1); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1028 | tcg_opt_gen_movi(s, op, op->args[0], 0); |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 1029 | continue; |
| 1030 | } |
| 1031 | if (affected == 0) { |
Aurelien Jarno | eabb7b9 | 2016-04-21 10:48:49 +0200 | [diff] [blame] | 1032 | tcg_debug_assert(nb_oargs == 1); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1033 | tcg_opt_gen_mov(s, op, op->args[0], op->args[1]); |
Paolo Bonzini | 633f650 | 2013-01-11 15:42:53 -0800 | [diff] [blame] | 1034 | continue; |
| 1035 | } |
| 1036 | |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 1037 | /* Simplify expression for "op r, a, 0 => movi r, 0" cases */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1038 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1039 | CASE_OP_32_64_VEC(and): |
| 1040 | CASE_OP_32_64_VEC(mul): |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 1041 | CASE_OP_32_64(muluh): |
| 1042 | CASE_OP_32_64(mulsh): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1043 | if (arg_is_const(op->args[2]) |
| 1044 | && arg_info(op->args[2])->val == 0) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1045 | tcg_opt_gen_movi(s, op, op->args[0], 0); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 1046 | continue; |
| 1047 | } |
| 1048 | break; |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 1049 | default: |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | /* Simplify expression for "op r, a, a => mov r, a" cases */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1054 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1055 | CASE_OP_32_64_VEC(or): |
| 1056 | CASE_OP_32_64_VEC(and): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1057 | if (args_are_copies(op->args[1], op->args[2])) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1058 | tcg_opt_gen_mov(s, op, op->args[0], op->args[1]); |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 1059 | continue; |
| 1060 | } |
| 1061 | break; |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 1062 | default: |
| 1063 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 1064 | } |
| 1065 | |
Aurelien Jarno | 3c94193 | 2012-09-18 19:12:36 +0200 | [diff] [blame] | 1066 | /* Simplify expression for "op r, a, a => movi r, 0" cases */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1067 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1068 | CASE_OP_32_64_VEC(andc): |
| 1069 | CASE_OP_32_64_VEC(sub): |
| 1070 | CASE_OP_32_64_VEC(xor): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1071 | if (args_are_copies(op->args[1], op->args[2])) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1072 | tcg_opt_gen_movi(s, op, op->args[0], 0); |
Aurelien Jarno | 3c94193 | 2012-09-18 19:12:36 +0200 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
| 1075 | break; |
| 1076 | default: |
| 1077 | break; |
| 1078 | } |
| 1079 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1080 | /* Propagate constants through copy operations and do constant |
| 1081 | folding. Constants will be substituted to arguments by register |
| 1082 | allocator where needed and possible. Also detect copies. */ |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1083 | switch (opc) { |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1084 | CASE_OP_32_64_VEC(mov): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1085 | tcg_opt_gen_mov(s, op, op->args[0], op->args[1]); |
Aurelien Jarno | 97a79eb | 2015-06-05 11:19:18 +0200 | [diff] [blame] | 1086 | break; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1087 | CASE_OP_32_64(movi): |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1088 | case INDEX_op_dupi_vec: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1089 | tcg_opt_gen_movi(s, op, op->args[0], op->args[1]); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1090 | break; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1091 | |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1092 | case INDEX_op_dup_vec: |
| 1093 | if (arg_is_const(op->args[1])) { |
| 1094 | tmp = arg_info(op->args[1])->val; |
| 1095 | tmp = dup_const(TCGOP_VECE(op), tmp); |
| 1096 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 1fb57da7 | 2018-08-05 16:32:58 -0700 | [diff] [blame] | 1097 | break; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1098 | } |
Richard Henderson | 1fb57da7 | 2018-08-05 16:32:58 -0700 | [diff] [blame] | 1099 | goto do_default; |
Richard Henderson | 170ba88 | 2017-11-22 09:07:11 +0100 | [diff] [blame] | 1100 | |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 1101 | CASE_OP_32_64(not): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 1102 | CASE_OP_32_64(neg): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 1103 | CASE_OP_32_64(ext8s): |
| 1104 | CASE_OP_32_64(ext8u): |
| 1105 | CASE_OP_32_64(ext16s): |
| 1106 | CASE_OP_32_64(ext16u): |
Richard Henderson | a768e4e | 2016-11-21 11:13:39 +0100 | [diff] [blame] | 1107 | CASE_OP_32_64(ctpop): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 1108 | case INDEX_op_ext32s_i64: |
| 1109 | case INDEX_op_ext32u_i64: |
Aurelien Jarno | 8bcb5c8 | 2015-07-27 12:41:45 +0200 | [diff] [blame] | 1110 | case INDEX_op_ext_i32_i64: |
| 1111 | case INDEX_op_extu_i32_i64: |
Richard Henderson | 609ad70 | 2015-07-24 07:16:00 -0700 | [diff] [blame] | 1112 | case INDEX_op_extrl_i64_i32: |
| 1113 | case INDEX_op_extrh_i64_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1114 | if (arg_is_const(op->args[1])) { |
| 1115 | tmp = do_constant_folding(opc, arg_info(op->args[1])->val, 0); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1116 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1117 | break; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 1118 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1119 | goto do_default; |
| 1120 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 1121 | CASE_OP_32_64(add): |
| 1122 | CASE_OP_32_64(sub): |
| 1123 | CASE_OP_32_64(mul): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 1124 | CASE_OP_32_64(or): |
| 1125 | CASE_OP_32_64(and): |
| 1126 | CASE_OP_32_64(xor): |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 1127 | CASE_OP_32_64(shl): |
| 1128 | CASE_OP_32_64(shr): |
| 1129 | CASE_OP_32_64(sar): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 1130 | CASE_OP_32_64(rotl): |
| 1131 | CASE_OP_32_64(rotr): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 1132 | CASE_OP_32_64(andc): |
| 1133 | CASE_OP_32_64(orc): |
| 1134 | CASE_OP_32_64(eqv): |
| 1135 | CASE_OP_32_64(nand): |
| 1136 | CASE_OP_32_64(nor): |
Richard Henderson | 0327152 | 2013-08-14 14:35:56 -0700 | [diff] [blame] | 1137 | CASE_OP_32_64(muluh): |
| 1138 | CASE_OP_32_64(mulsh): |
Richard Henderson | 01547f7 | 2013-08-14 15:22:46 -0700 | [diff] [blame] | 1139 | CASE_OP_32_64(div): |
| 1140 | CASE_OP_32_64(divu): |
| 1141 | CASE_OP_32_64(rem): |
| 1142 | CASE_OP_32_64(remu): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1143 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1144 | tmp = do_constant_folding(opc, arg_info(op->args[1])->val, |
| 1145 | arg_info(op->args[2])->val); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1146 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1147 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 1148 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1149 | goto do_default; |
| 1150 | |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 1151 | CASE_OP_32_64(clz): |
| 1152 | CASE_OP_32_64(ctz): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1153 | if (arg_is_const(op->args[1])) { |
| 1154 | TCGArg v = arg_info(op->args[1])->val; |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 1155 | if (v != 0) { |
| 1156 | tmp = do_constant_folding(opc, v, 0); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1157 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 1158 | } else { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1159 | tcg_opt_gen_mov(s, op, op->args[0], op->args[2]); |
Richard Henderson | 0e28d00 | 2016-11-16 09:23:28 +0100 | [diff] [blame] | 1160 | } |
| 1161 | break; |
| 1162 | } |
| 1163 | goto do_default; |
| 1164 | |
Aurelien Jarno | 7ef55fc | 2012-09-21 11:07:29 +0200 | [diff] [blame] | 1165 | CASE_OP_32_64(deposit): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1166 | if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { |
| 1167 | tmp = deposit64(arg_info(op->args[1])->val, |
| 1168 | op->args[3], op->args[4], |
| 1169 | arg_info(op->args[2])->val); |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1170 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1171 | break; |
Aurelien Jarno | 7ef55fc | 2012-09-21 11:07:29 +0200 | [diff] [blame] | 1172 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1173 | goto do_default; |
| 1174 | |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 1175 | CASE_OP_32_64(extract): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1176 | if (arg_is_const(op->args[1])) { |
| 1177 | tmp = extract64(arg_info(op->args[1])->val, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1178 | op->args[2], op->args[3]); |
| 1179 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 1180 | break; |
| 1181 | } |
| 1182 | goto do_default; |
| 1183 | |
| 1184 | CASE_OP_32_64(sextract): |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1185 | if (arg_is_const(op->args[1])) { |
| 1186 | tmp = sextract64(arg_info(op->args[1])->val, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1187 | op->args[2], op->args[3]); |
| 1188 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 7ec8bab | 2016-10-14 12:04:32 -0500 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | goto do_default; |
| 1192 | |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1193 | CASE_OP_32_64(setcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1194 | tmp = do_constant_folding_cond(opc, op->args[1], |
| 1195 | op->args[2], op->args[3]); |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 1196 | if (tmp != 2) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1197 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1198 | break; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1199 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1200 | goto do_default; |
| 1201 | |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1202 | CASE_OP_32_64(brcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1203 | tmp = do_constant_folding_cond(opc, op->args[0], |
| 1204 | op->args[1], op->args[2]); |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 1205 | if (tmp != 2) { |
| 1206 | if (tmp) { |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 1207 | bitmap_zero(temps_used.l, nb_temps); |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1208 | op->opc = INDEX_op_br; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1209 | op->args[0] = op->args[3]; |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1210 | } else { |
Richard Henderson | 0c627cd | 2014-03-30 16:51:54 -0700 | [diff] [blame] | 1211 | tcg_op_remove(s, op); |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1212 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1213 | break; |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 1214 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1215 | goto do_default; |
| 1216 | |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 1217 | CASE_OP_32_64(movcond): |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1218 | tmp = do_constant_folding_cond(opc, op->args[1], |
| 1219 | op->args[2], op->args[5]); |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 1220 | if (tmp != 2) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1221 | tcg_opt_gen_mov(s, op, op->args[0], op->args[4-tmp]); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1222 | break; |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 1223 | } |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1224 | if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) { |
| 1225 | tcg_target_ulong tv = arg_info(op->args[3])->val; |
| 1226 | tcg_target_ulong fv = arg_info(op->args[4])->val; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1227 | TCGCond cond = op->args[5]; |
Richard Henderson | 333b21b | 2016-10-23 20:44:32 -0700 | [diff] [blame] | 1228 | if (fv == 1 && tv == 0) { |
| 1229 | cond = tcg_invert_cond(cond); |
| 1230 | } else if (!(tv == 1 && fv == 0)) { |
| 1231 | goto do_default; |
| 1232 | } |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1233 | op->args[3] = cond; |
Richard Henderson | 333b21b | 2016-10-23 20:44:32 -0700 | [diff] [blame] | 1234 | op->opc = opc = (opc == INDEX_op_movcond_i32 |
| 1235 | ? INDEX_op_setcond_i32 |
| 1236 | : INDEX_op_setcond_i64); |
| 1237 | nb_iargs = 2; |
| 1238 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1239 | goto do_default; |
| 1240 | |
Richard Henderson | 212c328 | 2012-10-02 11:32:28 -0700 | [diff] [blame] | 1241 | case INDEX_op_add2_i32: |
| 1242 | case INDEX_op_sub2_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1243 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3]) |
| 1244 | && arg_is_const(op->args[4]) && arg_is_const(op->args[5])) { |
| 1245 | uint32_t al = arg_info(op->args[2])->val; |
| 1246 | uint32_t ah = arg_info(op->args[3])->val; |
| 1247 | uint32_t bl = arg_info(op->args[4])->val; |
| 1248 | uint32_t bh = arg_info(op->args[5])->val; |
Richard Henderson | 212c328 | 2012-10-02 11:32:28 -0700 | [diff] [blame] | 1249 | uint64_t a = ((uint64_t)ah << 32) | al; |
| 1250 | uint64_t b = ((uint64_t)bh << 32) | bl; |
| 1251 | TCGArg rl, rh; |
Richard Henderson | 5a18407 | 2016-06-23 20:34:33 -0700 | [diff] [blame] | 1252 | TCGOp *op2 = tcg_op_insert_before(s, op, INDEX_op_movi_i32, 2); |
Richard Henderson | 212c328 | 2012-10-02 11:32:28 -0700 | [diff] [blame] | 1253 | |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1254 | if (opc == INDEX_op_add2_i32) { |
Richard Henderson | 212c328 | 2012-10-02 11:32:28 -0700 | [diff] [blame] | 1255 | a += b; |
| 1256 | } else { |
| 1257 | a -= b; |
| 1258 | } |
| 1259 | |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1260 | rl = op->args[0]; |
| 1261 | rh = op->args[1]; |
| 1262 | tcg_opt_gen_movi(s, op, rl, (int32_t)a); |
| 1263 | tcg_opt_gen_movi(s, op2, rh, (int32_t)(a >> 32)); |
Richard Henderson | 212c328 | 2012-10-02 11:32:28 -0700 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | goto do_default; |
| 1267 | |
Richard Henderson | 1414968 | 2012-10-02 11:32:30 -0700 | [diff] [blame] | 1268 | case INDEX_op_mulu2_i32: |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1269 | if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { |
| 1270 | uint32_t a = arg_info(op->args[2])->val; |
| 1271 | uint32_t b = arg_info(op->args[3])->val; |
Richard Henderson | 1414968 | 2012-10-02 11:32:30 -0700 | [diff] [blame] | 1272 | uint64_t r = (uint64_t)a * b; |
| 1273 | TCGArg rl, rh; |
Richard Henderson | 5a18407 | 2016-06-23 20:34:33 -0700 | [diff] [blame] | 1274 | TCGOp *op2 = tcg_op_insert_before(s, op, INDEX_op_movi_i32, 2); |
Richard Henderson | 1414968 | 2012-10-02 11:32:30 -0700 | [diff] [blame] | 1275 | |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1276 | rl = op->args[0]; |
| 1277 | rh = op->args[1]; |
| 1278 | tcg_opt_gen_movi(s, op, rl, (int32_t)r); |
| 1279 | tcg_opt_gen_movi(s, op2, rh, (int32_t)(r >> 32)); |
Richard Henderson | 1414968 | 2012-10-02 11:32:30 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | } |
| 1282 | goto do_default; |
| 1283 | |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 1284 | case INDEX_op_brcond2_i32: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1285 | tmp = do_constant_folding_cond2(&op->args[0], &op->args[2], |
| 1286 | op->args[4]); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1287 | if (tmp != 2) { |
| 1288 | if (tmp) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1289 | do_brcond_true: |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 1290 | bitmap_zero(temps_used.l, nb_temps); |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1291 | op->opc = INDEX_op_br; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1292 | op->args[0] = op->args[5]; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1293 | } else { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1294 | do_brcond_false: |
Richard Henderson | 0c627cd | 2014-03-30 16:51:54 -0700 | [diff] [blame] | 1295 | tcg_op_remove(s, op); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1296 | } |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1297 | } else if ((op->args[4] == TCG_COND_LT |
| 1298 | || op->args[4] == TCG_COND_GE) |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1299 | && arg_is_const(op->args[2]) |
| 1300 | && arg_info(op->args[2])->val == 0 |
| 1301 | && arg_is_const(op->args[3]) |
| 1302 | && arg_info(op->args[3])->val == 0) { |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1303 | /* Simplify LT/GE comparisons vs zero to a single compare |
| 1304 | vs the high word of the input. */ |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1305 | do_brcond_high: |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 1306 | bitmap_zero(temps_used.l, nb_temps); |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1307 | op->opc = INDEX_op_brcond_i32; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1308 | op->args[0] = op->args[1]; |
| 1309 | op->args[1] = op->args[3]; |
| 1310 | op->args[2] = op->args[4]; |
| 1311 | op->args[3] = op->args[5]; |
| 1312 | } else if (op->args[4] == TCG_COND_EQ) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1313 | /* Simplify EQ comparisons where one of the pairs |
| 1314 | can be simplified. */ |
| 1315 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1316 | op->args[0], op->args[2], |
| 1317 | TCG_COND_EQ); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1318 | if (tmp == 0) { |
| 1319 | goto do_brcond_false; |
| 1320 | } else if (tmp == 1) { |
| 1321 | goto do_brcond_high; |
| 1322 | } |
| 1323 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1324 | op->args[1], op->args[3], |
| 1325 | TCG_COND_EQ); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1326 | if (tmp == 0) { |
| 1327 | goto do_brcond_false; |
| 1328 | } else if (tmp != 1) { |
| 1329 | goto do_default; |
| 1330 | } |
| 1331 | do_brcond_low: |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 1332 | bitmap_zero(temps_used.l, nb_temps); |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1333 | op->opc = INDEX_op_brcond_i32; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1334 | op->args[1] = op->args[2]; |
| 1335 | op->args[2] = op->args[4]; |
| 1336 | op->args[3] = op->args[5]; |
| 1337 | } else if (op->args[4] == TCG_COND_NE) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1338 | /* Simplify NE comparisons where one of the pairs |
| 1339 | can be simplified. */ |
| 1340 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1341 | op->args[0], op->args[2], |
| 1342 | TCG_COND_NE); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1343 | if (tmp == 0) { |
| 1344 | goto do_brcond_high; |
| 1345 | } else if (tmp == 1) { |
| 1346 | goto do_brcond_true; |
| 1347 | } |
| 1348 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1349 | op->args[1], op->args[3], |
| 1350 | TCG_COND_NE); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1351 | if (tmp == 0) { |
| 1352 | goto do_brcond_low; |
| 1353 | } else if (tmp == 1) { |
| 1354 | goto do_brcond_true; |
| 1355 | } |
| 1356 | goto do_default; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1357 | } else { |
| 1358 | goto do_default; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 1359 | } |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1360 | break; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 1361 | |
| 1362 | case INDEX_op_setcond2_i32: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1363 | tmp = do_constant_folding_cond2(&op->args[1], &op->args[3], |
| 1364 | op->args[5]); |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1365 | if (tmp != 2) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1366 | do_setcond_const: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1367 | tcg_opt_gen_movi(s, op, op->args[0], tmp); |
| 1368 | } else if ((op->args[5] == TCG_COND_LT |
| 1369 | || op->args[5] == TCG_COND_GE) |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1370 | && arg_is_const(op->args[3]) |
| 1371 | && arg_info(op->args[3])->val == 0 |
| 1372 | && arg_is_const(op->args[4]) |
| 1373 | && arg_info(op->args[4])->val == 0) { |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1374 | /* Simplify LT/GE comparisons vs zero to a single compare |
| 1375 | vs the high word of the input. */ |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1376 | do_setcond_high: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1377 | reset_temp(op->args[0]); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1378 | arg_info(op->args[0])->mask = 1; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1379 | op->opc = INDEX_op_setcond_i32; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1380 | op->args[1] = op->args[2]; |
| 1381 | op->args[2] = op->args[4]; |
| 1382 | op->args[3] = op->args[5]; |
| 1383 | } else if (op->args[5] == TCG_COND_EQ) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1384 | /* Simplify EQ comparisons where one of the pairs |
| 1385 | can be simplified. */ |
| 1386 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1387 | op->args[1], op->args[3], |
| 1388 | TCG_COND_EQ); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1389 | if (tmp == 0) { |
| 1390 | goto do_setcond_const; |
| 1391 | } else if (tmp == 1) { |
| 1392 | goto do_setcond_high; |
| 1393 | } |
| 1394 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1395 | op->args[2], op->args[4], |
| 1396 | TCG_COND_EQ); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1397 | if (tmp == 0) { |
| 1398 | goto do_setcond_high; |
| 1399 | } else if (tmp != 1) { |
| 1400 | goto do_default; |
| 1401 | } |
| 1402 | do_setcond_low: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1403 | reset_temp(op->args[0]); |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1404 | arg_info(op->args[0])->mask = 1; |
Richard Henderson | c45cb8b | 2014-09-19 13:49:15 -0700 | [diff] [blame] | 1405 | op->opc = INDEX_op_setcond_i32; |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1406 | op->args[2] = op->args[3]; |
| 1407 | op->args[3] = op->args[5]; |
| 1408 | } else if (op->args[5] == TCG_COND_NE) { |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1409 | /* Simplify NE comparisons where one of the pairs |
| 1410 | can be simplified. */ |
| 1411 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1412 | op->args[1], op->args[3], |
| 1413 | TCG_COND_NE); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1414 | if (tmp == 0) { |
| 1415 | goto do_setcond_high; |
| 1416 | } else if (tmp == 1) { |
| 1417 | goto do_setcond_const; |
| 1418 | } |
| 1419 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1420 | op->args[2], op->args[4], |
| 1421 | TCG_COND_NE); |
Richard Henderson | a763551 | 2014-04-23 22:18:30 -0700 | [diff] [blame] | 1422 | if (tmp == 0) { |
| 1423 | goto do_setcond_low; |
| 1424 | } else if (tmp == 1) { |
| 1425 | goto do_setcond_const; |
| 1426 | } |
| 1427 | goto do_default; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1428 | } else { |
| 1429 | goto do_default; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 1430 | } |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame] | 1431 | break; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 1432 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1433 | case INDEX_op_call: |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1434 | if (!(op->args[nb_oargs + nb_iargs + 1] |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 1435 | & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1436 | for (i = 0; i < nb_globals; i++) { |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 1437 | if (test_bit(i, temps_used.l)) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1438 | reset_ts(&s->temps[i]); |
Aurelien Jarno | 1208d7d | 2015-07-27 12:41:44 +0200 | [diff] [blame] | 1439 | } |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1440 | } |
| 1441 | } |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 1442 | goto do_reset_output; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1443 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1444 | default: |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 1445 | do_default: |
| 1446 | /* Default case: we know nothing about operation (or were unable |
| 1447 | to compute the operation result) so no propagation is done. |
| 1448 | We trash everything if the operation is the end of a basic |
Paolo Bonzini | 3a9d8b1 | 2013-01-11 15:42:52 -0800 | [diff] [blame] | 1449 | block, otherwise we only trash the output args. "mask" is |
| 1450 | the non-zero bits mask for the first output arg. */ |
Aurelien Jarno | a255066 | 2012-09-19 21:40:30 +0200 | [diff] [blame] | 1451 | if (def->flags & TCG_OPF_BB_END) { |
Emilio G. Cota | 34184b0 | 2017-07-19 14:32:24 -0400 | [diff] [blame] | 1452 | bitmap_zero(temps_used.l, nb_temps); |
Aurelien Jarno | a255066 | 2012-09-19 21:40:30 +0200 | [diff] [blame] | 1453 | } else { |
Richard Henderson | cf06667 | 2014-03-22 20:06:52 -0700 | [diff] [blame] | 1454 | do_reset_output: |
| 1455 | for (i = 0; i < nb_oargs; i++) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1456 | reset_temp(op->args[i]); |
Aurelien Jarno | 3031244 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 1457 | /* Save the corresponding known-zero bits mask for the |
| 1458 | first output argument (only one supported so far). */ |
| 1459 | if (i == 0) { |
Richard Henderson | 6349039 | 2017-06-20 13:43:15 -0700 | [diff] [blame] | 1460 | arg_info(op->args[i])->mask = mask; |
Aurelien Jarno | 3031244 | 2013-09-03 08:27:38 +0200 | [diff] [blame] | 1461 | } |
Aurelien Jarno | a255066 | 2012-09-19 21:40:30 +0200 | [diff] [blame] | 1462 | } |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 1463 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1464 | break; |
| 1465 | } |
Pranith Kumar | 34f9392 | 2016-08-23 09:48:25 -0400 | [diff] [blame] | 1466 | |
| 1467 | /* Eliminate duplicate and redundant fence instructions. */ |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1468 | if (prev_mb) { |
Pranith Kumar | 34f9392 | 2016-08-23 09:48:25 -0400 | [diff] [blame] | 1469 | switch (opc) { |
| 1470 | case INDEX_op_mb: |
| 1471 | /* Merge two barriers of the same type into one, |
| 1472 | * or a weaker barrier into a stronger one, |
| 1473 | * or two weaker barriers into a stronger one. |
| 1474 | * mb X; mb Y => mb X|Y |
| 1475 | * mb; strl => mb; st |
| 1476 | * ldaq; mb => ld; mb |
| 1477 | * ldaq; strl => ld; mb; st |
| 1478 | * Other combinations are also merged into a strong |
| 1479 | * barrier. This is stricter than specified but for |
| 1480 | * the purposes of TCG is better than not optimizing. |
| 1481 | */ |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1482 | prev_mb->args[0] |= op->args[0]; |
Pranith Kumar | 34f9392 | 2016-08-23 09:48:25 -0400 | [diff] [blame] | 1483 | tcg_op_remove(s, op); |
| 1484 | break; |
| 1485 | |
| 1486 | default: |
| 1487 | /* Opcodes that end the block stop the optimization. */ |
| 1488 | if ((def->flags & TCG_OPF_BB_END) == 0) { |
| 1489 | break; |
| 1490 | } |
| 1491 | /* fallthru */ |
| 1492 | case INDEX_op_qemu_ld_i32: |
| 1493 | case INDEX_op_qemu_ld_i64: |
| 1494 | case INDEX_op_qemu_st_i32: |
| 1495 | case INDEX_op_qemu_st_i64: |
| 1496 | case INDEX_op_call: |
| 1497 | /* Opcodes that touch guest memory stop the optimization. */ |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1498 | prev_mb = NULL; |
Pranith Kumar | 34f9392 | 2016-08-23 09:48:25 -0400 | [diff] [blame] | 1499 | break; |
| 1500 | } |
| 1501 | } else if (opc == INDEX_op_mb) { |
Richard Henderson | acd9370 | 2016-12-08 12:28:42 -0800 | [diff] [blame] | 1502 | prev_mb = op; |
Pranith Kumar | 34f9392 | 2016-08-23 09:48:25 -0400 | [diff] [blame] | 1503 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1504 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 1505 | } |