blob: abe016a28afc2f57260c2e33ce4f9c31cb664904 [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
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200295/* Return 2 if the condition can't be simplified, and the result
296 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200297static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
298 TCGArg y, TCGCond c)
299{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200300 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
301 switch (op_bits(op)) {
302 case 32:
303 switch (c) {
304 case TCG_COND_EQ:
305 return (uint32_t)temps[x].val == (uint32_t)temps[y].val;
306 case TCG_COND_NE:
307 return (uint32_t)temps[x].val != (uint32_t)temps[y].val;
308 case TCG_COND_LT:
309 return (int32_t)temps[x].val < (int32_t)temps[y].val;
310 case TCG_COND_GE:
311 return (int32_t)temps[x].val >= (int32_t)temps[y].val;
312 case TCG_COND_LE:
313 return (int32_t)temps[x].val <= (int32_t)temps[y].val;
314 case TCG_COND_GT:
315 return (int32_t)temps[x].val > (int32_t)temps[y].val;
316 case TCG_COND_LTU:
317 return (uint32_t)temps[x].val < (uint32_t)temps[y].val;
318 case TCG_COND_GEU:
319 return (uint32_t)temps[x].val >= (uint32_t)temps[y].val;
320 case TCG_COND_LEU:
321 return (uint32_t)temps[x].val <= (uint32_t)temps[y].val;
322 case TCG_COND_GTU:
323 return (uint32_t)temps[x].val > (uint32_t)temps[y].val;
324 }
325 break;
326 case 64:
327 switch (c) {
328 case TCG_COND_EQ:
329 return (uint64_t)temps[x].val == (uint64_t)temps[y].val;
330 case TCG_COND_NE:
331 return (uint64_t)temps[x].val != (uint64_t)temps[y].val;
332 case TCG_COND_LT:
333 return (int64_t)temps[x].val < (int64_t)temps[y].val;
334 case TCG_COND_GE:
335 return (int64_t)temps[x].val >= (int64_t)temps[y].val;
336 case TCG_COND_LE:
337 return (int64_t)temps[x].val <= (int64_t)temps[y].val;
338 case TCG_COND_GT:
339 return (int64_t)temps[x].val > (int64_t)temps[y].val;
340 case TCG_COND_LTU:
341 return (uint64_t)temps[x].val < (uint64_t)temps[y].val;
342 case TCG_COND_GEU:
343 return (uint64_t)temps[x].val >= (uint64_t)temps[y].val;
344 case TCG_COND_LEU:
345 return (uint64_t)temps[x].val <= (uint64_t)temps[y].val;
346 case TCG_COND_GTU:
347 return (uint64_t)temps[x].val > (uint64_t)temps[y].val;
348 }
349 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200350 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200351 } else if (temps_are_copies(x, y)) {
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200352 switch (c) {
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200353 case TCG_COND_GT:
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200354 case TCG_COND_LTU:
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200355 case TCG_COND_LT:
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200356 case TCG_COND_GTU:
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200357 case TCG_COND_NE:
358 return 0;
359 case TCG_COND_GE:
360 case TCG_COND_GEU:
361 case TCG_COND_LE:
362 case TCG_COND_LEU:
363 case TCG_COND_EQ:
364 return 1;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200365 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200366 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
367 switch (c) {
368 case TCG_COND_LTU:
369 return 0;
370 case TCG_COND_GEU:
371 return 1;
372 default:
373 return 2;
374 }
375 } else {
376 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200377 }
378
379 fprintf(stderr,
380 "Unrecognized bitness %d or condition %d in "
381 "do_constant_folding_cond.\n", op_bits(op), c);
382 tcg_abort();
383}
384
Kirill Batuzov22613af2011-07-07 16:37:13 +0400385/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400386static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
387 TCGArg *args, TCGOpDef *tcg_op_defs)
388{
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000389 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
390 TCGOpcode op;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400391 const TCGOpDef *def;
392 TCGArg *gen_args;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400393 TCGArg tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700394 TCGCond cond;
395
Kirill Batuzov22613af2011-07-07 16:37:13 +0400396 /* Array VALS has an element for each temp.
397 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200398 If this temp is a copy of other ones then the other copies are
399 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400400
401 nb_temps = s->nb_temps;
402 nb_globals = s->nb_globals;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400403 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400404
405 nb_ops = tcg_opc_ptr - gen_opc_buf;
406 gen_args = args;
407 for (op_index = 0; op_index < nb_ops; op_index++) {
408 op = gen_opc_buf[op_index];
409 def = &tcg_op_defs[op];
Kirill Batuzov22613af2011-07-07 16:37:13 +0400410 /* Do copy propagation */
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200411 if (op == INDEX_op_call) {
412 int nb_oargs = args[0] >> 16;
413 int nb_iargs = args[0] & 0xffff;
414 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
415 if (temps[args[i]].state == TCG_TEMP_COPY) {
416 args[i] = find_better_copy(s, args[i]);
417 }
418 }
419 } else {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400420 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
421 if (temps[args[i]].state == TCG_TEMP_COPY) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200422 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400423 }
424 }
425 }
426
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400427 /* For commutative operations make constant second argument */
428 switch (op) {
429 CASE_OP_32_64(add):
430 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400431 CASE_OP_32_64(and):
432 CASE_OP_32_64(or):
433 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700434 CASE_OP_32_64(eqv):
435 CASE_OP_32_64(nand):
436 CASE_OP_32_64(nor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400437 if (temps[args[1]].state == TCG_TEMP_CONST) {
438 tmp = args[1];
439 args[1] = args[2];
440 args[2] = tmp;
441 }
442 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200443 CASE_OP_32_64(brcond):
444 if (temps[args[0]].state == TCG_TEMP_CONST
445 && temps[args[1]].state != TCG_TEMP_CONST) {
446 tmp = args[0];
447 args[0] = args[1];
448 args[1] = tmp;
449 args[2] = tcg_swap_cond(args[2]);
450 }
451 break;
452 CASE_OP_32_64(setcond):
453 if (temps[args[1]].state == TCG_TEMP_CONST
454 && temps[args[2]].state != TCG_TEMP_CONST) {
455 tmp = args[1];
456 args[1] = args[2];
457 args[2] = tmp;
458 args[3] = tcg_swap_cond(args[3]);
459 }
460 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700461 CASE_OP_32_64(movcond):
Richard Henderson5d8f5362012-09-21 10:13:38 -0700462 cond = args[5];
Richard Hendersonfa01a202012-09-21 10:13:37 -0700463 if (temps[args[1]].state == TCG_TEMP_CONST
464 && temps[args[2]].state != TCG_TEMP_CONST) {
465 tmp = args[1];
466 args[1] = args[2];
467 args[2] = tmp;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700468 cond = tcg_swap_cond(cond);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700469 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700470 /* For movcond, we canonicalize the "false" input reg to match
471 the destination reg so that the tcg backend can implement
472 a "move if true" operation. */
473 if (args[0] == args[3]) {
474 tmp = args[3];
475 args[3] = args[4];
476 args[4] = tmp;
477 cond = tcg_invert_cond(cond);
478 }
479 args[5] = cond;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400480 default:
481 break;
482 }
483
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200484 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
485 switch (op) {
486 CASE_OP_32_64(shl):
487 CASE_OP_32_64(shr):
488 CASE_OP_32_64(sar):
489 CASE_OP_32_64(rotl):
490 CASE_OP_32_64(rotr):
491 if (temps[args[1]].state == TCG_TEMP_CONST
492 && temps[args[1]].val == 0) {
493 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200494 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200495 args += 3;
496 gen_args += 2;
497 continue;
498 }
499 break;
500 default:
501 break;
502 }
503
Aurelien Jarno56e49432012-09-06 16:47:13 +0200504 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400505 switch (op) {
506 CASE_OP_32_64(add):
507 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400508 CASE_OP_32_64(shl):
509 CASE_OP_32_64(shr):
510 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700511 CASE_OP_32_64(rotl):
512 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200513 CASE_OP_32_64(or):
514 CASE_OP_32_64(xor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400515 if (temps[args[1]].state == TCG_TEMP_CONST) {
516 /* Proceed with possible constant folding. */
517 break;
518 }
519 if (temps[args[2]].state == TCG_TEMP_CONST
520 && temps[args[2]].val == 0) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200521 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400522 gen_opc_buf[op_index] = INDEX_op_nop;
523 } else {
524 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200525 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400526 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400527 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200528 args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400529 continue;
530 }
531 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200532 default:
533 break;
534 }
535
536 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
537 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200538 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400539 CASE_OP_32_64(mul):
540 if ((temps[args[2]].state == TCG_TEMP_CONST
541 && temps[args[2]].val == 0)) {
542 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200543 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400544 args += 3;
545 gen_args += 2;
546 continue;
547 }
548 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200549 default:
550 break;
551 }
552
553 /* Simplify expression for "op r, a, a => mov r, a" cases */
554 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400555 CASE_OP_32_64(or):
556 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200557 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200558 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400559 gen_opc_buf[op_index] = INDEX_op_nop;
560 } else {
561 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200562 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400563 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400564 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200565 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400566 continue;
567 }
568 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000569 default:
570 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400571 }
572
Aurelien Jarno3c941932012-09-18 19:12:36 +0200573 /* Simplify expression for "op r, a, a => movi r, 0" cases */
574 switch (op) {
575 CASE_OP_32_64(sub):
576 CASE_OP_32_64(xor):
577 if (temps_are_copies(args[1], args[2])) {
578 gen_opc_buf[op_index] = op_to_movi(op);
579 tcg_opt_gen_movi(gen_args, args[0], 0);
580 gen_args += 2;
581 args += 3;
582 continue;
583 }
584 break;
585 default:
586 break;
587 }
588
Kirill Batuzov22613af2011-07-07 16:37:13 +0400589 /* Propagate constants through copy operations and do constant
590 folding. Constants will be substituted to arguments by register
591 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400592 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400593 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200594 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400595 args += 2;
596 gen_opc_buf[op_index] = INDEX_op_nop;
597 break;
598 }
599 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200600 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400601 gen_args += 2;
602 args += 2;
603 break;
604 }
605 /* Source argument is constant. Rewrite the operation and
606 let movi case handle it. */
607 op = op_to_movi(op);
608 gen_opc_buf[op_index] = op;
609 args[1] = temps[args[1]].val;
610 /* fallthrough */
611 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200612 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400613 gen_args += 2;
614 args += 2;
615 break;
Kirill Batuzova640f032011-07-07 16:37:17 +0400616 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700617 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700618 CASE_OP_32_64(ext8s):
619 CASE_OP_32_64(ext8u):
620 CASE_OP_32_64(ext16s):
621 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400622 case INDEX_op_ext32s_i64:
623 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400624 if (temps[args[1]].state == TCG_TEMP_CONST) {
625 gen_opc_buf[op_index] = op_to_movi(op);
626 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200627 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzova640f032011-07-07 16:37:17 +0400628 } else {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200629 reset_temp(args[0]);
Kirill Batuzova640f032011-07-07 16:37:17 +0400630 gen_args[0] = args[0];
631 gen_args[1] = args[1];
Kirill Batuzova640f032011-07-07 16:37:17 +0400632 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200633 gen_args += 2;
634 args += 2;
635 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400636 CASE_OP_32_64(add):
637 CASE_OP_32_64(sub):
638 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400639 CASE_OP_32_64(or):
640 CASE_OP_32_64(and):
641 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400642 CASE_OP_32_64(shl):
643 CASE_OP_32_64(shr):
644 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700645 CASE_OP_32_64(rotl):
646 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700647 CASE_OP_32_64(andc):
648 CASE_OP_32_64(orc):
649 CASE_OP_32_64(eqv):
650 CASE_OP_32_64(nand):
651 CASE_OP_32_64(nor):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400652 if (temps[args[1]].state == TCG_TEMP_CONST
653 && temps[args[2]].state == TCG_TEMP_CONST) {
654 gen_opc_buf[op_index] = op_to_movi(op);
655 tmp = do_constant_folding(op, temps[args[1]].val,
656 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200657 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400658 gen_args += 2;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400659 } else {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200660 reset_temp(args[0]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400661 gen_args[0] = args[0];
662 gen_args[1] = args[1];
663 gen_args[2] = args[2];
664 gen_args += 3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400665 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200666 args += 3;
667 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200668 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200669 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
670 if (tmp != 2) {
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200671 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200672 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200673 gen_args += 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200674 } else {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200675 reset_temp(args[0]);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200676 gen_args[0] = args[0];
677 gen_args[1] = args[1];
678 gen_args[2] = args[2];
679 gen_args[3] = args[3];
680 gen_args += 4;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200681 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200682 args += 4;
683 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200684 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200685 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
686 if (tmp != 2) {
687 if (tmp) {
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200688 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
689 gen_opc_buf[op_index] = INDEX_op_br;
690 gen_args[0] = args[3];
691 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200692 } else {
693 gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200694 }
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200695 } else {
696 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200697 reset_temp(args[0]);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200698 gen_args[0] = args[0];
699 gen_args[1] = args[1];
700 gen_args[2] = args[2];
701 gen_args[3] = args[3];
702 gen_args += 4;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +0200703 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200704 args += 4;
705 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700706 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200707 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
708 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200709 if (temps_are_copies(args[0], args[4-tmp])) {
Richard Hendersonfa01a202012-09-21 10:13:37 -0700710 gen_opc_buf[op_index] = INDEX_op_nop;
711 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
712 gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200713 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700714 gen_args += 2;
715 } else {
716 gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200717 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700718 gen_args += 2;
719 }
720 } else {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200721 reset_temp(args[0]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700722 gen_args[0] = args[0];
723 gen_args[1] = args[1];
724 gen_args[2] = args[2];
725 gen_args[3] = args[3];
726 gen_args[4] = args[4];
727 gen_args[5] = args[5];
728 gen_args += 6;
729 }
730 args += 6;
731 break;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400732 case INDEX_op_call:
Kirill Batuzov22613af2011-07-07 16:37:13 +0400733 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
734 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
735 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200736 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400737 }
738 }
739 for (i = 0; i < (args[0] >> 16); i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200740 reset_temp(args[i + 1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400741 }
742 i = nb_call_args + 3;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400743 while (i) {
744 *gen_args = *args;
745 args++;
746 gen_args++;
747 i--;
748 }
749 break;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400750 default:
Kirill Batuzov22613af2011-07-07 16:37:13 +0400751 /* Default case: we do know nothing about operation so no
Aurelien Jarnoa2550662012-09-19 21:40:30 +0200752 propagation is done. We trash everything if the operation
753 is the end of a basic block, otherwise we only trash the
754 output args. */
755 if (def->flags & TCG_OPF_BB_END) {
756 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
757 } else {
758 for (i = 0; i < def->nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200759 reset_temp(args[i]);
Aurelien Jarnoa2550662012-09-19 21:40:30 +0200760 }
Kirill Batuzov22613af2011-07-07 16:37:13 +0400761 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400762 for (i = 0; i < def->nb_args; i++) {
763 gen_args[i] = args[i];
764 }
765 args += def->nb_args;
766 gen_args += def->nb_args;
767 break;
768 }
769 }
770
771 return gen_args;
772}
773
774TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
775 TCGArg *args, TCGOpDef *tcg_op_defs)
776{
777 TCGArg *res;
778 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
779 return res;
780}