Line data Source code
1 : // Copyright 2013 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/crankshaft/hydrogen-dehoist.h"
6 :
7 : #include "src/base/safe_math.h"
8 : #include "src/objects-inl.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 :
13 81172 : static void DehoistArrayIndex(ArrayInstructionInterface* array_operation) {
14 81172 : HValue* index = array_operation->GetKey()->ActualValue();
15 153079 : if (!index->representation().IsSmiOrInteger32()) return;
16 151220 : if (!index->IsAdd() && !index->IsSub()) return;
17 :
18 24326 : HConstant* constant;
19 : HValue* subexpression;
20 : HBinaryOperation* binary_operation = HBinaryOperation::cast(index);
21 15448 : if (binary_operation->left()->IsConstant() && index->IsAdd()) {
22 : subexpression = binary_operation->right();
23 : constant = HConstant::cast(binary_operation->left());
24 11482 : } else if (binary_operation->right()->IsConstant()) {
25 : subexpression = binary_operation->left();
26 : constant = HConstant::cast(binary_operation->right());
27 : } else {
28 : return;
29 : }
30 :
31 12163 : if (!constant->HasInteger32Value()) return;
32 : v8::base::internal::CheckedNumeric<int32_t> checked_value =
33 : constant->Integer32Value();
34 33598 : int32_t sign = binary_operation->IsSub() ? -1 : 1;
35 12163 : checked_value = checked_value * sign;
36 :
37 : // Multiply value by elements size, bailing out on overflow.
38 : int32_t elements_kind_size =
39 12163 : 1 << ElementsKindToShiftSize(array_operation->elements_kind());
40 12163 : checked_value = checked_value * elements_kind_size;
41 12163 : if (!checked_value.IsValid()) return;
42 12149 : int32_t value = checked_value.ValueOrDie();
43 12149 : if (value < 0) return;
44 :
45 : // Ensure that the array operation can add value to existing base offset
46 : // without overflowing.
47 9288 : if (!array_operation->TryIncreaseBaseOffset(value)) return;
48 :
49 9272 : array_operation->SetKey(subexpression);
50 9272 : if (binary_operation->HasNoUses()) {
51 6959 : binary_operation->DeleteAndReplaceWith(NULL);
52 : }
53 :
54 9272 : array_operation->SetDehoisted(true);
55 : }
56 :
57 :
58 283196 : void HDehoistIndexComputationsPhase::Run() {
59 283196 : const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
60 9553508 : for (int i = 0; i < blocks->length(); ++i) {
61 38902370 : for (HInstructionIterator it(blocks->at(i)); !it.Done(); it.Advance()) {
62 : HInstruction* instr = it.Current();
63 50276952 : if (instr->IsLoadKeyed()) {
64 52284 : DehoistArrayIndex(HLoadKeyed::cast(instr));
65 25086155 : } else if (instr->IsStoreKeyed()) {
66 28891 : DehoistArrayIndex(HStoreKeyed::cast(instr));
67 : }
68 : }
69 : }
70 283196 : }
71 :
72 : } // namespace internal
73 : } // namespace v8
|