diff options
| author | 2019-06-24 17:45:36 +0530 | |
|---|---|---|
| committer | 2019-06-26 08:20:48 +0000 | |
| commit | 41249cd548d60d72b94532b00a261332451fd6f2 (patch) | |
| tree | 454392f0835f3fdd193a523d5c36f6f8fff30800 | |
| parent | 5be5260cfec2fc0a8c21ef1a08e7144523a8bfcd (diff) | |
Tests for Invariant Loops
Test: ./test.py --host
Change-Id: I6c2631af2c62a41b1811f646804d395bb0cefa35
Signed-off-by: anuvarsh <anuvarshini.bc@intel.com>
| -rw-r--r-- | test/2022-Invariantloops/expected.txt | 0 | ||||
| -rw-r--r-- | test/2022-Invariantloops/info.txt | 2 | ||||
| -rw-r--r-- | test/2022-Invariantloops/src/Main.java | 48 | ||||
| -rw-r--r-- | test/2023-InvariantLoops_typecast/expected.txt | 0 | ||||
| -rw-r--r-- | test/2023-InvariantLoops_typecast/info.txt | 2 | ||||
| -rw-r--r-- | test/2023-InvariantLoops_typecast/src/Main.java | 79 | ||||
| -rw-r--r-- | test/2024-InvariantNegativeLoop/expected.txt | 0 | ||||
| -rw-r--r-- | test/2024-InvariantNegativeLoop/info.txt | 1 | ||||
| -rw-r--r-- | test/2024-InvariantNegativeLoop/src/Main.java | 51 |
9 files changed, 183 insertions, 0 deletions
diff --git a/test/2022-Invariantloops/expected.txt b/test/2022-Invariantloops/expected.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/2022-Invariantloops/expected.txt diff --git a/test/2022-Invariantloops/info.txt b/test/2022-Invariantloops/info.txt new file mode 100644 index 0000000000..fd7589e1b4 --- /dev/null +++ b/test/2022-Invariantloops/info.txt @@ -0,0 +1,2 @@ +Test a loop with invariants. In this case, since the invariants gets used later in the loop constant calculation sinking doesnt work. +Hence loop cannot be removed. diff --git a/test/2022-Invariantloops/src/Main.java b/test/2022-Invariantloops/src/Main.java new file mode 100644 index 0000000000..b975ef6315 --- /dev/null +++ b/test/2022-Invariantloops/src/Main.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public class Main { + + public static void assertIntEquals(int expected, int result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + public int loop1() { + int used1 = 1; + int used2 = 2; + int used3 = 3; + int used4 = 4; + int invar1 = 15; + int invar2 = 25; + int invar3 = 35; + int invar4 = 45; + + for (int i = 0; i < 10000; i++) { + used1 += invar1 + invar2; + used2 -= used1 + invar2 - invar3; + used3 *= used2 + invar3 * invar4; + used4 /= used3 + invar1 * invar2 - invar3 + invar4; + } + assertIntEquals(used1 + used2 + used3 + used4, -1999709997); + return used1 + used2 + used3 + used4; + } + + public static void main(String[] args) { + int res = new Main().loop1(); + } +} diff --git a/test/2023-InvariantLoops_typecast/expected.txt b/test/2023-InvariantLoops_typecast/expected.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/2023-InvariantLoops_typecast/expected.txt diff --git a/test/2023-InvariantLoops_typecast/info.txt b/test/2023-InvariantLoops_typecast/info.txt new file mode 100644 index 0000000000..adb8b3e32a --- /dev/null +++ b/test/2023-InvariantLoops_typecast/info.txt @@ -0,0 +1,2 @@ +Test a loop with invariants, which should be hoisted. +In this case type casts are not sunk/hoisted. diff --git a/test/2023-InvariantLoops_typecast/src/Main.java b/test/2023-InvariantLoops_typecast/src/Main.java new file mode 100644 index 0000000000..eba1440586 --- /dev/null +++ b/test/2023-InvariantLoops_typecast/src/Main.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public class Main { + + public static void assertIntEquals(int expected, int result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + public static void assertLongEquals(long expected, long result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + public byte loop1() { + byte used1 = 1; + byte used2 = 2; + byte used3 = 3; + byte used4 = 4; + byte invar1 = 15; + byte invar2 = 25; + byte invar3 = 35; + byte invar4 = 45; + + + for (byte i = 0; i < 127; i++) { + used1 -= (byte)(invar1 + invar2); + used2 *= (byte)(invar2 - invar3); + used3 += (byte)(invar3 * invar4); + used4 /= (byte)(invar1 * invar2 - invar3 + invar4); + } + + assertIntEquals((byte)(used1 + used2 + used3 + used4), -123); + return (byte)(used1 + used2 + used3 + used4); + } + + public long loop2() { + double used1 = 1; + double used2 = 2; + double used3 = 3; + double used4 = 4; + double invar1 = 234234234234l; + double invar2 = 2523423423423424l; + double invar3 = 35234234234234234l; + double invar4 = 45234234234234234l; + + for (double i = 0; i < 10000; i++) { + used1 += invar1 + invar2; + used2 *= invar2 - invar3; + used3 -= invar3 * invar4; + used4 /= invar1 * invar2 - invar3 + invar4; + } + assertLongEquals(Double.doubleToLongBits(used1 + used2 + used3 + used4), + 9218868437227405312l); + return Double.doubleToLongBits(used1 + used2 + used3 + used4); + + } + + public static void main(String[] args) { + byte res = new Main().loop1(); + long res1 = new Main().loop2(); + } +} diff --git a/test/2024-InvariantNegativeLoop/expected.txt b/test/2024-InvariantNegativeLoop/expected.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/2024-InvariantNegativeLoop/expected.txt diff --git a/test/2024-InvariantNegativeLoop/info.txt b/test/2024-InvariantNegativeLoop/info.txt new file mode 100644 index 0000000000..7d7d05542b --- /dev/null +++ b/test/2024-InvariantNegativeLoop/info.txt @@ -0,0 +1 @@ +Test a loop with invariants. diff --git a/test/2024-InvariantNegativeLoop/src/Main.java b/test/2024-InvariantNegativeLoop/src/Main.java new file mode 100644 index 0000000000..5e729f826f --- /dev/null +++ b/test/2024-InvariantNegativeLoop/src/Main.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +public class Main { + + public static void assertFloatEquals(float expected, float result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + public int loop() { + float used1 = 1; + float used2 = 2; + float used3 = 3; + float used4 = 4; + float invar1 = 15; + float invar2 = 25; + float invar3 = 35; + float invar4 = 45; + float i = 0.5f; + + do { + used1 = invar1 + invar2; + used2 = invar2 - invar3; + used3 = invar3 * invar4; + used4 = invar1 * invar2 - invar3 + invar4; + i += 0.5f; + } while (i < 5000.25f); + assertFloatEquals(Float.floatToIntBits(used1 + used2 + used3 + used4), 1157152768); + return Float.floatToIntBits(used1 + used2 + used3 + used4); + } + + public static void main(String[] args) { + int res = new Main().loop(); + } +} |