X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=fold.c;h=d485ed23f1dba4c9957f4635773983b8467fc75b;hp=32eeae625670c87fc246eae863c616c74e952346;hb=151606e25558ec8f8f211db5aba4c4fa66948731;hpb=e18849fa423e57637c30e3f1aa633f8485fb96e6 diff --git a/fold.c b/fold.c index 32eeae6..d485ed2 100644 --- a/fold.c +++ b/fold.c @@ -338,7 +338,7 @@ ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool transla if (translate) { char name[32]; - util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++)); + platform_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++)); out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING); out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */ } else @@ -494,11 +494,9 @@ static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_ } else if (isvector(a)) { if (fold_can_2(a, b)) { if (fold_can_div(b)) { - printf("hit wrong logic\n"); return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b))); } else { - printf("hit logic\n"); return (ast_expression*)fold->imm_vector[2]; /* inf */ } } else { @@ -793,13 +791,55 @@ ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **a /*#define isstring(X) ((X)->vtype == TYPE_STRING)*/ /*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/ #define fold_immvalue_float(X) ((X)->constval.vfloat) -/*#define fold_immvalue_vector(X) ((X)->constval.vvec)*/ +#define fold_immvalue_vector(X) ((X)->constval.vvec) /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/ #define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST) /*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/ +ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) { + ast_value *load; -int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) { + if (!ast_istype(left, ast_value) || !fold_can_1((load = (ast_value*)right))) + return NULL; + + switch (op) { + case INSTR_MUL_F: + case INSTR_DIV_F: + if (fold_immvalue_float(load) == 1.0f) { + ++opts_optimizationcount[OPTIM_PEEPHOLE]; + return (ast_expression*)left; + } + break; + + + case INSTR_ADD_F: + case INSTR_SUB_F: + if (fold_immvalue_float(load) == 0.0f) { + ++opts_optimizationcount[OPTIM_PEEPHOLE]; + return (ast_expression*)left; + } + break; + + case INSTR_MUL_V: + if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) { + ++opts_optimizationcount[OPTIM_PEEPHOLE]; + return (ast_expression*)left; + } + break; + + case INSTR_ADD_V: + case INSTR_SUB_V: + if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) { + ++opts_optimizationcount[OPTIM_PEEPHOLE]; + return (ast_expression*)left; + } + break; + } + + return NULL; +} + +static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) { if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) { ast_expression_codegen *cgen; ir_block *elide; @@ -833,3 +873,11 @@ int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) { } return -1; /* nothing done */ } + +int fold_cond_ternary(ir_value *condval, ast_function *func, ast_ternary *branch) { + return fold_cond(condval, func, (ast_ifthen*)branch); +} + +int fold_cond_ifthen(ir_value *condval, ast_function *func, ast_ifthen *branch) { + return fold_cond(condval, func, branch); +}