blob: 77da2f942af5c01cb94769ca59654f32eb7403ce [file] [log] [blame]
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001/*
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 Batuzov8f2e8c02011-07-07 16:37:12 +040034#define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040037
Kirill Batuzov22613af2011-07-07 16:37:13 +040038typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
Kirill Batuzov22613af2011-07-07 16:37:13 +040042} tcg_temp_state;
43
44struct tcg_temp_info {
45 tcg_temp_state state;
46 uint16_t prev_copy;
47 uint16_t next_copy;
48 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080049 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040050};
51
52static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020054/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
55 the copy flag from the left temp. */
56static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040057{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020058 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 Batuzov22613af2011-07-07 16:37:13 +040064 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040065 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020066 temps[temp].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080067 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040068}
69
Paolo Bonzinid193a142013-01-11 15:42:51 -080070/* Reset all temporaries, given that there are NB_TEMPS of them. */
71static 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 Bonzini3a9d8b12013-01-11 15:42:52 -080076 temps[i].mask = -1;
Paolo Bonzinid193a142013-01-11 15:42:51 -080077 }
78}
79
Blue Swirlfe0de7a2011-07-30 19:18:32 +000080static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040081{
Richard Henderson8399ad52011-08-17 14:11:45 -070082 const TCGOpDef *def = &tcg_op_defs[op];
83 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040084}
85
Richard Hendersona62f6f52014-05-22 10:59:12 -070086static TCGOpcode op_to_mov(TCGOpcode op)
87{
88 switch (op_bits(op)) {
89 case 32:
90 return INDEX_op_mov_i32;
91 case 64:
92 return INDEX_op_mov_i64;
93 default:
94 fprintf(stderr, "op_to_mov: unexpected return value of "
95 "function op_bits.\n");
96 tcg_abort();
97 }
98}
99
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000100static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400101{
102 switch (op_bits(op)) {
103 case 32:
104 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400105 case 64:
106 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400107 default:
108 fprintf(stderr, "op_to_movi: unexpected return value of "
109 "function op_bits.\n");
110 tcg_abort();
111 }
112}
113
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200114static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
115{
116 TCGArg i;
117
118 /* If this is already a global, we can't do better. */
119 if (temp < s->nb_globals) {
120 return temp;
121 }
122
123 /* Search for a global first. */
124 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
125 if (i < s->nb_globals) {
126 return i;
127 }
128 }
129
130 /* If it is a temp, search for a temp local. */
131 if (!s->temps[temp].temp_local) {
132 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
133 if (s->temps[i].temp_local) {
134 return i;
135 }
136 }
137 }
138
139 /* Failure to find a better representation, return the same temp. */
140 return temp;
141}
142
143static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
144{
145 TCGArg i;
146
147 if (arg1 == arg2) {
148 return true;
149 }
150
151 if (temps[arg1].state != TCG_TEMP_COPY
152 || temps[arg2].state != TCG_TEMP_COPY) {
153 return false;
154 }
155
156 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
157 if (i == arg2) {
158 return true;
159 }
160 }
161
162 return false;
163}
164
Richard Hendersona62f6f52014-05-22 10:59:12 -0700165static void tcg_opt_gen_mov(TCGContext *s, int op_index, TCGArg *gen_args,
166 TCGOpcode old_op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400167{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700168 TCGOpcode new_op = op_to_mov(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700169 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700170
171 s->gen_opc_buf[op_index] = new_op;
172
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800173 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700174 mask = temps[src].mask;
175 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
176 /* High bits of the destination are now garbage. */
177 mask |= ~0xffffffffull;
178 }
179 temps[dst].mask = mask;
180
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800181 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200182
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800183 if (s->temps[src].type == s->temps[dst].type) {
184 if (temps[src].state != TCG_TEMP_COPY) {
185 temps[src].state = TCG_TEMP_COPY;
186 temps[src].next_copy = src;
187 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400188 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800189 temps[dst].state = TCG_TEMP_COPY;
190 temps[dst].next_copy = temps[src].next_copy;
191 temps[dst].prev_copy = src;
192 temps[temps[dst].next_copy].prev_copy = dst;
193 temps[src].next_copy = dst;
194 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200195
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800196 gen_args[0] = dst;
197 gen_args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400198}
199
Richard Hendersona62f6f52014-05-22 10:59:12 -0700200static void tcg_opt_gen_movi(TCGContext *s, int op_index, TCGArg *gen_args,
201 TCGOpcode old_op, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400202{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700203 TCGOpcode new_op = op_to_movi(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700204 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700205
206 s->gen_opc_buf[op_index] = new_op;
207
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800208 reset_temp(dst);
209 temps[dst].state = TCG_TEMP_CONST;
210 temps[dst].val = val;
Richard Henderson24666ba2014-05-22 11:14:10 -0700211 mask = val;
212 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
213 /* High bits of the destination are now garbage. */
214 mask |= ~0xffffffffull;
215 }
216 temps[dst].mask = mask;
217
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800218 gen_args[0] = dst;
219 gen_args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400220}
221
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000222static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400223{
Richard Henderson03271522013-08-14 14:35:56 -0700224 uint64_t l64, h64;
225
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400226 switch (op) {
227 CASE_OP_32_64(add):
228 return x + y;
229
230 CASE_OP_32_64(sub):
231 return x - y;
232
233 CASE_OP_32_64(mul):
234 return x * y;
235
Kirill Batuzov9a810902011-07-07 16:37:15 +0400236 CASE_OP_32_64(and):
237 return x & y;
238
239 CASE_OP_32_64(or):
240 return x | y;
241
242 CASE_OP_32_64(xor):
243 return x ^ y;
244
Kirill Batuzov55c09752011-07-07 16:37:16 +0400245 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700246 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400247
Kirill Batuzov55c09752011-07-07 16:37:16 +0400248 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700249 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400250
251 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700252 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400253
Richard Henderson4bb7a412013-09-09 17:03:24 -0700254 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400255 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700256 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400257
258 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700259 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400260
Kirill Batuzov55c09752011-07-07 16:37:16 +0400261 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700262 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400263
264 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700265 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400266
Kirill Batuzov55c09752011-07-07 16:37:16 +0400267 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700268 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400269
270 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700271 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400272
Kirill Batuzov55c09752011-07-07 16:37:16 +0400273 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700274 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400275
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700276 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400277 return ~x;
278
Richard Hendersoncb25c802011-08-17 14:11:47 -0700279 CASE_OP_32_64(neg):
280 return -x;
281
282 CASE_OP_32_64(andc):
283 return x & ~y;
284
285 CASE_OP_32_64(orc):
286 return x | ~y;
287
288 CASE_OP_32_64(eqv):
289 return ~(x ^ y);
290
291 CASE_OP_32_64(nand):
292 return ~(x & y);
293
294 CASE_OP_32_64(nor):
295 return ~(x | y);
296
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700297 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400298 return (int8_t)x;
299
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700300 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400301 return (int16_t)x;
302
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700303 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400304 return (uint8_t)x;
305
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700306 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400307 return (uint16_t)x;
308
Kirill Batuzova640f032011-07-07 16:37:17 +0400309 case INDEX_op_ext32s_i64:
310 return (int32_t)x;
311
312 case INDEX_op_ext32u_i64:
313 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400314
Richard Henderson03271522013-08-14 14:35:56 -0700315 case INDEX_op_muluh_i32:
316 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
317 case INDEX_op_mulsh_i32:
318 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
319
320 case INDEX_op_muluh_i64:
321 mulu64(&l64, &h64, x, y);
322 return h64;
323 case INDEX_op_mulsh_i64:
324 muls64(&l64, &h64, x, y);
325 return h64;
326
Richard Henderson01547f72013-08-14 15:22:46 -0700327 case INDEX_op_div_i32:
328 /* Avoid crashing on divide by zero, otherwise undefined. */
329 return (int32_t)x / ((int32_t)y ? : 1);
330 case INDEX_op_divu_i32:
331 return (uint32_t)x / ((uint32_t)y ? : 1);
332 case INDEX_op_div_i64:
333 return (int64_t)x / ((int64_t)y ? : 1);
334 case INDEX_op_divu_i64:
335 return (uint64_t)x / ((uint64_t)y ? : 1);
336
337 case INDEX_op_rem_i32:
338 return (int32_t)x % ((int32_t)y ? : 1);
339 case INDEX_op_remu_i32:
340 return (uint32_t)x % ((uint32_t)y ? : 1);
341 case INDEX_op_rem_i64:
342 return (int64_t)x % ((int64_t)y ? : 1);
343 case INDEX_op_remu_i64:
344 return (uint64_t)x % ((uint64_t)y ? : 1);
345
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400346 default:
347 fprintf(stderr,
348 "Unrecognized operation %d in do_constant_folding.\n", op);
349 tcg_abort();
350 }
351}
352
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000353static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400354{
355 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400356 if (op_bits(op) == 32) {
357 res &= 0xffffffff;
358 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400359 return res;
360}
361
Richard Henderson9519da72012-10-02 11:32:26 -0700362static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
363{
364 switch (c) {
365 case TCG_COND_EQ:
366 return x == y;
367 case TCG_COND_NE:
368 return x != y;
369 case TCG_COND_LT:
370 return (int32_t)x < (int32_t)y;
371 case TCG_COND_GE:
372 return (int32_t)x >= (int32_t)y;
373 case TCG_COND_LE:
374 return (int32_t)x <= (int32_t)y;
375 case TCG_COND_GT:
376 return (int32_t)x > (int32_t)y;
377 case TCG_COND_LTU:
378 return x < y;
379 case TCG_COND_GEU:
380 return x >= y;
381 case TCG_COND_LEU:
382 return x <= y;
383 case TCG_COND_GTU:
384 return x > y;
385 default:
386 tcg_abort();
387 }
388}
389
390static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
391{
392 switch (c) {
393 case TCG_COND_EQ:
394 return x == y;
395 case TCG_COND_NE:
396 return x != y;
397 case TCG_COND_LT:
398 return (int64_t)x < (int64_t)y;
399 case TCG_COND_GE:
400 return (int64_t)x >= (int64_t)y;
401 case TCG_COND_LE:
402 return (int64_t)x <= (int64_t)y;
403 case TCG_COND_GT:
404 return (int64_t)x > (int64_t)y;
405 case TCG_COND_LTU:
406 return x < y;
407 case TCG_COND_GEU:
408 return x >= y;
409 case TCG_COND_LEU:
410 return x <= y;
411 case TCG_COND_GTU:
412 return x > y;
413 default:
414 tcg_abort();
415 }
416}
417
418static bool do_constant_folding_cond_eq(TCGCond c)
419{
420 switch (c) {
421 case TCG_COND_GT:
422 case TCG_COND_LTU:
423 case TCG_COND_LT:
424 case TCG_COND_GTU:
425 case TCG_COND_NE:
426 return 0;
427 case TCG_COND_GE:
428 case TCG_COND_GEU:
429 case TCG_COND_LE:
430 case TCG_COND_LEU:
431 case TCG_COND_EQ:
432 return 1;
433 default:
434 tcg_abort();
435 }
436}
437
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200438/* Return 2 if the condition can't be simplified, and the result
439 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200440static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
441 TCGArg y, TCGCond c)
442{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200443 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
444 switch (op_bits(op)) {
445 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700446 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200447 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700448 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
449 default:
450 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200451 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200452 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700453 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200454 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
455 switch (c) {
456 case TCG_COND_LTU:
457 return 0;
458 case TCG_COND_GEU:
459 return 1;
460 default:
461 return 2;
462 }
463 } else {
464 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200465 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200466}
467
Richard Henderson6c4382f2012-10-02 11:32:27 -0700468/* Return 2 if the condition can't be simplified, and the result
469 of the condition (0 or 1) if it can */
470static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
471{
472 TCGArg al = p1[0], ah = p1[1];
473 TCGArg bl = p2[0], bh = p2[1];
474
475 if (temps[bl].state == TCG_TEMP_CONST
476 && temps[bh].state == TCG_TEMP_CONST) {
477 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
478
479 if (temps[al].state == TCG_TEMP_CONST
480 && temps[ah].state == TCG_TEMP_CONST) {
481 uint64_t a;
482 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
483 return do_constant_folding_cond_64(a, b, c);
484 }
485 if (b == 0) {
486 switch (c) {
487 case TCG_COND_LTU:
488 return 0;
489 case TCG_COND_GEU:
490 return 1;
491 default:
492 break;
493 }
494 }
495 }
496 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
497 return do_constant_folding_cond_eq(c);
498 }
499 return 2;
500}
501
Richard Henderson24c9ae42012-10-02 11:32:21 -0700502static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
503{
504 TCGArg a1 = *p1, a2 = *p2;
505 int sum = 0;
506 sum += temps[a1].state == TCG_TEMP_CONST;
507 sum -= temps[a2].state == TCG_TEMP_CONST;
508
509 /* Prefer the constant in second argument, and then the form
510 op a, a, b, which is better handled on non-RISC hosts. */
511 if (sum > 0 || (sum == 0 && dest == a2)) {
512 *p1 = a2;
513 *p2 = a1;
514 return true;
515 }
516 return false;
517}
518
Richard Henderson0bfcb862012-10-02 11:32:23 -0700519static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
520{
521 int sum = 0;
522 sum += temps[p1[0]].state == TCG_TEMP_CONST;
523 sum += temps[p1[1]].state == TCG_TEMP_CONST;
524 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
525 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
526 if (sum > 0) {
527 TCGArg t;
528 t = p1[0], p1[0] = p2[0], p2[0] = t;
529 t = p1[1], p1[1] = p2[1], p2[1] = t;
530 return true;
531 }
532 return false;
533}
534
Kirill Batuzov22613af2011-07-07 16:37:13 +0400535/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400536static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
537 TCGArg *args, TCGOpDef *tcg_op_defs)
538{
Richard Hendersoncf066672014-03-22 20:06:52 -0700539 int nb_ops, op_index, nb_temps, nb_globals;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400540 TCGArg *gen_args;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700541
Kirill Batuzov22613af2011-07-07 16:37:13 +0400542 /* Array VALS has an element for each temp.
543 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200544 If this temp is a copy of other ones then the other copies are
545 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400546
547 nb_temps = s->nb_temps;
548 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800549 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400550
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400551 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400552 gen_args = args;
553 for (op_index = 0; op_index < nb_ops; op_index++) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700554 TCGOpcode op = s->gen_opc_buf[op_index];
555 const TCGOpDef *def = &tcg_op_defs[op];
Richard Henderson24666ba2014-05-22 11:14:10 -0700556 tcg_target_ulong mask, partmask, affected;
Richard Hendersoncf066672014-03-22 20:06:52 -0700557 int nb_oargs, nb_iargs, nb_args, i;
558 TCGArg tmp;
559
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200560 if (op == INDEX_op_call) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700561 *gen_args++ = tmp = *args++;
562 nb_oargs = tmp >> 16;
563 nb_iargs = tmp & 0xffff;
564 nb_args = nb_oargs + nb_iargs + def->nb_cargs;
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200565 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700566 nb_oargs = def->nb_oargs;
567 nb_iargs = def->nb_iargs;
568 nb_args = def->nb_args;
569 }
570
571 /* Do copy propagation */
572 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
573 if (temps[args[i]].state == TCG_TEMP_COPY) {
574 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400575 }
576 }
577
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400578 /* For commutative operations make constant second argument */
579 switch (op) {
580 CASE_OP_32_64(add):
581 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400582 CASE_OP_32_64(and):
583 CASE_OP_32_64(or):
584 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700585 CASE_OP_32_64(eqv):
586 CASE_OP_32_64(nand):
587 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700588 CASE_OP_32_64(muluh):
589 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700590 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400591 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200592 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700593 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200594 args[2] = tcg_swap_cond(args[2]);
595 }
596 break;
597 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700598 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200599 args[3] = tcg_swap_cond(args[3]);
600 }
601 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700602 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700603 if (swap_commutative(-1, &args[1], &args[2])) {
604 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700605 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700606 /* For movcond, we canonicalize the "false" input reg to match
607 the destination reg so that the tcg backend can implement
608 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700609 if (swap_commutative(args[0], &args[4], &args[3])) {
610 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700611 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700612 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800613 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700614 swap_commutative(args[0], &args[2], &args[4]);
615 swap_commutative(args[1], &args[3], &args[5]);
616 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800617 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800618 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700619 swap_commutative(args[0], &args[2], &args[3]);
620 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700621 case INDEX_op_brcond2_i32:
622 if (swap_commutative2(&args[0], &args[2])) {
623 args[4] = tcg_swap_cond(args[4]);
624 }
625 break;
626 case INDEX_op_setcond2_i32:
627 if (swap_commutative2(&args[1], &args[3])) {
628 args[5] = tcg_swap_cond(args[5]);
629 }
630 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400631 default:
632 break;
633 }
634
Richard Henderson2d497542013-03-21 09:13:33 -0700635 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
636 and "sub r, 0, a => neg r, a" case. */
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200637 switch (op) {
638 CASE_OP_32_64(shl):
639 CASE_OP_32_64(shr):
640 CASE_OP_32_64(sar):
641 CASE_OP_32_64(rotl):
642 CASE_OP_32_64(rotr):
643 if (temps[args[1]].state == TCG_TEMP_CONST
644 && temps[args[1]].val == 0) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700645 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200646 args += 3;
647 gen_args += 2;
648 continue;
649 }
650 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700651 CASE_OP_32_64(sub):
652 {
653 TCGOpcode neg_op;
654 bool have_neg;
655
656 if (temps[args[2]].state == TCG_TEMP_CONST) {
657 /* Proceed with possible constant folding. */
658 break;
659 }
660 if (op == INDEX_op_sub_i32) {
661 neg_op = INDEX_op_neg_i32;
662 have_neg = TCG_TARGET_HAS_neg_i32;
663 } else {
664 neg_op = INDEX_op_neg_i64;
665 have_neg = TCG_TARGET_HAS_neg_i64;
666 }
667 if (!have_neg) {
668 break;
669 }
670 if (temps[args[1]].state == TCG_TEMP_CONST
671 && temps[args[1]].val == 0) {
672 s->gen_opc_buf[op_index] = neg_op;
673 reset_temp(args[0]);
674 gen_args[0] = args[0];
675 gen_args[1] = args[2];
676 args += 3;
677 gen_args += 2;
678 continue;
679 }
680 }
681 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800682 CASE_OP_32_64(xor):
683 CASE_OP_32_64(nand):
684 if (temps[args[1]].state != TCG_TEMP_CONST
685 && temps[args[2]].state == TCG_TEMP_CONST
686 && temps[args[2]].val == -1) {
687 i = 1;
688 goto try_not;
689 }
690 break;
691 CASE_OP_32_64(nor):
692 if (temps[args[1]].state != TCG_TEMP_CONST
693 && temps[args[2]].state == TCG_TEMP_CONST
694 && temps[args[2]].val == 0) {
695 i = 1;
696 goto try_not;
697 }
698 break;
699 CASE_OP_32_64(andc):
700 if (temps[args[2]].state != TCG_TEMP_CONST
701 && temps[args[1]].state == TCG_TEMP_CONST
702 && temps[args[1]].val == -1) {
703 i = 2;
704 goto try_not;
705 }
706 break;
707 CASE_OP_32_64(orc):
708 CASE_OP_32_64(eqv):
709 if (temps[args[2]].state != TCG_TEMP_CONST
710 && temps[args[1]].state == TCG_TEMP_CONST
711 && temps[args[1]].val == 0) {
712 i = 2;
713 goto try_not;
714 }
715 break;
716 try_not:
717 {
718 TCGOpcode not_op;
719 bool have_not;
720
721 if (def->flags & TCG_OPF_64BIT) {
722 not_op = INDEX_op_not_i64;
723 have_not = TCG_TARGET_HAS_not_i64;
724 } else {
725 not_op = INDEX_op_not_i32;
726 have_not = TCG_TARGET_HAS_not_i32;
727 }
728 if (!have_not) {
729 break;
730 }
731 s->gen_opc_buf[op_index] = not_op;
732 reset_temp(args[0]);
733 gen_args[0] = args[0];
734 gen_args[1] = args[i];
735 args += 3;
736 gen_args += 2;
737 continue;
738 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200739 default:
740 break;
741 }
742
Richard Henderson464a1442014-01-31 07:42:11 -0600743 /* Simplify expression for "op r, a, const => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400744 switch (op) {
745 CASE_OP_32_64(add):
746 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400747 CASE_OP_32_64(shl):
748 CASE_OP_32_64(shr):
749 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700750 CASE_OP_32_64(rotl):
751 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200752 CASE_OP_32_64(or):
753 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600754 CASE_OP_32_64(andc):
755 if (temps[args[1]].state != TCG_TEMP_CONST
756 && temps[args[2]].state == TCG_TEMP_CONST
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400757 && temps[args[2]].val == 0) {
Richard Henderson464a1442014-01-31 07:42:11 -0600758 goto do_mov3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400759 }
760 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600761 CASE_OP_32_64(and):
762 CASE_OP_32_64(orc):
763 CASE_OP_32_64(eqv):
764 if (temps[args[1]].state != TCG_TEMP_CONST
765 && temps[args[2]].state == TCG_TEMP_CONST
766 && temps[args[2]].val == -1) {
767 goto do_mov3;
768 }
769 break;
770 do_mov3:
771 if (temps_are_copies(args[0], args[1])) {
772 s->gen_opc_buf[op_index] = INDEX_op_nop;
773 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700774 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Richard Henderson464a1442014-01-31 07:42:11 -0600775 gen_args += 2;
776 }
777 args += 3;
778 continue;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200779 default:
780 break;
781 }
782
Aurelien Jarno30312442013-09-03 08:27:38 +0200783 /* Simplify using known-zero bits. Currently only ops with a single
784 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800785 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800786 affected = -1;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800787 switch (op) {
788 CASE_OP_32_64(ext8s):
789 if ((temps[args[1]].mask & 0x80) != 0) {
790 break;
791 }
792 CASE_OP_32_64(ext8u):
793 mask = 0xff;
794 goto and_const;
795 CASE_OP_32_64(ext16s):
796 if ((temps[args[1]].mask & 0x8000) != 0) {
797 break;
798 }
799 CASE_OP_32_64(ext16u):
800 mask = 0xffff;
801 goto and_const;
802 case INDEX_op_ext32s_i64:
803 if ((temps[args[1]].mask & 0x80000000) != 0) {
804 break;
805 }
806 case INDEX_op_ext32u_i64:
807 mask = 0xffffffffU;
808 goto and_const;
809
810 CASE_OP_32_64(and):
811 mask = temps[args[2]].mask;
812 if (temps[args[2]].state == TCG_TEMP_CONST) {
813 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800814 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800815 }
816 mask = temps[args[1]].mask & mask;
817 break;
818
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800819 CASE_OP_32_64(andc):
820 /* Known-zeros does not imply known-ones. Therefore unless
821 args[2] is constant, we can't infer anything from it. */
822 if (temps[args[2]].state == TCG_TEMP_CONST) {
823 mask = ~temps[args[2]].mask;
824 goto and_const;
825 }
826 /* But we certainly know nothing outside args[1] may be set. */
827 mask = temps[args[1]].mask;
828 break;
829
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200830 case INDEX_op_sar_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800831 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700832 tmp = temps[args[2]].val & 31;
833 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200834 }
835 break;
836 case INDEX_op_sar_i64:
837 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700838 tmp = temps[args[2]].val & 63;
839 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800840 }
841 break;
842
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200843 case INDEX_op_shr_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800844 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700845 tmp = temps[args[2]].val & 31;
846 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200847 }
848 break;
849 case INDEX_op_shr_i64:
850 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700851 tmp = temps[args[2]].val & 63;
852 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800853 }
854 break;
855
Richard Henderson4bb7a412013-09-09 17:03:24 -0700856 case INDEX_op_trunc_shr_i32:
857 mask = (uint64_t)temps[args[1]].mask >> args[2];
858 break;
859
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800860 CASE_OP_32_64(shl):
861 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700862 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
863 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800864 }
865 break;
866
867 CASE_OP_32_64(neg):
868 /* Set to 1 all bits to the left of the rightmost. */
869 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
870 break;
871
872 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700873 mask = deposit64(temps[args[1]].mask, args[3], args[4],
874 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800875 break;
876
877 CASE_OP_32_64(or):
878 CASE_OP_32_64(xor):
879 mask = temps[args[1]].mask | temps[args[2]].mask;
880 break;
881
882 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700883 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800884 mask = 1;
885 break;
886
887 CASE_OP_32_64(movcond):
888 mask = temps[args[3]].mask | temps[args[4]].mask;
889 break;
890
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200891 CASE_OP_32_64(ld8u):
892 case INDEX_op_qemu_ld8u:
893 mask = 0xff;
894 break;
895 CASE_OP_32_64(ld16u):
896 case INDEX_op_qemu_ld16u:
897 mask = 0xffff;
898 break;
899 case INDEX_op_ld32u_i64:
900#if TCG_TARGET_REG_BITS == 64
901 case INDEX_op_qemu_ld32u:
902#endif
903 mask = 0xffffffffu;
904 break;
905
906 CASE_OP_32_64(qemu_ld):
907 {
Richard Hendersoncf066672014-03-22 20:06:52 -0700908 TCGMemOp mop = args[nb_oargs + nb_iargs];
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200909 if (!(mop & MO_SIGN)) {
910 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
911 }
912 }
913 break;
914
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800915 default:
916 break;
917 }
918
Richard Henderson24666ba2014-05-22 11:14:10 -0700919 /* 32-bit ops (non 64-bit ops and non load/store ops) generate
920 32-bit results. For the result is zero test below, we can
921 ignore high bits, but for further optimizations we need to
922 record that the high bits contain garbage. */
923 partmask = mask;
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200924 if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_64BIT))) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700925 mask |= ~(tcg_target_ulong)0xffffffffu;
926 partmask &= 0xffffffffu;
927 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200928 }
929
Richard Henderson24666ba2014-05-22 11:14:10 -0700930 if (partmask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700931 assert(nb_oargs == 1);
Richard Hendersona62f6f52014-05-22 10:59:12 -0700932 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Richard Hendersoncf066672014-03-22 20:06:52 -0700933 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800934 gen_args += 2;
935 continue;
936 }
937 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700938 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800939 if (temps_are_copies(args[0], args[1])) {
940 s->gen_opc_buf[op_index] = INDEX_op_nop;
941 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700942 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800943 gen_args += 2;
944 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700945 tcg_opt_gen_movi(s, op_index, gen_args, op,
946 args[0], temps[args[1]].val);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800947 gen_args += 2;
948 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700949 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800950 continue;
951 }
952
Aurelien Jarno56e49432012-09-06 16:47:13 +0200953 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
954 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200955 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400956 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700957 CASE_OP_32_64(muluh):
958 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400959 if ((temps[args[2]].state == TCG_TEMP_CONST
960 && temps[args[2]].val == 0)) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700961 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400962 args += 3;
963 gen_args += 2;
964 continue;
965 }
966 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200967 default:
968 break;
969 }
970
971 /* Simplify expression for "op r, a, a => mov r, a" cases */
972 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400973 CASE_OP_32_64(or):
974 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200975 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200976 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400977 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400978 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700979 tcg_opt_gen_mov(s, op_index, gen_args, op,
980 args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400981 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400982 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200983 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400984 continue;
985 }
986 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000987 default:
988 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400989 }
990
Aurelien Jarno3c941932012-09-18 19:12:36 +0200991 /* Simplify expression for "op r, a, a => movi r, 0" cases */
992 switch (op) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800993 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +0200994 CASE_OP_32_64(sub):
995 CASE_OP_32_64(xor):
996 if (temps_are_copies(args[1], args[2])) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700997 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200998 gen_args += 2;
999 args += 3;
1000 continue;
1001 }
1002 break;
1003 default:
1004 break;
1005 }
1006
Kirill Batuzov22613af2011-07-07 16:37:13 +04001007 /* Propagate constants through copy operations and do constant
1008 folding. Constants will be substituted to arguments by register
1009 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001010 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001011 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001012 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001013 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001014 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001015 break;
1016 }
1017 if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001018 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001019 gen_args += 2;
1020 args += 2;
1021 break;
1022 }
1023 /* Source argument is constant. Rewrite the operation and
1024 let movi case handle it. */
Kirill Batuzov22613af2011-07-07 16:37:13 +04001025 args[1] = temps[args[1]].val;
1026 /* fallthrough */
1027 CASE_OP_32_64(movi):
Richard Hendersona62f6f52014-05-22 10:59:12 -07001028 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001029 gen_args += 2;
1030 args += 2;
1031 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001032
Kirill Batuzova640f032011-07-07 16:37:17 +04001033 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001034 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001035 CASE_OP_32_64(ext8s):
1036 CASE_OP_32_64(ext8u):
1037 CASE_OP_32_64(ext16s):
1038 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001039 case INDEX_op_ext32s_i64:
1040 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +04001041 if (temps[args[1]].state == TCG_TEMP_CONST) {
Kirill Batuzova640f032011-07-07 16:37:17 +04001042 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001043 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001044 gen_args += 2;
1045 args += 2;
1046 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001047 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001048 goto do_default;
1049
Richard Henderson4bb7a412013-09-09 17:03:24 -07001050 case INDEX_op_trunc_shr_i32:
1051 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Henderson4bb7a412013-09-09 17:03:24 -07001052 tmp = do_constant_folding(op, temps[args[1]].val, args[2]);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001053 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001054 gen_args += 2;
1055 args += 3;
1056 break;
1057 }
1058 goto do_default;
1059
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001060 CASE_OP_32_64(add):
1061 CASE_OP_32_64(sub):
1062 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001063 CASE_OP_32_64(or):
1064 CASE_OP_32_64(and):
1065 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001066 CASE_OP_32_64(shl):
1067 CASE_OP_32_64(shr):
1068 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001069 CASE_OP_32_64(rotl):
1070 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001071 CASE_OP_32_64(andc):
1072 CASE_OP_32_64(orc):
1073 CASE_OP_32_64(eqv):
1074 CASE_OP_32_64(nand):
1075 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001076 CASE_OP_32_64(muluh):
1077 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001078 CASE_OP_32_64(div):
1079 CASE_OP_32_64(divu):
1080 CASE_OP_32_64(rem):
1081 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001082 if (temps[args[1]].state == TCG_TEMP_CONST
1083 && temps[args[2]].state == TCG_TEMP_CONST) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001084 tmp = do_constant_folding(op, temps[args[1]].val,
1085 temps[args[2]].val);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001086 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001087 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001088 args += 3;
1089 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001090 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001091 goto do_default;
1092
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001093 CASE_OP_32_64(deposit):
1094 if (temps[args[1]].state == TCG_TEMP_CONST
1095 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersond998e552014-03-18 14:23:52 -07001096 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1097 temps[args[2]].val);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001098 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001099 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001100 args += 5;
1101 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001102 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001103 goto do_default;
1104
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001105 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001106 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
1107 if (tmp != 2) {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001108 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001109 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001110 args += 4;
1111 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001112 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001113 goto do_default;
1114
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001115 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001116 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
1117 if (tmp != 2) {
1118 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001119 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001120 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001121 gen_args[0] = args[3];
1122 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001123 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001124 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001125 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001126 args += 4;
1127 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001128 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001129 goto do_default;
1130
Richard Hendersonfa01a202012-09-21 10:13:37 -07001131 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001132 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
1133 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001134 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001135 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001136 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001137 tcg_opt_gen_movi(s, op_index, gen_args, op,
1138 args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001139 gen_args += 2;
1140 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001141 tcg_opt_gen_mov(s, op_index, gen_args, op,
1142 args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001143 gen_args += 2;
1144 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001145 args += 6;
1146 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001147 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001148 goto do_default;
1149
Richard Henderson212c3282012-10-02 11:32:28 -07001150 case INDEX_op_add2_i32:
1151 case INDEX_op_sub2_i32:
1152 if (temps[args[2]].state == TCG_TEMP_CONST
1153 && temps[args[3]].state == TCG_TEMP_CONST
1154 && temps[args[4]].state == TCG_TEMP_CONST
1155 && temps[args[5]].state == TCG_TEMP_CONST) {
1156 uint32_t al = temps[args[2]].val;
1157 uint32_t ah = temps[args[3]].val;
1158 uint32_t bl = temps[args[4]].val;
1159 uint32_t bh = temps[args[5]].val;
1160 uint64_t a = ((uint64_t)ah << 32) | al;
1161 uint64_t b = ((uint64_t)bh << 32) | bl;
1162 TCGArg rl, rh;
1163
1164 if (op == INDEX_op_add2_i32) {
1165 a += b;
1166 } else {
1167 a -= b;
1168 }
1169
1170 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001171 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -07001172
1173 rl = args[0];
1174 rh = args[1];
Richard Hendersona62f6f52014-05-22 10:59:12 -07001175 tcg_opt_gen_movi(s, op_index, &gen_args[0],
1176 op, rl, (uint32_t)a);
1177 tcg_opt_gen_movi(s, ++op_index, &gen_args[2],
1178 op, rh, (uint32_t)(a >> 32));
Richard Henderson212c3282012-10-02 11:32:28 -07001179 gen_args += 4;
1180 args += 6;
1181 break;
1182 }
1183 goto do_default;
1184
Richard Henderson14149682012-10-02 11:32:30 -07001185 case INDEX_op_mulu2_i32:
1186 if (temps[args[2]].state == TCG_TEMP_CONST
1187 && temps[args[3]].state == TCG_TEMP_CONST) {
1188 uint32_t a = temps[args[2]].val;
1189 uint32_t b = temps[args[3]].val;
1190 uint64_t r = (uint64_t)a * b;
1191 TCGArg rl, rh;
1192
1193 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001194 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -07001195
1196 rl = args[0];
1197 rh = args[1];
Richard Hendersona62f6f52014-05-22 10:59:12 -07001198 tcg_opt_gen_movi(s, op_index, &gen_args[0],
1199 op, rl, (uint32_t)r);
1200 tcg_opt_gen_movi(s, ++op_index, &gen_args[2],
1201 op, rh, (uint32_t)(r >> 32));
Richard Henderson14149682012-10-02 11:32:30 -07001202 gen_args += 4;
1203 args += 4;
1204 break;
1205 }
1206 goto do_default;
1207
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001208 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001209 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1210 if (tmp != 2) {
1211 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001212 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001213 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001214 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001215 gen_args[0] = args[5];
1216 gen_args += 1;
1217 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001218 do_brcond_false:
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001219 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001220 }
1221 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1222 && temps[args[2]].state == TCG_TEMP_CONST
1223 && temps[args[3]].state == TCG_TEMP_CONST
1224 && temps[args[2]].val == 0
1225 && temps[args[3]].val == 0) {
1226 /* Simplify LT/GE comparisons vs zero to a single compare
1227 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001228 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001229 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001230 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001231 gen_args[0] = args[1];
1232 gen_args[1] = args[3];
1233 gen_args[2] = args[4];
1234 gen_args[3] = args[5];
1235 gen_args += 4;
Richard Hendersona7635512014-04-23 22:18:30 -07001236 } else if (args[4] == TCG_COND_EQ) {
1237 /* Simplify EQ comparisons where one of the pairs
1238 can be simplified. */
1239 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1240 args[0], args[2], TCG_COND_EQ);
1241 if (tmp == 0) {
1242 goto do_brcond_false;
1243 } else if (tmp == 1) {
1244 goto do_brcond_high;
1245 }
1246 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1247 args[1], args[3], TCG_COND_EQ);
1248 if (tmp == 0) {
1249 goto do_brcond_false;
1250 } else if (tmp != 1) {
1251 goto do_default;
1252 }
1253 do_brcond_low:
1254 reset_all_temps(nb_temps);
1255 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
1256 gen_args[0] = args[0];
1257 gen_args[1] = args[2];
1258 gen_args[2] = args[4];
1259 gen_args[3] = args[5];
1260 gen_args += 4;
1261 } else if (args[4] == TCG_COND_NE) {
1262 /* Simplify NE comparisons where one of the pairs
1263 can be simplified. */
1264 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1265 args[0], args[2], TCG_COND_NE);
1266 if (tmp == 0) {
1267 goto do_brcond_high;
1268 } else if (tmp == 1) {
1269 goto do_brcond_true;
1270 }
1271 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1272 args[1], args[3], TCG_COND_NE);
1273 if (tmp == 0) {
1274 goto do_brcond_low;
1275 } else if (tmp == 1) {
1276 goto do_brcond_true;
1277 }
1278 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001279 } else {
1280 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001281 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001282 args += 6;
1283 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001284
1285 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001286 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1287 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001288 do_setcond_const:
Richard Hendersona62f6f52014-05-22 10:59:12 -07001289 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001290 gen_args += 2;
1291 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1292 && temps[args[3]].state == TCG_TEMP_CONST
1293 && temps[args[4]].state == TCG_TEMP_CONST
1294 && temps[args[3]].val == 0
1295 && temps[args[4]].val == 0) {
1296 /* Simplify LT/GE comparisons vs zero to a single compare
1297 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001298 do_setcond_high:
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001299 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001300 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001301 temps[args[0]].mask = 1;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001302 gen_args[0] = args[0];
1303 gen_args[1] = args[2];
1304 gen_args[2] = args[4];
1305 gen_args[3] = args[5];
1306 gen_args += 4;
Richard Hendersona7635512014-04-23 22:18:30 -07001307 } else if (args[5] == TCG_COND_EQ) {
1308 /* Simplify EQ comparisons where one of the pairs
1309 can be simplified. */
1310 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1311 args[1], args[3], TCG_COND_EQ);
1312 if (tmp == 0) {
1313 goto do_setcond_const;
1314 } else if (tmp == 1) {
1315 goto do_setcond_high;
1316 }
1317 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1318 args[2], args[4], TCG_COND_EQ);
1319 if (tmp == 0) {
1320 goto do_setcond_high;
1321 } else if (tmp != 1) {
1322 goto do_default;
1323 }
1324 do_setcond_low:
1325 reset_temp(args[0]);
1326 temps[args[0]].mask = 1;
1327 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
1328 gen_args[0] = args[0];
1329 gen_args[1] = args[1];
1330 gen_args[2] = args[3];
1331 gen_args[3] = args[5];
1332 gen_args += 4;
1333 } else if (args[5] == TCG_COND_NE) {
1334 /* Simplify NE comparisons where one of the pairs
1335 can be simplified. */
1336 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1337 args[1], args[3], TCG_COND_NE);
1338 if (tmp == 0) {
1339 goto do_setcond_high;
1340 } else if (tmp == 1) {
1341 goto do_setcond_const;
1342 }
1343 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1344 args[2], args[4], TCG_COND_NE);
1345 if (tmp == 0) {
1346 goto do_setcond_low;
1347 } else if (tmp == 1) {
1348 goto do_setcond_const;
1349 }
1350 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001351 } else {
1352 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001353 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001354 args += 6;
1355 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001356
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001357 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001358 if (!(args[nb_oargs + nb_iargs + 1]
1359 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001360 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001361 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001362 }
1363 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001364 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001365
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001366 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001367 do_default:
1368 /* Default case: we know nothing about operation (or were unable
1369 to compute the operation result) so no propagation is done.
1370 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001371 block, otherwise we only trash the output args. "mask" is
1372 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001373 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001374 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001375 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001376 do_reset_output:
1377 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001378 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001379 /* Save the corresponding known-zero bits mask for the
1380 first output argument (only one supported so far). */
1381 if (i == 0) {
1382 temps[args[i]].mask = mask;
1383 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001384 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001385 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001386 for (i = 0; i < nb_args; i++) {
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001387 gen_args[i] = args[i];
1388 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001389 args += nb_args;
1390 gen_args += nb_args;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001391 break;
1392 }
1393 }
1394
1395 return gen_args;
1396}
1397
1398TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1399 TCGArg *args, TCGOpDef *tcg_op_defs)
1400{
1401 TCGArg *res;
1402 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1403 return res;
1404}