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 79734 : static void DehoistArrayIndex(ArrayInstructionInterface* array_operation) {
14 79734 : HValue* index = array_operation->GetKey()->ActualValue();
15 150221 : if (!index->representation().IsSmiOrInteger32()) return;
16 148396 : if (!index->IsAdd() && !index->IsSub()) return;
17 :
18 24232 : HConstant* constant;
19 : HValue* subexpression;
20 : HBinaryOperation* binary_operation = HBinaryOperation::cast(index);
21 15375 : if (binary_operation->left()->IsConstant() && index->IsAdd()) {
22 : subexpression = binary_operation->right();
23 : constant = HConstant::cast(binary_operation->left());
24 11429 : } 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 12116 : if (!constant->HasInteger32Value()) return;
32 : v8::base::internal::CheckedNumeric<int32_t> checked_value =
33 : constant->Integer32Value();
34 33481 : int32_t sign = binary_operation->IsSub() ? -1 : 1;
35 12116 : checked_value = checked_value * sign;
36 :
37 : // Multiply value by elements size, bailing out on overflow.
38 : int32_t elements_kind_size =
39 12116 : 1 << ElementsKindToShiftSize(array_operation->elements_kind());
40 12116 : checked_value = checked_value * elements_kind_size;
41 12116 : if (!checked_value.IsValid()) return;
42 12102 : int32_t value = checked_value.ValueOrDie();
43 12102 : if (value < 0) return;
44 :
45 : // Ensure that the array operation can add value to existing base offset
46 : // without overflowing.
47 9265 : if (!array_operation->TryIncreaseBaseOffset(value)) return;
48 :
49 9249 : array_operation->SetKey(subexpression);
50 9249 : if (binary_operation->HasNoUses()) {
51 6958 : binary_operation->DeleteAndReplaceWith(NULL);
52 : }
53 :
54 9249 : array_operation->SetDehoisted(true);
55 : }
56 :
57 :
58 283728 : void HDehoistIndexComputationsPhase::Run() {
59 283728 : const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
60 9590814 : for (int i = 0; i < blocks->length(); ++i) {
61 39029300 : for (HInstructionIterator it(blocks->at(i)); !it.Done(); it.Advance()) {
62 : HInstruction* instr = it.Current();
63 50421045 : if (instr->IsLoadKeyed()) {
64 51622 : DehoistArrayIndex(HLoadKeyed::cast(instr));
65 25158885 : } else if (instr->IsStoreKeyed()) {
66 28114 : DehoistArrayIndex(HStoreKeyed::cast(instr));
67 : }
68 : }
69 : }
70 283730 : }
71 :
72 : } // namespace internal
73 : } // namespace v8
|