opt_common_subexpr: Optimizer: Common Subexpression Elimination.

View source: R/opt-common-subexpr.R

opt_common_subexprR Documentation

Optimizer: Common Subexpression Elimination.

Description

Performs one common subexpression elimination pass. Carefully examine the results after running this function!

Usage

opt_common_subexpr(texts, n_values = 2, in_fun_call = FALSE)

Arguments

texts

A list of character vectors with the code to optimize.

n_values

A numeric indicating the minimum number of values to consider a subexpression.

in_fun_call

A logical indicating whether it should propagate in function calls. Note: this could change the semantics of the program.

Examples

code <- paste(
  "a <- b * c + g",
  "d = b * c * e",
  sep = "\n"
)
cat(opt_common_subexpr(list(code))$codes[[1]])

heron_formula <- paste(
  "area <- (a/2 + b/2 +c/2) * (a/2 + b/2 + c/2 - a) * (a/2 + b/2 + c/2 - b) *",
  "  (a/2 + b/2 + c/2 - c)",
  "area <- sqrt(area)",
  sep = '\n'
)
cat(opt_common_subexpr(list(heron_formula))$codes[[1]])

jcrodriguez1989/rco documentation built on Nov. 12, 2024, 12:23 p.m.