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_BINOPC(name,op,cast) \
48 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
51 #define FOLD_BINOP2_IMM(name, op) \
53 dest->inst_c0 = arg1->inst_c0 op ins->inst_imm; \
56 #define FOLD_BINOPC2_IMM(name, op, cast) \
58 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)ins->inst_imm; \
61 #define FOLD_BINOPCXX(name,op,cast) \
63 res = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
66 #define ALLOC_DEST(cfg, dest, ins) do { \
68 MONO_INST_NEW ((cfg), (dest), -1); \
69 (dest)->dreg = (ins)->dreg; \
76 * mono_constant_fold_ins:
78 * Perform constant folding on INS, using ARG1 and ARG2 as the arguments. If OVERWRITE is
79 * true, then store the result back into INS and return INS. Otherwise allocate a new ins,
80 * store the result into it and return it. If constant folding cannot be performed, return
84 mono_constant_fold_ins (MonoCompile
*cfg
, MonoInst
*ins
, MonoInst
*arg1
, MonoInst
*arg2
, gboolean overwrite
)
86 MonoInst
*dest
= NULL
;
91 switch (ins
->opcode
) {
97 if (arg2
->opcode
== OP_ICONST
) {
98 if (arg1
->opcode
== OP_ICONST
) {
99 ALLOC_DEST (cfg
, dest
, ins
);
100 switch (ins
->opcode
) {
101 FOLD_BINOP (OP_IMUL
, *);
102 FOLD_BINOP (OP_IADD
, +);
103 FOLD_BINOP (OP_IAND
, &);
104 FOLD_BINOP (OP_IOR
, |);
105 FOLD_BINOP (OP_IXOR
, ^);
107 dest
->opcode
= OP_ICONST
;
108 MONO_INST_NULLIFY_SREGS (dest
);
110 } else if (arg1
->opcode
== OP_ICONST
) {
112 * This is commutative so swap the arguments, allowing the _imm variant
115 if (mono_op_to_op_imm (ins
->opcode
) != -1) {
116 ALLOC_DEST (cfg
, dest
, ins
);
117 dest
->opcode
= mono_op_to_op_imm (ins
->opcode
);
118 dest
->sreg1
= ins
->sreg2
;
120 dest
->inst_imm
= arg1
->inst_c0
;
134 if (arg1
->opcode
== OP_ICONST
) {
135 ALLOC_DEST (cfg
, dest
, ins
);
136 switch (ins
->opcode
) {
137 FOLD_BINOP2_IMM (OP_IMUL_IMM
, *);
138 FOLD_BINOP2_IMM (OP_IADD_IMM
, +);
139 FOLD_BINOP2_IMM (OP_IAND_IMM
, &);
140 FOLD_BINOP2_IMM (OP_IOR_IMM
, |);
141 FOLD_BINOP2_IMM (OP_IXOR_IMM
, ^);
142 FOLD_BINOP2_IMM (OP_ISUB_IMM
, -);
143 FOLD_BINOPC2_IMM (OP_ISHL_IMM
, <<, gint32
);
144 FOLD_BINOPC2_IMM (OP_ISHR_IMM
, >>, gint32
);
145 FOLD_BINOPC2_IMM (OP_ISHR_UN_IMM
, >>, guint32
);
146 FOLD_BINOP2_IMM (OP_SHL_IMM
, <<);
148 dest
->opcode
= OP_ICONST
;
149 MONO_INST_NULLIFY_SREGS (dest
);
156 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
)) {
157 ALLOC_DEST (cfg
, dest
, ins
);
158 switch (ins
->opcode
) {
159 FOLD_BINOP (OP_ISUB
, -);
160 FOLD_BINOP (OP_ISHL
, <<);
161 FOLD_BINOP (OP_ISHR
, >>);
162 FOLD_BINOPC (OP_ISHR_UN
, >>, guint32
);
164 dest
->opcode
= OP_ICONST
;
165 MONO_INST_NULLIFY_SREGS (dest
);
172 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
)) {
173 if ((arg2
->inst_c0
== 0) || ((arg1
->inst_c0
== G_MININT32
) && (arg2
->inst_c0
== -1)))
175 ALLOC_DEST (cfg
, dest
, ins
);
176 switch (ins
->opcode
) {
177 FOLD_BINOPC (OP_IDIV
, /, gint32
);
178 FOLD_BINOPC (OP_IDIV_UN
, /, guint32
);
179 FOLD_BINOPC (OP_IREM
, %, gint32
);
180 FOLD_BINOPC (OP_IREM_UN
, %, guint32
);
182 dest
->opcode
= OP_ICONST
;
183 MONO_INST_NULLIFY_SREGS (dest
);
190 if (arg1
->opcode
== OP_ICONST
) {
191 if ((ins
->inst_imm
== 0) || ((arg1
->inst_c0
== G_MININT32
) && (ins
->inst_imm
== -1)))
193 ALLOC_DEST (cfg
, dest
, ins
);
194 switch (ins
->opcode
) {
195 FOLD_BINOPC2_IMM (OP_IDIV_IMM
, /, gint32
);
196 FOLD_BINOPC2_IMM (OP_IDIV_UN_IMM
, /, guint32
);
197 FOLD_BINOPC2_IMM (OP_IREM_IMM
, %, gint32
);
198 FOLD_BINOPC2_IMM (OP_IREM_UN_IMM
, %, guint32
);
200 g_assert_not_reached ();
202 dest
->opcode
= OP_ICONST
;
203 MONO_INST_NULLIFY_SREGS (dest
);
209 if (arg1
->opcode
== OP_ICONST
) {
210 /* INEG sets cflags on x86, and the LNEG decomposition depends on that */
211 #if SIZEOF_REGISTER == 4
212 if (ins
->opcode
== OP_INEG
)
215 ALLOC_DEST (cfg
, dest
, ins
);
216 switch (ins
->opcode
) {
217 FOLD_UNOP (OP_INEG
,-);
218 FOLD_UNOP (OP_INOT
,~);
220 dest
->opcode
= OP_ICONST
;
221 MONO_INST_NULLIFY_SREGS (dest
);
225 if (arg1
->opcode
== OP_ICONST
&& arg1
->inst_c0
>= 0 && arg1
->inst_c0
< (1 << 16) && overwrite
) {
226 dest
->opcode
= OP_ICONST
;
228 dest
->inst_c0
= arg1
->inst_c0
;
232 #if SIZEOF_REGISTER == 8
233 if ((arg1
->opcode
== OP_ICONST
) || (arg1
->opcode
== OP_I8CONST
)) {
235 if (arg1
->opcode
== OP_ICONST
) {
237 ALLOC_DEST (cfg
, dest
, ins
);
238 dest
->opcode
= arg1
->opcode
;
239 MONO_INST_NULLIFY_SREGS (dest
);
240 dest
->inst_c0
= arg1
->inst_c0
;
244 if (arg1
->opcode
== OP_VZERO
) {
245 ALLOC_DEST (cfg
, dest
, ins
);
246 dest
->opcode
= OP_VZERO
;
251 if (arg1
->opcode
== OP_XZERO
) {
252 ALLOC_DEST (cfg
, dest
, ins
);
253 dest
->opcode
= OP_XZERO
;
260 case OP_ICOMPARE_IMM
: {
262 if (ins
->sreg2
== -1) {
264 arg2
->opcode
= OP_ICONST
;
265 arg2
->inst_c0
= ins
->inst_imm
;
268 if ((arg1
->opcode
== OP_ICONST
) && (arg2
->opcode
== OP_ICONST
) && ins
->next
) {
269 MonoInst
*next
= ins
->next
;
270 gboolean res
= FALSE
;
272 switch (next
->opcode
) {
283 switch (next
->opcode
) {
284 FOLD_BINOPCXX (OP_CEQ
,==,gint32
);
285 FOLD_BINOPCXX (OP_ICEQ
,==,gint32
);
286 FOLD_BINOPCXX (OP_CGT
,>,gint32
);
287 FOLD_BINOPCXX (OP_ICGT
,>,gint32
);
288 FOLD_BINOPCXX (OP_CGT_UN
,>,guint32
);
289 FOLD_BINOPCXX (OP_ICGT_UN
,>,guint32
);
290 FOLD_BINOPCXX (OP_CLT
,<,gint32
);
291 FOLD_BINOPCXX (OP_ICLT
,<,gint32
);
292 FOLD_BINOPCXX (OP_CLT_UN
,<,guint32
);
293 FOLD_BINOPCXX (OP_ICLT_UN
,<,guint32
);
298 next
->opcode
= OP_ICONST
;
300 MONO_INST_NULLIFY_SREGS (next
);
302 ALLOC_DEST (cfg
, dest
, ins
);
303 dest
->opcode
= OP_ICONST
;
317 switch (next
->opcode
) {
318 FOLD_BINOPCXX (OP_IBEQ
,==,gint32
);
319 FOLD_BINOPCXX (OP_IBNE_UN
,!=,guint32
);
320 FOLD_BINOPCXX (OP_IBGT
,>,gint32
);
321 FOLD_BINOPCXX (OP_IBGT_UN
,>,guint32
);
322 FOLD_BINOPCXX (OP_IBGE
,>=,gint32
);
323 FOLD_BINOPCXX (OP_IBGE_UN
,>=,guint32
);
324 FOLD_BINOPCXX (OP_IBLT
,<,gint32
);
325 FOLD_BINOPCXX (OP_IBLT_UN
,<,guint32
);
326 FOLD_BINOPCXX (OP_IBLE
,<=,gint32
);
327 FOLD_BINOPCXX (OP_IBLE_UN
,<=,guint32
);
332 * Can't nullify OP_COMPARE here since the decompose long branch
333 * opcodes depend on it being executed. Also, the branch might not
334 * be eliminated after all if loop opts is disabled, for example.
337 next
->flags
|= MONO_INST_CFOLD_TAKEN
;
339 next
->flags
|= MONO_INST_CFOLD_NOT_TAKEN
;
341 ALLOC_DEST (cfg
, dest
, ins
);
342 dest
->opcode
= OP_ICONST
;
347 res
= arg1
->inst_c0
== arg2
->inst_c0
;
353 ALLOC_DEST (cfg
, dest
, ins
);
354 dest
->opcode
= OP_ICONST
;
361 /* This happens when a conditional branch is eliminated */
362 if (next
->next
== NULL
) {
372 if ((arg1
->opcode
== OP_PCONST
) && (arg2
->opcode
== OP_PCONST
) && ins
->next
) {
373 MonoInst
*next
= ins
->next
;
375 if (next
->opcode
== OP_LCEQ
) {
376 gboolean res
= arg1
->inst_p0
== arg2
->inst_p0
;
379 next
->opcode
= OP_ICONST
;
381 MONO_INST_NULLIFY_SREGS (next
);
383 ALLOC_DEST (cfg
, dest
, ins
);
384 dest
->opcode
= OP_ICONST
;
393 if (arg1
->opcode
== OP_R8CONST
) {
394 ALLOC_DEST (cfg
, dest
, ins
);
395 dest
->opcode
= OP_R8CONST
;
397 dest
->inst_p0
= arg1
->inst_p0
;
404 * *ovf* opcodes? It's slow and hard to do in C.
405 * switch can be replaced by a simple jump
415 #endif /* DISABLE_JIT */