blob: 5c60e1c228c152c702a5926dd66fc3fd4367b7b5 [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 +040038struct tcg_temp_info {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020039 bool is_const;
Kirill Batuzov22613af2011-07-07 16:37:13 +040040 uint16_t prev_copy;
41 uint16_t next_copy;
42 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080043 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040044};
45
46static struct tcg_temp_info temps[TCG_MAX_TEMPS];
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020047static TCGTempSet temps_used;
Kirill Batuzov22613af2011-07-07 16:37:13 +040048
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020049static inline bool temp_is_const(TCGArg arg)
50{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020051 return temps[arg].is_const;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020052}
53
54static inline bool temp_is_copy(TCGArg arg)
55{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020056 return temps[arg].next_copy != arg;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020057}
58
Aurelien Jarnob41059d2015-07-27 12:41:44 +020059/* Reset TEMP's state, possibly removing the temp for the list of copies. */
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020060static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040061{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020062 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
63 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
64 temps[temp].next_copy = temp;
65 temps[temp].prev_copy = temp;
66 temps[temp].is_const = false;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080067 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040068}
69
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020070/* Reset all temporaries, given that there are NB_TEMPS of them. */
71static void reset_all_temps(int nb_temps)
72{
73 bitmap_zero(temps_used.l, nb_temps);
74}
75
76/* Initialize and activate a temporary. */
77static void init_temp_info(TCGArg temp)
78{
79 if (!test_bit(temp, temps_used.l)) {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020080 temps[temp].next_copy = temp;
81 temps[temp].prev_copy = temp;
82 temps[temp].is_const = false;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020083 temps[temp].mask = -1;
84 set_bit(temp, temps_used.l);
85 }
86}
87
Richard Hendersona4ce0992014-03-30 17:14:02 -070088static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
89 TCGOpcode opc, int nargs)
90{
91 int oi = s->gen_next_op_idx;
92 int pi = s->gen_next_parm_idx;
93 int prev = old_op->prev;
94 int next = old_op - s->gen_op_buf;
95 TCGOp *new_op;
96
97 tcg_debug_assert(oi < OPC_BUF_SIZE);
98 tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
99 s->gen_next_op_idx = oi + 1;
100 s->gen_next_parm_idx = pi + nargs;
101
102 new_op = &s->gen_op_buf[oi];
103 *new_op = (TCGOp){
104 .opc = opc,
105 .args = pi,
106 .prev = prev,
107 .next = next
108 };
109 if (prev >= 0) {
110 s->gen_op_buf[prev].next = oi;
111 } else {
112 s->gen_first_op_idx = oi;
113 }
114 old_op->prev = oi;
115
116 return new_op;
117}
118
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000119static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400120{
Richard Henderson8399ad52011-08-17 14:11:45 -0700121 const TCGOpDef *def = &tcg_op_defs[op];
122 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400123}
124
Richard Hendersona62f6f52014-05-22 10:59:12 -0700125static TCGOpcode op_to_mov(TCGOpcode op)
126{
127 switch (op_bits(op)) {
128 case 32:
129 return INDEX_op_mov_i32;
130 case 64:
131 return INDEX_op_mov_i64;
132 default:
133 fprintf(stderr, "op_to_mov: unexpected return value of "
134 "function op_bits.\n");
135 tcg_abort();
136 }
137}
138
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000139static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400140{
141 switch (op_bits(op)) {
142 case 32:
143 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400144 case 64:
145 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400146 default:
147 fprintf(stderr, "op_to_movi: unexpected return value of "
148 "function op_bits.\n");
149 tcg_abort();
150 }
151}
152
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200153static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
154{
155 TCGArg i;
156
157 /* If this is already a global, we can't do better. */
158 if (temp < s->nb_globals) {
159 return temp;
160 }
161
162 /* Search for a global first. */
163 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
164 if (i < s->nb_globals) {
165 return i;
166 }
167 }
168
169 /* If it is a temp, search for a temp local. */
170 if (!s->temps[temp].temp_local) {
171 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
172 if (s->temps[i].temp_local) {
173 return i;
174 }
175 }
176 }
177
178 /* Failure to find a better representation, return the same temp. */
179 return temp;
180}
181
182static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
183{
184 TCGArg i;
185
186 if (arg1 == arg2) {
187 return true;
188 }
189
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200190 if (!temp_is_copy(arg1) || !temp_is_copy(arg2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200191 return false;
192 }
193
194 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
195 if (i == arg2) {
196 return true;
197 }
198 }
199
200 return false;
201}
202
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200203static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
204 TCGArg dst, TCGArg val)
205{
206 TCGOpcode new_op = op_to_movi(op->opc);
207 tcg_target_ulong mask;
208
209 op->opc = new_op;
210
211 reset_temp(dst);
Aurelien Jarnob41059d2015-07-27 12:41:44 +0200212 temps[dst].is_const = true;
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200213 temps[dst].val = val;
214 mask = val;
Aurelien Jarno96152122015-07-10 18:03:30 +0200215 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_movi_i32) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200216 /* High bits of the destination are now garbage. */
217 mask |= ~0xffffffffull;
218 }
219 temps[dst].mask = mask;
220
221 args[0] = dst;
222 args[1] = val;
223}
224
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700225static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200226 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400227{
Aurelien Jarno53657182015-06-04 21:53:25 +0200228 if (temps_are_copies(dst, src)) {
229 tcg_op_remove(s, op);
230 return;
231 }
232
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200233 if (temp_is_const(src)) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200234 tcg_opt_gen_movi(s, op, args, dst, temps[src].val);
235 return;
236 }
237
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200238 TCGOpcode new_op = op_to_mov(op->opc);
Richard Henderson24666ba2014-05-22 11:14:10 -0700239 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700240
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700241 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700242
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800243 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700244 mask = temps[src].mask;
245 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
246 /* High bits of the destination are now garbage. */
247 mask |= ~0xffffffffull;
248 }
249 temps[dst].mask = mask;
250
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200251 assert(!temp_is_const(src));
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200252
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800253 if (s->temps[src].type == s->temps[dst].type) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800254 temps[dst].next_copy = temps[src].next_copy;
255 temps[dst].prev_copy = src;
256 temps[temps[dst].next_copy].prev_copy = dst;
257 temps[src].next_copy = dst;
Aurelien Jarnob41059d2015-07-27 12:41:44 +0200258 temps[dst].is_const = false;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800259 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200260
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700261 args[0] = dst;
262 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400263}
264
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000265static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400266{
Richard Henderson03271522013-08-14 14:35:56 -0700267 uint64_t l64, h64;
268
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400269 switch (op) {
270 CASE_OP_32_64(add):
271 return x + y;
272
273 CASE_OP_32_64(sub):
274 return x - y;
275
276 CASE_OP_32_64(mul):
277 return x * y;
278
Kirill Batuzov9a810902011-07-07 16:37:15 +0400279 CASE_OP_32_64(and):
280 return x & y;
281
282 CASE_OP_32_64(or):
283 return x | y;
284
285 CASE_OP_32_64(xor):
286 return x ^ y;
287
Kirill Batuzov55c09752011-07-07 16:37:16 +0400288 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700289 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400290
Kirill Batuzov55c09752011-07-07 16:37:16 +0400291 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700292 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400293
294 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700295 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400296
Richard Henderson4bb7a412013-09-09 17:03:24 -0700297 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400298 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700299 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400300
301 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700302 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400303
Kirill Batuzov55c09752011-07-07 16:37:16 +0400304 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700305 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400306
307 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700308 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400309
Kirill Batuzov55c09752011-07-07 16:37:16 +0400310 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700311 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400312
313 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700314 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400315
Kirill Batuzov55c09752011-07-07 16:37:16 +0400316 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700317 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400318
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700319 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400320 return ~x;
321
Richard Hendersoncb25c802011-08-17 14:11:47 -0700322 CASE_OP_32_64(neg):
323 return -x;
324
325 CASE_OP_32_64(andc):
326 return x & ~y;
327
328 CASE_OP_32_64(orc):
329 return x | ~y;
330
331 CASE_OP_32_64(eqv):
332 return ~(x ^ y);
333
334 CASE_OP_32_64(nand):
335 return ~(x & y);
336
337 CASE_OP_32_64(nor):
338 return ~(x | y);
339
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700340 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400341 return (int8_t)x;
342
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700343 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400344 return (int16_t)x;
345
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700346 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400347 return (uint8_t)x;
348
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700349 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400350 return (uint16_t)x;
351
Kirill Batuzova640f032011-07-07 16:37:17 +0400352 case INDEX_op_ext32s_i64:
353 return (int32_t)x;
354
355 case INDEX_op_ext32u_i64:
356 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400357
Richard Henderson03271522013-08-14 14:35:56 -0700358 case INDEX_op_muluh_i32:
359 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
360 case INDEX_op_mulsh_i32:
361 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
362
363 case INDEX_op_muluh_i64:
364 mulu64(&l64, &h64, x, y);
365 return h64;
366 case INDEX_op_mulsh_i64:
367 muls64(&l64, &h64, x, y);
368 return h64;
369
Richard Henderson01547f72013-08-14 15:22:46 -0700370 case INDEX_op_div_i32:
371 /* Avoid crashing on divide by zero, otherwise undefined. */
372 return (int32_t)x / ((int32_t)y ? : 1);
373 case INDEX_op_divu_i32:
374 return (uint32_t)x / ((uint32_t)y ? : 1);
375 case INDEX_op_div_i64:
376 return (int64_t)x / ((int64_t)y ? : 1);
377 case INDEX_op_divu_i64:
378 return (uint64_t)x / ((uint64_t)y ? : 1);
379
380 case INDEX_op_rem_i32:
381 return (int32_t)x % ((int32_t)y ? : 1);
382 case INDEX_op_remu_i32:
383 return (uint32_t)x % ((uint32_t)y ? : 1);
384 case INDEX_op_rem_i64:
385 return (int64_t)x % ((int64_t)y ? : 1);
386 case INDEX_op_remu_i64:
387 return (uint64_t)x % ((uint64_t)y ? : 1);
388
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400389 default:
390 fprintf(stderr,
391 "Unrecognized operation %d in do_constant_folding.\n", op);
392 tcg_abort();
393 }
394}
395
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000396static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400397{
398 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400399 if (op_bits(op) == 32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200400 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400401 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400402 return res;
403}
404
Richard Henderson9519da72012-10-02 11:32:26 -0700405static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
406{
407 switch (c) {
408 case TCG_COND_EQ:
409 return x == y;
410 case TCG_COND_NE:
411 return x != y;
412 case TCG_COND_LT:
413 return (int32_t)x < (int32_t)y;
414 case TCG_COND_GE:
415 return (int32_t)x >= (int32_t)y;
416 case TCG_COND_LE:
417 return (int32_t)x <= (int32_t)y;
418 case TCG_COND_GT:
419 return (int32_t)x > (int32_t)y;
420 case TCG_COND_LTU:
421 return x < y;
422 case TCG_COND_GEU:
423 return x >= y;
424 case TCG_COND_LEU:
425 return x <= y;
426 case TCG_COND_GTU:
427 return x > y;
428 default:
429 tcg_abort();
430 }
431}
432
433static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
434{
435 switch (c) {
436 case TCG_COND_EQ:
437 return x == y;
438 case TCG_COND_NE:
439 return x != y;
440 case TCG_COND_LT:
441 return (int64_t)x < (int64_t)y;
442 case TCG_COND_GE:
443 return (int64_t)x >= (int64_t)y;
444 case TCG_COND_LE:
445 return (int64_t)x <= (int64_t)y;
446 case TCG_COND_GT:
447 return (int64_t)x > (int64_t)y;
448 case TCG_COND_LTU:
449 return x < y;
450 case TCG_COND_GEU:
451 return x >= y;
452 case TCG_COND_LEU:
453 return x <= y;
454 case TCG_COND_GTU:
455 return x > y;
456 default:
457 tcg_abort();
458 }
459}
460
461static bool do_constant_folding_cond_eq(TCGCond c)
462{
463 switch (c) {
464 case TCG_COND_GT:
465 case TCG_COND_LTU:
466 case TCG_COND_LT:
467 case TCG_COND_GTU:
468 case TCG_COND_NE:
469 return 0;
470 case TCG_COND_GE:
471 case TCG_COND_GEU:
472 case TCG_COND_LE:
473 case TCG_COND_LEU:
474 case TCG_COND_EQ:
475 return 1;
476 default:
477 tcg_abort();
478 }
479}
480
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200481/* Return 2 if the condition can't be simplified, and the result
482 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200483static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
484 TCGArg y, TCGCond c)
485{
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200486 if (temp_is_const(x) && temp_is_const(y)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200487 switch (op_bits(op)) {
488 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700489 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200490 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700491 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
492 default:
493 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200494 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200495 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700496 return do_constant_folding_cond_eq(c);
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200497 } else if (temp_is_const(y) && temps[y].val == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200498 switch (c) {
499 case TCG_COND_LTU:
500 return 0;
501 case TCG_COND_GEU:
502 return 1;
503 default:
504 return 2;
505 }
506 } else {
507 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200508 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200509}
510
Richard Henderson6c4382f2012-10-02 11:32:27 -0700511/* Return 2 if the condition can't be simplified, and the result
512 of the condition (0 or 1) if it can */
513static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
514{
515 TCGArg al = p1[0], ah = p1[1];
516 TCGArg bl = p2[0], bh = p2[1];
517
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200518 if (temp_is_const(bl) && temp_is_const(bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700519 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
520
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200521 if (temp_is_const(al) && temp_is_const(ah)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700522 uint64_t a;
523 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
524 return do_constant_folding_cond_64(a, b, c);
525 }
526 if (b == 0) {
527 switch (c) {
528 case TCG_COND_LTU:
529 return 0;
530 case TCG_COND_GEU:
531 return 1;
532 default:
533 break;
534 }
535 }
536 }
537 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
538 return do_constant_folding_cond_eq(c);
539 }
540 return 2;
541}
542
Richard Henderson24c9ae42012-10-02 11:32:21 -0700543static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
544{
545 TCGArg a1 = *p1, a2 = *p2;
546 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200547 sum += temp_is_const(a1);
548 sum -= temp_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700549
550 /* Prefer the constant in second argument, and then the form
551 op a, a, b, which is better handled on non-RISC hosts. */
552 if (sum > 0 || (sum == 0 && dest == a2)) {
553 *p1 = a2;
554 *p2 = a1;
555 return true;
556 }
557 return false;
558}
559
Richard Henderson0bfcb862012-10-02 11:32:23 -0700560static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
561{
562 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200563 sum += temp_is_const(p1[0]);
564 sum += temp_is_const(p1[1]);
565 sum -= temp_is_const(p2[0]);
566 sum -= temp_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700567 if (sum > 0) {
568 TCGArg t;
569 t = p1[0], p1[0] = p2[0], p2[0] = t;
570 t = p1[1], p1[1] = p2[1], p2[1] = t;
571 return true;
572 }
573 return false;
574}
575
Kirill Batuzov22613af2011-07-07 16:37:13 +0400576/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +0200577void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400578{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700579 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700580
Kirill Batuzov22613af2011-07-07 16:37:13 +0400581 /* Array VALS has an element for each temp.
582 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200583 If this temp is a copy of other ones then the other copies are
584 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400585
586 nb_temps = s->nb_temps;
587 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800588 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400589
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700590 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700591 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700592 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700593 TCGArg tmp;
594
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700595 TCGOp * const op = &s->gen_op_buf[oi];
596 TCGArg * const args = &s->gen_opparam_buf[op->args];
597 TCGOpcode opc = op->opc;
598 const TCGOpDef *def = &tcg_op_defs[opc];
599
600 oi_next = op->next;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200601
602 /* Count the arguments, and initialize the temps that are
603 going to be used */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700604 if (opc == INDEX_op_call) {
605 nb_oargs = op->callo;
606 nb_iargs = op->calli;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200607 for (i = 0; i < nb_oargs + nb_iargs; i++) {
608 tmp = args[i];
609 if (tmp != TCG_CALL_DUMMY_ARG) {
610 init_temp_info(tmp);
611 }
612 }
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200613 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700614 nb_oargs = def->nb_oargs;
615 nb_iargs = def->nb_iargs;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200616 for (i = 0; i < nb_oargs + nb_iargs; i++) {
617 init_temp_info(args[i]);
618 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700619 }
620
621 /* Do copy propagation */
622 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200623 if (temp_is_copy(args[i])) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700624 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400625 }
626 }
627
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400628 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700629 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400630 CASE_OP_32_64(add):
631 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400632 CASE_OP_32_64(and):
633 CASE_OP_32_64(or):
634 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700635 CASE_OP_32_64(eqv):
636 CASE_OP_32_64(nand):
637 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700638 CASE_OP_32_64(muluh):
639 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700640 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400641 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200642 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700643 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200644 args[2] = tcg_swap_cond(args[2]);
645 }
646 break;
647 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700648 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200649 args[3] = tcg_swap_cond(args[3]);
650 }
651 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700652 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700653 if (swap_commutative(-1, &args[1], &args[2])) {
654 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700655 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700656 /* For movcond, we canonicalize the "false" input reg to match
657 the destination reg so that the tcg backend can implement
658 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700659 if (swap_commutative(args[0], &args[4], &args[3])) {
660 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700661 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700662 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800663 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700664 swap_commutative(args[0], &args[2], &args[4]);
665 swap_commutative(args[1], &args[3], &args[5]);
666 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800667 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800668 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700669 swap_commutative(args[0], &args[2], &args[3]);
670 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700671 case INDEX_op_brcond2_i32:
672 if (swap_commutative2(&args[0], &args[2])) {
673 args[4] = tcg_swap_cond(args[4]);
674 }
675 break;
676 case INDEX_op_setcond2_i32:
677 if (swap_commutative2(&args[1], &args[3])) {
678 args[5] = tcg_swap_cond(args[5]);
679 }
680 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400681 default:
682 break;
683 }
684
Richard Henderson2d497542013-03-21 09:13:33 -0700685 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
686 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700687 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200688 CASE_OP_32_64(shl):
689 CASE_OP_32_64(shr):
690 CASE_OP_32_64(sar):
691 CASE_OP_32_64(rotl):
692 CASE_OP_32_64(rotr):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200693 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200694 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200695 continue;
696 }
697 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700698 CASE_OP_32_64(sub):
699 {
700 TCGOpcode neg_op;
701 bool have_neg;
702
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200703 if (temp_is_const(args[2])) {
Richard Henderson2d497542013-03-21 09:13:33 -0700704 /* Proceed with possible constant folding. */
705 break;
706 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700707 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700708 neg_op = INDEX_op_neg_i32;
709 have_neg = TCG_TARGET_HAS_neg_i32;
710 } else {
711 neg_op = INDEX_op_neg_i64;
712 have_neg = TCG_TARGET_HAS_neg_i64;
713 }
714 if (!have_neg) {
715 break;
716 }
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200717 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700718 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700719 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700720 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700721 continue;
722 }
723 }
724 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800725 CASE_OP_32_64(xor):
726 CASE_OP_32_64(nand):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200727 if (!temp_is_const(args[1])
728 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800729 i = 1;
730 goto try_not;
731 }
732 break;
733 CASE_OP_32_64(nor):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200734 if (!temp_is_const(args[1])
735 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800736 i = 1;
737 goto try_not;
738 }
739 break;
740 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200741 if (!temp_is_const(args[2])
742 && temp_is_const(args[1]) && temps[args[1]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800743 i = 2;
744 goto try_not;
745 }
746 break;
747 CASE_OP_32_64(orc):
748 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200749 if (!temp_is_const(args[2])
750 && temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800751 i = 2;
752 goto try_not;
753 }
754 break;
755 try_not:
756 {
757 TCGOpcode not_op;
758 bool have_not;
759
760 if (def->flags & TCG_OPF_64BIT) {
761 not_op = INDEX_op_not_i64;
762 have_not = TCG_TARGET_HAS_not_i64;
763 } else {
764 not_op = INDEX_op_not_i32;
765 have_not = TCG_TARGET_HAS_not_i32;
766 }
767 if (!have_not) {
768 break;
769 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700770 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800771 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700772 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800773 continue;
774 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200775 default:
776 break;
777 }
778
Richard Henderson464a1442014-01-31 07:42:11 -0600779 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700780 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400781 CASE_OP_32_64(add):
782 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400783 CASE_OP_32_64(shl):
784 CASE_OP_32_64(shr):
785 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700786 CASE_OP_32_64(rotl):
787 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200788 CASE_OP_32_64(or):
789 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600790 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200791 if (!temp_is_const(args[1])
792 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200793 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
794 continue;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400795 }
796 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600797 CASE_OP_32_64(and):
798 CASE_OP_32_64(orc):
799 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200800 if (!temp_is_const(args[1])
801 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200802 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
803 continue;
Richard Henderson464a1442014-01-31 07:42:11 -0600804 }
805 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200806 default:
807 break;
808 }
809
Aurelien Jarno30312442013-09-03 08:27:38 +0200810 /* Simplify using known-zero bits. Currently only ops with a single
811 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800812 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800813 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700814 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800815 CASE_OP_32_64(ext8s):
816 if ((temps[args[1]].mask & 0x80) != 0) {
817 break;
818 }
819 CASE_OP_32_64(ext8u):
820 mask = 0xff;
821 goto and_const;
822 CASE_OP_32_64(ext16s):
823 if ((temps[args[1]].mask & 0x8000) != 0) {
824 break;
825 }
826 CASE_OP_32_64(ext16u):
827 mask = 0xffff;
828 goto and_const;
829 case INDEX_op_ext32s_i64:
830 if ((temps[args[1]].mask & 0x80000000) != 0) {
831 break;
832 }
833 case INDEX_op_ext32u_i64:
834 mask = 0xffffffffU;
835 goto and_const;
836
837 CASE_OP_32_64(and):
838 mask = temps[args[2]].mask;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200839 if (temp_is_const(args[2])) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800840 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800841 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800842 }
843 mask = temps[args[1]].mask & mask;
844 break;
845
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800846 CASE_OP_32_64(andc):
847 /* Known-zeros does not imply known-ones. Therefore unless
848 args[2] is constant, we can't infer anything from it. */
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200849 if (temp_is_const(args[2])) {
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800850 mask = ~temps[args[2]].mask;
851 goto and_const;
852 }
853 /* But we certainly know nothing outside args[1] may be set. */
854 mask = temps[args[1]].mask;
855 break;
856
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200857 case INDEX_op_sar_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200858 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700859 tmp = temps[args[2]].val & 31;
860 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200861 }
862 break;
863 case INDEX_op_sar_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200864 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700865 tmp = temps[args[2]].val & 63;
866 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800867 }
868 break;
869
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200870 case INDEX_op_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200871 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700872 tmp = temps[args[2]].val & 31;
873 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200874 }
875 break;
876 case INDEX_op_shr_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200877 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700878 tmp = temps[args[2]].val & 63;
879 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800880 }
881 break;
882
Richard Henderson4bb7a412013-09-09 17:03:24 -0700883 case INDEX_op_trunc_shr_i32:
884 mask = (uint64_t)temps[args[1]].mask >> args[2];
885 break;
886
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800887 CASE_OP_32_64(shl):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200888 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700889 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
890 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800891 }
892 break;
893
894 CASE_OP_32_64(neg):
895 /* Set to 1 all bits to the left of the rightmost. */
896 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
897 break;
898
899 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700900 mask = deposit64(temps[args[1]].mask, args[3], args[4],
901 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800902 break;
903
904 CASE_OP_32_64(or):
905 CASE_OP_32_64(xor):
906 mask = temps[args[1]].mask | temps[args[2]].mask;
907 break;
908
909 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700910 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800911 mask = 1;
912 break;
913
914 CASE_OP_32_64(movcond):
915 mask = temps[args[3]].mask | temps[args[4]].mask;
916 break;
917
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200918 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200919 mask = 0xff;
920 break;
921 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200922 mask = 0xffff;
923 break;
924 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200925 mask = 0xffffffffu;
926 break;
927
928 CASE_OP_32_64(qemu_ld):
929 {
Richard Henderson59227d52015-05-12 11:51:44 -0700930 TCGMemOpIdx oi = args[nb_oargs + nb_iargs];
931 TCGMemOp mop = get_memop(oi);
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200932 if (!(mop & MO_SIGN)) {
933 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
934 }
935 }
936 break;
937
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800938 default:
939 break;
940 }
941
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700942 /* 32-bit ops generate 32-bit results. For the result is zero test
943 below, we can ignore high bits, but for further optimizations we
944 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700945 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700946 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700947 mask |= ~(tcg_target_ulong)0xffffffffu;
948 partmask &= 0xffffffffu;
949 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200950 }
951
Richard Henderson24666ba2014-05-22 11:14:10 -0700952 if (partmask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700953 assert(nb_oargs == 1);
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200954 tcg_opt_gen_movi(s, op, args, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800955 continue;
956 }
957 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700958 assert(nb_oargs == 1);
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200959 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800960 continue;
961 }
962
Aurelien Jarno56e49432012-09-06 16:47:13 +0200963 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700964 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200965 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400966 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700967 CASE_OP_32_64(muluh):
968 CASE_OP_32_64(mulsh):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200969 if ((temp_is_const(args[2]) && temps[args[2]].val == 0)) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200970 tcg_opt_gen_movi(s, op, args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400971 continue;
972 }
973 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200974 default:
975 break;
976 }
977
978 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700979 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400980 CASE_OP_32_64(or):
981 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200982 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200983 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400984 continue;
985 }
986 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000987 default:
988 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400989 }
990
Aurelien Jarno3c941932012-09-18 19:12:36 +0200991 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700992 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800993 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +0200994 CASE_OP_32_64(sub):
995 CASE_OP_32_64(xor):
996 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200997 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200998 continue;
999 }
1000 break;
1001 default:
1002 break;
1003 }
1004
Kirill Batuzov22613af2011-07-07 16:37:13 +04001005 /* Propagate constants through copy operations and do constant
1006 folding. Constants will be substituted to arguments by register
1007 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001008 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001009 CASE_OP_32_64(mov):
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001010 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
1011 break;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001012 CASE_OP_32_64(movi):
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001013 tcg_opt_gen_movi(s, op, args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001014 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001015
Kirill Batuzova640f032011-07-07 16:37:17 +04001016 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001017 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001018 CASE_OP_32_64(ext8s):
1019 CASE_OP_32_64(ext8u):
1020 CASE_OP_32_64(ext16s):
1021 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001022 case INDEX_op_ext32s_i64:
1023 case INDEX_op_ext32u_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001024 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001025 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001026 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001027 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001028 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001029 goto do_default;
1030
Richard Henderson4bb7a412013-09-09 17:03:24 -07001031 case INDEX_op_trunc_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001032 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001033 tmp = do_constant_folding(opc, temps[args[1]].val, args[2]);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001034 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001035 break;
1036 }
1037 goto do_default;
1038
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001039 CASE_OP_32_64(add):
1040 CASE_OP_32_64(sub):
1041 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001042 CASE_OP_32_64(or):
1043 CASE_OP_32_64(and):
1044 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001045 CASE_OP_32_64(shl):
1046 CASE_OP_32_64(shr):
1047 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001048 CASE_OP_32_64(rotl):
1049 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001050 CASE_OP_32_64(andc):
1051 CASE_OP_32_64(orc):
1052 CASE_OP_32_64(eqv):
1053 CASE_OP_32_64(nand):
1054 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001055 CASE_OP_32_64(muluh):
1056 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001057 CASE_OP_32_64(div):
1058 CASE_OP_32_64(divu):
1059 CASE_OP_32_64(rem):
1060 CASE_OP_32_64(remu):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001061 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001062 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001063 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001064 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001065 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001066 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001067 goto do_default;
1068
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001069 CASE_OP_32_64(deposit):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001070 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersond998e552014-03-18 14:23:52 -07001071 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1072 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001073 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001074 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001075 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001076 goto do_default;
1077
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001078 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001079 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001080 if (tmp != 2) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001081 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001082 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001083 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001084 goto do_default;
1085
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001086 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001087 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001088 if (tmp != 2) {
1089 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001090 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001091 op->opc = INDEX_op_br;
1092 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001093 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001094 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001095 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001096 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001097 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001098 goto do_default;
1099
Richard Hendersonfa01a202012-09-21 10:13:37 -07001100 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001101 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001102 if (tmp != 2) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001103 tcg_opt_gen_mov(s, op, args, args[0], args[4-tmp]);
Richard Henderson6e14e912012-10-02 11:32:24 -07001104 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001105 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001106 goto do_default;
1107
Richard Henderson212c3282012-10-02 11:32:28 -07001108 case INDEX_op_add2_i32:
1109 case INDEX_op_sub2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001110 if (temp_is_const(args[2]) && temp_is_const(args[3])
1111 && temp_is_const(args[4]) && temp_is_const(args[5])) {
Richard Henderson212c3282012-10-02 11:32:28 -07001112 uint32_t al = temps[args[2]].val;
1113 uint32_t ah = temps[args[3]].val;
1114 uint32_t bl = temps[args[4]].val;
1115 uint32_t bh = temps[args[5]].val;
1116 uint64_t a = ((uint64_t)ah << 32) | al;
1117 uint64_t b = ((uint64_t)bh << 32) | bl;
1118 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001119 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1120 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001121
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001122 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001123 a += b;
1124 } else {
1125 a -= b;
1126 }
1127
Richard Henderson212c3282012-10-02 11:32:28 -07001128 rl = args[0];
1129 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001130 tcg_opt_gen_movi(s, op, args, rl, (int32_t)a);
1131 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(a >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001132
1133 /* We've done all we need to do with the movi. Skip it. */
1134 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001135 break;
1136 }
1137 goto do_default;
1138
Richard Henderson14149682012-10-02 11:32:30 -07001139 case INDEX_op_mulu2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001140 if (temp_is_const(args[2]) && temp_is_const(args[3])) {
Richard Henderson14149682012-10-02 11:32:30 -07001141 uint32_t a = temps[args[2]].val;
1142 uint32_t b = temps[args[3]].val;
1143 uint64_t r = (uint64_t)a * b;
1144 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001145 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1146 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001147
1148 rl = args[0];
1149 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001150 tcg_opt_gen_movi(s, op, args, rl, (int32_t)r);
1151 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(r >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001152
1153 /* We've done all we need to do with the movi. Skip it. */
1154 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001155 break;
1156 }
1157 goto do_default;
1158
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001159 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001160 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1161 if (tmp != 2) {
1162 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001163 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001164 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001165 op->opc = INDEX_op_br;
1166 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001167 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001168 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001169 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001170 }
1171 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001172 && temp_is_const(args[2]) && temps[args[2]].val == 0
1173 && temp_is_const(args[3]) && temps[args[3]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001174 /* Simplify LT/GE comparisons vs zero to a single compare
1175 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001176 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001177 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001178 op->opc = INDEX_op_brcond_i32;
1179 args[0] = args[1];
1180 args[1] = args[3];
1181 args[2] = args[4];
1182 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001183 } else if (args[4] == TCG_COND_EQ) {
1184 /* Simplify EQ comparisons where one of the pairs
1185 can be simplified. */
1186 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1187 args[0], args[2], TCG_COND_EQ);
1188 if (tmp == 0) {
1189 goto do_brcond_false;
1190 } else if (tmp == 1) {
1191 goto do_brcond_high;
1192 }
1193 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1194 args[1], args[3], TCG_COND_EQ);
1195 if (tmp == 0) {
1196 goto do_brcond_false;
1197 } else if (tmp != 1) {
1198 goto do_default;
1199 }
1200 do_brcond_low:
1201 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001202 op->opc = INDEX_op_brcond_i32;
1203 args[1] = args[2];
1204 args[2] = args[4];
1205 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001206 } else if (args[4] == TCG_COND_NE) {
1207 /* Simplify NE comparisons where one of the pairs
1208 can be simplified. */
1209 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1210 args[0], args[2], TCG_COND_NE);
1211 if (tmp == 0) {
1212 goto do_brcond_high;
1213 } else if (tmp == 1) {
1214 goto do_brcond_true;
1215 }
1216 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1217 args[1], args[3], TCG_COND_NE);
1218 if (tmp == 0) {
1219 goto do_brcond_low;
1220 } else if (tmp == 1) {
1221 goto do_brcond_true;
1222 }
1223 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001224 } else {
1225 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001226 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001227 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001228
1229 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001230 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1231 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001232 do_setcond_const:
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001233 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001234 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001235 && temp_is_const(args[3]) && temps[args[3]].val == 0
1236 && temp_is_const(args[4]) && temps[args[4]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001237 /* Simplify LT/GE comparisons vs zero to a single compare
1238 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001239 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001240 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001241 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001242 op->opc = INDEX_op_setcond_i32;
1243 args[1] = args[2];
1244 args[2] = args[4];
1245 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001246 } else if (args[5] == TCG_COND_EQ) {
1247 /* Simplify EQ comparisons where one of the pairs
1248 can be simplified. */
1249 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1250 args[1], args[3], TCG_COND_EQ);
1251 if (tmp == 0) {
1252 goto do_setcond_const;
1253 } else if (tmp == 1) {
1254 goto do_setcond_high;
1255 }
1256 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1257 args[2], args[4], TCG_COND_EQ);
1258 if (tmp == 0) {
1259 goto do_setcond_high;
1260 } else if (tmp != 1) {
1261 goto do_default;
1262 }
1263 do_setcond_low:
1264 reset_temp(args[0]);
1265 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001266 op->opc = INDEX_op_setcond_i32;
1267 args[2] = args[3];
1268 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001269 } else if (args[5] == TCG_COND_NE) {
1270 /* Simplify NE comparisons where one of the pairs
1271 can be simplified. */
1272 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1273 args[1], args[3], TCG_COND_NE);
1274 if (tmp == 0) {
1275 goto do_setcond_high;
1276 } else if (tmp == 1) {
1277 goto do_setcond_const;
1278 }
1279 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1280 args[2], args[4], TCG_COND_NE);
1281 if (tmp == 0) {
1282 goto do_setcond_low;
1283 } else if (tmp == 1) {
1284 goto do_setcond_const;
1285 }
1286 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001287 } else {
1288 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001289 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001290 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001291
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001292 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001293 if (!(args[nb_oargs + nb_iargs + 1]
1294 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001295 for (i = 0; i < nb_globals; i++) {
Aurelien Jarno1208d7d2015-07-27 12:41:44 +02001296 if (test_bit(i, temps_used.l)) {
1297 reset_temp(i);
1298 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001299 }
1300 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001301 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001302
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001303 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001304 do_default:
1305 /* Default case: we know nothing about operation (or were unable
1306 to compute the operation result) so no propagation is done.
1307 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001308 block, otherwise we only trash the output args. "mask" is
1309 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001310 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001311 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001312 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001313 do_reset_output:
1314 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001315 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001316 /* Save the corresponding known-zero bits mask for the
1317 first output argument (only one supported so far). */
1318 if (i == 0) {
1319 temps[args[i]].mask = mask;
1320 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001321 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001322 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001323 break;
1324 }
1325 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001326}