aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-04-13 18:13:57 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-04-13 18:13:57 -0700
commit252ca4a148b0a210f2ba8ec817981aa9557c1ffe (patch)
tree0a0e23638f4dc1f54b73198035cd0ec0fc65d225
parente216a8e3d7312b97e9d0822aaab27247a29d341d (diff)
fix overflow detection in i64 uadd, and add testcase
-rw-r--r--src/library.js5
-rw-r--r--tests/cases/uadd_overflow_64_ta2.ll30
-rw-r--r--tests/cases/uadd_overflow_64_ta2.txt2
3 files changed, 35 insertions, 2 deletions
diff --git a/src/library.js b/src/library.js
index 2e0f3afb..f9864134 100644
--- a/src/library.js
+++ b/src/library.js
@@ -7482,9 +7482,10 @@ LibraryManager.library = {
var l = 0, h = 0, overflow = 0;
l = (a + c)>>>0;
h = (b + d)>>>0;
- if ((l>>>0) < (a>>>0)) { // iff we overflowed
+ if ((h>>>0) < (b>>>0)) overflow = 1;
+ if ((l>>>0) < (a>>>0)) {
h = (h+1)>>>0;
- overflow = 1;
+ if ((h>>>0) == 0) overflow = 1; // two possibilities to overflow here
}
{{{ makeStructuralReturn(['l|0', 'h', 'overflow'], true) }}};
},
diff --git a/tests/cases/uadd_overflow_64_ta2.ll b/tests/cases/uadd_overflow_64_ta2.ll
new file mode 100644
index 00000000..a4f3e40b
--- /dev/null
+++ b/tests/cases/uadd_overflow_64_ta2.ll
@@ -0,0 +1,30 @@
+; ModuleID = 'tests/hello_world.bc'
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
+target triple = "i386-pc-linux-gnu"
+
+@.str2 = private constant [11 x i8] c"*%llx,%d*\0A\00", align 1 ; [#uses=1]
+
+; [#uses=0]
+define i32 @main() {
+entry:
+ %uadd1 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 18446744073709551606, i64 9999)
+ %a0 = extractvalue { i64, i1 } %uadd1, 0
+ %a1 = extractvalue { i64, i1 } %uadd1, 1
+ %a2 = zext i1 %a1 to i32
+ call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([11 x i8]* @.str2, i32 0, i32 0), i64 %a0, i32 %a2) ; [#uses=0]
+
+ %uadd2 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 18446744073709, i64 9999)
+ %b0 = extractvalue { i64, i1 } %uadd2, 0
+ %b1 = extractvalue { i64, i1 } %uadd2, 1
+ %b2 = zext i1 %b1 to i32
+ call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([11 x i8]* @.str2, i32 0, i32 0), i64 %b0, i32 %b2) ; [#uses=0]
+
+ ret i32 1
+}
+
+; [#uses=1]
+declare i32 @printf(i8*, ...)
+
+declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32) nounwind readnone
+declare { i64, i1 } @llvm.uadd.with.overflow.i64(i64, i64) nounwind readnone
+
diff --git a/tests/cases/uadd_overflow_64_ta2.txt b/tests/cases/uadd_overflow_64_ta2.txt
new file mode 100644
index 00000000..f5711309
--- /dev/null
+++ b/tests/cases/uadd_overflow_64_ta2.txt
@@ -0,0 +1,2 @@
+*2705,1*
+*10c6f7a0dcfc,0*