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