Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Unified Diff: test/mjsunit/compare-generic.js

Issue 24366004: Split HCompareGeneric in a test and a branch part. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/ia32/lithium-codegen-ia32.cc ('K') | « src/x64/lithium-x64.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/compare-generic.js
diff --git a/test/mjsunit/compare-generic.js b/test/mjsunit/compare-generic.js
new file mode 100644
index 0000000000000000000000000000000000000000..199c523059f3b2ffbe5ae75f672ca98f5cff0e51
--- /dev/null
+++ b/test/mjsunit/compare-generic.js
@@ -0,0 +1,311 @@
+// Copyright 2013 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Flags: --allow-natives-syntax
+
+(function() {
+ // Test the correct placement of the simulates after HCompareGenericAndBranch:
+ function Checker() {
+ this.str = "1";
+ var toStringCalled = 0;
+ var toStringExpected = 0;
+ this.toString = function() {
+ toStringCalled++;
+ return this.str;
+ };
+ this.check = function() {
+ toStringExpected++;
+ assertEquals(toStringExpected, toStringCalled);
+ };
+ };
+ var left = new Checker();
+ var right = new Checker();
+
+ // This test compares a < b against x < y where
+ // x/y are objects providing a/b as toString. In the end we
+ // check if the observable side effects match our
+ // expectations, thus we make sure that we deopted to a
+ // simulate after the comparison was done.
+ function test(a,b) {
+ left.str = a;
+ right.str = b;
+ if (left >= right) {
+ assertTrue(a >= b);
+ } else {
+ assertFalse(a >= b);
+ }
+ left.check();
+ right.check();
+ }
+
+ test("ab","abc");
+ test("ab","a");
+ %OptimizeFunctionOnNextCall(test);
+ test("a","ab");
+ test(1,"a");
+ test("a","ab");
+ %OptimizeFunctionOnNextCall(test);
+ test("a","ab");
+ test("a",1);
+ test("ab","a");
+
+ // Use generic compare in value, effect and test contexts
+ function Checker2() {
+ var valueOfCalled = 0;
+ this.valueOf = function() {
+ return valueOfCalled++;
+ }
+ this.valueOfCalled = function() {
+ return valueOfCalled;
+ }
+ }
+
+ var x = new Checker2();
+ var y = new Checker2();
+ if (x < y || y < x || x <= y) {
+ assertEquals(3, x.valueOfCalled());
+ assertEquals(3, y.valueOfCalled());
+ assertEquals(1, (x < y) + (y < x) + (x <= y))
+ assertEquals(6, x.valueOfCalled());
+ assertEquals(6, y.valueOfCalled());
+ x < y;
+ assertEquals(7, x.valueOfCalled());
+ assertEquals(7, y.valueOfCalled());
+ x < y;
+ assertEquals(8, x.valueOfCalled());
+ assertEquals(8, y.valueOfCalled());
+ var res;
+ if (x <= y) {
+ res = 1+(x > {});
+ } else {
+ assertTrue(false);
+ res = y <= {};
+ }
+ assertEquals(10, x.valueOfCalled());
+ assertEquals(9, y.valueOfCalled());
+ assertEquals(1, res);
+ assertFalse(x < y);
+
+ var tb = 0, fb = 0;
+ var val = 0;
+ for (var i = 1; i < 10; i++) {
+ var res = 0;
+ // uses x,y in control context
+ if (x <= y) {
+ res += val;
+ assertTrue(x <= y);
+ // adds 1 + 0, uses x in value context
+ res += 1+(x > {});
+ tb++;
+ assertEquals(fb, tb);
+ } else {
+ res += val;
+ assertFalse(x < y);
+ // adds 1, uses y in value context, increments 2
+ res += (y <= y);
+ // use x in value context, increments x once to make it equal to y again
+ x + 2;
+ assertEquals(fb, tb);
+ fb++;
+ }
+ assertEquals(11+(2*i)+tb+fb, x.valueOfCalled());
+ assertEquals(10+(2*i)+(2*fb), y.valueOfCalled());
+ assertEquals(1 + val, res);
+ // Triggers deopt inside branch.
+ if (i%5 == 0) val += 0.5;
+ }
+ } else {
+ assertTrue(false);
+ }
+
+ function t(a,b) { return (b < a) - (a < b); };
+ function fun() {
+ x = new Checker2();
+ y = new Checker2();
+ var tb = 0, fb = 0;
+ var val = 0;
+ for (var i = 1; i < 10; i++) {
+ var res = 0;
+ if ((x < y) + (y < x)) {
+ res += val;
+ res += x<0;
+ fb++;
+ } else {
+ res += val;
+ res += y<0;
+ tb++;
+ }
+ assertEquals(0, res + 1 - res - 1);
+ assertEquals((2*i)+fb, x.valueOfCalled());
+ assertEquals((2*i)+tb, y.valueOfCalled());
+ assertEquals(val, res);
+ if (i%4 == 0) val += 0.5;
+ }
+ }
+
+ fun();
+ %OptimizeFunctionOnNextCall(fun);
+ fun();
+
+ var a = {valueOf: function(){this.conv++; return 1;}};
+ var b = {valueOf: function(){this.conv++; return 2;}};
+
+ a.conv = 0;
+ b.conv = 0;
+
+ function fun2(a,b,d1,d2) {
+ var runs = 0;
+ if ((a < b) + (a < b)) {
+ if (d2) { d2 += 0.2; }
+ runs++;
+ } else {
+ assertUnreachable();
+ }
+ assertEquals(1, runs);
+ if (a > b) {
+ assertUnreachable();
+ } else {
+ if (d1) { d1 += 0.2; }
+ runs++;
+ }
+ assertEquals(2, runs);
+ }
+
+ fun2(a,b);
+ fun2(a,b);
+
+ %OptimizeFunctionOnNextCall(fun2);
+ fun2(a,b);
+ fun2(a,b);
+
+ fun2(a,b,true);
+ fun2(a,b);
+
+ %OptimizeFunctionOnNextCall(fun2);
+ fun2(a,b);
+ fun2(a,b);
+
+ fun2(a,b,false,true);
+ fun2(a,b);
+
+ assertEquals(30, a.conv);
+ assertEquals(30, b.conv);
+
+ b.valueOf = function(){ return {}; }
+ try {
+ fun2(a,b);
+ } catch(e) {
+ res = e.stack;
+ }
+})();
+
+
+// ------ Regression tests for value and control contexts: --------
+
+(function() {
+ var count = 0;
+ var deopt = false;
+
+ function f(x, y) { return x < y; };
+
+ function X() { };
+ X.prototype.toString = function () {
+ if (deopt) { count++; %DeoptimizeFunction(f); } return 'b'
+ };
+
+ f('a', new X());
+ %OptimizeFunctionOnNextCall(f);
+ var result = f('a', new X());
+ assertEquals(result, true);
+
+ deopt = true;
+ result = f('a', new X());
+ assertEquals(result, true);
+ assertEquals(count, 1);
+
+ var count2 = 0;
+ var deopt2 = false;
+
+ function f2(x, y) { return x > y; };
+
+ function X2() { };
+ X2.prototype.toString = function () {
+ if (deopt2) { count2++; %DeoptimizeFunction(f2); } return 'b'
+ };
+
+ f2('a', new X());
+ %OptimizeFunctionOnNextCall(f2);
+ f2('a', new X());
+
+ deopt2 = true;
+ var result2 = f2('a', new X2());
+ assertEquals(result2, false);
+ assertEquals(count2, 1);
+
+ var count3 = 0;
+ var deopt3 = false;
+
+ function f3(x, y) { var res = {};
+ if(y < x) { res = 2; } else { res = 1; }
+ return res; };
+
+ function X3() { };
+ X3.prototype.toString = function () {
+ if (deopt3) { count3++; %DeoptimizeFunction(f3); } return 'b'
+ };
+
+ f3('a', new X());
+ %OptimizeFunctionOnNextCall(f3);
+ f3('a', new X());
+
+ deopt3 = true;
+ var result3 = f3('a', new X3());
+ assertEquals(result3, 1);
+ assertEquals(count3, 1);
+
+ var count4 = 0;
+ var deopt4 = false;
+
+ function f4(x, y) { var res = {};
+ if(x < y) { res = 2; } else { res = 1; }
+ return res; };
+
+ function X4() { };
+ X4.prototype.toString = function () {
+ if (deopt4) { count4++; %DeoptimizeFunction(f4); } return 'b'
+ };
+
+ f4('a', new X());
+ %OptimizeFunctionOnNextCall(f4);
+ f4('a', new X());
+
+ deopt4 = true;
+ var result4 = f4('a', new X4());
+ assertEquals(result4, 2);
+ assertEquals(count4, 1);
+})();
+
« src/ia32/lithium-codegen-ia32.cc ('K') | « src/x64/lithium-x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698