blob: 3a504a19615d4d687e53dd83c177aafe97ef6e70 [file] [log] [blame]
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001/*
2 * Optimizations for Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "config.h"
27
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "qemu-common.h"
32#include "tcg-op.h"
33
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040034#define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040037
Kirill Batuzov22613af2011-07-07 16:37:13 +040038typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
Kirill Batuzov22613af2011-07-07 16:37:13 +040042} tcg_temp_state;
43
44struct tcg_temp_info {
45 tcg_temp_state state;
46 uint16_t prev_copy;
47 uint16_t next_copy;
48 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080049 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040050};
51
52static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020054/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
55 the copy flag from the left temp. */
56static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040057{
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020058 if (temps[temp].state == TCG_TEMP_COPY) {
59 if (temps[temp].prev_copy == temps[temp].next_copy) {
60 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
61 } else {
62 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
63 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
Kirill Batuzov22613af2011-07-07 16:37:13 +040064 }
Kirill Batuzov22613af2011-07-07 16:37:13 +040065 }
Aurelien Jarno48b56ce2012-09-10 23:51:42 +020066 temps[temp].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080067 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040068}
69
Paolo Bonzinid193a142013-01-11 15:42:51 -080070/* Reset all temporaries, given that there are NB_TEMPS of them. */
71static void reset_all_temps(int nb_temps)
72{
73 int i;
74 for (i = 0; i < nb_temps; i++) {
75 temps[i].state = TCG_TEMP_UNDEF;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080076 temps[i].mask = -1;
Paolo Bonzinid193a142013-01-11 15:42:51 -080077 }
78}
79
Blue Swirlfe0de7a2011-07-30 19:18:32 +000080static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040081{
Richard Henderson8399ad52011-08-17 14:11:45 -070082 const TCGOpDef *def = &tcg_op_defs[op];
83 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040084}
85
Blue Swirlfe0de7a2011-07-30 19:18:32 +000086static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +040087{
88 switch (op_bits(op)) {
89 case 32:
90 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +040091 case 64:
92 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +040093 default:
94 fprintf(stderr, "op_to_movi: unexpected return value of "
95 "function op_bits.\n");
96 tcg_abort();
97 }
98}
99
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200100static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
101{
102 TCGArg i;
103
104 /* If this is already a global, we can't do better. */
105 if (temp < s->nb_globals) {
106 return temp;
107 }
108
109 /* Search for a global first. */
110 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
111 if (i < s->nb_globals) {
112 return i;
113 }
114 }
115
116 /* If it is a temp, search for a temp local. */
117 if (!s->temps[temp].temp_local) {
118 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
119 if (s->temps[i].temp_local) {
120 return i;
121 }
122 }
123 }
124
125 /* Failure to find a better representation, return the same temp. */
126 return temp;
127}
128
129static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
130{
131 TCGArg i;
132
133 if (arg1 == arg2) {
134 return true;
135 }
136
137 if (temps[arg1].state != TCG_TEMP_COPY
138 || temps[arg2].state != TCG_TEMP_COPY) {
139 return false;
140 }
141
142 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
143 if (i == arg2) {
144 return true;
145 }
146 }
147
148 return false;
149}
150
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200151static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
152 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400153{
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800154 reset_temp(dst);
155 temps[dst].mask = temps[src].mask;
156 assert(temps[src].state != TCG_TEMP_CONST);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200157
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800158 if (s->temps[src].type == s->temps[dst].type) {
159 if (temps[src].state != TCG_TEMP_COPY) {
160 temps[src].state = TCG_TEMP_COPY;
161 temps[src].next_copy = src;
162 temps[src].prev_copy = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400163 }
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800164 temps[dst].state = TCG_TEMP_COPY;
165 temps[dst].next_copy = temps[src].next_copy;
166 temps[dst].prev_copy = src;
167 temps[temps[dst].next_copy].prev_copy = dst;
168 temps[src].next_copy = dst;
169 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200170
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800171 gen_args[0] = dst;
172 gen_args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400173}
174
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200175static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400176{
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800177 reset_temp(dst);
178 temps[dst].state = TCG_TEMP_CONST;
179 temps[dst].val = val;
180 temps[dst].mask = val;
181 gen_args[0] = dst;
182 gen_args[1] = val;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400183}
184
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000185static TCGOpcode op_to_mov(TCGOpcode op)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400186{
187 switch (op_bits(op)) {
188 case 32:
189 return INDEX_op_mov_i32;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400190 case 64:
191 return INDEX_op_mov_i64;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400192 default:
193 fprintf(stderr, "op_to_mov: unexpected return value of "
194 "function op_bits.\n");
195 tcg_abort();
196 }
197}
198
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000199static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400200{
Richard Henderson03271522013-08-14 14:35:56 -0700201 uint64_t l64, h64;
202
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400203 switch (op) {
204 CASE_OP_32_64(add):
205 return x + y;
206
207 CASE_OP_32_64(sub):
208 return x - y;
209
210 CASE_OP_32_64(mul):
211 return x * y;
212
Kirill Batuzov9a810902011-07-07 16:37:15 +0400213 CASE_OP_32_64(and):
214 return x & y;
215
216 CASE_OP_32_64(or):
217 return x | y;
218
219 CASE_OP_32_64(xor):
220 return x ^ y;
221
Kirill Batuzov55c09752011-07-07 16:37:16 +0400222 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700223 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400224
Kirill Batuzov55c09752011-07-07 16:37:16 +0400225 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700226 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400227
228 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700229 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400230
Richard Henderson4bb7a412013-09-09 17:03:24 -0700231 case INDEX_op_trunc_shr_i32:
Kirill Batuzov55c09752011-07-07 16:37:16 +0400232 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700233 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400234
235 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700236 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400237
Kirill Batuzov55c09752011-07-07 16:37:16 +0400238 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700239 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400240
241 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700242 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400243
Kirill Batuzov55c09752011-07-07 16:37:16 +0400244 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700245 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400246
247 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700248 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400249
Kirill Batuzov55c09752011-07-07 16:37:16 +0400250 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700251 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400252
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700253 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400254 return ~x;
255
Richard Hendersoncb25c802011-08-17 14:11:47 -0700256 CASE_OP_32_64(neg):
257 return -x;
258
259 CASE_OP_32_64(andc):
260 return x & ~y;
261
262 CASE_OP_32_64(orc):
263 return x | ~y;
264
265 CASE_OP_32_64(eqv):
266 return ~(x ^ y);
267
268 CASE_OP_32_64(nand):
269 return ~(x & y);
270
271 CASE_OP_32_64(nor):
272 return ~(x | y);
273
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700274 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400275 return (int8_t)x;
276
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700277 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400278 return (int16_t)x;
279
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700280 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400281 return (uint8_t)x;
282
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700283 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400284 return (uint16_t)x;
285
Kirill Batuzova640f032011-07-07 16:37:17 +0400286 case INDEX_op_ext32s_i64:
287 return (int32_t)x;
288
289 case INDEX_op_ext32u_i64:
290 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400291
Richard Henderson03271522013-08-14 14:35:56 -0700292 case INDEX_op_muluh_i32:
293 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
294 case INDEX_op_mulsh_i32:
295 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
296
297 case INDEX_op_muluh_i64:
298 mulu64(&l64, &h64, x, y);
299 return h64;
300 case INDEX_op_mulsh_i64:
301 muls64(&l64, &h64, x, y);
302 return h64;
303
Richard Henderson01547f72013-08-14 15:22:46 -0700304 case INDEX_op_div_i32:
305 /* Avoid crashing on divide by zero, otherwise undefined. */
306 return (int32_t)x / ((int32_t)y ? : 1);
307 case INDEX_op_divu_i32:
308 return (uint32_t)x / ((uint32_t)y ? : 1);
309 case INDEX_op_div_i64:
310 return (int64_t)x / ((int64_t)y ? : 1);
311 case INDEX_op_divu_i64:
312 return (uint64_t)x / ((uint64_t)y ? : 1);
313
314 case INDEX_op_rem_i32:
315 return (int32_t)x % ((int32_t)y ? : 1);
316 case INDEX_op_remu_i32:
317 return (uint32_t)x % ((uint32_t)y ? : 1);
318 case INDEX_op_rem_i64:
319 return (int64_t)x % ((int64_t)y ? : 1);
320 case INDEX_op_remu_i64:
321 return (uint64_t)x % ((uint64_t)y ? : 1);
322
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400323 default:
324 fprintf(stderr,
325 "Unrecognized operation %d in do_constant_folding.\n", op);
326 tcg_abort();
327 }
328}
329
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000330static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400331{
332 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400333 if (op_bits(op) == 32) {
334 res &= 0xffffffff;
335 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400336 return res;
337}
338
Richard Henderson9519da72012-10-02 11:32:26 -0700339static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
340{
341 switch (c) {
342 case TCG_COND_EQ:
343 return x == y;
344 case TCG_COND_NE:
345 return x != y;
346 case TCG_COND_LT:
347 return (int32_t)x < (int32_t)y;
348 case TCG_COND_GE:
349 return (int32_t)x >= (int32_t)y;
350 case TCG_COND_LE:
351 return (int32_t)x <= (int32_t)y;
352 case TCG_COND_GT:
353 return (int32_t)x > (int32_t)y;
354 case TCG_COND_LTU:
355 return x < y;
356 case TCG_COND_GEU:
357 return x >= y;
358 case TCG_COND_LEU:
359 return x <= y;
360 case TCG_COND_GTU:
361 return x > y;
362 default:
363 tcg_abort();
364 }
365}
366
367static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
368{
369 switch (c) {
370 case TCG_COND_EQ:
371 return x == y;
372 case TCG_COND_NE:
373 return x != y;
374 case TCG_COND_LT:
375 return (int64_t)x < (int64_t)y;
376 case TCG_COND_GE:
377 return (int64_t)x >= (int64_t)y;
378 case TCG_COND_LE:
379 return (int64_t)x <= (int64_t)y;
380 case TCG_COND_GT:
381 return (int64_t)x > (int64_t)y;
382 case TCG_COND_LTU:
383 return x < y;
384 case TCG_COND_GEU:
385 return x >= y;
386 case TCG_COND_LEU:
387 return x <= y;
388 case TCG_COND_GTU:
389 return x > y;
390 default:
391 tcg_abort();
392 }
393}
394
395static bool do_constant_folding_cond_eq(TCGCond c)
396{
397 switch (c) {
398 case TCG_COND_GT:
399 case TCG_COND_LTU:
400 case TCG_COND_LT:
401 case TCG_COND_GTU:
402 case TCG_COND_NE:
403 return 0;
404 case TCG_COND_GE:
405 case TCG_COND_GEU:
406 case TCG_COND_LE:
407 case TCG_COND_LEU:
408 case TCG_COND_EQ:
409 return 1;
410 default:
411 tcg_abort();
412 }
413}
414
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200415/* Return 2 if the condition can't be simplified, and the result
416 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200417static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
418 TCGArg y, TCGCond c)
419{
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200420 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
421 switch (op_bits(op)) {
422 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700423 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200424 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700425 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
426 default:
427 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200428 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200429 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700430 return do_constant_folding_cond_eq(c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200431 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
432 switch (c) {
433 case TCG_COND_LTU:
434 return 0;
435 case TCG_COND_GEU:
436 return 1;
437 default:
438 return 2;
439 }
440 } else {
441 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200442 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200443}
444
Richard Henderson6c4382f2012-10-02 11:32:27 -0700445/* Return 2 if the condition can't be simplified, and the result
446 of the condition (0 or 1) if it can */
447static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
448{
449 TCGArg al = p1[0], ah = p1[1];
450 TCGArg bl = p2[0], bh = p2[1];
451
452 if (temps[bl].state == TCG_TEMP_CONST
453 && temps[bh].state == TCG_TEMP_CONST) {
454 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
455
456 if (temps[al].state == TCG_TEMP_CONST
457 && temps[ah].state == TCG_TEMP_CONST) {
458 uint64_t a;
459 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
460 return do_constant_folding_cond_64(a, b, c);
461 }
462 if (b == 0) {
463 switch (c) {
464 case TCG_COND_LTU:
465 return 0;
466 case TCG_COND_GEU:
467 return 1;
468 default:
469 break;
470 }
471 }
472 }
473 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
474 return do_constant_folding_cond_eq(c);
475 }
476 return 2;
477}
478
Richard Henderson24c9ae42012-10-02 11:32:21 -0700479static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
480{
481 TCGArg a1 = *p1, a2 = *p2;
482 int sum = 0;
483 sum += temps[a1].state == TCG_TEMP_CONST;
484 sum -= temps[a2].state == TCG_TEMP_CONST;
485
486 /* Prefer the constant in second argument, and then the form
487 op a, a, b, which is better handled on non-RISC hosts. */
488 if (sum > 0 || (sum == 0 && dest == a2)) {
489 *p1 = a2;
490 *p2 = a1;
491 return true;
492 }
493 return false;
494}
495
Richard Henderson0bfcb862012-10-02 11:32:23 -0700496static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
497{
498 int sum = 0;
499 sum += temps[p1[0]].state == TCG_TEMP_CONST;
500 sum += temps[p1[1]].state == TCG_TEMP_CONST;
501 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
502 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
503 if (sum > 0) {
504 TCGArg t;
505 t = p1[0], p1[0] = p2[0], p2[0] = t;
506 t = p1[1], p1[1] = p2[1], p2[1] = t;
507 return true;
508 }
509 return false;
510}
511
Kirill Batuzov22613af2011-07-07 16:37:13 +0400512/* Propagate constants and copies, fold constant expressions. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400513static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
514 TCGArg *args, TCGOpDef *tcg_op_defs)
515{
Richard Hendersoncf066672014-03-22 20:06:52 -0700516 int nb_ops, op_index, nb_temps, nb_globals;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400517 TCGArg *gen_args;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700518
Kirill Batuzov22613af2011-07-07 16:37:13 +0400519 /* Array VALS has an element for each temp.
520 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200521 If this temp is a copy of other ones then the other copies are
522 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400523
524 nb_temps = s->nb_temps;
525 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800526 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400527
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400528 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400529 gen_args = args;
530 for (op_index = 0; op_index < nb_ops; op_index++) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700531 TCGOpcode op = s->gen_opc_buf[op_index];
532 const TCGOpDef *def = &tcg_op_defs[op];
533 tcg_target_ulong mask, affected;
534 int nb_oargs, nb_iargs, nb_args, i;
535 TCGArg tmp;
536
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200537 if (op == INDEX_op_call) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700538 *gen_args++ = tmp = *args++;
539 nb_oargs = tmp >> 16;
540 nb_iargs = tmp & 0xffff;
541 nb_args = nb_oargs + nb_iargs + def->nb_cargs;
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200542 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700543 nb_oargs = def->nb_oargs;
544 nb_iargs = def->nb_iargs;
545 nb_args = def->nb_args;
546 }
547
548 /* Do copy propagation */
549 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
550 if (temps[args[i]].state == TCG_TEMP_COPY) {
551 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400552 }
553 }
554
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400555 /* For commutative operations make constant second argument */
556 switch (op) {
557 CASE_OP_32_64(add):
558 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400559 CASE_OP_32_64(and):
560 CASE_OP_32_64(or):
561 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700562 CASE_OP_32_64(eqv):
563 CASE_OP_32_64(nand):
564 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700565 CASE_OP_32_64(muluh):
566 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700567 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400568 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200569 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700570 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200571 args[2] = tcg_swap_cond(args[2]);
572 }
573 break;
574 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700575 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200576 args[3] = tcg_swap_cond(args[3]);
577 }
578 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700579 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700580 if (swap_commutative(-1, &args[1], &args[2])) {
581 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700582 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700583 /* For movcond, we canonicalize the "false" input reg to match
584 the destination reg so that the tcg backend can implement
585 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700586 if (swap_commutative(args[0], &args[4], &args[3])) {
587 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700588 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700589 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800590 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700591 swap_commutative(args[0], &args[2], &args[4]);
592 swap_commutative(args[1], &args[3], &args[5]);
593 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800594 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800595 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700596 swap_commutative(args[0], &args[2], &args[3]);
597 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700598 case INDEX_op_brcond2_i32:
599 if (swap_commutative2(&args[0], &args[2])) {
600 args[4] = tcg_swap_cond(args[4]);
601 }
602 break;
603 case INDEX_op_setcond2_i32:
604 if (swap_commutative2(&args[1], &args[3])) {
605 args[5] = tcg_swap_cond(args[5]);
606 }
607 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400608 default:
609 break;
610 }
611
Richard Henderson2d497542013-03-21 09:13:33 -0700612 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
613 and "sub r, 0, a => neg r, a" case. */
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200614 switch (op) {
615 CASE_OP_32_64(shl):
616 CASE_OP_32_64(shr):
617 CASE_OP_32_64(sar):
618 CASE_OP_32_64(rotl):
619 CASE_OP_32_64(rotr):
620 if (temps[args[1]].state == TCG_TEMP_CONST
621 && temps[args[1]].val == 0) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400622 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200623 tcg_opt_gen_movi(gen_args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200624 args += 3;
625 gen_args += 2;
626 continue;
627 }
628 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700629 CASE_OP_32_64(sub):
630 {
631 TCGOpcode neg_op;
632 bool have_neg;
633
634 if (temps[args[2]].state == TCG_TEMP_CONST) {
635 /* Proceed with possible constant folding. */
636 break;
637 }
638 if (op == INDEX_op_sub_i32) {
639 neg_op = INDEX_op_neg_i32;
640 have_neg = TCG_TARGET_HAS_neg_i32;
641 } else {
642 neg_op = INDEX_op_neg_i64;
643 have_neg = TCG_TARGET_HAS_neg_i64;
644 }
645 if (!have_neg) {
646 break;
647 }
648 if (temps[args[1]].state == TCG_TEMP_CONST
649 && temps[args[1]].val == 0) {
650 s->gen_opc_buf[op_index] = neg_op;
651 reset_temp(args[0]);
652 gen_args[0] = args[0];
653 gen_args[1] = args[2];
654 args += 3;
655 gen_args += 2;
656 continue;
657 }
658 }
659 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800660 CASE_OP_32_64(xor):
661 CASE_OP_32_64(nand):
662 if (temps[args[1]].state != TCG_TEMP_CONST
663 && temps[args[2]].state == TCG_TEMP_CONST
664 && temps[args[2]].val == -1) {
665 i = 1;
666 goto try_not;
667 }
668 break;
669 CASE_OP_32_64(nor):
670 if (temps[args[1]].state != TCG_TEMP_CONST
671 && temps[args[2]].state == TCG_TEMP_CONST
672 && temps[args[2]].val == 0) {
673 i = 1;
674 goto try_not;
675 }
676 break;
677 CASE_OP_32_64(andc):
678 if (temps[args[2]].state != TCG_TEMP_CONST
679 && temps[args[1]].state == TCG_TEMP_CONST
680 && temps[args[1]].val == -1) {
681 i = 2;
682 goto try_not;
683 }
684 break;
685 CASE_OP_32_64(orc):
686 CASE_OP_32_64(eqv):
687 if (temps[args[2]].state != TCG_TEMP_CONST
688 && temps[args[1]].state == TCG_TEMP_CONST
689 && temps[args[1]].val == 0) {
690 i = 2;
691 goto try_not;
692 }
693 break;
694 try_not:
695 {
696 TCGOpcode not_op;
697 bool have_not;
698
699 if (def->flags & TCG_OPF_64BIT) {
700 not_op = INDEX_op_not_i64;
701 have_not = TCG_TARGET_HAS_not_i64;
702 } else {
703 not_op = INDEX_op_not_i32;
704 have_not = TCG_TARGET_HAS_not_i32;
705 }
706 if (!have_not) {
707 break;
708 }
709 s->gen_opc_buf[op_index] = not_op;
710 reset_temp(args[0]);
711 gen_args[0] = args[0];
712 gen_args[1] = args[i];
713 args += 3;
714 gen_args += 2;
715 continue;
716 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200717 default:
718 break;
719 }
720
Richard Henderson464a1442014-01-31 07:42:11 -0600721 /* Simplify expression for "op r, a, const => mov r, a" cases */
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400722 switch (op) {
723 CASE_OP_32_64(add):
724 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400725 CASE_OP_32_64(shl):
726 CASE_OP_32_64(shr):
727 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700728 CASE_OP_32_64(rotl):
729 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200730 CASE_OP_32_64(or):
731 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600732 CASE_OP_32_64(andc):
733 if (temps[args[1]].state != TCG_TEMP_CONST
734 && temps[args[2]].state == TCG_TEMP_CONST
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400735 && temps[args[2]].val == 0) {
Richard Henderson464a1442014-01-31 07:42:11 -0600736 goto do_mov3;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400737 }
738 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600739 CASE_OP_32_64(and):
740 CASE_OP_32_64(orc):
741 CASE_OP_32_64(eqv):
742 if (temps[args[1]].state != TCG_TEMP_CONST
743 && temps[args[2]].state == TCG_TEMP_CONST
744 && temps[args[2]].val == -1) {
745 goto do_mov3;
746 }
747 break;
748 do_mov3:
749 if (temps_are_copies(args[0], args[1])) {
750 s->gen_opc_buf[op_index] = INDEX_op_nop;
751 } else {
752 s->gen_opc_buf[op_index] = op_to_mov(op);
753 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
754 gen_args += 2;
755 }
756 args += 3;
757 continue;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200758 default:
759 break;
760 }
761
Aurelien Jarno30312442013-09-03 08:27:38 +0200762 /* Simplify using known-zero bits. Currently only ops with a single
763 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800764 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800765 affected = -1;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800766 switch (op) {
767 CASE_OP_32_64(ext8s):
768 if ((temps[args[1]].mask & 0x80) != 0) {
769 break;
770 }
771 CASE_OP_32_64(ext8u):
772 mask = 0xff;
773 goto and_const;
774 CASE_OP_32_64(ext16s):
775 if ((temps[args[1]].mask & 0x8000) != 0) {
776 break;
777 }
778 CASE_OP_32_64(ext16u):
779 mask = 0xffff;
780 goto and_const;
781 case INDEX_op_ext32s_i64:
782 if ((temps[args[1]].mask & 0x80000000) != 0) {
783 break;
784 }
785 case INDEX_op_ext32u_i64:
786 mask = 0xffffffffU;
787 goto and_const;
788
789 CASE_OP_32_64(and):
790 mask = temps[args[2]].mask;
791 if (temps[args[2]].state == TCG_TEMP_CONST) {
792 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800793 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800794 }
795 mask = temps[args[1]].mask & mask;
796 break;
797
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800798 CASE_OP_32_64(andc):
799 /* Known-zeros does not imply known-ones. Therefore unless
800 args[2] is constant, we can't infer anything from it. */
801 if (temps[args[2]].state == TCG_TEMP_CONST) {
802 mask = ~temps[args[2]].mask;
803 goto and_const;
804 }
805 /* But we certainly know nothing outside args[1] may be set. */
806 mask = temps[args[1]].mask;
807 break;
808
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200809 case INDEX_op_sar_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800810 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700811 tmp = temps[args[2]].val & 31;
812 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200813 }
814 break;
815 case INDEX_op_sar_i64:
816 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700817 tmp = temps[args[2]].val & 63;
818 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800819 }
820 break;
821
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200822 case INDEX_op_shr_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800823 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700824 tmp = temps[args[2]].val & 31;
825 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200826 }
827 break;
828 case INDEX_op_shr_i64:
829 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700830 tmp = temps[args[2]].val & 63;
831 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800832 }
833 break;
834
Richard Henderson4bb7a412013-09-09 17:03:24 -0700835 case INDEX_op_trunc_shr_i32:
836 mask = (uint64_t)temps[args[1]].mask >> args[2];
837 break;
838
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800839 CASE_OP_32_64(shl):
840 if (temps[args[2]].state == TCG_TEMP_CONST) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700841 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
842 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800843 }
844 break;
845
846 CASE_OP_32_64(neg):
847 /* Set to 1 all bits to the left of the rightmost. */
848 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
849 break;
850
851 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700852 mask = deposit64(temps[args[1]].mask, args[3], args[4],
853 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800854 break;
855
856 CASE_OP_32_64(or):
857 CASE_OP_32_64(xor):
858 mask = temps[args[1]].mask | temps[args[2]].mask;
859 break;
860
861 CASE_OP_32_64(setcond):
862 mask = 1;
863 break;
864
865 CASE_OP_32_64(movcond):
866 mask = temps[args[3]].mask | temps[args[4]].mask;
867 break;
868
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200869 CASE_OP_32_64(ld8u):
870 case INDEX_op_qemu_ld8u:
871 mask = 0xff;
872 break;
873 CASE_OP_32_64(ld16u):
874 case INDEX_op_qemu_ld16u:
875 mask = 0xffff;
876 break;
877 case INDEX_op_ld32u_i64:
878#if TCG_TARGET_REG_BITS == 64
879 case INDEX_op_qemu_ld32u:
880#endif
881 mask = 0xffffffffu;
882 break;
883
884 CASE_OP_32_64(qemu_ld):
885 {
Richard Hendersoncf066672014-03-22 20:06:52 -0700886 TCGMemOp mop = args[nb_oargs + nb_iargs];
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200887 if (!(mop & MO_SIGN)) {
888 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
889 }
890 }
891 break;
892
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800893 default:
894 break;
895 }
896
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200897 /* 32-bit ops (non 64-bit ops and non load/store ops) generate 32-bit
898 results */
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200899 if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_64BIT))) {
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200900 mask &= 0xffffffffu;
901 }
902
Paolo Bonzini633f6502013-01-11 15:42:53 -0800903 if (mask == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700904 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800905 s->gen_opc_buf[op_index] = op_to_movi(op);
906 tcg_opt_gen_movi(gen_args, args[0], 0);
Richard Hendersoncf066672014-03-22 20:06:52 -0700907 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800908 gen_args += 2;
909 continue;
910 }
911 if (affected == 0) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700912 assert(nb_oargs == 1);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800913 if (temps_are_copies(args[0], args[1])) {
914 s->gen_opc_buf[op_index] = INDEX_op_nop;
915 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
916 s->gen_opc_buf[op_index] = op_to_mov(op);
917 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
918 gen_args += 2;
919 } else {
920 s->gen_opc_buf[op_index] = op_to_movi(op);
921 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
922 gen_args += 2;
923 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700924 args += nb_args;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800925 continue;
926 }
927
Aurelien Jarno56e49432012-09-06 16:47:13 +0200928 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
929 switch (op) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200930 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400931 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700932 CASE_OP_32_64(muluh):
933 CASE_OP_32_64(mulsh):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400934 if ((temps[args[2]].state == TCG_TEMP_CONST
935 && temps[args[2]].val == 0)) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400936 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200937 tcg_opt_gen_movi(gen_args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400938 args += 3;
939 gen_args += 2;
940 continue;
941 }
942 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200943 default:
944 break;
945 }
946
947 /* Simplify expression for "op r, a, a => mov r, a" cases */
948 switch (op) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400949 CASE_OP_32_64(or):
950 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200951 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200952 if (temps_are_copies(args[0], args[1])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400953 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400954 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400955 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200956 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400957 gen_args += 2;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400958 }
Aurelien Jarnofedc0da2012-09-07 12:24:32 +0200959 args += 3;
Kirill Batuzov9a810902011-07-07 16:37:15 +0400960 continue;
961 }
962 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000963 default:
964 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400965 }
966
Aurelien Jarno3c941932012-09-18 19:12:36 +0200967 /* Simplify expression for "op r, a, a => movi r, 0" cases */
968 switch (op) {
Richard Hendersone64e9582014-01-28 13:26:17 -0800969 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +0200970 CASE_OP_32_64(sub):
971 CASE_OP_32_64(xor):
972 if (temps_are_copies(args[1], args[2])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400973 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarno3c941932012-09-18 19:12:36 +0200974 tcg_opt_gen_movi(gen_args, args[0], 0);
975 gen_args += 2;
976 args += 3;
977 continue;
978 }
979 break;
980 default:
981 break;
982 }
983
Kirill Batuzov22613af2011-07-07 16:37:13 +0400984 /* Propagate constants through copy operations and do constant
985 folding. Constants will be substituted to arguments by register
986 allocator where needed and possible. Also detect copies. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400987 switch (op) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400988 CASE_OP_32_64(mov):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200989 if (temps_are_copies(args[0], args[1])) {
Kirill Batuzov22613af2011-07-07 16:37:13 +0400990 args += 2;
Evgeny Voevodin92414b32012-11-12 13:27:47 +0400991 s->gen_opc_buf[op_index] = INDEX_op_nop;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400992 break;
993 }
994 if (temps[args[1]].state != TCG_TEMP_CONST) {
Aurelien Jarnob80bb012012-09-11 12:26:23 +0200995 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400996 gen_args += 2;
997 args += 2;
998 break;
999 }
1000 /* Source argument is constant. Rewrite the operation and
1001 let movi case handle it. */
1002 op = op_to_movi(op);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001003 s->gen_opc_buf[op_index] = op;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001004 args[1] = temps[args[1]].val;
1005 /* fallthrough */
1006 CASE_OP_32_64(movi):
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001007 tcg_opt_gen_movi(gen_args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001008 gen_args += 2;
1009 args += 2;
1010 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001011
Kirill Batuzova640f032011-07-07 16:37:17 +04001012 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001013 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001014 CASE_OP_32_64(ext8s):
1015 CASE_OP_32_64(ext8u):
1016 CASE_OP_32_64(ext16s):
1017 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001018 case INDEX_op_ext32s_i64:
1019 case INDEX_op_ext32u_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +04001020 if (temps[args[1]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001021 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzova640f032011-07-07 16:37:17 +04001022 tmp = do_constant_folding(op, temps[args[1]].val, 0);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001023 tcg_opt_gen_movi(gen_args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001024 gen_args += 2;
1025 args += 2;
1026 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001027 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001028 goto do_default;
1029
Richard Henderson4bb7a412013-09-09 17:03:24 -07001030 case INDEX_op_trunc_shr_i32:
1031 if (temps[args[1]].state == TCG_TEMP_CONST) {
1032 s->gen_opc_buf[op_index] = op_to_movi(op);
1033 tmp = do_constant_folding(op, temps[args[1]].val, args[2]);
1034 tcg_opt_gen_movi(gen_args, args[0], tmp);
1035 gen_args += 2;
1036 args += 3;
1037 break;
1038 }
1039 goto do_default;
1040
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001041 CASE_OP_32_64(add):
1042 CASE_OP_32_64(sub):
1043 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001044 CASE_OP_32_64(or):
1045 CASE_OP_32_64(and):
1046 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001047 CASE_OP_32_64(shl):
1048 CASE_OP_32_64(shr):
1049 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001050 CASE_OP_32_64(rotl):
1051 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001052 CASE_OP_32_64(andc):
1053 CASE_OP_32_64(orc):
1054 CASE_OP_32_64(eqv):
1055 CASE_OP_32_64(nand):
1056 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001057 CASE_OP_32_64(muluh):
1058 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001059 CASE_OP_32_64(div):
1060 CASE_OP_32_64(divu):
1061 CASE_OP_32_64(rem):
1062 CASE_OP_32_64(remu):
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001063 if (temps[args[1]].state == TCG_TEMP_CONST
1064 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001065 s->gen_opc_buf[op_index] = op_to_movi(op);
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001066 tmp = do_constant_folding(op, temps[args[1]].val,
1067 temps[args[2]].val);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001068 tcg_opt_gen_movi(gen_args, args[0], tmp);
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001069 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001070 args += 3;
1071 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001072 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001073 goto do_default;
1074
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001075 CASE_OP_32_64(deposit):
1076 if (temps[args[1]].state == TCG_TEMP_CONST
1077 && temps[args[2]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001078 s->gen_opc_buf[op_index] = op_to_movi(op);
Richard Hendersond998e552014-03-18 14:23:52 -07001079 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1080 temps[args[2]].val);
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001081 tcg_opt_gen_movi(gen_args, args[0], tmp);
1082 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001083 args += 5;
1084 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001085 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001086 goto do_default;
1087
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001088 CASE_OP_32_64(setcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001089 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
1090 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001091 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001092 tcg_opt_gen_movi(gen_args, args[0], tmp);
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001093 gen_args += 2;
Richard Henderson6e14e912012-10-02 11:32:24 -07001094 args += 4;
1095 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001096 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001097 goto do_default;
1098
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001099 CASE_OP_32_64(brcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001100 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
1101 if (tmp != 2) {
1102 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001103 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001104 s->gen_opc_buf[op_index] = INDEX_op_br;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001105 gen_args[0] = args[3];
1106 gen_args += 1;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001107 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001108 s->gen_opc_buf[op_index] = INDEX_op_nop;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001109 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001110 args += 4;
1111 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001112 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001113 goto do_default;
1114
Richard Hendersonfa01a202012-09-21 10:13:37 -07001115 CASE_OP_32_64(movcond):
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001116 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
1117 if (tmp != 2) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001118 if (temps_are_copies(args[0], args[4-tmp])) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001119 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001120 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001121 s->gen_opc_buf[op_index] = op_to_movi(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001122 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001123 gen_args += 2;
1124 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001125 s->gen_opc_buf[op_index] = op_to_mov(op);
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001126 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
Richard Hendersonfa01a202012-09-21 10:13:37 -07001127 gen_args += 2;
1128 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001129 args += 6;
1130 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001131 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001132 goto do_default;
1133
Richard Henderson212c3282012-10-02 11:32:28 -07001134 case INDEX_op_add2_i32:
1135 case INDEX_op_sub2_i32:
1136 if (temps[args[2]].state == TCG_TEMP_CONST
1137 && temps[args[3]].state == TCG_TEMP_CONST
1138 && temps[args[4]].state == TCG_TEMP_CONST
1139 && temps[args[5]].state == TCG_TEMP_CONST) {
1140 uint32_t al = temps[args[2]].val;
1141 uint32_t ah = temps[args[3]].val;
1142 uint32_t bl = temps[args[4]].val;
1143 uint32_t bh = temps[args[5]].val;
1144 uint64_t a = ((uint64_t)ah << 32) | al;
1145 uint64_t b = ((uint64_t)bh << 32) | bl;
1146 TCGArg rl, rh;
1147
1148 if (op == INDEX_op_add2_i32) {
1149 a += b;
1150 } else {
1151 a -= b;
1152 }
1153
1154 /* We emit the extra nop when we emit the add2/sub2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001155 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson212c3282012-10-02 11:32:28 -07001156
1157 rl = args[0];
1158 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001159 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1160 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson212c3282012-10-02 11:32:28 -07001161 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
1162 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
1163 gen_args += 4;
1164 args += 6;
1165 break;
1166 }
1167 goto do_default;
1168
Richard Henderson14149682012-10-02 11:32:30 -07001169 case INDEX_op_mulu2_i32:
1170 if (temps[args[2]].state == TCG_TEMP_CONST
1171 && temps[args[3]].state == TCG_TEMP_CONST) {
1172 uint32_t a = temps[args[2]].val;
1173 uint32_t b = temps[args[3]].val;
1174 uint64_t r = (uint64_t)a * b;
1175 TCGArg rl, rh;
1176
1177 /* We emit the extra nop when we emit the mulu2. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001178 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
Richard Henderson14149682012-10-02 11:32:30 -07001179
1180 rl = args[0];
1181 rh = args[1];
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001182 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1183 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
Richard Henderson14149682012-10-02 11:32:30 -07001184 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
1185 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
1186 gen_args += 4;
1187 args += 4;
1188 break;
1189 }
1190 goto do_default;
1191
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001192 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001193 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1194 if (tmp != 2) {
1195 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001196 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001197 s->gen_opc_buf[op_index] = INDEX_op_br;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001198 gen_args[0] = args[5];
1199 gen_args += 1;
1200 } else {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001201 s->gen_opc_buf[op_index] = INDEX_op_nop;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001202 }
1203 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1204 && temps[args[2]].state == TCG_TEMP_CONST
1205 && temps[args[3]].state == TCG_TEMP_CONST
1206 && temps[args[2]].val == 0
1207 && temps[args[3]].val == 0) {
1208 /* Simplify LT/GE comparisons vs zero to a single compare
1209 vs the high word of the input. */
Paolo Bonzinid193a142013-01-11 15:42:51 -08001210 reset_all_temps(nb_temps);
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001211 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001212 gen_args[0] = args[1];
1213 gen_args[1] = args[3];
1214 gen_args[2] = args[4];
1215 gen_args[3] = args[5];
1216 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001217 } else {
1218 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001219 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001220 args += 6;
1221 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001222
1223 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001224 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1225 if (tmp != 2) {
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001226 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001227 tcg_opt_gen_movi(gen_args, args[0], tmp);
1228 gen_args += 2;
1229 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1230 && temps[args[3]].state == TCG_TEMP_CONST
1231 && temps[args[4]].state == TCG_TEMP_CONST
1232 && temps[args[3]].val == 0
1233 && temps[args[4]].val == 0) {
1234 /* Simplify LT/GE comparisons vs zero to a single compare
1235 vs the high word of the input. */
Evgeny Voevodin92414b32012-11-12 13:27:47 +04001236 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001237 reset_temp(args[0]);
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001238 gen_args[0] = args[0];
1239 gen_args[1] = args[2];
1240 gen_args[2] = args[4];
1241 gen_args[3] = args[5];
1242 gen_args += 4;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001243 } else {
1244 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001245 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001246 args += 6;
1247 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001248
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001249 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001250 if (!(args[nb_oargs + nb_iargs + 1]
1251 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001252 for (i = 0; i < nb_globals; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001253 reset_temp(i);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001254 }
1255 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001256 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001257
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001258 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001259 do_default:
1260 /* Default case: we know nothing about operation (or were unable
1261 to compute the operation result) so no propagation is done.
1262 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001263 block, otherwise we only trash the output args. "mask" is
1264 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001265 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001266 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001267 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001268 do_reset_output:
1269 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001270 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001271 /* Save the corresponding known-zero bits mask for the
1272 first output argument (only one supported so far). */
1273 if (i == 0) {
1274 temps[args[i]].mask = mask;
1275 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001276 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001277 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001278 for (i = 0; i < nb_args; i++) {
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001279 gen_args[i] = args[i];
1280 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001281 args += nb_args;
1282 gen_args += nb_args;
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001283 break;
1284 }
1285 }
1286
1287 return gen_args;
1288}
1289
1290TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1291 TCGArg *args, TCGOpDef *tcg_op_defs)
1292{
1293 TCGArg *res;
1294 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1295 return res;
1296}