| OLD | NEW |
| (Empty) | |
| 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 (function() { |
| 31 // Test the correct placement of the simulates after HCompareGenericAndBranch: |
| 32 function Checker() { |
| 33 this.str = "1"; |
| 34 var toStringCalled = 0; |
| 35 var toStringExpected = 0; |
| 36 this.toString = function() { |
| 37 toStringCalled++; |
| 38 return this.str; |
| 39 }; |
| 40 this.check = function() { |
| 41 toStringExpected++; |
| 42 assertEquals(toStringExpected, toStringCalled); |
| 43 }; |
| 44 }; |
| 45 var left = new Checker(); |
| 46 var right = new Checker(); |
| 47 |
| 48 // This test compares a < b against x < y where |
| 49 // x/y are objects providing a/b as toString. In the end we |
| 50 // check if the observable side effects match our |
| 51 // expectations, thus we make sure that we deopted to a |
| 52 // simulate after the comparison was done. |
| 53 function test(a,b) { |
| 54 left.str = a; |
| 55 right.str = b; |
| 56 if (left >= right) { |
| 57 assertTrue(a >= b); |
| 58 } else { |
| 59 assertFalse(a >= b); |
| 60 } |
| 61 left.check(); |
| 62 right.check(); |
| 63 } |
| 64 |
| 65 test("ab","abc"); |
| 66 test("ab","a"); |
| 67 %OptimizeFunctionOnNextCall(test); |
| 68 test("a","ab"); |
| 69 test(1,"a"); |
| 70 test("a","ab"); |
| 71 %OptimizeFunctionOnNextCall(test); |
| 72 test("a","ab"); |
| 73 test("a",1); |
| 74 test("ab","a"); |
| 75 |
| 76 // Use generic compare in value, effect and test contexts |
| 77 function Checker2() { |
| 78 var valueOfCalled = 0; |
| 79 this.valueOf = function() { |
| 80 return valueOfCalled++; |
| 81 } |
| 82 this.valueOfCalled = function() { |
| 83 return valueOfCalled; |
| 84 } |
| 85 } |
| 86 |
| 87 var x = new Checker2(); |
| 88 var y = new Checker2(); |
| 89 if (x < y || y < x || x <= y) { |
| 90 assertEquals(3, x.valueOfCalled()); |
| 91 assertEquals(3, y.valueOfCalled()); |
| 92 assertEquals(1, (x < y) + (y < x) + (x <= y)) |
| 93 assertEquals(6, x.valueOfCalled()); |
| 94 assertEquals(6, y.valueOfCalled()); |
| 95 x < y; |
| 96 assertEquals(7, x.valueOfCalled()); |
| 97 assertEquals(7, y.valueOfCalled()); |
| 98 x < y; |
| 99 assertEquals(8, x.valueOfCalled()); |
| 100 assertEquals(8, y.valueOfCalled()); |
| 101 var res; |
| 102 if (x <= y) { |
| 103 res = 1+(x > {}); |
| 104 } else { |
| 105 assertTrue(false); |
| 106 res = y <= {}; |
| 107 } |
| 108 assertEquals(10, x.valueOfCalled()); |
| 109 assertEquals(9, y.valueOfCalled()); |
| 110 assertEquals(1, res); |
| 111 assertFalse(x < y); |
| 112 |
| 113 var tb = 0, fb = 0; |
| 114 var val = 0; |
| 115 for (var i = 1; i < 10; i++) { |
| 116 var res = 0; |
| 117 // uses x,y in control context |
| 118 if (x <= y) { |
| 119 res += val; |
| 120 assertTrue(x <= y); |
| 121 // adds 1 + 0, uses x in value context |
| 122 res += 1+(x > {}); |
| 123 tb++; |
| 124 assertEquals(fb, tb); |
| 125 } else { |
| 126 res += val; |
| 127 assertFalse(x < y); |
| 128 // adds 1, uses y in value context, increments 2 |
| 129 res += (y <= y); |
| 130 // use x in value context, increments x once to make it equal to y again |
| 131 x + 2; |
| 132 assertEquals(fb, tb); |
| 133 fb++; |
| 134 } |
| 135 assertEquals(11+(2*i)+tb+fb, x.valueOfCalled()); |
| 136 assertEquals(10+(2*i)+(2*fb), y.valueOfCalled()); |
| 137 assertEquals(1 + val, res); |
| 138 // Triggers deopt inside branch. |
| 139 if (i%5 == 0) val += 0.5; |
| 140 } |
| 141 } else { |
| 142 assertTrue(false); |
| 143 } |
| 144 |
| 145 function t(a,b) { return (b < a) - (a < b); }; |
| 146 function fun() { |
| 147 x = new Checker2(); |
| 148 y = new Checker2(); |
| 149 var tb = 0, fb = 0; |
| 150 var val = 0; |
| 151 for (var i = 1; i < 10; i++) { |
| 152 var res = 0; |
| 153 if ((x < y) + (y < x)) { |
| 154 res += val; |
| 155 res += x<0; |
| 156 fb++; |
| 157 } else { |
| 158 res += val; |
| 159 res += y<0; |
| 160 tb++; |
| 161 } |
| 162 assertEquals(0, res + 1 - res - 1); |
| 163 assertEquals((2*i)+fb, x.valueOfCalled()); |
| 164 assertEquals((2*i)+tb, y.valueOfCalled()); |
| 165 assertEquals(val, res); |
| 166 if (i%4 == 0) val += 0.5; |
| 167 } |
| 168 } |
| 169 |
| 170 fun(); |
| 171 %OptimizeFunctionOnNextCall(fun); |
| 172 fun(); |
| 173 |
| 174 var a = {valueOf: function(){this.conv++; return 1;}}; |
| 175 var b = {valueOf: function(){this.conv++; return 2;}}; |
| 176 |
| 177 a.conv = 0; |
| 178 b.conv = 0; |
| 179 |
| 180 function fun2(a,b,d1,d2) { |
| 181 var runs = 0; |
| 182 if ((a < b) + (a < b)) { |
| 183 if (d2) { d2 += 0.2; } |
| 184 runs++; |
| 185 } else { |
| 186 assertUnreachable(); |
| 187 } |
| 188 assertEquals(1, runs); |
| 189 if (a > b) { |
| 190 assertUnreachable(); |
| 191 } else { |
| 192 if (d1) { d1 += 0.2; } |
| 193 runs++; |
| 194 } |
| 195 assertEquals(2, runs); |
| 196 } |
| 197 |
| 198 fun2(a,b); |
| 199 fun2(a,b); |
| 200 |
| 201 %OptimizeFunctionOnNextCall(fun2); |
| 202 fun2(a,b); |
| 203 fun2(a,b); |
| 204 |
| 205 fun2(a,b,true); |
| 206 fun2(a,b); |
| 207 |
| 208 %OptimizeFunctionOnNextCall(fun2); |
| 209 fun2(a,b); |
| 210 fun2(a,b); |
| 211 |
| 212 fun2(a,b,false,true); |
| 213 fun2(a,b); |
| 214 |
| 215 assertEquals(30, a.conv); |
| 216 assertEquals(30, b.conv); |
| 217 |
| 218 b.valueOf = function(){ return {}; } |
| 219 try { |
| 220 fun2(a,b); |
| 221 } catch(e) { |
| 222 res = e.stack; |
| 223 } |
| 224 })(); |
| 225 |
| 226 |
| 227 // ------ Regression tests for value and control contexts: -------- |
| 228 |
| 229 (function() { |
| 230 var count = 0; |
| 231 var deopt = false; |
| 232 |
| 233 function f(x, y) { return x < y; }; |
| 234 |
| 235 function X() { }; |
| 236 X.prototype.toString = function () { |
| 237 if (deopt) { count++; %DeoptimizeFunction(f); } return 'b' |
| 238 }; |
| 239 |
| 240 f('a', new X()); |
| 241 %OptimizeFunctionOnNextCall(f); |
| 242 var result = f('a', new X()); |
| 243 assertEquals(result, true); |
| 244 |
| 245 deopt = true; |
| 246 result = f('a', new X()); |
| 247 assertEquals(result, true); |
| 248 assertEquals(count, 1); |
| 249 |
| 250 var count2 = 0; |
| 251 var deopt2 = false; |
| 252 |
| 253 function f2(x, y) { return x > y; }; |
| 254 |
| 255 function X2() { }; |
| 256 X2.prototype.toString = function () { |
| 257 if (deopt2) { count2++; %DeoptimizeFunction(f2); } return 'b' |
| 258 }; |
| 259 |
| 260 f2('a', new X()); |
| 261 %OptimizeFunctionOnNextCall(f2); |
| 262 f2('a', new X()); |
| 263 |
| 264 deopt2 = true; |
| 265 var result2 = f2('a', new X2()); |
| 266 assertEquals(result2, false); |
| 267 assertEquals(count2, 1); |
| 268 |
| 269 var count3 = 0; |
| 270 var deopt3 = false; |
| 271 |
| 272 function f3(x, y) { var res = {}; |
| 273 if(y < x) { res = 2; } else { res = 1; } |
| 274 return res; }; |
| 275 |
| 276 function X3() { }; |
| 277 X3.prototype.toString = function () { |
| 278 if (deopt3) { count3++; %DeoptimizeFunction(f3); } return 'b' |
| 279 }; |
| 280 |
| 281 f3('a', new X()); |
| 282 %OptimizeFunctionOnNextCall(f3); |
| 283 f3('a', new X()); |
| 284 |
| 285 deopt3 = true; |
| 286 var result3 = f3('a', new X3()); |
| 287 assertEquals(result3, 1); |
| 288 assertEquals(count3, 1); |
| 289 |
| 290 var count4 = 0; |
| 291 var deopt4 = false; |
| 292 |
| 293 function f4(x, y) { var res = {}; |
| 294 if(x < y) { res = 2; } else { res = 1; } |
| 295 return res; }; |
| 296 |
| 297 function X4() { }; |
| 298 X4.prototype.toString = function () { |
| 299 if (deopt4) { count4++; %DeoptimizeFunction(f4); } return 'b' |
| 300 }; |
| 301 |
| 302 f4('a', new X()); |
| 303 %OptimizeFunctionOnNextCall(f4); |
| 304 f4('a', new X()); |
| 305 |
| 306 deopt4 = true; |
| 307 var result4 = f4('a', new X4()); |
| 308 assertEquals(result4, 2); |
| 309 assertEquals(count4, 1); |
| 310 })(); |
| 311 |
| OLD | NEW |