blob: 5a0e13b7f674d0351dd767eb1a43ec82094c43fa [file] [log] [blame]
Mingyao Yang0304e182015-01-30 16:41:29 -08001/*
2* Copyright (C) 2015 The Android Open Source Project
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17public class Main {
18
19 // CHECK-START: int Main.sieve(int) BCE (before)
20 // CHECK: BoundsCheck
21 // CHECK: ArraySet
22 // CHECK: BoundsCheck
23 // CHECK: ArrayGet
24 // CHECK: BoundsCheck
25 // CHECK: ArraySet
26
27 // CHECK-START: int Main.sieve(int) BCE (after)
28 // CHECK-NOT: BoundsCheck
29 // CHECK: ArraySet
30 // CHECK-NOT: BoundsCheck
31 // CHECK: ArrayGet
32 // CHECK: BoundsCheck
33 // CHECK: ArraySet
34
35 static int sieve(int size) {
36 int primeCount = 0;
37 boolean[] flags = new boolean[size + 1];
38 for (int i = 1; i < size; i++) flags[i] = true; // Can eliminate.
39 for (int i = 2; i < size; i++) {
40 if (flags[i]) { // Can eliminate.
41 primeCount++;
42 for (int k = i + 1; k <= size; k += i)
43 flags[k - 1] = false; // Can't eliminate yet due to (k+i) may overflow.
44 }
45 }
46 return primeCount;
47 }
48
49 // CHECK-START: void Main.narrow(int[], int) BCE (before)
50 // CHECK: BoundsCheck
51 // CHECK: ArraySet
52 // CHECK: BoundsCheck
53 // CHECK: ArraySet
54 // CHECK: BoundsCheck
55 // CHECK: ArraySet
56
57 // CHECK-START: void Main.narrow(int[], int) BCE (after)
58 // CHECK-NOT: BoundsCheck
59 // CHECK: ArraySet
60 // CHECK-NOT: BoundsCheck
61 // CHECK: ArraySet
62 // CHECK: BoundsCheck
63 // CHECK: ArraySet
64
65 static void narrow(int array[], int offset) {
66 if (offset < 0) {
67 return;
68 }
69 if (offset < array.length) {
70 // offset is in range [0, array.length-1].
71 // Bounds check can be eliminated.
72 array[offset] = 1;
73
74 int biased_offset1 = offset + 1;
75 // biased_offset1 is in range [1, array.length].
76 if (biased_offset1 < array.length) {
77 // biased_offset1 is in range [1, array.length-1].
78 // Bounds check can be eliminated.
79 array[biased_offset1] = 1;
80 }
81
82 int biased_offset2 = offset + 0x70000000;
83 // biased_offset2 is in range [0x70000000, array.length-1+0x70000000].
84 // It may overflow and be negative.
85 if (biased_offset2 < array.length) {
86 // Even with this test, biased_offset2 can be negative so we can't
87 // eliminate this bounds check.
88 array[biased_offset2] = 1;
89 }
90 }
91 }
92
93 public static void main(String[] args) {
94 sieve(20);
95 }
96}