From 1d67ab02aeb71b1a250a44161c8fdb3e97b04210 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Fri, 28 Jul 2017 12:45:19 -0700 Subject: do not combine a load/store offset with a constant pointer if it would wrap a negative value to a positive one, as trapping is tricky --- src/passes/OptimizeInstructions.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/passes/OptimizeInstructions.cpp') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index de6f96ccc..2930fe9c9 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -908,8 +908,15 @@ private: // it's better to do the opposite for gzip purposes as well as for readability. auto* last = ptr->dynCast(); if (last) { - last->value = Literal(int32_t(last->value.geti32() + offset)); - offset = 0; + // don't do this if it would wrap the pointer + uint64_t value64 = last->value.geti32(); + uint64_t offset64 = offset; + if (value64 <= std::numeric_limits::max() && + offset64 <= std::numeric_limits::max() && + value64 + offset64 <= std::numeric_limits::max()) { + last->value = Literal(int32_t(value64 + offset64)); + offset = 0; + } } } -- cgit v1.2.3