2 * Copyright (C) 2012, 2013, 2014
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 * The constant folder is also responsible for validating if the constant
34 * expressions produce valid results. We cannot trust the FPU control
35 * unit for these exceptions because setting FPU control words might not
36 * work. Systems can set and enforce FPU modes of operation. It's also valid
37 * for libc's to simply ignore FPU exceptions. For instance ARM CPUs in
38 * glibc. We implement some trivial and IEE 754 conformant functions which
39 * emulate those operations. This is an entierly optional compiler feature
40 * which shouldn't be enabled for anything other than performing strict
41 * passes on constant expressions since it's quite slow.
43 typedef uint32_t sfloat_t;
54 SFLOAT_UNDERFLOW = 16,
56 } sfloat_exceptionflags_t;
59 SFLOAT_ROUND_NEAREST_EVEN,
63 } sfloat_roundingmode_t;
71 sfloat_roundingmode_t roundingmode;
72 sfloat_exceptionflags_t exceptionflags;
73 sfloat_tdetect_t tiny;
76 /* Count of leading zero bits before the most-significand 1 bit. */
78 /* MSVC has an intrinsic for this */
79 static GMQCC_INLINE uint32_t sfloat_clz(uint32_t x) {
81 _BitScanForward(&r, x);
84 # define SFLOAT_CLZ(X, SUB) \
85 (sfloat_clz((X)) - (SUB))
86 #elif defined(__GNUC__) || defined(__CLANG__)
87 /* Clang and GCC have a builtin for this */
88 # define SFLOAT_CLZ(X, SUB) \
89 (__builtin_clz((X)) - (SUB))
92 static GMQCC_INLINE uint32_t sfloat_popcnt(uint32_t x) {
93 x -= ((x >> 1) & 0x55555555);
94 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
95 x = (((x >> 4) + x) & 0x0F0F0F0F);
98 return x & 0x0000003F;
100 static GMQCC_INLINE uint32_t sfloat_clz(uint32_t x) {
106 return 32 - sfloat_popcnt(x);
108 # define SFLOAT_CLZ(X, SUB) \
109 (sfloat_clz((X) - (SUB)))
112 /* The value of a NaN */
113 #define SFLOAT_NAN 0xFFC00000
115 #define SFLOAT_ISNAN(A) \
116 (0xFF000000 < (uint32_t)((A) << 1))
117 /* Test if signaling NaN */
118 #define SFLOAT_ISSNAN(A) \
119 (((((A) >> 22) & 0x1FF) == 0x1FE) && ((A) & 0x003FFFFF))
120 /* Raise exception */
121 #define SFLOAT_RAISE(STATE, FLAGS) \
122 ((STATE)->exceptionflags |= (FLAGS))
124 * Shifts `A' right `COUNT' bits. Non-zero bits are stored in LSB. Size
125 * sets the arbitrarly-large limit.
127 #define SFLOAT_SHIFT(SIZE, A, COUNT, Z) \
128 *(Z) = ((COUNT) == 0) \
130 : (((COUNT) < (SIZE)) \
131 ? ((A) >> (COUNT)) | (((A) << ((-(COUNT)) & ((SIZE) - 1))) != 0) \
133 /* Extract fractional component */
134 #define SFLOAT_EXTRACT_FRAC(X) \
135 ((uint32_t)((X) & 0x007FFFFF))
136 /* Extract exponent component */
137 #define SFLOAT_EXTRACT_EXP(X) \
138 ((int16_t)((X) >> 23) & 0xFF)
139 /* Extract sign bit */
140 #define SFLOAT_EXTRACT_SIGN(X) \
142 /* Normalize a subnormal */
143 #define SFLOAT_SUBNORMALIZE(SA, Z, SZ) \
144 (void)(*(SZ) = (SA) << SFLOAT_CLZ((SA), 8), *(SZ) = 1 - SFLOAT_CLZ((SA), 8))
146 * Pack sign, exponent and significand and produce a float.
148 * Integer portions of the significand are added to the exponent. The
149 * exponent input should be one less than the result exponent whenever
150 * the significand is normalized since normalized significand will
151 * always have an integer portion of value one.
153 #define SFLOAT_PACK(SIGN, EXP, SIG) \
154 (sfloat_t)((((uint32_t)(SIGN)) << 31) + (((uint32_t)(EXP)) << 23) + (SIG))
156 /* Calculate NaN. If either operands are signaling then raise invalid */
157 static sfloat_t sfloat_propagate_nan(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
158 bool isnan_a = SFLOAT_ISNAN(a);
159 bool issnan_a = SFLOAT_ISSNAN(a);
160 bool isnan_b = SFLOAT_ISNAN(b);
161 bool issnan_b = SFLOAT_ISSNAN(b);
166 if (issnan_a | issnan_b)
167 SFLOAT_RAISE(state, SFLOAT_INEXACT);
171 return isnan_b ? b : a;
172 } else if (isnan_a) {
173 if (issnan_b | !isnan_b)
176 if ((uint32_t)(a << 1) < (uint32_t)(b << 1)) return b;
177 if ((uint32_t)(b << 1) < (uint32_t)(a << 1)) return a;
178 return (a < b) ? a : b;
184 static sfloat_t SFLOAT_PACK_round(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
185 sfloat_roundingmode_t mode = state->roundingmode;
186 bool even = !!(mode == SFLOAT_ROUND_NEAREST_EVEN);
187 unsigned char increment = 0x40;
188 unsigned char bits = sig_z & 0x7F;
191 if (mode == SFLOAT_ROUND_TO_ZERO)
196 if (mode == SFLOAT_ROUND_UP)
199 if (mode == SFLOAT_ROUND_DOWN)
205 if (0xFD <= (uint16_t)exp_z) {
206 if ((0xFD < exp_z) || ((exp_z == 0xFD) && ((int32_t)(sig_z + increment) < 0))) {
207 SFLOAT_RAISE(state, SFLOAT_OVERFLOW | SFLOAT_INEXACT);
208 return SFLOAT_PACK(sign_z, 0xFF, 0) - (increment == 0);
211 /* Check for underflow */
212 bool tiny = (state->tiny == SFLOAT_TBEFORE) || (exp_z < -1) || (sig_z + increment < 0x80000000);
213 SFLOAT_SHIFT(32, sig_z, -exp_z, &sig_z);
217 SFLOAT_RAISE(state, SFLOAT_UNDERFLOW);
222 * Significand has point between bits 30 and 29, 7 bits to the left of
223 * the usual place. This shifted significand has to be normalized
224 * or smaller, if it isn't the exponent must be zero, in which case
225 * no rounding occurs since the result will be a subnormal.
228 SFLOAT_RAISE(state, SFLOAT_INEXACT);
229 sig_z = (sig_z + increment) >> 7;
230 sig_z &= ~(((bits ^ 0x40) == 0) & even);
233 return SFLOAT_PACK(sign_z, exp_z, sig_z);
236 /* Normalized round and pack */
237 static sfloat_t SFLOAT_PACK_normal(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
238 unsigned char c = SFLOAT_CLZ(sig_z, 1);
239 return SFLOAT_PACK_round(state, sign_z, exp_z - c, sig_z << c);
242 static sfloat_t sfloat_add_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
243 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
244 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
246 int16_t exp_d = exp_a - exp_b;
247 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 6;
248 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 6;
253 return sig_a ? sfloat_propagate_nan(state, a, b) : a;
258 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
260 } else if (exp_d < 0) {
262 return sig_b ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0xFF, 0);
267 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
271 return (sig_a | sig_b) ? sfloat_propagate_nan(state, a, b) : a;
273 return SFLOAT_PACK(sign_z, 0, (sig_a + sig_b) >> 6);
274 sig_z = 0x40000000 + sig_a + sig_b;
279 sig_z = (sig_a + sig_b) << 1;
281 if ((int32_t)sig_z < 0) {
282 sig_z = sig_a + sig_b;
286 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
289 static sfloat_t sfloat_sub_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
290 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
291 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
293 int16_t exp_d = exp_a - exp_b;
294 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 7;
295 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 7;
298 if (0 < exp_d) goto exp_greater_a;
299 if (exp_d < 0) goto exp_greater_b;
303 return sfloat_propagate_nan(state, a, b);
304 SFLOAT_RAISE(state, SFLOAT_INVALID);
311 if (sig_b < sig_a) goto greater_a;
312 if (sig_a < sig_b) goto greater_b;
314 return SFLOAT_PACK(state->roundingmode == SFLOAT_ROUND_DOWN, 0, 0);
318 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z ^ 1, 0xFF, 0);
323 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
326 sig_z = sig_b - sig_a;
333 return (sig_a) ? sfloat_propagate_nan(state, a, b) : a;
338 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
341 sig_z = sig_a - sig_b;
346 return SFLOAT_PACK_normal(state, sign_z, exp_z, sig_z);
349 static GMQCC_INLINE sfloat_t sfloat_add(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
350 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
351 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
352 return (sign_a == sign_b) ? sfloat_add_impl(state, a, b, sign_a)
353 : sfloat_sub_impl(state, a, b, sign_a);
356 static GMQCC_INLINE sfloat_t sfloat_sub(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
357 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
358 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
359 return (sign_a == sign_b) ? sfloat_sub_impl(state, a, b, sign_a)
360 : sfloat_add_impl(state, a, b, sign_a);
363 static sfloat_t sfloat_mul(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
364 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
365 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
367 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
368 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
370 uint64_t sig_z64 = 0;
371 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
372 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
373 bool sign_z = sign_a ^ sign_b;
376 if (sig_a || ((exp_b == 0xFF) && sig_b))
377 return sfloat_propagate_nan(state, a, b);
378 if ((exp_b | sig_b) == 0) {
379 SFLOAT_RAISE(state, SFLOAT_INVALID);
382 return SFLOAT_PACK(sign_z, 0xFF, 0);
386 return sfloat_propagate_nan(state, a, b);
387 if ((exp_a | sig_a) == 0) {
388 SFLOAT_RAISE(state, SFLOAT_INVALID);
391 return SFLOAT_PACK(sign_z, 0xFF, 0);
395 return SFLOAT_PACK(sign_z, 0, 0);
396 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
400 return SFLOAT_PACK(sign_z, 0, 0);
401 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
403 exp_z = exp_a + exp_b - 0x7F;
404 sig_a = (sig_a | 0x00800000) << 7;
405 sig_b = (sig_b | 0x00800000) << 8;
406 SFLOAT_SHIFT(64, ((uint64_t)sig_a) * sig_b, 32, &sig_z64);
408 if (0 <= (int32_t)(sig_z << 1)) {
412 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
415 static sfloat_t sfloat_div(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
416 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
417 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
419 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
420 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
422 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
423 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
424 bool sign_z = sign_a ^ sign_b;
428 return sfloat_propagate_nan(state, a, b);
431 return sfloat_propagate_nan(state, a, b);
432 SFLOAT_RAISE(state, SFLOAT_INVALID);
435 return SFLOAT_PACK(sign_z, 0xFF, 0);
438 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0, 0);
441 if ((exp_a | sig_a) == 0) {
442 SFLOAT_RAISE(state, SFLOAT_INVALID);
445 SFLOAT_RAISE(state, SFLOAT_DIVBYZERO);
446 return SFLOAT_PACK(sign_z, 0xFF, 0);
448 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
452 return SFLOAT_PACK(sign_z, 0, 0);
453 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
455 exp_z = exp_a - exp_b + 0x7D;
456 sig_a = (sig_a | 0x00800000) << 7;
457 sig_b = (sig_b | 0x00800000) << 8;
458 if (sig_b <= (sig_a + sig_a)) {
462 sig_z = (((uint64_t)sig_a) << 32) / sig_b;
463 if ((sig_z & 0x3F) == 0)
464 sig_z |= ((uint64_t)sig_b * sig_z != ((uint64_t)sig_a) << 32);
465 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
469 * There is two stages to constant folding in GMQCC: there is the parse
470 * stage constant folding, where, witht he help of the AST, operator
471 * usages can be constant folded. Then there is the constant folding
472 * in the IR for things like eliding if statements, can occur.
474 * This file is thus, split into two parts.
477 #define isfloat(X) (((ast_expression*)(X))->vtype == TYPE_FLOAT)
478 #define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR)
479 #define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING)
480 #define isfloats(X,Y) (isfloat (X) && isfloat (Y))
483 * Implementation of basic vector math for vec3_t, for trivial constant
486 * TODO: gcc/clang hinting for autovectorization
488 static GMQCC_INLINE vec3_t vec3_add(vec3_t a, vec3_t b) {
496 static GMQCC_INLINE vec3_t vec3_sub(vec3_t a, vec3_t b) {
504 static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) {
512 static GMQCC_INLINE vec3_t vec3_or(vec3_t a, vec3_t b) {
514 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b.x));
515 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b.y));
516 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b.z));
520 static GMQCC_INLINE vec3_t vec3_orvf(vec3_t a, qcfloat_t b) {
522 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b));
523 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b));
524 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b));
528 static GMQCC_INLINE vec3_t vec3_and(vec3_t a, vec3_t b) {
530 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b.x));
531 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b.y));
532 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b.z));
536 static GMQCC_INLINE vec3_t vec3_andvf(vec3_t a, qcfloat_t b) {
538 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b));
539 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b));
540 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b));
544 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
546 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
547 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
548 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
552 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
554 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
555 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
556 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
560 static GMQCC_INLINE vec3_t vec3_not(vec3_t a) {
568 static GMQCC_INLINE qcfloat_t vec3_mulvv(vec3_t a, vec3_t b) {
569 return (a.x * b.x + a.y * b.y + a.z * b.z);
572 static GMQCC_INLINE vec3_t vec3_mulvf(vec3_t a, qcfloat_t b) {
580 static GMQCC_INLINE bool vec3_cmp(vec3_t a, vec3_t b) {
586 static GMQCC_INLINE vec3_t vec3_create(float x, float y, float z) {
594 static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
595 return (!a.x && !a.y && !a.z);
598 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
599 return (a.x || a.y || a.z);
602 static GMQCC_INLINE vec3_t vec3_cross(vec3_t a, vec3_t b) {
604 out.x = a.y * b.z - a.z * b.y;
605 out.y = a.z * b.x - a.x * b.z;
606 out.z = a.x * b.y - a.y * b.x;
610 static lex_ctx_t fold_ctx(fold_t *fold) {
612 if (fold->parser->lex)
613 return parser_ctx(fold->parser);
615 memset(&ctx, 0, sizeof(ctx));
619 static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
620 switch (v->expression.vtype) {
622 return !!v->constval.vfloat;
624 return !!v->constval.vint;
626 if (OPTS_FLAG(CORRECT_LOGIC))
627 return vec3_pbool(v->constval.vvec);
628 return !!(v->constval.vvec.x);
630 if (!v->constval.vstring)
632 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
634 return !!v->constval.vstring[0];
636 compile_error(fold_ctx(fold), "internal error: fold_immediate_true on invalid type");
639 return !!v->constval.vfunc;
642 /* Handy macros to determine if an ast_value can be constant folded. */
643 #define fold_can_1(X) \
644 (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
645 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
647 #define fold_can_2(X, Y) (fold_can_1(X) && fold_can_1(Y))
649 #define fold_immvalue_float(E) ((E)->constval.vfloat)
650 #define fold_immvalue_vector(E) ((E)->constval.vvec)
651 #define fold_immvalue_string(E) ((E)->constval.vstring)
653 fold_t *fold_init(parser_t *parser) {
654 fold_t *fold = (fold_t*)mem_a(sizeof(fold_t));
655 fold->parser = parser;
656 fold->imm_float = NULL;
657 fold->imm_vector = NULL;
658 fold->imm_string = NULL;
659 fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
660 fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
663 * prime the tables with common constant values at constant
666 (void)fold_constgen_float (fold, 0.0f, false);
667 (void)fold_constgen_float (fold, 1.0f, false);
668 (void)fold_constgen_float (fold, -1.0f, false);
669 (void)fold_constgen_float (fold, 2.0f, false);
671 (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
672 (void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
677 bool fold_generate(fold_t *fold, ir_builder *ir) {
678 /* generate globals for immediate folded values */
682 for (i = 0; i < vec_size(fold->imm_float); ++i)
683 if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
684 for (i = 0; i < vec_size(fold->imm_vector); ++i)
685 if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
686 for (i = 0; i < vec_size(fold->imm_string); ++i)
687 if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
692 con_out("failed to generate global %s\n", cur->name);
693 ir_builder_delete(ir);
697 void fold_cleanup(fold_t *fold) {
700 for (i = 0; i < vec_size(fold->imm_float); ++i) ast_delete(fold->imm_float[i]);
701 for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
702 for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
704 vec_free(fold->imm_float);
705 vec_free(fold->imm_vector);
706 vec_free(fold->imm_string);
708 util_htdel(fold->imm_string_untranslate);
709 util_htdel(fold->imm_string_dotranslate);
714 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value, bool inexact) {
715 ast_value *out = NULL;
718 for (i = 0; i < vec_size(fold->imm_float); i++) {
719 if (!memcmp(&fold->imm_float[i]->constval.vfloat, &value, sizeof(qcfloat_t)))
720 return (ast_expression*)fold->imm_float[i];
723 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
725 out->hasvalue = true;
726 out->inexact = inexact;
727 out->constval.vfloat = value;
729 vec_push(fold->imm_float, out);
731 return (ast_expression*)out;
734 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
738 for (i = 0; i < vec_size(fold->imm_vector); i++) {
739 if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
740 return (ast_expression*)fold->imm_vector[i];
743 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
745 out->hasvalue = true;
746 out->constval.vvec = value;
748 vec_push(fold->imm_vector, out);
750 return (ast_expression*)out;
753 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
754 hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
755 ast_value *out = NULL;
756 size_t hash = util_hthash(table, str);
758 if ((out = (ast_value*)util_htgeth(table, str, hash)))
759 return (ast_expression*)out;
763 util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
764 out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
765 out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
767 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
770 out->hasvalue = true;
772 out->constval.vstring = parser_strdup(str);
774 vec_push(fold->imm_string, out);
775 util_htseth(table, str, hash, out);
777 return (ast_expression*)out;
781 static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, ast_value *sel, const char *set) {
783 * vector-component constant folding works by matching the component sets
784 * to eliminate expensive operations on whole-vectors (3 components at runtime).
785 * to achive this effect in a clean manner this function generalizes the
786 * values through the use of a set paramater, which is used as an indexing method
787 * for creating the elided ast binary expression.
789 * Consider 'n 0 0' where y, and z need to be tested for 0, and x is
790 * used as the value in a binary operation generating an INSTR_MUL instruction,
791 * to acomplish the indexing of the correct component value we use set[0], set[1], set[2]
792 * as x, y, z, where the values of those operations return 'x', 'y', 'z'. Because
793 * of how ASCII works we can easily deliniate:
794 * vec.z is the same as set[2]-'x' for when set[2] is 'z', 'z'-'x' results in a
795 * literal value of 2, using this 2, we know that taking the address of vec->x (float)
796 * and indxing it with this literal will yeild the immediate address of that component
798 * Of course more work needs to be done to generate the correct index for the ast_member_new
799 * call, which is no problem: set[0]-'x' suffices that job.
801 qcfloat_t x = (&vec.x)[set[0]-'x'];
802 qcfloat_t y = (&vec.x)[set[1]-'x'];
803 qcfloat_t z = (&vec.x)[set[2]-'x'];
807 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
808 out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
809 out->node.keep = false;
810 ((ast_member*)out)->rvalue = true;
812 return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x, false), out);
818 static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
821 return fold_constgen_float(fold, -fold_immvalue_float(a), false);
822 } else if (isvector(a)) {
824 return fold_constgen_vector(fold, vec3_neg(fold_immvalue_vector(a)));
829 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
832 return fold_constgen_float(fold, !fold_immvalue_float(a), false);
833 } else if (isvector(a)) {
835 return fold_constgen_float(fold, vec3_notf(fold_immvalue_vector(a)), false);
836 } else if (isstring(a)) {
838 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
839 return fold_constgen_float(fold, !fold_immvalue_string(a), false);
841 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a), false);
847 static bool fold_check_except_float(sfloat_t (*callback)(sfloat_state_t *, sfloat_t, sfloat_t),
856 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS) && !OPTS_WARN(WARN_INEXACT_COMPARES))
859 s.roundingmode = SFLOAT_ROUND_NEAREST_EVEN;
860 s.tiny = SFLOAT_TBEFORE;
861 s.exceptionflags = 0;
862 ca.f = fold_immvalue_float(a);
863 cb.f = fold_immvalue_float(b);
865 callback(&s, ca.s, cb.s);
866 if (s.exceptionflags == 0)
869 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
870 goto inexact_possible;
872 if (s.exceptionflags & SFLOAT_DIVBYZERO)
873 compile_error(fold_ctx(fold), "division by zero");
874 if (s.exceptionflags & SFLOAT_INVALID)
875 compile_error(fold_ctx(fold), "undefined (inf)");
876 if (s.exceptionflags & SFLOAT_OVERFLOW)
877 compile_error(fold_ctx(fold), "arithmetic overflow");
878 if (s.exceptionflags & SFLOAT_UNDERFLOW)
879 compile_error(fold_ctx(fold), "arithmetic underflow");
882 return s.exceptionflags & SFLOAT_INEXACT;
885 static bool fold_check_inexact_float(fold_t *fold, ast_value *a, ast_value *b) {
886 lex_ctx_t ctx = fold_ctx(fold);
887 if (!OPTS_WARN(WARN_INEXACT_COMPARES))
889 if (!a->inexact && !b->inexact)
891 return compile_warning(ctx, WARN_INEXACT_COMPARES, "inexact value in comparison");
894 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
896 if (fold_can_2(a, b)) {
897 bool inexact = fold_check_except_float(&sfloat_add, fold, a, b);
898 return fold_constgen_float(fold, fold_immvalue_float(a) + fold_immvalue_float(b), inexact);
900 } else if (isvector(a)) {
901 if (fold_can_2(a, b))
902 return fold_constgen_vector(fold, vec3_add(fold_immvalue_vector(a), fold_immvalue_vector(b)));
907 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
909 if (fold_can_2(a, b)) {
910 bool inexact = fold_check_except_float(&sfloat_sub, fold, a, b);
911 return fold_constgen_float(fold, fold_immvalue_float(a) - fold_immvalue_float(b), inexact);
913 } else if (isvector(a)) {
914 if (fold_can_2(a, b))
915 return fold_constgen_vector(fold, vec3_sub(fold_immvalue_vector(a), fold_immvalue_vector(b)));
920 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
923 if (fold_can_2(a, b))
924 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a)));
926 if (fold_can_2(a, b)) {
927 bool inexact = fold_check_except_float(&sfloat_mul, fold, a, b);
928 return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b), inexact);
931 } else if (isvector(a)) {
933 if (fold_can_2(a, b))
934 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
936 if (fold_can_2(a, b)) {
937 return fold_constgen_float(fold, vec3_mulvv(fold_immvalue_vector(a), fold_immvalue_vector(b)), false);
938 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(a)) {
940 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "xyz"))) return out;
941 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "yxz"))) return out;
942 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "zxy"))) return out;
943 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(b)) {
945 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "xyz"))) return out;
946 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "yxz"))) return out;
947 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "zxy"))) return out;
954 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
956 if (fold_can_2(a, b)) {
957 bool inexact = fold_check_except_float(&sfloat_div, fold, a, b);
958 return fold_constgen_float(fold, fold_immvalue_float(a) / fold_immvalue_float(b), inexact);
959 } else if (fold_can_1(b)) {
960 return (ast_expression*)ast_binary_new(
964 fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
967 } else if (isvector(a)) {
968 if (fold_can_2(a, b)) {
969 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
971 return (ast_expression*)ast_binary_new(
976 ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
977 : (ast_expression*)ast_binary_new(
980 (ast_expression*)fold->imm_float[1],
989 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
990 return (fold_can_2(a, b))
991 ? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)), false)
995 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
997 if (fold_can_2(a, b))
998 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))), false);
1001 if (fold_can_2(a, b))
1002 return fold_constgen_vector(fold, vec3_or(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1004 if (fold_can_2(a, b))
1005 return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1011 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
1013 if (fold_can_2(a, b))
1014 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))), false);
1017 if (fold_can_2(a, b))
1018 return fold_constgen_vector(fold, vec3_and(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1020 if (fold_can_2(a, b))
1021 return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1027 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
1029 if (fold_can_2(a, b))
1030 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))), false);
1032 if (fold_can_2(a, b)) {
1034 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1036 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1042 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
1043 if (fold_can_2(a, b) && isfloats(a, b))
1044 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) * powf(2.0f, fold_immvalue_float(b))), false);
1048 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
1049 if (fold_can_2(a, b) && isfloats(a, b))
1050 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) / powf(2.0f, fold_immvalue_float(b))), false);
1054 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
1055 if (fold_can_2(a, b)) {
1056 if (OPTS_FLAG(PERL_LOGIC)) {
1058 return (fold_immediate_true(fold, a)) ? (ast_expression*)a : (ast_expression*)b;
1060 return (fold_immediate_true(fold, a)) ? (ast_expression*)b : (ast_expression*)a;
1062 return fold_constgen_float (
1064 ((expr) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
1065 : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
1075 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
1076 if (fold_can_1(a)) {
1077 return fold_immediate_true(fold, a)
1078 ? (ast_expression*)b
1079 : (ast_expression*)c;
1084 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
1085 if (fold_can_2(a, b))
1086 return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)), false);
1090 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
1091 if (fold_can_2(a,b)) {
1092 fold_check_inexact_float(fold, a, b);
1093 if (fold_immvalue_float(a) < fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[2];
1094 if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
1095 if (fold_immvalue_float(a) > fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
1100 static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast_value *b, bool lt) {
1101 if (fold_can_2(a, b)) {
1102 fold_check_inexact_float(fold, a, b);
1103 return (lt) ? (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) < fold_immvalue_float(b))]
1104 : (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) > fold_immvalue_float(b))];
1109 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
1110 if (fold_can_2(a, b)) {
1111 if (isfloat(a) && isfloat(b)) {
1112 float la = fold_immvalue_float(a);
1113 float lb = fold_immvalue_float(b);
1114 fold_check_inexact_float(fold, a, b);
1115 return (ast_expression*)fold->imm_float[!(ne ? la == lb : la != lb)];
1116 } if (isvector(a) && isvector(b)) {
1117 vec3_t la = fold_immvalue_vector(a);
1118 vec3_t lb = fold_immvalue_vector(b);
1119 return (ast_expression*)fold->imm_float[!(ne ? vec3_cmp(la, lb) : !vec3_cmp(la, lb))];
1125 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
1128 return fold_constgen_float(fold, -1-fold_immvalue_float(a), false);
1132 return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
1138 static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
1139 if (fold_can_2(a, b))
1140 return fold_constgen_vector(fold, vec3_cross(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1144 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
1145 ast_value *a = (ast_value*)opexprs[0];
1146 ast_value *b = (ast_value*)opexprs[1];
1147 ast_value *c = (ast_value*)opexprs[2];
1148 ast_expression *e = NULL;
1150 /* can a fold operation be applied to this operator usage? */
1154 switch(info->operands) {
1155 case 3: if(!c) return NULL;
1156 case 2: if(!b) return NULL;
1159 compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
1165 * we could use a boolean and default case but ironically gcc produces
1166 * invalid broken assembly from that operation. clang/tcc get it right,
1167 * but interestingly ignore compiling this to a jump-table when I do that,
1168 * this happens to be the most efficent method, since you have per-level
1169 * granularity on the pointer check happening only for the case you check
1170 * it in. Opposed to the default method which would involve a boolean and
1171 * pointer check after wards.
1173 #define fold_op_case(ARGS, ARGS_OPID, OP, ARGS_FOLD) \
1174 case opid##ARGS ARGS_OPID: \
1175 if ((e = fold_op_##OP ARGS_FOLD)) { \
1176 ++opts_optimizationcount[OPTIM_CONST_FOLD]; \
1181 fold_op_case(2, ('-', 'P'), neg, (fold, a));
1182 fold_op_case(2, ('!', 'P'), not, (fold, a));
1183 fold_op_case(1, ('+'), add, (fold, a, b));
1184 fold_op_case(1, ('-'), sub, (fold, a, b));
1185 fold_op_case(1, ('*'), mul, (fold, a, b));
1186 fold_op_case(1, ('/'), div, (fold, a, b));
1187 fold_op_case(1, ('%'), mod, (fold, a, b));
1188 fold_op_case(1, ('|'), bor, (fold, a, b));
1189 fold_op_case(1, ('&'), band, (fold, a, b));
1190 fold_op_case(1, ('^'), xor, (fold, a, b));
1191 fold_op_case(1, ('<'), ltgt, (fold, a, b, true));
1192 fold_op_case(1, ('>'), ltgt, (fold, a, b, false));
1193 fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
1194 fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
1195 fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
1196 fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
1197 fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
1198 fold_op_case(2, ('*', '*'), exp, (fold, a, b));
1199 fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
1200 fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
1201 fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
1202 fold_op_case(2, ('~', 'P'), bnot, (fold, a));
1203 fold_op_case(2, ('>', '<'), cross, (fold, a, b));
1206 compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
1211 * Constant folding for compiler intrinsics, simaler approach to operator
1212 * folding, primarly: individual functions for each intrinsics to fold,
1213 * and a generic selection function.
1215 static GMQCC_INLINE ast_expression *fold_intrin_isfinite(fold_t *fold, ast_value *a) {
1216 return fold_constgen_float(fold, isfinite(fold_immvalue_float(a)), false);
1218 static GMQCC_INLINE ast_expression *fold_intrin_isinf(fold_t *fold, ast_value *a) {
1219 return fold_constgen_float(fold, isinf(fold_immvalue_float(a)), false);
1221 static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *a) {
1222 return fold_constgen_float(fold, isnan(fold_immvalue_float(a)), false);
1224 static GMQCC_INLINE ast_expression *fold_intrin_isnormal(fold_t *fold, ast_value *a) {
1225 return fold_constgen_float(fold, isnormal(fold_immvalue_float(a)), false);
1227 static GMQCC_INLINE ast_expression *fold_intrin_signbit(fold_t *fold, ast_value *a) {
1228 return fold_constgen_float(fold, signbit(fold_immvalue_float(a)), false);
1230 static GMQCC_INLINE ast_expression *fold_intirn_acosh(fold_t *fold, ast_value *a) {
1231 return fold_constgen_float(fold, acoshf(fold_immvalue_float(a)), false);
1233 static GMQCC_INLINE ast_expression *fold_intrin_asinh(fold_t *fold, ast_value *a) {
1234 return fold_constgen_float(fold, asinhf(fold_immvalue_float(a)), false);
1236 static GMQCC_INLINE ast_expression *fold_intrin_atanh(fold_t *fold, ast_value *a) {
1237 return fold_constgen_float(fold, (float)atanh(fold_immvalue_float(a)), false);
1239 static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *a) {
1240 return fold_constgen_float(fold, expf(fold_immvalue_float(a)), false);
1242 static GMQCC_INLINE ast_expression *fold_intrin_exp2(fold_t *fold, ast_value *a) {
1243 return fold_constgen_float(fold, exp2f(fold_immvalue_float(a)), false);
1245 static GMQCC_INLINE ast_expression *fold_intrin_expm1(fold_t *fold, ast_value *a) {
1246 return fold_constgen_float(fold, expm1f(fold_immvalue_float(a)), false);
1248 static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1249 return fold_constgen_float(fold, fmodf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1251 static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1252 return fold_constgen_float(fold, powf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1254 static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *a) {
1255 return fold_constgen_float(fold, fabsf(fold_immvalue_float(a)), false);
1259 ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
1260 ast_expression *ret = NULL;
1261 ast_value *a = (ast_value*)arg[0];
1262 ast_value *b = (ast_value*)arg[1];
1264 if (!strcmp(intrin, "isfinite")) ret = fold_intrin_isfinite(fold, a);
1265 if (!strcmp(intrin, "isinf")) ret = fold_intrin_isinf(fold, a);
1266 if (!strcmp(intrin, "isnan")) ret = fold_intrin_isnan(fold, a);
1267 if (!strcmp(intrin, "isnormal")) ret = fold_intrin_isnormal(fold, a);
1268 if (!strcmp(intrin, "signbit")) ret = fold_intrin_signbit(fold, a);
1269 if (!strcmp(intrin, "acosh")) ret = fold_intirn_acosh(fold, a);
1270 if (!strcmp(intrin, "asinh")) ret = fold_intrin_asinh(fold, a);
1271 if (!strcmp(intrin, "atanh")) ret = fold_intrin_atanh(fold, a);
1272 if (!strcmp(intrin, "exp")) ret = fold_intrin_exp(fold, a);
1273 if (!strcmp(intrin, "exp2")) ret = fold_intrin_exp2(fold, a);
1274 if (!strcmp(intrin, "expm1")) ret = fold_intrin_expm1(fold, a);
1275 if (!strcmp(intrin, "mod")) ret = fold_intrin_mod(fold, a, b);
1276 if (!strcmp(intrin, "pow")) ret = fold_intrin_pow(fold, a, b);
1277 if (!strcmp(intrin, "fabs")) ret = fold_intrin_fabs(fold, a);
1280 ++opts_optimizationcount[OPTIM_CONST_FOLD];
1286 * These are all the actual constant folding methods that happen in between
1287 * the AST/IR stage of the compiler , i.e eliminating branches for const
1288 * expressions, which is the only supported thing so far. We undefine the
1289 * testing macros here because an ir_value is differant than an ast_value.
1295 #undef fold_immvalue_float
1296 #undef fold_immvalue_string
1297 #undef fold_immvalue_vector
1301 #define isfloat(X) ((X)->vtype == TYPE_FLOAT)
1302 /*#define isstring(X) ((X)->vtype == TYPE_STRING)*/
1303 /*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/
1304 #define fold_immvalue_float(X) ((X)->constval.vfloat)
1305 #define fold_immvalue_vector(X) ((X)->constval.vvec)
1306 /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
1307 #define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST)
1308 /*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
1310 static ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
1311 ast_expression *swapped = NULL; /* using this as bool */
1314 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right))) {
1320 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right)))
1328 if (fold_immvalue_float(load) == 1.0f) {
1329 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1340 if (fold_immvalue_float(load) == 0.0f) {
1341 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1348 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) {
1349 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1359 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
1360 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1370 ast_expression *fold_binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right) {
1371 ast_expression *ret = fold_superfluous(left, right, op);
1374 return (ast_expression*)ast_binary_new(ctx, op, left, right);
1377 static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1378 if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) {
1379 ast_expression_codegen *cgen;
1382 bool istrue = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
1383 bool isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
1384 ast_expression *path = (istrue) ? branch->on_true :
1385 (isfalse) ? branch->on_false : NULL;
1388 * no path to take implies that the evaluation is if(0) and there
1389 * is no else block. so eliminate all the code.
1391 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1395 if (!(elide = ir_function_create_block(ast_ctx(branch), func->ir_func, ast_function_label(func, ((istrue) ? "ontrue" : "onfalse")))))
1397 if (!(*(cgen = path->codegen))((ast_expression*)path, func, false, &dummy))
1399 if (!ir_block_create_jump(func->curblock, ast_ctx(branch), elide))
1402 * now the branch has been eliminated and the correct block for the constant evaluation
1403 * is expanded into the current block for the function.
1405 func->curblock = elide;
1406 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1409 return -1; /* nothing done */
1412 int fold_cond_ternary(ir_value *condval, ast_function *func, ast_ternary *branch) {
1413 return fold_cond(condval, func, (ast_ifthen*)branch);
1416 int fold_cond_ifthen(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1417 return fold_cond(condval, func, branch);