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