blob: b29bf25b67b103cefb66e6d9074c96a5d839332e [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
Richard Henderson01547f72013-08-14 15:22:46 -0700307 case INDEX_op_div_i32:
308 /* Avoid crashing on divide by zero, otherwise undefined. */
309 return (int32_t)x / ((int32_t)y ? : 1);
310 case INDEX_op_divu_i32:
311 return (uint32_t)x / ((uint32_t)y ? : 1);
312 case INDEX_op_div_i64:
313 return (int64_t)x / ((int64_t)y ? : 1);
314 case INDEX_op_divu_i64:
315 return (uint64_t)x / ((uint64_t)y ? : 1);
316
317 case INDEX_op_rem_i32:
318 return (int32_t)x % ((int32_t)y ? : 1);
319 case INDEX_op_remu_i32:
320 return (uint32_t)x % ((uint32_t)y ? : 1);
321 case INDEX_op_rem_i64:
322 return (int64_t)x % ((int64_t)y ? : 1);
323 case INDEX_op_remu_i64:
324 return (uint64_t)x % ((uint64_t)y ? : 1);
325
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400326 default:
327 fprintf(stderr,
328 "Unrecognized operation %d in do_constant_folding.\n", op);
329 tcg_abort();
330 }
331}
332
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000333static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400334{
335 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400336 if (op_bits(op) == 32) {
337 res &= 0xffffffff;
338 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400339 return res;
340}
341
Richard Henderson9519da72012-10-02 11:32:26 -0700342static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
343{
344 switch (c) {
345 case TCG_COND_EQ:
346 return x == y;
347 case TCG_COND_NE:
348 return x != y;
349 case TCG_COND_LT:
350 return (int32_t)x < (int32_t)y;
351 case TCG_COND_GE:
352 return (int32_t)x >= (int32_t)y;
353 case TCG_COND_LE:
354 return (int32_t)x <= (int32_t)y;
355 case TCG_COND_GT:
356 return (int32_t)x > (int32_t)y;
357 case TCG_COND_LTU:
358 return x < y;
359 case TCG_COND_GEU:
360 return x >= y;
361 case TCG_COND_LEU:
362 return x <= y;
363 case TCG_COND_GTU:
364 return x > y;
365 default:
366 tcg_abort();
367 }
368}
369
370static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
371{
372 switch (c) {
373 case TCG_COND_EQ:
374 return x == y;
375 case TCG_COND_NE:
376 return x != y;
377 case TCG_COND_LT:
378 return (int64_t)x < (int64_t)y;
379 case TCG_COND_GE:
380 return (int64_t)x >= (int64_t)y;
381 case TCG_COND_LE:
382 return (int64_t)x <= (int64_t)y;
383 case TCG_COND_GT:
384 return (int64_t)x > (int64_t)y;
385 case TCG_COND_LTU:
386 return x < y;
387 case TCG_COND_GEU:
388 return x >= y;
389 case TCG_COND_LEU:
390 return x <= y;
391 case TCG_COND_GTU:
392 return x > y;
393 default:
394 tcg_abort();
395 }
396}
397
398static bool do_constant_folding_cond_eq(TCGCond c)
399{
400 switch (c) {
401 case TCG_COND_GT:
402 case TCG_COND_LTU:
403 case TCG_COND_LT:
404 case TCG_COND_GTU:
405 case TCG_COND_NE:
406 return 0;
407 case TCG_COND_GE:
408 case TCG_COND_GEU:
409 case TCG_COND_LE:
410 case TCG_COND_LEU:
411 case TCG_COND_EQ:
412 return 1;
413 default:
414 tcg_abort();
415 }
416}
417
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200418/* Return 2 if the condition can't be simplified, and the result
419 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200420static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
421 TCGArg y, TCGCond c)
422{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200423 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
424 switch (op_bits(op)) {
425 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700426 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200427 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700428 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
429 default:
430 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200431 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200432 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700433 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200434 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
435 switch (c) {
436 case TCG_COND_LTU:
437 return 0;
438 case TCG_COND_GEU:
439 return 1;
440 default:
441 return 2;
442 }
443 } else {
444 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200445 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200446}
447
Richard Henderson6c4382f2012-10-02 11:32:27 -0700448/* Return 2 if the condition can't be simplified, and the result
449 of the condition (0 or 1) if it can */
450static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
451{
452 TCGArg al = p1[0], ah = p1[1];
453 TCGArg bl = p2[0], bh = p2[1];
454
455 if (temps[bl].state == TCG_TEMP_CONST
456 && temps[bh].state == TCG_TEMP_CONST) {
457 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
458
459 if (temps[al].state == TCG_TEMP_CONST
460 && temps[ah].state == TCG_TEMP_CONST) {
461 uint64_t a;
462 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
463 return do_constant_folding_cond_64(a, b, c);
464 }
465 if (b == 0) {
466 switch (c) {
467 case TCG_COND_LTU:
468 return 0;
469 case TCG_COND_GEU:
470 return 1;
471 default:
472 break;
473 }
474 }
475 }
476 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
477 return do_constant_folding_cond_eq(c);
478 }
479 return 2;
480}
481
Richard Henderson24c9ae42012-10-02 11:32:21 -0700482static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
483{
484 TCGArg a1 = *p1, a2 = *p2;
485 int sum = 0;
486 sum += temps[a1].state == TCG_TEMP_CONST;
487 sum -= temps[a2].state == TCG_TEMP_CONST;
488
489 /* Prefer the constant in second argument, and then the form
490 op a, a, b, which is better handled on non-RISC hosts. */
491 if (sum > 0 || (sum == 0 && dest == a2)) {
492 *p1 = a2;
493 *p2 = a1;
494 return true;
495 }
496 return false;
497}
498
Richard Henderson0bfcb862012-10-02 11:32:23 -0700499static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
500{
501 int sum = 0;
502 sum += temps[p1[0]].state == TCG_TEMP_CONST;
503 sum += temps[p1[1]].state == TCG_TEMP_CONST;
504 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
505 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
506 if (sum > 0) {
507 TCGArg t;
508 t = p1[0], p1[0] = p2[0], p2[0] = t;
509 t = p1[1], p1[1] = p2[1], p2[1] = t;
510 return true;
511 }
512 return false;
513}
514
Kirill Batuzov22613af2011-07-07 16:37:13 +0400515/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400516static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
517 TCGArg *args, TCGOpDef *tcg_op_defs)
518{
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000519 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800520 tcg_target_ulong mask, affected;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000521 TCGOpcode op;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400522 const TCGOpDef *def;
523 TCGArg *gen_args;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400524 TCGArg tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700525
Kirill Batuzov22613af2011-07-07 16:37:13 +0400526 /* Array VALS has an element for each temp.
527 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200528 If this temp is a copy of other ones then the other copies are
529 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400530
531 nb_temps = s->nb_temps;
532 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800533 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400534
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400535 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400536 gen_args = args;
537 for (op_index = 0; op_index < nb_ops; op_index++) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400538 op = s->gen_opc_buf[op_index];
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400539 def = &tcg_op_defs[op];
Kirill Batuzov22613af2011-07-07 16:37:13 +0400540 /* Do copy propagation */
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200541 if (op == INDEX_op_call) {
542 int nb_oargs = args[0] >> 16;
543 int nb_iargs = args[0] & 0xffff;
544 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
545 if (temps[args[i]].state == TCG_TEMP_COPY) {
546 args[i] = find_better_copy(s, args[i]);
547 }
548 }
549 } else {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400550 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
551 if (temps[args[i]].state == TCG_TEMP_COPY) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200552 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400553 }
554 }
555 }
556
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400557 /* For commutative operations make constant second argument */
558 switch (op) {
559 CASE_OP_32_64(add):
560 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400561 CASE_OP_32_64(and):
562 CASE_OP_32_64(or):
563 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700564 CASE_OP_32_64(eqv):
565 CASE_OP_32_64(nand):
566 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700567 CASE_OP_32_64(muluh):
568 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700569 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400570 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200571 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700572 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200573 args[2] = tcg_swap_cond(args[2]);
574 }
575 break;
576 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700577 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200578 args[3] = tcg_swap_cond(args[3]);
579 }
580 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700581 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700582 if (swap_commutative(-1, &args[1], &args[2])) {
583 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700584 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700585 /* For movcond, we canonicalize the "false" input reg to match
586 the destination reg so that the tcg backend can implement
587 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700588 if (swap_commutative(args[0], &args[4], &args[3])) {
589 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700590 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700591 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800592 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700593 swap_commutative(args[0], &args[2], &args[4]);
594 swap_commutative(args[1], &args[3], &args[5]);
595 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800596 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800597 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700598 swap_commutative(args[0], &args[2], &args[3]);
599 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700600 case INDEX_op_brcond2_i32:
601 if (swap_commutative2(&args[0], &args[2])) {
602 args[4] = tcg_swap_cond(args[4]);
603 }
604 break;
605 case INDEX_op_setcond2_i32:
606 if (swap_commutative2(&args[1], &args[3])) {
607 args[5] = tcg_swap_cond(args[5]);
608 }
609 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400610 default:
611 break;
612 }
613
Richard Henderson2d497542013-03-21 09:13:33 -0700614 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
615 and "sub r, 0, a => neg r, a" case. */
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200616 switch (op) {
617 CASE_OP_32_64(shl):
618 CASE_OP_32_64(shr):
619 CASE_OP_32_64(sar):
620 CASE_OP_32_64(rotl):
621 CASE_OP_32_64(rotr):
622 if (temps[args[1]].state == TCG_TEMP_CONST
623 && temps[args[1]].val == 0) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400624 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200625 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200626 args += 3;
627 gen_args += 2;
628 continue;
629 }
630 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700631 CASE_OP_32_64(sub):
632 {
633 TCGOpcode neg_op;
634 bool have_neg;
635
636 if (temps[args[2]].state == TCG_TEMP_CONST) {
637 /* Proceed with possible constant folding. */
638 break;
639 }
640 if (op == INDEX_op_sub_i32) {
641 neg_op = INDEX_op_neg_i32;
642 have_neg = TCG_TARGET_HAS_neg_i32;
643 } else {
644 neg_op = INDEX_op_neg_i64;
645 have_neg = TCG_TARGET_HAS_neg_i64;
646 }
647 if (!have_neg) {
648 break;
649 }
650 if (temps[args[1]].state == TCG_TEMP_CONST
651 && temps[args[1]].val == 0) {
652 s->gen_opc_buf[op_index] = neg_op;
653 reset_temp(args[0]);
654 gen_args[0] = args[0];
655 gen_args[1] = args[2];
656 args += 3;
657 gen_args += 2;
658 continue;
659 }
660 }
661 break;
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200662 default:
663 break;
664 }
665
Aurelien Jarno56e49432012-09-06 16:47:13 +0200666 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400667 switch (op) {
668 CASE_OP_32_64(add):
669 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400670 CASE_OP_32_64(shl):
671 CASE_OP_32_64(shr):
672 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700673 CASE_OP_32_64(rotl):
674 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200675 CASE_OP_32_64(or):
676 CASE_OP_32_64(xor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400677 if (temps[args[1]].state == TCG_TEMP_CONST) {
678 /* Proceed with possible constant folding. */
679 break;
680 }
681 if (temps[args[2]].state == TCG_TEMP_CONST
682 && temps[args[2]].val == 0) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200683 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400684 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400685 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400686 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200687 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400688 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400689 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200690 args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400691 continue;
692 }
693 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200694 default:
695 break;
696 }
697
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800698 /* Simplify using known-zero bits */
699 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800700 affected = -1;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800701 switch (op) {
702 CASE_OP_32_64(ext8s):
703 if ((temps[args[1]].mask & 0x80) != 0) {
704 break;
705 }
706 CASE_OP_32_64(ext8u):
707 mask = 0xff;
708 goto and_const;
709 CASE_OP_32_64(ext16s):
710 if ((temps[args[1]].mask & 0x8000) != 0) {
711 break;
712 }
713 CASE_OP_32_64(ext16u):
714 mask = 0xffff;
715 goto and_const;
716 case INDEX_op_ext32s_i64:
717 if ((temps[args[1]].mask & 0x80000000) != 0) {
718 break;
719 }
720 case INDEX_op_ext32u_i64:
721 mask = 0xffffffffU;
722 goto and_const;
723
724 CASE_OP_32_64(and):
725 mask = temps[args[2]].mask;
726 if (temps[args[2]].state == TCG_TEMP_CONST) {
727 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800728 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800729 }
730 mask = temps[args[1]].mask & mask;
731 break;
732
733 CASE_OP_32_64(sar):
734 if (temps[args[2]].state == TCG_TEMP_CONST) {
735 mask = ((tcg_target_long)temps[args[1]].mask
736 >> temps[args[2]].val);
737 }
738 break;
739
740 CASE_OP_32_64(shr):
741 if (temps[args[2]].state == TCG_TEMP_CONST) {
742 mask = temps[args[1]].mask >> temps[args[2]].val;
743 }
744 break;
745
746 CASE_OP_32_64(shl):
747 if (temps[args[2]].state == TCG_TEMP_CONST) {
748 mask = temps[args[1]].mask << temps[args[2]].val;
749 }
750 break;
751
752 CASE_OP_32_64(neg):
753 /* Set to 1 all bits to the left of the rightmost. */
754 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
755 break;
756
757 CASE_OP_32_64(deposit):
758 tmp = ((1ull << args[4]) - 1);
759 mask = ((temps[args[1]].mask & ~(tmp << args[3]))
760 | ((temps[args[2]].mask & tmp) << args[3]));
761 break;
762
763 CASE_OP_32_64(or):
764 CASE_OP_32_64(xor):
765 mask = temps[args[1]].mask | temps[args[2]].mask;
766 break;
767
768 CASE_OP_32_64(setcond):
769 mask = 1;
770 break;
771
772 CASE_OP_32_64(movcond):
773 mask = temps[args[3]].mask | temps[args[4]].mask;
774 break;
775
776 default:
777 break;
778 }
779
Paolo Bonzini633f6502013-01-11 15:42:53 -0800780 if (mask == 0) {
781 assert(def->nb_oargs == 1);
782 s->gen_opc_buf[op_index] = op_to_movi(op);
783 tcg_opt_gen_movi(gen_args, args[0], 0);
784 args += def->nb_oargs + def->nb_iargs + def->nb_cargs;
785 gen_args += 2;
786 continue;
787 }
788 if (affected == 0) {
789 assert(def->nb_oargs == 1);
790 if (temps_are_copies(args[0], args[1])) {
791 s->gen_opc_buf[op_index] = INDEX_op_nop;
792 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
793 s->gen_opc_buf[op_index] = op_to_mov(op);
794 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
795 gen_args += 2;
796 } else {
797 s->gen_opc_buf[op_index] = op_to_movi(op);
798 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
799 gen_args += 2;
800 }
801 args += def->nb_iargs + 1;
802 continue;
803 }
804
Aurelien Jarno56e49432012-09-06 16:47:13 +0200805 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
806 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200807 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400808 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700809 CASE_OP_32_64(muluh):
810 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400811 if ((temps[args[2]].state == TCG_TEMP_CONST
812 && temps[args[2]].val == 0)) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400813 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200814 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400815 args += 3;
816 gen_args += 2;
817 continue;
818 }
819 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200820 default:
821 break;
822 }
823
824 /* Simplify expression for "op r, a, a => mov r, a" cases */
825 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400826 CASE_OP_32_64(or):
827 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200828 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200829 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400830 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400831 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400832 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200833 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400834 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400835 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200836 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400837 continue;
838 }
839 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000840 default:
841 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400842 }
843
Aurelien Jarno3c941932012-09-18 19:12:36 +0200844 /* Simplify expression for "op r, a, a => movi r, 0" cases */
845 switch (op) {
846 CASE_OP_32_64(sub):
847 CASE_OP_32_64(xor):
848 if (temps_are_copies(args[1], args[2])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400849 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200850 tcg_opt_gen_movi(gen_args, args[0], 0);
851 gen_args += 2;
852 args += 3;
853 continue;
854 }
855 break;
856 default:
857 break;
858 }
859
Kirill Batuzov22613af2011-07-07 16:37:13 +0400860 /* Propagate constants through copy operations and do constant
861 folding. Constants will be substituted to arguments by register
862 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400863 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400864 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200865 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400866 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400867 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400868 break;
869 }
870 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200871 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400872 gen_args += 2;
873 args += 2;
874 break;
875 }
876 /* Source argument is constant. Rewrite the operation and
877 let movi case handle it. */
878 op = op_to_movi(op);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400879 s->gen_opc_buf[op_index] = op;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400880 args[1] = temps[args[1]].val;
881 /* fallthrough */
882 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200883 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400884 gen_args += 2;
885 args += 2;
886 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700887
Kirill Batuzova640f032011-07-07 16:37:17 +0400888 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700889 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700890 CASE_OP_32_64(ext8s):
891 CASE_OP_32_64(ext8u):
892 CASE_OP_32_64(ext16s):
893 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400894 case INDEX_op_ext32s_i64:
895 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400896 if (temps[args[1]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400897 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzova640f032011-07-07 16:37:17 +0400898 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200899 tcg_opt_gen_movi(gen_args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -0700900 gen_args += 2;
901 args += 2;
902 break;
Kirill Batuzova640f032011-07-07 16:37:17 +0400903 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700904 goto do_default;
905
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400906 CASE_OP_32_64(add):
907 CASE_OP_32_64(sub):
908 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400909 CASE_OP_32_64(or):
910 CASE_OP_32_64(and):
911 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400912 CASE_OP_32_64(shl):
913 CASE_OP_32_64(shr):
914 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700915 CASE_OP_32_64(rotl):
916 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700917 CASE_OP_32_64(andc):
918 CASE_OP_32_64(orc):
919 CASE_OP_32_64(eqv):
920 CASE_OP_32_64(nand):
921 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700922 CASE_OP_32_64(muluh):
923 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -0700924 CASE_OP_32_64(div):
925 CASE_OP_32_64(divu):
926 CASE_OP_32_64(rem):
927 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400928 if (temps[args[1]].state == TCG_TEMP_CONST
929 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400930 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400931 tmp = do_constant_folding(op, temps[args[1]].val,
932 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200933 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400934 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700935 args += 3;
936 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400937 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700938 goto do_default;
939
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200940 CASE_OP_32_64(deposit):
941 if (temps[args[1]].state == TCG_TEMP_CONST
942 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400943 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200944 tmp = ((1ull << args[4]) - 1);
945 tmp = (temps[args[1]].val & ~(tmp << args[3]))
946 | ((temps[args[2]].val & tmp) << args[3]);
947 tcg_opt_gen_movi(gen_args, args[0], tmp);
948 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700949 args += 5;
950 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200951 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700952 goto do_default;
953
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200954 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200955 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
956 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400957 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200958 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200959 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700960 args += 4;
961 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200962 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700963 goto do_default;
964
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200965 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200966 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
967 if (tmp != 2) {
968 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -0800969 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400970 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200971 gen_args[0] = args[3];
972 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200973 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400974 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200975 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700976 args += 4;
977 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200978 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700979 goto do_default;
980
Richard Hendersonfa01a202012-09-21 10:13:37 -0700981 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200982 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
983 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200984 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400985 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700986 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400987 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200988 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700989 gen_args += 2;
990 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400991 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200992 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700993 gen_args += 2;
994 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700995 args += 6;
996 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700997 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700998 goto do_default;
999
Richard Henderson212c3282012-10-02 11:32:28 -07001000 case INDEX_op_add2_i32:
1001 case INDEX_op_sub2_i32:
1002 if (temps[args[2]].state == TCG_TEMP_CONST
1003 && temps[args[3]].state == TCG_TEMP_CONST
1004 && temps[args[4]].state == TCG_TEMP_CONST
1005 && temps[args[5]].state == TCG_TEMP_CONST) {
1006 uint32_t al = temps[args[2]].val;
1007 uint32_t ah = temps[args[3]].val;
1008 uint32_t bl = temps[args[4]].val;
1009 uint32_t bh = temps[args[5]].val;
1010 uint64_t a = ((uint64_t)ah << 32) | al;
1011 uint64_t b = ((uint64_t)bh << 32) | bl;
1012 TCGArg rl, rh;
1013
1014 if (op == INDEX_op_add2_i32) {
1015 a += b;
1016 } else {
1017 a -= b;
1018 }
1019
1020 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001021 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -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 Henderson212c3282012-10-02 11:32:28 -07001027 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
1028 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
1029 gen_args += 4;
1030 args += 6;
1031 break;
1032 }
1033 goto do_default;
1034
Richard Henderson14149682012-10-02 11:32:30 -07001035 case INDEX_op_mulu2_i32:
1036 if (temps[args[2]].state == TCG_TEMP_CONST
1037 && temps[args[3]].state == TCG_TEMP_CONST) {
1038 uint32_t a = temps[args[2]].val;
1039 uint32_t b = temps[args[3]].val;
1040 uint64_t r = (uint64_t)a * b;
1041 TCGArg rl, rh;
1042
1043 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001044 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -07001045
1046 rl = args[0];
1047 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001048 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1049 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson14149682012-10-02 11:32:30 -07001050 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
1051 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
1052 gen_args += 4;
1053 args += 4;
1054 break;
1055 }
1056 goto do_default;
1057
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001058 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001059 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1060 if (tmp != 2) {
1061 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001062 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001063 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001064 gen_args[0] = args[5];
1065 gen_args += 1;
1066 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001067 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001068 }
1069 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1070 && temps[args[2]].state == TCG_TEMP_CONST
1071 && temps[args[3]].state == TCG_TEMP_CONST
1072 && temps[args[2]].val == 0
1073 && temps[args[3]].val == 0) {
1074 /* Simplify LT/GE comparisons vs zero to a single compare
1075 vs the high word of the input. */
Paolo Bonzinid193a142013-01-11 15:42:51 -08001076 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001077 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001078 gen_args[0] = args[1];
1079 gen_args[1] = args[3];
1080 gen_args[2] = args[4];
1081 gen_args[3] = args[5];
1082 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001083 } else {
1084 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001085 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001086 args += 6;
1087 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001088
1089 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001090 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1091 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001092 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001093 tcg_opt_gen_movi(gen_args, args[0], tmp);
1094 gen_args += 2;
1095 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1096 && temps[args[3]].state == TCG_TEMP_CONST
1097 && temps[args[4]].state == TCG_TEMP_CONST
1098 && temps[args[3]].val == 0
1099 && temps[args[4]].val == 0) {
1100 /* Simplify LT/GE comparisons vs zero to a single compare
1101 vs the high word of the input. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001102 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001103 reset_temp(args[0]);
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001104 gen_args[0] = args[0];
1105 gen_args[1] = args[2];
1106 gen_args[2] = args[4];
1107 gen_args[3] = args[5];
1108 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001109 } else {
1110 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001111 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001112 args += 6;
1113 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001114
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001115 case INDEX_op_call:
Kirill Batuzov22613af2011-07-07 16:37:13 +04001116 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
Aurelien Jarno78505272012-10-09 21:53:08 +02001117 if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1118 TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001119 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001120 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001121 }
1122 }
1123 for (i = 0; i < (args[0] >> 16); i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001124 reset_temp(args[i + 1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001125 }
1126 i = nb_call_args + 3;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001127 while (i) {
1128 *gen_args = *args;
1129 args++;
1130 gen_args++;
1131 i--;
1132 }
1133 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001134
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001135 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001136 do_default:
1137 /* Default case: we know nothing about operation (or were unable
1138 to compute the operation result) so no propagation is done.
1139 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001140 block, otherwise we only trash the output args. "mask" is
1141 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001142 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001143 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001144 } else {
1145 for (i = 0; i < def->nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001146 reset_temp(args[i]);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001147 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001148 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001149 for (i = 0; i < def->nb_args; i++) {
1150 gen_args[i] = args[i];
1151 }
1152 args += def->nb_args;
1153 gen_args += def->nb_args;
1154 break;
1155 }
1156 }
1157
1158 return gen_args;
1159}
1160
1161TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1162 TCGArg *args, TCGOpDef *tcg_op_defs)
1163{
1164 TCGArg *res;
1165 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1166 return res;
1167}