blob: 585f1ed7bb04631bbb904b69fcd0e98fcfd6bdb2 [file] [log] [blame]
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001/*
2 * Optimizations for Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "config.h"
27
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "qemu-common.h"
32#include "tcg-op.h"
33
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040034#define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040037
Kirill Batuzov22613af2011-07-07 16:37:13 +040038typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
Kirill Batuzov22613af2011-07-07 16:37:13 +040042} tcg_temp_state;
43
44struct tcg_temp_info {
45 tcg_temp_state state;
46 uint16_t prev_copy;
47 uint16_t next_copy;
48 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080049 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040050};
51
52static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020054/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
55 the copy flag from the left temp. */
56static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040057{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020058 if (temps[temp].state == TCG_TEMP_COPY) {
59 if (temps[temp].prev_copy == temps[temp].next_copy) {
60 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
61 } else {
62 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
63 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
Kirill Batuzov22613af2011-07-07 16:37:13 +040064 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040065 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020066 temps[temp].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080067 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040068}
69
Richard Hendersona4ce0992014-03-30 17:14:02 -070070static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
71 TCGOpcode opc, int nargs)
72{
73 int oi = s->gen_next_op_idx;
74 int pi = s->gen_next_parm_idx;
75 int prev = old_op->prev;
76 int next = old_op - s->gen_op_buf;
77 TCGOp *new_op;
78
79 tcg_debug_assert(oi < OPC_BUF_SIZE);
80 tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
81 s->gen_next_op_idx = oi + 1;
82 s->gen_next_parm_idx = pi + nargs;
83
84 new_op = &s->gen_op_buf[oi];
85 *new_op = (TCGOp){
86 .opc = opc,
87 .args = pi,
88 .prev = prev,
89 .next = next
90 };
91 if (prev >= 0) {
92 s->gen_op_buf[prev].next = oi;
93 } else {
94 s->gen_first_op_idx = oi;
95 }
96 old_op->prev = oi;
97
98 return new_op;
99}
100
Paolo Bonzinid193a142013-01-11 15:42:51 -0800101/* Reset all temporaries, given that there are NB_TEMPS of them. */
102static void reset_all_temps(int nb_temps)
103{
104 int i;
105 for (i = 0; i < nb_temps; i++) {
106 temps[i].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800107 temps[i].mask = -1;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800108 }
109}
110
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000111static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400112{
Richard Henderson8399ad52011-08-17 14:11:45 -0700113 const TCGOpDef *def = &tcg_op_defs[op];
114 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400115}
116
Richard Hendersona62f6f52014-05-22 10:59:12 -0700117static TCGOpcode op_to_mov(TCGOpcode op)
118{
119 switch (op_bits(op)) {
120 case 32:
121 return INDEX_op_mov_i32;
122 case 64:
123 return INDEX_op_mov_i64;
124 default:
125 fprintf(stderr, "op_to_mov: unexpected return value of "
126 "function op_bits.\n");
127 tcg_abort();
128 }
129}
130
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000131static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400132{
133 switch (op_bits(op)) {
134 case 32:
135 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400136 case 64:
137 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400138 default:
139 fprintf(stderr, "op_to_movi: unexpected return value of "
140 "function op_bits.\n");
141 tcg_abort();
142 }
143}
144
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200145static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
146{
147 TCGArg i;
148
149 /* If this is already a global, we can't do better. */
150 if (temp < s->nb_globals) {
151 return temp;
152 }
153
154 /* Search for a global first. */
155 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
156 if (i < s->nb_globals) {
157 return i;
158 }
159 }
160
161 /* If it is a temp, search for a temp local. */
162 if (!s->temps[temp].temp_local) {
163 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
164 if (s->temps[i].temp_local) {
165 return i;
166 }
167 }
168 }
169
170 /* Failure to find a better representation, return the same temp. */
171 return temp;
172}
173
174static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
175{
176 TCGArg i;
177
178 if (arg1 == arg2) {
179 return true;
180 }
181
182 if (temps[arg1].state != TCG_TEMP_COPY
183 || temps[arg2].state != TCG_TEMP_COPY) {
184 return false;
185 }
186
187 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
188 if (i == arg2) {
189 return true;
190 }
191 }
192
193 return false;
194}
195
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700196static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700197 TCGOpcode old_op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400198{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700199 TCGOpcode new_op = op_to_mov(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700200 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700201
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700202 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700203
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800204 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700205 mask = temps[src].mask;
206 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
207 /* High bits of the destination are now garbage. */
208 mask |= ~0xffffffffull;
209 }
210 temps[dst].mask = mask;
211
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800212 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200213
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800214 if (s->temps[src].type == s->temps[dst].type) {
215 if (temps[src].state != TCG_TEMP_COPY) {
216 temps[src].state = TCG_TEMP_COPY;
217 temps[src].next_copy = src;
218 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400219 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800220 temps[dst].state = TCG_TEMP_COPY;
221 temps[dst].next_copy = temps[src].next_copy;
222 temps[dst].prev_copy = src;
223 temps[temps[dst].next_copy].prev_copy = dst;
224 temps[src].next_copy = dst;
225 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200226
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700227 args[0] = dst;
228 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400229}
230
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700231static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700232 TCGOpcode old_op, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400233{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700234 TCGOpcode new_op = op_to_movi(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700235 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700236
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700237 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700238
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800239 reset_temp(dst);
240 temps[dst].state = TCG_TEMP_CONST;
241 temps[dst].val = val;
Richard Henderson24666ba2014-05-22 11:14:10 -0700242 mask = val;
243 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
244 /* High bits of the destination are now garbage. */
245 mask |= ~0xffffffffull;
246 }
247 temps[dst].mask = mask;
248
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700249 args[0] = dst;
250 args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400251}
252
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000253static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400254{
Richard Henderson03271522013-08-14 14:35:56 -0700255 uint64_t l64, h64;
256
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400257 switch (op) {
258 CASE_OP_32_64(add):
259 return x + y;
260
261 CASE_OP_32_64(sub):
262 return x - y;
263
264 CASE_OP_32_64(mul):
265 return x * y;
266
Kirill Batuzov9a810902011-07-07 16:37:15 +0400267 CASE_OP_32_64(and):
268 return x & y;
269
270 CASE_OP_32_64(or):
271 return x | y;
272
273 CASE_OP_32_64(xor):
274 return x ^ y;
275
Kirill Batuzov55c09752011-07-07 16:37:16 +0400276 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700277 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400278
Kirill Batuzov55c09752011-07-07 16:37:16 +0400279 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700280 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400281
282 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700283 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400284
Richard Henderson4bb7a412013-09-09 17:03:24 -0700285 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400286 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700287 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400288
289 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700290 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400291
Kirill Batuzov55c09752011-07-07 16:37:16 +0400292 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700293 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400294
295 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700296 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400297
Kirill Batuzov55c09752011-07-07 16:37:16 +0400298 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700299 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400300
301 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700302 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400303
Kirill Batuzov55c09752011-07-07 16:37:16 +0400304 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700305 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400306
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700307 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400308 return ~x;
309
Richard Hendersoncb25c802011-08-17 14:11:47 -0700310 CASE_OP_32_64(neg):
311 return -x;
312
313 CASE_OP_32_64(andc):
314 return x & ~y;
315
316 CASE_OP_32_64(orc):
317 return x | ~y;
318
319 CASE_OP_32_64(eqv):
320 return ~(x ^ y);
321
322 CASE_OP_32_64(nand):
323 return ~(x & y);
324
325 CASE_OP_32_64(nor):
326 return ~(x | y);
327
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700328 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400329 return (int8_t)x;
330
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700331 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400332 return (int16_t)x;
333
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700334 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400335 return (uint8_t)x;
336
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700337 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400338 return (uint16_t)x;
339
Kirill Batuzova640f032011-07-07 16:37:17 +0400340 case INDEX_op_ext32s_i64:
341 return (int32_t)x;
342
343 case INDEX_op_ext32u_i64:
344 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400345
Richard Henderson03271522013-08-14 14:35:56 -0700346 case INDEX_op_muluh_i32:
347 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
348 case INDEX_op_mulsh_i32:
349 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
350
351 case INDEX_op_muluh_i64:
352 mulu64(&l64, &h64, x, y);
353 return h64;
354 case INDEX_op_mulsh_i64:
355 muls64(&l64, &h64, x, y);
356 return h64;
357
Richard Henderson01547f72013-08-14 15:22:46 -0700358 case INDEX_op_div_i32:
359 /* Avoid crashing on divide by zero, otherwise undefined. */
360 return (int32_t)x / ((int32_t)y ? : 1);
361 case INDEX_op_divu_i32:
362 return (uint32_t)x / ((uint32_t)y ? : 1);
363 case INDEX_op_div_i64:
364 return (int64_t)x / ((int64_t)y ? : 1);
365 case INDEX_op_divu_i64:
366 return (uint64_t)x / ((uint64_t)y ? : 1);
367
368 case INDEX_op_rem_i32:
369 return (int32_t)x % ((int32_t)y ? : 1);
370 case INDEX_op_remu_i32:
371 return (uint32_t)x % ((uint32_t)y ? : 1);
372 case INDEX_op_rem_i64:
373 return (int64_t)x % ((int64_t)y ? : 1);
374 case INDEX_op_remu_i64:
375 return (uint64_t)x % ((uint64_t)y ? : 1);
376
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400377 default:
378 fprintf(stderr,
379 "Unrecognized operation %d in do_constant_folding.\n", op);
380 tcg_abort();
381 }
382}
383
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000384static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400385{
386 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400387 if (op_bits(op) == 32) {
388 res &= 0xffffffff;
389 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400390 return res;
391}
392
Richard Henderson9519da72012-10-02 11:32:26 -0700393static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
394{
395 switch (c) {
396 case TCG_COND_EQ:
397 return x == y;
398 case TCG_COND_NE:
399 return x != y;
400 case TCG_COND_LT:
401 return (int32_t)x < (int32_t)y;
402 case TCG_COND_GE:
403 return (int32_t)x >= (int32_t)y;
404 case TCG_COND_LE:
405 return (int32_t)x <= (int32_t)y;
406 case TCG_COND_GT:
407 return (int32_t)x > (int32_t)y;
408 case TCG_COND_LTU:
409 return x < y;
410 case TCG_COND_GEU:
411 return x >= y;
412 case TCG_COND_LEU:
413 return x <= y;
414 case TCG_COND_GTU:
415 return x > y;
416 default:
417 tcg_abort();
418 }
419}
420
421static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
422{
423 switch (c) {
424 case TCG_COND_EQ:
425 return x == y;
426 case TCG_COND_NE:
427 return x != y;
428 case TCG_COND_LT:
429 return (int64_t)x < (int64_t)y;
430 case TCG_COND_GE:
431 return (int64_t)x >= (int64_t)y;
432 case TCG_COND_LE:
433 return (int64_t)x <= (int64_t)y;
434 case TCG_COND_GT:
435 return (int64_t)x > (int64_t)y;
436 case TCG_COND_LTU:
437 return x < y;
438 case TCG_COND_GEU:
439 return x >= y;
440 case TCG_COND_LEU:
441 return x <= y;
442 case TCG_COND_GTU:
443 return x > y;
444 default:
445 tcg_abort();
446 }
447}
448
449static bool do_constant_folding_cond_eq(TCGCond c)
450{
451 switch (c) {
452 case TCG_COND_GT:
453 case TCG_COND_LTU:
454 case TCG_COND_LT:
455 case TCG_COND_GTU:
456 case TCG_COND_NE:
457 return 0;
458 case TCG_COND_GE:
459 case TCG_COND_GEU:
460 case TCG_COND_LE:
461 case TCG_COND_LEU:
462 case TCG_COND_EQ:
463 return 1;
464 default:
465 tcg_abort();
466 }
467}
468
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200469/* Return 2 if the condition can't be simplified, and the result
470 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200471static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
472 TCGArg y, TCGCond c)
473{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200474 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
475 switch (op_bits(op)) {
476 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700477 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200478 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700479 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
480 default:
481 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200482 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200483 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700484 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200485 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
486 switch (c) {
487 case TCG_COND_LTU:
488 return 0;
489 case TCG_COND_GEU:
490 return 1;
491 default:
492 return 2;
493 }
494 } else {
495 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200496 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200497}
498
Richard Henderson6c4382f2012-10-02 11:32:27 -0700499/* Return 2 if the condition can't be simplified, and the result
500 of the condition (0 or 1) if it can */
501static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
502{
503 TCGArg al = p1[0], ah = p1[1];
504 TCGArg bl = p2[0], bh = p2[1];
505
506 if (temps[bl].state == TCG_TEMP_CONST
507 && temps[bh].state == TCG_TEMP_CONST) {
508 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
509
510 if (temps[al].state == TCG_TEMP_CONST
511 && temps[ah].state == TCG_TEMP_CONST) {
512 uint64_t a;
513 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
514 return do_constant_folding_cond_64(a, b, c);
515 }
516 if (b == 0) {
517 switch (c) {
518 case TCG_COND_LTU:
519 return 0;
520 case TCG_COND_GEU:
521 return 1;
522 default:
523 break;
524 }
525 }
526 }
527 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
528 return do_constant_folding_cond_eq(c);
529 }
530 return 2;
531}
532
Richard Henderson24c9ae42012-10-02 11:32:21 -0700533static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
534{
535 TCGArg a1 = *p1, a2 = *p2;
536 int sum = 0;
537 sum += temps[a1].state == TCG_TEMP_CONST;
538 sum -= temps[a2].state == TCG_TEMP_CONST;
539
540 /* Prefer the constant in second argument, and then the form
541 op a, a, b, which is better handled on non-RISC hosts. */
542 if (sum > 0 || (sum == 0 && dest == a2)) {
543 *p1 = a2;
544 *p2 = a1;
545 return true;
546 }
547 return false;
548}
549
Richard Henderson0bfcb862012-10-02 11:32:23 -0700550static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
551{
552 int sum = 0;
553 sum += temps[p1[0]].state == TCG_TEMP_CONST;
554 sum += temps[p1[1]].state == TCG_TEMP_CONST;
555 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
556 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
557 if (sum > 0) {
558 TCGArg t;
559 t = p1[0], p1[0] = p2[0], p2[0] = t;
560 t = p1[1], p1[1] = p2[1], p2[1] = t;
561 return true;
562 }
563 return false;
564}
565
Kirill Batuzov22613af2011-07-07 16:37:13 +0400566/* Propagate constants and copies, fold constant expressions. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700567static void tcg_constant_folding(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400568{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700569 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700570
Kirill Batuzov22613af2011-07-07 16:37:13 +0400571 /* Array VALS has an element for each temp.
572 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200573 If this temp is a copy of other ones then the other copies are
574 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400575
576 nb_temps = s->nb_temps;
577 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800578 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400579
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700580 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700581 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700582 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700583 TCGArg tmp;
584
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700585 TCGOp * const op = &s->gen_op_buf[oi];
586 TCGArg * const args = &s->gen_opparam_buf[op->args];
587 TCGOpcode opc = op->opc;
588 const TCGOpDef *def = &tcg_op_defs[opc];
589
590 oi_next = op->next;
591 if (opc == INDEX_op_call) {
592 nb_oargs = op->callo;
593 nb_iargs = op->calli;
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200594 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700595 nb_oargs = def->nb_oargs;
596 nb_iargs = def->nb_iargs;
Richard Hendersoncf066672014-03-22 20:06:52 -0700597 }
598
599 /* Do copy propagation */
600 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
601 if (temps[args[i]].state == TCG_TEMP_COPY) {
602 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400603 }
604 }
605
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400606 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700607 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400608 CASE_OP_32_64(add):
609 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400610 CASE_OP_32_64(and):
611 CASE_OP_32_64(or):
612 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700613 CASE_OP_32_64(eqv):
614 CASE_OP_32_64(nand):
615 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700616 CASE_OP_32_64(muluh):
617 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700618 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400619 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200620 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700621 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200622 args[2] = tcg_swap_cond(args[2]);
623 }
624 break;
625 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700626 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200627 args[3] = tcg_swap_cond(args[3]);
628 }
629 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700630 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700631 if (swap_commutative(-1, &args[1], &args[2])) {
632 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700633 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700634 /* For movcond, we canonicalize the "false" input reg to match
635 the destination reg so that the tcg backend can implement
636 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700637 if (swap_commutative(args[0], &args[4], &args[3])) {
638 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700639 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700640 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800641 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700642 swap_commutative(args[0], &args[2], &args[4]);
643 swap_commutative(args[1], &args[3], &args[5]);
644 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800645 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800646 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700647 swap_commutative(args[0], &args[2], &args[3]);
648 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700649 case INDEX_op_brcond2_i32:
650 if (swap_commutative2(&args[0], &args[2])) {
651 args[4] = tcg_swap_cond(args[4]);
652 }
653 break;
654 case INDEX_op_setcond2_i32:
655 if (swap_commutative2(&args[1], &args[3])) {
656 args[5] = tcg_swap_cond(args[5]);
657 }
658 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400659 default:
660 break;
661 }
662
Richard Henderson2d497542013-03-21 09:13:33 -0700663 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
664 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700665 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200666 CASE_OP_32_64(shl):
667 CASE_OP_32_64(shr):
668 CASE_OP_32_64(sar):
669 CASE_OP_32_64(rotl):
670 CASE_OP_32_64(rotr):
671 if (temps[args[1]].state == TCG_TEMP_CONST
672 && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700673 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200674 continue;
675 }
676 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700677 CASE_OP_32_64(sub):
678 {
679 TCGOpcode neg_op;
680 bool have_neg;
681
682 if (temps[args[2]].state == TCG_TEMP_CONST) {
683 /* Proceed with possible constant folding. */
684 break;
685 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700686 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700687 neg_op = INDEX_op_neg_i32;
688 have_neg = TCG_TARGET_HAS_neg_i32;
689 } else {
690 neg_op = INDEX_op_neg_i64;
691 have_neg = TCG_TARGET_HAS_neg_i64;
692 }
693 if (!have_neg) {
694 break;
695 }
696 if (temps[args[1]].state == TCG_TEMP_CONST
697 && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700698 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700699 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700700 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700701 continue;
702 }
703 }
704 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800705 CASE_OP_32_64(xor):
706 CASE_OP_32_64(nand):
707 if (temps[args[1]].state != TCG_TEMP_CONST
708 && temps[args[2]].state == TCG_TEMP_CONST
709 && temps[args[2]].val == -1) {
710 i = 1;
711 goto try_not;
712 }
713 break;
714 CASE_OP_32_64(nor):
715 if (temps[args[1]].state != TCG_TEMP_CONST
716 && temps[args[2]].state == TCG_TEMP_CONST
717 && temps[args[2]].val == 0) {
718 i = 1;
719 goto try_not;
720 }
721 break;
722 CASE_OP_32_64(andc):
723 if (temps[args[2]].state != TCG_TEMP_CONST
724 && temps[args[1]].state == TCG_TEMP_CONST
725 && temps[args[1]].val == -1) {
726 i = 2;
727 goto try_not;
728 }
729 break;
730 CASE_OP_32_64(orc):
731 CASE_OP_32_64(eqv):
732 if (temps[args[2]].state != TCG_TEMP_CONST
733 && temps[args[1]].state == TCG_TEMP_CONST
734 && temps[args[1]].val == 0) {
735 i = 2;
736 goto try_not;
737 }
738 break;
739 try_not:
740 {
741 TCGOpcode not_op;
742 bool have_not;
743
744 if (def->flags & TCG_OPF_64BIT) {
745 not_op = INDEX_op_not_i64;
746 have_not = TCG_TARGET_HAS_not_i64;
747 } else {
748 not_op = INDEX_op_not_i32;
749 have_not = TCG_TARGET_HAS_not_i32;
750 }
751 if (!have_not) {
752 break;
753 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700754 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800755 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700756 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800757 continue;
758 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200759 default:
760 break;
761 }
762
Richard Henderson464a1442014-01-31 07:42:11 -0600763 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700764 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400765 CASE_OP_32_64(add):
766 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400767 CASE_OP_32_64(shl):
768 CASE_OP_32_64(shr):
769 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700770 CASE_OP_32_64(rotl):
771 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200772 CASE_OP_32_64(or):
773 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600774 CASE_OP_32_64(andc):
775 if (temps[args[1]].state != TCG_TEMP_CONST
776 && temps[args[2]].state == TCG_TEMP_CONST
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400777 && temps[args[2]].val == 0) {
Richard Henderson464a1442014-01-31 07:42:11 -0600778 goto do_mov3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400779 }
780 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600781 CASE_OP_32_64(and):
782 CASE_OP_32_64(orc):
783 CASE_OP_32_64(eqv):
784 if (temps[args[1]].state != TCG_TEMP_CONST
785 && temps[args[2]].state == TCG_TEMP_CONST
786 && temps[args[2]].val == -1) {
787 goto do_mov3;
788 }
789 break;
790 do_mov3:
791 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700792 tcg_op_remove(s, op);
Richard Henderson464a1442014-01-31 07:42:11 -0600793 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700794 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Richard Henderson464a1442014-01-31 07:42:11 -0600795 }
Richard Henderson464a1442014-01-31 07:42:11 -0600796 continue;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200797 default:
798 break;
799 }
800
Aurelien Jarno30312442013-09-03 08:27:38 +0200801 /* Simplify using known-zero bits. Currently only ops with a single
802 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800803 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800804 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700805 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800806 CASE_OP_32_64(ext8s):
807 if ((temps[args[1]].mask & 0x80) != 0) {
808 break;
809 }
810 CASE_OP_32_64(ext8u):
811 mask = 0xff;
812 goto and_const;
813 CASE_OP_32_64(ext16s):
814 if ((temps[args[1]].mask & 0x8000) != 0) {
815 break;
816 }
817 CASE_OP_32_64(ext16u):
818 mask = 0xffff;
819 goto and_const;
820 case INDEX_op_ext32s_i64:
821 if ((temps[args[1]].mask & 0x80000000) != 0) {
822 break;
823 }
824 case INDEX_op_ext32u_i64:
825 mask = 0xffffffffU;
826 goto and_const;
827
828 CASE_OP_32_64(and):
829 mask = temps[args[2]].mask;
830 if (temps[args[2]].state == TCG_TEMP_CONST) {
831 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800832 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800833 }
834 mask = temps[args[1]].mask & mask;
835 break;
836
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800837 CASE_OP_32_64(andc):
838 /* Known-zeros does not imply known-ones. Therefore unless
839 args[2] is constant, we can't infer anything from it. */
840 if (temps[args[2]].state == TCG_TEMP_CONST) {
841 mask = ~temps[args[2]].mask;
842 goto and_const;
843 }
844 /* But we certainly know nothing outside args[1] may be set. */
845 mask = temps[args[1]].mask;
846 break;
847
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200848 case INDEX_op_sar_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800849 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700850 tmp = temps[args[2]].val & 31;
851 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200852 }
853 break;
854 case INDEX_op_sar_i64:
855 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700856 tmp = temps[args[2]].val & 63;
857 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800858 }
859 break;
860
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200861 case INDEX_op_shr_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800862 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700863 tmp = temps[args[2]].val & 31;
864 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200865 }
866 break;
867 case INDEX_op_shr_i64:
868 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700869 tmp = temps[args[2]].val & 63;
870 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800871 }
872 break;
873
Richard Henderson4bb7a412013-09-09 17:03:24 -0700874 case INDEX_op_trunc_shr_i32:
875 mask = (uint64_t)temps[args[1]].mask >> args[2];
876 break;
877
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800878 CASE_OP_32_64(shl):
879 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700880 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
881 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800882 }
883 break;
884
885 CASE_OP_32_64(neg):
886 /* Set to 1 all bits to the left of the rightmost. */
887 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
888 break;
889
890 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700891 mask = deposit64(temps[args[1]].mask, args[3], args[4],
892 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800893 break;
894
895 CASE_OP_32_64(or):
896 CASE_OP_32_64(xor):
897 mask = temps[args[1]].mask | temps[args[2]].mask;
898 break;
899
900 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700901 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800902 mask = 1;
903 break;
904
905 CASE_OP_32_64(movcond):
906 mask = temps[args[3]].mask | temps[args[4]].mask;
907 break;
908
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200909 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200910 mask = 0xff;
911 break;
912 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200913 mask = 0xffff;
914 break;
915 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200916 mask = 0xffffffffu;
917 break;
918
919 CASE_OP_32_64(qemu_ld):
920 {
Richard Henderson59227d52015-05-12 11:51:44 -0700921 TCGMemOpIdx oi = args[nb_oargs + nb_iargs];
922 TCGMemOp mop = get_memop(oi);
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200923 if (!(mop & MO_SIGN)) {
924 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
925 }
926 }
927 break;
928
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800929 default:
930 break;
931 }
932
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700933 /* 32-bit ops generate 32-bit results. For the result is zero test
934 below, we can ignore high bits, but for further optimizations we
935 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700936 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700937 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700938 mask |= ~(tcg_target_ulong)0xffffffffu;
939 partmask &= 0xffffffffu;
940 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200941 }
942
Richard Henderson24666ba2014-05-22 11:14:10 -0700943 if (partmask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700944 assert(nb_oargs == 1);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700945 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800946 continue;
947 }
948 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700949 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800950 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700951 tcg_op_remove(s, op);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800952 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700953 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800954 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700955 tcg_opt_gen_movi(s, op, args, opc,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700956 args[0], temps[args[1]].val);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800957 }
Paolo Bonzini633f6502013-01-11 15:42:53 -0800958 continue;
959 }
960
Aurelien Jarno56e49432012-09-06 16:47:13 +0200961 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700962 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200963 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400964 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700965 CASE_OP_32_64(muluh):
966 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400967 if ((temps[args[2]].state == TCG_TEMP_CONST
968 && temps[args[2]].val == 0)) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700969 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400970 continue;
971 }
972 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200973 default:
974 break;
975 }
976
977 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700978 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400979 CASE_OP_32_64(or):
980 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200981 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200982 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700983 tcg_op_remove(s, op);
Richard Henderson2374c4b2015-03-13 12:26:21 -0700984 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700985 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Richard Henderson2374c4b2015-03-13 12:26:21 -0700986 } else {
987 tcg_opt_gen_movi(s, op, args, opc,
988 args[0], temps[args[1]].val);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400989 }
990 continue;
991 }
992 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000993 default:
994 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400995 }
996
Aurelien Jarno3c941932012-09-18 19:12:36 +0200997 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700998 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800999 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +02001000 CASE_OP_32_64(sub):
1001 CASE_OP_32_64(xor):
1002 if (temps_are_copies(args[1], args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001003 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +02001004 continue;
1005 }
1006 break;
1007 default:
1008 break;
1009 }
1010
Kirill Batuzov22613af2011-07-07 16:37:13 +04001011 /* Propagate constants through copy operations and do constant
1012 folding. Constants will be substituted to arguments by register
1013 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001014 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001015 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001016 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001017 tcg_op_remove(s, op);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001018 break;
1019 }
1020 if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001021 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001022 break;
1023 }
1024 /* Source argument is constant. Rewrite the operation and
1025 let movi case handle it. */
Kirill Batuzov22613af2011-07-07 16:37:13 +04001026 args[1] = temps[args[1]].val;
1027 /* fallthrough */
1028 CASE_OP_32_64(movi):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001029 tcg_opt_gen_movi(s, op, args, opc, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001030 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001031
Kirill Batuzova640f032011-07-07 16:37:17 +04001032 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001033 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001034 CASE_OP_32_64(ext8s):
1035 CASE_OP_32_64(ext8u):
1036 CASE_OP_32_64(ext16s):
1037 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001038 case INDEX_op_ext32s_i64:
1039 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +04001040 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001041 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
1042 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001043 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001044 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001045 goto do_default;
1046
Richard Henderson4bb7a412013-09-09 17:03:24 -07001047 case INDEX_op_trunc_shr_i32:
1048 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001049 tmp = do_constant_folding(opc, temps[args[1]].val, args[2]);
1050 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001051 break;
1052 }
1053 goto do_default;
1054
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001055 CASE_OP_32_64(add):
1056 CASE_OP_32_64(sub):
1057 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001058 CASE_OP_32_64(or):
1059 CASE_OP_32_64(and):
1060 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001061 CASE_OP_32_64(shl):
1062 CASE_OP_32_64(shr):
1063 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001064 CASE_OP_32_64(rotl):
1065 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001066 CASE_OP_32_64(andc):
1067 CASE_OP_32_64(orc):
1068 CASE_OP_32_64(eqv):
1069 CASE_OP_32_64(nand):
1070 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001071 CASE_OP_32_64(muluh):
1072 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001073 CASE_OP_32_64(div):
1074 CASE_OP_32_64(divu):
1075 CASE_OP_32_64(rem):
1076 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001077 if (temps[args[1]].state == TCG_TEMP_CONST
1078 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001079 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001080 temps[args[2]].val);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001081 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001082 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001083 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001084 goto do_default;
1085
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001086 CASE_OP_32_64(deposit):
1087 if (temps[args[1]].state == TCG_TEMP_CONST
1088 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersond998e552014-03-18 14:23:52 -07001089 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1090 temps[args[2]].val);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001091 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001092 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001093 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001094 goto do_default;
1095
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001096 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001097 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001098 if (tmp != 2) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001099 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001100 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001101 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001102 goto do_default;
1103
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001104 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001105 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001106 if (tmp != 2) {
1107 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001108 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001109 op->opc = INDEX_op_br;
1110 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001111 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001112 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001113 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001114 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001115 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001116 goto do_default;
1117
Richard Hendersonfa01a202012-09-21 10:13:37 -07001118 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001119 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001120 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001121 if (temps_are_copies(args[0], args[4-tmp])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001122 tcg_op_remove(s, op);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001123 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001124 tcg_opt_gen_movi(s, op, args, opc,
Richard Hendersona62f6f52014-05-22 10:59:12 -07001125 args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001126 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001127 tcg_opt_gen_mov(s, op, args, opc, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001128 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001129 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001130 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001131 goto do_default;
1132
Richard Henderson212c3282012-10-02 11:32:28 -07001133 case INDEX_op_add2_i32:
1134 case INDEX_op_sub2_i32:
1135 if (temps[args[2]].state == TCG_TEMP_CONST
1136 && temps[args[3]].state == TCG_TEMP_CONST
1137 && temps[args[4]].state == TCG_TEMP_CONST
1138 && temps[args[5]].state == TCG_TEMP_CONST) {
1139 uint32_t al = temps[args[2]].val;
1140 uint32_t ah = temps[args[3]].val;
1141 uint32_t bl = temps[args[4]].val;
1142 uint32_t bh = temps[args[5]].val;
1143 uint64_t a = ((uint64_t)ah << 32) | al;
1144 uint64_t b = ((uint64_t)bh << 32) | bl;
1145 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001146 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1147 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001148
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001149 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001150 a += b;
1151 } else {
1152 a -= b;
1153 }
1154
Richard Henderson212c3282012-10-02 11:32:28 -07001155 rl = args[0];
1156 rh = args[1];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001157 tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)a);
1158 tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(a >> 32));
1159
1160 /* We've done all we need to do with the movi. Skip it. */
1161 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001162 break;
1163 }
1164 goto do_default;
1165
Richard Henderson14149682012-10-02 11:32:30 -07001166 case INDEX_op_mulu2_i32:
1167 if (temps[args[2]].state == TCG_TEMP_CONST
1168 && temps[args[3]].state == TCG_TEMP_CONST) {
1169 uint32_t a = temps[args[2]].val;
1170 uint32_t b = temps[args[3]].val;
1171 uint64_t r = (uint64_t)a * b;
1172 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001173 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1174 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001175
1176 rl = args[0];
1177 rh = args[1];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001178 tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)r);
1179 tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(r >> 32));
1180
1181 /* We've done all we need to do with the movi. Skip it. */
1182 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001183 break;
1184 }
1185 goto do_default;
1186
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001187 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001188 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1189 if (tmp != 2) {
1190 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001191 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001192 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001193 op->opc = INDEX_op_br;
1194 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001195 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001196 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001197 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001198 }
1199 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1200 && temps[args[2]].state == TCG_TEMP_CONST
1201 && temps[args[3]].state == TCG_TEMP_CONST
1202 && temps[args[2]].val == 0
1203 && temps[args[3]].val == 0) {
1204 /* Simplify LT/GE comparisons vs zero to a single compare
1205 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001206 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001207 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001208 op->opc = INDEX_op_brcond_i32;
1209 args[0] = args[1];
1210 args[1] = args[3];
1211 args[2] = args[4];
1212 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001213 } else if (args[4] == TCG_COND_EQ) {
1214 /* Simplify EQ comparisons where one of the pairs
1215 can be simplified. */
1216 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1217 args[0], args[2], TCG_COND_EQ);
1218 if (tmp == 0) {
1219 goto do_brcond_false;
1220 } else if (tmp == 1) {
1221 goto do_brcond_high;
1222 }
1223 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1224 args[1], args[3], TCG_COND_EQ);
1225 if (tmp == 0) {
1226 goto do_brcond_false;
1227 } else if (tmp != 1) {
1228 goto do_default;
1229 }
1230 do_brcond_low:
1231 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001232 op->opc = INDEX_op_brcond_i32;
1233 args[1] = args[2];
1234 args[2] = args[4];
1235 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001236 } else if (args[4] == TCG_COND_NE) {
1237 /* Simplify NE comparisons where one of the pairs
1238 can be simplified. */
1239 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1240 args[0], args[2], TCG_COND_NE);
1241 if (tmp == 0) {
1242 goto do_brcond_high;
1243 } else if (tmp == 1) {
1244 goto do_brcond_true;
1245 }
1246 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1247 args[1], args[3], TCG_COND_NE);
1248 if (tmp == 0) {
1249 goto do_brcond_low;
1250 } else if (tmp == 1) {
1251 goto do_brcond_true;
1252 }
1253 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001254 } else {
1255 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001256 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001257 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001258
1259 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001260 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1261 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001262 do_setcond_const:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001263 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001264 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1265 && temps[args[3]].state == TCG_TEMP_CONST
1266 && temps[args[4]].state == TCG_TEMP_CONST
1267 && temps[args[3]].val == 0
1268 && temps[args[4]].val == 0) {
1269 /* Simplify LT/GE comparisons vs zero to a single compare
1270 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001271 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001272 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001273 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001274 op->opc = INDEX_op_setcond_i32;
1275 args[1] = args[2];
1276 args[2] = args[4];
1277 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001278 } else if (args[5] == TCG_COND_EQ) {
1279 /* Simplify EQ comparisons where one of the pairs
1280 can be simplified. */
1281 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1282 args[1], args[3], TCG_COND_EQ);
1283 if (tmp == 0) {
1284 goto do_setcond_const;
1285 } else if (tmp == 1) {
1286 goto do_setcond_high;
1287 }
1288 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1289 args[2], args[4], TCG_COND_EQ);
1290 if (tmp == 0) {
1291 goto do_setcond_high;
1292 } else if (tmp != 1) {
1293 goto do_default;
1294 }
1295 do_setcond_low:
1296 reset_temp(args[0]);
1297 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001298 op->opc = INDEX_op_setcond_i32;
1299 args[2] = args[3];
1300 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001301 } else if (args[5] == TCG_COND_NE) {
1302 /* Simplify NE comparisons where one of the pairs
1303 can be simplified. */
1304 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1305 args[1], args[3], TCG_COND_NE);
1306 if (tmp == 0) {
1307 goto do_setcond_high;
1308 } else if (tmp == 1) {
1309 goto do_setcond_const;
1310 }
1311 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1312 args[2], args[4], TCG_COND_NE);
1313 if (tmp == 0) {
1314 goto do_setcond_low;
1315 } else if (tmp == 1) {
1316 goto do_setcond_const;
1317 }
1318 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001319 } else {
1320 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001321 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001322 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001323
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001324 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001325 if (!(args[nb_oargs + nb_iargs + 1]
1326 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001327 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001328 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001329 }
1330 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001331 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001332
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001333 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001334 do_default:
1335 /* Default case: we know nothing about operation (or were unable
1336 to compute the operation result) so no propagation is done.
1337 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001338 block, otherwise we only trash the output args. "mask" is
1339 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001340 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001341 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001342 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001343 do_reset_output:
1344 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001345 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001346 /* Save the corresponding known-zero bits mask for the
1347 first output argument (only one supported so far). */
1348 if (i == 0) {
1349 temps[args[i]].mask = mask;
1350 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001351 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001352 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001353 break;
1354 }
1355 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001356}
1357
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001358void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001359{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001360 tcg_constant_folding(s);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001361}