2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define FOLD_STRING_UNTRANSLATE_HTSIZE 1024
30 #define FOLD_STRING_DOTRANSLATE_HTSIZE 1024
33 * There is two stages to constant folding in GMQCC: there is the parse
34 * stage constant folding, where, witht he help of the AST, operator
35 * usages can be constant folded. Then there is the constant folding
36 * in the IR for things like eliding if statements, can occur.
38 * This file is thus, split into two parts.
41 #define isfloat(X) (((ast_expression*)(X))->vtype == TYPE_FLOAT)
42 #define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR)
43 #define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING)
44 #define isfloats(X,Y) (isfloat (X) && isfloat (Y))
47 * Implementation of basic vector math for vec3_t, for trivial constant
50 * TODO: gcc/clang hinting for autovectorization
52 static GMQCC_INLINE vec3_t vec3_add(vec3_t a, vec3_t b) {
60 static GMQCC_INLINE vec3_t vec3_sub(vec3_t a, vec3_t b) {
68 static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) {
76 static GMQCC_INLINE vec3_t vec3_or(vec3_t a, vec3_t b) {
78 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b.x));
79 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b.y));
80 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b.z));
84 static GMQCC_INLINE vec3_t vec3_orvf(vec3_t a, qcfloat_t b) {
86 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b));
87 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b));
88 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b));
92 static GMQCC_INLINE vec3_t vec3_and(vec3_t a, vec3_t b) {
94 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b.x));
95 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b.y));
96 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b.z));
100 static GMQCC_INLINE vec3_t vec3_andvf(vec3_t a, qcfloat_t b) {
102 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b));
103 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b));
104 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b));
108 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
110 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
111 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
112 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
116 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
118 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
119 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
120 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
124 static GMQCC_INLINE vec3_t vec3_not(vec3_t a) {
126 out.x = (qcfloat_t)(~((qcint_t)a.x));
127 out.y = (qcfloat_t)(~((qcint_t)a.y));
128 out.z = (qcfloat_t)(~((qcint_t)a.z));
132 static GMQCC_INLINE qcfloat_t vec3_mulvv(vec3_t a, vec3_t b) {
133 return (a.x * b.x + a.y * b.y + a.z * b.z);
136 static GMQCC_INLINE vec3_t vec3_mulvf(vec3_t a, qcfloat_t b) {
144 static GMQCC_INLINE bool vec3_cmp(vec3_t a, vec3_t b) {
150 static GMQCC_INLINE vec3_t vec3_create(float x, float y, float z) {
158 static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
159 return (!a.x && !a.y && !a.z);
162 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
163 return (a.x && a.y && a.z);
166 static GMQCC_INLINE vec3_t vec3_cross(vec3_t a, vec3_t b) {
168 out.x = a.y * b.z - a.z * b.y;
169 out.y = a.z * b.x - a.x * b.z;
170 out.z = a.x * b.y - a.y * b.x;
174 static lex_ctx_t fold_ctx(fold_t *fold) {
176 if (fold->parser->lex)
177 return parser_ctx(fold->parser);
179 memset(&ctx, 0, sizeof(ctx));
183 static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
184 switch (v->expression.vtype) {
186 return !!v->constval.vfloat;
188 return !!v->constval.vint;
190 if (OPTS_FLAG(CORRECT_LOGIC))
191 return vec3_pbool(v->constval.vvec);
192 return !!(v->constval.vvec.x);
194 if (!v->constval.vstring)
196 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
198 return !!v->constval.vstring[0];
200 compile_error(fold_ctx(fold), "internal error: fold_immediate_true on invalid type");
203 return !!v->constval.vfunc;
206 /* Handy macros to determine if an ast_value can be constant folded. */
207 #define fold_can_1(X) \
208 (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
209 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
211 #define fold_can_2(X, Y) (fold_can_1(X) && fold_can_1(Y))
213 #define fold_immvalue_float(E) ((E)->constval.vfloat)
214 #define fold_immvalue_vector(E) ((E)->constval.vvec)
215 #define fold_immvalue_string(E) ((E)->constval.vstring)
217 fold_t *fold_init(parser_t *parser) {
218 fold_t *fold = (fold_t*)mem_a(sizeof(fold_t));
219 fold->parser = parser;
220 fold->imm_float = NULL;
221 fold->imm_vector = NULL;
222 fold->imm_string = NULL;
223 fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
224 fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
227 * prime the tables with common constant values at constant
230 (void)fold_constgen_float (fold, 0.0f);
231 (void)fold_constgen_float (fold, 1.0f);
232 (void)fold_constgen_float (fold, -1.0f);
234 (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
235 (void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
240 bool fold_generate(fold_t *fold, ir_builder *ir) {
241 /* generate globals for immediate folded values */
245 for (i = 0; i < vec_size(fold->imm_float); ++i)
246 if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
247 for (i = 0; i < vec_size(fold->imm_vector); ++i)
248 if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
249 for (i = 0; i < vec_size(fold->imm_string); ++i)
250 if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
255 con_out("failed to generate global %s\n", cur->name);
256 ir_builder_delete(ir);
260 void fold_cleanup(fold_t *fold) {
263 for (i = 0; i < vec_size(fold->imm_float); ++i) ast_delete(fold->imm_float[i]);
264 for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
265 for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
267 vec_free(fold->imm_float);
268 vec_free(fold->imm_vector);
269 vec_free(fold->imm_string);
271 util_htdel(fold->imm_string_untranslate);
272 util_htdel(fold->imm_string_dotranslate);
277 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value) {
278 ast_value *out = NULL;
281 for (i = 0; i < vec_size(fold->imm_float); i++) {
282 if (fold->imm_float[i]->constval.vfloat == value)
283 return (ast_expression*)fold->imm_float[i];
286 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
288 out->hasvalue = true;
289 out->constval.vfloat = value;
291 vec_push(fold->imm_float, out);
293 return (ast_expression*)out;
296 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
300 for (i = 0; i < vec_size(fold->imm_vector); i++) {
301 if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
302 return (ast_expression*)fold->imm_vector[i];
305 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
307 out->hasvalue = true;
308 out->constval.vvec = value;
310 vec_push(fold->imm_vector, out);
312 return (ast_expression*)out;
315 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
316 hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
317 ast_value *out = NULL;
318 size_t hash = util_hthash(table, str);
320 if ((out = (ast_value*)util_htgeth(table, str, hash)))
321 return (ast_expression*)out;
325 util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
326 out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
327 out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
329 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
332 out->hasvalue = true;
334 out->constval.vstring = parser_strdup(str);
336 vec_push(fold->imm_string, out);
337 util_htseth(table, str, hash, out);
339 return (ast_expression*)out;
343 static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, ast_value *sel, const char *set) {
345 * vector-component constant folding works by matching the component sets
346 * to eliminate expensive operations on whole-vectors (3 components at runtime).
347 * to achive this effect in a clean manner this function generalizes the
348 * values through the use of a set paramater, which is used as an indexing method
349 * for creating the elided ast binary expression.
351 * Consider 'n 0 0' where y, and z need to be tested for 0, and x is
352 * used as the value in a binary operation generating an INSTR_MUL instruction,
353 * to acomplish the indexing of the correct component value we use set[0], set[1], set[2]
354 * as x, y, z, where the values of those operations return 'x', 'y', 'z'. Because
355 * of how ASCII works we can easily deliniate:
356 * vec.z is the same as set[2]-'x' for when set[2] is 'z', 'z'-'x' results in a
357 * literal value of 2, using this 2, we know that taking the address of vec->x (float)
358 * and indxing it with this literal will yeild the immediate address of that component
360 * Of course more work needs to be done to generate the correct index for the ast_member_new
361 * call, which is no problem: set[0]-'x' suffices that job.
363 qcfloat_t x = (&vec.x)[set[0]-'x'];
364 qcfloat_t y = (&vec.x)[set[1]-'x'];
365 qcfloat_t z = (&vec.x)[set[2]-'x'];
369 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
370 out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
371 out->node.keep = false;
372 ((ast_member*)out)->rvalue = true;
374 return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x), out);
380 static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
383 return fold_constgen_float(fold, -fold_immvalue_float(a));
384 } else if (isvector(a)) {
386 return fold_constgen_vector(fold, vec3_neg(fold_immvalue_vector(a)));
391 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
394 return fold_constgen_float(fold, !fold_immvalue_float(a));
395 } else if (isvector(a)) {
397 return fold_constgen_float(fold, vec3_notf(fold_immvalue_vector(a)));
398 } else if (isstring(a)) {
400 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
401 return fold_constgen_float(fold, !fold_immvalue_string(a));
403 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a));
409 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
411 if (fold_can_2(a, b))
412 return fold_constgen_float(fold, fold_immvalue_float(a) + fold_immvalue_float(b));
413 } else if (isvector(a)) {
414 if (fold_can_2(a, b))
415 return fold_constgen_vector(fold, vec3_add(fold_immvalue_vector(a), fold_immvalue_vector(b)));
420 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
422 if (fold_can_2(a, b))
423 return fold_constgen_float(fold, fold_immvalue_float(a) - fold_immvalue_float(b));
424 } else if (isvector(a)) {
425 if (fold_can_2(a, b))
426 return fold_constgen_vector(fold, vec3_sub(fold_immvalue_vector(a), fold_immvalue_vector(b)));
431 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
434 if (fold_can_2(a, b))
435 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a)));
437 if (fold_can_2(a, b))
438 return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b));
440 } else if (isvector(a)) {
442 if (fold_can_2(a, b))
443 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
445 if (fold_can_2(a, b)) {
446 return fold_constgen_float(fold, vec3_mulvv(fold_immvalue_vector(a), fold_immvalue_vector(b)));
447 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(a)) {
449 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "xyz"))) return out;
450 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "yxz"))) return out;
451 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "zxy"))) return out;
452 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(b)) {
454 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "xyz"))) return out;
455 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "yxz"))) return out;
456 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "zxy"))) return out;
463 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
465 if (fold_can_2(a, b)) {
466 return fold_constgen_float(fold, fold_immvalue_float(a) / fold_immvalue_float(b));
467 } else if (fold_can_1(b)) {
468 return (ast_expression*)ast_binary_new(
472 fold_constgen_float(fold, 1.0f / fold_immvalue_float(b))
475 } else if (isvector(a)) {
476 if (fold_can_2(a, b)) {
477 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
479 return (ast_expression*)ast_binary_new(
484 ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b))
485 : (ast_expression*)ast_binary_new(
488 (ast_expression*)fold->imm_float[1],
497 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
498 return (fold_can_2(a, b))
499 ? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)))
503 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
505 if (fold_can_2(a, b))
506 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))));
509 if (fold_can_2(a, b))
510 return fold_constgen_vector(fold, vec3_or(fold_immvalue_vector(a), fold_immvalue_vector(b)));
512 if (fold_can_2(a, b))
513 return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
519 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
521 if (fold_can_2(a, b))
522 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))));
525 if (fold_can_2(a, b))
526 return fold_constgen_vector(fold, vec3_and(fold_immvalue_vector(a), fold_immvalue_vector(b)));
528 if (fold_can_2(a, b))
529 return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
535 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
537 if (fold_can_2(a, b))
538 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))));
541 if (fold_can_2(a, b))
542 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
544 if (fold_can_2(a, b))
545 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
551 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
552 if (fold_can_2(a, b) && isfloats(a, b))
553 return fold_constgen_float(fold, (qcfloat_t)((qcuint_t)(fold_immvalue_float(a)) << (qcuint_t)(fold_immvalue_float(b))));
557 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
558 if (fold_can_2(a, b) && isfloats(a, b))
559 return fold_constgen_float(fold, (qcfloat_t)((qcuint_t)(fold_immvalue_float(a)) >> (qcuint_t)(fold_immvalue_float(b))));
563 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
564 if (fold_can_2(a, b)) {
565 if (OPTS_FLAG(PERL_LOGIC)) {
566 if (fold_immediate_true(fold, a))
567 return (ast_expression*)b;
569 return fold_constgen_float (
571 ((expr) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
572 : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
581 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
583 return fold_immediate_true(fold, a)
585 : (ast_expression*)c;
590 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
591 if (fold_can_2(a, b))
592 return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)));
596 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
597 if (fold_can_2(a,b)) {
598 if (fold_immvalue_float(a) < fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[2];
599 if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
600 if (fold_immvalue_float(a) > fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
605 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
606 if (fold_can_2(a, b)) {
607 return fold_constgen_float(
609 (ne) ? (fold_immvalue_float(a) != fold_immvalue_float(b))
610 : (fold_immvalue_float(a) == fold_immvalue_float(b))
616 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
619 return fold_constgen_float(fold, ~((qcint_t)fold_immvalue_float(a)));
623 return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
629 static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
630 if (fold_can_2(a, b))
631 return fold_constgen_vector(fold, vec3_cross(fold_immvalue_vector(a), fold_immvalue_vector(b)));
635 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
636 ast_value *a = (ast_value*)opexprs[0];
637 ast_value *b = (ast_value*)opexprs[1];
638 ast_value *c = (ast_value*)opexprs[2];
639 ast_expression *e = NULL;
641 /* can a fold operation be applied to this operator usage? */
645 switch(info->operands) {
646 case 3: if(!c) return NULL;
647 case 2: if(!b) return NULL;
650 compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
656 * we could use a boolean and default case but ironically gcc produces
657 * invalid broken assembly from that operation. clang/tcc get it right,
658 * but interestingly ignore compiling this to a jump-table when I do that,
659 * this happens to be the most efficent method, since you have per-level
660 * granularity on the pointer check happening only for the case you check
661 * it in. Opposed to the default method which would involve a boolean and
662 * pointer check after wards.
664 #define fold_op_case(ARGS, ARGS_OPID, OP, ARGS_FOLD) \
665 case opid##ARGS ARGS_OPID: \
666 if ((e = fold_op_##OP ARGS_FOLD)) { \
667 ++opts_optimizationcount[OPTIM_CONST_FOLD]; \
672 fold_op_case(2, ('-', 'P'), neg, (fold, a));
673 fold_op_case(2, ('!', 'P'), not, (fold, a));
674 fold_op_case(1, ('+'), add, (fold, a, b));
675 fold_op_case(1, ('-'), sub, (fold, a, b));
676 fold_op_case(1, ('*'), mul, (fold, a, b));
677 fold_op_case(1, ('/'), div, (fold, a, b));
678 fold_op_case(1, ('%'), mod, (fold, a, b));
679 fold_op_case(1, ('|'), bor, (fold, a, b));
680 fold_op_case(1, ('&'), band, (fold, a, b));
681 fold_op_case(1, ('^'), xor, (fold, a, b));
682 fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
683 fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
684 fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
685 fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
686 fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
687 fold_op_case(2, ('*', '*'), exp, (fold, a, b));
688 fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
689 fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
690 fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
691 fold_op_case(2, ('~', 'P'), bnot, (fold, a));
692 fold_op_case(2, ('>', '<'), cross, (fold, a, b));
695 compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
700 * Constant folding for compiler intrinsics, simaler approach to operator
701 * folding, primarly: individual functions for each intrinsics to fold,
702 * and a generic selection function.
704 static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
705 return fold_constgen_float(
708 fold_immvalue_float(lhs),
709 fold_immvalue_float(rhs)
714 static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
715 return fold_constgen_float(
718 fold_immvalue_float(lhs),
719 fold_immvalue_float(rhs)
724 static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *value) {
725 return fold_constgen_float(fold, exp(fold_immvalue_float(value)));
728 static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *value) {
729 return fold_constgen_float(fold, isnan(fold_immvalue_float(value)) != 0.0f);
732 static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *value) {
733 return fold_constgen_float(fold, fabs(fold_immvalue_float(value)));
736 ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
737 if (!strcmp(intrin, "mod")) return fold_intrin_mod (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
738 if (!strcmp(intrin, "pow")) return fold_intrin_pow (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
739 if (!strcmp(intrin, "exp")) return fold_intrin_exp (fold, (ast_value*)arg[0]);
740 if (!strcmp(intrin, "isnan")) return fold_intrin_isnan(fold, (ast_value*)arg[0]);
741 if (!strcmp(intrin, "fabs")) return fold_intrin_fabs (fold, (ast_value*)arg[0]);
747 * These are all the actual constant folding methods that happen in between
748 * the AST/IR stage of the compiler , i.e eliminating branches for const
749 * expressions, which is the only supported thing so far. We undefine the
750 * testing macros here because an ir_value is differant than an ast_value.
756 #undef fold_immvalue_float
757 #undef fold_immvalue_string
758 #undef fold_immvalue_vector
762 #define isfloat(X) ((X)->vtype == TYPE_FLOAT)
763 /*#define isstring(X) ((X)->vtype == TYPE_STRING)*/
764 /*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/
765 #define fold_immvalue_float(X) ((X)->constval.vfloat)
766 #define fold_immvalue_vector(X) ((X)->constval.vvec)
767 /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
768 #define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST)
769 /*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
771 ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
774 if (!ast_istype(left, ast_value) || !fold_can_1((load = (ast_value*)right)))
780 if (fold_immvalue_float(load) == 1.0f) {
781 ++opts_optimizationcount[OPTIM_PEEPHOLE];
782 return (ast_expression*)left;
789 if (fold_immvalue_float(load) == 0.0f) {
790 ++opts_optimizationcount[OPTIM_PEEPHOLE];
791 return (ast_expression*)left;
796 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) {
797 ++opts_optimizationcount[OPTIM_PEEPHOLE];
798 return (ast_expression*)left;
804 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
805 ++opts_optimizationcount[OPTIM_PEEPHOLE];
806 return (ast_expression*)left;
814 static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
815 if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) {
816 ast_expression_codegen *cgen;
819 bool istrue = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
820 bool isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
821 ast_expression *path = (istrue) ? branch->on_true :
822 (isfalse) ? branch->on_false : NULL;
825 * no path to take implies that the evaluation is if(0) and there
826 * is no else block. so eliminate all the code.
828 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
832 if (!(elide = ir_function_create_block(ast_ctx(branch), func->ir_func, ast_function_label(func, ((istrue) ? "ontrue" : "onfalse")))))
834 if (!(*(cgen = path->codegen))((ast_expression*)path, func, false, &dummy))
836 if (!ir_block_create_jump(func->curblock, ast_ctx(branch), elide))
839 * now the branch has been eliminated and the correct block for the constant evaluation
840 * is expanded into the current block for the function.
842 func->curblock = elide;
843 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
846 return -1; /* nothing done */
849 int fold_cond_ternary(ir_value *condval, ast_function *func, ast_ternary *branch) {
850 return fold_cond(condval, func, (ast_ifthen*)branch);
853 int fold_cond_ifthen(ir_value *condval, ast_function *func, ast_ifthen *branch) {
854 return fold_cond(condval, func, branch);