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 30 var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 31 32 // Key is HParameter 33 function aoo(i) { 34 return a[i + 1]; 35 } 36 37 aoo(1); 38 aoo(-1); 39 %OptimizeFunctionOnNextCall(aoo); 40 aoo(-1); 41 42 43 // Key is HChange, used by either dehoised or non-dehoisted 44 function 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 54 boo(1); 55 boo(-1); 56 %OptimizeFunctionOnNextCall(boo); 57 boo(-1); 58 59 60 // Key is HMul(-i ==> i * (-1)) 61 function 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 70 coo(); 71 coo(); 72 %OptimizeFunctionOnNextCall(coo); 73 coo(); 74 75 76 // Key is HPhi, used only by dehoisted 77 function 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 } 84 doo(); 85 doo(); 86 %OptimizeFunctionOnNextCall(doo); 87 doo(); 88 89 // Key is HPhi, but used by both dehoisted and non-dehoisted 90 // sign extend is useless 91 function 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 } 100 eoo(); 101 eoo(); 102 %OptimizeFunctionOnNextCall(eoo); 103 eoo(); 104 105 106 107 // Key is HPhi, but used by either dehoisted or non-dehoisted 108 function 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 121 foo(); 122 foo(); 123 %OptimizeFunctionOnNextCall(foo); 124 foo(); 125 126 // Key is HPhi, but not induction variable 127 function goo(i) { 128 if (i > 0) { 129 i += 1; 130 } else { 131 i += -1; 132 } 133 134 return a[i + 3]; 135 } 136 goo(-1); 137 goo(-1); 138 %OptimizeFunctionOnNextCall(goo); 139 goo(-1); 140 141 // Key is return value of function 142 function index() { 143 return 1; 144 } 145 %NeverOptimizeFunction(index); 146 function hoo() { 147 return a[index() + 3]; 148 } 149 150 hoo(); 151 hoo(); 152 %OptimizeFunctionOnNextCall(hoo); 153 hoo(); 154 155 // Sign extension of key makes AssertZeroExtended fail in DoBoundsCheck 156 function ioo(i) { 157 return a[i] + a[i + 1]; 158 } 159 160 ioo(1); 161 ioo(1); 162 %OptimizeFunctionOnNextCall(ioo); 163 ioo(-1); 164