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