1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29
30var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
31
32// Key is HParameter
33function aoo(i) {
34  return a[i + 1];
35}
36
37aoo(1);
38aoo(-1);
39%OptimizeFunctionOnNextCall(aoo);
40aoo(-1);
41
42
43// Key is HChange, used by either dehoised or non-dehoisted
44function boo(i) {
45  var ret = 0;
46  if (i < 0) {
47    ret = a[i + 10];
48  } else {
49    ret = a[i];
50  }
51  return ret;
52}
53
54boo(1);
55boo(-1);
56%OptimizeFunctionOnNextCall(boo);
57boo(-1);
58
59
60// Key is HMul(-i ==> i * (-1))
61function coo() {
62  var ret = 0;
63  for (var i = 4; i > 0; i -= 1) {
64    ret += a[-i + 4];  // dehoisted
65  }
66
67  return ret;
68}
69
70coo();
71coo();
72%OptimizeFunctionOnNextCall(coo);
73coo();
74
75
76// Key is HPhi, used only by dehoisted
77function doo() {
78  var ret = 0;
79  for (var i = 0; i < 5; i += 1) {
80    ret += a[i + 1];  // dehoisted
81  }
82  return ret;
83}
84doo();
85doo();
86%OptimizeFunctionOnNextCall(doo);
87doo();
88
89// Key is HPhi, but used by both dehoisted and non-dehoisted
90// sign extend is useless
91function eoo() {
92  var ret = 0;
93  for (var i = 0; i < 5; i += 1) {
94    ret += a[i];      // non-dehoisted
95    ret += a[i + 1];  // dehoisted
96  }
97
98  return ret;
99}
100eoo();
101eoo();
102%OptimizeFunctionOnNextCall(eoo);
103eoo();
104
105
106
107// Key is HPhi, but used by either dehoisted or non-dehoisted
108function foo() {
109  var ret = 0;
110  for (var i = -3; i < 4; i += 1) {
111    if (i < 0) {
112      ret += a[i + 4];  // dehoisted
113    } else {
114      ret += a[i];      // non-dehoisted
115    }
116  }
117
118  return ret;
119}
120
121foo();
122foo();
123%OptimizeFunctionOnNextCall(foo);
124foo();
125
126// Key is HPhi, but not induction variable
127function goo(i) {
128  if (i > 0) {
129    i += 1;
130  } else {
131    i += -1;
132  }
133
134  return a[i + 3];
135}
136goo(-1);
137goo(-1);
138%OptimizeFunctionOnNextCall(goo);
139goo(-1);
140
141// Key is return value of function
142function index() {
143  return 1;
144}
145%NeverOptimizeFunction(index);
146function hoo() {
147   return a[index() + 3];
148}
149
150hoo();
151hoo();
152%OptimizeFunctionOnNextCall(hoo);
153hoo();
154
155// Sign extension of key makes AssertZeroExtended fail in DoBoundsCheck
156function ioo(i) {
157  return a[i] + a[i + 1];
158}
159
160ioo(1);
161ioo(1);
162%OptimizeFunctionOnNextCall(ioo);
163ioo(-1);
164