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
13 mono_is_power_of_two (guint32 val)
17 for (i = 0, j = 1, k = 0xfffffffe; i < 32; ++i, j = j << 1, k = k << 1) {
21 if (i == 32 || val & k)
26 #define FOLD_BINOP(name,op) \
28 if (inst->inst_i0->opcode != OP_ICONST) \
30 if (inst->inst_i1->opcode == OP_ICONST) { \
31 inst->opcode = OP_ICONST; \
32 inst->inst_c0 = (gint32)(inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0); \
37 * We try to put constants on the left side of a commutative operation
38 * because it reduces register pressure and it matches the usual cpu
39 * instructions with immediates.
41 #define FOLD_BINOPCOMM(name,op) \
43 if (inst->inst_i0->opcode == OP_ICONST) {\
44 if (inst->inst_i1->opcode == OP_ICONST) { \
45 inst->opcode = OP_ICONST; \
46 inst->inst_c0 = (gint32)(inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0); \
49 MonoInst *tmp = inst->inst_i0; \
50 inst->inst_i0 = inst->inst_i1; \
51 inst->inst_i1 = tmp; \
54 if (inst->inst_i1->opcode == OP_ICONST && inst->opcode == CEE_ADD) { \
55 if (inst->inst_i1->inst_c0 == 0) { \
56 *inst = *(inst->inst_i0); \
60 if (inst->inst_i1->opcode == OP_ICONST && inst->opcode == CEE_MUL) { \
62 if (inst->inst_i1->inst_c0 == 1) { \
63 *inst = *(inst->inst_i0); \
65 } else if (inst->inst_i1->inst_c0 == -1) { \
66 inst->opcode = CEE_NEG; \
69 power2 = mono_is_power_of_two (inst->inst_i1->inst_c0); \
70 if (power2 < 0) return; \
71 inst->opcode = CEE_SHL; \
72 inst->inst_i1->inst_c0 = power2; \
77 #define MYGINT32_MAX 2147483647
78 #define G_MININT32 (-MYGINT32_MAX -1)
82 * We can't let this cause a division by zero exception since the division
83 * might not be executed during runtime.
85 #define FOLD_BINOPZ(name,op,cast) \
87 if (inst->inst_i1->opcode == OP_ICONST && inst->opcode == CEE_REM_UN && inst->inst_i1->inst_c0 == 2) { \
88 inst->opcode = CEE_AND; \
89 inst->inst_i1->inst_c0 = 1; \
92 if (inst->inst_i1->opcode == OP_ICONST) { \
93 if (!inst->inst_i1->inst_c0) return; \
94 if (inst->inst_i0->opcode == OP_ICONST) { \
95 if ((inst->inst_i0->inst_c0 == G_MININT32) && (inst->inst_i1->inst_c0 == -1)) \
97 inst->inst_c0 = (cast)inst->inst_i0->inst_c0 op (cast)inst->inst_i1->inst_c0; \
98 inst->opcode = OP_ICONST; \
100 int power2 = mono_is_power_of_two (inst->inst_i1->inst_c0); \
101 if (power2 < 0) return; \
102 if (inst->opcode == CEE_REM_UN) { \
103 inst->opcode = CEE_AND; \
104 inst->inst_i1->inst_c0 = (1 << power2) - 1; \
105 } else if (inst->opcode == CEE_DIV_UN) { \
106 inst->opcode = CEE_SHR_UN; \
107 inst->inst_i1->inst_c0 = power2; \
113 #define FOLD_BINOPA(name,op,cast) \
115 if (inst->inst_i0->opcode != OP_ICONST) \
117 if (inst->inst_i1->opcode == OP_ICONST) { \
118 inst->opcode = OP_ICONST; \
119 inst->inst_c0 = (cast)inst->inst_i0->inst_c0 op (cast)inst->inst_i1->inst_c0; \
123 #define FOLD_CXX(name,op,cast) \
125 if (inst->inst_i0->opcode != OP_COMPARE) \
127 if (inst->inst_i0->inst_i0->opcode != OP_ICONST) \
129 if (inst->inst_i0->inst_i1->opcode == OP_ICONST) { \
130 inst->opcode = OP_ICONST; \
131 inst->inst_c0 = (cast)inst->inst_i0->inst_i0->inst_c0 op (cast)inst->inst_i0->inst_i1->inst_c0; \
135 #define FOLD_UNOP(name,op) \
137 if (inst->inst_i0->opcode == OP_ICONST) { \
138 inst->opcode = OP_ICONST; \
139 inst->inst_c0 = op inst->inst_i0->inst_c0; \
140 } else if (inst->inst_i0->opcode == OP_I8CONST) { \
141 inst->opcode = OP_I8CONST; \
142 inst->inst_l = op inst->inst_i0->inst_l; \
145 #define FOLD_BRBINOP(name,op,cast) \
147 if (inst->inst_i0->opcode != OP_COMPARE) \
149 if (inst->inst_i0->inst_i0->opcode != OP_ICONST) \
151 if (inst->inst_i0->inst_i1->opcode == OP_ICONST) { \
152 if ((cast)inst->inst_i0->inst_i0->inst_c0 op (cast)inst->inst_i0->inst_i1->inst_c0) \
153 inst->opcode = OP_BR; \
155 inst->opcode = OP_NOP; \
160 * Helper function to do constant expression evaluation.
161 * We do constant folding of integers only, FP stuff is much more tricky,
162 * int64 probably not worth it.
165 mono_constant_fold_inst (MonoInst *inst, gpointer data)
167 switch (inst->opcode) {
169 /* FIXME: the CEE_B* don't contain operands, need to use the OP_COMPARE instruction */
170 /*FOLD_BRBINOP (CEE_BEQ,==,gint32)
171 FOLD_BRBINOP (CEE_BGE,>=,gint32)
172 FOLD_BRBINOP (CEE_BGT,>,gint32)
173 FOLD_BRBINOP (CEE_BLE,<=,gint32)
174 FOLD_BRBINOP (CEE_BLT,<,gint32)
175 FOLD_BRBINOP (CEE_BNE_UN,!=,guint32)
176 FOLD_BRBINOP (CEE_BGE_UN,>=,guint32)
177 FOLD_BRBINOP (CEE_BGT_UN,>,guint32)
178 FOLD_BRBINOP (CEE_BLE_UN,<=,guint32)
179 FOLD_BRBINOP (CEE_BLT_UN,<,guint32)*/
181 FOLD_BINOPCOMM (CEE_MUL,*)
183 FOLD_BINOPCOMM (CEE_ADD,+)
184 FOLD_BINOP (CEE_SUB,-)
185 FOLD_BINOPZ (CEE_DIV,/,gint32)
186 FOLD_BINOPZ (CEE_DIV_UN,/,guint32)
187 FOLD_BINOPZ (CEE_REM,%,gint32)
188 FOLD_BINOPZ (CEE_REM_UN,%,guint32)
189 FOLD_BINOPCOMM (CEE_AND,&)
190 FOLD_BINOPCOMM (CEE_OR,|)
191 FOLD_BINOPCOMM (CEE_XOR,^)
192 FOLD_BINOP (CEE_SHL,<<)
193 FOLD_BINOP (CEE_SHR,>>)
195 if (inst->inst_i0->opcode != OP_ICONST)
197 if (inst->inst_i1->opcode == OP_ICONST) {
198 inst->opcode = OP_ICONST;
199 inst->inst_c0 = (guint32)inst->inst_i0->inst_c0 >> (guint32)inst->inst_i1->inst_c0;
202 FOLD_UNOP (CEE_NEG,-)
203 FOLD_UNOP (CEE_NOT,~)
204 FOLD_CXX (OP_CEQ,==,gint32)
205 FOLD_CXX (OP_CGT,>,gint32)
206 FOLD_CXX (OP_CGT_UN,>,guint32)
207 FOLD_CXX (OP_CLT,<,gint32)
208 FOLD_CXX (OP_CLT_UN,<,guint32)
210 if (inst->inst_i0->opcode == OP_ICONST) {
211 inst->opcode = OP_I8CONST;
212 inst->inst_l = inst->inst_i0->inst_c0;
217 if (inst->inst_i0->opcode == OP_ICONST) {
218 inst->opcode = OP_ICONST;
219 inst->inst_c0 = inst->inst_i0->inst_c0;
220 } else if (inst->inst_i0->opcode == CEE_LDIND_I) {
221 *inst = *inst->inst_i0;
224 /* we should be able to handle isinst and castclass as well */
230 * *ovf* opcodes? I'ts slow and hard to do in C.
231 * switch can be replaced by a simple jump
233 #if SIZEOF_VOID_P == 4
235 if ((inst->inst_left->type == STACK_I4) || (inst->inst_left->type == STACK_PTR)) {
236 *inst = *inst->inst_left;
246 mono_constant_fold (MonoCompile *cfg)
250 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
252 for (ins = bb->code; ins; ins = ins->next)
253 mono_inst_foreach (ins, mono_constant_fold_inst, NULL);
258 * If the arguments to the cond branch are constants, eval and
259 * return BRANCH_NOT_TAKEN for not taken, BRANCH_TAKEN for taken,
260 * BRANCH_UNDEF otherwise.
261 * If this code is changed to handle also non-const values, make sure
262 * side effects are handled in optimize_branches() in mini.c, by
263 * inserting pop instructions.
266 mono_eval_cond_branch (MonoInst *ins)
268 MonoInst *left, *right;
269 /* FIXME: handle also 64 bit ints */
270 left = ins->inst_left->inst_left;
271 if (left->opcode != OP_ICONST && left->opcode != OP_PCONST)
273 right = ins->inst_left->inst_right;
274 if (right->opcode != OP_ICONST && right->opcode != OP_PCONST)
276 switch (ins->opcode) {
278 if (left->inst_c0 == right->inst_c0)
280 return BRANCH_NOT_TAKEN;
282 if (left->inst_c0 >= right->inst_c0)
284 return BRANCH_NOT_TAKEN;
286 if (left->inst_c0 > right->inst_c0)
288 return BRANCH_NOT_TAKEN;
290 if (left->inst_c0 <= right->inst_c0)
292 return BRANCH_NOT_TAKEN;
294 if (left->inst_c0 < right->inst_c0)
296 return BRANCH_NOT_TAKEN;
298 if ((gsize)left->inst_c0 != (gsize)right->inst_c0)
300 return BRANCH_NOT_TAKEN;
302 if ((gsize)left->inst_c0 >= (gsize)right->inst_c0)
304 return BRANCH_NOT_TAKEN;
306 if ((gsize)left->inst_c0 > (gsize)right->inst_c0)
308 return BRANCH_NOT_TAKEN;
310 if ((gsize)left->inst_c0 <= (gsize)right->inst_c0)
312 return BRANCH_NOT_TAKEN;
314 if ((gsize)left->inst_c0 < (gsize)right->inst_c0)
316 return BRANCH_NOT_TAKEN;