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