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 --load-elimination 29 30 // Test local load elimination of redundant loads and stores. 31 32 function B(x, y) { 33 this.x = x; 34 this.y = y; 35 return this; 36 } 37 38 function C() { 39 } 40 41 function test_load() { 42 var a = new B(1, 2); 43 return a.x + a.x + a.x + a.x; 44 } 45 46 47 function test_load_from_different_contexts() { 48 var r = 1; 49 this.f = function() { 50 var fr = r; 51 this.g = function(flag) { 52 var gr; 53 if (flag) { 54 gr = r; 55 } else { 56 gr = r; 57 } 58 return gr + r + fr; 59 }; 60 }; 61 this.f(); 62 return this.g(true); 63 } 64 65 66 function test_store_load() { 67 var a = new B(1, 2); 68 a.x = 4; 69 var f = a.x; 70 a.x = 5; 71 var g = a.x; 72 a.x = 6; 73 var h = a.x; 74 a.x = 7; 75 return f + g + h + a.x; 76 } 77 78 function test_nonaliasing_store1() { 79 var a = new B(2, 3), b = new B(3, 4); 80 b.x = 4; 81 var f = a.x; 82 b.x = 5; 83 var g = a.x; 84 b.x = 6; 85 var h = a.x; 86 b.x = 7; 87 return f + g + h + a.x; 88 } 89 90 function test_transitioning_store1() { 91 var a = new B(2, 3); 92 var f = a.x, g = a.y; 93 var b = new B(3, 4); 94 return a.x + a.y; 95 } 96 97 function test_transitioning_store2() { 98 var b = new C(); 99 var a = new B(-1, 5); 100 var f = a.x, g = a.y; 101 b.x = 9; 102 b.y = 11; 103 return a.x + a.y; 104 } 105 106 var false_v = false; 107 function test_transitioning_store3() { 108 var o = new C(); 109 var v = o; 110 if (false_v) v = 0; 111 v.x = 20; 112 return o.x; 113 } 114 115 function killall() { 116 try { } catch(e) { } 117 } 118 119 %NeverOptimizeFunction(killall); 120 121 function test_store_load_kill() { 122 var a = new B(1, 2); 123 a.x = 4; 124 var f = a.x; 125 a.x = 5; 126 var g = a.x; 127 killall(); 128 a.x = 6; 129 var h = a.x; 130 a.x = 7; 131 return f + g + h + a.x; 132 } 133 134 function test_store_store() { 135 var a = new B(6, 7); 136 a.x = 7; 137 a.x = 7; 138 a.x = 7; 139 a.x = 7; 140 return a.x; 141 } 142 143 function test(x, f) { 144 assertEquals(x, f()); 145 assertEquals(x, f()); 146 %OptimizeFunctionOnNextCall(f); 147 assertEquals(x, f()); 148 } 149 150 test(4, test_load); 151 test(3, new test_load_from_different_contexts().g); 152 test(22, test_store_load); 153 test(8, test_nonaliasing_store1); 154 test(5, test_transitioning_store1); 155 test(4, test_transitioning_store2); 156 test(20, test_transitioning_store3); 157 test(22, test_store_load_kill); 158 test(7, test_store_store); 159