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