2 * cfold.c: Constant folding support
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc. http://www.ximian.com
14 mono_is_power_of_two (guint32 val)
18 for (i = 0, j = 1, k = 0xfffffffe; i < 32; ++i, j = j << 1, k = k << 1) {
22 if (i == 32 || val & k)
28 #define MYGINT32_MAX 2147483647
29 #define G_MININT32 (-MYGINT32_MAX -1)
32 #define FOLD_UNOP(name,op) \
34 dest->inst_c0 = op arg1->inst_c0; \
37 #define FOLD_BINOP(name, op) \
39 dest->inst_c0 = arg1->inst_c0 op arg2->inst_c0; \
42 #define FOLD_BINOPC(name,op,cast) \
44 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
47 #define FOLD_BINOP2_IMM(name, op) \
49 dest->inst_c0 = arg1->inst_c0 op ins->inst_imm; \
52 #define FOLD_BINOPC2_IMM(name, op, cast) \
54 dest->inst_c0 = (cast)arg1->inst_c0 op (cast)ins->inst_imm; \
57 #define FOLD_BINOPCXX(name,op,cast) \
59 res = (cast)arg1->inst_c0 op (cast)arg2->inst_c0; \
62 #define ALLOC_DEST(cfg, dest, ins) do { \
64 MONO_INST_NEW ((cfg), (dest), -1); \
65 (dest)->dreg = (ins)->dreg; \
70 * mono_constant_fold_ins:
72 * Perform constant folding on INS, using ARG1 and ARG2 as the arguments. If OVERWRITE is
73 * true, then store the result back into INS and return INS. Otherwise allocate a new ins,
74 * store the result into it and return it. If constant folding cannot be performed, return
78 mono_constant_fold_ins (MonoCompile *cfg, MonoInst *ins, MonoInst *arg1, MonoInst *arg2, gboolean overwrite)
80 MonoInst *dest = NULL;
85 switch (ins->opcode) {
91 if (arg2->opcode == OP_ICONST) {
92 if (arg1->opcode == OP_ICONST) {
93 ALLOC_DEST (cfg, dest, ins);
94 switch (ins->opcode) {
95 FOLD_BINOP (OP_IMUL, *);
96 FOLD_BINOP (OP_IADD, +);
97 FOLD_BINOP (OP_IAND, &);
98 FOLD_BINOP (OP_IOR, |);
99 FOLD_BINOP (OP_IXOR, ^);
101 dest->opcode = OP_ICONST;
102 MONO_INST_NULLIFY_SREGS (dest);
104 } else if (arg1->opcode == OP_ICONST) {
106 * This is commutative so swap the arguments, allowing the _imm variant
109 if (mono_op_to_op_imm (ins->opcode) != -1) {
110 ALLOC_DEST (cfg, dest, ins);
111 dest->opcode = mono_op_to_op_imm (ins->opcode);
112 dest->sreg1 = ins->sreg2;
114 dest->inst_imm = arg1->inst_c0;
128 if (arg1->opcode == OP_ICONST) {
129 ALLOC_DEST (cfg, dest, ins);
130 switch (ins->opcode) {
131 FOLD_BINOP2_IMM (OP_IMUL_IMM, *);
132 FOLD_BINOP2_IMM (OP_IADD_IMM, +);
133 FOLD_BINOP2_IMM (OP_IAND_IMM, &);
134 FOLD_BINOP2_IMM (OP_IOR_IMM, |);
135 FOLD_BINOP2_IMM (OP_IXOR_IMM, ^);
136 FOLD_BINOP2_IMM (OP_ISUB_IMM, -);
137 FOLD_BINOPC2_IMM (OP_ISHL_IMM, <<, gint32);
138 FOLD_BINOPC2_IMM (OP_ISHR_IMM, >>, gint32);
139 FOLD_BINOPC2_IMM (OP_ISHR_UN_IMM, >>, guint32);
140 FOLD_BINOP2_IMM (OP_SHL_IMM, <<);
142 dest->opcode = OP_ICONST;
143 MONO_INST_NULLIFY_SREGS (dest);
150 if ((arg1->opcode == OP_ICONST) && (arg2->opcode == OP_ICONST)) {
151 ALLOC_DEST (cfg, dest, ins);
152 switch (ins->opcode) {
153 FOLD_BINOP (OP_ISUB, -);
154 FOLD_BINOP (OP_ISHL, <<);
155 FOLD_BINOP (OP_ISHR, >>);
156 FOLD_BINOPC (OP_ISHR_UN, >>, guint32);
158 dest->opcode = OP_ICONST;
159 MONO_INST_NULLIFY_SREGS (dest);
166 if ((arg1->opcode == OP_ICONST) && (arg2->opcode == OP_ICONST)) {
167 if ((arg2->inst_c0 == 0) || ((arg1->inst_c0 == G_MININT32) && (arg2->inst_c0 == -1)))
169 ALLOC_DEST (cfg, dest, ins);
170 switch (ins->opcode) {
171 FOLD_BINOPC (OP_IDIV, /, gint32);
172 FOLD_BINOPC (OP_IDIV_UN, /, guint32);
173 FOLD_BINOPC (OP_IREM, %, gint32);
174 FOLD_BINOPC (OP_IREM_UN, %, guint32);
176 dest->opcode = OP_ICONST;
177 MONO_INST_NULLIFY_SREGS (dest);
184 if (arg1->opcode == OP_ICONST) {
185 if ((ins->inst_imm == 0) || ((arg1->inst_c0 == G_MININT32) && (ins->inst_imm == -1)))
187 ALLOC_DEST (cfg, dest, ins);
188 switch (ins->opcode) {
189 FOLD_BINOPC2_IMM (OP_IDIV_IMM, /, gint32);
190 FOLD_BINOPC2_IMM (OP_IDIV_UN_IMM, /, guint32);
191 FOLD_BINOPC2_IMM (OP_IREM_IMM, %, gint32);
192 FOLD_BINOPC2_IMM (OP_IREM_UN_IMM, %, guint32);
194 g_assert_not_reached ();
196 dest->opcode = OP_ICONST;
197 MONO_INST_NULLIFY_SREGS (dest);
203 if (arg1->opcode == OP_ICONST) {
204 /* INEG sets cflags on x86, and the LNEG decomposition depends on that */
205 #if SIZEOF_REGISTER == 4
206 if (ins->opcode == OP_INEG)
209 ALLOC_DEST (cfg, dest, ins);
210 switch (ins->opcode) {
211 FOLD_UNOP (OP_INEG,-);
212 FOLD_UNOP (OP_INOT,~);
214 dest->opcode = OP_ICONST;
215 MONO_INST_NULLIFY_SREGS (dest);
219 #if SIZEOF_REGISTER == 8
220 if ((arg1->opcode == OP_ICONST) || (arg1->opcode == OP_I8CONST)) {
222 if (arg1->opcode == OP_ICONST) {
224 ALLOC_DEST (cfg, dest, ins);
225 dest->opcode = arg1->opcode;
226 MONO_INST_NULLIFY_SREGS (dest);
227 dest->inst_c0 = arg1->inst_c0;
231 if (arg1->opcode == OP_VZERO) {
232 ALLOC_DEST (cfg, dest, ins);
233 dest->opcode = OP_VZERO;
238 if (arg1->opcode == OP_XZERO) {
239 ALLOC_DEST (cfg, dest, ins);
240 dest->opcode = OP_XZERO;
247 case OP_ICOMPARE_IMM: {
249 if (ins->sreg2 == -1) {
251 arg2->opcode = OP_ICONST;
252 arg2->inst_c0 = ins->inst_imm;
255 if ((arg1->opcode == OP_ICONST) && (arg2->opcode == OP_ICONST) && ins->next) {
256 MonoInst *next = ins->next;
257 gboolean res = FALSE;
259 switch (next->opcode) {
270 switch (next->opcode) {
271 FOLD_BINOPCXX (OP_CEQ,==,gint32);
272 FOLD_BINOPCXX (OP_ICEQ,==,gint32);
273 FOLD_BINOPCXX (OP_CGT,>,gint32);
274 FOLD_BINOPCXX (OP_ICGT,>,gint32);
275 FOLD_BINOPCXX (OP_CGT_UN,>,guint32);
276 FOLD_BINOPCXX (OP_ICGT_UN,>,guint32);
277 FOLD_BINOPCXX (OP_CLT,<,gint32);
278 FOLD_BINOPCXX (OP_ICLT,<,gint32);
279 FOLD_BINOPCXX (OP_CLT_UN,<,guint32);
280 FOLD_BINOPCXX (OP_ICLT_UN,<,guint32);
285 next->opcode = OP_ICONST;
287 MONO_INST_NULLIFY_SREGS (next);
289 ALLOC_DEST (cfg, dest, ins);
290 dest->opcode = OP_ICONST;
304 switch (next->opcode) {
305 FOLD_BINOPCXX (OP_IBEQ,==,gint32);
306 FOLD_BINOPCXX (OP_IBNE_UN,!=,guint32);
307 FOLD_BINOPCXX (OP_IBGT,>,gint32);
308 FOLD_BINOPCXX (OP_IBGT_UN,>,guint32);
309 FOLD_BINOPCXX (OP_IBGE,>=,gint32);
310 FOLD_BINOPCXX (OP_IBGE_UN,>=,guint32);
311 FOLD_BINOPCXX (OP_IBLT,<,gint32);
312 FOLD_BINOPCXX (OP_IBLT_UN,<,guint32);
313 FOLD_BINOPCXX (OP_IBLE,<=,gint32);
314 FOLD_BINOPCXX (OP_IBLE_UN,<=,guint32);
319 * Can't nullify OP_COMPARE here since the decompose long branch
320 * opcodes depend on it being executed. Also, the branch might not
321 * be eliminated after all if loop opts is disabled, for example.
324 next->flags |= MONO_INST_CFOLD_TAKEN;
326 next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
328 ALLOC_DEST (cfg, dest, ins);
329 dest->opcode = OP_ICONST;
335 /* This happens when a conditional branch is eliminated */
336 if (next->next == NULL) {
349 if (arg1->opcode == OP_R8CONST) {
350 ALLOC_DEST (cfg, dest, ins);
351 dest->opcode = OP_R8CONST;
353 dest->inst_p0 = arg1->inst_p0;
360 * *ovf* opcodes? It's slow and hard to do in C.
361 * switch can be replaced by a simple jump