blob: c058d04ec08f26ac8e5b542a254289cdbd6f0df8 [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];
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020053static TCGTempSet temps_used;
Kirill Batuzov22613af2011-07-07 16:37:13 +040054
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020055static inline bool temp_is_const(TCGArg arg)
56{
57 return temps[arg].state == TCG_TEMP_CONST;
58}
59
60static inline bool temp_is_copy(TCGArg arg)
61{
62 return temps[arg].state == TCG_TEMP_COPY;
63}
64
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020065/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
66 the copy flag from the left temp. */
67static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040068{
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020069 if (temp_is_copy(temp)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020070 if (temps[temp].prev_copy == temps[temp].next_copy) {
71 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
72 } else {
73 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
74 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
Kirill Batuzov22613af2011-07-07 16:37:13 +040075 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040076 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020077 temps[temp].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080078 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040079}
80
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020081/* Reset all temporaries, given that there are NB_TEMPS of them. */
82static void reset_all_temps(int nb_temps)
83{
84 bitmap_zero(temps_used.l, nb_temps);
85}
86
87/* Initialize and activate a temporary. */
88static void init_temp_info(TCGArg temp)
89{
90 if (!test_bit(temp, temps_used.l)) {
91 temps[temp].state = TCG_TEMP_UNDEF;
92 temps[temp].mask = -1;
93 set_bit(temp, temps_used.l);
94 }
95}
96
Richard Hendersona4ce0992014-03-30 17:14:02 -070097static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
98 TCGOpcode opc, int nargs)
99{
100 int oi = s->gen_next_op_idx;
101 int pi = s->gen_next_parm_idx;
102 int prev = old_op->prev;
103 int next = old_op - s->gen_op_buf;
104 TCGOp *new_op;
105
106 tcg_debug_assert(oi < OPC_BUF_SIZE);
107 tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
108 s->gen_next_op_idx = oi + 1;
109 s->gen_next_parm_idx = pi + nargs;
110
111 new_op = &s->gen_op_buf[oi];
112 *new_op = (TCGOp){
113 .opc = opc,
114 .args = pi,
115 .prev = prev,
116 .next = next
117 };
118 if (prev >= 0) {
119 s->gen_op_buf[prev].next = oi;
120 } else {
121 s->gen_first_op_idx = oi;
122 }
123 old_op->prev = oi;
124
125 return new_op;
126}
127
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000128static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400129{
Richard Henderson8399ad52011-08-17 14:11:45 -0700130 const TCGOpDef *def = &tcg_op_defs[op];
131 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400132}
133
Richard Hendersona62f6f52014-05-22 10:59:12 -0700134static TCGOpcode op_to_mov(TCGOpcode op)
135{
136 switch (op_bits(op)) {
137 case 32:
138 return INDEX_op_mov_i32;
139 case 64:
140 return INDEX_op_mov_i64;
141 default:
142 fprintf(stderr, "op_to_mov: unexpected return value of "
143 "function op_bits.\n");
144 tcg_abort();
145 }
146}
147
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000148static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400149{
150 switch (op_bits(op)) {
151 case 32:
152 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400153 case 64:
154 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400155 default:
156 fprintf(stderr, "op_to_movi: unexpected return value of "
157 "function op_bits.\n");
158 tcg_abort();
159 }
160}
161
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200162static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
163{
164 TCGArg i;
165
166 /* If this is already a global, we can't do better. */
167 if (temp < s->nb_globals) {
168 return temp;
169 }
170
171 /* Search for a global first. */
172 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
173 if (i < s->nb_globals) {
174 return i;
175 }
176 }
177
178 /* If it is a temp, search for a temp local. */
179 if (!s->temps[temp].temp_local) {
180 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
181 if (s->temps[i].temp_local) {
182 return i;
183 }
184 }
185 }
186
187 /* Failure to find a better representation, return the same temp. */
188 return temp;
189}
190
191static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
192{
193 TCGArg i;
194
195 if (arg1 == arg2) {
196 return true;
197 }
198
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200199 if (!temp_is_copy(arg1) || !temp_is_copy(arg2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200200 return false;
201 }
202
203 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
204 if (i == arg2) {
205 return true;
206 }
207 }
208
209 return false;
210}
211
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200212static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
213 TCGArg dst, TCGArg val)
214{
215 TCGOpcode new_op = op_to_movi(op->opc);
216 tcg_target_ulong mask;
217
218 op->opc = new_op;
219
220 reset_temp(dst);
221 temps[dst].state = TCG_TEMP_CONST;
222 temps[dst].val = val;
223 mask = val;
Aurelien Jarno96152122015-07-10 18:03:30 +0200224 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_movi_i32) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200225 /* High bits of the destination are now garbage. */
226 mask |= ~0xffffffffull;
227 }
228 temps[dst].mask = mask;
229
230 args[0] = dst;
231 args[1] = val;
232}
233
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700234static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200235 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400236{
Aurelien Jarno53657182015-06-04 21:53:25 +0200237 if (temps_are_copies(dst, src)) {
238 tcg_op_remove(s, op);
239 return;
240 }
241
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200242 if (temp_is_const(src)) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200243 tcg_opt_gen_movi(s, op, args, dst, temps[src].val);
244 return;
245 }
246
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200247 TCGOpcode new_op = op_to_mov(op->opc);
Richard Henderson24666ba2014-05-22 11:14:10 -0700248 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700249
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700250 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700251
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800252 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700253 mask = temps[src].mask;
254 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
255 /* High bits of the destination are now garbage. */
256 mask |= ~0xffffffffull;
257 }
258 temps[dst].mask = mask;
259
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200260 assert(!temp_is_const(src));
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200261
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800262 if (s->temps[src].type == s->temps[dst].type) {
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200263 if (!temp_is_copy(src)) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800264 temps[src].state = TCG_TEMP_COPY;
265 temps[src].next_copy = src;
266 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400267 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800268 temps[dst].state = TCG_TEMP_COPY;
269 temps[dst].next_copy = temps[src].next_copy;
270 temps[dst].prev_copy = src;
271 temps[temps[dst].next_copy].prev_copy = dst;
272 temps[src].next_copy = dst;
273 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200274
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700275 args[0] = dst;
276 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400277}
278
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000279static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400280{
Richard Henderson03271522013-08-14 14:35:56 -0700281 uint64_t l64, h64;
282
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400283 switch (op) {
284 CASE_OP_32_64(add):
285 return x + y;
286
287 CASE_OP_32_64(sub):
288 return x - y;
289
290 CASE_OP_32_64(mul):
291 return x * y;
292
Kirill Batuzov9a810902011-07-07 16:37:15 +0400293 CASE_OP_32_64(and):
294 return x & y;
295
296 CASE_OP_32_64(or):
297 return x | y;
298
299 CASE_OP_32_64(xor):
300 return x ^ y;
301
Kirill Batuzov55c09752011-07-07 16:37:16 +0400302 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700303 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400304
Kirill Batuzov55c09752011-07-07 16:37:16 +0400305 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700306 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400307
308 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700309 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400310
Richard Henderson4bb7a412013-09-09 17:03:24 -0700311 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400312 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700313 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400314
315 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700316 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400317
Kirill Batuzov55c09752011-07-07 16:37:16 +0400318 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700319 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400320
321 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700322 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400323
Kirill Batuzov55c09752011-07-07 16:37:16 +0400324 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700325 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400326
327 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700328 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400329
Kirill Batuzov55c09752011-07-07 16:37:16 +0400330 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700331 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400332
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700333 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400334 return ~x;
335
Richard Hendersoncb25c802011-08-17 14:11:47 -0700336 CASE_OP_32_64(neg):
337 return -x;
338
339 CASE_OP_32_64(andc):
340 return x & ~y;
341
342 CASE_OP_32_64(orc):
343 return x | ~y;
344
345 CASE_OP_32_64(eqv):
346 return ~(x ^ y);
347
348 CASE_OP_32_64(nand):
349 return ~(x & y);
350
351 CASE_OP_32_64(nor):
352 return ~(x | y);
353
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700354 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400355 return (int8_t)x;
356
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700357 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400358 return (int16_t)x;
359
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700360 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400361 return (uint8_t)x;
362
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700363 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400364 return (uint16_t)x;
365
Kirill Batuzova640f032011-07-07 16:37:17 +0400366 case INDEX_op_ext32s_i64:
367 return (int32_t)x;
368
369 case INDEX_op_ext32u_i64:
370 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400371
Richard Henderson03271522013-08-14 14:35:56 -0700372 case INDEX_op_muluh_i32:
373 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
374 case INDEX_op_mulsh_i32:
375 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
376
377 case INDEX_op_muluh_i64:
378 mulu64(&l64, &h64, x, y);
379 return h64;
380 case INDEX_op_mulsh_i64:
381 muls64(&l64, &h64, x, y);
382 return h64;
383
Richard Henderson01547f72013-08-14 15:22:46 -0700384 case INDEX_op_div_i32:
385 /* Avoid crashing on divide by zero, otherwise undefined. */
386 return (int32_t)x / ((int32_t)y ? : 1);
387 case INDEX_op_divu_i32:
388 return (uint32_t)x / ((uint32_t)y ? : 1);
389 case INDEX_op_div_i64:
390 return (int64_t)x / ((int64_t)y ? : 1);
391 case INDEX_op_divu_i64:
392 return (uint64_t)x / ((uint64_t)y ? : 1);
393
394 case INDEX_op_rem_i32:
395 return (int32_t)x % ((int32_t)y ? : 1);
396 case INDEX_op_remu_i32:
397 return (uint32_t)x % ((uint32_t)y ? : 1);
398 case INDEX_op_rem_i64:
399 return (int64_t)x % ((int64_t)y ? : 1);
400 case INDEX_op_remu_i64:
401 return (uint64_t)x % ((uint64_t)y ? : 1);
402
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400403 default:
404 fprintf(stderr,
405 "Unrecognized operation %d in do_constant_folding.\n", op);
406 tcg_abort();
407 }
408}
409
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000410static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400411{
412 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400413 if (op_bits(op) == 32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200414 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400415 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400416 return res;
417}
418
Richard Henderson9519da72012-10-02 11:32:26 -0700419static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
420{
421 switch (c) {
422 case TCG_COND_EQ:
423 return x == y;
424 case TCG_COND_NE:
425 return x != y;
426 case TCG_COND_LT:
427 return (int32_t)x < (int32_t)y;
428 case TCG_COND_GE:
429 return (int32_t)x >= (int32_t)y;
430 case TCG_COND_LE:
431 return (int32_t)x <= (int32_t)y;
432 case TCG_COND_GT:
433 return (int32_t)x > (int32_t)y;
434 case TCG_COND_LTU:
435 return x < y;
436 case TCG_COND_GEU:
437 return x >= y;
438 case TCG_COND_LEU:
439 return x <= y;
440 case TCG_COND_GTU:
441 return x > y;
442 default:
443 tcg_abort();
444 }
445}
446
447static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
448{
449 switch (c) {
450 case TCG_COND_EQ:
451 return x == y;
452 case TCG_COND_NE:
453 return x != y;
454 case TCG_COND_LT:
455 return (int64_t)x < (int64_t)y;
456 case TCG_COND_GE:
457 return (int64_t)x >= (int64_t)y;
458 case TCG_COND_LE:
459 return (int64_t)x <= (int64_t)y;
460 case TCG_COND_GT:
461 return (int64_t)x > (int64_t)y;
462 case TCG_COND_LTU:
463 return x < y;
464 case TCG_COND_GEU:
465 return x >= y;
466 case TCG_COND_LEU:
467 return x <= y;
468 case TCG_COND_GTU:
469 return x > y;
470 default:
471 tcg_abort();
472 }
473}
474
475static bool do_constant_folding_cond_eq(TCGCond c)
476{
477 switch (c) {
478 case TCG_COND_GT:
479 case TCG_COND_LTU:
480 case TCG_COND_LT:
481 case TCG_COND_GTU:
482 case TCG_COND_NE:
483 return 0;
484 case TCG_COND_GE:
485 case TCG_COND_GEU:
486 case TCG_COND_LE:
487 case TCG_COND_LEU:
488 case TCG_COND_EQ:
489 return 1;
490 default:
491 tcg_abort();
492 }
493}
494
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200495/* Return 2 if the condition can't be simplified, and the result
496 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200497static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
498 TCGArg y, TCGCond c)
499{
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200500 if (temp_is_const(x) && temp_is_const(y)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200501 switch (op_bits(op)) {
502 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700503 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200504 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700505 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
506 default:
507 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200508 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200509 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700510 return do_constant_folding_cond_eq(c);
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200511 } else if (temp_is_const(y) && temps[y].val == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200512 switch (c) {
513 case TCG_COND_LTU:
514 return 0;
515 case TCG_COND_GEU:
516 return 1;
517 default:
518 return 2;
519 }
520 } else {
521 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200522 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200523}
524
Richard Henderson6c4382f2012-10-02 11:32:27 -0700525/* Return 2 if the condition can't be simplified, and the result
526 of the condition (0 or 1) if it can */
527static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
528{
529 TCGArg al = p1[0], ah = p1[1];
530 TCGArg bl = p2[0], bh = p2[1];
531
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200532 if (temp_is_const(bl) && temp_is_const(bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700533 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
534
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200535 if (temp_is_const(al) && temp_is_const(ah)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700536 uint64_t a;
537 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
538 return do_constant_folding_cond_64(a, b, c);
539 }
540 if (b == 0) {
541 switch (c) {
542 case TCG_COND_LTU:
543 return 0;
544 case TCG_COND_GEU:
545 return 1;
546 default:
547 break;
548 }
549 }
550 }
551 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
552 return do_constant_folding_cond_eq(c);
553 }
554 return 2;
555}
556
Richard Henderson24c9ae42012-10-02 11:32:21 -0700557static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
558{
559 TCGArg a1 = *p1, a2 = *p2;
560 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200561 sum += temp_is_const(a1);
562 sum -= temp_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700563
564 /* Prefer the constant in second argument, and then the form
565 op a, a, b, which is better handled on non-RISC hosts. */
566 if (sum > 0 || (sum == 0 && dest == a2)) {
567 *p1 = a2;
568 *p2 = a1;
569 return true;
570 }
571 return false;
572}
573
Richard Henderson0bfcb862012-10-02 11:32:23 -0700574static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
575{
576 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200577 sum += temp_is_const(p1[0]);
578 sum += temp_is_const(p1[1]);
579 sum -= temp_is_const(p2[0]);
580 sum -= temp_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700581 if (sum > 0) {
582 TCGArg t;
583 t = p1[0], p1[0] = p2[0], p2[0] = t;
584 t = p1[1], p1[1] = p2[1], p2[1] = t;
585 return true;
586 }
587 return false;
588}
589
Kirill Batuzov22613af2011-07-07 16:37:13 +0400590/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +0200591void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400592{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700593 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700594
Kirill Batuzov22613af2011-07-07 16:37:13 +0400595 /* Array VALS has an element for each temp.
596 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200597 If this temp is a copy of other ones then the other copies are
598 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400599
600 nb_temps = s->nb_temps;
601 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800602 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400603
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700604 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700605 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700606 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700607 TCGArg tmp;
608
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700609 TCGOp * const op = &s->gen_op_buf[oi];
610 TCGArg * const args = &s->gen_opparam_buf[op->args];
611 TCGOpcode opc = op->opc;
612 const TCGOpDef *def = &tcg_op_defs[opc];
613
614 oi_next = op->next;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200615
616 /* Count the arguments, and initialize the temps that are
617 going to be used */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700618 if (opc == INDEX_op_call) {
619 nb_oargs = op->callo;
620 nb_iargs = op->calli;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200621 for (i = 0; i < nb_oargs + nb_iargs; i++) {
622 tmp = args[i];
623 if (tmp != TCG_CALL_DUMMY_ARG) {
624 init_temp_info(tmp);
625 }
626 }
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200627 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700628 nb_oargs = def->nb_oargs;
629 nb_iargs = def->nb_iargs;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200630 for (i = 0; i < nb_oargs + nb_iargs; i++) {
631 init_temp_info(args[i]);
632 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700633 }
634
635 /* Do copy propagation */
636 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200637 if (temp_is_copy(args[i])) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700638 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400639 }
640 }
641
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400642 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700643 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400644 CASE_OP_32_64(add):
645 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400646 CASE_OP_32_64(and):
647 CASE_OP_32_64(or):
648 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700649 CASE_OP_32_64(eqv):
650 CASE_OP_32_64(nand):
651 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700652 CASE_OP_32_64(muluh):
653 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700654 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400655 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200656 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700657 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200658 args[2] = tcg_swap_cond(args[2]);
659 }
660 break;
661 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700662 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200663 args[3] = tcg_swap_cond(args[3]);
664 }
665 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700666 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700667 if (swap_commutative(-1, &args[1], &args[2])) {
668 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700669 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700670 /* For movcond, we canonicalize the "false" input reg to match
671 the destination reg so that the tcg backend can implement
672 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700673 if (swap_commutative(args[0], &args[4], &args[3])) {
674 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700675 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700676 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800677 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700678 swap_commutative(args[0], &args[2], &args[4]);
679 swap_commutative(args[1], &args[3], &args[5]);
680 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800681 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800682 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700683 swap_commutative(args[0], &args[2], &args[3]);
684 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700685 case INDEX_op_brcond2_i32:
686 if (swap_commutative2(&args[0], &args[2])) {
687 args[4] = tcg_swap_cond(args[4]);
688 }
689 break;
690 case INDEX_op_setcond2_i32:
691 if (swap_commutative2(&args[1], &args[3])) {
692 args[5] = tcg_swap_cond(args[5]);
693 }
694 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400695 default:
696 break;
697 }
698
Richard Henderson2d497542013-03-21 09:13:33 -0700699 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
700 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700701 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200702 CASE_OP_32_64(shl):
703 CASE_OP_32_64(shr):
704 CASE_OP_32_64(sar):
705 CASE_OP_32_64(rotl):
706 CASE_OP_32_64(rotr):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200707 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200708 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200709 continue;
710 }
711 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700712 CASE_OP_32_64(sub):
713 {
714 TCGOpcode neg_op;
715 bool have_neg;
716
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200717 if (temp_is_const(args[2])) {
Richard Henderson2d497542013-03-21 09:13:33 -0700718 /* Proceed with possible constant folding. */
719 break;
720 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700721 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700722 neg_op = INDEX_op_neg_i32;
723 have_neg = TCG_TARGET_HAS_neg_i32;
724 } else {
725 neg_op = INDEX_op_neg_i64;
726 have_neg = TCG_TARGET_HAS_neg_i64;
727 }
728 if (!have_neg) {
729 break;
730 }
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200731 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700732 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700733 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700734 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700735 continue;
736 }
737 }
738 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800739 CASE_OP_32_64(xor):
740 CASE_OP_32_64(nand):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200741 if (!temp_is_const(args[1])
742 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800743 i = 1;
744 goto try_not;
745 }
746 break;
747 CASE_OP_32_64(nor):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200748 if (!temp_is_const(args[1])
749 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800750 i = 1;
751 goto try_not;
752 }
753 break;
754 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200755 if (!temp_is_const(args[2])
756 && temp_is_const(args[1]) && temps[args[1]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800757 i = 2;
758 goto try_not;
759 }
760 break;
761 CASE_OP_32_64(orc):
762 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200763 if (!temp_is_const(args[2])
764 && temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800765 i = 2;
766 goto try_not;
767 }
768 break;
769 try_not:
770 {
771 TCGOpcode not_op;
772 bool have_not;
773
774 if (def->flags & TCG_OPF_64BIT) {
775 not_op = INDEX_op_not_i64;
776 have_not = TCG_TARGET_HAS_not_i64;
777 } else {
778 not_op = INDEX_op_not_i32;
779 have_not = TCG_TARGET_HAS_not_i32;
780 }
781 if (!have_not) {
782 break;
783 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700784 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800785 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700786 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800787 continue;
788 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200789 default:
790 break;
791 }
792
Richard Henderson464a1442014-01-31 07:42:11 -0600793 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700794 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400795 CASE_OP_32_64(add):
796 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400797 CASE_OP_32_64(shl):
798 CASE_OP_32_64(shr):
799 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700800 CASE_OP_32_64(rotl):
801 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200802 CASE_OP_32_64(or):
803 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600804 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200805 if (!temp_is_const(args[1])
806 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200807 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
808 continue;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400809 }
810 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600811 CASE_OP_32_64(and):
812 CASE_OP_32_64(orc):
813 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200814 if (!temp_is_const(args[1])
815 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200816 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
817 continue;
Richard Henderson464a1442014-01-31 07:42:11 -0600818 }
819 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200820 default:
821 break;
822 }
823
Aurelien Jarno30312442013-09-03 08:27:38 +0200824 /* Simplify using known-zero bits. Currently only ops with a single
825 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800826 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800827 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700828 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800829 CASE_OP_32_64(ext8s):
830 if ((temps[args[1]].mask & 0x80) != 0) {
831 break;
832 }
833 CASE_OP_32_64(ext8u):
834 mask = 0xff;
835 goto and_const;
836 CASE_OP_32_64(ext16s):
837 if ((temps[args[1]].mask & 0x8000) != 0) {
838 break;
839 }
840 CASE_OP_32_64(ext16u):
841 mask = 0xffff;
842 goto and_const;
843 case INDEX_op_ext32s_i64:
844 if ((temps[args[1]].mask & 0x80000000) != 0) {
845 break;
846 }
847 case INDEX_op_ext32u_i64:
848 mask = 0xffffffffU;
849 goto and_const;
850
851 CASE_OP_32_64(and):
852 mask = temps[args[2]].mask;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200853 if (temp_is_const(args[2])) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800854 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800855 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800856 }
857 mask = temps[args[1]].mask & mask;
858 break;
859
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800860 CASE_OP_32_64(andc):
861 /* Known-zeros does not imply known-ones. Therefore unless
862 args[2] is constant, we can't infer anything from it. */
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200863 if (temp_is_const(args[2])) {
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800864 mask = ~temps[args[2]].mask;
865 goto and_const;
866 }
867 /* But we certainly know nothing outside args[1] may be set. */
868 mask = temps[args[1]].mask;
869 break;
870
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200871 case INDEX_op_sar_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200872 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700873 tmp = temps[args[2]].val & 31;
874 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200875 }
876 break;
877 case INDEX_op_sar_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200878 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700879 tmp = temps[args[2]].val & 63;
880 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800881 }
882 break;
883
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200884 case INDEX_op_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200885 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700886 tmp = temps[args[2]].val & 31;
887 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200888 }
889 break;
890 case INDEX_op_shr_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200891 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700892 tmp = temps[args[2]].val & 63;
893 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800894 }
895 break;
896
Richard Henderson4bb7a412013-09-09 17:03:24 -0700897 case INDEX_op_trunc_shr_i32:
898 mask = (uint64_t)temps[args[1]].mask >> args[2];
899 break;
900
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800901 CASE_OP_32_64(shl):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200902 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700903 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
904 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800905 }
906 break;
907
908 CASE_OP_32_64(neg):
909 /* Set to 1 all bits to the left of the rightmost. */
910 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
911 break;
912
913 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700914 mask = deposit64(temps[args[1]].mask, args[3], args[4],
915 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800916 break;
917
918 CASE_OP_32_64(or):
919 CASE_OP_32_64(xor):
920 mask = temps[args[1]].mask | temps[args[2]].mask;
921 break;
922
923 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700924 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800925 mask = 1;
926 break;
927
928 CASE_OP_32_64(movcond):
929 mask = temps[args[3]].mask | temps[args[4]].mask;
930 break;
931
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200932 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200933 mask = 0xff;
934 break;
935 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200936 mask = 0xffff;
937 break;
938 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200939 mask = 0xffffffffu;
940 break;
941
942 CASE_OP_32_64(qemu_ld):
943 {
Richard Henderson59227d52015-05-12 11:51:44 -0700944 TCGMemOpIdx oi = args[nb_oargs + nb_iargs];
945 TCGMemOp mop = get_memop(oi);
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200946 if (!(mop & MO_SIGN)) {
947 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
948 }
949 }
950 break;
951
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800952 default:
953 break;
954 }
955
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700956 /* 32-bit ops generate 32-bit results. For the result is zero test
957 below, we can ignore high bits, but for further optimizations we
958 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700959 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700960 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700961 mask |= ~(tcg_target_ulong)0xffffffffu;
962 partmask &= 0xffffffffu;
963 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200964 }
965
Richard Henderson24666ba2014-05-22 11:14:10 -0700966 if (partmask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700967 assert(nb_oargs == 1);
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200968 tcg_opt_gen_movi(s, op, args, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800969 continue;
970 }
971 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700972 assert(nb_oargs == 1);
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200973 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800974 continue;
975 }
976
Aurelien Jarno56e49432012-09-06 16:47:13 +0200977 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700978 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200979 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400980 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700981 CASE_OP_32_64(muluh):
982 CASE_OP_32_64(mulsh):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200983 if ((temp_is_const(args[2]) && temps[args[2]].val == 0)) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200984 tcg_opt_gen_movi(s, op, args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400985 continue;
986 }
987 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200988 default:
989 break;
990 }
991
992 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700993 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400994 CASE_OP_32_64(or):
995 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200996 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200997 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400998 continue;
999 }
1000 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +00001001 default:
1002 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001003 }
1004
Aurelien Jarno3c941932012-09-18 19:12:36 +02001005 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001006 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -08001007 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +02001008 CASE_OP_32_64(sub):
1009 CASE_OP_32_64(xor):
1010 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001011 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +02001012 continue;
1013 }
1014 break;
1015 default:
1016 break;
1017 }
1018
Kirill Batuzov22613af2011-07-07 16:37:13 +04001019 /* Propagate constants through copy operations and do constant
1020 folding. Constants will be substituted to arguments by register
1021 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001022 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001023 CASE_OP_32_64(mov):
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001024 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
1025 break;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001026 CASE_OP_32_64(movi):
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001027 tcg_opt_gen_movi(s, op, args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001028 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001029
Kirill Batuzova640f032011-07-07 16:37:17 +04001030 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001031 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001032 CASE_OP_32_64(ext8s):
1033 CASE_OP_32_64(ext8u):
1034 CASE_OP_32_64(ext16s):
1035 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001036 case INDEX_op_ext32s_i64:
1037 case INDEX_op_ext32u_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001038 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001039 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001040 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001041 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001042 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001043 goto do_default;
1044
Richard Henderson4bb7a412013-09-09 17:03:24 -07001045 case INDEX_op_trunc_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001046 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001047 tmp = do_constant_folding(opc, temps[args[1]].val, args[2]);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001048 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001049 break;
1050 }
1051 goto do_default;
1052
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001053 CASE_OP_32_64(add):
1054 CASE_OP_32_64(sub):
1055 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001056 CASE_OP_32_64(or):
1057 CASE_OP_32_64(and):
1058 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001059 CASE_OP_32_64(shl):
1060 CASE_OP_32_64(shr):
1061 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001062 CASE_OP_32_64(rotl):
1063 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001064 CASE_OP_32_64(andc):
1065 CASE_OP_32_64(orc):
1066 CASE_OP_32_64(eqv):
1067 CASE_OP_32_64(nand):
1068 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001069 CASE_OP_32_64(muluh):
1070 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001071 CASE_OP_32_64(div):
1072 CASE_OP_32_64(divu):
1073 CASE_OP_32_64(rem):
1074 CASE_OP_32_64(remu):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001075 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001076 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001077 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001078 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001079 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001080 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001081 goto do_default;
1082
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001083 CASE_OP_32_64(deposit):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001084 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersond998e552014-03-18 14:23:52 -07001085 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1086 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001087 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001088 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001089 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001090 goto do_default;
1091
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001092 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001093 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001094 if (tmp != 2) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001095 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001096 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001097 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001098 goto do_default;
1099
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001100 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001101 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001102 if (tmp != 2) {
1103 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001104 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001105 op->opc = INDEX_op_br;
1106 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001107 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001108 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001109 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001110 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001111 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001112 goto do_default;
1113
Richard Hendersonfa01a202012-09-21 10:13:37 -07001114 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001115 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001116 if (tmp != 2) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001117 tcg_opt_gen_mov(s, op, args, args[0], args[4-tmp]);
Richard Henderson6e14e912012-10-02 11:32:24 -07001118 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001119 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001120 goto do_default;
1121
Richard Henderson212c3282012-10-02 11:32:28 -07001122 case INDEX_op_add2_i32:
1123 case INDEX_op_sub2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001124 if (temp_is_const(args[2]) && temp_is_const(args[3])
1125 && temp_is_const(args[4]) && temp_is_const(args[5])) {
Richard Henderson212c3282012-10-02 11:32:28 -07001126 uint32_t al = temps[args[2]].val;
1127 uint32_t ah = temps[args[3]].val;
1128 uint32_t bl = temps[args[4]].val;
1129 uint32_t bh = temps[args[5]].val;
1130 uint64_t a = ((uint64_t)ah << 32) | al;
1131 uint64_t b = ((uint64_t)bh << 32) | bl;
1132 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001133 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1134 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001135
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001136 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001137 a += b;
1138 } else {
1139 a -= b;
1140 }
1141
Richard Henderson212c3282012-10-02 11:32:28 -07001142 rl = args[0];
1143 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001144 tcg_opt_gen_movi(s, op, args, rl, (int32_t)a);
1145 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(a >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001146
1147 /* We've done all we need to do with the movi. Skip it. */
1148 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001149 break;
1150 }
1151 goto do_default;
1152
Richard Henderson14149682012-10-02 11:32:30 -07001153 case INDEX_op_mulu2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001154 if (temp_is_const(args[2]) && temp_is_const(args[3])) {
Richard Henderson14149682012-10-02 11:32:30 -07001155 uint32_t a = temps[args[2]].val;
1156 uint32_t b = temps[args[3]].val;
1157 uint64_t r = (uint64_t)a * b;
1158 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001159 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1160 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001161
1162 rl = args[0];
1163 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001164 tcg_opt_gen_movi(s, op, args, rl, (int32_t)r);
1165 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(r >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001166
1167 /* We've done all we need to do with the movi. Skip it. */
1168 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001169 break;
1170 }
1171 goto do_default;
1172
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001173 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001174 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1175 if (tmp != 2) {
1176 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001177 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001178 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001179 op->opc = INDEX_op_br;
1180 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001181 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001182 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001183 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001184 }
1185 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001186 && temp_is_const(args[2]) && temps[args[2]].val == 0
1187 && temp_is_const(args[3]) && temps[args[3]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001188 /* Simplify LT/GE comparisons vs zero to a single compare
1189 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001190 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001191 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001192 op->opc = INDEX_op_brcond_i32;
1193 args[0] = args[1];
1194 args[1] = args[3];
1195 args[2] = args[4];
1196 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001197 } else if (args[4] == TCG_COND_EQ) {
1198 /* Simplify EQ comparisons where one of the pairs
1199 can be simplified. */
1200 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1201 args[0], args[2], TCG_COND_EQ);
1202 if (tmp == 0) {
1203 goto do_brcond_false;
1204 } else if (tmp == 1) {
1205 goto do_brcond_high;
1206 }
1207 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1208 args[1], args[3], TCG_COND_EQ);
1209 if (tmp == 0) {
1210 goto do_brcond_false;
1211 } else if (tmp != 1) {
1212 goto do_default;
1213 }
1214 do_brcond_low:
1215 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001216 op->opc = INDEX_op_brcond_i32;
1217 args[1] = args[2];
1218 args[2] = args[4];
1219 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001220 } else if (args[4] == TCG_COND_NE) {
1221 /* Simplify NE comparisons where one of the pairs
1222 can be simplified. */
1223 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1224 args[0], args[2], TCG_COND_NE);
1225 if (tmp == 0) {
1226 goto do_brcond_high;
1227 } else if (tmp == 1) {
1228 goto do_brcond_true;
1229 }
1230 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1231 args[1], args[3], TCG_COND_NE);
1232 if (tmp == 0) {
1233 goto do_brcond_low;
1234 } else if (tmp == 1) {
1235 goto do_brcond_true;
1236 }
1237 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001238 } else {
1239 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001240 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001241 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001242
1243 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001244 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1245 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001246 do_setcond_const:
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001247 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001248 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001249 && temp_is_const(args[3]) && temps[args[3]].val == 0
1250 && temp_is_const(args[4]) && temps[args[4]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001251 /* Simplify LT/GE comparisons vs zero to a single compare
1252 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001253 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001254 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001255 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001256 op->opc = INDEX_op_setcond_i32;
1257 args[1] = args[2];
1258 args[2] = args[4];
1259 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001260 } else if (args[5] == TCG_COND_EQ) {
1261 /* Simplify EQ comparisons where one of the pairs
1262 can be simplified. */
1263 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1264 args[1], args[3], TCG_COND_EQ);
1265 if (tmp == 0) {
1266 goto do_setcond_const;
1267 } else if (tmp == 1) {
1268 goto do_setcond_high;
1269 }
1270 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1271 args[2], args[4], TCG_COND_EQ);
1272 if (tmp == 0) {
1273 goto do_setcond_high;
1274 } else if (tmp != 1) {
1275 goto do_default;
1276 }
1277 do_setcond_low:
1278 reset_temp(args[0]);
1279 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001280 op->opc = INDEX_op_setcond_i32;
1281 args[2] = args[3];
1282 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001283 } else if (args[5] == TCG_COND_NE) {
1284 /* Simplify NE comparisons where one of the pairs
1285 can be simplified. */
1286 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1287 args[1], args[3], TCG_COND_NE);
1288 if (tmp == 0) {
1289 goto do_setcond_high;
1290 } else if (tmp == 1) {
1291 goto do_setcond_const;
1292 }
1293 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1294 args[2], args[4], TCG_COND_NE);
1295 if (tmp == 0) {
1296 goto do_setcond_low;
1297 } else if (tmp == 1) {
1298 goto do_setcond_const;
1299 }
1300 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001301 } else {
1302 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001303 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001304 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001305
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001306 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001307 if (!(args[nb_oargs + nb_iargs + 1]
1308 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001309 for (i = 0; i < nb_globals; i++) {
Aurelien Jarno1208d7d2015-07-27 12:41:44 +02001310 if (test_bit(i, temps_used.l)) {
1311 reset_temp(i);
1312 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001313 }
1314 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001315 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001316
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001317 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001318 do_default:
1319 /* Default case: we know nothing about operation (or were unable
1320 to compute the operation result) so no propagation is done.
1321 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001322 block, otherwise we only trash the output args. "mask" is
1323 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001324 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001325 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001326 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001327 do_reset_output:
1328 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001329 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001330 /* Save the corresponding known-zero bits mask for the
1331 first output argument (only one supported so far). */
1332 if (i == 0) {
1333 temps[args[i]].mask = mask;
1334 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001335 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001336 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001337 break;
1338 }
1339 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001340}