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_BINOP2_IMM (OP_ISHL_IMM, <<);
138 FOLD_BINOP2_IMM (OP_ISHR_IMM, >>);
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 ((ins->opcode == OP_INEG) && ins->next && (ins->next->opcode == OP_ADC_IMM))
207 ALLOC_DEST (cfg, dest, ins);
208 switch (ins->opcode) {
209 FOLD_UNOP (OP_INEG,-);
210 FOLD_UNOP (OP_INOT,~);
212 dest->opcode = OP_ICONST;
213 MONO_INST_NULLIFY_SREGS (dest);
217 #if SIZEOF_REGISTER == 8
218 if ((arg1->opcode == OP_ICONST) || (arg1->opcode == OP_I8CONST)) {
220 if (arg1->opcode == OP_ICONST) {
222 ALLOC_DEST (cfg, dest, ins);
223 dest->opcode = arg1->opcode;
224 MONO_INST_NULLIFY_SREGS (dest);
225 dest->inst_c0 = arg1->inst_c0;
229 if (arg1->opcode == OP_VZERO) {
230 ALLOC_DEST (cfg, dest, ins);
231 dest->opcode = OP_VZERO;
236 if (arg1->opcode == OP_XZERO) {
237 ALLOC_DEST (cfg, dest, ins);
238 dest->opcode = OP_XZERO;
245 case OP_ICOMPARE_IMM: {
247 if (ins->sreg2 == -1) {
249 arg2->opcode = OP_ICONST;
250 arg2->inst_c0 = ins->inst_imm;
253 if ((arg1->opcode == OP_ICONST) && (arg2->opcode == OP_ICONST) && ins->next) {
254 MonoInst *next = ins->next;
255 gboolean res = FALSE;
257 switch (next->opcode) {
268 switch (next->opcode) {
269 FOLD_BINOPCXX (OP_CEQ,==,gint32);
270 FOLD_BINOPCXX (OP_ICEQ,==,gint32);
271 FOLD_BINOPCXX (OP_CGT,>,gint32);
272 FOLD_BINOPCXX (OP_ICGT,>,gint32);
273 FOLD_BINOPCXX (OP_CGT_UN,>,guint32);
274 FOLD_BINOPCXX (OP_ICGT_UN,>,guint32);
275 FOLD_BINOPCXX (OP_CLT,<,gint32);
276 FOLD_BINOPCXX (OP_ICLT,<,gint32);
277 FOLD_BINOPCXX (OP_CLT_UN,<,guint32);
278 FOLD_BINOPCXX (OP_ICLT_UN,<,guint32);
283 next->opcode = OP_ICONST;
285 MONO_INST_NULLIFY_SREGS (next);
287 ALLOC_DEST (cfg, dest, ins);
288 dest->opcode = OP_ICONST;
302 switch (next->opcode) {
303 FOLD_BINOPCXX (OP_IBEQ,==,gint32);
304 FOLD_BINOPCXX (OP_IBNE_UN,!=,guint32);
305 FOLD_BINOPCXX (OP_IBGT,>,gint32);
306 FOLD_BINOPCXX (OP_IBGT_UN,>,guint32);
307 FOLD_BINOPCXX (OP_IBGE,>=,gint32);
308 FOLD_BINOPCXX (OP_IBGE_UN,>=,guint32);
309 FOLD_BINOPCXX (OP_IBLT,<,gint32);
310 FOLD_BINOPCXX (OP_IBLT_UN,<,guint32);
311 FOLD_BINOPCXX (OP_IBLE,<=,gint32);
312 FOLD_BINOPCXX (OP_IBLE_UN,<=,guint32);
317 * Can't nullify OP_COMPARE here since the decompose long branch
318 * opcodes depend on it being executed. Also, the branch might not
319 * be eliminated after all if loop opts is disabled, for example.
322 next->flags |= MONO_INST_CFOLD_TAKEN;
324 next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
326 ALLOC_DEST (cfg, dest, ins);
327 dest->opcode = OP_ICONST;
333 /* This happens when a conditional branch is eliminated */
334 if (next->next == NULL) {
347 if (arg1->opcode == OP_R8CONST) {
348 ALLOC_DEST (cfg, dest, ins);
349 dest->opcode = OP_R8CONST;
351 dest->inst_p0 = arg1->inst_p0;
358 * *ovf* opcodes? It's slow and hard to do in C.
359 * switch can be replaced by a simple jump