blob: e8dedf3eb410b1e1dc01c2e92b15b9afcc241ad3 [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
Blue Swirlfe0de7a2011-07-30 19:18:32 +000086static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040087{
88 switch (op_bits(op)) {
89 case 32:
90 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040091 case 64:
92 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +040093 default:
94 fprintf(stderr, "op_to_movi: unexpected return value of "
95 "function op_bits.\n");
96 tcg_abort();
97 }
98}
99
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200100static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
101{
102 TCGArg i;
103
104 /* If this is already a global, we can't do better. */
105 if (temp < s->nb_globals) {
106 return temp;
107 }
108
109 /* Search for a global first. */
110 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
111 if (i < s->nb_globals) {
112 return i;
113 }
114 }
115
116 /* If it is a temp, search for a temp local. */
117 if (!s->temps[temp].temp_local) {
118 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
119 if (s->temps[i].temp_local) {
120 return i;
121 }
122 }
123 }
124
125 /* Failure to find a better representation, return the same temp. */
126 return temp;
127}
128
129static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
130{
131 TCGArg i;
132
133 if (arg1 == arg2) {
134 return true;
135 }
136
137 if (temps[arg1].state != TCG_TEMP_COPY
138 || temps[arg2].state != TCG_TEMP_COPY) {
139 return false;
140 }
141
142 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
143 if (i == arg2) {
144 return true;
145 }
146 }
147
148 return false;
149}
150
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200151static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
152 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400153{
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800154 reset_temp(dst);
155 temps[dst].mask = temps[src].mask;
156 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200157
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800158 if (s->temps[src].type == s->temps[dst].type) {
159 if (temps[src].state != TCG_TEMP_COPY) {
160 temps[src].state = TCG_TEMP_COPY;
161 temps[src].next_copy = src;
162 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400163 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800164 temps[dst].state = TCG_TEMP_COPY;
165 temps[dst].next_copy = temps[src].next_copy;
166 temps[dst].prev_copy = src;
167 temps[temps[dst].next_copy].prev_copy = dst;
168 temps[src].next_copy = dst;
169 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200170
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800171 gen_args[0] = dst;
172 gen_args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400173}
174
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200175static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400176{
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800177 reset_temp(dst);
178 temps[dst].state = TCG_TEMP_CONST;
179 temps[dst].val = val;
180 temps[dst].mask = val;
181 gen_args[0] = dst;
182 gen_args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400183}
184
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000185static TCGOpcode op_to_mov(TCGOpcode op)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400186{
187 switch (op_bits(op)) {
188 case 32:
189 return INDEX_op_mov_i32;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400190 case 64:
191 return INDEX_op_mov_i64;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400192 default:
193 fprintf(stderr, "op_to_mov: unexpected return value of "
194 "function op_bits.\n");
195 tcg_abort();
196 }
197}
198
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000199static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400200{
Richard Henderson03271522013-08-14 14:35:56 -0700201 uint64_t l64, h64;
202
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400203 switch (op) {
204 CASE_OP_32_64(add):
205 return x + y;
206
207 CASE_OP_32_64(sub):
208 return x - y;
209
210 CASE_OP_32_64(mul):
211 return x * y;
212
Kirill Batuzov9a810902011-07-07 16:37:15 +0400213 CASE_OP_32_64(and):
214 return x & y;
215
216 CASE_OP_32_64(or):
217 return x | y;
218
219 CASE_OP_32_64(xor):
220 return x ^ y;
221
Kirill Batuzov55c09752011-07-07 16:37:16 +0400222 case INDEX_op_shl_i32:
223 return (uint32_t)x << (uint32_t)y;
224
Kirill Batuzov55c09752011-07-07 16:37:16 +0400225 case INDEX_op_shl_i64:
226 return (uint64_t)x << (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400227
228 case INDEX_op_shr_i32:
229 return (uint32_t)x >> (uint32_t)y;
230
Kirill Batuzov55c09752011-07-07 16:37:16 +0400231 case INDEX_op_shr_i64:
232 return (uint64_t)x >> (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400233
234 case INDEX_op_sar_i32:
235 return (int32_t)x >> (int32_t)y;
236
Kirill Batuzov55c09752011-07-07 16:37:16 +0400237 case INDEX_op_sar_i64:
238 return (int64_t)x >> (int64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400239
240 case INDEX_op_rotr_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700241 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400242 return x;
243
Kirill Batuzov55c09752011-07-07 16:37:16 +0400244 case INDEX_op_rotr_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700245 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400246 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400247
248 case INDEX_op_rotl_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700249 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400250 return x;
251
Kirill Batuzov55c09752011-07-07 16:37:16 +0400252 case INDEX_op_rotl_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700253 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400254 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400255
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700256 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400257 return ~x;
258
Richard Hendersoncb25c802011-08-17 14:11:47 -0700259 CASE_OP_32_64(neg):
260 return -x;
261
262 CASE_OP_32_64(andc):
263 return x & ~y;
264
265 CASE_OP_32_64(orc):
266 return x | ~y;
267
268 CASE_OP_32_64(eqv):
269 return ~(x ^ y);
270
271 CASE_OP_32_64(nand):
272 return ~(x & y);
273
274 CASE_OP_32_64(nor):
275 return ~(x | y);
276
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700277 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400278 return (int8_t)x;
279
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700280 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400281 return (int16_t)x;
282
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700283 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400284 return (uint8_t)x;
285
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700286 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400287 return (uint16_t)x;
288
Kirill Batuzova640f032011-07-07 16:37:17 +0400289 case INDEX_op_ext32s_i64:
290 return (int32_t)x;
291
292 case INDEX_op_ext32u_i64:
293 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400294
Richard Henderson03271522013-08-14 14:35:56 -0700295 case INDEX_op_muluh_i32:
296 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
297 case INDEX_op_mulsh_i32:
298 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
299
300 case INDEX_op_muluh_i64:
301 mulu64(&l64, &h64, x, y);
302 return h64;
303 case INDEX_op_mulsh_i64:
304 muls64(&l64, &h64, x, y);
305 return h64;
306
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400307 default:
308 fprintf(stderr,
309 "Unrecognized operation %d in do_constant_folding.\n", op);
310 tcg_abort();
311 }
312}
313
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000314static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400315{
316 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400317 if (op_bits(op) == 32) {
318 res &= 0xffffffff;
319 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400320 return res;
321}
322
Richard Henderson9519da72012-10-02 11:32:26 -0700323static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
324{
325 switch (c) {
326 case TCG_COND_EQ:
327 return x == y;
328 case TCG_COND_NE:
329 return x != y;
330 case TCG_COND_LT:
331 return (int32_t)x < (int32_t)y;
332 case TCG_COND_GE:
333 return (int32_t)x >= (int32_t)y;
334 case TCG_COND_LE:
335 return (int32_t)x <= (int32_t)y;
336 case TCG_COND_GT:
337 return (int32_t)x > (int32_t)y;
338 case TCG_COND_LTU:
339 return x < y;
340 case TCG_COND_GEU:
341 return x >= y;
342 case TCG_COND_LEU:
343 return x <= y;
344 case TCG_COND_GTU:
345 return x > y;
346 default:
347 tcg_abort();
348 }
349}
350
351static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
352{
353 switch (c) {
354 case TCG_COND_EQ:
355 return x == y;
356 case TCG_COND_NE:
357 return x != y;
358 case TCG_COND_LT:
359 return (int64_t)x < (int64_t)y;
360 case TCG_COND_GE:
361 return (int64_t)x >= (int64_t)y;
362 case TCG_COND_LE:
363 return (int64_t)x <= (int64_t)y;
364 case TCG_COND_GT:
365 return (int64_t)x > (int64_t)y;
366 case TCG_COND_LTU:
367 return x < y;
368 case TCG_COND_GEU:
369 return x >= y;
370 case TCG_COND_LEU:
371 return x <= y;
372 case TCG_COND_GTU:
373 return x > y;
374 default:
375 tcg_abort();
376 }
377}
378
379static bool do_constant_folding_cond_eq(TCGCond c)
380{
381 switch (c) {
382 case TCG_COND_GT:
383 case TCG_COND_LTU:
384 case TCG_COND_LT:
385 case TCG_COND_GTU:
386 case TCG_COND_NE:
387 return 0;
388 case TCG_COND_GE:
389 case TCG_COND_GEU:
390 case TCG_COND_LE:
391 case TCG_COND_LEU:
392 case TCG_COND_EQ:
393 return 1;
394 default:
395 tcg_abort();
396 }
397}
398
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200399/* Return 2 if the condition can't be simplified, and the result
400 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200401static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
402 TCGArg y, TCGCond c)
403{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200404 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
405 switch (op_bits(op)) {
406 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700407 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200408 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700409 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
410 default:
411 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200412 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200413 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700414 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200415 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
416 switch (c) {
417 case TCG_COND_LTU:
418 return 0;
419 case TCG_COND_GEU:
420 return 1;
421 default:
422 return 2;
423 }
424 } else {
425 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200426 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200427}
428
Richard Henderson6c4382f2012-10-02 11:32:27 -0700429/* Return 2 if the condition can't be simplified, and the result
430 of the condition (0 or 1) if it can */
431static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
432{
433 TCGArg al = p1[0], ah = p1[1];
434 TCGArg bl = p2[0], bh = p2[1];
435
436 if (temps[bl].state == TCG_TEMP_CONST
437 && temps[bh].state == TCG_TEMP_CONST) {
438 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
439
440 if (temps[al].state == TCG_TEMP_CONST
441 && temps[ah].state == TCG_TEMP_CONST) {
442 uint64_t a;
443 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
444 return do_constant_folding_cond_64(a, b, c);
445 }
446 if (b == 0) {
447 switch (c) {
448 case TCG_COND_LTU:
449 return 0;
450 case TCG_COND_GEU:
451 return 1;
452 default:
453 break;
454 }
455 }
456 }
457 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
458 return do_constant_folding_cond_eq(c);
459 }
460 return 2;
461}
462
Richard Henderson24c9ae42012-10-02 11:32:21 -0700463static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
464{
465 TCGArg a1 = *p1, a2 = *p2;
466 int sum = 0;
467 sum += temps[a1].state == TCG_TEMP_CONST;
468 sum -= temps[a2].state == TCG_TEMP_CONST;
469
470 /* Prefer the constant in second argument, and then the form
471 op a, a, b, which is better handled on non-RISC hosts. */
472 if (sum > 0 || (sum == 0 && dest == a2)) {
473 *p1 = a2;
474 *p2 = a1;
475 return true;
476 }
477 return false;
478}
479
Richard Henderson0bfcb862012-10-02 11:32:23 -0700480static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
481{
482 int sum = 0;
483 sum += temps[p1[0]].state == TCG_TEMP_CONST;
484 sum += temps[p1[1]].state == TCG_TEMP_CONST;
485 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
486 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
487 if (sum > 0) {
488 TCGArg t;
489 t = p1[0], p1[0] = p2[0], p2[0] = t;
490 t = p1[1], p1[1] = p2[1], p2[1] = t;
491 return true;
492 }
493 return false;
494}
495
Kirill Batuzov22613af2011-07-07 16:37:13 +0400496/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400497static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
498 TCGArg *args, TCGOpDef *tcg_op_defs)
499{
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000500 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800501 tcg_target_ulong mask, affected;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000502 TCGOpcode op;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400503 const TCGOpDef *def;
504 TCGArg *gen_args;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400505 TCGArg tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700506
Kirill Batuzov22613af2011-07-07 16:37:13 +0400507 /* Array VALS has an element for each temp.
508 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200509 If this temp is a copy of other ones then the other copies are
510 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400511
512 nb_temps = s->nb_temps;
513 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800514 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400515
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400516 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400517 gen_args = args;
518 for (op_index = 0; op_index < nb_ops; op_index++) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400519 op = s->gen_opc_buf[op_index];
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400520 def = &tcg_op_defs[op];
Kirill Batuzov22613af2011-07-07 16:37:13 +0400521 /* Do copy propagation */
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200522 if (op == INDEX_op_call) {
523 int nb_oargs = args[0] >> 16;
524 int nb_iargs = args[0] & 0xffff;
525 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
526 if (temps[args[i]].state == TCG_TEMP_COPY) {
527 args[i] = find_better_copy(s, args[i]);
528 }
529 }
530 } else {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400531 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
532 if (temps[args[i]].state == TCG_TEMP_COPY) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200533 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400534 }
535 }
536 }
537
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400538 /* For commutative operations make constant second argument */
539 switch (op) {
540 CASE_OP_32_64(add):
541 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400542 CASE_OP_32_64(and):
543 CASE_OP_32_64(or):
544 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700545 CASE_OP_32_64(eqv):
546 CASE_OP_32_64(nand):
547 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700548 CASE_OP_32_64(muluh):
549 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700550 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400551 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200552 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700553 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200554 args[2] = tcg_swap_cond(args[2]);
555 }
556 break;
557 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700558 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200559 args[3] = tcg_swap_cond(args[3]);
560 }
561 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700562 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700563 if (swap_commutative(-1, &args[1], &args[2])) {
564 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700565 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700566 /* For movcond, we canonicalize the "false" input reg to match
567 the destination reg so that the tcg backend can implement
568 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700569 if (swap_commutative(args[0], &args[4], &args[3])) {
570 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700571 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700572 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800573 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700574 swap_commutative(args[0], &args[2], &args[4]);
575 swap_commutative(args[1], &args[3], &args[5]);
576 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800577 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800578 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700579 swap_commutative(args[0], &args[2], &args[3]);
580 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700581 case INDEX_op_brcond2_i32:
582 if (swap_commutative2(&args[0], &args[2])) {
583 args[4] = tcg_swap_cond(args[4]);
584 }
585 break;
586 case INDEX_op_setcond2_i32:
587 if (swap_commutative2(&args[1], &args[3])) {
588 args[5] = tcg_swap_cond(args[5]);
589 }
590 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400591 default:
592 break;
593 }
594
Richard Henderson2d497542013-03-21 09:13:33 -0700595 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
596 and "sub r, 0, a => neg r, a" case. */
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200597 switch (op) {
598 CASE_OP_32_64(shl):
599 CASE_OP_32_64(shr):
600 CASE_OP_32_64(sar):
601 CASE_OP_32_64(rotl):
602 CASE_OP_32_64(rotr):
603 if (temps[args[1]].state == TCG_TEMP_CONST
604 && temps[args[1]].val == 0) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400605 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200606 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200607 args += 3;
608 gen_args += 2;
609 continue;
610 }
611 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700612 CASE_OP_32_64(sub):
613 {
614 TCGOpcode neg_op;
615 bool have_neg;
616
617 if (temps[args[2]].state == TCG_TEMP_CONST) {
618 /* Proceed with possible constant folding. */
619 break;
620 }
621 if (op == INDEX_op_sub_i32) {
622 neg_op = INDEX_op_neg_i32;
623 have_neg = TCG_TARGET_HAS_neg_i32;
624 } else {
625 neg_op = INDEX_op_neg_i64;
626 have_neg = TCG_TARGET_HAS_neg_i64;
627 }
628 if (!have_neg) {
629 break;
630 }
631 if (temps[args[1]].state == TCG_TEMP_CONST
632 && temps[args[1]].val == 0) {
633 s->gen_opc_buf[op_index] = neg_op;
634 reset_temp(args[0]);
635 gen_args[0] = args[0];
636 gen_args[1] = args[2];
637 args += 3;
638 gen_args += 2;
639 continue;
640 }
641 }
642 break;
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200643 default:
644 break;
645 }
646
Aurelien Jarno56e49432012-09-06 16:47:13 +0200647 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400648 switch (op) {
649 CASE_OP_32_64(add):
650 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400651 CASE_OP_32_64(shl):
652 CASE_OP_32_64(shr):
653 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700654 CASE_OP_32_64(rotl):
655 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200656 CASE_OP_32_64(or):
657 CASE_OP_32_64(xor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400658 if (temps[args[1]].state == TCG_TEMP_CONST) {
659 /* Proceed with possible constant folding. */
660 break;
661 }
662 if (temps[args[2]].state == TCG_TEMP_CONST
663 && temps[args[2]].val == 0) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200664 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400665 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400666 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400667 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200668 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400669 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400670 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200671 args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400672 continue;
673 }
674 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200675 default:
676 break;
677 }
678
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800679 /* Simplify using known-zero bits */
680 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800681 affected = -1;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800682 switch (op) {
683 CASE_OP_32_64(ext8s):
684 if ((temps[args[1]].mask & 0x80) != 0) {
685 break;
686 }
687 CASE_OP_32_64(ext8u):
688 mask = 0xff;
689 goto and_const;
690 CASE_OP_32_64(ext16s):
691 if ((temps[args[1]].mask & 0x8000) != 0) {
692 break;
693 }
694 CASE_OP_32_64(ext16u):
695 mask = 0xffff;
696 goto and_const;
697 case INDEX_op_ext32s_i64:
698 if ((temps[args[1]].mask & 0x80000000) != 0) {
699 break;
700 }
701 case INDEX_op_ext32u_i64:
702 mask = 0xffffffffU;
703 goto and_const;
704
705 CASE_OP_32_64(and):
706 mask = temps[args[2]].mask;
707 if (temps[args[2]].state == TCG_TEMP_CONST) {
708 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800709 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800710 }
711 mask = temps[args[1]].mask & mask;
712 break;
713
714 CASE_OP_32_64(sar):
715 if (temps[args[2]].state == TCG_TEMP_CONST) {
716 mask = ((tcg_target_long)temps[args[1]].mask
717 >> temps[args[2]].val);
718 }
719 break;
720
721 CASE_OP_32_64(shr):
722 if (temps[args[2]].state == TCG_TEMP_CONST) {
723 mask = temps[args[1]].mask >> temps[args[2]].val;
724 }
725 break;
726
727 CASE_OP_32_64(shl):
728 if (temps[args[2]].state == TCG_TEMP_CONST) {
729 mask = temps[args[1]].mask << temps[args[2]].val;
730 }
731 break;
732
733 CASE_OP_32_64(neg):
734 /* Set to 1 all bits to the left of the rightmost. */
735 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
736 break;
737
738 CASE_OP_32_64(deposit):
739 tmp = ((1ull << args[4]) - 1);
740 mask = ((temps[args[1]].mask & ~(tmp << args[3]))
741 | ((temps[args[2]].mask & tmp) << args[3]));
742 break;
743
744 CASE_OP_32_64(or):
745 CASE_OP_32_64(xor):
746 mask = temps[args[1]].mask | temps[args[2]].mask;
747 break;
748
749 CASE_OP_32_64(setcond):
750 mask = 1;
751 break;
752
753 CASE_OP_32_64(movcond):
754 mask = temps[args[3]].mask | temps[args[4]].mask;
755 break;
756
757 default:
758 break;
759 }
760
Paolo Bonzini633f6502013-01-11 15:42:53 -0800761 if (mask == 0) {
762 assert(def->nb_oargs == 1);
763 s->gen_opc_buf[op_index] = op_to_movi(op);
764 tcg_opt_gen_movi(gen_args, args[0], 0);
765 args += def->nb_oargs + def->nb_iargs + def->nb_cargs;
766 gen_args += 2;
767 continue;
768 }
769 if (affected == 0) {
770 assert(def->nb_oargs == 1);
771 if (temps_are_copies(args[0], args[1])) {
772 s->gen_opc_buf[op_index] = INDEX_op_nop;
773 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
774 s->gen_opc_buf[op_index] = op_to_mov(op);
775 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
776 gen_args += 2;
777 } else {
778 s->gen_opc_buf[op_index] = op_to_movi(op);
779 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
780 gen_args += 2;
781 }
782 args += def->nb_iargs + 1;
783 continue;
784 }
785
Aurelien Jarno56e49432012-09-06 16:47:13 +0200786 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
787 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200788 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400789 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700790 CASE_OP_32_64(muluh):
791 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400792 if ((temps[args[2]].state == TCG_TEMP_CONST
793 && temps[args[2]].val == 0)) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400794 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200795 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400796 args += 3;
797 gen_args += 2;
798 continue;
799 }
800 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200801 default:
802 break;
803 }
804
805 /* Simplify expression for "op r, a, a => mov r, a" cases */
806 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400807 CASE_OP_32_64(or):
808 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200809 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200810 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400811 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400812 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400813 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200814 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400815 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400816 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200817 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400818 continue;
819 }
820 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000821 default:
822 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400823 }
824
Aurelien Jarno3c941932012-09-18 19:12:36 +0200825 /* Simplify expression for "op r, a, a => movi r, 0" cases */
826 switch (op) {
827 CASE_OP_32_64(sub):
828 CASE_OP_32_64(xor):
829 if (temps_are_copies(args[1], args[2])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400830 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200831 tcg_opt_gen_movi(gen_args, args[0], 0);
832 gen_args += 2;
833 args += 3;
834 continue;
835 }
836 break;
837 default:
838 break;
839 }
840
Kirill Batuzov22613af2011-07-07 16:37:13 +0400841 /* Propagate constants through copy operations and do constant
842 folding. Constants will be substituted to arguments by register
843 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400844 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400845 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200846 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400847 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400848 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400849 break;
850 }
851 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200852 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400853 gen_args += 2;
854 args += 2;
855 break;
856 }
857 /* Source argument is constant. Rewrite the operation and
858 let movi case handle it. */
859 op = op_to_movi(op);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400860 s->gen_opc_buf[op_index] = op;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400861 args[1] = temps[args[1]].val;
862 /* fallthrough */
863 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200864 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400865 gen_args += 2;
866 args += 2;
867 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700868
Kirill Batuzova640f032011-07-07 16:37:17 +0400869 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700870 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700871 CASE_OP_32_64(ext8s):
872 CASE_OP_32_64(ext8u):
873 CASE_OP_32_64(ext16s):
874 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400875 case INDEX_op_ext32s_i64:
876 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400877 if (temps[args[1]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400878 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzova640f032011-07-07 16:37:17 +0400879 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200880 tcg_opt_gen_movi(gen_args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -0700881 gen_args += 2;
882 args += 2;
883 break;
Kirill Batuzova640f032011-07-07 16:37:17 +0400884 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700885 goto do_default;
886
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400887 CASE_OP_32_64(add):
888 CASE_OP_32_64(sub):
889 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400890 CASE_OP_32_64(or):
891 CASE_OP_32_64(and):
892 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400893 CASE_OP_32_64(shl):
894 CASE_OP_32_64(shr):
895 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700896 CASE_OP_32_64(rotl):
897 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700898 CASE_OP_32_64(andc):
899 CASE_OP_32_64(orc):
900 CASE_OP_32_64(eqv):
901 CASE_OP_32_64(nand):
902 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700903 CASE_OP_32_64(muluh):
904 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400905 if (temps[args[1]].state == TCG_TEMP_CONST
906 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400907 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400908 tmp = do_constant_folding(op, temps[args[1]].val,
909 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200910 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400911 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700912 args += 3;
913 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400914 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700915 goto do_default;
916
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200917 CASE_OP_32_64(deposit):
918 if (temps[args[1]].state == TCG_TEMP_CONST
919 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400920 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200921 tmp = ((1ull << args[4]) - 1);
922 tmp = (temps[args[1]].val & ~(tmp << args[3]))
923 | ((temps[args[2]].val & tmp) << args[3]);
924 tcg_opt_gen_movi(gen_args, args[0], tmp);
925 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700926 args += 5;
927 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200928 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700929 goto do_default;
930
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200931 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200932 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
933 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400934 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200935 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200936 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700937 args += 4;
938 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200939 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700940 goto do_default;
941
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200942 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200943 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
944 if (tmp != 2) {
945 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -0800946 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400947 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200948 gen_args[0] = args[3];
949 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200950 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400951 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200952 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700953 args += 4;
954 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200955 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700956 goto do_default;
957
Richard Hendersonfa01a202012-09-21 10:13:37 -0700958 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200959 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
960 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200961 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400962 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700963 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400964 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200965 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700966 gen_args += 2;
967 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400968 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200969 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700970 gen_args += 2;
971 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700972 args += 6;
973 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700974 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700975 goto do_default;
976
Richard Henderson212c3282012-10-02 11:32:28 -0700977 case INDEX_op_add2_i32:
978 case INDEX_op_sub2_i32:
979 if (temps[args[2]].state == TCG_TEMP_CONST
980 && temps[args[3]].state == TCG_TEMP_CONST
981 && temps[args[4]].state == TCG_TEMP_CONST
982 && temps[args[5]].state == TCG_TEMP_CONST) {
983 uint32_t al = temps[args[2]].val;
984 uint32_t ah = temps[args[3]].val;
985 uint32_t bl = temps[args[4]].val;
986 uint32_t bh = temps[args[5]].val;
987 uint64_t a = ((uint64_t)ah << 32) | al;
988 uint64_t b = ((uint64_t)bh << 32) | bl;
989 TCGArg rl, rh;
990
991 if (op == INDEX_op_add2_i32) {
992 a += b;
993 } else {
994 a -= b;
995 }
996
997 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400998 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -0700999
1000 rl = args[0];
1001 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001002 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1003 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson212c3282012-10-02 11:32:28 -07001004 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
1005 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
1006 gen_args += 4;
1007 args += 6;
1008 break;
1009 }
1010 goto do_default;
1011
Richard Henderson14149682012-10-02 11:32:30 -07001012 case INDEX_op_mulu2_i32:
1013 if (temps[args[2]].state == TCG_TEMP_CONST
1014 && temps[args[3]].state == TCG_TEMP_CONST) {
1015 uint32_t a = temps[args[2]].val;
1016 uint32_t b = temps[args[3]].val;
1017 uint64_t r = (uint64_t)a * b;
1018 TCGArg rl, rh;
1019
1020 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001021 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -07001022
1023 rl = args[0];
1024 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001025 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1026 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson14149682012-10-02 11:32:30 -07001027 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
1028 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
1029 gen_args += 4;
1030 args += 4;
1031 break;
1032 }
1033 goto do_default;
1034
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001035 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001036 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1037 if (tmp != 2) {
1038 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001039 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001040 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001041 gen_args[0] = args[5];
1042 gen_args += 1;
1043 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001044 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001045 }
1046 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1047 && temps[args[2]].state == TCG_TEMP_CONST
1048 && temps[args[3]].state == TCG_TEMP_CONST
1049 && temps[args[2]].val == 0
1050 && temps[args[3]].val == 0) {
1051 /* Simplify LT/GE comparisons vs zero to a single compare
1052 vs the high word of the input. */
Paolo Bonzinid193a142013-01-11 15:42:51 -08001053 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001054 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001055 gen_args[0] = args[1];
1056 gen_args[1] = args[3];
1057 gen_args[2] = args[4];
1058 gen_args[3] = args[5];
1059 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001060 } else {
1061 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001062 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001063 args += 6;
1064 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001065
1066 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001067 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1068 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001069 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001070 tcg_opt_gen_movi(gen_args, args[0], tmp);
1071 gen_args += 2;
1072 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1073 && temps[args[3]].state == TCG_TEMP_CONST
1074 && temps[args[4]].state == TCG_TEMP_CONST
1075 && temps[args[3]].val == 0
1076 && temps[args[4]].val == 0) {
1077 /* Simplify LT/GE comparisons vs zero to a single compare
1078 vs the high word of the input. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001079 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001080 reset_temp(args[0]);
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001081 gen_args[0] = args[0];
1082 gen_args[1] = args[2];
1083 gen_args[2] = args[4];
1084 gen_args[3] = args[5];
1085 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001086 } else {
1087 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001088 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001089 args += 6;
1090 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001091
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001092 case INDEX_op_call:
Kirill Batuzov22613af2011-07-07 16:37:13 +04001093 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
Aurelien Jarno78505272012-10-09 21:53:08 +02001094 if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1095 TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001096 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001097 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001098 }
1099 }
1100 for (i = 0; i < (args[0] >> 16); i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001101 reset_temp(args[i + 1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001102 }
1103 i = nb_call_args + 3;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001104 while (i) {
1105 *gen_args = *args;
1106 args++;
1107 gen_args++;
1108 i--;
1109 }
1110 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001111
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001112 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001113 do_default:
1114 /* Default case: we know nothing about operation (or were unable
1115 to compute the operation result) so no propagation is done.
1116 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001117 block, otherwise we only trash the output args. "mask" is
1118 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001119 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001120 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001121 } else {
1122 for (i = 0; i < def->nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001123 reset_temp(args[i]);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001124 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001125 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001126 for (i = 0; i < def->nb_args; i++) {
1127 gen_args[i] = args[i];
1128 }
1129 args += def->nb_args;
1130 gen_args += def->nb_args;
1131 break;
1132 }
1133 }
1134
1135 return gen_args;
1136}
1137
1138TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1139 TCGArg *args, TCGOpDef *tcg_op_defs)
1140{
1141 TCGArg *res;
1142 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1143 return res;
1144}