summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ir/possible-contents.cpp31
-rw-r--r--src/passes/ConstantFieldPropagation.cpp11
2 files changed, 42 insertions, 0 deletions
diff --git a/src/ir/possible-contents.cpp b/src/ir/possible-contents.cpp
index 83762a458..d63830c40 100644
--- a/src/ir/possible-contents.cpp
+++ b/src/ir/possible-contents.cpp
@@ -17,8 +17,10 @@
#include <optional>
#include <variant>
+#include "ir/bits.h"
#include "ir/branch-utils.h"
#include "ir/eh-utils.h"
+#include "ir/gc-type-utils.h"
#include "ir/local-graph.h"
#include "ir/module-utils.h"
#include "ir/possible-contents.h"
@@ -1430,6 +1432,8 @@ private:
bool& worthSendingMore);
void filterGlobalContents(PossibleContents& contents,
const GlobalLocation& globalLoc);
+ void filterDataContents(PossibleContents& contents,
+ const DataLocation& dataLoc);
// Reads from GC data: a struct.get or array.get. This is given the type of
// the read operation, the field that is read on that type, the known contents
@@ -1739,6 +1743,9 @@ bool Flower::updateContents(LocationIndex locationIndex,
} else if (auto* globalLoc = std::get_if<GlobalLocation>(&location)) {
filterGlobalContents(contents, *globalLoc);
filtered = true;
+ } else if (auto* dataLoc = std::get_if<DataLocation>(&location)) {
+ filterDataContents(contents, *dataLoc);
+ filtered = true;
}
// Check if anything changed after filtering, if we did so.
@@ -1971,6 +1978,30 @@ void Flower::filterGlobalContents(PossibleContents& contents,
}
}
+void Flower::filterDataContents(PossibleContents& contents,
+ const DataLocation& dataLoc) {
+ auto field = GCTypeUtils::getField(dataLoc.type, dataLoc.index);
+ assert(field);
+ if (field->isPacked()) {
+ // We must handle packed fields carefully.
+ if (contents.isLiteral()) {
+ // This is a constant. We can truncate it and use that value.
+ auto mask = Literal(int32_t(Bits::lowBitMask(field->getByteSize() * 8)));
+ contents = PossibleContents::literal(contents.getLiteral().and_(mask));
+ } else {
+ // This is not a constant. We can't even handle a global here, as we'd
+ // need to track that this global's value must be truncated before it is
+ // used, and we don't do that atm. Leave only the type.
+ // TODO Consider tracking packing on GlobalInfo alongside the type.
+ // Another option is to make GUFA.cpp apply packing on the read,
+ // like CFP does - but that can only be done when replacing a
+ // StructGet of a packed field, and not anywhere else we saw that
+ // value reach.
+ contents = PossibleContents::fromType(contents.getType());
+ }
+ }
+}
+
void Flower::readFromData(Type declaredType,
Index fieldIndex,
const PossibleContents& refContents,
diff --git a/src/passes/ConstantFieldPropagation.cpp b/src/passes/ConstantFieldPropagation.cpp
index c5b95b15a..f97b440ea 100644
--- a/src/passes/ConstantFieldPropagation.cpp
+++ b/src/passes/ConstantFieldPropagation.cpp
@@ -27,6 +27,8 @@
// wasm GC programs we need to check for type escaping.
//
+#include "ir/bits.h"
+#include "ir/gc-type-utils.h"
#include "ir/module-utils.h"
#include "ir/possible-constant.h"
#include "ir/struct-utils.h"
@@ -105,6 +107,15 @@ struct FunctionOptimizer : public WalkerPass<PostWalker<FunctionOptimizer>> {
// constant value. (Leave it to further optimizations to get rid of the
// ref.)
Expression* value = info.makeExpression(*getModule());
+ auto field = GCTypeUtils::getField(type, curr->index);
+ assert(field);
+ if (field->isPacked()) {
+ // We cannot just pass through a value that is packed, as the input gets
+ // truncated.
+ auto mask = Bits::lowBitMask(field->getByteSize() * 8);
+ value =
+ builder.makeBinary(AndInt32, value, builder.makeConst(int32_t(mask)));
+ }
replaceCurrent(builder.makeSequence(
builder.makeDrop(builder.makeRefAs(RefAsNonNull, curr->ref)), value));
changed = true;