diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 9 | ||||
-rw-r--r-- | test/lit/passes/optimize-instructions-memory64.wast | 31 |
2 files changed, 38 insertions, 2 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index e1478b54f..3a2841c1e 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -42,6 +42,7 @@ #include <ir/type-updating.h> #include <ir/utils.h> #include <pass.h> +#include <support/stdckdint.h> #include <support/threads.h> #include <wasm.h> @@ -3501,8 +3502,12 @@ private: uint64_t offset64 = offset; auto mem = getModule()->getMemory(memory); if (mem->is64()) { - last->value = Literal(int64_t(value64 + offset64)); - offset = 0; + // Check for a 64-bit overflow. + uint64_t sum; + if (!std::ckd_add(&sum, value64, offset64)) { + last->value = Literal(int64_t(sum)); + offset = 0; + } } else { // don't do this if it would wrap the pointer if (value64 <= uint64_t(std::numeric_limits<int32_t>::max()) && diff --git a/test/lit/passes/optimize-instructions-memory64.wast b/test/lit/passes/optimize-instructions-memory64.wast new file mode 100644 index 000000000..f74dad7a8 --- /dev/null +++ b/test/lit/passes/optimize-instructions-memory64.wast @@ -0,0 +1,31 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --optimize-instructions -all -S -o - | filecheck %s + +(module + ;; CHECK: (memory $0 i64 16 17) + (memory $0 i64 16 17) + + ;; CHECK: (func $offsets (type $0) + ;; CHECK-NEXT: (i64.store + ;; CHECK-NEXT: (i64.const 10) + ;; CHECK-NEXT: (i64.const 42) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (f32.store offset=4 + ;; CHECK-NEXT: (i64.const -3) + ;; CHECK-NEXT: (f32.const 3.141590118408203) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $offsets + ;; It is safe to combine the offset and constant here. + (i64.store offset=4 + (i64.const 6) + (i64.const 42) + ) + ;; This would overflow, so do not optimize. + (f32.store offset=4 + (i64.const -3) + (f32.const 3.14159) + ) + ) +) + |