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 /* The value of a NaN */
77 #define SFLOAT_NAN 0xFFC00000
78 /* Count of leading zero bits before the most-significand 1 bit. */
79 #define SFLOAT_CLZ(X, SUB) \
80 (__builtin_clz((X)) - (SUB))
82 #define SFLOAT_ISNAN(A) \
83 (0xFF000000 < (uint32_t)((A) << 1))
84 /* Test if signaling NaN */
85 #define SFLOAT_ISSNAN(A) \
86 (((((A) >> 22) & 0x1FF) == 0x1FE) && ((A) & 0x003FFFFF))
88 #define SFLOAT_RAISE(STATE, FLAGS) \
89 ((STATE)->exceptionflags |= (FLAGS))
91 * Shifts `A' right `COUNT' bits. Non-zero bits are stored in LSB. Size
92 * sets the arbitrarly-large limit.
94 #define SFLOAT_SHIFT(SIZE, A, COUNT, Z) \
95 *(Z) = ((COUNT) == 0) \
97 : (((COUNT) < (SIZE)) \
98 ? ((A) >> (COUNT)) | (((A) << ((-(COUNT)) & ((SIZE) - 1))) != 0) \
100 /* Extract fractional component */
101 #define SFLOAT_EXTRACT_FRAC(X) \
102 ((uint32_t)((X) & 0x007FFFFF))
103 /* Extract exponent component */
104 #define SFLOAT_EXTRACT_EXP(X) \
105 ((int16_t)((X) >> 23) & 0xFF)
106 /* Extract sign bit */
107 #define SFLOAT_EXTRACT_SIGN(X) \
109 /* Normalize a subnormal */
110 #define SFLOAT_SUBNORMALIZE(SA, Z, SZ) \
111 (void)(*(SZ) = (SA) << SFLOAT_CLZ((SA), 8), *(SZ) = 1 - SFLOAT_CLZ((SA), 8))
113 * Pack sign, exponent and significand and produce a float.
115 * Integer portions of the significand are added to the exponent. The
116 * exponent input should be one less than the result exponent whenever
117 * the significand is normalized since normalized significand will
118 * always have an integer portion of value one.
120 #define SFLOAT_PACK(SIGN, EXP, SIG) \
121 (sfloat_t)((((uint32_t)(SIGN)) << 31) + (((uint32_t)(EXP)) << 23) + (SIG))
123 /* Calculate NaN. If either operands are signaling then raise invalid */
124 static sfloat_t sfloat_propagate_nan(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
125 bool isnan_a = SFLOAT_ISNAN(a);
126 bool issnan_a = SFLOAT_ISSNAN(a);
127 bool isnan_b = SFLOAT_ISNAN(b);
128 bool issnan_b = SFLOAT_ISSNAN(b);
133 if (issnan_a | issnan_b)
134 SFLOAT_RAISE(state, SFLOAT_INEXACT);
138 return isnan_b ? b : a;
139 } else if (isnan_a) {
140 if (issnan_b | !isnan_b)
143 if ((uint32_t)(a << 1) < (uint32_t)(b << 1)) return b;
144 if ((uint32_t)(b << 1) < (uint32_t)(a << 1)) return a;
145 return (a < b) ? a : b;
151 static sfloat_t SFLOAT_PACK_round(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
152 sfloat_roundingmode_t mode = state->roundingmode;
153 bool even = !!(mode == SFLOAT_ROUND_NEAREST_EVEN);
154 unsigned char increment = 0x40;
155 unsigned char bits = sig_z & 0x7F;
158 if (mode == SFLOAT_ROUND_TO_ZERO)
163 if (mode == SFLOAT_ROUND_UP)
166 if (mode == SFLOAT_ROUND_DOWN)
172 if (0xFD <= (uint16_t)exp_z) {
173 if ((0xFD < exp_z) || ((exp_z == 0xFD) && ((int32_t)(sig_z + increment) < 0))) {
174 SFLOAT_RAISE(state, SFLOAT_OVERFLOW | SFLOAT_INEXACT);
175 return SFLOAT_PACK(sign_z, 0xFF, 0) - (increment == 0);
178 /* Check for underflow */
179 bool tiny = (state->tiny == SFLOAT_TBEFORE) || (exp_z < -1) || (sig_z + increment < 0x80000000);
180 SFLOAT_SHIFT(32, sig_z, -exp_z, &sig_z);
184 SFLOAT_RAISE(state, SFLOAT_UNDERFLOW);
189 * Significand has point between bits 30 and 29, 7 bits to the left of
190 * the usual place. This shifted significand has to be normalized
191 * or smaller, if it isn't the exponent must be zero, in which case
192 * no rounding occurs since the result will be a subnormal.
195 SFLOAT_RAISE(state, SFLOAT_INEXACT);
196 sig_z = (sig_z + increment) >> 7;
197 sig_z &= ~(((bits ^ 0x40) == 0) & even);
200 return SFLOAT_PACK(sign_z, exp_z, sig_z);
203 /* Normalized round and pack */
204 static sfloat_t SFLOAT_PACK_normal(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
205 unsigned char c = SFLOAT_CLZ(sig_z, 1);
206 return SFLOAT_PACK_round(state, sign_z, exp_z - c, sig_z << c);
209 static sfloat_t sfloat_add_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
210 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
211 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
213 int16_t exp_d = exp_a - exp_b;
214 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 6;
215 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 6;
220 return sig_a ? sfloat_propagate_nan(state, a, b) : a;
225 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
227 } else if (exp_d < 0) {
229 return sig_b ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0xFF, 0);
234 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
238 return (sig_a | sig_b) ? sfloat_propagate_nan(state, a, b) : a;
240 return SFLOAT_PACK(sign_z, 0, (sig_a + sig_b) >> 6);
241 sig_z = 0x40000000 + sig_a + sig_b;
246 sig_z = (sig_a + sig_b) << 1;
248 if ((int32_t)sig_z < 0) {
249 sig_z = sig_a + sig_b;
253 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
256 static sfloat_t sfloat_sub_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
257 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
258 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
260 int16_t exp_d = exp_a - exp_b;
261 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 7;
262 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 7;
265 if (0 < exp_d) goto exp_greater_a;
266 if (exp_d < 0) goto exp_greater_b;
270 return sfloat_propagate_nan(state, a, b);
271 SFLOAT_RAISE(state, SFLOAT_INVALID);
278 if (sig_b < sig_a) goto greater_a;
279 if (sig_a < sig_b) goto greater_b;
281 return SFLOAT_PACK(state->roundingmode == SFLOAT_ROUND_DOWN, 0, 0);
285 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z ^ 1, 0xFF, 0);
290 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
293 sig_z = sig_b - sig_a;
300 return (sig_a) ? sfloat_propagate_nan(state, a, b) : a;
305 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
308 sig_z = sig_a - sig_b;
313 return SFLOAT_PACK_normal(state, sign_z, exp_z, sig_z);
316 static GMQCC_INLINE sfloat_t sfloat_add(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
317 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
318 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
319 return (sign_a == sign_b) ? sfloat_add_impl(state, a, b, sign_a)
320 : sfloat_sub_impl(state, a, b, sign_a);
323 static GMQCC_INLINE sfloat_t sfloat_sub(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
324 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
325 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
326 return (sign_a == sign_b) ? sfloat_sub_impl(state, a, b, sign_a)
327 : sfloat_add_impl(state, a, b, sign_a);
330 static sfloat_t sfloat_mul(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
331 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
332 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
334 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
335 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
337 uint64_t sig_z64 = 0;
338 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
339 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
340 bool sign_z = sign_a ^ sign_b;
343 if (sig_a || ((exp_b == 0xFF) && sig_b))
344 return sfloat_propagate_nan(state, a, b);
345 if ((exp_b | sig_b) == 0) {
346 SFLOAT_RAISE(state, SFLOAT_INVALID);
349 return SFLOAT_PACK(sign_z, 0xFF, 0);
353 return sfloat_propagate_nan(state, a, b);
354 if ((exp_a | sig_a) == 0) {
355 SFLOAT_RAISE(state, SFLOAT_INVALID);
358 return SFLOAT_PACK(sign_z, 0xFF, 0);
362 return SFLOAT_PACK(sign_z, 0, 0);
363 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
367 return SFLOAT_PACK(sign_z, 0, 0);
368 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
370 exp_z = exp_a + exp_b - 0x7F;
371 sig_a = (sig_a | 0x00800000) << 7;
372 sig_b = (sig_b | 0x00800000) << 8;
373 SFLOAT_SHIFT(64, ((uint64_t)sig_a) * sig_b, 32, &sig_z64);
375 if (0 <= (int32_t)(sig_z << 1)) {
379 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
382 static sfloat_t sfloat_div(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
383 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
384 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
386 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
387 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
389 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
390 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
391 bool sign_z = sign_a ^ sign_b;
395 return sfloat_propagate_nan(state, a, b);
398 return sfloat_propagate_nan(state, a, b);
399 SFLOAT_RAISE(state, SFLOAT_INVALID);
402 return SFLOAT_PACK(sign_z, 0xFF, 0);
405 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0, 0);
408 if ((exp_a | sig_a) == 0) {
409 SFLOAT_RAISE(state, SFLOAT_INVALID);
412 SFLOAT_RAISE(state, SFLOAT_DIVBYZERO);
413 return SFLOAT_PACK(sign_z, 0xFF, 0);
415 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
419 return SFLOAT_PACK(sign_z, 0, 0);
420 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
422 exp_z = exp_a - exp_b + 0x7D;
423 sig_a = (sig_a | 0x00800000) << 7;
424 sig_b = (sig_b | 0x00800000) << 8;
425 if (sig_b <= (sig_a + sig_a)) {
429 sig_z = (((uint64_t)sig_a) << 32) / sig_b;
430 if ((sig_z & 0x3F) == 0)
431 sig_z |= ((uint64_t)sig_b * sig_z != ((uint64_t)sig_a) << 32);
432 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
436 * There is two stages to constant folding in GMQCC: there is the parse
437 * stage constant folding, where, witht he help of the AST, operator
438 * usages can be constant folded. Then there is the constant folding
439 * in the IR for things like eliding if statements, can occur.
441 * This file is thus, split into two parts.
444 #define isfloat(X) (((ast_expression*)(X))->vtype == TYPE_FLOAT)
445 #define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR)
446 #define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING)
447 #define isfloats(X,Y) (isfloat (X) && isfloat (Y))
450 * Implementation of basic vector math for vec3_t, for trivial constant
453 * TODO: gcc/clang hinting for autovectorization
455 static GMQCC_INLINE vec3_t vec3_add(vec3_t a, vec3_t b) {
463 static GMQCC_INLINE vec3_t vec3_sub(vec3_t a, vec3_t b) {
471 static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) {
479 static GMQCC_INLINE vec3_t vec3_or(vec3_t a, vec3_t b) {
481 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b.x));
482 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b.y));
483 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b.z));
487 static GMQCC_INLINE vec3_t vec3_orvf(vec3_t a, qcfloat_t b) {
489 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b));
490 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b));
491 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b));
495 static GMQCC_INLINE vec3_t vec3_and(vec3_t a, vec3_t b) {
497 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b.x));
498 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b.y));
499 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b.z));
503 static GMQCC_INLINE vec3_t vec3_andvf(vec3_t a, qcfloat_t b) {
505 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b));
506 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b));
507 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b));
511 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
513 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
514 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
515 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
519 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
521 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
522 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
523 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
527 static GMQCC_INLINE vec3_t vec3_not(vec3_t a) {
535 static GMQCC_INLINE qcfloat_t vec3_mulvv(vec3_t a, vec3_t b) {
536 return (a.x * b.x + a.y * b.y + a.z * b.z);
539 static GMQCC_INLINE vec3_t vec3_mulvf(vec3_t a, qcfloat_t b) {
547 static GMQCC_INLINE bool vec3_cmp(vec3_t a, vec3_t b) {
553 static GMQCC_INLINE vec3_t vec3_create(float x, float y, float z) {
561 static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
562 return (!a.x && !a.y && !a.z);
565 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
566 return (a.x || a.y || a.z);
569 static GMQCC_INLINE vec3_t vec3_cross(vec3_t a, vec3_t b) {
571 out.x = a.y * b.z - a.z * b.y;
572 out.y = a.z * b.x - a.x * b.z;
573 out.z = a.x * b.y - a.y * b.x;
577 static lex_ctx_t fold_ctx(fold_t *fold) {
579 if (fold->parser->lex)
580 return parser_ctx(fold->parser);
582 memset(&ctx, 0, sizeof(ctx));
586 static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
587 switch (v->expression.vtype) {
589 return !!v->constval.vfloat;
591 return !!v->constval.vint;
593 if (OPTS_FLAG(CORRECT_LOGIC))
594 return vec3_pbool(v->constval.vvec);
595 return !!(v->constval.vvec.x);
597 if (!v->constval.vstring)
599 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
601 return !!v->constval.vstring[0];
603 compile_error(fold_ctx(fold), "internal error: fold_immediate_true on invalid type");
606 return !!v->constval.vfunc;
609 /* Handy macros to determine if an ast_value can be constant folded. */
610 #define fold_can_1(X) \
611 (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
612 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
614 #define fold_can_2(X, Y) (fold_can_1(X) && fold_can_1(Y))
616 #define fold_immvalue_float(E) ((E)->constval.vfloat)
617 #define fold_immvalue_vector(E) ((E)->constval.vvec)
618 #define fold_immvalue_string(E) ((E)->constval.vstring)
620 fold_t *fold_init(parser_t *parser) {
621 fold_t *fold = (fold_t*)mem_a(sizeof(fold_t));
622 fold->parser = parser;
623 fold->imm_float = NULL;
624 fold->imm_vector = NULL;
625 fold->imm_string = NULL;
626 fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
627 fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
630 * prime the tables with common constant values at constant
633 (void)fold_constgen_float (fold, 0.0f, false);
634 (void)fold_constgen_float (fold, 1.0f, false);
635 (void)fold_constgen_float (fold, -1.0f, false);
636 (void)fold_constgen_float (fold, 2.0f, false);
638 (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
639 (void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
644 bool fold_generate(fold_t *fold, ir_builder *ir) {
645 /* generate globals for immediate folded values */
649 for (i = 0; i < vec_size(fold->imm_float); ++i)
650 if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
651 for (i = 0; i < vec_size(fold->imm_vector); ++i)
652 if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
653 for (i = 0; i < vec_size(fold->imm_string); ++i)
654 if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
659 con_out("failed to generate global %s\n", cur->name);
660 ir_builder_delete(ir);
664 void fold_cleanup(fold_t *fold) {
667 for (i = 0; i < vec_size(fold->imm_float); ++i) ast_delete(fold->imm_float[i]);
668 for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
669 for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
671 vec_free(fold->imm_float);
672 vec_free(fold->imm_vector);
673 vec_free(fold->imm_string);
675 util_htdel(fold->imm_string_untranslate);
676 util_htdel(fold->imm_string_dotranslate);
681 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value, bool inexact) {
682 ast_value *out = NULL;
685 for (i = 0; i < vec_size(fold->imm_float); i++) {
686 if (!memcmp(&fold->imm_float[i]->constval.vfloat, &value, sizeof(qcfloat_t)))
687 return (ast_expression*)fold->imm_float[i];
690 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
692 out->hasvalue = true;
693 out->inexact = inexact;
694 out->constval.vfloat = value;
696 vec_push(fold->imm_float, out);
698 return (ast_expression*)out;
701 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
705 for (i = 0; i < vec_size(fold->imm_vector); i++) {
706 if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
707 return (ast_expression*)fold->imm_vector[i];
710 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
712 out->hasvalue = true;
713 out->constval.vvec = value;
715 vec_push(fold->imm_vector, out);
717 return (ast_expression*)out;
720 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
721 hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
722 ast_value *out = NULL;
723 size_t hash = util_hthash(table, str);
725 if ((out = (ast_value*)util_htgeth(table, str, hash)))
726 return (ast_expression*)out;
730 util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
731 out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
732 out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
734 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
737 out->hasvalue = true;
739 out->constval.vstring = parser_strdup(str);
741 vec_push(fold->imm_string, out);
742 util_htseth(table, str, hash, out);
744 return (ast_expression*)out;
748 static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, ast_value *sel, const char *set) {
750 * vector-component constant folding works by matching the component sets
751 * to eliminate expensive operations on whole-vectors (3 components at runtime).
752 * to achive this effect in a clean manner this function generalizes the
753 * values through the use of a set paramater, which is used as an indexing method
754 * for creating the elided ast binary expression.
756 * Consider 'n 0 0' where y, and z need to be tested for 0, and x is
757 * used as the value in a binary operation generating an INSTR_MUL instruction,
758 * to acomplish the indexing of the correct component value we use set[0], set[1], set[2]
759 * as x, y, z, where the values of those operations return 'x', 'y', 'z'. Because
760 * of how ASCII works we can easily deliniate:
761 * vec.z is the same as set[2]-'x' for when set[2] is 'z', 'z'-'x' results in a
762 * literal value of 2, using this 2, we know that taking the address of vec->x (float)
763 * and indxing it with this literal will yeild the immediate address of that component
765 * Of course more work needs to be done to generate the correct index for the ast_member_new
766 * call, which is no problem: set[0]-'x' suffices that job.
768 qcfloat_t x = (&vec.x)[set[0]-'x'];
769 qcfloat_t y = (&vec.x)[set[1]-'x'];
770 qcfloat_t z = (&vec.x)[set[2]-'x'];
774 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
775 out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
776 out->node.keep = false;
777 ((ast_member*)out)->rvalue = true;
779 return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x, false), out);
785 static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
788 return fold_constgen_float(fold, -fold_immvalue_float(a), false);
789 } else if (isvector(a)) {
791 return fold_constgen_vector(fold, vec3_neg(fold_immvalue_vector(a)));
796 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
799 return fold_constgen_float(fold, !fold_immvalue_float(a), false);
800 } else if (isvector(a)) {
802 return fold_constgen_float(fold, vec3_notf(fold_immvalue_vector(a)), false);
803 } else if (isstring(a)) {
805 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
806 return fold_constgen_float(fold, !fold_immvalue_string(a), false);
808 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a), false);
814 static bool fold_check_except_float(sfloat_t (*callback)(sfloat_state_t *, sfloat_t, sfloat_t),
823 s.roundingmode = SFLOAT_ROUND_NEAREST_EVEN;
824 s.tiny = SFLOAT_TBEFORE;
825 s.exceptionflags = 0;
826 ca.f = fold_immvalue_float(a);
827 cb.f = fold_immvalue_float(b);
829 callback(&s, ca.s, cb.s);
830 if (s.exceptionflags == 0)
833 if (s.exceptionflags & SFLOAT_DIVBYZERO)
834 compile_error(fold_ctx(fold), "division by zero");
837 * To be enabled once softfloat implementations for stuff like sqrt()
840 if (s.exceptionflags & SFLOAT_INVALID)
841 compile_error(fold_ctx(fold), "invalid argument");
844 if (s.exceptionflags & SFLOAT_OVERFLOW)
845 compile_error(fold_ctx(fold), "arithmetic overflow");
846 if (s.exceptionflags & SFLOAT_UNDERFLOW)
847 compile_error(fold_ctx(fold), "arithmetic underflow");
849 return s.exceptionflags == SFLOAT_INEXACT;
852 static bool fold_check_inexact_float(fold_t *fold, ast_value *a, ast_value *b) {
853 lex_ctx_t ctx = fold_ctx(fold);
854 if (!a->inexact && !b->inexact)
856 return compile_warning(ctx, WARN_INEXACT_COMPARES, "inexact value in comparison");
859 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
861 if (fold_can_2(a, b)) {
862 bool inexact = fold_check_except_float(&sfloat_add, fold, a, b);
863 return fold_constgen_float(fold, fold_immvalue_float(a) + fold_immvalue_float(b), inexact);
865 } else if (isvector(a)) {
866 if (fold_can_2(a, b))
867 return fold_constgen_vector(fold, vec3_add(fold_immvalue_vector(a), fold_immvalue_vector(b)));
872 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
874 if (fold_can_2(a, b)) {
875 bool inexact = fold_check_except_float(&sfloat_sub, fold, a, b);
876 return fold_constgen_float(fold, fold_immvalue_float(a) - fold_immvalue_float(b), inexact);
878 } else if (isvector(a)) {
879 if (fold_can_2(a, b))
880 return fold_constgen_vector(fold, vec3_sub(fold_immvalue_vector(a), fold_immvalue_vector(b)));
885 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
888 if (fold_can_2(a, b))
889 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a)));
891 if (fold_can_2(a, b)) {
892 bool inexact = fold_check_except_float(&sfloat_mul, fold, a, b);
893 return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b), inexact);
896 } else if (isvector(a)) {
898 if (fold_can_2(a, b))
899 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
901 if (fold_can_2(a, b)) {
902 return fold_constgen_float(fold, vec3_mulvv(fold_immvalue_vector(a), fold_immvalue_vector(b)), false);
903 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(a)) {
905 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "xyz"))) return out;
906 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "yxz"))) return out;
907 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "zxy"))) return out;
908 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(b)) {
910 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "xyz"))) return out;
911 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "yxz"))) return out;
912 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "zxy"))) return out;
919 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
921 if (fold_can_2(a, b)) {
922 bool inexact = fold_check_except_float(&sfloat_div, fold, a, b);
923 return fold_constgen_float(fold, fold_immvalue_float(a) / fold_immvalue_float(b), inexact);
924 } else if (fold_can_1(b)) {
925 return (ast_expression*)ast_binary_new(
929 fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
932 } else if (isvector(a)) {
933 if (fold_can_2(a, b)) {
934 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
936 return (ast_expression*)ast_binary_new(
941 ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
942 : (ast_expression*)ast_binary_new(
945 (ast_expression*)fold->imm_float[1],
954 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
955 return (fold_can_2(a, b))
956 ? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)), false)
960 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
962 if (fold_can_2(a, b))
963 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))), false);
966 if (fold_can_2(a, b))
967 return fold_constgen_vector(fold, vec3_or(fold_immvalue_vector(a), fold_immvalue_vector(b)));
969 if (fold_can_2(a, b))
970 return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
976 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
978 if (fold_can_2(a, b))
979 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))), false);
982 if (fold_can_2(a, b))
983 return fold_constgen_vector(fold, vec3_and(fold_immvalue_vector(a), fold_immvalue_vector(b)));
985 if (fold_can_2(a, b))
986 return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
992 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
994 if (fold_can_2(a, b))
995 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))), false);
997 if (fold_can_2(a, b)) {
999 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1001 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1007 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
1008 if (fold_can_2(a, b) && isfloats(a, b))
1009 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) * powf(2.0f, fold_immvalue_float(b))), false);
1013 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
1014 if (fold_can_2(a, b) && isfloats(a, b))
1015 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) / powf(2.0f, fold_immvalue_float(b))), false);
1019 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
1020 if (fold_can_2(a, b)) {
1021 if (OPTS_FLAG(PERL_LOGIC)) {
1023 return (fold_immediate_true(fold, a)) ? (ast_expression*)a : (ast_expression*)b;
1025 return (fold_immediate_true(fold, a)) ? (ast_expression*)b : (ast_expression*)a;
1027 return fold_constgen_float (
1029 ((expr) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
1030 : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
1040 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
1041 if (fold_can_1(a)) {
1042 return fold_immediate_true(fold, a)
1043 ? (ast_expression*)b
1044 : (ast_expression*)c;
1049 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
1050 if (fold_can_2(a, b))
1051 return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)), false);
1055 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
1056 if (fold_can_2(a,b)) {
1057 fold_check_inexact_float(fold, a, b);
1058 if (fold_immvalue_float(a) < fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[2];
1059 if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
1060 if (fold_immvalue_float(a) > fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
1065 static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast_value *b, bool lt) {
1066 if (fold_can_2(a, b)) {
1067 fold_check_inexact_float(fold, a, b);
1068 return (lt) ? (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) < fold_immvalue_float(b))]
1069 : (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) > fold_immvalue_float(b))];
1074 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
1075 if (fold_can_2(a, b)) {
1076 if (isfloat(a) && isfloat(b)) {
1077 float la = fold_immvalue_float(a);
1078 float lb = fold_immvalue_float(b);
1079 fold_check_inexact_float(fold, a, b);
1080 return (ast_expression*)fold->imm_float[!(ne ? la == lb : la != lb)];
1081 } if (isvector(a) && isvector(b)) {
1082 vec3_t la = fold_immvalue_vector(a);
1083 vec3_t lb = fold_immvalue_vector(b);
1084 return (ast_expression*)fold->imm_float[!(ne ? vec3_cmp(la, lb) : !vec3_cmp(la, lb))];
1090 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
1093 return fold_constgen_float(fold, -1-fold_immvalue_float(a), false);
1097 return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
1103 static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
1104 if (fold_can_2(a, b))
1105 return fold_constgen_vector(fold, vec3_cross(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1109 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
1110 ast_value *a = (ast_value*)opexprs[0];
1111 ast_value *b = (ast_value*)opexprs[1];
1112 ast_value *c = (ast_value*)opexprs[2];
1113 ast_expression *e = NULL;
1115 /* can a fold operation be applied to this operator usage? */
1119 switch(info->operands) {
1120 case 3: if(!c) return NULL;
1121 case 2: if(!b) return NULL;
1124 compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
1130 * we could use a boolean and default case but ironically gcc produces
1131 * invalid broken assembly from that operation. clang/tcc get it right,
1132 * but interestingly ignore compiling this to a jump-table when I do that,
1133 * this happens to be the most efficent method, since you have per-level
1134 * granularity on the pointer check happening only for the case you check
1135 * it in. Opposed to the default method which would involve a boolean and
1136 * pointer check after wards.
1138 #define fold_op_case(ARGS, ARGS_OPID, OP, ARGS_FOLD) \
1139 case opid##ARGS ARGS_OPID: \
1140 if ((e = fold_op_##OP ARGS_FOLD)) { \
1141 ++opts_optimizationcount[OPTIM_CONST_FOLD]; \
1146 fold_op_case(2, ('-', 'P'), neg, (fold, a));
1147 fold_op_case(2, ('!', 'P'), not, (fold, a));
1148 fold_op_case(1, ('+'), add, (fold, a, b));
1149 fold_op_case(1, ('-'), sub, (fold, a, b));
1150 fold_op_case(1, ('*'), mul, (fold, a, b));
1151 fold_op_case(1, ('/'), div, (fold, a, b));
1152 fold_op_case(1, ('%'), mod, (fold, a, b));
1153 fold_op_case(1, ('|'), bor, (fold, a, b));
1154 fold_op_case(1, ('&'), band, (fold, a, b));
1155 fold_op_case(1, ('^'), xor, (fold, a, b));
1156 fold_op_case(1, ('<'), ltgt, (fold, a, b, true));
1157 fold_op_case(1, ('>'), ltgt, (fold, a, b, false));
1158 fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
1159 fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
1160 fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
1161 fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
1162 fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
1163 fold_op_case(2, ('*', '*'), exp, (fold, a, b));
1164 fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
1165 fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
1166 fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
1167 fold_op_case(2, ('~', 'P'), bnot, (fold, a));
1168 fold_op_case(2, ('>', '<'), cross, (fold, a, b));
1171 compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
1176 * Constant folding for compiler intrinsics, simaler approach to operator
1177 * folding, primarly: individual functions for each intrinsics to fold,
1178 * and a generic selection function.
1180 static GMQCC_INLINE ast_expression *fold_intrin_isfinite(fold_t *fold, ast_value *a) {
1181 return fold_constgen_float(fold, isfinite(fold_immvalue_float(a)), false);
1183 static GMQCC_INLINE ast_expression *fold_intrin_isinf(fold_t *fold, ast_value *a) {
1184 return fold_constgen_float(fold, isinf(fold_immvalue_float(a)), false);
1186 static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *a) {
1187 return fold_constgen_float(fold, isnan(fold_immvalue_float(a)), false);
1189 static GMQCC_INLINE ast_expression *fold_intrin_isnormal(fold_t *fold, ast_value *a) {
1190 return fold_constgen_float(fold, isnormal(fold_immvalue_float(a)), false);
1192 static GMQCC_INLINE ast_expression *fold_intrin_signbit(fold_t *fold, ast_value *a) {
1193 return fold_constgen_float(fold, signbit(fold_immvalue_float(a)), false);
1195 static GMQCC_INLINE ast_expression *fold_intirn_acosh(fold_t *fold, ast_value *a) {
1196 return fold_constgen_float(fold, acoshf(fold_immvalue_float(a)), false);
1198 static GMQCC_INLINE ast_expression *fold_intrin_asinh(fold_t *fold, ast_value *a) {
1199 return fold_constgen_float(fold, asinhf(fold_immvalue_float(a)), false);
1201 static GMQCC_INLINE ast_expression *fold_intrin_atanh(fold_t *fold, ast_value *a) {
1202 return fold_constgen_float(fold, (float)atanh(fold_immvalue_float(a)), false);
1204 static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *a) {
1205 return fold_constgen_float(fold, expf(fold_immvalue_float(a)), false);
1207 static GMQCC_INLINE ast_expression *fold_intrin_exp2(fold_t *fold, ast_value *a) {
1208 return fold_constgen_float(fold, exp2f(fold_immvalue_float(a)), false);
1210 static GMQCC_INLINE ast_expression *fold_intrin_expm1(fold_t *fold, ast_value *a) {
1211 return fold_constgen_float(fold, expm1f(fold_immvalue_float(a)), false);
1213 static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1214 return fold_constgen_float(fold, fmodf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1216 static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1217 return fold_constgen_float(fold, powf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1219 static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *a) {
1220 return fold_constgen_float(fold, fabsf(fold_immvalue_float(a)), false);
1224 ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
1225 ast_expression *ret = NULL;
1226 ast_value *a = (ast_value*)arg[0];
1227 ast_value *b = (ast_value*)arg[1];
1229 if (!strcmp(intrin, "isfinite")) ret = fold_intrin_isfinite(fold, a);
1230 if (!strcmp(intrin, "isinf")) ret = fold_intrin_isinf(fold, a);
1231 if (!strcmp(intrin, "isnan")) ret = fold_intrin_isnan(fold, a);
1232 if (!strcmp(intrin, "isnormal")) ret = fold_intrin_isnormal(fold, a);
1233 if (!strcmp(intrin, "signbit")) ret = fold_intrin_signbit(fold, a);
1234 if (!strcmp(intrin, "acosh")) ret = fold_intirn_acosh(fold, a);
1235 if (!strcmp(intrin, "asinh")) ret = fold_intrin_asinh(fold, a);
1236 if (!strcmp(intrin, "atanh")) ret = fold_intrin_atanh(fold, a);
1237 if (!strcmp(intrin, "exp")) ret = fold_intrin_exp(fold, a);
1238 if (!strcmp(intrin, "exp2")) ret = fold_intrin_exp2(fold, a);
1239 if (!strcmp(intrin, "expm1")) ret = fold_intrin_expm1(fold, a);
1240 if (!strcmp(intrin, "mod")) ret = fold_intrin_mod(fold, a, b);
1241 if (!strcmp(intrin, "pow")) ret = fold_intrin_pow(fold, a, b);
1242 if (!strcmp(intrin, "fabs")) ret = fold_intrin_fabs(fold, a);
1245 ++opts_optimizationcount[OPTIM_CONST_FOLD];
1251 * These are all the actual constant folding methods that happen in between
1252 * the AST/IR stage of the compiler , i.e eliminating branches for const
1253 * expressions, which is the only supported thing so far. We undefine the
1254 * testing macros here because an ir_value is differant than an ast_value.
1260 #undef fold_immvalue_float
1261 #undef fold_immvalue_string
1262 #undef fold_immvalue_vector
1266 #define isfloat(X) ((X)->vtype == TYPE_FLOAT)
1267 /*#define isstring(X) ((X)->vtype == TYPE_STRING)*/
1268 /*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/
1269 #define fold_immvalue_float(X) ((X)->constval.vfloat)
1270 #define fold_immvalue_vector(X) ((X)->constval.vvec)
1271 /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
1272 #define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST)
1273 /*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
1275 static ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
1276 ast_expression *swapped = NULL; /* using this as bool */
1279 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right))) {
1285 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right)))
1293 if (fold_immvalue_float(load) == 1.0f) {
1294 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1305 if (fold_immvalue_float(load) == 0.0f) {
1306 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1313 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) {
1314 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1324 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
1325 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1335 ast_expression *fold_binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right) {
1336 ast_expression *ret = fold_superfluous(left, right, op);
1339 return (ast_expression*)ast_binary_new(ctx, op, left, right);
1342 static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1343 if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) {
1344 ast_expression_codegen *cgen;
1347 bool istrue = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
1348 bool isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
1349 ast_expression *path = (istrue) ? branch->on_true :
1350 (isfalse) ? branch->on_false : NULL;
1353 * no path to take implies that the evaluation is if(0) and there
1354 * is no else block. so eliminate all the code.
1356 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1360 if (!(elide = ir_function_create_block(ast_ctx(branch), func->ir_func, ast_function_label(func, ((istrue) ? "ontrue" : "onfalse")))))
1362 if (!(*(cgen = path->codegen))((ast_expression*)path, func, false, &dummy))
1364 if (!ir_block_create_jump(func->curblock, ast_ctx(branch), elide))
1367 * now the branch has been eliminated and the correct block for the constant evaluation
1368 * is expanded into the current block for the function.
1370 func->curblock = elide;
1371 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1374 return -1; /* nothing done */
1377 int fold_cond_ternary(ir_value *condval, ast_function *func, ast_ternary *branch) {
1378 return fold_cond(condval, func, (ast_ifthen*)branch);
1381 int fold_cond_ifthen(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1382 return fold_cond(condval, func, branch);