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