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