Implement constant folding on ternary operations via fold_cond.

This commit is contained in:
Dale Weiler 2013-09-26 06:51:49 -04:00
parent cf2352893f
commit 15b0555546
3 changed files with 18 additions and 4 deletions

9
ast.c
View file

@ -2582,8 +2582,8 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
/* update the block which will get the jump - because short-logic or ternaries may have changed this */
cond = func->curblock;
/* try constant folding away the if */
if ((fold = fold_cond(condval, func, self)) != -1)
/* try constant folding away the condition */
if ((fold = fold_cond_ifthen(condval, func, self)) != -1)
return fold;
if (self->on_true) {
@ -2666,6 +2666,7 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
ir_block *ontrue, *ontrue_out = NULL;
ir_block *onfalse, *onfalse_out = NULL;
ir_block *merge;
int fold = 0;
/* Ternary can never create an lvalue... */
if (lvalue)
@ -2690,6 +2691,10 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
return false;
cond_out = func->curblock;
/* try constant folding away the condition */
if ((fold = fold_cond_ternary(condval, func, self)) != -1)
return fold;
/* create on-true block */
ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
if (!ontrue)

10
fold.c
View file

@ -797,7 +797,7 @@ ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **a
/*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
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;
@ -831,3 +831,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);
}

View file

@ -133,7 +133,8 @@ bool fold_generate (fold_t *, ir_builder *);
ast_expression *fold_op (fold_t *, const oper_info *, ast_expression **);
ast_expression *fold_intrin (fold_t *, const char *, ast_expression **);
int fold_cond (ir_value *, ast_function *, ast_ifthen *);
int fold_cond_ifthen (ir_value *, ast_function *, ast_ifthen *);
int fold_cond_ternary (ir_value *, ast_function *, ast_ternary *);
/* intrin.c */
intrin_t *intrin_init (parser_t *parser);