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