blob: f01160815c17be5fcec4845b901343e801ce1cbf [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
Peter Maydell757e7252016-01-26 18:17:08 +000026#include "qemu/osdep.h"
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040027
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040028
29#include "qemu-common.h"
30#include "tcg-op.h"
31
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040032#define CASE_OP_32_64(x) \
33 glue(glue(case INDEX_op_, x), _i32): \
34 glue(glue(case INDEX_op_, x), _i64)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +040035
Kirill Batuzov22613af2011-07-07 16:37:13 +040036struct tcg_temp_info {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020037 bool is_const;
Kirill Batuzov22613af2011-07-07 16:37:13 +040038 uint16_t prev_copy;
39 uint16_t next_copy;
40 tcg_target_ulong val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080041 tcg_target_ulong mask;
Kirill Batuzov22613af2011-07-07 16:37:13 +040042};
43
44static struct tcg_temp_info temps[TCG_MAX_TEMPS];
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020045static TCGTempSet temps_used;
Kirill Batuzov22613af2011-07-07 16:37:13 +040046
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020047static inline bool temp_is_const(TCGArg arg)
48{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020049 return temps[arg].is_const;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020050}
51
52static inline bool temp_is_copy(TCGArg arg)
53{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020054 return temps[arg].next_copy != arg;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +020055}
56
Aurelien Jarnob41059d2015-07-27 12:41:44 +020057/* Reset TEMP's state, possibly removing the temp for the list of copies. */
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +020058static void reset_temp(TCGArg temp)
Kirill Batuzov22613af2011-07-07 16:37:13 +040059{
Aurelien Jarnob41059d2015-07-27 12:41:44 +020060 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
61 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
62 temps[temp].next_copy = temp;
63 temps[temp].prev_copy = temp;
64 temps[temp].is_const = false;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -080065 temps[temp].mask = -1;
Kirill Batuzov22613af2011-07-07 16:37:13 +040066}
67
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020068/* Reset all temporaries, given that there are NB_TEMPS of them. */
69static void reset_all_temps(int nb_temps)
70{
71 bitmap_zero(temps_used.l, nb_temps);
72}
73
74/* Initialize and activate a temporary. */
75static void init_temp_info(TCGArg temp)
76{
77 if (!test_bit(temp, temps_used.l)) {
Aurelien Jarnob41059d2015-07-27 12:41:44 +020078 temps[temp].next_copy = temp;
79 temps[temp].prev_copy = temp;
80 temps[temp].is_const = false;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +020081 temps[temp].mask = -1;
82 set_bit(temp, temps_used.l);
83 }
84}
85
Richard Hendersona4ce0992014-03-30 17:14:02 -070086static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
87 TCGOpcode opc, int nargs)
88{
89 int oi = s->gen_next_op_idx;
90 int pi = s->gen_next_parm_idx;
91 int prev = old_op->prev;
92 int next = old_op - s->gen_op_buf;
93 TCGOp *new_op;
94
95 tcg_debug_assert(oi < OPC_BUF_SIZE);
96 tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
97 s->gen_next_op_idx = oi + 1;
98 s->gen_next_parm_idx = pi + nargs;
99
100 new_op = &s->gen_op_buf[oi];
101 *new_op = (TCGOp){
102 .opc = opc,
103 .args = pi,
104 .prev = prev,
105 .next = next
106 };
107 if (prev >= 0) {
108 s->gen_op_buf[prev].next = oi;
109 } else {
110 s->gen_first_op_idx = oi;
111 }
112 old_op->prev = oi;
113
114 return new_op;
115}
116
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000117static int op_bits(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400118{
Richard Henderson8399ad52011-08-17 14:11:45 -0700119 const TCGOpDef *def = &tcg_op_defs[op];
120 return def->flags & TCG_OPF_64BIT ? 64 : 32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400121}
122
Richard Hendersona62f6f52014-05-22 10:59:12 -0700123static TCGOpcode op_to_mov(TCGOpcode op)
124{
125 switch (op_bits(op)) {
126 case 32:
127 return INDEX_op_mov_i32;
128 case 64:
129 return INDEX_op_mov_i64;
130 default:
131 fprintf(stderr, "op_to_mov: unexpected return value of "
132 "function op_bits.\n");
133 tcg_abort();
134 }
135}
136
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000137static TCGOpcode op_to_movi(TCGOpcode op)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400138{
139 switch (op_bits(op)) {
140 case 32:
141 return INDEX_op_movi_i32;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400142 case 64:
143 return INDEX_op_movi_i64;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400144 default:
145 fprintf(stderr, "op_to_movi: unexpected return value of "
146 "function op_bits.\n");
147 tcg_abort();
148 }
149}
150
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200151static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
152{
153 TCGArg i;
154
155 /* If this is already a global, we can't do better. */
156 if (temp < s->nb_globals) {
157 return temp;
158 }
159
160 /* Search for a global first. */
161 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
162 if (i < s->nb_globals) {
163 return i;
164 }
165 }
166
167 /* If it is a temp, search for a temp local. */
168 if (!s->temps[temp].temp_local) {
169 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
170 if (s->temps[i].temp_local) {
171 return i;
172 }
173 }
174 }
175
176 /* Failure to find a better representation, return the same temp. */
177 return temp;
178}
179
180static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
181{
182 TCGArg i;
183
184 if (arg1 == arg2) {
185 return true;
186 }
187
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200188 if (!temp_is_copy(arg1) || !temp_is_copy(arg2)) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200189 return false;
190 }
191
192 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
193 if (i == arg2) {
194 return true;
195 }
196 }
197
198 return false;
199}
200
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200201static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args,
202 TCGArg dst, TCGArg val)
203{
204 TCGOpcode new_op = op_to_movi(op->opc);
205 tcg_target_ulong mask;
206
207 op->opc = new_op;
208
209 reset_temp(dst);
Aurelien Jarnob41059d2015-07-27 12:41:44 +0200210 temps[dst].is_const = true;
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200211 temps[dst].val = val;
212 mask = val;
Aurelien Jarno96152122015-07-10 18:03:30 +0200213 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_movi_i32) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200214 /* High bits of the destination are now garbage. */
215 mask |= ~0xffffffffull;
216 }
217 temps[dst].mask = mask;
218
219 args[0] = dst;
220 args[1] = val;
221}
222
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700223static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args,
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200224 TCGArg dst, TCGArg src)
Kirill Batuzov22613af2011-07-07 16:37:13 +0400225{
Aurelien Jarno53657182015-06-04 21:53:25 +0200226 if (temps_are_copies(dst, src)) {
227 tcg_op_remove(s, op);
228 return;
229 }
230
Aurelien Jarno8d6a9162015-06-04 21:53:24 +0200231 TCGOpcode new_op = op_to_mov(op->opc);
Richard Henderson24666ba2014-05-22 11:14:10 -0700232 tcg_target_ulong mask;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700233
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700234 op->opc = new_op;
Richard Hendersona62f6f52014-05-22 10:59:12 -0700235
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800236 reset_temp(dst);
Richard Henderson24666ba2014-05-22 11:14:10 -0700237 mask = temps[src].mask;
238 if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) {
239 /* High bits of the destination are now garbage. */
240 mask |= ~0xffffffffull;
241 }
242 temps[dst].mask = mask;
243
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800244 if (s->temps[src].type == s->temps[dst].type) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800245 temps[dst].next_copy = temps[src].next_copy;
246 temps[dst].prev_copy = src;
247 temps[temps[dst].next_copy].prev_copy = dst;
248 temps[src].next_copy = dst;
Aurelien Jarno299f8012015-07-27 12:41:44 +0200249 temps[dst].is_const = temps[src].is_const;
250 temps[dst].val = temps[src].val;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800251 }
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200252
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700253 args[0] = dst;
254 args[1] = src;
Kirill Batuzov22613af2011-07-07 16:37:13 +0400255}
256
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000257static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400258{
Richard Henderson03271522013-08-14 14:35:56 -0700259 uint64_t l64, h64;
260
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400261 switch (op) {
262 CASE_OP_32_64(add):
263 return x + y;
264
265 CASE_OP_32_64(sub):
266 return x - y;
267
268 CASE_OP_32_64(mul):
269 return x * y;
270
Kirill Batuzov9a810902011-07-07 16:37:15 +0400271 CASE_OP_32_64(and):
272 return x & y;
273
274 CASE_OP_32_64(or):
275 return x | y;
276
277 CASE_OP_32_64(xor):
278 return x ^ y;
279
Kirill Batuzov55c09752011-07-07 16:37:16 +0400280 case INDEX_op_shl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700281 return (uint32_t)x << (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400282
Kirill Batuzov55c09752011-07-07 16:37:16 +0400283 case INDEX_op_shl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700284 return (uint64_t)x << (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400285
286 case INDEX_op_shr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700287 return (uint32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400288
Kirill Batuzov55c09752011-07-07 16:37:16 +0400289 case INDEX_op_shr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700290 return (uint64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400291
292 case INDEX_op_sar_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700293 return (int32_t)x >> (y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400294
Kirill Batuzov55c09752011-07-07 16:37:16 +0400295 case INDEX_op_sar_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700296 return (int64_t)x >> (y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400297
298 case INDEX_op_rotr_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700299 return ror32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400300
Kirill Batuzov55c09752011-07-07 16:37:16 +0400301 case INDEX_op_rotr_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700302 return ror64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400303
304 case INDEX_op_rotl_i32:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700305 return rol32(x, y & 31);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400306
Kirill Batuzov55c09752011-07-07 16:37:16 +0400307 case INDEX_op_rotl_i64:
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700308 return rol64(x, y & 63);
Kirill Batuzov55c09752011-07-07 16:37:16 +0400309
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700310 CASE_OP_32_64(not):
Kirill Batuzova640f032011-07-07 16:37:17 +0400311 return ~x;
312
Richard Hendersoncb25c802011-08-17 14:11:47 -0700313 CASE_OP_32_64(neg):
314 return -x;
315
316 CASE_OP_32_64(andc):
317 return x & ~y;
318
319 CASE_OP_32_64(orc):
320 return x | ~y;
321
322 CASE_OP_32_64(eqv):
323 return ~(x ^ y);
324
325 CASE_OP_32_64(nand):
326 return ~(x & y);
327
328 CASE_OP_32_64(nor):
329 return ~(x | y);
330
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700331 CASE_OP_32_64(ext8s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400332 return (int8_t)x;
333
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700334 CASE_OP_32_64(ext16s):
Kirill Batuzova640f032011-07-07 16:37:17 +0400335 return (int16_t)x;
336
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700337 CASE_OP_32_64(ext8u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400338 return (uint8_t)x;
339
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700340 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +0400341 return (uint16_t)x;
342
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200343 case INDEX_op_ext_i32_i64:
Kirill Batuzova640f032011-07-07 16:37:17 +0400344 case INDEX_op_ext32s_i64:
345 return (int32_t)x;
346
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200347 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -0700348 case INDEX_op_extrl_i64_i32:
Kirill Batuzova640f032011-07-07 16:37:17 +0400349 case INDEX_op_ext32u_i64:
350 return (uint32_t)x;
Kirill Batuzova640f032011-07-07 16:37:17 +0400351
Richard Henderson609ad702015-07-24 07:16:00 -0700352 case INDEX_op_extrh_i64_i32:
353 return (uint64_t)x >> 32;
354
Richard Henderson03271522013-08-14 14:35:56 -0700355 case INDEX_op_muluh_i32:
356 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
357 case INDEX_op_mulsh_i32:
358 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
359
360 case INDEX_op_muluh_i64:
361 mulu64(&l64, &h64, x, y);
362 return h64;
363 case INDEX_op_mulsh_i64:
364 muls64(&l64, &h64, x, y);
365 return h64;
366
Richard Henderson01547f72013-08-14 15:22:46 -0700367 case INDEX_op_div_i32:
368 /* Avoid crashing on divide by zero, otherwise undefined. */
369 return (int32_t)x / ((int32_t)y ? : 1);
370 case INDEX_op_divu_i32:
371 return (uint32_t)x / ((uint32_t)y ? : 1);
372 case INDEX_op_div_i64:
373 return (int64_t)x / ((int64_t)y ? : 1);
374 case INDEX_op_divu_i64:
375 return (uint64_t)x / ((uint64_t)y ? : 1);
376
377 case INDEX_op_rem_i32:
378 return (int32_t)x % ((int32_t)y ? : 1);
379 case INDEX_op_remu_i32:
380 return (uint32_t)x % ((uint32_t)y ? : 1);
381 case INDEX_op_rem_i64:
382 return (int64_t)x % ((int64_t)y ? : 1);
383 case INDEX_op_remu_i64:
384 return (uint64_t)x % ((uint64_t)y ? : 1);
385
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400386 default:
387 fprintf(stderr,
388 "Unrecognized operation %d in do_constant_folding.\n", op);
389 tcg_abort();
390 }
391}
392
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000393static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400394{
395 TCGArg res = do_constant_folding_2(op, x, y);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400396 if (op_bits(op) == 32) {
Aurelien Jarno29f3ff82015-07-10 18:03:31 +0200397 res = (int32_t)res;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400398 }
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400399 return res;
400}
401
Richard Henderson9519da72012-10-02 11:32:26 -0700402static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
403{
404 switch (c) {
405 case TCG_COND_EQ:
406 return x == y;
407 case TCG_COND_NE:
408 return x != y;
409 case TCG_COND_LT:
410 return (int32_t)x < (int32_t)y;
411 case TCG_COND_GE:
412 return (int32_t)x >= (int32_t)y;
413 case TCG_COND_LE:
414 return (int32_t)x <= (int32_t)y;
415 case TCG_COND_GT:
416 return (int32_t)x > (int32_t)y;
417 case TCG_COND_LTU:
418 return x < y;
419 case TCG_COND_GEU:
420 return x >= y;
421 case TCG_COND_LEU:
422 return x <= y;
423 case TCG_COND_GTU:
424 return x > y;
425 default:
426 tcg_abort();
427 }
428}
429
430static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
431{
432 switch (c) {
433 case TCG_COND_EQ:
434 return x == y;
435 case TCG_COND_NE:
436 return x != y;
437 case TCG_COND_LT:
438 return (int64_t)x < (int64_t)y;
439 case TCG_COND_GE:
440 return (int64_t)x >= (int64_t)y;
441 case TCG_COND_LE:
442 return (int64_t)x <= (int64_t)y;
443 case TCG_COND_GT:
444 return (int64_t)x > (int64_t)y;
445 case TCG_COND_LTU:
446 return x < y;
447 case TCG_COND_GEU:
448 return x >= y;
449 case TCG_COND_LEU:
450 return x <= y;
451 case TCG_COND_GTU:
452 return x > y;
453 default:
454 tcg_abort();
455 }
456}
457
458static bool do_constant_folding_cond_eq(TCGCond c)
459{
460 switch (c) {
461 case TCG_COND_GT:
462 case TCG_COND_LTU:
463 case TCG_COND_LT:
464 case TCG_COND_GTU:
465 case TCG_COND_NE:
466 return 0;
467 case TCG_COND_GE:
468 case TCG_COND_GEU:
469 case TCG_COND_LE:
470 case TCG_COND_LEU:
471 case TCG_COND_EQ:
472 return 1;
473 default:
474 tcg_abort();
475 }
476}
477
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200478/* Return 2 if the condition can't be simplified, and the result
479 of the condition (0 or 1) if it can */
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200480static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
481 TCGArg y, TCGCond c)
482{
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200483 if (temp_is_const(x) && temp_is_const(y)) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200484 switch (op_bits(op)) {
485 case 32:
Richard Henderson9519da72012-10-02 11:32:26 -0700486 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200487 case 64:
Richard Henderson9519da72012-10-02 11:32:26 -0700488 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
489 default:
490 tcg_abort();
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200491 }
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200492 } else if (temps_are_copies(x, y)) {
Richard Henderson9519da72012-10-02 11:32:26 -0700493 return do_constant_folding_cond_eq(c);
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200494 } else if (temp_is_const(y) && temps[y].val == 0) {
Aurelien Jarnob336ceb2012-09-18 19:37:00 +0200495 switch (c) {
496 case TCG_COND_LTU:
497 return 0;
498 case TCG_COND_GEU:
499 return 1;
500 default:
501 return 2;
502 }
503 } else {
504 return 2;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200505 }
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +0200506}
507
Richard Henderson6c4382f2012-10-02 11:32:27 -0700508/* Return 2 if the condition can't be simplified, and the result
509 of the condition (0 or 1) if it can */
510static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
511{
512 TCGArg al = p1[0], ah = p1[1];
513 TCGArg bl = p2[0], bh = p2[1];
514
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200515 if (temp_is_const(bl) && temp_is_const(bh)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700516 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
517
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200518 if (temp_is_const(al) && temp_is_const(ah)) {
Richard Henderson6c4382f2012-10-02 11:32:27 -0700519 uint64_t a;
520 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
521 return do_constant_folding_cond_64(a, b, c);
522 }
523 if (b == 0) {
524 switch (c) {
525 case TCG_COND_LTU:
526 return 0;
527 case TCG_COND_GEU:
528 return 1;
529 default:
530 break;
531 }
532 }
533 }
534 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
535 return do_constant_folding_cond_eq(c);
536 }
537 return 2;
538}
539
Richard Henderson24c9ae42012-10-02 11:32:21 -0700540static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
541{
542 TCGArg a1 = *p1, a2 = *p2;
543 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200544 sum += temp_is_const(a1);
545 sum -= temp_is_const(a2);
Richard Henderson24c9ae42012-10-02 11:32:21 -0700546
547 /* Prefer the constant in second argument, and then the form
548 op a, a, b, which is better handled on non-RISC hosts. */
549 if (sum > 0 || (sum == 0 && dest == a2)) {
550 *p1 = a2;
551 *p2 = a1;
552 return true;
553 }
554 return false;
555}
556
Richard Henderson0bfcb862012-10-02 11:32:23 -0700557static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
558{
559 int sum = 0;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200560 sum += temp_is_const(p1[0]);
561 sum += temp_is_const(p1[1]);
562 sum -= temp_is_const(p2[0]);
563 sum -= temp_is_const(p2[1]);
Richard Henderson0bfcb862012-10-02 11:32:23 -0700564 if (sum > 0) {
565 TCGArg t;
566 t = p1[0], p1[0] = p2[0], p2[0] = t;
567 t = p1[1], p1[1] = p2[1], p2[1] = t;
568 return true;
569 }
570 return false;
571}
572
Kirill Batuzov22613af2011-07-07 16:37:13 +0400573/* Propagate constants and copies, fold constant expressions. */
Aurelien Jarno36e60ef2015-06-04 21:53:27 +0200574void tcg_optimize(TCGContext *s)
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400575{
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700576 int oi, oi_next, nb_temps, nb_globals;
Richard Henderson5d8f5362012-09-21 10:13:38 -0700577
Kirill Batuzov22613af2011-07-07 16:37:13 +0400578 /* Array VALS has an element for each temp.
579 If this temp holds a constant then its value is kept in VALS' element.
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +0200580 If this temp is a copy of other ones then the other copies are
581 available through the doubly linked circular list. */
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400582
583 nb_temps = s->nb_temps;
584 nb_globals = s->nb_globals;
Paolo Bonzinid193a142013-01-11 15:42:51 -0800585 reset_all_temps(nb_temps);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +0400586
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700587 for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700588 tcg_target_ulong mask, partmask, affected;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700589 int nb_oargs, nb_iargs, i;
Richard Hendersoncf066672014-03-22 20:06:52 -0700590 TCGArg tmp;
591
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700592 TCGOp * const op = &s->gen_op_buf[oi];
593 TCGArg * const args = &s->gen_opparam_buf[op->args];
594 TCGOpcode opc = op->opc;
595 const TCGOpDef *def = &tcg_op_defs[opc];
596
597 oi_next = op->next;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200598
599 /* Count the arguments, and initialize the temps that are
600 going to be used */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700601 if (opc == INDEX_op_call) {
602 nb_oargs = op->callo;
603 nb_iargs = op->calli;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200604 for (i = 0; i < nb_oargs + nb_iargs; i++) {
605 tmp = args[i];
606 if (tmp != TCG_CALL_DUMMY_ARG) {
607 init_temp_info(tmp);
608 }
609 }
Aurelien Jarno1ff8c542012-09-11 16:18:49 +0200610 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -0700611 nb_oargs = def->nb_oargs;
612 nb_iargs = def->nb_iargs;
Aurelien Jarno1208d7d2015-07-27 12:41:44 +0200613 for (i = 0; i < nb_oargs + nb_iargs; i++) {
614 init_temp_info(args[i]);
615 }
Richard Hendersoncf066672014-03-22 20:06:52 -0700616 }
617
618 /* Do copy propagation */
619 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200620 if (temp_is_copy(args[i])) {
Richard Hendersoncf066672014-03-22 20:06:52 -0700621 args[i] = find_better_copy(s, args[i]);
Kirill Batuzov22613af2011-07-07 16:37:13 +0400622 }
623 }
624
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400625 /* For commutative operations make constant second argument */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700626 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400627 CASE_OP_32_64(add):
628 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +0400629 CASE_OP_32_64(and):
630 CASE_OP_32_64(or):
631 CASE_OP_32_64(xor):
Richard Hendersoncb25c802011-08-17 14:11:47 -0700632 CASE_OP_32_64(eqv):
633 CASE_OP_32_64(nand):
634 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -0700635 CASE_OP_32_64(muluh):
636 CASE_OP_32_64(mulsh):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700637 swap_commutative(args[0], &args[1], &args[2]);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400638 break;
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200639 CASE_OP_32_64(brcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700640 if (swap_commutative(-1, &args[0], &args[1])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200641 args[2] = tcg_swap_cond(args[2]);
642 }
643 break;
644 CASE_OP_32_64(setcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700645 if (swap_commutative(args[0], &args[1], &args[2])) {
Aurelien Jarno65a7cce2012-09-06 16:47:14 +0200646 args[3] = tcg_swap_cond(args[3]);
647 }
648 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -0700649 CASE_OP_32_64(movcond):
Richard Henderson24c9ae42012-10-02 11:32:21 -0700650 if (swap_commutative(-1, &args[1], &args[2])) {
651 args[5] = tcg_swap_cond(args[5]);
Richard Hendersonfa01a202012-09-21 10:13:37 -0700652 }
Richard Henderson5d8f5362012-09-21 10:13:38 -0700653 /* For movcond, we canonicalize the "false" input reg to match
654 the destination reg so that the tcg backend can implement
655 a "move if true" operation. */
Richard Henderson24c9ae42012-10-02 11:32:21 -0700656 if (swap_commutative(args[0], &args[4], &args[3])) {
657 args[5] = tcg_invert_cond(args[5]);
Richard Henderson5d8f5362012-09-21 10:13:38 -0700658 }
Richard Henderson1e484e62012-10-02 11:32:22 -0700659 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800660 CASE_OP_32_64(add2):
Richard Henderson1e484e62012-10-02 11:32:22 -0700661 swap_commutative(args[0], &args[2], &args[4]);
662 swap_commutative(args[1], &args[3], &args[5]);
663 break;
Richard Hendersond7156f72013-02-19 23:51:52 -0800664 CASE_OP_32_64(mulu2):
Richard Henderson4d3203f2013-02-19 23:51:53 -0800665 CASE_OP_32_64(muls2):
Richard Henderson14149682012-10-02 11:32:30 -0700666 swap_commutative(args[0], &args[2], &args[3]);
667 break;
Richard Henderson0bfcb862012-10-02 11:32:23 -0700668 case INDEX_op_brcond2_i32:
669 if (swap_commutative2(&args[0], &args[2])) {
670 args[4] = tcg_swap_cond(args[4]);
671 }
672 break;
673 case INDEX_op_setcond2_i32:
674 if (swap_commutative2(&args[1], &args[3])) {
675 args[5] = tcg_swap_cond(args[5]);
676 }
677 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400678 default:
679 break;
680 }
681
Richard Henderson2d497542013-03-21 09:13:33 -0700682 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
683 and "sub r, 0, a => neg r, a" case. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700684 switch (opc) {
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200685 CASE_OP_32_64(shl):
686 CASE_OP_32_64(shr):
687 CASE_OP_32_64(sar):
688 CASE_OP_32_64(rotl):
689 CASE_OP_32_64(rotr):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200690 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200691 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200692 continue;
693 }
694 break;
Richard Henderson2d497542013-03-21 09:13:33 -0700695 CASE_OP_32_64(sub):
696 {
697 TCGOpcode neg_op;
698 bool have_neg;
699
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200700 if (temp_is_const(args[2])) {
Richard Henderson2d497542013-03-21 09:13:33 -0700701 /* Proceed with possible constant folding. */
702 break;
703 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700704 if (opc == INDEX_op_sub_i32) {
Richard Henderson2d497542013-03-21 09:13:33 -0700705 neg_op = INDEX_op_neg_i32;
706 have_neg = TCG_TARGET_HAS_neg_i32;
707 } else {
708 neg_op = INDEX_op_neg_i64;
709 have_neg = TCG_TARGET_HAS_neg_i64;
710 }
711 if (!have_neg) {
712 break;
713 }
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200714 if (temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700715 op->opc = neg_op;
Richard Henderson2d497542013-03-21 09:13:33 -0700716 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700717 args[1] = args[2];
Richard Henderson2d497542013-03-21 09:13:33 -0700718 continue;
719 }
720 }
721 break;
Richard Hendersone201b562014-01-28 13:15:38 -0800722 CASE_OP_32_64(xor):
723 CASE_OP_32_64(nand):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200724 if (!temp_is_const(args[1])
725 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800726 i = 1;
727 goto try_not;
728 }
729 break;
730 CASE_OP_32_64(nor):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200731 if (!temp_is_const(args[1])
732 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800733 i = 1;
734 goto try_not;
735 }
736 break;
737 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200738 if (!temp_is_const(args[2])
739 && temp_is_const(args[1]) && temps[args[1]].val == -1) {
Richard Hendersone201b562014-01-28 13:15:38 -0800740 i = 2;
741 goto try_not;
742 }
743 break;
744 CASE_OP_32_64(orc):
745 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200746 if (!temp_is_const(args[2])
747 && temp_is_const(args[1]) && temps[args[1]].val == 0) {
Richard Hendersone201b562014-01-28 13:15:38 -0800748 i = 2;
749 goto try_not;
750 }
751 break;
752 try_not:
753 {
754 TCGOpcode not_op;
755 bool have_not;
756
757 if (def->flags & TCG_OPF_64BIT) {
758 not_op = INDEX_op_not_i64;
759 have_not = TCG_TARGET_HAS_not_i64;
760 } else {
761 not_op = INDEX_op_not_i32;
762 have_not = TCG_TARGET_HAS_not_i32;
763 }
764 if (!have_not) {
765 break;
766 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700767 op->opc = not_op;
Richard Hendersone201b562014-01-28 13:15:38 -0800768 reset_temp(args[0]);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700769 args[1] = args[i];
Richard Hendersone201b562014-01-28 13:15:38 -0800770 continue;
771 }
Aurelien Jarno01ee5282012-09-06 16:47:14 +0200772 default:
773 break;
774 }
775
Richard Henderson464a1442014-01-31 07:42:11 -0600776 /* Simplify expression for "op r, a, const => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700777 switch (opc) {
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400778 CASE_OP_32_64(add):
779 CASE_OP_32_64(sub):
Kirill Batuzov55c09752011-07-07 16:37:16 +0400780 CASE_OP_32_64(shl):
781 CASE_OP_32_64(shr):
782 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700783 CASE_OP_32_64(rotl):
784 CASE_OP_32_64(rotr):
Aurelien Jarno38ee1882012-09-06 16:47:14 +0200785 CASE_OP_32_64(or):
786 CASE_OP_32_64(xor):
Richard Henderson464a1442014-01-31 07:42:11 -0600787 CASE_OP_32_64(andc):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200788 if (!temp_is_const(args[1])
789 && temp_is_const(args[2]) && temps[args[2]].val == 0) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200790 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
791 continue;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400792 }
793 break;
Richard Henderson464a1442014-01-31 07:42:11 -0600794 CASE_OP_32_64(and):
795 CASE_OP_32_64(orc):
796 CASE_OP_32_64(eqv):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200797 if (!temp_is_const(args[1])
798 && temp_is_const(args[2]) && temps[args[2]].val == -1) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200799 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
800 continue;
Richard Henderson464a1442014-01-31 07:42:11 -0600801 }
802 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200803 default:
804 break;
805 }
806
Aurelien Jarno30312442013-09-03 08:27:38 +0200807 /* Simplify using known-zero bits. Currently only ops with a single
808 output argument is supported. */
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800809 mask = -1;
Paolo Bonzini633f6502013-01-11 15:42:53 -0800810 affected = -1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700811 switch (opc) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800812 CASE_OP_32_64(ext8s):
813 if ((temps[args[1]].mask & 0x80) != 0) {
814 break;
815 }
816 CASE_OP_32_64(ext8u):
817 mask = 0xff;
818 goto and_const;
819 CASE_OP_32_64(ext16s):
820 if ((temps[args[1]].mask & 0x8000) != 0) {
821 break;
822 }
823 CASE_OP_32_64(ext16u):
824 mask = 0xffff;
825 goto and_const;
826 case INDEX_op_ext32s_i64:
827 if ((temps[args[1]].mask & 0x80000000) != 0) {
828 break;
829 }
830 case INDEX_op_ext32u_i64:
831 mask = 0xffffffffU;
832 goto and_const;
833
834 CASE_OP_32_64(and):
835 mask = temps[args[2]].mask;
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200836 if (temp_is_const(args[2])) {
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800837 and_const:
Paolo Bonzini633f6502013-01-11 15:42:53 -0800838 affected = temps[args[1]].mask & ~mask;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800839 }
840 mask = temps[args[1]].mask & mask;
841 break;
842
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +0200843 case INDEX_op_ext_i32_i64:
844 if ((temps[args[1]].mask & 0x80000000) != 0) {
845 break;
846 }
847 case INDEX_op_extu_i32_i64:
848 /* We do not compute affected as it is a size changing op. */
849 mask = (uint32_t)temps[args[1]].mask;
850 break;
851
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800852 CASE_OP_32_64(andc):
853 /* Known-zeros does not imply known-ones. Therefore unless
854 args[2] is constant, we can't infer anything from it. */
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200855 if (temp_is_const(args[2])) {
Richard Henderson23ec69ed2014-01-28 12:03:24 -0800856 mask = ~temps[args[2]].mask;
857 goto and_const;
858 }
859 /* But we certainly know nothing outside args[1] may be set. */
860 mask = temps[args[1]].mask;
861 break;
862
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200863 case INDEX_op_sar_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200864 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700865 tmp = temps[args[2]].val & 31;
866 mask = (int32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200867 }
868 break;
869 case INDEX_op_sar_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200870 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700871 tmp = temps[args[2]].val & 63;
872 mask = (int64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800873 }
874 break;
875
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200876 case INDEX_op_shr_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200877 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700878 tmp = temps[args[2]].val & 31;
879 mask = (uint32_t)temps[args[1]].mask >> tmp;
Aurelien Jarnoe46b2252013-09-03 08:27:38 +0200880 }
881 break;
882 case INDEX_op_shr_i64:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200883 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700884 tmp = temps[args[2]].val & 63;
885 mask = (uint64_t)temps[args[1]].mask >> tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800886 }
887 break;
888
Richard Henderson609ad702015-07-24 07:16:00 -0700889 case INDEX_op_extrl_i64_i32:
890 mask = (uint32_t)temps[args[1]].mask;
891 break;
892 case INDEX_op_extrh_i64_i32:
893 mask = (uint64_t)temps[args[1]].mask >> 32;
Richard Henderson4bb7a412013-09-09 17:03:24 -0700894 break;
895
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800896 CASE_OP_32_64(shl):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200897 if (temp_is_const(args[2])) {
Richard Henderson50c5c4d2014-03-18 07:45:39 -0700898 tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1);
899 mask = temps[args[1]].mask << tmp;
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800900 }
901 break;
902
903 CASE_OP_32_64(neg):
904 /* Set to 1 all bits to the left of the rightmost. */
905 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
906 break;
907
908 CASE_OP_32_64(deposit):
Richard Hendersond998e552014-03-18 14:23:52 -0700909 mask = deposit64(temps[args[1]].mask, args[3], args[4],
910 temps[args[2]].mask);
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800911 break;
912
913 CASE_OP_32_64(or):
914 CASE_OP_32_64(xor):
915 mask = temps[args[1]].mask | temps[args[2]].mask;
916 break;
917
918 CASE_OP_32_64(setcond):
Richard Hendersona7635512014-04-23 22:18:30 -0700919 case INDEX_op_setcond2_i32:
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800920 mask = 1;
921 break;
922
923 CASE_OP_32_64(movcond):
924 mask = temps[args[3]].mask | temps[args[4]].mask;
925 break;
926
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200927 CASE_OP_32_64(ld8u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200928 mask = 0xff;
929 break;
930 CASE_OP_32_64(ld16u):
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200931 mask = 0xffff;
932 break;
933 case INDEX_op_ld32u_i64:
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200934 mask = 0xffffffffu;
935 break;
936
937 CASE_OP_32_64(qemu_ld):
938 {
Richard Henderson59227d52015-05-12 11:51:44 -0700939 TCGMemOpIdx oi = args[nb_oargs + nb_iargs];
940 TCGMemOp mop = get_memop(oi);
Aurelien Jarnoc8d70272013-09-03 08:27:39 +0200941 if (!(mop & MO_SIGN)) {
942 mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1;
943 }
944 }
945 break;
946
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -0800947 default:
948 break;
949 }
950
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700951 /* 32-bit ops generate 32-bit results. For the result is zero test
952 below, we can ignore high bits, but for further optimizations we
953 need to record that the high bits contain garbage. */
Richard Henderson24666ba2014-05-22 11:14:10 -0700954 partmask = mask;
Richard Hendersonbc8d6882014-06-08 18:24:14 -0700955 if (!(def->flags & TCG_OPF_64BIT)) {
Richard Henderson24666ba2014-05-22 11:14:10 -0700956 mask |= ~(tcg_target_ulong)0xffffffffu;
957 partmask &= 0xffffffffu;
958 affected &= 0xffffffffu;
Aurelien Jarnof096dc92013-09-03 08:27:38 +0200959 }
960
Richard Henderson24666ba2014-05-22 11:14:10 -0700961 if (partmask == 0) {
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +0200962 tcg_debug_assert(nb_oargs == 1);
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200963 tcg_opt_gen_movi(s, op, args, args[0], 0);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800964 continue;
965 }
966 if (affected == 0) {
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +0200967 tcg_debug_assert(nb_oargs == 1);
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200968 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Paolo Bonzini633f6502013-01-11 15:42:53 -0800969 continue;
970 }
971
Aurelien Jarno56e49432012-09-06 16:47:13 +0200972 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700973 switch (opc) {
Aurelien Jarno61251c02012-09-06 16:47:14 +0200974 CASE_OP_32_64(and):
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400975 CASE_OP_32_64(mul):
Richard Henderson03271522013-08-14 14:35:56 -0700976 CASE_OP_32_64(muluh):
977 CASE_OP_32_64(mulsh):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +0200978 if ((temp_is_const(args[2]) && temps[args[2]].val == 0)) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +0200979 tcg_opt_gen_movi(s, op, args, args[0], 0);
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400980 continue;
981 }
982 break;
Aurelien Jarno56e49432012-09-06 16:47:13 +0200983 default:
984 break;
985 }
986
987 /* Simplify expression for "op r, a, a => mov r, a" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700988 switch (opc) {
Kirill Batuzov9a810902011-07-07 16:37:15 +0400989 CASE_OP_32_64(or):
990 CASE_OP_32_64(and):
Aurelien Jarno0aba1c72012-09-18 19:11:32 +0200991 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +0200992 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
Kirill Batuzov9a810902011-07-07 16:37:15 +0400993 continue;
994 }
995 break;
Blue Swirlfe0de7a2011-07-30 19:18:32 +0000996 default:
997 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +0400998 }
999
Aurelien Jarno3c941932012-09-18 19:12:36 +02001000 /* Simplify expression for "op r, a, a => movi r, 0" cases */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001001 switch (opc) {
Richard Hendersone64e9582014-01-28 13:26:17 -08001002 CASE_OP_32_64(andc):
Aurelien Jarno3c941932012-09-18 19:12:36 +02001003 CASE_OP_32_64(sub):
1004 CASE_OP_32_64(xor):
1005 if (temps_are_copies(args[1], args[2])) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001006 tcg_opt_gen_movi(s, op, args, args[0], 0);
Aurelien Jarno3c941932012-09-18 19:12:36 +02001007 continue;
1008 }
1009 break;
1010 default:
1011 break;
1012 }
1013
Kirill Batuzov22613af2011-07-07 16:37:13 +04001014 /* Propagate constants through copy operations and do constant
1015 folding. Constants will be substituted to arguments by register
1016 allocator where needed and possible. Also detect copies. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001017 switch (opc) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001018 CASE_OP_32_64(mov):
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001019 tcg_opt_gen_mov(s, op, args, args[0], args[1]);
1020 break;
Kirill Batuzov22613af2011-07-07 16:37:13 +04001021 CASE_OP_32_64(movi):
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001022 tcg_opt_gen_movi(s, op, args, args[0], args[1]);
Kirill Batuzov22613af2011-07-07 16:37:13 +04001023 break;
Richard Henderson6e14e912012-10-02 11:32:24 -07001024
Kirill Batuzova640f032011-07-07 16:37:17 +04001025 CASE_OP_32_64(not):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001026 CASE_OP_32_64(neg):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001027 CASE_OP_32_64(ext8s):
1028 CASE_OP_32_64(ext8u):
1029 CASE_OP_32_64(ext16s):
1030 CASE_OP_32_64(ext16u):
Kirill Batuzova640f032011-07-07 16:37:17 +04001031 case INDEX_op_ext32s_i64:
1032 case INDEX_op_ext32u_i64:
Aurelien Jarno8bcb5c82015-07-27 12:41:45 +02001033 case INDEX_op_ext_i32_i64:
1034 case INDEX_op_extu_i32_i64:
Richard Henderson609ad702015-07-24 07:16:00 -07001035 case INDEX_op_extrl_i64_i32:
1036 case INDEX_op_extrh_i64_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001037 if (temp_is_const(args[1])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001038 tmp = do_constant_folding(opc, temps[args[1]].val, 0);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001039 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001040 break;
Kirill Batuzova640f032011-07-07 16:37:17 +04001041 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001042 goto do_default;
1043
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001044 CASE_OP_32_64(add):
1045 CASE_OP_32_64(sub):
1046 CASE_OP_32_64(mul):
Kirill Batuzov9a810902011-07-07 16:37:15 +04001047 CASE_OP_32_64(or):
1048 CASE_OP_32_64(and):
1049 CASE_OP_32_64(xor):
Kirill Batuzov55c09752011-07-07 16:37:16 +04001050 CASE_OP_32_64(shl):
1051 CASE_OP_32_64(shr):
1052 CASE_OP_32_64(sar):
Richard Henderson25c4d9c2011-08-17 14:11:46 -07001053 CASE_OP_32_64(rotl):
1054 CASE_OP_32_64(rotr):
Richard Hendersoncb25c802011-08-17 14:11:47 -07001055 CASE_OP_32_64(andc):
1056 CASE_OP_32_64(orc):
1057 CASE_OP_32_64(eqv):
1058 CASE_OP_32_64(nand):
1059 CASE_OP_32_64(nor):
Richard Henderson03271522013-08-14 14:35:56 -07001060 CASE_OP_32_64(muluh):
1061 CASE_OP_32_64(mulsh):
Richard Henderson01547f72013-08-14 15:22:46 -07001062 CASE_OP_32_64(div):
1063 CASE_OP_32_64(divu):
1064 CASE_OP_32_64(rem):
1065 CASE_OP_32_64(remu):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001066 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001067 tmp = do_constant_folding(opc, temps[args[1]].val,
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001068 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001069 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001070 break;
Kirill Batuzov53108fb2011-07-07 16:37:14 +04001071 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001072 goto do_default;
1073
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001074 CASE_OP_32_64(deposit):
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001075 if (temp_is_const(args[1]) && temp_is_const(args[2])) {
Richard Hendersond998e552014-03-18 14:23:52 -07001076 tmp = deposit64(temps[args[1]].val, args[3], args[4],
1077 temps[args[2]].val);
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001078 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001079 break;
Aurelien Jarno7ef55fc2012-09-21 11:07:29 +02001080 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001081 goto do_default;
1082
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001083 CASE_OP_32_64(setcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001084 tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001085 if (tmp != 2) {
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001086 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6e14e912012-10-02 11:32:24 -07001087 break;
Aurelien Jarnof8dd19e2012-09-06 16:47:14 +02001088 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001089 goto do_default;
1090
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001091 CASE_OP_32_64(brcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001092 tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001093 if (tmp != 2) {
1094 if (tmp) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001095 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001096 op->opc = INDEX_op_br;
1097 args[0] = args[3];
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001098 } else {
Richard Henderson0c627cd2014-03-30 16:51:54 -07001099 tcg_op_remove(s, op);
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001100 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001101 break;
Aurelien Jarnofbeaa262012-09-06 16:47:14 +02001102 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001103 goto do_default;
1104
Richard Hendersonfa01a202012-09-21 10:13:37 -07001105 CASE_OP_32_64(movcond):
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001106 tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]);
Aurelien Jarnob336ceb2012-09-18 19:37:00 +02001107 if (tmp != 2) {
Aurelien Jarno97a79eb2015-06-05 11:19:18 +02001108 tcg_opt_gen_mov(s, op, args, args[0], args[4-tmp]);
Richard Henderson6e14e912012-10-02 11:32:24 -07001109 break;
Richard Hendersonfa01a202012-09-21 10:13:37 -07001110 }
Richard Henderson6e14e912012-10-02 11:32:24 -07001111 goto do_default;
1112
Richard Henderson212c3282012-10-02 11:32:28 -07001113 case INDEX_op_add2_i32:
1114 case INDEX_op_sub2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001115 if (temp_is_const(args[2]) && temp_is_const(args[3])
1116 && temp_is_const(args[4]) && temp_is_const(args[5])) {
Richard Henderson212c3282012-10-02 11:32:28 -07001117 uint32_t al = temps[args[2]].val;
1118 uint32_t ah = temps[args[3]].val;
1119 uint32_t bl = temps[args[4]].val;
1120 uint32_t bh = temps[args[5]].val;
1121 uint64_t a = ((uint64_t)ah << 32) | al;
1122 uint64_t b = ((uint64_t)bh << 32) | bl;
1123 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001124 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1125 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson212c3282012-10-02 11:32:28 -07001126
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001127 if (opc == INDEX_op_add2_i32) {
Richard Henderson212c3282012-10-02 11:32:28 -07001128 a += b;
1129 } else {
1130 a -= b;
1131 }
1132
Richard Henderson212c3282012-10-02 11:32:28 -07001133 rl = args[0];
1134 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001135 tcg_opt_gen_movi(s, op, args, rl, (int32_t)a);
1136 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(a >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001137
1138 /* We've done all we need to do with the movi. Skip it. */
1139 oi_next = op2->next;
Richard Henderson212c3282012-10-02 11:32:28 -07001140 break;
1141 }
1142 goto do_default;
1143
Richard Henderson14149682012-10-02 11:32:30 -07001144 case INDEX_op_mulu2_i32:
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001145 if (temp_is_const(args[2]) && temp_is_const(args[3])) {
Richard Henderson14149682012-10-02 11:32:30 -07001146 uint32_t a = temps[args[2]].val;
1147 uint32_t b = temps[args[3]].val;
1148 uint64_t r = (uint64_t)a * b;
1149 TCGArg rl, rh;
Richard Hendersona4ce0992014-03-30 17:14:02 -07001150 TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
1151 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
Richard Henderson14149682012-10-02 11:32:30 -07001152
1153 rl = args[0];
1154 rh = args[1];
Aurelien Jarno29f3ff82015-07-10 18:03:31 +02001155 tcg_opt_gen_movi(s, op, args, rl, (int32_t)r);
1156 tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(r >> 32));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001157
1158 /* We've done all we need to do with the movi. Skip it. */
1159 oi_next = op2->next;
Richard Henderson14149682012-10-02 11:32:30 -07001160 break;
1161 }
1162 goto do_default;
1163
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001164 case INDEX_op_brcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001165 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1166 if (tmp != 2) {
1167 if (tmp) {
Richard Hendersona7635512014-04-23 22:18:30 -07001168 do_brcond_true:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001169 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001170 op->opc = INDEX_op_br;
1171 args[0] = args[5];
Richard Henderson6c4382f2012-10-02 11:32:27 -07001172 } else {
Richard Hendersona7635512014-04-23 22:18:30 -07001173 do_brcond_false:
Richard Henderson0c627cd2014-03-30 16:51:54 -07001174 tcg_op_remove(s, op);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001175 }
1176 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001177 && temp_is_const(args[2]) && temps[args[2]].val == 0
1178 && temp_is_const(args[3]) && temps[args[3]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001179 /* Simplify LT/GE comparisons vs zero to a single compare
1180 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001181 do_brcond_high:
Paolo Bonzinid193a142013-01-11 15:42:51 -08001182 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001183 op->opc = INDEX_op_brcond_i32;
1184 args[0] = args[1];
1185 args[1] = args[3];
1186 args[2] = args[4];
1187 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001188 } else if (args[4] == TCG_COND_EQ) {
1189 /* Simplify EQ comparisons where one of the pairs
1190 can be simplified. */
1191 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1192 args[0], args[2], TCG_COND_EQ);
1193 if (tmp == 0) {
1194 goto do_brcond_false;
1195 } else if (tmp == 1) {
1196 goto do_brcond_high;
1197 }
1198 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1199 args[1], args[3], TCG_COND_EQ);
1200 if (tmp == 0) {
1201 goto do_brcond_false;
1202 } else if (tmp != 1) {
1203 goto do_default;
1204 }
1205 do_brcond_low:
1206 reset_all_temps(nb_temps);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001207 op->opc = INDEX_op_brcond_i32;
1208 args[1] = args[2];
1209 args[2] = args[4];
1210 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001211 } else if (args[4] == TCG_COND_NE) {
1212 /* Simplify NE comparisons where one of the pairs
1213 can be simplified. */
1214 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1215 args[0], args[2], TCG_COND_NE);
1216 if (tmp == 0) {
1217 goto do_brcond_high;
1218 } else if (tmp == 1) {
1219 goto do_brcond_true;
1220 }
1221 tmp = do_constant_folding_cond(INDEX_op_brcond_i32,
1222 args[1], args[3], TCG_COND_NE);
1223 if (tmp == 0) {
1224 goto do_brcond_low;
1225 } else if (tmp == 1) {
1226 goto do_brcond_true;
1227 }
1228 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001229 } else {
1230 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001231 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001232 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001233
1234 case INDEX_op_setcond2_i32:
Richard Henderson6c4382f2012-10-02 11:32:27 -07001235 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1236 if (tmp != 2) {
Richard Hendersona7635512014-04-23 22:18:30 -07001237 do_setcond_const:
Aurelien Jarnoebd27392015-06-04 21:53:23 +02001238 tcg_opt_gen_movi(s, op, args, args[0], tmp);
Richard Henderson6c4382f2012-10-02 11:32:27 -07001239 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
Aurelien Jarnod9c769c2015-07-27 12:41:44 +02001240 && temp_is_const(args[3]) && temps[args[3]].val == 0
1241 && temp_is_const(args[4]) && temps[args[4]].val == 0) {
Richard Henderson6c4382f2012-10-02 11:32:27 -07001242 /* Simplify LT/GE comparisons vs zero to a single compare
1243 vs the high word of the input. */
Richard Hendersona7635512014-04-23 22:18:30 -07001244 do_setcond_high:
Aurelien Jarno66e61b52013-05-08 22:36:39 +02001245 reset_temp(args[0]);
Richard Hendersona7635512014-04-23 22:18:30 -07001246 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001247 op->opc = INDEX_op_setcond_i32;
1248 args[1] = args[2];
1249 args[2] = args[4];
1250 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001251 } else if (args[5] == TCG_COND_EQ) {
1252 /* Simplify EQ comparisons where one of the pairs
1253 can be simplified. */
1254 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1255 args[1], args[3], TCG_COND_EQ);
1256 if (tmp == 0) {
1257 goto do_setcond_const;
1258 } else if (tmp == 1) {
1259 goto do_setcond_high;
1260 }
1261 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1262 args[2], args[4], TCG_COND_EQ);
1263 if (tmp == 0) {
1264 goto do_setcond_high;
1265 } else if (tmp != 1) {
1266 goto do_default;
1267 }
1268 do_setcond_low:
1269 reset_temp(args[0]);
1270 temps[args[0]].mask = 1;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001271 op->opc = INDEX_op_setcond_i32;
1272 args[2] = args[3];
1273 args[3] = args[5];
Richard Hendersona7635512014-04-23 22:18:30 -07001274 } else if (args[5] == TCG_COND_NE) {
1275 /* Simplify NE comparisons where one of the pairs
1276 can be simplified. */
1277 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1278 args[1], args[3], TCG_COND_NE);
1279 if (tmp == 0) {
1280 goto do_setcond_high;
1281 } else if (tmp == 1) {
1282 goto do_setcond_const;
1283 }
1284 tmp = do_constant_folding_cond(INDEX_op_setcond_i32,
1285 args[2], args[4], TCG_COND_NE);
1286 if (tmp == 0) {
1287 goto do_setcond_low;
1288 } else if (tmp == 1) {
1289 goto do_setcond_const;
1290 }
1291 goto do_default;
Richard Henderson6c4382f2012-10-02 11:32:27 -07001292 } else {
1293 goto do_default;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001294 }
Richard Henderson6c4382f2012-10-02 11:32:27 -07001295 break;
Richard Hendersonbc1473e2012-10-02 11:32:25 -07001296
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001297 case INDEX_op_call:
Richard Hendersoncf066672014-03-22 20:06:52 -07001298 if (!(args[nb_oargs + nb_iargs + 1]
1299 & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
Kirill Batuzov22613af2011-07-07 16:37:13 +04001300 for (i = 0; i < nb_globals; i++) {
Aurelien Jarno1208d7d2015-07-27 12:41:44 +02001301 if (test_bit(i, temps_used.l)) {
1302 reset_temp(i);
1303 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001304 }
1305 }
Richard Hendersoncf066672014-03-22 20:06:52 -07001306 goto do_reset_output;
Richard Henderson6e14e912012-10-02 11:32:24 -07001307
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001308 default:
Richard Henderson6e14e912012-10-02 11:32:24 -07001309 do_default:
1310 /* Default case: we know nothing about operation (or were unable
1311 to compute the operation result) so no propagation is done.
1312 We trash everything if the operation is the end of a basic
Paolo Bonzini3a9d8b12013-01-11 15:42:52 -08001313 block, otherwise we only trash the output args. "mask" is
1314 the non-zero bits mask for the first output arg. */
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001315 if (def->flags & TCG_OPF_BB_END) {
Paolo Bonzinid193a142013-01-11 15:42:51 -08001316 reset_all_temps(nb_temps);
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001317 } else {
Richard Hendersoncf066672014-03-22 20:06:52 -07001318 do_reset_output:
1319 for (i = 0; i < nb_oargs; i++) {
Aurelien Jarnoe590d4e2012-09-11 12:31:21 +02001320 reset_temp(args[i]);
Aurelien Jarno30312442013-09-03 08:27:38 +02001321 /* Save the corresponding known-zero bits mask for the
1322 first output argument (only one supported so far). */
1323 if (i == 0) {
1324 temps[args[i]].mask = mask;
1325 }
Aurelien Jarnoa2550662012-09-19 21:40:30 +02001326 }
Kirill Batuzov22613af2011-07-07 16:37:13 +04001327 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001328 break;
1329 }
1330 }
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001331}