3 * Constant folding support
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc. http://www.ximian.com
16 /* WTF is this doing here?!?!? */
18 mono_is_power_of_two (guint32 val
)
22 for (i
= 0, j
= 1, k
= 0xfffffffe; i
< 32; ++i
, j
= j
<< 1, k
= k
<< 1) {
26 if (i
== 32 || val
& k
)
32 #define MYGINT32_MAX 2147483647
33 #define G_MININT32 (-MYGINT32_MAX -1)
36 #define FOLD_UNOP(name,op) \
38 dest->inst_c0 = op arg1->inst_c0; \
41 #define FOLD_BINOP(name, op) \
43 dest->inst_c0 = arg1->inst_c0 op arg2->inst_c0; \
46 #define FOLD_FBINOP(name, op) \
48 dest->inst_p0 = (double *)mono_domain_alloc (cfg->domain, sizeof (double)); \
49 *(double *)dest->inst_p0 = (*((double *) arg1->inst_p0)) op (*((double *) arg2->inst_p0)); \
52 #define FOLD_BINOPC(name,op,cast) \
54 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
57 #define FOLD_BINOP2_IMM(name, op) \
59 dest->inst_c0 = arg1->inst_c0 op ins->inst_imm; \
62 #define FOLD_BINOPC2_IMM(name, op, cast) \
64 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)ins->inst_imm; \
67 #define FOLD_BINOPCXX(name,op,cast) \
69 res = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
72 #define ALLOC_DEST(cfg, dest, ins) do { \
74 MONO_INST_NEW ((cfg), (dest), -1); \
75 (dest)->dreg = (ins)->dreg; \
82 * mono_constant_fold_ins:
84 * Perform constant folding on INS, using ARG1 and ARG2 as the arguments. If OVERWRITE is
85 * true, then store the result back into INS and return INS. Otherwise allocate a new ins,
86 * store the result into it and return it. If constant folding cannot be performed, return
90 mono_constant_fold_ins (MonoCompile
*cfg
, MonoInst
*ins
, MonoInst
*arg1
, MonoInst
*arg2
, gboolean overwrite
)
92 MonoInst
*dest
= NULL
;
97 switch (ins
->opcode
) {
100 if (arg2
->opcode
== OP_R8CONST
&& arg2
->opcode
== OP_R8CONST
){
101 ALLOC_DEST (cfg
, dest
, ins
);
102 switch (ins
->opcode
) {
103 FOLD_FBINOP (OP_FADD
, +);
104 FOLD_FBINOP (OP_FMUL
, *);
106 dest
->opcode
= OP_R8CONST
;
107 MONO_INST_NULLIFY_SREGS (dest
);
115 if (arg2
->opcode
== OP_ICONST
) {
116 if (arg1
->opcode
== OP_ICONST
) {
117 ALLOC_DEST (cfg
, dest
, ins
);
118 switch (ins
->opcode
) {
119 FOLD_BINOP (OP_IMUL
, *);
120 FOLD_BINOP (OP_IADD
, +);
121 FOLD_BINOP (OP_IAND
, &);
122 FOLD_BINOP (OP_IOR
, |);
123 FOLD_BINOP (OP_IXOR
, ^);
125 dest
->opcode
= OP_ICONST
;
126 MONO_INST_NULLIFY_SREGS (dest
);
128 } else if (arg1
->opcode
== OP_ICONST
) {
130 * This is commutative so swap the arguments, allowing the _imm variant
133 if (mono_op_to_op_imm (ins
->opcode
) != -1) {
134 ALLOC_DEST (cfg
, dest
, ins
);
135 dest
->opcode
= mono_op_to_op_imm (ins
->opcode
);
136 dest
->sreg1
= ins
->sreg2
;
138 dest
->inst_imm
= arg1
->inst_c0
;
152 if (arg1
->opcode
== OP_ICONST
) {
153 ALLOC_DEST (cfg
, dest
, ins
);
154 switch (ins
->opcode
) {
155 FOLD_BINOP2_IMM (OP_IMUL_IMM
, *);
156 FOLD_BINOP2_IMM (OP_IADD_IMM
, +);
157 FOLD_BINOP2_IMM (OP_IAND_IMM
, &);
158 FOLD_BINOP2_IMM (OP_IOR_IMM
, |);
159 FOLD_BINOP2_IMM (OP_IXOR_IMM
, ^);
160 FOLD_BINOP2_IMM (OP_ISUB_IMM
, -);
161 FOLD_BINOPC2_IMM (OP_ISHL_IMM
, <<, gint32
);
162 FOLD_BINOPC2_IMM (OP_ISHR_IMM
, >>, gint32
);
163 FOLD_BINOPC2_IMM (OP_ISHR_UN_IMM
, >>, guint32
);
164 FOLD_BINOP2_IMM (OP_SHL_IMM
, <<);
166 dest
->opcode
= OP_ICONST
;
167 MONO_INST_NULLIFY_SREGS (dest
);
174 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
)) {
175 ALLOC_DEST (cfg
, dest
, ins
);
176 switch (ins
->opcode
) {
177 FOLD_BINOP (OP_ISUB
, -);
178 FOLD_BINOP (OP_ISHL
, <<);
179 FOLD_BINOP (OP_ISHR
, >>);
180 FOLD_BINOPC (OP_ISHR_UN
, >>, guint32
);
182 dest
->opcode
= OP_ICONST
;
183 MONO_INST_NULLIFY_SREGS (dest
);
190 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
)) {
191 if ((arg2
->inst_c0
== 0) || ((arg1
->inst_c0
== G_MININT32
) && (arg2
->inst_c0
== -1)))
193 ALLOC_DEST (cfg
, dest
, ins
);
194 switch (ins
->opcode
) {
195 FOLD_BINOPC (OP_IDIV
, /, gint32
);
196 FOLD_BINOPC (OP_IDIV_UN
, /, guint32
);
197 FOLD_BINOPC (OP_IREM
, %, gint32
);
198 FOLD_BINOPC (OP_IREM_UN
, %, guint32
);
200 dest
->opcode
= OP_ICONST
;
201 MONO_INST_NULLIFY_SREGS (dest
);
208 if (arg1
->opcode
== OP_ICONST
) {
209 if ((ins
->inst_imm
== 0) || ((arg1
->inst_c0
== G_MININT32
) && (ins
->inst_imm
== -1)))
211 ALLOC_DEST (cfg
, dest
, ins
);
212 switch (ins
->opcode
) {
213 FOLD_BINOPC2_IMM (OP_IDIV_IMM
, /, gint32
);
214 FOLD_BINOPC2_IMM (OP_IDIV_UN_IMM
, /, guint32
);
215 FOLD_BINOPC2_IMM (OP_IREM_IMM
, %, gint32
);
216 FOLD_BINOPC2_IMM (OP_IREM_UN_IMM
, %, guint32
);
218 g_assert_not_reached ();
220 dest
->opcode
= OP_ICONST
;
221 MONO_INST_NULLIFY_SREGS (dest
);
227 if (arg1
->opcode
== OP_ICONST
) {
228 /* INEG sets cflags on x86, and the LNEG decomposition depends on that */
229 #if SIZEOF_REGISTER == 4
230 if (ins
->opcode
== OP_INEG
)
233 ALLOC_DEST (cfg
, dest
, ins
);
234 switch (ins
->opcode
) {
235 FOLD_UNOP (OP_INEG
,-);
236 FOLD_UNOP (OP_INOT
,~);
238 dest
->opcode
= OP_ICONST
;
239 MONO_INST_NULLIFY_SREGS (dest
);
243 if (arg1
->opcode
== OP_ICONST
&& arg1
->inst_c0
>= 0 && arg1
->inst_c0
< (1 << 16) && overwrite
) {
244 dest
->opcode
= OP_ICONST
;
246 dest
->inst_c0
= arg1
->inst_c0
;
250 #if SIZEOF_REGISTER == 8
251 if ((arg1
->opcode
== OP_ICONST
) || (arg1
->opcode
== OP_I8CONST
)) {
253 if (arg1
->opcode
== OP_ICONST
) {
255 ALLOC_DEST (cfg
, dest
, ins
);
256 dest
->opcode
= arg1
->opcode
;
257 MONO_INST_NULLIFY_SREGS (dest
);
258 dest
->inst_c0
= arg1
->inst_c0
;
262 if (arg1
->opcode
== OP_VZERO
) {
263 ALLOC_DEST (cfg
, dest
, ins
);
264 dest
->opcode
= OP_VZERO
;
269 if (arg1
->opcode
== OP_XZERO
) {
270 ALLOC_DEST (cfg
, dest
, ins
);
271 dest
->opcode
= OP_XZERO
;
278 case OP_ICOMPARE_IMM
: {
280 if (ins
->sreg2
== -1) {
282 arg2
->opcode
= OP_ICONST
;
283 arg2
->inst_c0
= ins
->inst_imm
;
286 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
) && ins
->next
) {
287 MonoInst
*next
= ins
->next
;
288 gboolean res
= FALSE
;
290 switch (next
->opcode
) {
301 switch (next
->opcode
) {
302 FOLD_BINOPCXX (OP_CEQ
,==,gint32
);
303 FOLD_BINOPCXX (OP_ICEQ
,==,gint32
);
304 FOLD_BINOPCXX (OP_CGT
,>,gint32
);
305 FOLD_BINOPCXX (OP_ICGT
,>,gint32
);
306 FOLD_BINOPCXX (OP_CGT_UN
,>,guint32
);
307 FOLD_BINOPCXX (OP_ICGT_UN
,>,guint32
);
308 FOLD_BINOPCXX (OP_CLT
,<,gint32
);
309 FOLD_BINOPCXX (OP_ICLT
,<,gint32
);
310 FOLD_BINOPCXX (OP_CLT_UN
,<,guint32
);
311 FOLD_BINOPCXX (OP_ICLT_UN
,<,guint32
);
316 next
->opcode
= OP_ICONST
;
318 MONO_INST_NULLIFY_SREGS (next
);
320 ALLOC_DEST (cfg
, dest
, ins
);
321 dest
->opcode
= OP_ICONST
;
335 switch (next
->opcode
) {
336 FOLD_BINOPCXX (OP_IBEQ
,==,gint32
);
337 FOLD_BINOPCXX (OP_IBNE_UN
,!=,guint32
);
338 FOLD_BINOPCXX (OP_IBGT
,>,gint32
);
339 FOLD_BINOPCXX (OP_IBGT_UN
,>,guint32
);
340 FOLD_BINOPCXX (OP_IBGE
,>=,gint32
);
341 FOLD_BINOPCXX (OP_IBGE_UN
,>=,guint32
);
342 FOLD_BINOPCXX (OP_IBLT
,<,gint32
);
343 FOLD_BINOPCXX (OP_IBLT_UN
,<,guint32
);
344 FOLD_BINOPCXX (OP_IBLE
,<=,gint32
);
345 FOLD_BINOPCXX (OP_IBLE_UN
,<=,guint32
);
350 * Can't nullify OP_COMPARE here since the decompose long branch
351 * opcodes depend on it being executed. Also, the branch might not
352 * be eliminated after all if loop opts is disabled, for example.
355 next
->flags
|= MONO_INST_CFOLD_TAKEN
;
357 next
->flags
|= MONO_INST_CFOLD_NOT_TAKEN
;
359 ALLOC_DEST (cfg
, dest
, ins
);
360 dest
->opcode
= OP_ICONST
;
365 res
= arg1
->inst_c0
== arg2
->inst_c0
;
371 ALLOC_DEST (cfg
, dest
, ins
);
372 dest
->opcode
= OP_ICONST
;
379 /* This happens when a conditional branch is eliminated */
380 if (next
->next
== NULL
) {
390 if ((arg1
->opcode
== OP_PCONST
) && (arg2
->opcode
== OP_PCONST
) && ins
->next
) {
391 MonoInst
*next
= ins
->next
;
393 if (next
->opcode
== OP_LCEQ
) {
394 gboolean res
= arg1
->inst_p0
== arg2
->inst_p0
;
397 next
->opcode
= OP_ICONST
;
399 MONO_INST_NULLIFY_SREGS (next
);
401 ALLOC_DEST (cfg
, dest
, ins
);
402 dest
->opcode
= OP_ICONST
;
411 if (arg1
->opcode
== OP_R8CONST
) {
412 ALLOC_DEST (cfg
, dest
, ins
);
413 dest
->opcode
= OP_R8CONST
;
415 dest
->inst_p0
= arg1
->inst_p0
;
422 * *ovf* opcodes? It's slow and hard to do in C.
423 * switch can be replaced by a simple jump
433 #endif /* DISABLE_JIT */