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 SIZEOF_REGISTER == 8
226 if ((arg1->opcode == OP_ICONST) || (arg1->opcode == OP_I8CONST)) {
228 if (arg1->opcode == OP_ICONST) {
230 ALLOC_DEST (cfg, dest, ins);
231 dest->opcode = arg1->opcode;
232 MONO_INST_NULLIFY_SREGS (dest);
233 dest->inst_c0 = arg1->inst_c0;
237 if (arg1->opcode == OP_VZERO) {
238 ALLOC_DEST (cfg, dest, ins);
239 dest->opcode = OP_VZERO;
244 if (arg1->opcode == OP_XZERO) {
245 ALLOC_DEST (cfg, dest, ins);
246 dest->opcode = OP_XZERO;
253 case OP_ICOMPARE_IMM: {
255 if (ins->sreg2 == -1) {
257 arg2->opcode = OP_ICONST;
258 arg2->inst_c0 = ins->inst_imm;
261 if ((arg1->opcode == OP_ICONST) && (arg2->opcode == OP_ICONST) && ins->next) {
262 MonoInst *next = ins->next;
263 gboolean res = FALSE;
265 switch (next->opcode) {
276 switch (next->opcode) {
277 FOLD_BINOPCXX (OP_CEQ,==,gint32);
278 FOLD_BINOPCXX (OP_ICEQ,==,gint32);
279 FOLD_BINOPCXX (OP_CGT,>,gint32);
280 FOLD_BINOPCXX (OP_ICGT,>,gint32);
281 FOLD_BINOPCXX (OP_CGT_UN,>,guint32);
282 FOLD_BINOPCXX (OP_ICGT_UN,>,guint32);
283 FOLD_BINOPCXX (OP_CLT,<,gint32);
284 FOLD_BINOPCXX (OP_ICLT,<,gint32);
285 FOLD_BINOPCXX (OP_CLT_UN,<,guint32);
286 FOLD_BINOPCXX (OP_ICLT_UN,<,guint32);
291 next->opcode = OP_ICONST;
293 MONO_INST_NULLIFY_SREGS (next);
295 ALLOC_DEST (cfg, dest, ins);
296 dest->opcode = OP_ICONST;
310 switch (next->opcode) {
311 FOLD_BINOPCXX (OP_IBEQ,==,gint32);
312 FOLD_BINOPCXX (OP_IBNE_UN,!=,guint32);
313 FOLD_BINOPCXX (OP_IBGT,>,gint32);
314 FOLD_BINOPCXX (OP_IBGT_UN,>,guint32);
315 FOLD_BINOPCXX (OP_IBGE,>=,gint32);
316 FOLD_BINOPCXX (OP_IBGE_UN,>=,guint32);
317 FOLD_BINOPCXX (OP_IBLT,<,gint32);
318 FOLD_BINOPCXX (OP_IBLT_UN,<,guint32);
319 FOLD_BINOPCXX (OP_IBLE,<=,gint32);
320 FOLD_BINOPCXX (OP_IBLE_UN,<=,guint32);
325 * Can't nullify OP_COMPARE here since the decompose long branch
326 * opcodes depend on it being executed. Also, the branch might not
327 * be eliminated after all if loop opts is disabled, for example.
330 next->flags |= MONO_INST_CFOLD_TAKEN;
332 next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
334 ALLOC_DEST (cfg, dest, ins);
335 dest->opcode = OP_ICONST;
340 res = arg1->inst_c0 == arg2->inst_c0;
346 ALLOC_DEST (cfg, dest, ins);
347 dest->opcode = OP_ICONST;
354 /* This happens when a conditional branch is eliminated */
355 if (next->next == NULL) {
365 if ((arg1->opcode == OP_PCONST) && (arg2->opcode == OP_PCONST) && ins->next) {
366 MonoInst *next = ins->next;
368 if (next->opcode == OP_LCEQ) {
369 gboolean res = arg1->inst_p0 == arg2->inst_p0;
372 next->opcode = OP_ICONST;
374 MONO_INST_NULLIFY_SREGS (next);
376 ALLOC_DEST (cfg, dest, ins);
377 dest->opcode = OP_ICONST;
386 if (arg1->opcode == OP_R8CONST) {
387 ALLOC_DEST (cfg, dest, ins);
388 dest->opcode = OP_R8CONST;
390 dest->inst_p0 = arg1->inst_p0;
397 * *ovf* opcodes? It's slow and hard to do in C.
398 * switch can be replaced by a simple jump
408 #endif /* DISABLE_JIT */