Add folding for redundant add/sub/mul/div/mix operations
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>
Sat, 17 Feb 2018 19:55:54 +0000 (11:55 -0800)
committerSteven Perron <stevenperron@google.com>
Tue, 20 Feb 2018 23:29:27 +0000 (18:29 -0500)
commit309be423cc469624cdbddb5fe6e4ceb1dfd1adca
treee55e55c6f94874f7a15c5f75663081ff1e5ac504
parentfa3ac3cc335ede09ef57716de74f5ab84d3aef19
Add folding for redundant add/sub/mul/div/mix operations

This change implements instruction folding for arithmetic operations
that are redundant, specifically:

  x + 0 = 0 + x = x
  x - 0 = x
  0 - x = -x
  x * 0 = 0 * x = 0
  x * 1 = 1 * x = x
  0 / x = 0
  x / 1 = x
  mix(a, b, 0) = a
  mix(a, b, 1) = b

Cache ExtInst import id in feature manager

This allows us to avoid string lookups during optimization; for now we
just cache GLSL std450 import id but I can imagine caching more sets as
they become utilized by the optimizer.

Add tests for add/sub/mul/div/mix folding

The tests cover scalar float/double cases, and some vector cases.

Since most of the code for floating point folding is shared, the tests
for vector folding are not as exhaustive as scalar.

To test sub->negate folding I had to implement a custom fixture.
source/opt/const_folding_rules.cpp
source/opt/feature_manager.cpp
source/opt/feature_manager.h
source/opt/folding_rules.cpp
source/opt/insert_extract_elim.cpp
source/opt/instruction.cpp
source/opt/instruction.h
source/opt/pass.h
test/opt/fold_test.cpp