blob: 38027dc5ad4492fdd612ea2a960ed01718716ed5 [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;
49};
50
51static struct tcg_temp_info temps[TCG_MAX_TEMPS];
52
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020053/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
54 the copy flag from the left temp. */
55static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040056{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020057 if (temps[temp].state == TCG_TEMP_COPY) {
58 if (temps[temp].prev_copy == temps[temp].next_copy) {
59 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
60 } else {
61 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
62 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
Kirill Batuzov22613af2011-07-07 16:37:13 +040063 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040064 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020065 temps[temp].state = TCG_TEMP_UNDEF;
Kirill Batuzov22613af2011-07-07 16:37:13 +040066}
67
Blue Swirlfe0de7a2011-07-30 19:18:32 +000068static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040069{
Richard Henderson8399ad52011-08-17 14:11:45 -070070 const TCGOpDef *def = &tcg_op_defs[op];
71 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040072}
73
Blue Swirlfe0de7a2011-07-30 19:18:32 +000074static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040075{
76 switch (op_bits(op)) {
77 case 32:
78 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040079 case 64:
80 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +040081 default:
82 fprintf(stderr, "op_to_movi: unexpected return value of "
83 "function op_bits.\n");
84 tcg_abort();
85 }
86}
87
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020088static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
89{
90 TCGArg i;
91
92 /* If this is already a global, we can't do better. */
93 if (temp < s->nb_globals) {
94 return temp;
95 }
96
97 /* Search for a global first. */
98 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
99 if (i < s->nb_globals) {
100 return i;
101 }
102 }
103
104 /* If it is a temp, search for a temp local. */
105 if (!s->temps[temp].temp_local) {
106 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
107 if (s->temps[i].temp_local) {
108 return i;
109 }
110 }
111 }
112
113 /* Failure to find a better representation, return the same temp. */
114 return temp;
115}
116
117static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
118{
119 TCGArg i;
120
121 if (arg1 == arg2) {
122 return true;
123 }
124
125 if (temps[arg1].state != TCG_TEMP_COPY
126 || temps[arg2].state != TCG_TEMP_COPY) {
127 return false;
128 }
129
130 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
131 if (i == arg2) {
132 return true;
133 }
134 }
135
136 return false;
137}
138
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200139static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
140 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400141{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200142 reset_temp(dst);
143 assert(temps[src].state != TCG_TEMP_CONST);
144
145 if (s->temps[src].type == s->temps[dst].type) {
146 if (temps[src].state != TCG_TEMP_COPY) {
147 temps[src].state = TCG_TEMP_COPY;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400148 temps[src].next_copy = src;
149 temps[src].prev_copy = src;
150 }
151 temps[dst].state = TCG_TEMP_COPY;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400152 temps[dst].next_copy = temps[src].next_copy;
153 temps[dst].prev_copy = src;
154 temps[temps[dst].next_copy].prev_copy = dst;
155 temps[src].next_copy = dst;
156 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200157
Kirill Batuzov22613af2011-07-07 16:37:13 +0400158 gen_args[0] = dst;
159 gen_args[1] = src;
160}
161
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200162static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400163{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200164 reset_temp(dst);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400165 temps[dst].state = TCG_TEMP_CONST;
166 temps[dst].val = val;
167 gen_args[0] = dst;
168 gen_args[1] = val;
169}
170
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000171static TCGOpcode op_to_mov(TCGOpcode op)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400172{
173 switch (op_bits(op)) {
174 case 32:
175 return INDEX_op_mov_i32;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400176 case 64:
177 return INDEX_op_mov_i64;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400178 default:
179 fprintf(stderr, "op_to_mov: unexpected return value of "
180 "function op_bits.\n");
181 tcg_abort();
182 }
183}
184
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000185static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400186{
187 switch (op) {
188 CASE_OP_32_64(add):
189 return x + y;
190
191 CASE_OP_32_64(sub):
192 return x - y;
193
194 CASE_OP_32_64(mul):
195 return x * y;
196
Kirill Batuzov9a810902011-07-07 16:37:15 +0400197 CASE_OP_32_64(and):
198 return x & y;
199
200 CASE_OP_32_64(or):
201 return x | y;
202
203 CASE_OP_32_64(xor):
204 return x ^ y;
205
Kirill Batuzov55c09752011-07-07 16:37:16 +0400206 case INDEX_op_shl_i32:
207 return (uint32_t)x << (uint32_t)y;
208
Kirill Batuzov55c09752011-07-07 16:37:16 +0400209 case INDEX_op_shl_i64:
210 return (uint64_t)x << (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400211
212 case INDEX_op_shr_i32:
213 return (uint32_t)x >> (uint32_t)y;
214
Kirill Batuzov55c09752011-07-07 16:37:16 +0400215 case INDEX_op_shr_i64:
216 return (uint64_t)x >> (uint64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400217
218 case INDEX_op_sar_i32:
219 return (int32_t)x >> (int32_t)y;
220
Kirill Batuzov55c09752011-07-07 16:37:16 +0400221 case INDEX_op_sar_i64:
222 return (int64_t)x >> (int64_t)y;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400223
224 case INDEX_op_rotr_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700225 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400226 return x;
227
Kirill Batuzov55c09752011-07-07 16:37:16 +0400228 case INDEX_op_rotr_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700229 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400230 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400231
232 case INDEX_op_rotl_i32:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700233 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400234 return x;
235
Kirill Batuzov55c09752011-07-07 16:37:16 +0400236 case INDEX_op_rotl_i64:
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700237 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
Kirill Batuzov55c09752011-07-07 16:37:16 +0400238 return x;
Kirill Batuzov55c09752011-07-07 16:37:16 +0400239
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700240 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400241 return ~x;
242
Richard Hendersoncb25c802011-08-17 14:11:47 -0700243 CASE_OP_32_64(neg):
244 return -x;
245
246 CASE_OP_32_64(andc):
247 return x & ~y;
248
249 CASE_OP_32_64(orc):
250 return x | ~y;
251
252 CASE_OP_32_64(eqv):
253 return ~(x ^ y);
254
255 CASE_OP_32_64(nand):
256 return ~(x & y);
257
258 CASE_OP_32_64(nor):
259 return ~(x | y);
260
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700261 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400262 return (int8_t)x;
263
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700264 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400265 return (int16_t)x;
266
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700267 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400268 return (uint8_t)x;
269
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700270 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400271 return (uint16_t)x;
272
Kirill Batuzova640f032011-07-07 16:37:17 +0400273 case INDEX_op_ext32s_i64:
274 return (int32_t)x;
275
276 case INDEX_op_ext32u_i64:
277 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400278
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400279 default:
280 fprintf(stderr,
281 "Unrecognized operation %d in do_constant_folding.\n", op);
282 tcg_abort();
283 }
284}
285
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000286static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400287{
288 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400289 if (op_bits(op) == 32) {
290 res &= 0xffffffff;
291 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400292 return res;
293}
294
Richard Henderson9519da72012-10-02 11:32:26 -0700295static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
296{
297 switch (c) {
298 case TCG_COND_EQ:
299 return x == y;
300 case TCG_COND_NE:
301 return x != y;
302 case TCG_COND_LT:
303 return (int32_t)x < (int32_t)y;
304 case TCG_COND_GE:
305 return (int32_t)x >= (int32_t)y;
306 case TCG_COND_LE:
307 return (int32_t)x <= (int32_t)y;
308 case TCG_COND_GT:
309 return (int32_t)x > (int32_t)y;
310 case TCG_COND_LTU:
311 return x < y;
312 case TCG_COND_GEU:
313 return x >= y;
314 case TCG_COND_LEU:
315 return x <= y;
316 case TCG_COND_GTU:
317 return x > y;
318 default:
319 tcg_abort();
320 }
321}
322
323static bool do_constant_folding_cond_64(uint64_t x, uint64_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 (int64_t)x < (int64_t)y;
332 case TCG_COND_GE:
333 return (int64_t)x >= (int64_t)y;
334 case TCG_COND_LE:
335 return (int64_t)x <= (int64_t)y;
336 case TCG_COND_GT:
337 return (int64_t)x > (int64_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_eq(TCGCond c)
352{
353 switch (c) {
354 case TCG_COND_GT:
355 case TCG_COND_LTU:
356 case TCG_COND_LT:
357 case TCG_COND_GTU:
358 case TCG_COND_NE:
359 return 0;
360 case TCG_COND_GE:
361 case TCG_COND_GEU:
362 case TCG_COND_LE:
363 case TCG_COND_LEU:
364 case TCG_COND_EQ:
365 return 1;
366 default:
367 tcg_abort();
368 }
369}
370
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200371/* Return 2 if the condition can't be simplified, and the result
372 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200373static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
374 TCGArg y, TCGCond c)
375{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200376 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
377 switch (op_bits(op)) {
378 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700379 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200380 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700381 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
382 default:
383 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200384 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200385 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700386 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200387 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
388 switch (c) {
389 case TCG_COND_LTU:
390 return 0;
391 case TCG_COND_GEU:
392 return 1;
393 default:
394 return 2;
395 }
396 } else {
397 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200398 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200399}
400
Richard Henderson24c9ae42012-10-02 11:32:21 -0700401static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
402{
403 TCGArg a1 = *p1, a2 = *p2;
404 int sum = 0;
405 sum += temps[a1].state == TCG_TEMP_CONST;
406 sum -= temps[a2].state == TCG_TEMP_CONST;
407
408 /* Prefer the constant in second argument, and then the form
409 op a, a, b, which is better handled on non-RISC hosts. */
410 if (sum > 0 || (sum == 0 && dest == a2)) {
411 *p1 = a2;
412 *p2 = a1;
413 return true;
414 }
415 return false;
416}
417
Richard Henderson0bfcb862012-10-02 11:32:23 -0700418static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
419{
420 int sum = 0;
421 sum += temps[p1[0]].state == TCG_TEMP_CONST;
422 sum += temps[p1[1]].state == TCG_TEMP_CONST;
423 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
424 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
425 if (sum > 0) {
426 TCGArg t;
427 t = p1[0], p1[0] = p2[0], p2[0] = t;
428 t = p1[1], p1[1] = p2[1], p2[1] = t;
429 return true;
430 }
431 return false;
432}
433
Kirill Batuzov22613af2011-07-07 16:37:13 +0400434/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400435static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
436 TCGArg *args, TCGOpDef *tcg_op_defs)
437{
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000438 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
439 TCGOpcode op;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400440 const TCGOpDef *def;
441 TCGArg *gen_args;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400442 TCGArg tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700443
Kirill Batuzov22613af2011-07-07 16:37:13 +0400444 /* Array VALS has an element for each temp.
445 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200446 If this temp is a copy of other ones then the other copies are
447 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400448
449 nb_temps = s->nb_temps;
450 nb_globals = s->nb_globals;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400451 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400452
453 nb_ops = tcg_opc_ptr - gen_opc_buf;
454 gen_args = args;
455 for (op_index = 0; op_index < nb_ops; op_index++) {
456 op = gen_opc_buf[op_index];
457 def = &tcg_op_defs[op];
Kirill Batuzov22613af2011-07-07 16:37:13 +0400458 /* Do copy propagation */
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200459 if (op == INDEX_op_call) {
460 int nb_oargs = args[0] >> 16;
461 int nb_iargs = args[0] & 0xffff;
462 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
463 if (temps[args[i]].state == TCG_TEMP_COPY) {
464 args[i] = find_better_copy(s, args[i]);
465 }
466 }
467 } else {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400468 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
469 if (temps[args[i]].state == TCG_TEMP_COPY) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200470 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400471 }
472 }
473 }
474
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400475 /* For commutative operations make constant second argument */
476 switch (op) {
477 CASE_OP_32_64(add):
478 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400479 CASE_OP_32_64(and):
480 CASE_OP_32_64(or):
481 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700482 CASE_OP_32_64(eqv):
483 CASE_OP_32_64(nand):
484 CASE_OP_32_64(nor):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700485 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400486 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200487 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700488 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200489 args[2] = tcg_swap_cond(args[2]);
490 }
491 break;
492 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700493 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200494 args[3] = tcg_swap_cond(args[3]);
495 }
496 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700497 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700498 if (swap_commutative(-1, &args[1], &args[2])) {
499 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700500 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700501 /* For movcond, we canonicalize the "false" input reg to match
502 the destination reg so that the tcg backend can implement
503 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700504 if (swap_commutative(args[0], &args[4], &args[3])) {
505 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700506 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700507 break;
508 case INDEX_op_add2_i32:
509 swap_commutative(args[0], &args[2], &args[4]);
510 swap_commutative(args[1], &args[3], &args[5]);
511 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700512 case INDEX_op_brcond2_i32:
513 if (swap_commutative2(&args[0], &args[2])) {
514 args[4] = tcg_swap_cond(args[4]);
515 }
516 break;
517 case INDEX_op_setcond2_i32:
518 if (swap_commutative2(&args[1], &args[3])) {
519 args[5] = tcg_swap_cond(args[5]);
520 }
521 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400522 default:
523 break;
524 }
525
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200526 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
527 switch (op) {
528 CASE_OP_32_64(shl):
529 CASE_OP_32_64(shr):
530 CASE_OP_32_64(sar):
531 CASE_OP_32_64(rotl):
532 CASE_OP_32_64(rotr):
533 if (temps[args[1]].state == TCG_TEMP_CONST
534 && temps[args[1]].val == 0) {
535 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200536 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200537 args += 3;
538 gen_args += 2;
539 continue;
540 }
541 break;
542 default:
543 break;
544 }
545
Aurelien Jarno56e49432012-09-06 16:47:13 +0200546 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400547 switch (op) {
548 CASE_OP_32_64(add):
549 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400550 CASE_OP_32_64(shl):
551 CASE_OP_32_64(shr):
552 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700553 CASE_OP_32_64(rotl):
554 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200555 CASE_OP_32_64(or):
556 CASE_OP_32_64(xor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400557 if (temps[args[1]].state == TCG_TEMP_CONST) {
558 /* Proceed with possible constant folding. */
559 break;
560 }
561 if (temps[args[2]].state == TCG_TEMP_CONST
562 && temps[args[2]].val == 0) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200563 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400564 gen_opc_buf[op_index] = INDEX_op_nop;
565 } else {
566 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200567 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400568 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400569 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200570 args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400571 continue;
572 }
573 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200574 default:
575 break;
576 }
577
578 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
579 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200580 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400581 CASE_OP_32_64(mul):
582 if ((temps[args[2]].state == TCG_TEMP_CONST
583 && temps[args[2]].val == 0)) {
584 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200585 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400586 args += 3;
587 gen_args += 2;
588 continue;
589 }
590 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200591 default:
592 break;
593 }
594
595 /* Simplify expression for "op r, a, a => mov r, a" cases */
596 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400597 CASE_OP_32_64(or):
598 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200599 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200600 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400601 gen_opc_buf[op_index] = INDEX_op_nop;
602 } else {
603 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200604 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400605 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400606 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200607 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400608 continue;
609 }
610 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000611 default:
612 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400613 }
614
Aurelien Jarno3c941932012-09-18 19:12:36 +0200615 /* Simplify expression for "op r, a, a => movi r, 0" cases */
616 switch (op) {
617 CASE_OP_32_64(sub):
618 CASE_OP_32_64(xor):
619 if (temps_are_copies(args[1], args[2])) {
620 gen_opc_buf[op_index] = op_to_movi(op);
621 tcg_opt_gen_movi(gen_args, args[0], 0);
622 gen_args += 2;
623 args += 3;
624 continue;
625 }
626 break;
627 default:
628 break;
629 }
630
Kirill Batuzov22613af2011-07-07 16:37:13 +0400631 /* Propagate constants through copy operations and do constant
632 folding. Constants will be substituted to arguments by register
633 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400634 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400635 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200636 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400637 args += 2;
638 gen_opc_buf[op_index] = INDEX_op_nop;
639 break;
640 }
641 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200642 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400643 gen_args += 2;
644 args += 2;
645 break;
646 }
647 /* Source argument is constant. Rewrite the operation and
648 let movi case handle it. */
649 op = op_to_movi(op);
650 gen_opc_buf[op_index] = op;
651 args[1] = temps[args[1]].val;
652 /* fallthrough */
653 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200654 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400655 gen_args += 2;
656 args += 2;
657 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700658
Kirill Batuzova640f032011-07-07 16:37:17 +0400659 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700660 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700661 CASE_OP_32_64(ext8s):
662 CASE_OP_32_64(ext8u):
663 CASE_OP_32_64(ext16s):
664 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400665 case INDEX_op_ext32s_i64:
666 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400667 if (temps[args[1]].state == TCG_TEMP_CONST) {
668 gen_opc_buf[op_index] = op_to_movi(op);
669 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200670 tcg_opt_gen_movi(gen_args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -0700671 gen_args += 2;
672 args += 2;
673 break;
Kirill Batuzova640f032011-07-07 16:37:17 +0400674 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700675 goto do_default;
676
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400677 CASE_OP_32_64(add):
678 CASE_OP_32_64(sub):
679 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400680 CASE_OP_32_64(or):
681 CASE_OP_32_64(and):
682 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400683 CASE_OP_32_64(shl):
684 CASE_OP_32_64(shr):
685 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700686 CASE_OP_32_64(rotl):
687 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700688 CASE_OP_32_64(andc):
689 CASE_OP_32_64(orc):
690 CASE_OP_32_64(eqv):
691 CASE_OP_32_64(nand):
692 CASE_OP_32_64(nor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400693 if (temps[args[1]].state == TCG_TEMP_CONST
694 && temps[args[2]].state == TCG_TEMP_CONST) {
695 gen_opc_buf[op_index] = op_to_movi(op);
696 tmp = do_constant_folding(op, temps[args[1]].val,
697 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200698 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400699 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700700 args += 3;
701 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400702 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700703 goto do_default;
704
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200705 CASE_OP_32_64(deposit):
706 if (temps[args[1]].state == TCG_TEMP_CONST
707 && temps[args[2]].state == TCG_TEMP_CONST) {
708 gen_opc_buf[op_index] = op_to_movi(op);
709 tmp = ((1ull << args[4]) - 1);
710 tmp = (temps[args[1]].val & ~(tmp << args[3]))
711 | ((temps[args[2]].val & tmp) << args[3]);
712 tcg_opt_gen_movi(gen_args, args[0], tmp);
713 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700714 args += 5;
715 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +0200716 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700717 goto do_default;
718
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200719 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200720 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
721 if (tmp != 2) {
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200722 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200723 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200724 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -0700725 args += 4;
726 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200727 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700728 goto do_default;
729
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200730 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200731 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
732 if (tmp != 2) {
733 if (tmp) {
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200734 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
735 gen_opc_buf[op_index] = INDEX_op_br;
736 gen_args[0] = args[3];
737 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200738 } else {
739 gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200740 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700741 args += 4;
742 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200743 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700744 goto do_default;
745
Richard Hendersonfa01a202012-09-21 10:13:37 -0700746 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200747 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
748 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200749 if (temps_are_copies(args[0], args[4-tmp])) {
Richard Hendersonfa01a202012-09-21 10:13:37 -0700750 gen_opc_buf[op_index] = INDEX_op_nop;
751 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
752 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200753 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700754 gen_args += 2;
755 } else {
756 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200757 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700758 gen_args += 2;
759 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700760 args += 6;
761 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700762 }
Richard Henderson6e14e912012-10-02 11:32:24 -0700763 goto do_default;
764
Richard Hendersonbc1473e2012-10-02 11:32:25 -0700765 case INDEX_op_brcond2_i32:
766 /* Simplify LT/GE comparisons vs zero to a single compare
767 vs the high word of the input. */
768 if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
769 && temps[args[2]].state == TCG_TEMP_CONST
770 && temps[args[3]].state == TCG_TEMP_CONST
771 && temps[args[2]].val == 0
772 && temps[args[3]].val == 0) {
773 gen_opc_buf[op_index] = INDEX_op_brcond_i32;
774 gen_args[0] = args[1];
775 gen_args[1] = args[3];
776 gen_args[2] = args[4];
777 gen_args[3] = args[5];
778 gen_args += 4;
779 args += 6;
780 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
781 break;
782 }
783 goto do_default;
784
785 case INDEX_op_setcond2_i32:
786 /* Simplify LT/GE comparisons vs zero to a single compare
787 vs the high word of the input. */
788 if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
789 && temps[args[3]].state == TCG_TEMP_CONST
790 && temps[args[4]].state == TCG_TEMP_CONST
791 && temps[args[3]].val == 0
792 && temps[args[4]].val == 0) {
793 gen_opc_buf[op_index] = INDEX_op_setcond_i32;
794 gen_args[0] = args[0];
795 gen_args[1] = args[2];
796 gen_args[2] = args[4];
797 gen_args[3] = args[5];
798 gen_args += 4;
799 args += 6;
800 break;
801 }
802 goto do_default;
803
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400804 case INDEX_op_call:
Kirill Batuzov22613af2011-07-07 16:37:13 +0400805 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
806 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
807 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200808 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400809 }
810 }
811 for (i = 0; i < (args[0] >> 16); i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200812 reset_temp(args[i + 1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400813 }
814 i = nb_call_args + 3;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400815 while (i) {
816 *gen_args = *args;
817 args++;
818 gen_args++;
819 i--;
820 }
821 break;
Richard Henderson6e14e912012-10-02 11:32:24 -0700822
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400823 default:
Richard Henderson6e14e912012-10-02 11:32:24 -0700824 do_default:
825 /* Default case: we know nothing about operation (or were unable
826 to compute the operation result) so no propagation is done.
827 We trash everything if the operation is the end of a basic
828 block, otherwise we only trash the output args. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +0200829 if (def->flags & TCG_OPF_BB_END) {
830 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
831 } else {
832 for (i = 0; i < def->nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200833 reset_temp(args[i]);
Aurelien Jarnoa2550662012-09-19 21:40:30 +0200834 }
Kirill Batuzov22613af2011-07-07 16:37:13 +0400835 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400836 for (i = 0; i < def->nb_args; i++) {
837 gen_args[i] = args[i];
838 }
839 args += def->nb_args;
840 gen_args += def->nb_args;
841 break;
842 }
843 }
844
845 return gen_args;
846}
847
848TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
849 TCGArg *args, TCGOpDef *tcg_op_defs)
850{
851 TCGArg *res;
852 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
853 return res;
854}