Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common sub-expression elimination and loop-invariant code motion. #75

Open
plusvic opened this issue Jan 24, 2024 · 0 comments
Open

Common sub-expression elimination and loop-invariant code motion. #75

plusvic opened this issue Jan 24, 2024 · 0 comments

Comments

@plusvic
Copy link
Member

plusvic commented Jan 24, 2024

In complex YARA conditions there are many cases in which the same sub-expression is repeated more than once, and its results could be reused instead of re-computed. For instance, consider this condition:

uint16(0) == 0x15FF or uint16(0) == 0x25FF 

The sub-expression uint16(0) is used twice, and the current implementation calls the uint16 function twice with the same argument. However, the result from the first invocation could be stored in a temporary variable and reused when uint16 is called for the second time, instead of invoking the function again, which is an expensive operation.

Additionally, when a sub-expression is contained in the body of a loop, it can be moved out of the loop if the sub-expression doesn't depend on the loop variables. For instance,

for any offset in (0..filesize-1): (
   ((uint16(offset) == 0x15FF or uint16(offset) == 0x25FF) and 
      uint32(offset+2) == pe.sections[0].virtual_address + pe.image_base)
)

In the example above, the sub-expression pe.sections[0].virtual_address+pe.image_base doesn't depend on the offset variable, and therefore produces the same result on each loop iteration. This expression could be evaluated once outside the loop, and its value reused inside the loop.

Common sub-expression elimination (CSE) and loop-invariant code motion (LICM) are well-known techniques used in compilers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant