2727
2828namespace wasm {
2929
30+ static bool canReplaceWithReinterpret (Load* load) {
31+ // We can replace a full-size load with a valid pointer with
32+ // a reinterpret of the same address. A partial load would see
33+ // more bytes and possibly invalid data, and an unreachable
34+ // pointer is just not interesting to handle.
35+ return load->type != unreachable && load->bytes == getTypeSize (load->type );
36+ }
37+
3038static Load* getSingleLoad (LocalGraph* localGraph, GetLocal* get) {
3139 std::set<GetLocal*> seen;
3240 seen.insert (get);
@@ -104,7 +112,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
104112 for (auto & pair : infos) {
105113 auto * load = pair.first ;
106114 auto & info = pair.second ;
107- if (info.reinterpreted && load-> type != unreachable ) {
115+ if (info.reinterpreted && canReplaceWithReinterpret ( load) ) {
108116 // We should use another load here, to avoid reinterprets.
109117 info.ptrLocal = Builder::addVar (func, i32 );
110118 info.reinterpretedLocal =
@@ -131,8 +139,10 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
131139 if (isReinterpret (curr)) {
132140 auto * value = Properties::getFallthrough (curr->value );
133141 if (auto * load = value->dynCast <Load>()) {
134- // A reinterpret of a load - flip it right here.
135- replaceCurrent (makeReinterpretedLoad (load, load->ptr ));
142+ // A reinterpret of a load - flip it right here if we can.
143+ if (canReplaceWithReinterpret (load)) {
144+ replaceCurrent (makeReinterpretedLoad (load, load->ptr ));
145+ }
136146 } else if (auto * get = value->dynCast <GetLocal>()) {
137147 if (auto * load = getSingleLoad (localGraph, get)) {
138148 auto iter = infos.find (load);
0 commit comments