blob: 973fbb4729050a8df7de7a704d6c6aba89b9602b [file] [log] [blame]
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001/*
2 * Optimizations for Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "config.h"
27
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "qemu-common.h"
32#include "tcg-op.h"
33
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040034#define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040037
Kirill Batuzov22613af2011-07-07 16:37:13 +040038typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
Kirill Batuzov22613af2011-07-07 16:37:13 +040042} tcg_temp_state;
43
44struct tcg_temp_info {
45 tcg_temp_state state;
46 uint16_t prev_copy;
47 uint16_t next_copy;
48 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080049 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040050};
51
52static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020054/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
55 the copy flag from the left temp. */
56static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040057{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020058 if (temps[temp].state == TCG_TEMP_COPY) {
59 if (temps[temp].prev_copy == temps[temp].next_copy) {
60 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
61 } else {
62 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
63 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
Kirill Batuzov22613af2011-07-07 16:37:13 +040064 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040065 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020066 temps[temp].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080067 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040068}
69
Paolo Bonzinid193a142013-01-11 15:42:51 -080070/* Reset all temporaries, given that there are NB_TEMPS of them. */
71static void reset_all_temps(int nb_temps)
72{
73 int i;
74 for (i = 0; i < nb_temps; i++) {
75 temps[i].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080076 temps[i].mask = -1;
Paolo Bonzinid193a142013-01-11 15:42:51 -080077 }
78}
79
Blue Swirlfe0de7a2011-07-30 19:18:32 +000080static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040081{
Richard Henderson8399ad52011-08-17 14:11:45 -070082 const TCGOpDef *def = &tcg_op_defs[op];
83 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040084}
85
Richard Hendersona62f6f52014-05-22 10:59:12 -070086static TCGOpcode op_to_mov(TCGOpcode op)
87{
88 switch (op_bits(op)) {
89 case 32:
90 return INDEX_op_mov_i32;
91 case 64:
92 return INDEX_op_mov_i64;
93 default:
94 fprintf(stderr, "op_to_mov: unexpected return value of "
95 "function op_bits.\n");
96 tcg_abort();
97 }
98}
99
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000100static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400101{
102 switch (op_bits(op)) {
103 case 32:
104 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400105 case 64:
106 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400107 default:
108 fprintf(stderr, "op_to_movi: unexpected return value of "
109 "function op_bits.\n");
110 tcg_abort();
111 }
112}
113
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200114static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
115{
116 TCGArg i;
117
118 /* If this is already a global, we can't do better. */
119 if (temp < s->nb_globals) {
120 return temp;
121 }
122
123 /* Search for a global first. */
124 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
125 if (i < s->nb_globals) {
126 return i;
127 }
128 }
129
130 /* If it is a temp, search for a temp local. */
131 if (!s->temps[temp].temp_local) {
132 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
133 if (s->temps[i].temp_local) {
134 return i;
135 }
136 }
137 }
138
139 /* Failure to find a better representation, return the same temp. */
140 return temp;
141}
142
143static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
144{
145 TCGArg i;
146
147 if (arg1 == arg2) {
148 return true;
149 }
150
151 if (temps[arg1].state != TCG_TEMP_COPY
152 || temps[arg2].state != TCG_TEMP_COPY) {
153 return false;
154 }
155
156 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
157 if (i == arg2) {
158 return true;
159 }
160 }
161
162 return false;
163}
164
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700165static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700166 TCGOpcode old_op, TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400167{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700168 TCGOpcode new_op = op_to_mov(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700169 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700170
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700171 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700172
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800173 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700174 mask = temps[src].mask;
175 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
176 /* High bits of the destination are now garbage. */
177 mask |= ~0xffffffffull;
178 }
179 temps[dst].mask = mask;
180
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800181 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200182
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800183 if (s->temps[src].type == s->temps[dst].type) {
184 if (temps[src].state != TCG_TEMP_COPY) {
185 temps[src].state = TCG_TEMP_COPY;
186 temps[src].next_copy = src;
187 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400188 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800189 temps[dst].state = TCG_TEMP_COPY;
190 temps[dst].next_copy = temps[src].next_copy;
191 temps[dst].prev_copy = src;
192 temps[temps[dst].next_copy].prev_copy = dst;
193 temps[src].next_copy = dst;
194 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200195
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700196 args[0] = dst;
197 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400198}
199
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700200static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700201 TCGOpcode old_op, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400202{
Richard Hendersona62f6f52014-05-22 10:59:12 -0700203 TCGOpcode new_op = op_to_movi(old_op);
Richard Henderson24666ba2014-05-22 11:14:10 -0700204 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700205
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700206 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700207
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800208 reset_temp(dst);
209 temps[dst].state = TCG_TEMP_CONST;
210 temps[dst].val = val;
Richard Henderson24666ba2014-05-22 11:14:10 -0700211 mask = val;
212 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
213 /* High bits of the destination are now garbage. */
214 mask |= ~0xffffffffull;
215 }
216 temps[dst].mask = mask;
217
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700218 args[0] = dst;
219 args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400220}
221
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000222static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400223{
Richard Henderson03271522013-08-14 14:35:56 -0700224 uint64_t l64, h64;
225
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400226 switch (op) {
227 CASE_OP_32_64(add):
228 return x + y;
229
230 CASE_OP_32_64(sub):
231 return x - y;
232
233 CASE_OP_32_64(mul):
234 return x * y;
235
Kirill Batuzov9a810902011-07-07 16:37:15 +0400236 CASE_OP_32_64(and):
237 return x & y;
238
239 CASE_OP_32_64(or):
240 return x | y;
241
242 CASE_OP_32_64(xor):
243 return x ^ y;
244
Kirill Batuzov55c09752011-07-07 16:37:16 +0400245 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700246 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400247
Kirill Batuzov55c09752011-07-07 16:37:16 +0400248 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700249 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400250
251 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700252 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400253
Richard Henderson4bb7a412013-09-09 17:03:24 -0700254 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400255 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700256 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400257
258 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700259 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400260
Kirill Batuzov55c09752011-07-07 16:37:16 +0400261 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700262 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400263
264 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700265 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400266
Kirill Batuzov55c09752011-07-07 16:37:16 +0400267 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700268 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400269
270 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700271 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400272
Kirill Batuzov55c09752011-07-07 16:37:16 +0400273 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700274 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400275
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700276 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400277 return ~x;
278
Richard Hendersoncb25c802011-08-17 14:11:47 -0700279 CASE_OP_32_64(neg):
280 return -x;
281
282 CASE_OP_32_64(andc):
283 return x & ~y;
284
285 CASE_OP_32_64(orc):
286 return x | ~y;
287
288 CASE_OP_32_64(eqv):
289 return ~(x ^ y);
290
291 CASE_OP_32_64(nand):
292 return ~(x & y);
293
294 CASE_OP_32_64(nor):
295 return ~(x | y);
296
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700297 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400298 return (int8_t)x;
299
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700300 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400301 return (int16_t)x;
302
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700303 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400304 return (uint8_t)x;
305
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700306 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400307 return (uint16_t)x;
308
Kirill Batuzova640f032011-07-07 16:37:17 +0400309 case INDEX_op_ext32s_i64:
310 return (int32_t)x;
311
312 case INDEX_op_ext32u_i64:
313 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400314
Richard Henderson03271522013-08-14 14:35:56 -0700315 case INDEX_op_muluh_i32:
316 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
317 case INDEX_op_mulsh_i32:
318 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
319
320 case INDEX_op_muluh_i64:
321 mulu64(&l64, &h64, x, y);
322 return h64;
323 case INDEX_op_mulsh_i64:
324 muls64(&l64, &h64, x, y);
325 return h64;
326
Richard Henderson01547f72013-08-14 15:22:46 -0700327 case INDEX_op_div_i32:
328 /* Avoid crashing on divide by zero, otherwise undefined. */
329 return (int32_t)x / ((int32_t)y ? : 1);
330 case INDEX_op_divu_i32:
331 return (uint32_t)x / ((uint32_t)y ? : 1);
332 case INDEX_op_div_i64:
333 return (int64_t)x / ((int64_t)y ? : 1);
334 case INDEX_op_divu_i64:
335 return (uint64_t)x / ((uint64_t)y ? : 1);
336
337 case INDEX_op_rem_i32:
338 return (int32_t)x % ((int32_t)y ? : 1);
339 case INDEX_op_remu_i32:
340 return (uint32_t)x % ((uint32_t)y ? : 1);
341 case INDEX_op_rem_i64:
342 return (int64_t)x % ((int64_t)y ? : 1);
343 case INDEX_op_remu_i64:
344 return (uint64_t)x % ((uint64_t)y ? : 1);
345
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400346 default:
347 fprintf(stderr,
348 "Unrecognized operation %d in do_constant_folding.\n", op);
349 tcg_abort();
350 }
351}
352
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000353static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400354{
355 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400356 if (op_bits(op) == 32) {
357 res &= 0xffffffff;
358 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400359 return res;
360}
361
Richard Henderson9519da72012-10-02 11:32:26 -0700362static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
363{
364 switch (c) {
365 case TCG_COND_EQ:
366 return x == y;
367 case TCG_COND_NE:
368 return x != y;
369 case TCG_COND_LT:
370 return (int32_t)x < (int32_t)y;
371 case TCG_COND_GE:
372 return (int32_t)x >= (int32_t)y;
373 case TCG_COND_LE:
374 return (int32_t)x <= (int32_t)y;
375 case TCG_COND_GT:
376 return (int32_t)x > (int32_t)y;
377 case TCG_COND_LTU:
378 return x < y;
379 case TCG_COND_GEU:
380 return x >= y;
381 case TCG_COND_LEU:
382 return x <= y;
383 case TCG_COND_GTU:
384 return x > y;
385 default:
386 tcg_abort();
387 }
388}
389
390static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
391{
392 switch (c) {
393 case TCG_COND_EQ:
394 return x == y;
395 case TCG_COND_NE:
396 return x != y;
397 case TCG_COND_LT:
398 return (int64_t)x < (int64_t)y;
399 case TCG_COND_GE:
400 return (int64_t)x >= (int64_t)y;
401 case TCG_COND_LE:
402 return (int64_t)x <= (int64_t)y;
403 case TCG_COND_GT:
404 return (int64_t)x > (int64_t)y;
405 case TCG_COND_LTU:
406 return x < y;
407 case TCG_COND_GEU:
408 return x >= y;
409 case TCG_COND_LEU:
410 return x <= y;
411 case TCG_COND_GTU:
412 return x > y;
413 default:
414 tcg_abort();
415 }
416}
417
418static bool do_constant_folding_cond_eq(TCGCond c)
419{
420 switch (c) {
421 case TCG_COND_GT:
422 case TCG_COND_LTU:
423 case TCG_COND_LT:
424 case TCG_COND_GTU:
425 case TCG_COND_NE:
426 return 0;
427 case TCG_COND_GE:
428 case TCG_COND_GEU:
429 case TCG_COND_LE:
430 case TCG_COND_LEU:
431 case TCG_COND_EQ:
432 return 1;
433 default:
434 tcg_abort();
435 }
436}
437
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200438/* Return 2 if the condition can't be simplified, and the result
439 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200440static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
441 TCGArg y, TCGCond c)
442{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200443 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
444 switch (op_bits(op)) {
445 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700446 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200447 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700448 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
449 default:
450 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200451 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200452 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700453 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200454 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
455 switch (c) {
456 case TCG_COND_LTU:
457 return 0;
458 case TCG_COND_GEU:
459 return 1;
460 default:
461 return 2;
462 }
463 } else {
464 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200465 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200466}
467
Richard Henderson6c4382f2012-10-02 11:32:27 -0700468/* Return 2 if the condition can't be simplified, and the result
469 of the condition (0 or 1) if it can */
470static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
471{
472 TCGArg al = p1[0], ah = p1[1];
473 TCGArg bl = p2[0], bh = p2[1];
474
475 if (temps[bl].state == TCG_TEMP_CONST
476 && temps[bh].state == TCG_TEMP_CONST) {
477 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
478
479 if (temps[al].state == TCG_TEMP_CONST
480 && temps[ah].state == TCG_TEMP_CONST) {
481 uint64_t a;
482 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
483 return do_constant_folding_cond_64(a, b, c);
484 }
485 if (b == 0) {
486 switch (c) {
487 case TCG_COND_LTU:
488 return 0;
489 case TCG_COND_GEU:
490 return 1;
491 default:
492 break;
493 }
494 }
495 }
496 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
497 return do_constant_folding_cond_eq(c);
498 }
499 return 2;
500}
501
Richard Henderson24c9ae42012-10-02 11:32:21 -0700502static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
503{
504 TCGArg a1 = *p1, a2 = *p2;
505 int sum = 0;
506 sum += temps[a1].state == TCG_TEMP_CONST;
507 sum -= temps[a2].state == TCG_TEMP_CONST;
508
509 /* Prefer the constant in second argument, and then the form
510 op a, a, b, which is better handled on non-RISC hosts. */
511 if (sum > 0 || (sum == 0 && dest == a2)) {
512 *p1 = a2;
513 *p2 = a1;
514 return true;
515 }
516 return false;
517}
518
Richard Henderson0bfcb862012-10-02 11:32:23 -0700519static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
520{
521 int sum = 0;
522 sum += temps[p1[0]].state == TCG_TEMP_CONST;
523 sum += temps[p1[1]].state == TCG_TEMP_CONST;
524 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
525 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
526 if (sum > 0) {
527 TCGArg t;
528 t = p1[0], p1[0] = p2[0], p2[0] = t;
529 t = p1[1], p1[1] = p2[1], p2[1] = t;
530 return true;
531 }
532 return false;
533}
534
Kirill Batuzov22613af2011-07-07 16:37:13 +0400535/* Propagate constants and copies, fold constant expressions. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700536static void tcg_constant_folding(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400537{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700538 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700539
Kirill Batuzov22613af2011-07-07 16:37:13 +0400540 /* Array VALS has an element for each temp.
541 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200542 If this temp is a copy of other ones then the other copies are
543 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400544
545 nb_temps = s->nb_temps;
546 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800547 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400548
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700549 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700550 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700551 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700552 TCGArg tmp;
553
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700554 TCGOp * const op = &s->gen_op_buf[oi];
555 TCGArg * const args = &s->gen_opparam_buf[op->args];
556 TCGOpcode opc = op->opc;
557 const TCGOpDef *def = &tcg_op_defs[opc];
558
559 oi_next = op->next;
560 if (opc == INDEX_op_call) {
561 nb_oargs = op->callo;
562 nb_iargs = op->calli;
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200563 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700564 nb_oargs = def->nb_oargs;
565 nb_iargs = def->nb_iargs;
Richard Hendersoncf066672014-03-22 20:06:52 -0700566 }
567
568 /* Do copy propagation */
569 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
570 if (temps[args[i]].state == TCG_TEMP_COPY) {
571 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400572 }
573 }
574
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400575 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700576 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400577 CASE_OP_32_64(add):
578 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400579 CASE_OP_32_64(and):
580 CASE_OP_32_64(or):
581 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700582 CASE_OP_32_64(eqv):
583 CASE_OP_32_64(nand):
584 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700585 CASE_OP_32_64(muluh):
586 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700587 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400588 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200589 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700590 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200591 args[2] = tcg_swap_cond(args[2]);
592 }
593 break;
594 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700595 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200596 args[3] = tcg_swap_cond(args[3]);
597 }
598 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700599 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700600 if (swap_commutative(-1, &args[1], &args[2])) {
601 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700602 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700603 /* For movcond, we canonicalize the "false" input reg to match
604 the destination reg so that the tcg backend can implement
605 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700606 if (swap_commutative(args[0], &args[4], &args[3])) {
607 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700608 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700609 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800610 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700611 swap_commutative(args[0], &args[2], &args[4]);
612 swap_commutative(args[1], &args[3], &args[5]);
613 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800614 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800615 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700616 swap_commutative(args[0], &args[2], &args[3]);
617 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700618 case INDEX_op_brcond2_i32:
619 if (swap_commutative2(&args[0], &args[2])) {
620 args[4] = tcg_swap_cond(args[4]);
621 }
622 break;
623 case INDEX_op_setcond2_i32:
624 if (swap_commutative2(&args[1], &args[3])) {
625 args[5] = tcg_swap_cond(args[5]);
626 }
627 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400628 default:
629 break;
630 }
631
Richard Henderson2d497542013-03-21 09:13:33 -0700632 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
633 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700634 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200635 CASE_OP_32_64(shl):
636 CASE_OP_32_64(shr):
637 CASE_OP_32_64(sar):
638 CASE_OP_32_64(rotl):
639 CASE_OP_32_64(rotr):
640 if (temps[args[1]].state == TCG_TEMP_CONST
641 && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700642 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200643 continue;
644 }
645 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700646 CASE_OP_32_64(sub):
647 {
648 TCGOpcode neg_op;
649 bool have_neg;
650
651 if (temps[args[2]].state == TCG_TEMP_CONST) {
652 /* Proceed with possible constant folding. */
653 break;
654 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700655 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700656 neg_op = INDEX_op_neg_i32;
657 have_neg = TCG_TARGET_HAS_neg_i32;
658 } else {
659 neg_op = INDEX_op_neg_i64;
660 have_neg = TCG_TARGET_HAS_neg_i64;
661 }
662 if (!have_neg) {
663 break;
664 }
665 if (temps[args[1]].state == TCG_TEMP_CONST
666 && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700667 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700668 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700669 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700670 continue;
671 }
672 }
673 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800674 CASE_OP_32_64(xor):
675 CASE_OP_32_64(nand):
676 if (temps[args[1]].state != TCG_TEMP_CONST
677 && temps[args[2]].state == TCG_TEMP_CONST
678 && temps[args[2]].val == -1) {
679 i = 1;
680 goto try_not;
681 }
682 break;
683 CASE_OP_32_64(nor):
684 if (temps[args[1]].state != TCG_TEMP_CONST
685 && temps[args[2]].state == TCG_TEMP_CONST
686 && temps[args[2]].val == 0) {
687 i = 1;
688 goto try_not;
689 }
690 break;
691 CASE_OP_32_64(andc):
692 if (temps[args[2]].state != TCG_TEMP_CONST
693 && temps[args[1]].state == TCG_TEMP_CONST
694 && temps[args[1]].val == -1) {
695 i = 2;
696 goto try_not;
697 }
698 break;
699 CASE_OP_32_64(orc):
700 CASE_OP_32_64(eqv):
701 if (temps[args[2]].state != TCG_TEMP_CONST
702 && temps[args[1]].state == TCG_TEMP_CONST
703 && temps[args[1]].val == 0) {
704 i = 2;
705 goto try_not;
706 }
707 break;
708 try_not:
709 {
710 TCGOpcode not_op;
711 bool have_not;
712
713 if (def->flags & TCG_OPF_64BIT) {
714 not_op = INDEX_op_not_i64;
715 have_not = TCG_TARGET_HAS_not_i64;
716 } else {
717 not_op = INDEX_op_not_i32;
718 have_not = TCG_TARGET_HAS_not_i32;
719 }
720 if (!have_not) {
721 break;
722 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700723 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800724 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700725 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800726 continue;
727 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200728 default:
729 break;
730 }
731
Richard Henderson464a1442014-01-31 07:42:11 -0600732 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700733 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400734 CASE_OP_32_64(add):
735 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400736 CASE_OP_32_64(shl):
737 CASE_OP_32_64(shr):
738 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700739 CASE_OP_32_64(rotl):
740 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200741 CASE_OP_32_64(or):
742 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600743 CASE_OP_32_64(andc):
744 if (temps[args[1]].state != TCG_TEMP_CONST
745 && temps[args[2]].state == TCG_TEMP_CONST
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400746 && temps[args[2]].val == 0) {
Richard Henderson464a1442014-01-31 07:42:11 -0600747 goto do_mov3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400748 }
749 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600750 CASE_OP_32_64(and):
751 CASE_OP_32_64(orc):
752 CASE_OP_32_64(eqv):
753 if (temps[args[1]].state != TCG_TEMP_CONST
754 && temps[args[2]].state == TCG_TEMP_CONST
755 && temps[args[2]].val == -1) {
756 goto do_mov3;
757 }
758 break;
759 do_mov3:
760 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700761 tcg_op_remove(s, op);
Richard Henderson464a1442014-01-31 07:42:11 -0600762 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700763 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Richard Henderson464a1442014-01-31 07:42:11 -0600764 }
Richard Henderson464a1442014-01-31 07:42:11 -0600765 continue;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200766 default:
767 break;
768 }
769
Aurelien Jarno30312442013-09-03 08:27:38 +0200770 /* Simplify using known-zero bits. Currently only ops with a single
771 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800772 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800773 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700774 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800775 CASE_OP_32_64(ext8s):
776 if ((temps[args[1]].mask & 0x80) != 0) {
777 break;
778 }
779 CASE_OP_32_64(ext8u):
780 mask = 0xff;
781 goto and_const;
782 CASE_OP_32_64(ext16s):
783 if ((temps[args[1]].mask & 0x8000) != 0) {
784 break;
785 }
786 CASE_OP_32_64(ext16u):
787 mask = 0xffff;
788 goto and_const;
789 case INDEX_op_ext32s_i64:
790 if ((temps[args[1]].mask & 0x80000000) != 0) {
791 break;
792 }
793 case INDEX_op_ext32u_i64:
794 mask = 0xffffffffU;
795 goto and_const;
796
797 CASE_OP_32_64(and):
798 mask = temps[args[2]].mask;
799 if (temps[args[2]].state == TCG_TEMP_CONST) {
800 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800801 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800802 }
803 mask = temps[args[1]].mask & mask;
804 break;
805
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800806 CASE_OP_32_64(andc):
807 /* Known-zeros does not imply known-ones. Therefore unless
808 args[2] is constant, we can't infer anything from it. */
809 if (temps[args[2]].state == TCG_TEMP_CONST) {
810 mask = ~temps[args[2]].mask;
811 goto and_const;
812 }
813 /* But we certainly know nothing outside args[1] may be set. */
814 mask = temps[args[1]].mask;
815 break;
816
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200817 case INDEX_op_sar_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800818 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700819 tmp = temps[args[2]].val & 31;
820 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200821 }
822 break;
823 case INDEX_op_sar_i64:
824 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700825 tmp = temps[args[2]].val & 63;
826 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800827 }
828 break;
829
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200830 case INDEX_op_shr_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800831 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700832 tmp = temps[args[2]].val & 31;
833 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200834 }
835 break;
836 case INDEX_op_shr_i64:
837 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700838 tmp = temps[args[2]].val & 63;
839 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800840 }
841 break;
842
Richard Henderson4bb7a412013-09-09 17:03:24 -0700843 case INDEX_op_trunc_shr_i32:
844 mask = (uint64_t)temps[args[1]].mask >> args[2];
845 break;
846
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800847 CASE_OP_32_64(shl):
848 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700849 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
850 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800851 }
852 break;
853
854 CASE_OP_32_64(neg):
855 /* Set to 1 all bits to the left of the rightmost. */
856 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
857 break;
858
859 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700860 mask = deposit64(temps[args[1]].mask, args[3], args[4],
861 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800862 break;
863
864 CASE_OP_32_64(or):
865 CASE_OP_32_64(xor):
866 mask = temps[args[1]].mask | temps[args[2]].mask;
867 break;
868
869 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700870 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800871 mask = 1;
872 break;
873
874 CASE_OP_32_64(movcond):
875 mask = temps[args[3]].mask | temps[args[4]].mask;
876 break;
877
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200878 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200879 mask = 0xff;
880 break;
881 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200882 mask = 0xffff;
883 break;
884 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200885 mask = 0xffffffffu;
886 break;
887
888 CASE_OP_32_64(qemu_ld):
889 {
Richard Hendersoncf066672014-03-22 20:06:52 -0700890 TCGMemOp mop = args[nb_oargs + nb_iargs];
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200891 if (!(mop & MO_SIGN)) {
892 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
893 }
894 }
895 break;
896
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800897 default:
898 break;
899 }
900
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700901 /* 32-bit ops generate 32-bit results. For the result is zero test
902 below, we can ignore high bits, but for further optimizations we
903 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700904 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700905 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700906 mask |= ~(tcg_target_ulong)0xffffffffu;
907 partmask &= 0xffffffffu;
908 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200909 }
910
Richard Henderson24666ba2014-05-22 11:14:10 -0700911 if (partmask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700912 assert(nb_oargs == 1);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700913 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800914 continue;
915 }
916 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700917 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800918 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700919 tcg_op_remove(s, op);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800920 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700921 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800922 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700923 tcg_opt_gen_movi(s, op, args, opc,
Richard Hendersona62f6f52014-05-22 10:59:12 -0700924 args[0], temps[args[1]].val);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800925 }
Paolo Bonzini633f6502013-01-11 15:42:53 -0800926 continue;
927 }
928
Aurelien Jarno56e49432012-09-06 16:47:13 +0200929 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700930 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200931 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400932 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700933 CASE_OP_32_64(muluh):
934 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400935 if ((temps[args[2]].state == TCG_TEMP_CONST
936 && temps[args[2]].val == 0)) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700937 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400938 continue;
939 }
940 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200941 default:
942 break;
943 }
944
945 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700946 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400947 CASE_OP_32_64(or):
948 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200949 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200950 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700951 tcg_op_remove(s, op);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400952 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700953 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400954 }
955 continue;
956 }
957 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000958 default:
959 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400960 }
961
Aurelien Jarno3c941932012-09-18 19:12:36 +0200962 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700963 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800964 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +0200965 CASE_OP_32_64(sub):
966 CASE_OP_32_64(xor):
967 if (temps_are_copies(args[1], args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700968 tcg_opt_gen_movi(s, op, args, opc, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200969 continue;
970 }
971 break;
972 default:
973 break;
974 }
975
Kirill Batuzov22613af2011-07-07 16:37:13 +0400976 /* Propagate constants through copy operations and do constant
977 folding. Constants will be substituted to arguments by register
978 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700979 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400980 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200981 if (temps_are_copies(args[0], args[1])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -0700982 tcg_op_remove(s, op);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400983 break;
984 }
985 if (temps[args[1]].state != TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700986 tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400987 break;
988 }
989 /* Source argument is constant. Rewrite the operation and
990 let movi case handle it. */
Kirill Batuzov22613af2011-07-07 16:37:13 +0400991 args[1] = temps[args[1]].val;
992 /* fallthrough */
993 CASE_OP_32_64(movi):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700994 tcg_opt_gen_movi(s, op, args, opc, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400995 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700996
Kirill Batuzova640f032011-07-07 16:37:17 +0400997 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700998 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700999 CASE_OP_32_64(ext8s):
1000 CASE_OP_32_64(ext8u):
1001 CASE_OP_32_64(ext16s):
1002 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001003 case INDEX_op_ext32s_i64:
1004 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +04001005 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001006 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
1007 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001008 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001009 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001010 goto do_default;
1011
Richard Henderson4bb7a412013-09-09 17:03:24 -07001012 case INDEX_op_trunc_shr_i32:
1013 if (temps[args[1]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001014 tmp = do_constant_folding(opc, temps[args[1]].val, args[2]);
1015 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson4bb7a412013-09-09 17:03:24 -07001016 break;
1017 }
1018 goto do_default;
1019
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001020 CASE_OP_32_64(add):
1021 CASE_OP_32_64(sub):
1022 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001023 CASE_OP_32_64(or):
1024 CASE_OP_32_64(and):
1025 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001026 CASE_OP_32_64(shl):
1027 CASE_OP_32_64(shr):
1028 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001029 CASE_OP_32_64(rotl):
1030 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001031 CASE_OP_32_64(andc):
1032 CASE_OP_32_64(orc):
1033 CASE_OP_32_64(eqv):
1034 CASE_OP_32_64(nand):
1035 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001036 CASE_OP_32_64(muluh):
1037 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001038 CASE_OP_32_64(div):
1039 CASE_OP_32_64(divu):
1040 CASE_OP_32_64(rem):
1041 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001042 if (temps[args[1]].state == TCG_TEMP_CONST
1043 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001044 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001045 temps[args[2]].val);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001046 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001047 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001048 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001049 goto do_default;
1050
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001051 CASE_OP_32_64(deposit):
1052 if (temps[args[1]].state == TCG_TEMP_CONST
1053 && temps[args[2]].state == TCG_TEMP_CONST) {
Richard Hendersond998e552014-03-18 14:23:52 -07001054 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1055 temps[args[2]].val);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001056 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001057 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001058 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001059 goto do_default;
1060
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001061 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001062 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001063 if (tmp != 2) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001064 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001065 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001066 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001067 goto do_default;
1068
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001069 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001070 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001071 if (tmp != 2) {
1072 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001073 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001074 op->opc = INDEX_op_br;
1075 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001076 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001077 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001078 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001079 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001080 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001081 goto do_default;
1082
Richard Hendersonfa01a202012-09-21 10:13:37 -07001083 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001084 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001085 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001086 if (temps_are_copies(args[0], args[4-tmp])) {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001087 tcg_op_remove(s, op);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001088 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001089 tcg_opt_gen_movi(s, op, args, opc,
Richard Hendersona62f6f52014-05-22 10:59:12 -07001090 args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001091 } else {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001092 tcg_opt_gen_mov(s, op, args, opc, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001093 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001094 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001095 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001096 goto do_default;
1097
Richard Henderson212c3282012-10-02 11:32:28 -07001098 case INDEX_op_add2_i32:
1099 case INDEX_op_sub2_i32:
1100 if (temps[args[2]].state == TCG_TEMP_CONST
1101 && temps[args[3]].state == TCG_TEMP_CONST
1102 && temps[args[4]].state == TCG_TEMP_CONST
1103 && temps[args[5]].state == TCG_TEMP_CONST) {
1104 uint32_t al = temps[args[2]].val;
1105 uint32_t ah = temps[args[3]].val;
1106 uint32_t bl = temps[args[4]].val;
1107 uint32_t bh = temps[args[5]].val;
1108 uint64_t a = ((uint64_t)ah << 32) | al;
1109 uint64_t b = ((uint64_t)bh << 32) | bl;
1110 TCGArg rl, rh;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001111 TCGOp *op2;
1112 TCGArg *args2;
Richard Henderson212c3282012-10-02 11:32:28 -07001113
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001114 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001115 a += b;
1116 } else {
1117 a -= b;
1118 }
1119
1120 /* We emit the extra nop when we emit the add2/sub2. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001121 op2 = &s->gen_op_buf[oi_next];
1122 assert(op2->opc == INDEX_op_nop);
1123
1124 /* But we still have to allocate args for the op. */
1125 op2->args = s->gen_next_parm_idx;
1126 s->gen_next_parm_idx += 2;
1127 args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001128
1129 rl = args[0];
1130 rh = args[1];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001131 tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)a);
1132 tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(a >> 32));
1133
1134 /* We've done all we need to do with the movi. Skip it. */
1135 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001136 break;
1137 }
1138 goto do_default;
1139
Richard Henderson14149682012-10-02 11:32:30 -07001140 case INDEX_op_mulu2_i32:
1141 if (temps[args[2]].state == TCG_TEMP_CONST
1142 && temps[args[3]].state == TCG_TEMP_CONST) {
1143 uint32_t a = temps[args[2]].val;
1144 uint32_t b = temps[args[3]].val;
1145 uint64_t r = (uint64_t)a * b;
1146 TCGArg rl, rh;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001147 TCGOp *op2;
1148 TCGArg *args2;
Richard Henderson14149682012-10-02 11:32:30 -07001149
1150 /* We emit the extra nop when we emit the mulu2. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001151 op2 = &s->gen_op_buf[oi_next];
1152 assert(op2->opc == INDEX_op_nop);
1153
1154 /* But we still have to allocate args for the op. */
1155 op2->args = s->gen_next_parm_idx;
1156 s->gen_next_parm_idx += 2;
1157 args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001158
1159 rl = args[0];
1160 rh = args[1];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001161 tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)r);
1162 tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(r >> 32));
1163
1164 /* We've done all we need to do with the movi. Skip it. */
1165 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001166 break;
1167 }
1168 goto do_default;
1169
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001170 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001171 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1172 if (tmp != 2) {
1173 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001174 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001175 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001176 op->opc = INDEX_op_br;
1177 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001178 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001179 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001180 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001181 }
1182 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1183 && temps[args[2]].state == TCG_TEMP_CONST
1184 && temps[args[3]].state == TCG_TEMP_CONST
1185 && temps[args[2]].val == 0
1186 && temps[args[3]].val == 0) {
1187 /* Simplify LT/GE comparisons vs zero to a single compare
1188 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001189 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001190 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001191 op->opc = INDEX_op_brcond_i32;
1192 args[0] = args[1];
1193 args[1] = args[3];
1194 args[2] = args[4];
1195 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001196 } else if (args[4] == TCG_COND_EQ) {
1197 /* Simplify EQ comparisons where one of the pairs
1198 can be simplified. */
1199 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1200 args[0], args[2], TCG_COND_EQ);
1201 if (tmp == 0) {
1202 goto do_brcond_false;
1203 } else if (tmp == 1) {
1204 goto do_brcond_high;
1205 }
1206 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1207 args[1], args[3], TCG_COND_EQ);
1208 if (tmp == 0) {
1209 goto do_brcond_false;
1210 } else if (tmp != 1) {
1211 goto do_default;
1212 }
1213 do_brcond_low:
1214 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001215 op->opc = INDEX_op_brcond_i32;
1216 args[1] = args[2];
1217 args[2] = args[4];
1218 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001219 } else if (args[4] == TCG_COND_NE) {
1220 /* Simplify NE comparisons where one of the pairs
1221 can be simplified. */
1222 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1223 args[0], args[2], TCG_COND_NE);
1224 if (tmp == 0) {
1225 goto do_brcond_high;
1226 } else if (tmp == 1) {
1227 goto do_brcond_true;
1228 }
1229 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1230 args[1], args[3], TCG_COND_NE);
1231 if (tmp == 0) {
1232 goto do_brcond_low;
1233 } else if (tmp == 1) {
1234 goto do_brcond_true;
1235 }
1236 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001237 } else {
1238 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001239 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001240 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001241
1242 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001243 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1244 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001245 do_setcond_const:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001246 tcg_opt_gen_movi(s, op, args, opc, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001247 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1248 && temps[args[3]].state == TCG_TEMP_CONST
1249 && temps[args[4]].state == TCG_TEMP_CONST
1250 && temps[args[3]].val == 0
1251 && temps[args[4]].val == 0) {
1252 /* Simplify LT/GE comparisons vs zero to a single compare
1253 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001254 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001255 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001256 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001257 op->opc = INDEX_op_setcond_i32;
1258 args[1] = args[2];
1259 args[2] = args[4];
1260 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001261 } else if (args[5] == TCG_COND_EQ) {
1262 /* Simplify EQ comparisons where one of the pairs
1263 can be simplified. */
1264 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1265 args[1], args[3], TCG_COND_EQ);
1266 if (tmp == 0) {
1267 goto do_setcond_const;
1268 } else if (tmp == 1) {
1269 goto do_setcond_high;
1270 }
1271 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1272 args[2], args[4], TCG_COND_EQ);
1273 if (tmp == 0) {
1274 goto do_setcond_high;
1275 } else if (tmp != 1) {
1276 goto do_default;
1277 }
1278 do_setcond_low:
1279 reset_temp(args[0]);
1280 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001281 op->opc = INDEX_op_setcond_i32;
1282 args[2] = args[3];
1283 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001284 } else if (args[5] == TCG_COND_NE) {
1285 /* Simplify NE comparisons where one of the pairs
1286 can be simplified. */
1287 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1288 args[1], args[3], TCG_COND_NE);
1289 if (tmp == 0) {
1290 goto do_setcond_high;
1291 } else if (tmp == 1) {
1292 goto do_setcond_const;
1293 }
1294 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1295 args[2], args[4], TCG_COND_NE);
1296 if (tmp == 0) {
1297 goto do_setcond_low;
1298 } else if (tmp == 1) {
1299 goto do_setcond_const;
1300 }
1301 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001302 } else {
1303 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001304 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001305 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001306
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001307 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001308 if (!(args[nb_oargs + nb_iargs + 1]
1309 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001310 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001311 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001312 }
1313 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001314 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001315
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001316 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001317 do_default:
1318 /* Default case: we know nothing about operation (or were unable
1319 to compute the operation result) so no propagation is done.
1320 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001321 block, otherwise we only trash the output args. "mask" is
1322 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001323 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001324 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001325 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001326 do_reset_output:
1327 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001328 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001329 /* Save the corresponding known-zero bits mask for the
1330 first output argument (only one supported so far). */
1331 if (i == 0) {
1332 temps[args[i]].mask = mask;
1333 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001334 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001335 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001336 break;
1337 }
1338 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001339}
1340
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001341void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001342{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001343 tcg_constant_folding(s);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001344}