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