blob: c0d975b3d95178a8acf7e205d4f98d9063cd1605 [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
Peter Maydell757e7252016-01-26 18:17:08 +000026#include "qemu/osdep.h"
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040027#include "qemu-common.h"
Paolo Bonzini00f6da62016-03-15 13:16:36 +010028#include "exec/cpu-common.h"
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040029#include "tcg-op.h"
30
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040031#define CASE_OP_32_64(x) \
32 glue(glue(case INDEX_op_, x), _i32): \
33 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040034
Kirill Batuzov22613af2011-07-07 16:37:13 +040035struct tcg_temp_info {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020036 bool is_const;
Kirill Batuzov22613af2011-07-07 16:37:13 +040037 uint16_t prev_copy;
38 uint16_t next_copy;
39 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080040 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040041};
42
43static struct tcg_temp_info temps[TCG_MAX_TEMPS];
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020044static TCGTempSet temps_used;
Kirill Batuzov22613af2011-07-07 16:37:13 +040045
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020046static inline bool temp_is_const(TCGArg arg)
47{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020048 return temps[arg].is_const;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020049}
50
51static inline bool temp_is_copy(TCGArg arg)
52{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020053 return temps[arg].next_copy != arg;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020054}
55
Aurelien Jarnob41059d2015-07-27 12:41:44 +020056/* Reset TEMP's state, possibly removing the temp for the list of copies. */
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020057static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040058{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020059 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
60 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
61 temps[temp].next_copy = temp;
62 temps[temp].prev_copy = temp;
63 temps[temp].is_const = false;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080064 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040065}
66
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020067/* Reset all temporaries, given that there are NB_TEMPS of them. */
68static void reset_all_temps(int nb_temps)
69{
70 bitmap_zero(temps_used.l, nb_temps);
71}
72
73/* Initialize and activate a temporary. */
74static void init_temp_info(TCGArg temp)
75{
76 if (!test_bit(temp, temps_used.l)) {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020077 temps[temp].next_copy = temp;
78 temps[temp].prev_copy = temp;
79 temps[temp].is_const = false;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020080 temps[temp].mask = -1;
81 set_bit(temp, temps_used.l);
82 }
83}
84
Richard Hendersona4ce0992014-03-30 17:14:02 -070085static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
86 TCGOpcode opc, int nargs)
87{
88 int oi = s->gen_next_op_idx;
89 int pi = s->gen_next_parm_idx;
90 int prev = old_op->prev;
91 int next = old_op - s->gen_op_buf;
92 TCGOp *new_op;
93
94 tcg_debug_assert(oi < OPC_BUF_SIZE);
95 tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
96 s->gen_next_op_idx = oi + 1;
97 s->gen_next_parm_idx = pi + nargs;
98
99 new_op = &s->gen_op_buf[oi];
100 *new_op = (TCGOp){
101 .opc = opc,
102 .args = pi,
103 .prev = prev,
104 .next = next
105 };
106 if (prev >= 0) {
107 s->gen_op_buf[prev].next = oi;
108 } else {
109 s->gen_first_op_idx = oi;
110 }
111 old_op->prev = oi;
112
113 return new_op;
114}
115
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000116static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400117{
Richard Henderson8399ad52011-08-17 14:11:45 -0700118 const TCGOpDef *def = &tcg_op_defs[op];
119 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400120}
121
Richard Hendersona62f6f52014-05-22 10:59:12 -0700122static TCGOpcode op_to_mov(TCGOpcode op)
123{
124 switch (op_bits(op)) {
125 case 32:
126 return INDEX_op_mov_i32;
127 case 64:
128 return INDEX_op_mov_i64;
129 default:
130 fprintf(stderr, "op_to_mov: unexpected return value of "
131 "function op_bits.\n");
132 tcg_abort();
133 }
134}
135
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000136static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400137{
138 switch (op_bits(op)) {
139 case 32:
140 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400141 case 64:
142 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400143 default:
144 fprintf(stderr, "op_to_movi: unexpected return value of "
145 "function op_bits.\n");
146 tcg_abort();
147 }
148}
149
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200150static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
151{
152 TCGArg i;
153
154 /* If this is already a global, we can't do better. */
155 if (temp < s->nb_globals) {
156 return temp;
157 }
158
159 /* Search for a global first. */
160 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
161 if (i < s->nb_globals) {
162 return i;
163 }
164 }
165
166 /* If it is a temp, search for a temp local. */
167 if (!s->temps[temp].temp_local) {
168 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
169 if (s->temps[i].temp_local) {
170 return i;
171 }
172 }
173 }
174
175 /* Failure to find a better representation, return the same temp. */
176 return temp;
177}
178
179static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
180{
181 TCGArg i;
182
183 if (arg1 == arg2) {
184 return true;
185 }
186
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200187 if (!temp_is_copy(arg1) || !temp_is_copy(arg2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200188 return false;
189 }
190
191 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
192 if (i == arg2) {
193 return true;
194 }
195 }
196
197 return false;
198}
199
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200200static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
201 TCGArg dst, TCGArg val)
202{
203 TCGOpcode new_op = op_to_movi(op->opc);
204 tcg_target_ulong mask;
205
206 op->opc = new_op;
207
208 reset_temp(dst);
Aurelien Jarnob41059d2015-07-27 12:41:44 +0200209 temps[dst].is_const = true;
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200210 temps[dst].val = val;
211 mask = val;
Aurelien Jarno96152122015-07-10 18:03:30 +0200212 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_movi_i32) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200213 /* High bits of the destination are now garbage. */
214 mask |= ~0xffffffffull;
215 }
216 temps[dst].mask = mask;
217
218 args[0] = dst;
219 args[1] = val;
220}
221
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700222static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200223 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400224{
Aurelien Jarno53657182015-06-04 21:53:25 +0200225 if (temps_are_copies(dst, src)) {
226 tcg_op_remove(s, op);
227 return;
228 }
229
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200230 TCGOpcode new_op = op_to_mov(op->opc);
Richard Henderson24666ba2014-05-22 11:14:10 -0700231 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700232
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700233 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700234
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800235 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700236 mask = temps[src].mask;
237 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
238 /* High bits of the destination are now garbage. */
239 mask |= ~0xffffffffull;
240 }
241 temps[dst].mask = mask;
242
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800243 if (s->temps[src].type == s->temps[dst].type) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800244 temps[dst].next_copy = temps[src].next_copy;
245 temps[dst].prev_copy = src;
246 temps[temps[dst].next_copy].prev_copy = dst;
247 temps[src].next_copy = dst;
Aurelien Jarno299f8012015-07-27 12:41:44 +0200248 temps[dst].is_const = temps[src].is_const;
249 temps[dst].val = temps[src].val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800250 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200251
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700252 args[0] = dst;
253 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400254}
255
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000256static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400257{
Richard Henderson03271522013-08-14 14:35:56 -0700258 uint64_t l64, h64;
259
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400260 switch (op) {
261 CASE_OP_32_64(add):
262 return x + y;
263
264 CASE_OP_32_64(sub):
265 return x - y;
266
267 CASE_OP_32_64(mul):
268 return x * y;
269
Kirill Batuzov9a810902011-07-07 16:37:15 +0400270 CASE_OP_32_64(and):
271 return x & y;
272
273 CASE_OP_32_64(or):
274 return x | y;
275
276 CASE_OP_32_64(xor):
277 return x ^ y;
278
Kirill Batuzov55c09752011-07-07 16:37:16 +0400279 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700280 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400281
Kirill Batuzov55c09752011-07-07 16:37:16 +0400282 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700283 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400284
285 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700286 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400287
Kirill Batuzov55c09752011-07-07 16:37:16 +0400288 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700289 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400290
291 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700292 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400293
Kirill Batuzov55c09752011-07-07 16:37:16 +0400294 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700295 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400296
297 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700298 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400299
Kirill Batuzov55c09752011-07-07 16:37:16 +0400300 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700301 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400302
303 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700304 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400305
Kirill Batuzov55c09752011-07-07 16:37:16 +0400306 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700307 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400308
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700309 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400310 return ~x;
311
Richard Hendersoncb25c802011-08-17 14:11:47 -0700312 CASE_OP_32_64(neg):
313 return -x;
314
315 CASE_OP_32_64(andc):
316 return x & ~y;
317
318 CASE_OP_32_64(orc):
319 return x | ~y;
320
321 CASE_OP_32_64(eqv):
322 return ~(x ^ y);
323
324 CASE_OP_32_64(nand):
325 return ~(x & y);
326
327 CASE_OP_32_64(nor):
328 return ~(x | y);
329
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700330 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400331 return (int8_t)x;
332
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700333 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400334 return (int16_t)x;
335
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700336 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400337 return (uint8_t)x;
338
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700339 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400340 return (uint16_t)x;
341
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200342 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400343 case INDEX_op_ext32s_i64:
344 return (int32_t)x;
345
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200346 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700347 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400348 case INDEX_op_ext32u_i64:
349 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400350
Richard Henderson609ad702015-07-24 07:16:00 -0700351 case INDEX_op_extrh_i64_i32:
352 return (uint64_t)x >> 32;
353
Richard Henderson03271522013-08-14 14:35:56 -0700354 case INDEX_op_muluh_i32:
355 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
356 case INDEX_op_mulsh_i32:
357 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
358
359 case INDEX_op_muluh_i64:
360 mulu64(&l64, &h64, x, y);
361 return h64;
362 case INDEX_op_mulsh_i64:
363 muls64(&l64, &h64, x, y);
364 return h64;
365
Richard Henderson01547f72013-08-14 15:22:46 -0700366 case INDEX_op_div_i32:
367 /* Avoid crashing on divide by zero, otherwise undefined. */
368 return (int32_t)x / ((int32_t)y ? : 1);
369 case INDEX_op_divu_i32:
370 return (uint32_t)x / ((uint32_t)y ? : 1);
371 case INDEX_op_div_i64:
372 return (int64_t)x / ((int64_t)y ? : 1);
373 case INDEX_op_divu_i64:
374 return (uint64_t)x / ((uint64_t)y ? : 1);
375
376 case INDEX_op_rem_i32:
377 return (int32_t)x % ((int32_t)y ? : 1);
378 case INDEX_op_remu_i32:
379 return (uint32_t)x % ((uint32_t)y ? : 1);
380 case INDEX_op_rem_i64:
381 return (int64_t)x % ((int64_t)y ? : 1);
382 case INDEX_op_remu_i64:
383 return (uint64_t)x % ((uint64_t)y ? : 1);
384
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400385 default:
386 fprintf(stderr,
387 "Unrecognized operation %d in do_constant_folding.\n", op);
388 tcg_abort();
389 }
390}
391
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000392static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400393{
394 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400395 if (op_bits(op) == 32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200396 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400397 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400398 return res;
399}
400
Richard Henderson9519da72012-10-02 11:32:26 -0700401static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
402{
403 switch (c) {
404 case TCG_COND_EQ:
405 return x == y;
406 case TCG_COND_NE:
407 return x != y;
408 case TCG_COND_LT:
409 return (int32_t)x < (int32_t)y;
410 case TCG_COND_GE:
411 return (int32_t)x >= (int32_t)y;
412 case TCG_COND_LE:
413 return (int32_t)x <= (int32_t)y;
414 case TCG_COND_GT:
415 return (int32_t)x > (int32_t)y;
416 case TCG_COND_LTU:
417 return x < y;
418 case TCG_COND_GEU:
419 return x >= y;
420 case TCG_COND_LEU:
421 return x <= y;
422 case TCG_COND_GTU:
423 return x > y;
424 default:
425 tcg_abort();
426 }
427}
428
429static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
430{
431 switch (c) {
432 case TCG_COND_EQ:
433 return x == y;
434 case TCG_COND_NE:
435 return x != y;
436 case TCG_COND_LT:
437 return (int64_t)x < (int64_t)y;
438 case TCG_COND_GE:
439 return (int64_t)x >= (int64_t)y;
440 case TCG_COND_LE:
441 return (int64_t)x <= (int64_t)y;
442 case TCG_COND_GT:
443 return (int64_t)x > (int64_t)y;
444 case TCG_COND_LTU:
445 return x < y;
446 case TCG_COND_GEU:
447 return x >= y;
448 case TCG_COND_LEU:
449 return x <= y;
450 case TCG_COND_GTU:
451 return x > y;
452 default:
453 tcg_abort();
454 }
455}
456
457static bool do_constant_folding_cond_eq(TCGCond c)
458{
459 switch (c) {
460 case TCG_COND_GT:
461 case TCG_COND_LTU:
462 case TCG_COND_LT:
463 case TCG_COND_GTU:
464 case TCG_COND_NE:
465 return 0;
466 case TCG_COND_GE:
467 case TCG_COND_GEU:
468 case TCG_COND_LE:
469 case TCG_COND_LEU:
470 case TCG_COND_EQ:
471 return 1;
472 default:
473 tcg_abort();
474 }
475}
476
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200477/* Return 2 if the condition can't be simplified, and the result
478 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200479static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
480 TCGArg y, TCGCond c)
481{
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200482 if (temp_is_const(x) && temp_is_const(y)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200483 switch (op_bits(op)) {
484 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700485 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200486 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700487 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
488 default:
489 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200490 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200491 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700492 return do_constant_folding_cond_eq(c);
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200493 } else if (temp_is_const(y) && temps[y].val == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200494 switch (c) {
495 case TCG_COND_LTU:
496 return 0;
497 case TCG_COND_GEU:
498 return 1;
499 default:
500 return 2;
501 }
502 } else {
503 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200504 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200505}
506
Richard Henderson6c4382f2012-10-02 11:32:27 -0700507/* Return 2 if the condition can't be simplified, and the result
508 of the condition (0 or 1) if it can */
509static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
510{
511 TCGArg al = p1[0], ah = p1[1];
512 TCGArg bl = p2[0], bh = p2[1];
513
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200514 if (temp_is_const(bl) && temp_is_const(bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700515 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
516
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200517 if (temp_is_const(al) && temp_is_const(ah)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700518 uint64_t a;
519 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
520 return do_constant_folding_cond_64(a, b, c);
521 }
522 if (b == 0) {
523 switch (c) {
524 case TCG_COND_LTU:
525 return 0;
526 case TCG_COND_GEU:
527 return 1;
528 default:
529 break;
530 }
531 }
532 }
533 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
534 return do_constant_folding_cond_eq(c);
535 }
536 return 2;
537}
538
Richard Henderson24c9ae42012-10-02 11:32:21 -0700539static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
540{
541 TCGArg a1 = *p1, a2 = *p2;
542 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200543 sum += temp_is_const(a1);
544 sum -= temp_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700545
546 /* Prefer the constant in second argument, and then the form
547 op a, a, b, which is better handled on non-RISC hosts. */
548 if (sum > 0 || (sum == 0 && dest == a2)) {
549 *p1 = a2;
550 *p2 = a1;
551 return true;
552 }
553 return false;
554}
555
Richard Henderson0bfcb862012-10-02 11:32:23 -0700556static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
557{
558 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200559 sum += temp_is_const(p1[0]);
560 sum += temp_is_const(p1[1]);
561 sum -= temp_is_const(p2[0]);
562 sum -= temp_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700563 if (sum > 0) {
564 TCGArg t;
565 t = p1[0], p1[0] = p2[0], p2[0] = t;
566 t = p1[1], p1[1] = p2[1], p2[1] = t;
567 return true;
568 }
569 return false;
570}
571
Kirill Batuzov22613af2011-07-07 16:37:13 +0400572/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +0200573void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400574{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700575 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700576
Kirill Batuzov22613af2011-07-07 16:37:13 +0400577 /* Array VALS has an element for each temp.
578 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200579 If this temp is a copy of other ones then the other copies are
580 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400581
582 nb_temps = s->nb_temps;
583 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800584 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400585
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700586 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700587 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700588 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700589 TCGArg tmp;
590
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700591 TCGOp * const op = &s->gen_op_buf[oi];
592 TCGArg * const args = &s->gen_opparam_buf[op->args];
593 TCGOpcode opc = op->opc;
594 const TCGOpDef *def = &tcg_op_defs[opc];
595
596 oi_next = op->next;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200597
598 /* Count the arguments, and initialize the temps that are
599 going to be used */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700600 if (opc == INDEX_op_call) {
601 nb_oargs = op->callo;
602 nb_iargs = op->calli;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200603 for (i = 0; i < nb_oargs + nb_iargs; i++) {
604 tmp = args[i];
605 if (tmp != TCG_CALL_DUMMY_ARG) {
606 init_temp_info(tmp);
607 }
608 }
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200609 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700610 nb_oargs = def->nb_oargs;
611 nb_iargs = def->nb_iargs;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200612 for (i = 0; i < nb_oargs + nb_iargs; i++) {
613 init_temp_info(args[i]);
614 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700615 }
616
617 /* Do copy propagation */
618 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200619 if (temp_is_copy(args[i])) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700620 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400621 }
622 }
623
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400624 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700625 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400626 CASE_OP_32_64(add):
627 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400628 CASE_OP_32_64(and):
629 CASE_OP_32_64(or):
630 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700631 CASE_OP_32_64(eqv):
632 CASE_OP_32_64(nand):
633 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700634 CASE_OP_32_64(muluh):
635 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700636 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400637 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200638 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700639 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200640 args[2] = tcg_swap_cond(args[2]);
641 }
642 break;
643 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700644 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200645 args[3] = tcg_swap_cond(args[3]);
646 }
647 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700648 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700649 if (swap_commutative(-1, &args[1], &args[2])) {
650 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700651 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700652 /* For movcond, we canonicalize the "false" input reg to match
653 the destination reg so that the tcg backend can implement
654 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700655 if (swap_commutative(args[0], &args[4], &args[3])) {
656 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700657 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700658 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800659 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700660 swap_commutative(args[0], &args[2], &args[4]);
661 swap_commutative(args[1], &args[3], &args[5]);
662 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800663 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800664 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700665 swap_commutative(args[0], &args[2], &args[3]);
666 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700667 case INDEX_op_brcond2_i32:
668 if (swap_commutative2(&args[0], &args[2])) {
669 args[4] = tcg_swap_cond(args[4]);
670 }
671 break;
672 case INDEX_op_setcond2_i32:
673 if (swap_commutative2(&args[1], &args[3])) {
674 args[5] = tcg_swap_cond(args[5]);
675 }
676 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400677 default:
678 break;
679 }
680
Richard Henderson2d497542013-03-21 09:13:33 -0700681 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
682 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700683 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200684 CASE_OP_32_64(shl):
685 CASE_OP_32_64(shr):
686 CASE_OP_32_64(sar):
687 CASE_OP_32_64(rotl):
688 CASE_OP_32_64(rotr):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200689 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200690 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200691 continue;
692 }
693 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700694 CASE_OP_32_64(sub):
695 {
696 TCGOpcode neg_op;
697 bool have_neg;
698
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200699 if (temp_is_const(args[2])) {
Richard Henderson2d497542013-03-21 09:13:33 -0700700 /* Proceed with possible constant folding. */
701 break;
702 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700703 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700704 neg_op = INDEX_op_neg_i32;
705 have_neg = TCG_TARGET_HAS_neg_i32;
706 } else {
707 neg_op = INDEX_op_neg_i64;
708 have_neg = TCG_TARGET_HAS_neg_i64;
709 }
710 if (!have_neg) {
711 break;
712 }
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200713 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700714 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700715 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700716 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700717 continue;
718 }
719 }
720 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800721 CASE_OP_32_64(xor):
722 CASE_OP_32_64(nand):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200723 if (!temp_is_const(args[1])
724 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800725 i = 1;
726 goto try_not;
727 }
728 break;
729 CASE_OP_32_64(nor):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200730 if (!temp_is_const(args[1])
731 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800732 i = 1;
733 goto try_not;
734 }
735 break;
736 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200737 if (!temp_is_const(args[2])
738 && temp_is_const(args[1]) && temps[args[1]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800739 i = 2;
740 goto try_not;
741 }
742 break;
743 CASE_OP_32_64(orc):
744 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200745 if (!temp_is_const(args[2])
746 && temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800747 i = 2;
748 goto try_not;
749 }
750 break;
751 try_not:
752 {
753 TCGOpcode not_op;
754 bool have_not;
755
756 if (def->flags & TCG_OPF_64BIT) {
757 not_op = INDEX_op_not_i64;
758 have_not = TCG_TARGET_HAS_not_i64;
759 } else {
760 not_op = INDEX_op_not_i32;
761 have_not = TCG_TARGET_HAS_not_i32;
762 }
763 if (!have_not) {
764 break;
765 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700766 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800767 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700768 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800769 continue;
770 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200771 default:
772 break;
773 }
774
Richard Henderson464a1442014-01-31 07:42:11 -0600775 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700776 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400777 CASE_OP_32_64(add):
778 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400779 CASE_OP_32_64(shl):
780 CASE_OP_32_64(shr):
781 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700782 CASE_OP_32_64(rotl):
783 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200784 CASE_OP_32_64(or):
785 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600786 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200787 if (!temp_is_const(args[1])
788 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200789 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
790 continue;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400791 }
792 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600793 CASE_OP_32_64(and):
794 CASE_OP_32_64(orc):
795 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200796 if (!temp_is_const(args[1])
797 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200798 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
799 continue;
Richard Henderson464a1442014-01-31 07:42:11 -0600800 }
801 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200802 default:
803 break;
804 }
805
Aurelien Jarno30312442013-09-03 08:27:38 +0200806 /* Simplify using known-zero bits. Currently only ops with a single
807 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800808 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800809 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700810 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800811 CASE_OP_32_64(ext8s):
812 if ((temps[args[1]].mask & 0x80) != 0) {
813 break;
814 }
815 CASE_OP_32_64(ext8u):
816 mask = 0xff;
817 goto and_const;
818 CASE_OP_32_64(ext16s):
819 if ((temps[args[1]].mask & 0x8000) != 0) {
820 break;
821 }
822 CASE_OP_32_64(ext16u):
823 mask = 0xffff;
824 goto and_const;
825 case INDEX_op_ext32s_i64:
826 if ((temps[args[1]].mask & 0x80000000) != 0) {
827 break;
828 }
829 case INDEX_op_ext32u_i64:
830 mask = 0xffffffffU;
831 goto and_const;
832
833 CASE_OP_32_64(and):
834 mask = temps[args[2]].mask;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200835 if (temp_is_const(args[2])) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800836 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800837 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800838 }
839 mask = temps[args[1]].mask & mask;
840 break;
841
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200842 case INDEX_op_ext_i32_i64:
843 if ((temps[args[1]].mask & 0x80000000) != 0) {
844 break;
845 }
846 case INDEX_op_extu_i32_i64:
847 /* We do not compute affected as it is a size changing op. */
848 mask = (uint32_t)temps[args[1]].mask;
849 break;
850
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800851 CASE_OP_32_64(andc):
852 /* Known-zeros does not imply known-ones. Therefore unless
853 args[2] is constant, we can't infer anything from it. */
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200854 if (temp_is_const(args[2])) {
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800855 mask = ~temps[args[2]].mask;
856 goto and_const;
857 }
858 /* But we certainly know nothing outside args[1] may be set. */
859 mask = temps[args[1]].mask;
860 break;
861
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200862 case INDEX_op_sar_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200863 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700864 tmp = temps[args[2]].val & 31;
865 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200866 }
867 break;
868 case INDEX_op_sar_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200869 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700870 tmp = temps[args[2]].val & 63;
871 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800872 }
873 break;
874
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200875 case INDEX_op_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200876 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700877 tmp = temps[args[2]].val & 31;
878 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200879 }
880 break;
881 case INDEX_op_shr_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200882 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700883 tmp = temps[args[2]].val & 63;
884 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800885 }
886 break;
887
Richard Henderson609ad702015-07-24 07:16:00 -0700888 case INDEX_op_extrl_i64_i32:
889 mask = (uint32_t)temps[args[1]].mask;
890 break;
891 case INDEX_op_extrh_i64_i32:
892 mask = (uint64_t)temps[args[1]].mask >> 32;
Richard Henderson4bb7a412013-09-09 17:03:24 -0700893 break;
894
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800895 CASE_OP_32_64(shl):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200896 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700897 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
898 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800899 }
900 break;
901
902 CASE_OP_32_64(neg):
903 /* Set to 1 all bits to the left of the rightmost. */
904 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
905 break;
906
907 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700908 mask = deposit64(temps[args[1]].mask, args[3], args[4],
909 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800910 break;
911
912 CASE_OP_32_64(or):
913 CASE_OP_32_64(xor):
914 mask = temps[args[1]].mask | temps[args[2]].mask;
915 break;
916
917 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700918 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800919 mask = 1;
920 break;
921
922 CASE_OP_32_64(movcond):
923 mask = temps[args[3]].mask | temps[args[4]].mask;
924 break;
925
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200926 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200927 mask = 0xff;
928 break;
929 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200930 mask = 0xffff;
931 break;
932 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200933 mask = 0xffffffffu;
934 break;
935
936 CASE_OP_32_64(qemu_ld):
937 {
Richard Henderson59227d52015-05-12 11:51:44 -0700938 TCGMemOpIdx oi = args[nb_oargs + nb_iargs];
939 TCGMemOp mop = get_memop(oi);
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200940 if (!(mop & MO_SIGN)) {
941 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
942 }
943 }
944 break;
945
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800946 default:
947 break;
948 }
949
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700950 /* 32-bit ops generate 32-bit results. For the result is zero test
951 below, we can ignore high bits, but for further optimizations we
952 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700953 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700954 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700955 mask |= ~(tcg_target_ulong)0xffffffffu;
956 partmask &= 0xffffffffu;
957 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200958 }
959
Richard Henderson24666ba2014-05-22 11:14:10 -0700960 if (partmask == 0) {
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +0200961 tcg_debug_assert(nb_oargs == 1);
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200962 tcg_opt_gen_movi(s, op, args, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800963 continue;
964 }
965 if (affected == 0) {
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +0200966 tcg_debug_assert(nb_oargs == 1);
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200967 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800968 continue;
969 }
970
Aurelien Jarno56e49432012-09-06 16:47:13 +0200971 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700972 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200973 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400974 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700975 CASE_OP_32_64(muluh):
976 CASE_OP_32_64(mulsh):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200977 if ((temp_is_const(args[2]) && temps[args[2]].val == 0)) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200978 tcg_opt_gen_movi(s, op, args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400979 continue;
980 }
981 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200982 default:
983 break;
984 }
985
986 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700987 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400988 CASE_OP_32_64(or):
989 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200990 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200991 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400992 continue;
993 }
994 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000995 default:
996 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400997 }
998
Aurelien Jarno3c941932012-09-18 19:12:36 +0200999 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001000 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -08001001 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +02001002 CASE_OP_32_64(sub):
1003 CASE_OP_32_64(xor):
1004 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001005 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +02001006 continue;
1007 }
1008 break;
1009 default:
1010 break;
1011 }
1012
Kirill Batuzov22613af2011-07-07 16:37:13 +04001013 /* Propagate constants through copy operations and do constant
1014 folding. Constants will be substituted to arguments by register
1015 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001016 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001017 CASE_OP_32_64(mov):
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001018 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
1019 break;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001020 CASE_OP_32_64(movi):
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001021 tcg_opt_gen_movi(s, op, args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001022 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001023
Kirill Batuzova640f032011-07-07 16:37:17 +04001024 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001025 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001026 CASE_OP_32_64(ext8s):
1027 CASE_OP_32_64(ext8u):
1028 CASE_OP_32_64(ext16s):
1029 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001030 case INDEX_op_ext32s_i64:
1031 case INDEX_op_ext32u_i64:
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +02001032 case INDEX_op_ext_i32_i64:
1033 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -07001034 case INDEX_op_extrl_i64_i32:
1035 case INDEX_op_extrh_i64_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001036 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001037 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001038 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001039 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001040 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001041 goto do_default;
1042
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001043 CASE_OP_32_64(add):
1044 CASE_OP_32_64(sub):
1045 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001046 CASE_OP_32_64(or):
1047 CASE_OP_32_64(and):
1048 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001049 CASE_OP_32_64(shl):
1050 CASE_OP_32_64(shr):
1051 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001052 CASE_OP_32_64(rotl):
1053 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001054 CASE_OP_32_64(andc):
1055 CASE_OP_32_64(orc):
1056 CASE_OP_32_64(eqv):
1057 CASE_OP_32_64(nand):
1058 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001059 CASE_OP_32_64(muluh):
1060 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001061 CASE_OP_32_64(div):
1062 CASE_OP_32_64(divu):
1063 CASE_OP_32_64(rem):
1064 CASE_OP_32_64(remu):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001065 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001066 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001067 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001068 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001069 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001070 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001071 goto do_default;
1072
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001073 CASE_OP_32_64(deposit):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001074 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersond998e552014-03-18 14:23:52 -07001075 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1076 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001077 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001078 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001079 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001080 goto do_default;
1081
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001082 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001083 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001084 if (tmp != 2) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001085 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001086 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001087 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001088 goto do_default;
1089
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001090 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001091 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001092 if (tmp != 2) {
1093 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001094 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001095 op->opc = INDEX_op_br;
1096 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001097 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001098 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001099 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001100 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001101 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001102 goto do_default;
1103
Richard Hendersonfa01a202012-09-21 10:13:37 -07001104 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001105 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001106 if (tmp != 2) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001107 tcg_opt_gen_mov(s, op, args, args[0], args[4-tmp]);
Richard Henderson6e14e912012-10-02 11:32:24 -07001108 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001109 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001110 goto do_default;
1111
Richard Henderson212c3282012-10-02 11:32:28 -07001112 case INDEX_op_add2_i32:
1113 case INDEX_op_sub2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001114 if (temp_is_const(args[2]) && temp_is_const(args[3])
1115 && temp_is_const(args[4]) && temp_is_const(args[5])) {
Richard Henderson212c3282012-10-02 11:32:28 -07001116 uint32_t al = temps[args[2]].val;
1117 uint32_t ah = temps[args[3]].val;
1118 uint32_t bl = temps[args[4]].val;
1119 uint32_t bh = temps[args[5]].val;
1120 uint64_t a = ((uint64_t)ah << 32) | al;
1121 uint64_t b = ((uint64_t)bh << 32) | bl;
1122 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001123 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1124 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001125
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001126 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001127 a += b;
1128 } else {
1129 a -= b;
1130 }
1131
Richard Henderson212c3282012-10-02 11:32:28 -07001132 rl = args[0];
1133 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001134 tcg_opt_gen_movi(s, op, args, rl, (int32_t)a);
1135 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(a >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001136
1137 /* We've done all we need to do with the movi. Skip it. */
1138 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001139 break;
1140 }
1141 goto do_default;
1142
Richard Henderson14149682012-10-02 11:32:30 -07001143 case INDEX_op_mulu2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001144 if (temp_is_const(args[2]) && temp_is_const(args[3])) {
Richard Henderson14149682012-10-02 11:32:30 -07001145 uint32_t a = temps[args[2]].val;
1146 uint32_t b = temps[args[3]].val;
1147 uint64_t r = (uint64_t)a * b;
1148 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001149 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1150 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001151
1152 rl = args[0];
1153 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001154 tcg_opt_gen_movi(s, op, args, rl, (int32_t)r);
1155 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(r >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001156
1157 /* We've done all we need to do with the movi. Skip it. */
1158 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001159 break;
1160 }
1161 goto do_default;
1162
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001163 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001164 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1165 if (tmp != 2) {
1166 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001167 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001168 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001169 op->opc = INDEX_op_br;
1170 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001171 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001172 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001173 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001174 }
1175 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001176 && temp_is_const(args[2]) && temps[args[2]].val == 0
1177 && temp_is_const(args[3]) && temps[args[3]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001178 /* Simplify LT/GE comparisons vs zero to a single compare
1179 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001180 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001181 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001182 op->opc = INDEX_op_brcond_i32;
1183 args[0] = args[1];
1184 args[1] = args[3];
1185 args[2] = args[4];
1186 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001187 } else if (args[4] == TCG_COND_EQ) {
1188 /* Simplify EQ comparisons where one of the pairs
1189 can be simplified. */
1190 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1191 args[0], args[2], TCG_COND_EQ);
1192 if (tmp == 0) {
1193 goto do_brcond_false;
1194 } else if (tmp == 1) {
1195 goto do_brcond_high;
1196 }
1197 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1198 args[1], args[3], TCG_COND_EQ);
1199 if (tmp == 0) {
1200 goto do_brcond_false;
1201 } else if (tmp != 1) {
1202 goto do_default;
1203 }
1204 do_brcond_low:
1205 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001206 op->opc = INDEX_op_brcond_i32;
1207 args[1] = args[2];
1208 args[2] = args[4];
1209 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001210 } else if (args[4] == TCG_COND_NE) {
1211 /* Simplify NE comparisons where one of the pairs
1212 can be simplified. */
1213 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1214 args[0], args[2], TCG_COND_NE);
1215 if (tmp == 0) {
1216 goto do_brcond_high;
1217 } else if (tmp == 1) {
1218 goto do_brcond_true;
1219 }
1220 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1221 args[1], args[3], TCG_COND_NE);
1222 if (tmp == 0) {
1223 goto do_brcond_low;
1224 } else if (tmp == 1) {
1225 goto do_brcond_true;
1226 }
1227 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001228 } else {
1229 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001230 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001231 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001232
1233 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001234 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1235 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001236 do_setcond_const:
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001237 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001238 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001239 && temp_is_const(args[3]) && temps[args[3]].val == 0
1240 && temp_is_const(args[4]) && temps[args[4]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001241 /* Simplify LT/GE comparisons vs zero to a single compare
1242 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001243 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001244 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001245 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001246 op->opc = INDEX_op_setcond_i32;
1247 args[1] = args[2];
1248 args[2] = args[4];
1249 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001250 } else if (args[5] == TCG_COND_EQ) {
1251 /* Simplify EQ comparisons where one of the pairs
1252 can be simplified. */
1253 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1254 args[1], args[3], TCG_COND_EQ);
1255 if (tmp == 0) {
1256 goto do_setcond_const;
1257 } else if (tmp == 1) {
1258 goto do_setcond_high;
1259 }
1260 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1261 args[2], args[4], TCG_COND_EQ);
1262 if (tmp == 0) {
1263 goto do_setcond_high;
1264 } else if (tmp != 1) {
1265 goto do_default;
1266 }
1267 do_setcond_low:
1268 reset_temp(args[0]);
1269 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001270 op->opc = INDEX_op_setcond_i32;
1271 args[2] = args[3];
1272 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001273 } else if (args[5] == TCG_COND_NE) {
1274 /* Simplify NE comparisons where one of the pairs
1275 can be simplified. */
1276 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1277 args[1], args[3], TCG_COND_NE);
1278 if (tmp == 0) {
1279 goto do_setcond_high;
1280 } else if (tmp == 1) {
1281 goto do_setcond_const;
1282 }
1283 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1284 args[2], args[4], TCG_COND_NE);
1285 if (tmp == 0) {
1286 goto do_setcond_low;
1287 } else if (tmp == 1) {
1288 goto do_setcond_const;
1289 }
1290 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001291 } else {
1292 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001293 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001294 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001295
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001296 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001297 if (!(args[nb_oargs + nb_iargs + 1]
1298 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001299 for (i = 0; i < nb_globals; i++) {
Aurelien Jarno1208d7d2015-07-27 12:41:44 +02001300 if (test_bit(i, temps_used.l)) {
1301 reset_temp(i);
1302 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001303 }
1304 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001305 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001306
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001307 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001308 do_default:
1309 /* Default case: we know nothing about operation (or were unable
1310 to compute the operation result) so no propagation is done.
1311 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001312 block, otherwise we only trash the output args. "mask" is
1313 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001314 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001315 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001316 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001317 do_reset_output:
1318 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001319 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001320 /* Save the corresponding known-zero bits mask for the
1321 first output argument (only one supported so far). */
1322 if (i == 0) {
1323 temps[args[i]].mask = mask;
1324 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001325 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001326 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001327 break;
1328 }
1329 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001330}