summaryrefslogtreecommitdiff
path: root/src/passes/ConstantFieldPropagation.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-03-25 07:33:44 -0700
committerGitHub <noreply@github.com>2022-03-25 07:33:44 -0700
commit97d68ac572a0ffdc74fc5d8da2df65da42dc603e (patch)
tree6780465e64c245971d64014526c23fb3176e8a83 /src/passes/ConstantFieldPropagation.cpp
parent22d24fda983d471ebf73ebadbc37ef1741a5594d (diff)
downloadbinaryen-97d68ac572a0ffdc74fc5d8da2df65da42dc603e.tar.gz
binaryen-97d68ac572a0ffdc74fc5d8da2df65da42dc603e.tar.bz2
binaryen-97d68ac572a0ffdc74fc5d8da2df65da42dc603e.zip
[NFC] Refactor constant value finding code (#4546)
This just moves PossibleConstantValues to a new separate file (as a preparation for other passes using it too).
Diffstat (limited to 'src/passes/ConstantFieldPropagation.cpp')
-rw-r--r--src/passes/ConstantFieldPropagation.cpp117
1 files changed, 1 insertions, 116 deletions
diff --git a/src/passes/ConstantFieldPropagation.cpp b/src/passes/ConstantFieldPropagation.cpp
index 340b3b95e..44e74f2d4 100644
--- a/src/passes/ConstantFieldPropagation.cpp
+++ b/src/passes/ConstantFieldPropagation.cpp
@@ -27,9 +27,8 @@
// wasm GC programs we need to check for type escaping.
//
-#include <variant>
-
#include "ir/module-utils.h"
+#include "ir/possible-constant.h"
#include "ir/properties.h"
#include "ir/struct-utils.h"
#include "ir/utils.h"
@@ -43,120 +42,6 @@ namespace wasm {
namespace {
-// No possible value.
-struct None : public std::monostate {};
-
-// Many possible values, and so this represents unknown data: we cannot infer
-// anything there.
-struct Many : public std::monostate {};
-
-// Represents data about what constant values are possible in a particular
-// place. There may be no values, or one, or many, or if a non-constant value is
-// possible, then all we can say is that the value is "unknown" - it can be
-// anything. The values can either be literal values (Literal) or the names of
-// immutable globals (Name).
-//
-// Currently this just looks for a single constant value, and even two constant
-// values are treated as unknown. It may be worth optimizing more than that TODO
-struct PossibleConstantValues {
-private:
- using Variant = std::variant<None, Literal, Name, Many>;
- Variant value;
-
-public:
- PossibleConstantValues() : value(None()) {}
-
- // Note a written value as we see it, and update our internal knowledge based
- // on it and all previous values noted. This can be called using either a
- // Literal or a Name, so it uses a template.
- template<typename T> void note(T curr) {
- if (std::get_if<None>(&value)) {
- // This is the first value.
- value = curr;
- return;
- }
-
- if (std::get_if<Many>(&value)) {
- // This was already representing multiple values; nothing changes.
- return;
- }
-
- // This is a subsequent value. Check if it is different from all previous
- // ones.
- if (Variant(curr) != value) {
- noteUnknown();
- }
- }
-
- // Notes a value that is unknown - it can be anything. We have failed to
- // identify a constant value here.
- void noteUnknown() { value = Many(); }
-
- // Combine the information in a given PossibleConstantValues to this one. This
- // is the same as if we have called note*() on us with all the history of
- // calls to that other object.
- //
- // Returns whether we changed anything.
- bool combine(const PossibleConstantValues& other) {
- if (std::get_if<None>(&other.value)) {
- return false;
- }
-
- if (std::get_if<None>(&value)) {
- value = other.value;
- return true;
- }
-
- if (std::get_if<Many>(&value)) {
- return false;
- }
-
- if (other.value != value) {
- value = Many();
- return true;
- }
-
- return false;
- }
-
- // Check if all the values are identical and constant.
- bool isConstant() const {
- return !std::get_if<None>(&value) && !std::get_if<Many>(&value);
- }
-
- bool isConstantLiteral() const { return std::get_if<Literal>(&value); }
-
- bool isConstantGlobal() const { return std::get_if<Name>(&value); }
-
- // Returns the single constant value.
- Literal getConstantLiteral() const {
- assert(isConstant());
- return std::get<Literal>(value);
- }
-
- Name getConstantGlobal() const {
- assert(isConstant());
- return std::get<Name>(value);
- }
-
- // Returns whether we have ever noted a value.
- bool hasNoted() const { return !std::get_if<None>(&value); }
-
- void dump(std::ostream& o) {
- o << '[';
- if (!hasNoted()) {
- o << "unwritten";
- } else if (!isConstant()) {
- o << "unknown";
- } else if (isConstantLiteral()) {
- o << getConstantLiteral();
- } else if (isConstantGlobal()) {
- o << '$' << getConstantGlobal();
- }
- o << ']';
- }
-};
-
using PCVStructValuesMap = StructUtils::StructValuesMap<PossibleConstantValues>;
using PCVFunctionStructValuesMap =
StructUtils::FunctionStructValuesMap<PossibleConstantValues>;