blob: 090efbc1933ca2d32726d56f0e9f04f3f56cefac [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{
201 switch (op) {
202 CASE_OP_32_64(add):
203 return x + y;
204
205 CASE_OP_32_64(sub):
206 return x - y;
207
208 CASE_OP_32_64(mul):
209 return x * y;
210
Kirill Batuzov9a810902011-07-07 16:37:15 +0400211 CASE_OP_32_64(and):
212 return x & y;
213
214 CASE_OP_32_64(or):
215 return x | y;
216
217 CASE_OP_32_64(xor):
218 return x ^ y;
219
Kirill Batuzov55c09752011-07-07 16:37:16 +0400220 case INDEX_op_shl_i32:
221 return (uint32_t)x << (uint32_t)y;
222
Kirill Batuzov55c09752011-07-07 16:37:16 +0400223 case INDEX_op_shl_i64:
224 return (uint64_t)x << (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400225
226 case INDEX_op_shr_i32:
227 return (uint32_t)x >> (uint32_t)y;
228
Kirill Batuzov55c09752011-07-07 16:37:16 +0400229 case INDEX_op_shr_i64:
230 return (uint64_t)x >> (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400231
232 case INDEX_op_sar_i32:
233 return (int32_t)x >> (int32_t)y;
234
Kirill Batuzov55c09752011-07-07 16:37:16 +0400235 case INDEX_op_sar_i64:
236 return (int64_t)x >> (int64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400237
238 case INDEX_op_rotr_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700239 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400240 return x;
241
Kirill Batuzov55c09752011-07-07 16:37:16 +0400242 case INDEX_op_rotr_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700243 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400244 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400245
246 case INDEX_op_rotl_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700247 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400248 return x;
249
Kirill Batuzov55c09752011-07-07 16:37:16 +0400250 case INDEX_op_rotl_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700251 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400252 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400253
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700254 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400255 return ~x;
256
Richard Hendersoncb25c802011-08-17 14:11:47 -0700257 CASE_OP_32_64(neg):
258 return -x;
259
260 CASE_OP_32_64(andc):
261 return x & ~y;
262
263 CASE_OP_32_64(orc):
264 return x | ~y;
265
266 CASE_OP_32_64(eqv):
267 return ~(x ^ y);
268
269 CASE_OP_32_64(nand):
270 return ~(x & y);
271
272 CASE_OP_32_64(nor):
273 return ~(x | y);
274
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700275 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400276 return (int8_t)x;
277
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700278 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400279 return (int16_t)x;
280
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700281 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400282 return (uint8_t)x;
283
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700284 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400285 return (uint16_t)x;
286
Kirill Batuzova640f032011-07-07 16:37:17 +0400287 case INDEX_op_ext32s_i64:
288 return (int32_t)x;
289
290 case INDEX_op_ext32u_i64:
291 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400292
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400293 default:
294 fprintf(stderr,
295 "Unrecognized operation %d in do_constant_folding.\n", op);
296 tcg_abort();
297 }
298}
299
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000300static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400301{
302 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400303 if (op_bits(op) == 32) {
304 res &= 0xffffffff;
305 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400306 return res;
307}
308
Richard Henderson9519da72012-10-02 11:32:26 -0700309static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
310{
311 switch (c) {
312 case TCG_COND_EQ:
313 return x == y;
314 case TCG_COND_NE:
315 return x != y;
316 case TCG_COND_LT:
317 return (int32_t)x < (int32_t)y;
318 case TCG_COND_GE:
319 return (int32_t)x >= (int32_t)y;
320 case TCG_COND_LE:
321 return (int32_t)x <= (int32_t)y;
322 case TCG_COND_GT:
323 return (int32_t)x > (int32_t)y;
324 case TCG_COND_LTU:
325 return x < y;
326 case TCG_COND_GEU:
327 return x >= y;
328 case TCG_COND_LEU:
329 return x <= y;
330 case TCG_COND_GTU:
331 return x > y;
332 default:
333 tcg_abort();
334 }
335}
336
337static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
338{
339 switch (c) {
340 case TCG_COND_EQ:
341 return x == y;
342 case TCG_COND_NE:
343 return x != y;
344 case TCG_COND_LT:
345 return (int64_t)x < (int64_t)y;
346 case TCG_COND_GE:
347 return (int64_t)x >= (int64_t)y;
348 case TCG_COND_LE:
349 return (int64_t)x <= (int64_t)y;
350 case TCG_COND_GT:
351 return (int64_t)x > (int64_t)y;
352 case TCG_COND_LTU:
353 return x < y;
354 case TCG_COND_GEU:
355 return x >= y;
356 case TCG_COND_LEU:
357 return x <= y;
358 case TCG_COND_GTU:
359 return x > y;
360 default:
361 tcg_abort();
362 }
363}
364
365static bool do_constant_folding_cond_eq(TCGCond c)
366{
367 switch (c) {
368 case TCG_COND_GT:
369 case TCG_COND_LTU:
370 case TCG_COND_LT:
371 case TCG_COND_GTU:
372 case TCG_COND_NE:
373 return 0;
374 case TCG_COND_GE:
375 case TCG_COND_GEU:
376 case TCG_COND_LE:
377 case TCG_COND_LEU:
378 case TCG_COND_EQ:
379 return 1;
380 default:
381 tcg_abort();
382 }
383}
384
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200385/* Return 2 if the condition can't be simplified, and the result
386 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200387static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
388 TCGArg y, TCGCond c)
389{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200390 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
391 switch (op_bits(op)) {
392 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700393 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200394 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700395 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
396 default:
397 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200398 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200399 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700400 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200401 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
402 switch (c) {
403 case TCG_COND_LTU:
404 return 0;
405 case TCG_COND_GEU:
406 return 1;
407 default:
408 return 2;
409 }
410 } else {
411 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200412 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200413}
414
Richard Henderson6c4382f2012-10-02 11:32:27 -0700415/* Return 2 if the condition can't be simplified, and the result
416 of the condition (0 or 1) if it can */
417static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
418{
419 TCGArg al = p1[0], ah = p1[1];
420 TCGArg bl = p2[0], bh = p2[1];
421
422 if (temps[bl].state == TCG_TEMP_CONST
423 && temps[bh].state == TCG_TEMP_CONST) {
424 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
425
426 if (temps[al].state == TCG_TEMP_CONST
427 && temps[ah].state == TCG_TEMP_CONST) {
428 uint64_t a;
429 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
430 return do_constant_folding_cond_64(a, b, c);
431 }
432 if (b == 0) {
433 switch (c) {
434 case TCG_COND_LTU:
435 return 0;
436 case TCG_COND_GEU:
437 return 1;
438 default:
439 break;
440 }
441 }
442 }
443 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
444 return do_constant_folding_cond_eq(c);
445 }
446 return 2;
447}
448
Richard Henderson24c9ae42012-10-02 11:32:21 -0700449static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
450{
451 TCGArg a1 = *p1, a2 = *p2;
452 int sum = 0;
453 sum += temps[a1].state == TCG_TEMP_CONST;
454 sum -= temps[a2].state == TCG_TEMP_CONST;
455
456 /* Prefer the constant in second argument, and then the form
457 op a, a, b, which is better handled on non-RISC hosts. */
458 if (sum > 0 || (sum == 0 && dest == a2)) {
459 *p1 = a2;
460 *p2 = a1;
461 return true;
462 }
463 return false;
464}
465
Richard Henderson0bfcb862012-10-02 11:32:23 -0700466static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
467{
468 int sum = 0;
469 sum += temps[p1[0]].state == TCG_TEMP_CONST;
470 sum += temps[p1[1]].state == TCG_TEMP_CONST;
471 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
472 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
473 if (sum > 0) {
474 TCGArg t;
475 t = p1[0], p1[0] = p2[0], p2[0] = t;
476 t = p1[1], p1[1] = p2[1], p2[1] = t;
477 return true;
478 }
479 return false;
480}
481
Kirill Batuzov22613af2011-07-07 16:37:13 +0400482/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400483static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
484 TCGArg *args, TCGOpDef *tcg_op_defs)
485{
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000486 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800487 tcg_target_ulong mask;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000488 TCGOpcode op;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400489 const TCGOpDef *def;
490 TCGArg *gen_args;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400491 TCGArg tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700492
Kirill Batuzov22613af2011-07-07 16:37:13 +0400493 /* Array VALS has an element for each temp.
494 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200495 If this temp is a copy of other ones then the other copies are
496 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400497
498 nb_temps = s->nb_temps;
499 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800500 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400501
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400502 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400503 gen_args = args;
504 for (op_index = 0; op_index < nb_ops; op_index++) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400505 op = s->gen_opc_buf[op_index];
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400506 def = &tcg_op_defs[op];
Kirill Batuzov22613af2011-07-07 16:37:13 +0400507 /* Do copy propagation */
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200508 if (op == INDEX_op_call) {
509 int nb_oargs = args[0] >> 16;
510 int nb_iargs = args[0] & 0xffff;
511 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
512 if (temps[args[i]].state == TCG_TEMP_COPY) {
513 args[i] = find_better_copy(s, args[i]);
514 }
515 }
516 } else {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400517 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
518 if (temps[args[i]].state == TCG_TEMP_COPY) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200519 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400520 }
521 }
522 }
523
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400524 /* For commutative operations make constant second argument */
525 switch (op) {
526 CASE_OP_32_64(add):
527 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400528 CASE_OP_32_64(and):
529 CASE_OP_32_64(or):
530 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700531 CASE_OP_32_64(eqv):
532 CASE_OP_32_64(nand):
533 CASE_OP_32_64(nor):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700534 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400535 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200536 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700537 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200538 args[2] = tcg_swap_cond(args[2]);
539 }
540 break;
541 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700542 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200543 args[3] = tcg_swap_cond(args[3]);
544 }
545 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700546 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700547 if (swap_commutative(-1, &args[1], &args[2])) {
548 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700549 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700550 /* For movcond, we canonicalize the "false" input reg to match
551 the destination reg so that the tcg backend can implement
552 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700553 if (swap_commutative(args[0], &args[4], &args[3])) {
554 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700555 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700556 break;
557 case INDEX_op_add2_i32:
558 swap_commutative(args[0], &args[2], &args[4]);
559 swap_commutative(args[1], &args[3], &args[5]);
560 break;
Richard Henderson14149682012-10-02 11:32:30 -0700561 case INDEX_op_mulu2_i32:
562 swap_commutative(args[0], &args[2], &args[3]);
563 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700564 case INDEX_op_brcond2_i32:
565 if (swap_commutative2(&args[0], &args[2])) {
566 args[4] = tcg_swap_cond(args[4]);
567 }
568 break;
569 case INDEX_op_setcond2_i32:
570 if (swap_commutative2(&args[1], &args[3])) {
571 args[5] = tcg_swap_cond(args[5]);
572 }
573 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400574 default:
575 break;
576 }
577
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200578 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
579 switch (op) {
580 CASE_OP_32_64(shl):
581 CASE_OP_32_64(shr):
582 CASE_OP_32_64(sar):
583 CASE_OP_32_64(rotl):
584 CASE_OP_32_64(rotr):
585 if (temps[args[1]].state == TCG_TEMP_CONST
586 && temps[args[1]].val == 0) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400587 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200588 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200589 args += 3;
590 gen_args += 2;
591 continue;
592 }
593 break;
594 default:
595 break;
596 }
597
Aurelien Jarno56e49432012-09-06 16:47:13 +0200598 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400599 switch (op) {
600 CASE_OP_32_64(add):
601 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400602 CASE_OP_32_64(shl):
603 CASE_OP_32_64(shr):
604 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700605 CASE_OP_32_64(rotl):
606 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200607 CASE_OP_32_64(or):
608 CASE_OP_32_64(xor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400609 if (temps[args[1]].state == TCG_TEMP_CONST) {
610 /* Proceed with possible constant folding. */
611 break;
612 }
613 if (temps[args[2]].state == TCG_TEMP_CONST
614 && temps[args[2]].val == 0) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200615 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400616 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400617 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400618 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200619 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400620 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400621 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200622 args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400623 continue;
624 }
625 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200626 default:
627 break;
628 }
629
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800630 /* Simplify using known-zero bits */
631 mask = -1;
632 switch (op) {
633 CASE_OP_32_64(ext8s):
634 if ((temps[args[1]].mask & 0x80) != 0) {
635 break;
636 }
637 CASE_OP_32_64(ext8u):
638 mask = 0xff;
639 goto and_const;
640 CASE_OP_32_64(ext16s):
641 if ((temps[args[1]].mask & 0x8000) != 0) {
642 break;
643 }
644 CASE_OP_32_64(ext16u):
645 mask = 0xffff;
646 goto and_const;
647 case INDEX_op_ext32s_i64:
648 if ((temps[args[1]].mask & 0x80000000) != 0) {
649 break;
650 }
651 case INDEX_op_ext32u_i64:
652 mask = 0xffffffffU;
653 goto and_const;
654
655 CASE_OP_32_64(and):
656 mask = temps[args[2]].mask;
657 if (temps[args[2]].state == TCG_TEMP_CONST) {
658 and_const:
659 ;
660 }
661 mask = temps[args[1]].mask & mask;
662 break;
663
664 CASE_OP_32_64(sar):
665 if (temps[args[2]].state == TCG_TEMP_CONST) {
666 mask = ((tcg_target_long)temps[args[1]].mask
667 >> temps[args[2]].val);
668 }
669 break;
670
671 CASE_OP_32_64(shr):
672 if (temps[args[2]].state == TCG_TEMP_CONST) {
673 mask = temps[args[1]].mask >> temps[args[2]].val;
674 }
675 break;
676
677 CASE_OP_32_64(shl):
678 if (temps[args[2]].state == TCG_TEMP_CONST) {
679 mask = temps[args[1]].mask << temps[args[2]].val;
680 }
681 break;
682
683 CASE_OP_32_64(neg):
684 /* Set to 1 all bits to the left of the rightmost. */
685 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
686 break;
687
688 CASE_OP_32_64(deposit):
689 tmp = ((1ull << args[4]) - 1);
690 mask = ((temps[args[1]].mask & ~(tmp << args[3]))
691 | ((temps[args[2]].mask & tmp) << args[3]));
692 break;
693
694 CASE_OP_32_64(or):
695 CASE_OP_32_64(xor):
696 mask = temps[args[1]].mask | temps[args[2]].mask;
697 break;
698
699 CASE_OP_32_64(setcond):
700 mask = 1;
701 break;
702
703 CASE_OP_32_64(movcond):
704 mask = temps[args[3]].mask | temps[args[4]].mask;
705 break;
706
707 default:
708 break;
709 }
710
Aurelien Jarno56e49432012-09-06 16:47:13 +0200711 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
712 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200713 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400714 CASE_OP_32_64(mul):
715 if ((temps[args[2]].state == TCG_TEMP_CONST
716 && temps[args[2]].val == 0)) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400717 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200718 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400719 args += 3;
720 gen_args += 2;
721 continue;
722 }
723 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200724 default:
725 break;
726 }
727
728 /* Simplify expression for "op r, a, a => mov r, a" cases */
729 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400730 CASE_OP_32_64(or):
731 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200732 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200733 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400734 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400735 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400736 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200737 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400738 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400739 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200740 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400741 continue;
742 }
743 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000744 default:
745 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400746 }
747
Aurelien Jarno3c941932012-09-18 19:12:36 +0200748 /* Simplify expression for "op r, a, a => movi r, 0" cases */
749 switch (op) {
750 CASE_OP_32_64(sub):
751 CASE_OP_32_64(xor):
752 if (temps_are_copies(args[1], args[2])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400753 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200754 tcg_opt_gen_movi(gen_args, args[0], 0);
755 gen_args += 2;
756 args += 3;
757 continue;
758 }
759 break;
760 default:
761 break;
762 }
763
Kirill Batuzov22613af2011-07-07 16:37:13 +0400764 /* Propagate constants through copy operations and do constant
765 folding. Constants will be substituted to arguments by register
766 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400767 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400768 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200769 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400770 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400771 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400772 break;
773 }
774 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200775 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400776 gen_args += 2;
777 args += 2;
778 break;
779 }
780 /* Source argument is constant. Rewrite the operation and
781 let movi case handle it. */
782 op = op_to_movi(op);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400783 s->gen_opc_buf[op_index] = op;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400784 args[1] = temps[args[1]].val;
785 /* fallthrough */
786 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200787 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400788 gen_args += 2;
789 args += 2;
790 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700791
Kirill Batuzova640f032011-07-07 16:37:17 +0400792 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700793 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700794 CASE_OP_32_64(ext8s):
795 CASE_OP_32_64(ext8u):
796 CASE_OP_32_64(ext16s):
797 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400798 case INDEX_op_ext32s_i64:
799 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400800 if (temps[args[1]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400801 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzova640f032011-07-07 16:37:17 +0400802 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200803 tcg_opt_gen_movi(gen_args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -0700804 gen_args += 2;
805 args += 2;
806 break;
Kirill Batuzova640f032011-07-07 16:37:17 +0400807 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700808 goto do_default;
809
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400810 CASE_OP_32_64(add):
811 CASE_OP_32_64(sub):
812 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400813 CASE_OP_32_64(or):
814 CASE_OP_32_64(and):
815 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400816 CASE_OP_32_64(shl):
817 CASE_OP_32_64(shr):
818 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700819 CASE_OP_32_64(rotl):
820 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700821 CASE_OP_32_64(andc):
822 CASE_OP_32_64(orc):
823 CASE_OP_32_64(eqv):
824 CASE_OP_32_64(nand):
825 CASE_OP_32_64(nor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400826 if (temps[args[1]].state == TCG_TEMP_CONST
827 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400828 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400829 tmp = do_constant_folding(op, temps[args[1]].val,
830 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200831 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400832 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700833 args += 3;
834 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400835 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700836 goto do_default;
837
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200838 CASE_OP_32_64(deposit):
839 if (temps[args[1]].state == TCG_TEMP_CONST
840 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400841 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200842 tmp = ((1ull << args[4]) - 1);
843 tmp = (temps[args[1]].val & ~(tmp << args[3]))
844 | ((temps[args[2]].val & tmp) << args[3]);
845 tcg_opt_gen_movi(gen_args, args[0], tmp);
846 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700847 args += 5;
848 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200849 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700850 goto do_default;
851
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200852 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200853 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
854 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400855 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200856 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200857 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700858 args += 4;
859 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200860 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700861 goto do_default;
862
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200863 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200864 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
865 if (tmp != 2) {
866 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -0800867 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400868 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200869 gen_args[0] = args[3];
870 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200871 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400872 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200873 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700874 args += 4;
875 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200876 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700877 goto do_default;
878
Richard Hendersonfa01a202012-09-21 10:13:37 -0700879 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200880 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
881 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200882 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400883 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700884 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400885 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200886 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700887 gen_args += 2;
888 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400889 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200890 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700891 gen_args += 2;
892 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700893 args += 6;
894 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700895 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700896 goto do_default;
897
Richard Henderson212c3282012-10-02 11:32:28 -0700898 case INDEX_op_add2_i32:
899 case INDEX_op_sub2_i32:
900 if (temps[args[2]].state == TCG_TEMP_CONST
901 && temps[args[3]].state == TCG_TEMP_CONST
902 && temps[args[4]].state == TCG_TEMP_CONST
903 && temps[args[5]].state == TCG_TEMP_CONST) {
904 uint32_t al = temps[args[2]].val;
905 uint32_t ah = temps[args[3]].val;
906 uint32_t bl = temps[args[4]].val;
907 uint32_t bh = temps[args[5]].val;
908 uint64_t a = ((uint64_t)ah << 32) | al;
909 uint64_t b = ((uint64_t)bh << 32) | bl;
910 TCGArg rl, rh;
911
912 if (op == INDEX_op_add2_i32) {
913 a += b;
914 } else {
915 a -= b;
916 }
917
918 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400919 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -0700920
921 rl = args[0];
922 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400923 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
924 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson212c3282012-10-02 11:32:28 -0700925 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
926 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
927 gen_args += 4;
928 args += 6;
929 break;
930 }
931 goto do_default;
932
Richard Henderson14149682012-10-02 11:32:30 -0700933 case INDEX_op_mulu2_i32:
934 if (temps[args[2]].state == TCG_TEMP_CONST
935 && temps[args[3]].state == TCG_TEMP_CONST) {
936 uint32_t a = temps[args[2]].val;
937 uint32_t b = temps[args[3]].val;
938 uint64_t r = (uint64_t)a * b;
939 TCGArg rl, rh;
940
941 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400942 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -0700943
944 rl = args[0];
945 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400946 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
947 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson14149682012-10-02 11:32:30 -0700948 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
949 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
950 gen_args += 4;
951 args += 4;
952 break;
953 }
954 goto do_default;
955
Richard Hendersonbc1473e2012-10-02 11:32:25 -0700956 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -0700957 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
958 if (tmp != 2) {
959 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -0800960 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400961 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700962 gen_args[0] = args[5];
963 gen_args += 1;
964 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400965 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700966 }
967 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
968 && temps[args[2]].state == TCG_TEMP_CONST
969 && temps[args[3]].state == TCG_TEMP_CONST
970 && temps[args[2]].val == 0
971 && temps[args[3]].val == 0) {
972 /* Simplify LT/GE comparisons vs zero to a single compare
973 vs the high word of the input. */
Paolo Bonzinid193a142013-01-11 15:42:51 -0800974 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400975 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -0700976 gen_args[0] = args[1];
977 gen_args[1] = args[3];
978 gen_args[2] = args[4];
979 gen_args[3] = args[5];
980 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700981 } else {
982 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -0700983 }
Richard Henderson6c4382f2012-10-02 11:32:27 -0700984 args += 6;
985 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -0700986
987 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -0700988 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
989 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400990 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
Richard Henderson6c4382f2012-10-02 11:32:27 -0700991 tcg_opt_gen_movi(gen_args, args[0], tmp);
992 gen_args += 2;
993 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
994 && temps[args[3]].state == TCG_TEMP_CONST
995 && temps[args[4]].state == TCG_TEMP_CONST
996 && temps[args[3]].val == 0
997 && temps[args[4]].val == 0) {
998 /* Simplify LT/GE comparisons vs zero to a single compare
999 vs the high word of the input. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001000 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001001 gen_args[0] = args[0];
1002 gen_args[1] = args[2];
1003 gen_args[2] = args[4];
1004 gen_args[3] = args[5];
1005 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001006 } else {
1007 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001008 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001009 args += 6;
1010 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001011
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001012 case INDEX_op_call:
Kirill Batuzov22613af2011-07-07 16:37:13 +04001013 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
Aurelien Jarno78505272012-10-09 21:53:08 +02001014 if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1015 TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001016 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001017 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001018 }
1019 }
1020 for (i = 0; i < (args[0] >> 16); i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001021 reset_temp(args[i + 1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001022 }
1023 i = nb_call_args + 3;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001024 while (i) {
1025 *gen_args = *args;
1026 args++;
1027 gen_args++;
1028 i--;
1029 }
1030 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001031
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001032 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001033 do_default:
1034 /* Default case: we know nothing about operation (or were unable
1035 to compute the operation result) so no propagation is done.
1036 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001037 block, otherwise we only trash the output args. "mask" is
1038 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001039 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001040 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001041 } else {
1042 for (i = 0; i < def->nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001043 reset_temp(args[i]);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001044 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001045 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001046 for (i = 0; i < def->nb_args; i++) {
1047 gen_args[i] = args[i];
1048 }
1049 args += def->nb_args;
1050 gen_args += def->nb_args;
1051 break;
1052 }
1053 }
1054
1055 return gen_args;
1056}
1057
1058TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1059 TCGArg *args, TCGOpDef *tcg_op_defs)
1060{
1061 TCGArg *res;
1062 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1063 return res;
1064}