intrinsic folding cleanups (and improvements.)

This commit is contained in:
Dale Weiler 2013-08-30 07:12:16 -04:00
parent 76c37d9cc0
commit a50635bcd7
3 changed files with 60 additions and 61 deletions

90
fold.c
View file

@ -673,63 +673,49 @@ ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **op
return NULL; return NULL;
} }
#define expect(X) \ /*
do { \ * Constant folding for compiler intrinsics, simaler approach to operator
if (vec_size(params) != (X)) { \ * folding, primarly: individual functions for each intrinsics to fold,
compile_error( \ * and a generic selection function.
fold_ctx(fold), \ */
"internal error: attempted to constant-fold with invalid paramaters for intrinsic `%s`", \ static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
intrin \ return fold_constgen_float(
); \ fold,
return NULL; \ fmodf(
} \ fold_immvalue_float(lhs),
} while (0) fold_immvalue_float(rhs)
)
);
}
ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **params) { static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
if (!fold) return NULL; return fold_constgen_float(
if (!intrin) return NULL; fold,
powf(
fold_immvalue_float(lhs),
fold_immvalue_float(rhs)
)
);
}
if (!strcmp(intrin, "__builtin_exp")) { static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *value) {
expect(1); return fold_constgen_float(fold, exp(fold_immvalue_float(value)));
++opts_optimizationcount[OPTIM_CONST_FOLD]; }
return fold_constgen_float(fold, exp(fold_immvalue_float((ast_value*)params[0])));
}
if (!strcmp(intrin, "__builtin_mod")) { static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *value) {
expect(2); return fold_constgen_float(fold, isnan(fold_immvalue_float(value)) != 0.0f);
++opts_optimizationcount[OPTIM_CONST_FOLD]; }
return fold_constgen_float(
fold,
fmodf(
fold_immvalue_float((ast_value*)params[0]),
fold_immvalue_float((ast_value*)params[1])
)
);
}
if (!strcmp(intrin, "__builtin_pow")) { static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *value) {
expect(2); return fold_constgen_float(fold, fabs(fold_immvalue_float(value)));
++opts_optimizationcount[OPTIM_CONST_FOLD]; }
return fold_constgen_float(
fold,
powf(
fold_immvalue_float((ast_value*)params[0]),
fold_immvalue_float((ast_value*)params[1])
)
);
}
if (!strcmp(intrin, "__builtin_isnan")) { ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
expect(1); if (!strcmp(intrin, "mod")) return fold_intrin_mod (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
++opts_optimizationcount[OPTIM_CONST_FOLD]; if (!strcmp(intrin, "pow")) return fold_intrin_pow (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
return fold_constgen_float(fold, isnan(fold_immvalue_float((ast_value*)params[0])) != 0.0f); if (!strcmp(intrin, "exp")) return fold_intrin_exp (fold, (ast_value*)arg[0]);
} if (!strcmp(intrin, "isnan")) return fold_intrin_isnan(fold, (ast_value*)arg[0]);
if (!strcmp(intrin, "fabs")) return fold_intrin_fabs (fold, (ast_value*)arg[0]);
if (!strcmp(intrin, "__builtin_fabs")) {
expect(1);
++opts_optimizationcount[OPTIM_CONST_FOLD];
return fold_constgen_float(fold, fabs(fold_immvalue_float((ast_value*)params[0])));
}
return NULL; return NULL;
} }

View file

@ -409,12 +409,12 @@ ast_expression *intrin_debug_typestring(intrin_t *intrin) {
} }
static const intrin_func_t intrinsics[] = { static const intrin_func_t intrinsics[] = {
{&intrin_exp, "__builtin_exp", "exp"}, {&intrin_exp, "__builtin_exp", "exp", 1},
{&intrin_mod, "__builtin_mod", "mod"}, {&intrin_mod, "__builtin_mod", "mod", 2},
{&intrin_pow, "__builtin_pow", "pow"}, {&intrin_pow, "__builtin_pow", "pow", 2},
{&intrin_isnan, "__builtin_isnan", "isnan"}, {&intrin_isnan, "__builtin_isnan", "isnan", 1},
{&intrin_fabs, "__builtin_fabs", "fabs"}, {&intrin_fabs, "__builtin_fabs", "fabs", 1},
{&intrin_debug_typestring, "__builtin_debug_typestring", ""} {&intrin_debug_typestring, "__builtin_debug_typestring", "", 0}
}; };
static void intrin_error(intrin_t *intrin, const char *fmt, ...) { static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
@ -447,9 +447,21 @@ ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression *
if (!value || !value->name) if (!value || !value->name)
return NULL; return NULL;
for (i = 0; i < vec_size(intrin->intrinsics); i++) for (i = 0; i < vec_size(intrin->intrinsics); i++) {
if (!strcmp(value->name, intrin->intrinsics[i].name)) if (!strcmp(value->name, intrin->intrinsics[i].name)) {
return fold_intrin(intrin->fold, value->name, exprs); if (intrin->intrinsics[i].args != vec_size(exprs)) {
intrin_error(
intrin,
"internal error: attempted to constant-fold with invalid paramaters for intrinsic `%s`\n"
" ==> expected %u arguments, got %u instead",
value->name, intrin->intrinsics[i].args, vec_size(exprs)
);
return NULL;
}
/* +10 to skip the "__builtin_" substring in the string */
return fold_intrin(intrin->fold, value->name + 10, exprs);
}
}
return NULL; return NULL;
} }

View file

@ -43,6 +43,7 @@ typedef struct {
ast_expression *(*intrin)(intrin_t *); ast_expression *(*intrin)(intrin_t *);
const char *name; const char *name;
const char *alias; const char *alias;
size_t args;
} intrin_func_t; } intrin_func_t;
struct intrin_s { struct intrin_s {