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 #include "hydrogen-dehoist.h" 29 30 namespace v8 { 31 namespace internal { 32 33 static void DehoistArrayIndex(ArrayInstructionInterface* array_operation) { 34 HValue* index = array_operation->GetKey()->ActualValue(); 35 if (!index->representation().IsSmiOrInteger32()) return; 36 if (!index->IsAdd() && !index->IsSub()) return; 37 38 HConstant* constant; 39 HValue* subexpression; 40 HBinaryOperation* binary_operation = HBinaryOperation::cast(index); 41 if (binary_operation->left()->IsConstant() && index->IsAdd()) { 42 subexpression = binary_operation->right(); 43 constant = HConstant::cast(binary_operation->left()); 44 } else if (binary_operation->right()->IsConstant()) { 45 subexpression = binary_operation->left(); 46 constant = HConstant::cast(binary_operation->right()); 47 } else { 48 return; 49 } 50 51 if (!constant->HasInteger32Value()) return; 52 int32_t sign = binary_operation->IsSub() ? -1 : 1; 53 int32_t value = constant->Integer32Value() * sign; 54 // We limit offset values to 30 bits because we want to avoid the risk of 55 // overflows when the offset is added to the object header size. 56 if (value >= 1 << array_operation->MaxIndexOffsetBits() || value < 0) return; 57 array_operation->SetKey(subexpression); 58 if (binary_operation->HasNoUses()) { 59 binary_operation->DeleteAndReplaceWith(NULL); 60 } 61 array_operation->SetIndexOffset(static_cast<uint32_t>(value)); 62 array_operation->SetDehoisted(true); 63 } 64 65 66 void HDehoistIndexComputationsPhase::Run() { 67 const ZoneList<HBasicBlock*>* blocks(graph()->blocks()); 68 for (int i = 0; i < blocks->length(); ++i) { 69 for (HInstructionIterator it(blocks->at(i)); !it.Done(); it.Advance()) { 70 HInstruction* instr = it.Current(); 71 if (instr->IsLoadKeyed()) { 72 DehoistArrayIndex(HLoadKeyed::cast(instr)); 73 } else if (instr->IsStoreKeyed()) { 74 DehoistArrayIndex(HStoreKeyed::cast(instr)); 75 } 76 } 77 } 78 } 79 80 } } // namespace v8::internal 81