Skip to content

Commit

Permalink
avoid signed overflow undefined behavior in OptimizeInstructions cons…
Browse files Browse the repository at this point in the history
…tant computations (#1990)
  • Loading branch information
kripken authored Apr 9, 2019
1 parent d318b18 commit b13db5a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,11 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
// find added constants in an expression tree, including multiplied/shifted, and combine them
// note that we ignore division/shift-right, as rounding makes this nonlinear, so not a valid opt
Expression* optimizeAddedConstants(Binary* binary) {
int32_t constant = 0;
uint32_t constant = 0;
std::vector<Const*> constants;
std::function<void (Expression*, int)> seek = [&](Expression* curr, int mul) {
if (auto* c = curr->dynCast<Const>()) {
auto value = c->value.geti32();
uint32_t value = c->value.geti32();
if (value != 0) {
constant += value * mul;
constants.push_back(c);
Expand Down
6 changes: 6 additions & 0 deletions test/passes/O1.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
)
)
)
(func $signed-overflow (param $0 f32) (result i32)
(i32.sub
(i32.const 268435456)
(i32.const -2147483648)
)
)
)


0 comments on commit b13db5a

Please sign in to comment.