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