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; |
| 49 | }; |
| 50 | |
| 51 | static struct tcg_temp_info temps[TCG_MAX_TEMPS]; |
| 52 | |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 53 | /* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove |
| 54 | the copy flag from the left temp. */ |
| 55 | static void reset_temp(TCGArg temp) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 56 | { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 57 | if (temps[temp].state == TCG_TEMP_COPY) { |
| 58 | if (temps[temp].prev_copy == temps[temp].next_copy) { |
| 59 | temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF; |
| 60 | } else { |
| 61 | temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy; |
| 62 | temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 63 | } |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 64 | } |
Aurelien Jarno | 48b56ce | 2012-09-10 23:51:42 +0200 | [diff] [blame] | 65 | temps[temp].state = TCG_TEMP_UNDEF; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 66 | } |
| 67 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 68 | static int op_bits(TCGOpcode op) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 69 | { |
Richard Henderson | 8399ad5 | 2011-08-17 14:11:45 -0700 | [diff] [blame] | 70 | const TCGOpDef *def = &tcg_op_defs[op]; |
| 71 | return def->flags & TCG_OPF_64BIT ? 64 : 32; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 72 | } |
| 73 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 74 | static TCGOpcode op_to_movi(TCGOpcode op) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 75 | { |
| 76 | switch (op_bits(op)) { |
| 77 | case 32: |
| 78 | return INDEX_op_movi_i32; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 79 | case 64: |
| 80 | return INDEX_op_movi_i64; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 81 | default: |
| 82 | fprintf(stderr, "op_to_movi: unexpected return value of " |
| 83 | "function op_bits.\n"); |
| 84 | tcg_abort(); |
| 85 | } |
| 86 | } |
| 87 | |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 88 | static TCGArg find_better_copy(TCGContext *s, TCGArg temp) |
| 89 | { |
| 90 | TCGArg i; |
| 91 | |
| 92 | /* If this is already a global, we can't do better. */ |
| 93 | if (temp < s->nb_globals) { |
| 94 | return temp; |
| 95 | } |
| 96 | |
| 97 | /* Search for a global first. */ |
| 98 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { |
| 99 | if (i < s->nb_globals) { |
| 100 | return i; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* If it is a temp, search for a temp local. */ |
| 105 | if (!s->temps[temp].temp_local) { |
| 106 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { |
| 107 | if (s->temps[i].temp_local) { |
| 108 | return i; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Failure to find a better representation, return the same temp. */ |
| 114 | return temp; |
| 115 | } |
| 116 | |
| 117 | static bool temps_are_copies(TCGArg arg1, TCGArg arg2) |
| 118 | { |
| 119 | TCGArg i; |
| 120 | |
| 121 | if (arg1 == arg2) { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | if (temps[arg1].state != TCG_TEMP_COPY |
| 126 | || temps[arg2].state != TCG_TEMP_COPY) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) { |
| 131 | if (i == arg2) { |
| 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
Aurelien Jarno | b80bb01 | 2012-09-11 12:26:23 +0200 | [diff] [blame] | 139 | static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args, |
| 140 | TCGArg dst, TCGArg src) |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 141 | { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 142 | reset_temp(dst); |
| 143 | assert(temps[src].state != TCG_TEMP_CONST); |
| 144 | |
| 145 | if (s->temps[src].type == s->temps[dst].type) { |
| 146 | if (temps[src].state != TCG_TEMP_COPY) { |
| 147 | temps[src].state = TCG_TEMP_COPY; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 148 | temps[src].next_copy = src; |
| 149 | temps[src].prev_copy = src; |
| 150 | } |
| 151 | temps[dst].state = TCG_TEMP_COPY; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 152 | temps[dst].next_copy = temps[src].next_copy; |
| 153 | temps[dst].prev_copy = src; |
| 154 | temps[temps[dst].next_copy].prev_copy = dst; |
| 155 | temps[src].next_copy = dst; |
| 156 | } |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 157 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 158 | gen_args[0] = dst; |
| 159 | gen_args[1] = src; |
| 160 | } |
| 161 | |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 162 | 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] | 163 | { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 164 | reset_temp(dst); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 165 | temps[dst].state = TCG_TEMP_CONST; |
| 166 | temps[dst].val = val; |
| 167 | gen_args[0] = dst; |
| 168 | gen_args[1] = val; |
| 169 | } |
| 170 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 171 | static TCGOpcode op_to_mov(TCGOpcode op) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 172 | { |
| 173 | switch (op_bits(op)) { |
| 174 | case 32: |
| 175 | return INDEX_op_mov_i32; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 176 | case 64: |
| 177 | return INDEX_op_mov_i64; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 178 | default: |
| 179 | fprintf(stderr, "op_to_mov: unexpected return value of " |
| 180 | "function op_bits.\n"); |
| 181 | tcg_abort(); |
| 182 | } |
| 183 | } |
| 184 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 185 | static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 186 | { |
| 187 | switch (op) { |
| 188 | CASE_OP_32_64(add): |
| 189 | return x + y; |
| 190 | |
| 191 | CASE_OP_32_64(sub): |
| 192 | return x - y; |
| 193 | |
| 194 | CASE_OP_32_64(mul): |
| 195 | return x * y; |
| 196 | |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 197 | CASE_OP_32_64(and): |
| 198 | return x & y; |
| 199 | |
| 200 | CASE_OP_32_64(or): |
| 201 | return x | y; |
| 202 | |
| 203 | CASE_OP_32_64(xor): |
| 204 | return x ^ y; |
| 205 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 206 | case INDEX_op_shl_i32: |
| 207 | return (uint32_t)x << (uint32_t)y; |
| 208 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 209 | case INDEX_op_shl_i64: |
| 210 | return (uint64_t)x << (uint64_t)y; |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 211 | |
| 212 | case INDEX_op_shr_i32: |
| 213 | return (uint32_t)x >> (uint32_t)y; |
| 214 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 215 | case INDEX_op_shr_i64: |
| 216 | return (uint64_t)x >> (uint64_t)y; |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 217 | |
| 218 | case INDEX_op_sar_i32: |
| 219 | return (int32_t)x >> (int32_t)y; |
| 220 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 221 | case INDEX_op_sar_i64: |
| 222 | return (int64_t)x >> (int64_t)y; |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 223 | |
| 224 | case INDEX_op_rotr_i32: |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 225 | x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 226 | return x; |
| 227 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 228 | case INDEX_op_rotr_i64: |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 229 | x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 230 | return x; |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 231 | |
| 232 | case INDEX_op_rotl_i32: |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 233 | x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y)); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 234 | return x; |
| 235 | |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 236 | case INDEX_op_rotl_i64: |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 237 | x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y)); |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 238 | return x; |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 239 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 240 | CASE_OP_32_64(not): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 241 | return ~x; |
| 242 | |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 243 | CASE_OP_32_64(neg): |
| 244 | return -x; |
| 245 | |
| 246 | CASE_OP_32_64(andc): |
| 247 | return x & ~y; |
| 248 | |
| 249 | CASE_OP_32_64(orc): |
| 250 | return x | ~y; |
| 251 | |
| 252 | CASE_OP_32_64(eqv): |
| 253 | return ~(x ^ y); |
| 254 | |
| 255 | CASE_OP_32_64(nand): |
| 256 | return ~(x & y); |
| 257 | |
| 258 | CASE_OP_32_64(nor): |
| 259 | return ~(x | y); |
| 260 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 261 | CASE_OP_32_64(ext8s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 262 | return (int8_t)x; |
| 263 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 264 | CASE_OP_32_64(ext16s): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 265 | return (int16_t)x; |
| 266 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 267 | CASE_OP_32_64(ext8u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 268 | return (uint8_t)x; |
| 269 | |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 270 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 271 | return (uint16_t)x; |
| 272 | |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 273 | case INDEX_op_ext32s_i64: |
| 274 | return (int32_t)x; |
| 275 | |
| 276 | case INDEX_op_ext32u_i64: |
| 277 | return (uint32_t)x; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 278 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 279 | default: |
| 280 | fprintf(stderr, |
| 281 | "Unrecognized operation %d in do_constant_folding.\n", op); |
| 282 | tcg_abort(); |
| 283 | } |
| 284 | } |
| 285 | |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 286 | static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y) |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 287 | { |
| 288 | TCGArg res = do_constant_folding_2(op, x, y); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 289 | if (op_bits(op) == 32) { |
| 290 | res &= 0xffffffff; |
| 291 | } |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 292 | return res; |
| 293 | } |
| 294 | |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 295 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
| 296 | { |
| 297 | switch (c) { |
| 298 | case TCG_COND_EQ: |
| 299 | return x == y; |
| 300 | case TCG_COND_NE: |
| 301 | return x != y; |
| 302 | case TCG_COND_LT: |
| 303 | return (int32_t)x < (int32_t)y; |
| 304 | case TCG_COND_GE: |
| 305 | return (int32_t)x >= (int32_t)y; |
| 306 | case TCG_COND_LE: |
| 307 | return (int32_t)x <= (int32_t)y; |
| 308 | case TCG_COND_GT: |
| 309 | return (int32_t)x > (int32_t)y; |
| 310 | case TCG_COND_LTU: |
| 311 | return x < y; |
| 312 | case TCG_COND_GEU: |
| 313 | return x >= y; |
| 314 | case TCG_COND_LEU: |
| 315 | return x <= y; |
| 316 | case TCG_COND_GTU: |
| 317 | return x > y; |
| 318 | default: |
| 319 | tcg_abort(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static bool do_constant_folding_cond_64(uint64_t x, uint64_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 (int64_t)x < (int64_t)y; |
| 332 | case TCG_COND_GE: |
| 333 | return (int64_t)x >= (int64_t)y; |
| 334 | case TCG_COND_LE: |
| 335 | return (int64_t)x <= (int64_t)y; |
| 336 | case TCG_COND_GT: |
| 337 | return (int64_t)x > (int64_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_eq(TCGCond c) |
| 352 | { |
| 353 | switch (c) { |
| 354 | case TCG_COND_GT: |
| 355 | case TCG_COND_LTU: |
| 356 | case TCG_COND_LT: |
| 357 | case TCG_COND_GTU: |
| 358 | case TCG_COND_NE: |
| 359 | return 0; |
| 360 | case TCG_COND_GE: |
| 361 | case TCG_COND_GEU: |
| 362 | case TCG_COND_LE: |
| 363 | case TCG_COND_LEU: |
| 364 | case TCG_COND_EQ: |
| 365 | return 1; |
| 366 | default: |
| 367 | tcg_abort(); |
| 368 | } |
| 369 | } |
| 370 | |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 371 | /* Return 2 if the condition can't be simplified, and the result |
| 372 | of the condition (0 or 1) if it can */ |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 373 | static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x, |
| 374 | TCGArg y, TCGCond c) |
| 375 | { |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 376 | if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) { |
| 377 | switch (op_bits(op)) { |
| 378 | case 32: |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 379 | 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] | 380 | case 64: |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 381 | return do_constant_folding_cond_64(temps[x].val, temps[y].val, c); |
| 382 | default: |
| 383 | tcg_abort(); |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 384 | } |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 385 | } else if (temps_are_copies(x, y)) { |
Richard Henderson | 9519da7 | 2012-10-02 11:32:26 -0700 | [diff] [blame] | 386 | return do_constant_folding_cond_eq(c); |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 387 | } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) { |
| 388 | switch (c) { |
| 389 | case TCG_COND_LTU: |
| 390 | return 0; |
| 391 | case TCG_COND_GEU: |
| 392 | return 1; |
| 393 | default: |
| 394 | return 2; |
| 395 | } |
| 396 | } else { |
| 397 | return 2; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 398 | } |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 401 | /* Return 2 if the condition can't be simplified, and the result |
| 402 | of the condition (0 or 1) if it can */ |
| 403 | static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c) |
| 404 | { |
| 405 | TCGArg al = p1[0], ah = p1[1]; |
| 406 | TCGArg bl = p2[0], bh = p2[1]; |
| 407 | |
| 408 | if (temps[bl].state == TCG_TEMP_CONST |
| 409 | && temps[bh].state == TCG_TEMP_CONST) { |
| 410 | uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val; |
| 411 | |
| 412 | if (temps[al].state == TCG_TEMP_CONST |
| 413 | && temps[ah].state == TCG_TEMP_CONST) { |
| 414 | uint64_t a; |
| 415 | a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val; |
| 416 | return do_constant_folding_cond_64(a, b, c); |
| 417 | } |
| 418 | if (b == 0) { |
| 419 | switch (c) { |
| 420 | case TCG_COND_LTU: |
| 421 | return 0; |
| 422 | case TCG_COND_GEU: |
| 423 | return 1; |
| 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) { |
| 430 | return do_constant_folding_cond_eq(c); |
| 431 | } |
| 432 | return 2; |
| 433 | } |
| 434 | |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 435 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
| 436 | { |
| 437 | TCGArg a1 = *p1, a2 = *p2; |
| 438 | int sum = 0; |
| 439 | sum += temps[a1].state == TCG_TEMP_CONST; |
| 440 | sum -= temps[a2].state == TCG_TEMP_CONST; |
| 441 | |
| 442 | /* Prefer the constant in second argument, and then the form |
| 443 | op a, a, b, which is better handled on non-RISC hosts. */ |
| 444 | if (sum > 0 || (sum == 0 && dest == a2)) { |
| 445 | *p1 = a2; |
| 446 | *p2 = a1; |
| 447 | return true; |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 452 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
| 453 | { |
| 454 | int sum = 0; |
| 455 | sum += temps[p1[0]].state == TCG_TEMP_CONST; |
| 456 | sum += temps[p1[1]].state == TCG_TEMP_CONST; |
| 457 | sum -= temps[p2[0]].state == TCG_TEMP_CONST; |
| 458 | sum -= temps[p2[1]].state == TCG_TEMP_CONST; |
| 459 | if (sum > 0) { |
| 460 | TCGArg t; |
| 461 | t = p1[0], p1[0] = p2[0], p2[0] = t; |
| 462 | t = p1[1], p1[1] = p2[1], p2[1] = t; |
| 463 | return true; |
| 464 | } |
| 465 | return false; |
| 466 | } |
| 467 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 468 | /* Propagate constants and copies, fold constant expressions. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 469 | static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, |
| 470 | TCGArg *args, TCGOpDef *tcg_op_defs) |
| 471 | { |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 472 | int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args; |
| 473 | TCGOpcode op; |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 474 | const TCGOpDef *def; |
| 475 | TCGArg *gen_args; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 476 | TCGArg tmp; |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 477 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 478 | /* Array VALS has an element for each temp. |
| 479 | 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] | 480 | If this temp is a copy of other ones then the other copies are |
| 481 | available through the doubly linked circular list. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 482 | |
| 483 | nb_temps = s->nb_temps; |
| 484 | nb_globals = s->nb_globals; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 485 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 486 | |
| 487 | nb_ops = tcg_opc_ptr - gen_opc_buf; |
| 488 | gen_args = args; |
| 489 | for (op_index = 0; op_index < nb_ops; op_index++) { |
| 490 | op = gen_opc_buf[op_index]; |
| 491 | def = &tcg_op_defs[op]; |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 492 | /* Do copy propagation */ |
Aurelien Jarno | 1ff8c54 | 2012-09-11 16:18:49 +0200 | [diff] [blame] | 493 | if (op == INDEX_op_call) { |
| 494 | int nb_oargs = args[0] >> 16; |
| 495 | int nb_iargs = args[0] & 0xffff; |
| 496 | for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) { |
| 497 | if (temps[args[i]].state == TCG_TEMP_COPY) { |
| 498 | args[i] = find_better_copy(s, args[i]); |
| 499 | } |
| 500 | } |
| 501 | } else { |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 502 | for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) { |
| 503 | if (temps[args[i]].state == TCG_TEMP_COPY) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 504 | args[i] = find_better_copy(s, args[i]); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 509 | /* For commutative operations make constant second argument */ |
| 510 | switch (op) { |
| 511 | CASE_OP_32_64(add): |
| 512 | CASE_OP_32_64(mul): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 513 | CASE_OP_32_64(and): |
| 514 | CASE_OP_32_64(or): |
| 515 | CASE_OP_32_64(xor): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 516 | CASE_OP_32_64(eqv): |
| 517 | CASE_OP_32_64(nand): |
| 518 | CASE_OP_32_64(nor): |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 519 | swap_commutative(args[0], &args[1], &args[2]); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 520 | break; |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 521 | CASE_OP_32_64(brcond): |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 522 | if (swap_commutative(-1, &args[0], &args[1])) { |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 523 | args[2] = tcg_swap_cond(args[2]); |
| 524 | } |
| 525 | break; |
| 526 | CASE_OP_32_64(setcond): |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 527 | if (swap_commutative(args[0], &args[1], &args[2])) { |
Aurelien Jarno | 65a7cce | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 528 | args[3] = tcg_swap_cond(args[3]); |
| 529 | } |
| 530 | break; |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 531 | CASE_OP_32_64(movcond): |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 532 | if (swap_commutative(-1, &args[1], &args[2])) { |
| 533 | args[5] = tcg_swap_cond(args[5]); |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 534 | } |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 535 | /* For movcond, we canonicalize the "false" input reg to match |
| 536 | the destination reg so that the tcg backend can implement |
| 537 | a "move if true" operation. */ |
Richard Henderson | 24c9ae4 | 2012-10-02 11:32:21 -0700 | [diff] [blame] | 538 | if (swap_commutative(args[0], &args[4], &args[3])) { |
| 539 | args[5] = tcg_invert_cond(args[5]); |
Richard Henderson | 5d8f536 | 2012-09-21 10:13:38 -0700 | [diff] [blame] | 540 | } |
Richard Henderson | 1e484e6 | 2012-10-02 11:32:22 -0700 | [diff] [blame] | 541 | break; |
| 542 | case INDEX_op_add2_i32: |
| 543 | swap_commutative(args[0], &args[2], &args[4]); |
| 544 | swap_commutative(args[1], &args[3], &args[5]); |
| 545 | break; |
Richard Henderson | 0bfcb86 | 2012-10-02 11:32:23 -0700 | [diff] [blame] | 546 | case INDEX_op_brcond2_i32: |
| 547 | if (swap_commutative2(&args[0], &args[2])) { |
| 548 | args[4] = tcg_swap_cond(args[4]); |
| 549 | } |
| 550 | break; |
| 551 | case INDEX_op_setcond2_i32: |
| 552 | if (swap_commutative2(&args[1], &args[3])) { |
| 553 | args[5] = tcg_swap_cond(args[5]); |
| 554 | } |
| 555 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 556 | default: |
| 557 | break; |
| 558 | } |
| 559 | |
Aurelien Jarno | 01ee528 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 560 | /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */ |
| 561 | switch (op) { |
| 562 | CASE_OP_32_64(shl): |
| 563 | CASE_OP_32_64(shr): |
| 564 | CASE_OP_32_64(sar): |
| 565 | CASE_OP_32_64(rotl): |
| 566 | CASE_OP_32_64(rotr): |
| 567 | if (temps[args[1]].state == TCG_TEMP_CONST |
| 568 | && temps[args[1]].val == 0) { |
| 569 | gen_opc_buf[op_index] = op_to_movi(op); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 570 | tcg_opt_gen_movi(gen_args, args[0], 0); |
Aurelien Jarno | 01ee528 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 571 | args += 3; |
| 572 | gen_args += 2; |
| 573 | continue; |
| 574 | } |
| 575 | break; |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 580 | /* Simplify expression for "op r, a, 0 => mov r, a" cases */ |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 581 | switch (op) { |
| 582 | CASE_OP_32_64(add): |
| 583 | CASE_OP_32_64(sub): |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 584 | CASE_OP_32_64(shl): |
| 585 | CASE_OP_32_64(shr): |
| 586 | CASE_OP_32_64(sar): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 587 | CASE_OP_32_64(rotl): |
| 588 | CASE_OP_32_64(rotr): |
Aurelien Jarno | 38ee188 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 589 | CASE_OP_32_64(or): |
| 590 | CASE_OP_32_64(xor): |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 591 | if (temps[args[1]].state == TCG_TEMP_CONST) { |
| 592 | /* Proceed with possible constant folding. */ |
| 593 | break; |
| 594 | } |
| 595 | if (temps[args[2]].state == TCG_TEMP_CONST |
| 596 | && temps[args[2]].val == 0) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 597 | if (temps_are_copies(args[0], args[1])) { |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 598 | gen_opc_buf[op_index] = INDEX_op_nop; |
| 599 | } else { |
| 600 | gen_opc_buf[op_index] = op_to_mov(op); |
Aurelien Jarno | b80bb01 | 2012-09-11 12:26:23 +0200 | [diff] [blame] | 601 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 602 | gen_args += 2; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 603 | } |
Aurelien Jarno | fedc0da | 2012-09-07 12:24:32 +0200 | [diff] [blame] | 604 | args += 3; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 605 | continue; |
| 606 | } |
| 607 | break; |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 608 | default: |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | /* Simplify expression for "op r, a, 0 => movi r, 0" cases */ |
| 613 | switch (op) { |
Aurelien Jarno | 61251c0 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 614 | CASE_OP_32_64(and): |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 615 | CASE_OP_32_64(mul): |
| 616 | if ((temps[args[2]].state == TCG_TEMP_CONST |
| 617 | && temps[args[2]].val == 0)) { |
| 618 | gen_opc_buf[op_index] = op_to_movi(op); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 619 | tcg_opt_gen_movi(gen_args, args[0], 0); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 620 | args += 3; |
| 621 | gen_args += 2; |
| 622 | continue; |
| 623 | } |
| 624 | break; |
Aurelien Jarno | 56e4943 | 2012-09-06 16:47:13 +0200 | [diff] [blame] | 625 | default: |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | /* Simplify expression for "op r, a, a => mov r, a" cases */ |
| 630 | switch (op) { |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 631 | CASE_OP_32_64(or): |
| 632 | CASE_OP_32_64(and): |
Aurelien Jarno | 0aba1c7 | 2012-09-18 19:11:32 +0200 | [diff] [blame] | 633 | if (temps_are_copies(args[1], args[2])) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 634 | if (temps_are_copies(args[0], args[1])) { |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 635 | gen_opc_buf[op_index] = INDEX_op_nop; |
| 636 | } else { |
| 637 | gen_opc_buf[op_index] = op_to_mov(op); |
Aurelien Jarno | b80bb01 | 2012-09-11 12:26:23 +0200 | [diff] [blame] | 638 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 639 | gen_args += 2; |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 640 | } |
Aurelien Jarno | fedc0da | 2012-09-07 12:24:32 +0200 | [diff] [blame] | 641 | args += 3; |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 642 | continue; |
| 643 | } |
| 644 | break; |
Blue Swirl | fe0de7a | 2011-07-30 19:18:32 +0000 | [diff] [blame] | 645 | default: |
| 646 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 647 | } |
| 648 | |
Aurelien Jarno | 3c94193 | 2012-09-18 19:12:36 +0200 | [diff] [blame] | 649 | /* Simplify expression for "op r, a, a => movi r, 0" cases */ |
| 650 | switch (op) { |
| 651 | CASE_OP_32_64(sub): |
| 652 | CASE_OP_32_64(xor): |
| 653 | if (temps_are_copies(args[1], args[2])) { |
| 654 | gen_opc_buf[op_index] = op_to_movi(op); |
| 655 | tcg_opt_gen_movi(gen_args, args[0], 0); |
| 656 | gen_args += 2; |
| 657 | args += 3; |
| 658 | continue; |
| 659 | } |
| 660 | break; |
| 661 | default: |
| 662 | break; |
| 663 | } |
| 664 | |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 665 | /* Propagate constants through copy operations and do constant |
| 666 | folding. Constants will be substituted to arguments by register |
| 667 | allocator where needed and possible. Also detect copies. */ |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 668 | switch (op) { |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 669 | CASE_OP_32_64(mov): |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 670 | if (temps_are_copies(args[0], args[1])) { |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 671 | args += 2; |
| 672 | gen_opc_buf[op_index] = INDEX_op_nop; |
| 673 | break; |
| 674 | } |
| 675 | if (temps[args[1]].state != TCG_TEMP_CONST) { |
Aurelien Jarno | b80bb01 | 2012-09-11 12:26:23 +0200 | [diff] [blame] | 676 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 677 | gen_args += 2; |
| 678 | args += 2; |
| 679 | break; |
| 680 | } |
| 681 | /* Source argument is constant. Rewrite the operation and |
| 682 | let movi case handle it. */ |
| 683 | op = op_to_movi(op); |
| 684 | gen_opc_buf[op_index] = op; |
| 685 | args[1] = temps[args[1]].val; |
| 686 | /* fallthrough */ |
| 687 | CASE_OP_32_64(movi): |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 688 | tcg_opt_gen_movi(gen_args, args[0], args[1]); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 689 | gen_args += 2; |
| 690 | args += 2; |
| 691 | break; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 692 | |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 693 | CASE_OP_32_64(not): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 694 | CASE_OP_32_64(neg): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 695 | CASE_OP_32_64(ext8s): |
| 696 | CASE_OP_32_64(ext8u): |
| 697 | CASE_OP_32_64(ext16s): |
| 698 | CASE_OP_32_64(ext16u): |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 699 | case INDEX_op_ext32s_i64: |
| 700 | case INDEX_op_ext32u_i64: |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 701 | if (temps[args[1]].state == TCG_TEMP_CONST) { |
| 702 | gen_opc_buf[op_index] = op_to_movi(op); |
| 703 | tmp = do_constant_folding(op, temps[args[1]].val, 0); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 704 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 705 | gen_args += 2; |
| 706 | args += 2; |
| 707 | break; |
Kirill Batuzov | a640f03 | 2011-07-07 16:37:17 +0400 | [diff] [blame] | 708 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 709 | goto do_default; |
| 710 | |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 711 | CASE_OP_32_64(add): |
| 712 | CASE_OP_32_64(sub): |
| 713 | CASE_OP_32_64(mul): |
Kirill Batuzov | 9a81090 | 2011-07-07 16:37:15 +0400 | [diff] [blame] | 714 | CASE_OP_32_64(or): |
| 715 | CASE_OP_32_64(and): |
| 716 | CASE_OP_32_64(xor): |
Kirill Batuzov | 55c0975 | 2011-07-07 16:37:16 +0400 | [diff] [blame] | 717 | CASE_OP_32_64(shl): |
| 718 | CASE_OP_32_64(shr): |
| 719 | CASE_OP_32_64(sar): |
Richard Henderson | 25c4d9c | 2011-08-17 14:11:46 -0700 | [diff] [blame] | 720 | CASE_OP_32_64(rotl): |
| 721 | CASE_OP_32_64(rotr): |
Richard Henderson | cb25c80 | 2011-08-17 14:11:47 -0700 | [diff] [blame] | 722 | CASE_OP_32_64(andc): |
| 723 | CASE_OP_32_64(orc): |
| 724 | CASE_OP_32_64(eqv): |
| 725 | CASE_OP_32_64(nand): |
| 726 | CASE_OP_32_64(nor): |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 727 | if (temps[args[1]].state == TCG_TEMP_CONST |
| 728 | && temps[args[2]].state == TCG_TEMP_CONST) { |
| 729 | gen_opc_buf[op_index] = op_to_movi(op); |
| 730 | tmp = do_constant_folding(op, temps[args[1]].val, |
| 731 | temps[args[2]].val); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 732 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 733 | gen_args += 2; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 734 | args += 3; |
| 735 | break; |
Kirill Batuzov | 53108fb | 2011-07-07 16:37:14 +0400 | [diff] [blame] | 736 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 737 | goto do_default; |
| 738 | |
Aurelien Jarno | 7ef55fc | 2012-09-21 11:07:29 +0200 | [diff] [blame] | 739 | CASE_OP_32_64(deposit): |
| 740 | if (temps[args[1]].state == TCG_TEMP_CONST |
| 741 | && temps[args[2]].state == TCG_TEMP_CONST) { |
| 742 | gen_opc_buf[op_index] = op_to_movi(op); |
| 743 | tmp = ((1ull << args[4]) - 1); |
| 744 | tmp = (temps[args[1]].val & ~(tmp << args[3])) |
| 745 | | ((temps[args[2]].val & tmp) << args[3]); |
| 746 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
| 747 | gen_args += 2; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 748 | args += 5; |
| 749 | break; |
Aurelien Jarno | 7ef55fc | 2012-09-21 11:07:29 +0200 | [diff] [blame] | 750 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 751 | goto do_default; |
| 752 | |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 753 | CASE_OP_32_64(setcond): |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 754 | tmp = do_constant_folding_cond(op, args[1], args[2], args[3]); |
| 755 | if (tmp != 2) { |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 756 | gen_opc_buf[op_index] = op_to_movi(op); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 757 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 758 | gen_args += 2; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 759 | args += 4; |
| 760 | break; |
Aurelien Jarno | f8dd19e | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 761 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 762 | goto do_default; |
| 763 | |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 764 | CASE_OP_32_64(brcond): |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 765 | tmp = do_constant_folding_cond(op, args[0], args[1], args[2]); |
| 766 | if (tmp != 2) { |
| 767 | if (tmp) { |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 768 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
| 769 | gen_opc_buf[op_index] = INDEX_op_br; |
| 770 | gen_args[0] = args[3]; |
| 771 | gen_args += 1; |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 772 | } else { |
| 773 | gen_opc_buf[op_index] = INDEX_op_nop; |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 774 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 775 | args += 4; |
| 776 | break; |
Aurelien Jarno | fbeaa26 | 2012-09-06 16:47:14 +0200 | [diff] [blame] | 777 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 778 | goto do_default; |
| 779 | |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 780 | CASE_OP_32_64(movcond): |
Aurelien Jarno | b336ceb | 2012-09-18 19:37:00 +0200 | [diff] [blame] | 781 | tmp = do_constant_folding_cond(op, args[1], args[2], args[5]); |
| 782 | if (tmp != 2) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 783 | if (temps_are_copies(args[0], args[4-tmp])) { |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 784 | gen_opc_buf[op_index] = INDEX_op_nop; |
| 785 | } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) { |
| 786 | gen_opc_buf[op_index] = op_to_movi(op); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 787 | 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] | 788 | gen_args += 2; |
| 789 | } else { |
| 790 | gen_opc_buf[op_index] = op_to_mov(op); |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 791 | tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]); |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 792 | gen_args += 2; |
| 793 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 794 | args += 6; |
| 795 | break; |
Richard Henderson | fa01a20 | 2012-09-21 10:13:37 -0700 | [diff] [blame] | 796 | } |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 797 | goto do_default; |
| 798 | |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 799 | case INDEX_op_brcond2_i32: |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 800 | tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]); |
| 801 | if (tmp != 2) { |
| 802 | if (tmp) { |
| 803 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
| 804 | gen_opc_buf[op_index] = INDEX_op_br; |
| 805 | gen_args[0] = args[5]; |
| 806 | gen_args += 1; |
| 807 | } else { |
| 808 | gen_opc_buf[op_index] = INDEX_op_nop; |
| 809 | } |
| 810 | } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE) |
| 811 | && temps[args[2]].state == TCG_TEMP_CONST |
| 812 | && temps[args[3]].state == TCG_TEMP_CONST |
| 813 | && temps[args[2]].val == 0 |
| 814 | && temps[args[3]].val == 0) { |
| 815 | /* Simplify LT/GE comparisons vs zero to a single compare |
| 816 | vs the high word of the input. */ |
| 817 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 818 | gen_opc_buf[op_index] = INDEX_op_brcond_i32; |
| 819 | gen_args[0] = args[1]; |
| 820 | gen_args[1] = args[3]; |
| 821 | gen_args[2] = args[4]; |
| 822 | gen_args[3] = args[5]; |
| 823 | gen_args += 4; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 824 | } else { |
| 825 | goto do_default; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 826 | } |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 827 | args += 6; |
| 828 | break; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 829 | |
| 830 | case INDEX_op_setcond2_i32: |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 831 | tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]); |
| 832 | if (tmp != 2) { |
| 833 | gen_opc_buf[op_index] = INDEX_op_movi_i32; |
| 834 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
| 835 | gen_args += 2; |
| 836 | } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE) |
| 837 | && temps[args[3]].state == TCG_TEMP_CONST |
| 838 | && temps[args[4]].state == TCG_TEMP_CONST |
| 839 | && temps[args[3]].val == 0 |
| 840 | && temps[args[4]].val == 0) { |
| 841 | /* Simplify LT/GE comparisons vs zero to a single compare |
| 842 | vs the high word of the input. */ |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 843 | gen_opc_buf[op_index] = INDEX_op_setcond_i32; |
| 844 | gen_args[0] = args[0]; |
| 845 | gen_args[1] = args[2]; |
| 846 | gen_args[2] = args[4]; |
| 847 | gen_args[3] = args[5]; |
| 848 | gen_args += 4; |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 849 | } else { |
| 850 | goto do_default; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 851 | } |
Richard Henderson | 6c4382f | 2012-10-02 11:32:27 -0700 | [diff] [blame^] | 852 | args += 6; |
| 853 | break; |
Richard Henderson | bc1473e | 2012-10-02 11:32:25 -0700 | [diff] [blame] | 854 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 855 | case INDEX_op_call: |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 856 | nb_call_args = (args[0] >> 16) + (args[0] & 0xffff); |
| 857 | if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) { |
| 858 | for (i = 0; i < nb_globals; i++) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 859 | reset_temp(i); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | for (i = 0; i < (args[0] >> 16); i++) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 863 | reset_temp(args[i + 1]); |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 864 | } |
| 865 | i = nb_call_args + 3; |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 866 | while (i) { |
| 867 | *gen_args = *args; |
| 868 | args++; |
| 869 | gen_args++; |
| 870 | i--; |
| 871 | } |
| 872 | break; |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 873 | |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 874 | default: |
Richard Henderson | 6e14e91 | 2012-10-02 11:32:24 -0700 | [diff] [blame] | 875 | do_default: |
| 876 | /* Default case: we know nothing about operation (or were unable |
| 877 | to compute the operation result) so no propagation is done. |
| 878 | We trash everything if the operation is the end of a basic |
| 879 | block, otherwise we only trash the output args. */ |
Aurelien Jarno | a255066 | 2012-09-19 21:40:30 +0200 | [diff] [blame] | 880 | if (def->flags & TCG_OPF_BB_END) { |
| 881 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
| 882 | } else { |
| 883 | for (i = 0; i < def->nb_oargs; i++) { |
Aurelien Jarno | e590d4e | 2012-09-11 12:31:21 +0200 | [diff] [blame] | 884 | reset_temp(args[i]); |
Aurelien Jarno | a255066 | 2012-09-19 21:40:30 +0200 | [diff] [blame] | 885 | } |
Kirill Batuzov | 22613af | 2011-07-07 16:37:13 +0400 | [diff] [blame] | 886 | } |
Kirill Batuzov | 8f2e8c0 | 2011-07-07 16:37:12 +0400 | [diff] [blame] | 887 | for (i = 0; i < def->nb_args; i++) { |
| 888 | gen_args[i] = args[i]; |
| 889 | } |
| 890 | args += def->nb_args; |
| 891 | gen_args += def->nb_args; |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | return gen_args; |
| 897 | } |
| 898 | |
| 899 | TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, |
| 900 | TCGArg *args, TCGOpDef *tcg_op_defs) |
| 901 | { |
| 902 | TCGArg *res; |
| 903 | res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs); |
| 904 | return res; |
| 905 | } |