compact tree by evaluating constant expressions e.g. MINUS(X) where X is a constant number will be reduced to a single node with the value -X PLUS(MINUS(A), B) will be reduced to a single constant: B-A
| 290 | // a single node with the value -X |
| 291 | // PLUS(MINUS(A), B) will be reduced to a single constant: B-A |
| 292 | bool AR_EXP_ReduceToScalar |
| 293 | ( |
| 294 | AR_ExpNode *root, |
| 295 | bool reduce_params, |
| 296 | SIValue *val |
| 297 | ) { |
| 298 | if(val != NULL) { |
| 299 | *val = SI_NullVal(); |
| 300 | } |
| 301 | |
| 302 | if(root->type == AR_EXP_OPERAND) { |
| 303 | // in runtime, parameters are set so they can be evaluated |
| 304 | if(reduce_params && AR_EXP_IsParameter(root)) { |
| 305 | SIValue v = AR_EXP_Evaluate(root, NULL); |
| 306 | if(val != NULL) { |
| 307 | *val = v; |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | if(AR_EXP_IsConstant(root)) { |
| 312 | // Root is already a constant |
| 313 | if(val != NULL) *val = root->operand.constant; |
| 314 | return true; |
| 315 | } |
| 316 | // Root is variadic, no way to reduce |
| 317 | return false; |
| 318 | } else { |
| 319 | // root represents an operation |
| 320 | ASSERT(AR_EXP_IsOperation(root)); |
| 321 | |
| 322 | // see if we're able to reduce each child of root |
| 323 | // if so we'll be able to reduce root |
| 324 | bool reduce_children = true; |
| 325 | for(int i = 0; i < root->op.child_count; i++) { |
| 326 | if(!AR_EXP_ReduceToScalar(root->op.children[i], reduce_params, NULL)) { |
| 327 | // root reduce is not possible, but continue to reduce every reducable child |
| 328 | reduce_children = false; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // can't reduce root as one of its children is not a constant |
| 333 | if(!reduce_children) { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | // all child nodes are constants, make sure function is marked as reducible |
| 338 | if(!root->op.f->reducible) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | // evaluate function |
| 343 | SIValue v = AR_EXP_Evaluate(root, NULL); |
| 344 | if(val != NULL) *val = v; |
| 345 | if(SIValue_IsNull(v)) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | // reduce |
no test coverage detected