blob: bcd500c6aa02cf2f306d73b442a0ce8691c1f812 [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);
169
170 s->gen_opc_buf[op_index] = new_op;
171
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800172 reset_temp(dst);
173 temps[dst].mask = temps[src].mask;
174 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200175
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800176 if (s->temps[src].type == s->temps[dst].type) {
177 if (temps[src].state != TCG_TEMP_COPY) {
178 temps[src].state = TCG_TEMP_COPY;
179 temps[src].next_copy = src;
180 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400181 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800182 temps[dst].state = TCG_TEMP_COPY;
183 temps[dst].next_copy = temps[src].next_copy;
184 temps[dst].prev_copy = src;
185 temps[temps[dst].next_copy].prev_copy = dst;
186 temps[src].next_copy = dst;
187 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200188
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800189 gen_args[0] = dst;
190 gen_args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400191}
192
Richard Hendersona62f6f52014-05-22 10:59:12 -0700193static void tcg_opt_gen_movi(TCGContext *s, int op_index, TCGArg *gen_args,
194 TCGOpcode old_op, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400195{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700196 TCGOpcode new_op = op_to_movi(old_op);
197
198 s->gen_opc_buf[op_index] = new_op;
199
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800200 reset_temp(dst);
201 temps[dst].state = TCG_TEMP_CONST;
202 temps[dst].val = val;
203 temps[dst].mask = val;
204 gen_args[0] = dst;
205 gen_args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400206}
207
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000208static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400209{
Richard Henderson03271522013-08-14 14:35:56 -0700210 uint64_t l64, h64;
211
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400212 switch (op) {
213 CASE_OP_32_64(add):
214 return x + y;
215
216 CASE_OP_32_64(sub):
217 return x - y;
218
219 CASE_OP_32_64(mul):
220 return x * y;
221
Kirill Batuzov9a810902011-07-07 16:37:15 +0400222 CASE_OP_32_64(and):
223 return x & y;
224
225 CASE_OP_32_64(or):
226 return x | y;
227
228 CASE_OP_32_64(xor):
229 return x ^ y;
230
Kirill Batuzov55c09752011-07-07 16:37:16 +0400231 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700232 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400233
Kirill Batuzov55c09752011-07-07 16:37:16 +0400234 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700235 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400236
237 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700238 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400239
Richard Henderson4bb7a412013-09-09 17:03:24 -0700240 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400241 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700242 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400243
244 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700245 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400246
Kirill Batuzov55c09752011-07-07 16:37:16 +0400247 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700248 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400249
250 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700251 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400252
Kirill Batuzov55c09752011-07-07 16:37:16 +0400253 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700254 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400255
256 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700257 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400258
Kirill Batuzov55c09752011-07-07 16:37:16 +0400259 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700260 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400261
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700262 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400263 return ~x;
264
Richard Hendersoncb25c802011-08-17 14:11:47 -0700265 CASE_OP_32_64(neg):
266 return -x;
267
268 CASE_OP_32_64(andc):
269 return x & ~y;
270
271 CASE_OP_32_64(orc):
272 return x | ~y;
273
274 CASE_OP_32_64(eqv):
275 return ~(x ^ y);
276
277 CASE_OP_32_64(nand):
278 return ~(x & y);
279
280 CASE_OP_32_64(nor):
281 return ~(x | y);
282
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700283 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400284 return (int8_t)x;
285
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700286 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400287 return (int16_t)x;
288
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700289 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400290 return (uint8_t)x;
291
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700292 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400293 return (uint16_t)x;
294
Kirill Batuzova640f032011-07-07 16:37:17 +0400295 case INDEX_op_ext32s_i64:
296 return (int32_t)x;
297
298 case INDEX_op_ext32u_i64:
299 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400300
Richard Henderson03271522013-08-14 14:35:56 -0700301 case INDEX_op_muluh_i32:
302 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
303 case INDEX_op_mulsh_i32:
304 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
305
306 case INDEX_op_muluh_i64:
307 mulu64(&l64, &h64, x, y);
308 return h64;
309 case INDEX_op_mulsh_i64:
310 muls64(&l64, &h64, x, y);
311 return h64;
312
Richard Henderson01547f72013-08-14 15:22:46 -0700313 case INDEX_op_div_i32:
314 /* Avoid crashing on divide by zero, otherwise undefined. */
315 return (int32_t)x / ((int32_t)y ? : 1);
316 case INDEX_op_divu_i32:
317 return (uint32_t)x / ((uint32_t)y ? : 1);
318 case INDEX_op_div_i64:
319 return (int64_t)x / ((int64_t)y ? : 1);
320 case INDEX_op_divu_i64:
321 return (uint64_t)x / ((uint64_t)y ? : 1);
322
323 case INDEX_op_rem_i32:
324 return (int32_t)x % ((int32_t)y ? : 1);
325 case INDEX_op_remu_i32:
326 return (uint32_t)x % ((uint32_t)y ? : 1);
327 case INDEX_op_rem_i64:
328 return (int64_t)x % ((int64_t)y ? : 1);
329 case INDEX_op_remu_i64:
330 return (uint64_t)x % ((uint64_t)y ? : 1);
331
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400332 default:
333 fprintf(stderr,
334 "Unrecognized operation %d in do_constant_folding.\n", op);
335 tcg_abort();
336 }
337}
338
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000339static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400340{
341 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400342 if (op_bits(op) == 32) {
343 res &= 0xffffffff;
344 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400345 return res;
346}
347
Richard Henderson9519da72012-10-02 11:32:26 -0700348static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
349{
350 switch (c) {
351 case TCG_COND_EQ:
352 return x == y;
353 case TCG_COND_NE:
354 return x != y;
355 case TCG_COND_LT:
356 return (int32_t)x < (int32_t)y;
357 case TCG_COND_GE:
358 return (int32_t)x >= (int32_t)y;
359 case TCG_COND_LE:
360 return (int32_t)x <= (int32_t)y;
361 case TCG_COND_GT:
362 return (int32_t)x > (int32_t)y;
363 case TCG_COND_LTU:
364 return x < y;
365 case TCG_COND_GEU:
366 return x >= y;
367 case TCG_COND_LEU:
368 return x <= y;
369 case TCG_COND_GTU:
370 return x > y;
371 default:
372 tcg_abort();
373 }
374}
375
376static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
377{
378 switch (c) {
379 case TCG_COND_EQ:
380 return x == y;
381 case TCG_COND_NE:
382 return x != y;
383 case TCG_COND_LT:
384 return (int64_t)x < (int64_t)y;
385 case TCG_COND_GE:
386 return (int64_t)x >= (int64_t)y;
387 case TCG_COND_LE:
388 return (int64_t)x <= (int64_t)y;
389 case TCG_COND_GT:
390 return (int64_t)x > (int64_t)y;
391 case TCG_COND_LTU:
392 return x < y;
393 case TCG_COND_GEU:
394 return x >= y;
395 case TCG_COND_LEU:
396 return x <= y;
397 case TCG_COND_GTU:
398 return x > y;
399 default:
400 tcg_abort();
401 }
402}
403
404static bool do_constant_folding_cond_eq(TCGCond c)
405{
406 switch (c) {
407 case TCG_COND_GT:
408 case TCG_COND_LTU:
409 case TCG_COND_LT:
410 case TCG_COND_GTU:
411 case TCG_COND_NE:
412 return 0;
413 case TCG_COND_GE:
414 case TCG_COND_GEU:
415 case TCG_COND_LE:
416 case TCG_COND_LEU:
417 case TCG_COND_EQ:
418 return 1;
419 default:
420 tcg_abort();
421 }
422}
423
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200424/* Return 2 if the condition can't be simplified, and the result
425 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200426static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
427 TCGArg y, TCGCond c)
428{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200429 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
430 switch (op_bits(op)) {
431 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700432 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200433 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700434 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
435 default:
436 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200437 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200438 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700439 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200440 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
441 switch (c) {
442 case TCG_COND_LTU:
443 return 0;
444 case TCG_COND_GEU:
445 return 1;
446 default:
447 return 2;
448 }
449 } else {
450 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200451 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200452}
453
Richard Henderson6c4382f2012-10-02 11:32:27 -0700454/* Return 2 if the condition can't be simplified, and the result
455 of the condition (0 or 1) if it can */
456static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
457{
458 TCGArg al = p1[0], ah = p1[1];
459 TCGArg bl = p2[0], bh = p2[1];
460
461 if (temps[bl].state == TCG_TEMP_CONST
462 && temps[bh].state == TCG_TEMP_CONST) {
463 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
464
465 if (temps[al].state == TCG_TEMP_CONST
466 && temps[ah].state == TCG_TEMP_CONST) {
467 uint64_t a;
468 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
469 return do_constant_folding_cond_64(a, b, c);
470 }
471 if (b == 0) {
472 switch (c) {
473 case TCG_COND_LTU:
474 return 0;
475 case TCG_COND_GEU:
476 return 1;
477 default:
478 break;
479 }
480 }
481 }
482 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
483 return do_constant_folding_cond_eq(c);
484 }
485 return 2;
486}
487
Richard Henderson24c9ae42012-10-02 11:32:21 -0700488static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
489{
490 TCGArg a1 = *p1, a2 = *p2;
491 int sum = 0;
492 sum += temps[a1].state == TCG_TEMP_CONST;
493 sum -= temps[a2].state == TCG_TEMP_CONST;
494
495 /* Prefer the constant in second argument, and then the form
496 op a, a, b, which is better handled on non-RISC hosts. */
497 if (sum > 0 || (sum == 0 && dest == a2)) {
498 *p1 = a2;
499 *p2 = a1;
500 return true;
501 }
502 return false;
503}
504
Richard Henderson0bfcb862012-10-02 11:32:23 -0700505static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
506{
507 int sum = 0;
508 sum += temps[p1[0]].state == TCG_TEMP_CONST;
509 sum += temps[p1[1]].state == TCG_TEMP_CONST;
510 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
511 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
512 if (sum > 0) {
513 TCGArg t;
514 t = p1[0], p1[0] = p2[0], p2[0] = t;
515 t = p1[1], p1[1] = p2[1], p2[1] = t;
516 return true;
517 }
518 return false;
519}
520
Kirill Batuzov22613af2011-07-07 16:37:13 +0400521/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400522static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
523 TCGArg *args, TCGOpDef *tcg_op_defs)
524{
Richard Hendersoncf066672014-03-22 20:06:52 -0700525 int nb_ops, op_index, nb_temps, nb_globals;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400526 TCGArg *gen_args;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700527
Kirill Batuzov22613af2011-07-07 16:37:13 +0400528 /* Array VALS has an element for each temp.
529 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200530 If this temp is a copy of other ones then the other copies are
531 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400532
533 nb_temps = s->nb_temps;
534 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800535 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400536
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400537 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400538 gen_args = args;
539 for (op_index = 0; op_index < nb_ops; op_index++) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700540 TCGOpcode op = s->gen_opc_buf[op_index];
541 const TCGOpDef *def = &tcg_op_defs[op];
542 tcg_target_ulong mask, affected;
543 int nb_oargs, nb_iargs, nb_args, i;
544 TCGArg tmp;
545
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200546 if (op == INDEX_op_call) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700547 *gen_args++ = tmp = *args++;
548 nb_oargs = tmp >> 16;
549 nb_iargs = tmp & 0xffff;
550 nb_args = nb_oargs + nb_iargs + def->nb_cargs;
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200551 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700552 nb_oargs = def->nb_oargs;
553 nb_iargs = def->nb_iargs;
554 nb_args = def->nb_args;
555 }
556
557 /* Do copy propagation */
558 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
559 if (temps[args[i]].state == TCG_TEMP_COPY) {
560 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400561 }
562 }
563
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400564 /* For commutative operations make constant second argument */
565 switch (op) {
566 CASE_OP_32_64(add):
567 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400568 CASE_OP_32_64(and):
569 CASE_OP_32_64(or):
570 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700571 CASE_OP_32_64(eqv):
572 CASE_OP_32_64(nand):
573 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700574 CASE_OP_32_64(muluh):
575 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700576 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400577 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200578 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700579 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200580 args[2] = tcg_swap_cond(args[2]);
581 }
582 break;
583 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700584 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200585 args[3] = tcg_swap_cond(args[3]);
586 }
587 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700588 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700589 if (swap_commutative(-1, &args[1], &args[2])) {
590 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700591 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700592 /* For movcond, we canonicalize the "false" input reg to match
593 the destination reg so that the tcg backend can implement
594 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700595 if (swap_commutative(args[0], &args[4], &args[3])) {
596 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700597 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700598 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800599 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700600 swap_commutative(args[0], &args[2], &args[4]);
601 swap_commutative(args[1], &args[3], &args[5]);
602 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800603 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800604 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700605 swap_commutative(args[0], &args[2], &args[3]);
606 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700607 case INDEX_op_brcond2_i32:
608 if (swap_commutative2(&args[0], &args[2])) {
609 args[4] = tcg_swap_cond(args[4]);
610 }
611 break;
612 case INDEX_op_setcond2_i32:
613 if (swap_commutative2(&args[1], &args[3])) {
614 args[5] = tcg_swap_cond(args[5]);
615 }
616 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400617 default:
618 break;
619 }
620
Richard Henderson2d497542013-03-21 09:13:33 -0700621 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
622 and "sub r, 0, a => neg r, a" case. */
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200623 switch (op) {
624 CASE_OP_32_64(shl):
625 CASE_OP_32_64(shr):
626 CASE_OP_32_64(sar):
627 CASE_OP_32_64(rotl):
628 CASE_OP_32_64(rotr):
629 if (temps[args[1]].state == TCG_TEMP_CONST
630 && temps[args[1]].val == 0) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700631 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200632 args += 3;
633 gen_args += 2;
634 continue;
635 }
636 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700637 CASE_OP_32_64(sub):
638 {
639 TCGOpcode neg_op;
640 bool have_neg;
641
642 if (temps[args[2]].state == TCG_TEMP_CONST) {
643 /* Proceed with possible constant folding. */
644 break;
645 }
646 if (op == INDEX_op_sub_i32) {
647 neg_op = INDEX_op_neg_i32;
648 have_neg = TCG_TARGET_HAS_neg_i32;
649 } else {
650 neg_op = INDEX_op_neg_i64;
651 have_neg = TCG_TARGET_HAS_neg_i64;
652 }
653 if (!have_neg) {
654 break;
655 }
656 if (temps[args[1]].state == TCG_TEMP_CONST
657 && temps[args[1]].val == 0) {
658 s->gen_opc_buf[op_index] = neg_op;
659 reset_temp(args[0]);
660 gen_args[0] = args[0];
661 gen_args[1] = args[2];
662 args += 3;
663 gen_args += 2;
664 continue;
665 }
666 }
667 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800668 CASE_OP_32_64(xor):
669 CASE_OP_32_64(nand):
670 if (temps[args[1]].state != TCG_TEMP_CONST
671 && temps[args[2]].state == TCG_TEMP_CONST
672 && temps[args[2]].val == -1) {
673 i = 1;
674 goto try_not;
675 }
676 break;
677 CASE_OP_32_64(nor):
678 if (temps[args[1]].state != TCG_TEMP_CONST
679 && temps[args[2]].state == TCG_TEMP_CONST
680 && temps[args[2]].val == 0) {
681 i = 1;
682 goto try_not;
683 }
684 break;
685 CASE_OP_32_64(andc):
686 if (temps[args[2]].state != TCG_TEMP_CONST
687 && temps[args[1]].state == TCG_TEMP_CONST
688 && temps[args[1]].val == -1) {
689 i = 2;
690 goto try_not;
691 }
692 break;
693 CASE_OP_32_64(orc):
694 CASE_OP_32_64(eqv):
695 if (temps[args[2]].state != TCG_TEMP_CONST
696 && temps[args[1]].state == TCG_TEMP_CONST
697 && temps[args[1]].val == 0) {
698 i = 2;
699 goto try_not;
700 }
701 break;
702 try_not:
703 {
704 TCGOpcode not_op;
705 bool have_not;
706
707 if (def->flags & TCG_OPF_64BIT) {
708 not_op = INDEX_op_not_i64;
709 have_not = TCG_TARGET_HAS_not_i64;
710 } else {
711 not_op = INDEX_op_not_i32;
712 have_not = TCG_TARGET_HAS_not_i32;
713 }
714 if (!have_not) {
715 break;
716 }
717 s->gen_opc_buf[op_index] = not_op;
718 reset_temp(args[0]);
719 gen_args[0] = args[0];
720 gen_args[1] = args[i];
721 args += 3;
722 gen_args += 2;
723 continue;
724 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200725 default:
726 break;
727 }
728
Richard Henderson464a1442014-01-31 07:42:11 -0600729 /* Simplify expression for "op r, a, const => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400730 switch (op) {
731 CASE_OP_32_64(add):
732 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400733 CASE_OP_32_64(shl):
734 CASE_OP_32_64(shr):
735 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700736 CASE_OP_32_64(rotl):
737 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200738 CASE_OP_32_64(or):
739 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600740 CASE_OP_32_64(andc):
741 if (temps[args[1]].state != TCG_TEMP_CONST
742 && temps[args[2]].state == TCG_TEMP_CONST
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400743 && temps[args[2]].val == 0) {
Richard Henderson464a1442014-01-31 07:42:11 -0600744 goto do_mov3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400745 }
746 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600747 CASE_OP_32_64(and):
748 CASE_OP_32_64(orc):
749 CASE_OP_32_64(eqv):
750 if (temps[args[1]].state != TCG_TEMP_CONST
751 && temps[args[2]].state == TCG_TEMP_CONST
752 && temps[args[2]].val == -1) {
753 goto do_mov3;
754 }
755 break;
756 do_mov3:
757 if (temps_are_copies(args[0], args[1])) {
758 s->gen_opc_buf[op_index] = INDEX_op_nop;
759 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700760 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Richard Henderson464a1442014-01-31 07:42:11 -0600761 gen_args += 2;
762 }
763 args += 3;
764 continue;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200765 default:
766 break;
767 }
768
Aurelien Jarno30312442013-09-03 08:27:38 +0200769 /* Simplify using known-zero bits. Currently only ops with a single
770 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800771 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800772 affected = -1;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800773 switch (op) {
774 CASE_OP_32_64(ext8s):
775 if ((temps[args[1]].mask & 0x80) != 0) {
776 break;
777 }
778 CASE_OP_32_64(ext8u):
779 mask = 0xff;
780 goto and_const;
781 CASE_OP_32_64(ext16s):
782 if ((temps[args[1]].mask & 0x8000) != 0) {
783 break;
784 }
785 CASE_OP_32_64(ext16u):
786 mask = 0xffff;
787 goto and_const;
788 case INDEX_op_ext32s_i64:
789 if ((temps[args[1]].mask & 0x80000000) != 0) {
790 break;
791 }
792 case INDEX_op_ext32u_i64:
793 mask = 0xffffffffU;
794 goto and_const;
795
796 CASE_OP_32_64(and):
797 mask = temps[args[2]].mask;
798 if (temps[args[2]].state == TCG_TEMP_CONST) {
799 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800800 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800801 }
802 mask = temps[args[1]].mask & mask;
803 break;
804
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800805 CASE_OP_32_64(andc):
806 /* Known-zeros does not imply known-ones. Therefore unless
807 args[2] is constant, we can't infer anything from it. */
808 if (temps[args[2]].state == TCG_TEMP_CONST) {
809 mask = ~temps[args[2]].mask;
810 goto and_const;
811 }
812 /* But we certainly know nothing outside args[1] may be set. */
813 mask = temps[args[1]].mask;
814 break;
815
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200816 case INDEX_op_sar_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800817 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700818 tmp = temps[args[2]].val & 31;
819 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200820 }
821 break;
822 case INDEX_op_sar_i64:
823 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700824 tmp = temps[args[2]].val & 63;
825 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800826 }
827 break;
828
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200829 case INDEX_op_shr_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800830 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700831 tmp = temps[args[2]].val & 31;
832 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200833 }
834 break;
835 case INDEX_op_shr_i64:
836 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700837 tmp = temps[args[2]].val & 63;
838 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800839 }
840 break;
841
Richard Henderson4bb7a412013-09-09 17:03:24 -0700842 case INDEX_op_trunc_shr_i32:
843 mask = (uint64_t)temps[args[1]].mask >> args[2];
844 break;
845
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800846 CASE_OP_32_64(shl):
847 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700848 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
849 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800850 }
851 break;
852
853 CASE_OP_32_64(neg):
854 /* Set to 1 all bits to the left of the rightmost. */
855 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
856 break;
857
858 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700859 mask = deposit64(temps[args[1]].mask, args[3], args[4],
860 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800861 break;
862
863 CASE_OP_32_64(or):
864 CASE_OP_32_64(xor):
865 mask = temps[args[1]].mask | temps[args[2]].mask;
866 break;
867
868 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700869 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800870 mask = 1;
871 break;
872
873 CASE_OP_32_64(movcond):
874 mask = temps[args[3]].mask | temps[args[4]].mask;
875 break;
876
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200877 CASE_OP_32_64(ld8u):
878 case INDEX_op_qemu_ld8u:
879 mask = 0xff;
880 break;
881 CASE_OP_32_64(ld16u):
882 case INDEX_op_qemu_ld16u:
883 mask = 0xffff;
884 break;
885 case INDEX_op_ld32u_i64:
886#if TCG_TARGET_REG_BITS == 64
887 case INDEX_op_qemu_ld32u:
888#endif
889 mask = 0xffffffffu;
890 break;
891
892 CASE_OP_32_64(qemu_ld):
893 {
Richard Hendersoncf066672014-03-22 20:06:52 -0700894 TCGMemOp mop = args[nb_oargs + nb_iargs];
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200895 if (!(mop & MO_SIGN)) {
896 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
897 }
898 }
899 break;
900
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800901 default:
902 break;
903 }
904
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200905 /* 32-bit ops (non 64-bit ops and non load/store ops) generate 32-bit
906 results */
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200907 if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_64BIT))) {
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200908 mask &= 0xffffffffu;
909 }
910
Paolo Bonzini633f6502013-01-11 15:42:53 -0800911 if (mask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700912 assert(nb_oargs == 1);
Richard Hendersona62f6f52014-05-22 10:59:12 -0700913 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Richard Hendersoncf066672014-03-22 20:06:52 -0700914 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800915 gen_args += 2;
916 continue;
917 }
918 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700919 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800920 if (temps_are_copies(args[0], args[1])) {
921 s->gen_opc_buf[op_index] = INDEX_op_nop;
922 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700923 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800924 gen_args += 2;
925 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700926 tcg_opt_gen_movi(s, op_index, gen_args, op,
927 args[0], temps[args[1]].val);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800928 gen_args += 2;
929 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700930 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800931 continue;
932 }
933
Aurelien Jarno56e49432012-09-06 16:47:13 +0200934 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
935 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200936 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400937 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700938 CASE_OP_32_64(muluh):
939 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400940 if ((temps[args[2]].state == TCG_TEMP_CONST
941 && temps[args[2]].val == 0)) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700942 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400943 args += 3;
944 gen_args += 2;
945 continue;
946 }
947 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200948 default:
949 break;
950 }
951
952 /* Simplify expression for "op r, a, a => mov r, a" cases */
953 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400954 CASE_OP_32_64(or):
955 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200956 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200957 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400958 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400959 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700960 tcg_opt_gen_mov(s, op_index, gen_args, op,
961 args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400962 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400963 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200964 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400965 continue;
966 }
967 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000968 default:
969 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400970 }
971
Aurelien Jarno3c941932012-09-18 19:12:36 +0200972 /* Simplify expression for "op r, a, a => movi r, 0" cases */
973 switch (op) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800974 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +0200975 CASE_OP_32_64(sub):
976 CASE_OP_32_64(xor):
977 if (temps_are_copies(args[1], args[2])) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700978 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200979 gen_args += 2;
980 args += 3;
981 continue;
982 }
983 break;
984 default:
985 break;
986 }
987
Kirill Batuzov22613af2011-07-07 16:37:13 +0400988 /* Propagate constants through copy operations and do constant
989 folding. Constants will be substituted to arguments by register
990 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400991 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400992 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200993 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400994 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400995 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400996 break;
997 }
998 if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -0700999 tcg_opt_gen_mov(s, op_index, gen_args, op, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001000 gen_args += 2;
1001 args += 2;
1002 break;
1003 }
1004 /* Source argument is constant. Rewrite the operation and
1005 let movi case handle it. */
Kirill Batuzov22613af2011-07-07 16:37:13 +04001006 args[1] = temps[args[1]].val;
1007 /* fallthrough */
1008 CASE_OP_32_64(movi):
Richard Hendersona62f6f52014-05-22 10:59:12 -07001009 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001010 gen_args += 2;
1011 args += 2;
1012 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001013
Kirill Batuzova640f032011-07-07 16:37:17 +04001014 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001015 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001016 CASE_OP_32_64(ext8s):
1017 CASE_OP_32_64(ext8u):
1018 CASE_OP_32_64(ext16s):
1019 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001020 case INDEX_op_ext32s_i64:
1021 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +04001022 if (temps[args[1]].state == TCG_TEMP_CONST) {
Kirill Batuzova640f032011-07-07 16:37:17 +04001023 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001024 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001025 gen_args += 2;
1026 args += 2;
1027 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001028 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001029 goto do_default;
1030
Richard Henderson4bb7a412013-09-09 17:03:24 -07001031 case INDEX_op_trunc_shr_i32:
1032 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Henderson4bb7a412013-09-09 17:03:24 -07001033 tmp = do_constant_folding(op, temps[args[1]].val, args[2]);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001034 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001035 gen_args += 2;
1036 args += 3;
1037 break;
1038 }
1039 goto do_default;
1040
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001041 CASE_OP_32_64(add):
1042 CASE_OP_32_64(sub):
1043 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001044 CASE_OP_32_64(or):
1045 CASE_OP_32_64(and):
1046 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001047 CASE_OP_32_64(shl):
1048 CASE_OP_32_64(shr):
1049 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001050 CASE_OP_32_64(rotl):
1051 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001052 CASE_OP_32_64(andc):
1053 CASE_OP_32_64(orc):
1054 CASE_OP_32_64(eqv):
1055 CASE_OP_32_64(nand):
1056 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001057 CASE_OP_32_64(muluh):
1058 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001059 CASE_OP_32_64(div):
1060 CASE_OP_32_64(divu):
1061 CASE_OP_32_64(rem):
1062 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001063 if (temps[args[1]].state == TCG_TEMP_CONST
1064 && temps[args[2]].state == TCG_TEMP_CONST) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001065 tmp = do_constant_folding(op, temps[args[1]].val,
1066 temps[args[2]].val);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001067 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001068 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001069 args += 3;
1070 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001071 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001072 goto do_default;
1073
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001074 CASE_OP_32_64(deposit):
1075 if (temps[args[1]].state == TCG_TEMP_CONST
1076 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersond998e552014-03-18 14:23:52 -07001077 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1078 temps[args[2]].val);
Richard Hendersona62f6f52014-05-22 10:59:12 -07001079 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001080 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001081 args += 5;
1082 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001083 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001084 goto do_default;
1085
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001086 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001087 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
1088 if (tmp != 2) {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001089 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001090 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001091 args += 4;
1092 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001093 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001094 goto do_default;
1095
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001096 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001097 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
1098 if (tmp != 2) {
1099 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001100 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001101 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001102 gen_args[0] = args[3];
1103 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001104 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001105 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001106 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001107 args += 4;
1108 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001109 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001110 goto do_default;
1111
Richard Hendersonfa01a202012-09-21 10:13:37 -07001112 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001113 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
1114 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001115 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001116 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001117 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001118 tcg_opt_gen_movi(s, op_index, gen_args, op,
1119 args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001120 gen_args += 2;
1121 } else {
Richard Hendersona62f6f52014-05-22 10:59:12 -07001122 tcg_opt_gen_mov(s, op_index, gen_args, op,
1123 args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001124 gen_args += 2;
1125 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001126 args += 6;
1127 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001128 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001129 goto do_default;
1130
Richard Henderson212c3282012-10-02 11:32:28 -07001131 case INDEX_op_add2_i32:
1132 case INDEX_op_sub2_i32:
1133 if (temps[args[2]].state == TCG_TEMP_CONST
1134 && temps[args[3]].state == TCG_TEMP_CONST
1135 && temps[args[4]].state == TCG_TEMP_CONST
1136 && temps[args[5]].state == TCG_TEMP_CONST) {
1137 uint32_t al = temps[args[2]].val;
1138 uint32_t ah = temps[args[3]].val;
1139 uint32_t bl = temps[args[4]].val;
1140 uint32_t bh = temps[args[5]].val;
1141 uint64_t a = ((uint64_t)ah << 32) | al;
1142 uint64_t b = ((uint64_t)bh << 32) | bl;
1143 TCGArg rl, rh;
1144
1145 if (op == INDEX_op_add2_i32) {
1146 a += b;
1147 } else {
1148 a -= b;
1149 }
1150
1151 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001152 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -07001153
1154 rl = args[0];
1155 rh = args[1];
Richard Hendersona62f6f52014-05-22 10:59:12 -07001156 tcg_opt_gen_movi(s, op_index, &gen_args[0],
1157 op, rl, (uint32_t)a);
1158 tcg_opt_gen_movi(s, ++op_index, &gen_args[2],
1159 op, rh, (uint32_t)(a >> 32));
Richard Henderson212c3282012-10-02 11:32:28 -07001160 gen_args += 4;
1161 args += 6;
1162 break;
1163 }
1164 goto do_default;
1165
Richard Henderson14149682012-10-02 11:32:30 -07001166 case INDEX_op_mulu2_i32:
1167 if (temps[args[2]].state == TCG_TEMP_CONST
1168 && temps[args[3]].state == TCG_TEMP_CONST) {
1169 uint32_t a = temps[args[2]].val;
1170 uint32_t b = temps[args[3]].val;
1171 uint64_t r = (uint64_t)a * b;
1172 TCGArg rl, rh;
1173
1174 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001175 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -07001176
1177 rl = args[0];
1178 rh = args[1];
Richard Hendersona62f6f52014-05-22 10:59:12 -07001179 tcg_opt_gen_movi(s, op_index, &gen_args[0],
1180 op, rl, (uint32_t)r);
1181 tcg_opt_gen_movi(s, ++op_index, &gen_args[2],
1182 op, rh, (uint32_t)(r >> 32));
Richard Henderson14149682012-10-02 11:32:30 -07001183 gen_args += 4;
1184 args += 4;
1185 break;
1186 }
1187 goto do_default;
1188
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001189 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001190 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1191 if (tmp != 2) {
1192 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001193 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001194 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001195 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001196 gen_args[0] = args[5];
1197 gen_args += 1;
1198 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001199 do_brcond_false:
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001200 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001201 }
1202 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1203 && temps[args[2]].state == TCG_TEMP_CONST
1204 && temps[args[3]].state == TCG_TEMP_CONST
1205 && temps[args[2]].val == 0
1206 && temps[args[3]].val == 0) {
1207 /* Simplify LT/GE comparisons vs zero to a single compare
1208 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001209 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001210 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001211 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001212 gen_args[0] = args[1];
1213 gen_args[1] = args[3];
1214 gen_args[2] = args[4];
1215 gen_args[3] = args[5];
1216 gen_args += 4;
Richard Hendersona7635512014-04-23 22:18:30 -07001217 } else if (args[4] == TCG_COND_EQ) {
1218 /* Simplify EQ comparisons where one of the pairs
1219 can be simplified. */
1220 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1221 args[0], args[2], TCG_COND_EQ);
1222 if (tmp == 0) {
1223 goto do_brcond_false;
1224 } else if (tmp == 1) {
1225 goto do_brcond_high;
1226 }
1227 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1228 args[1], args[3], TCG_COND_EQ);
1229 if (tmp == 0) {
1230 goto do_brcond_false;
1231 } else if (tmp != 1) {
1232 goto do_default;
1233 }
1234 do_brcond_low:
1235 reset_all_temps(nb_temps);
1236 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
1237 gen_args[0] = args[0];
1238 gen_args[1] = args[2];
1239 gen_args[2] = args[4];
1240 gen_args[3] = args[5];
1241 gen_args += 4;
1242 } else if (args[4] == TCG_COND_NE) {
1243 /* Simplify NE comparisons where one of the pairs
1244 can be simplified. */
1245 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1246 args[0], args[2], TCG_COND_NE);
1247 if (tmp == 0) {
1248 goto do_brcond_high;
1249 } else if (tmp == 1) {
1250 goto do_brcond_true;
1251 }
1252 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1253 args[1], args[3], TCG_COND_NE);
1254 if (tmp == 0) {
1255 goto do_brcond_low;
1256 } else if (tmp == 1) {
1257 goto do_brcond_true;
1258 }
1259 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001260 } else {
1261 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001262 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001263 args += 6;
1264 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001265
1266 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001267 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1268 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001269 do_setcond_const:
Richard Hendersona62f6f52014-05-22 10:59:12 -07001270 tcg_opt_gen_movi(s, op_index, gen_args, op, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001271 gen_args += 2;
1272 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1273 && temps[args[3]].state == TCG_TEMP_CONST
1274 && temps[args[4]].state == TCG_TEMP_CONST
1275 && temps[args[3]].val == 0
1276 && temps[args[4]].val == 0) {
1277 /* Simplify LT/GE comparisons vs zero to a single compare
1278 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001279 do_setcond_high:
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001280 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001281 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001282 temps[args[0]].mask = 1;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001283 gen_args[0] = args[0];
1284 gen_args[1] = args[2];
1285 gen_args[2] = args[4];
1286 gen_args[3] = args[5];
1287 gen_args += 4;
Richard Hendersona7635512014-04-23 22:18:30 -07001288 } else if (args[5] == TCG_COND_EQ) {
1289 /* Simplify EQ comparisons where one of the pairs
1290 can be simplified. */
1291 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1292 args[1], args[3], TCG_COND_EQ);
1293 if (tmp == 0) {
1294 goto do_setcond_const;
1295 } else if (tmp == 1) {
1296 goto do_setcond_high;
1297 }
1298 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1299 args[2], args[4], TCG_COND_EQ);
1300 if (tmp == 0) {
1301 goto do_setcond_high;
1302 } else if (tmp != 1) {
1303 goto do_default;
1304 }
1305 do_setcond_low:
1306 reset_temp(args[0]);
1307 temps[args[0]].mask = 1;
1308 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
1309 gen_args[0] = args[0];
1310 gen_args[1] = args[1];
1311 gen_args[2] = args[3];
1312 gen_args[3] = args[5];
1313 gen_args += 4;
1314 } else if (args[5] == TCG_COND_NE) {
1315 /* Simplify NE comparisons where one of the pairs
1316 can be simplified. */
1317 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1318 args[1], args[3], TCG_COND_NE);
1319 if (tmp == 0) {
1320 goto do_setcond_high;
1321 } else if (tmp == 1) {
1322 goto do_setcond_const;
1323 }
1324 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1325 args[2], args[4], TCG_COND_NE);
1326 if (tmp == 0) {
1327 goto do_setcond_low;
1328 } else if (tmp == 1) {
1329 goto do_setcond_const;
1330 }
1331 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001332 } else {
1333 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001334 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001335 args += 6;
1336 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001337
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001338 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001339 if (!(args[nb_oargs + nb_iargs + 1]
1340 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001341 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001342 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001343 }
1344 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001345 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001346
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001347 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001348 do_default:
1349 /* Default case: we know nothing about operation (or were unable
1350 to compute the operation result) so no propagation is done.
1351 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001352 block, otherwise we only trash the output args. "mask" is
1353 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001354 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001355 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001356 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001357 do_reset_output:
1358 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001359 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001360 /* Save the corresponding known-zero bits mask for the
1361 first output argument (only one supported so far). */
1362 if (i == 0) {
1363 temps[args[i]].mask = mask;
1364 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001365 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001366 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001367 for (i = 0; i < nb_args; i++) {
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001368 gen_args[i] = args[i];
1369 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001370 args += nb_args;
1371 gen_args += nb_args;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001372 break;
1373 }
1374 }
1375
1376 return gen_args;
1377}
1378
1379TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1380 TCGArg *args, TCGOpDef *tcg_op_defs)
1381{
1382 TCGArg *res;
1383 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1384 return res;
1385}