Skip to content

Commit 83a0502

Browse files
authored
only look at the |signed| field of loads if it is relevant (#1235)
1 parent 1005b12 commit 83a0502

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

src/ast/ExpressionAnalyzer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "ast_utils.h"
1817
#include "support/hash.h"
19-
18+
#include "ast_utils.h"
19+
#include "ast/load-utils.h"
2020

2121
namespace wasm {
2222
// Given a stack of expressions, checks if the topmost is used as a result.
@@ -208,7 +208,10 @@ bool ExpressionAnalyzer::flexibleEqual(Expression* left, Expression* right, Expr
208208
}
209209
case Expression::Id::LoadId: {
210210
CHECK(Load, bytes);
211-
CHECK(Load, signed_);
211+
if (LoadUtils::isSignRelevant(left->cast<Load>()) &&
212+
LoadUtils::isSignRelevant(right->cast<Load>())) {
213+
CHECK(Load, signed_);
214+
}
212215
CHECK(Load, offset);
213216
CHECK(Load, align);
214217
PUSH(Load, ptr);
@@ -455,7 +458,9 @@ uint32_t ExpressionAnalyzer::hash(Expression* curr) {
455458
}
456459
case Expression::Id::LoadId: {
457460
HASH(Load, bytes);
458-
HASH(Load, signed_);
461+
if (LoadUtils::isSignRelevant(curr->cast<Load>())) {
462+
HASH(Load, signed_);
463+
}
459464
HASH(Load, offset);
460465
HASH(Load, align);
461466
PUSH(Load, ptr);

src/ast/load-utils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef wasm_ast_load_h
18+
#define wasm_ast_load_h
19+
20+
#include "wasm.h"
21+
22+
namespace wasm {
23+
24+
namespace LoadUtils {
25+
26+
// checks if the sign of a load matters, which is when an integer
27+
// load is of fewer bytes than the size of the type (so we must
28+
// fill in bits either signed or unsigned wise)
29+
inline bool isSignRelevant(Load* load) {
30+
auto type = load->type;
31+
if (load->type == unreachable) return false;
32+
return !isWasmTypeFloat(type) && load->bytes < getWasmTypeSize(type);
33+
}
34+
35+
} // namespace LoadUtils
36+
37+
} // namespace wasm
38+
39+
#endif // wasm_ast_load_h
40+

src/passes/OptimizeInstructions.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <ast/manipulation.h>
3131
#include <ast/properties.h>
3232
#include <ast/literal-utils.h>
33+
#include <ast/load-utils.h>
3334

3435
// TODO: Use the new sign-extension opcodes where appropriate. This needs to be conditionalized on the availability of atomics.
3536

@@ -243,7 +244,8 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
243244
return localInfoProvider->getMaxBitsForLocal(get);
244245
} else if (auto* load = curr->dynCast<Load>()) {
245246
// if signed, then the sign-extension might fill all the bits
246-
if (!load->signed_) {
247+
// if unsigned, then we have a limit
248+
if (LoadUtils::isSignRelevant(load) && !load->signed_) {
247249
return 8 * load->bytes;
248250
}
249251
}
@@ -320,7 +322,7 @@ struct LocalScanner : PostWalker<LocalScanner> {
320322
if (Properties::getSignExtValue(value)) {
321323
signExtBits = Properties::getSignExtBits(value);
322324
} else if (auto* load = value->dynCast<Load>()) {
323-
if (load->signed_) {
325+
if (LoadUtils::isSignRelevant(load) && load->signed_) {
324326
signExtBits = load->bytes * 8;
325327
}
326328
}

0 commit comments

Comments
 (0)