Commit 743b8976 authored by danno's avatar danno Committed by Commit bot

[csa] Re-introduce automatic constant folding for IntPtrAdd and IntPtrSub

R=ishell@chromium.org
LOG=N

Review-Url: https://codereview.chromium.org/2608433003
Cr-Commit-Position: refs/heads/master@{#41977}
parent 81df56ae
......@@ -37,9 +37,9 @@ class StringBuiltinsAssembler : public CodeStubAssembler {
const int header = SeqOneByteString::kHeaderSize - kHeapObjectTag;
Node* offset = index;
if (encoding == String::TWO_BYTE_ENCODING) {
offset = IntPtrAddFoldConstants(offset, offset);
offset = IntPtrAdd(offset, offset);
}
offset = IntPtrAddFoldConstants(offset, IntPtrConstant(header));
offset = IntPtrAdd(offset, IntPtrConstant(header));
return offset;
}
......
......@@ -164,43 +164,6 @@ Node* CodeStubAssembler::IntPtrOrSmiConstant(int value, ParameterMode mode) {
}
}
Node* CodeStubAssembler::IntPtrAddFoldConstants(Node* left, Node* right) {
int32_t left_constant;
bool is_left_constant = ToInt32Constant(left, left_constant);
int32_t right_constant;
bool is_right_constant = ToInt32Constant(right, right_constant);
if (is_left_constant) {
if (is_right_constant) {
return IntPtrConstant(left_constant + right_constant);
}
if (left_constant == 0) {
return right;
}
} else if (is_right_constant) {
if (right_constant == 0) {
return left;
}
}
return IntPtrAdd(left, right);
}
Node* CodeStubAssembler::IntPtrSubFoldConstants(Node* left, Node* right) {
int32_t left_constant;
bool is_left_constant = ToInt32Constant(left, left_constant);
int32_t right_constant;
bool is_right_constant = ToInt32Constant(right, right_constant);
if (is_left_constant) {
if (is_right_constant) {
return IntPtrConstant(left_constant - right_constant);
}
} else if (is_right_constant) {
if (right_constant == 0) {
return left;
}
}
return IntPtrSub(left, right);
}
Node* CodeStubAssembler::IntPtrRoundUpToPowerOfTwo32(Node* value) {
Comment("IntPtrRoundUpToPowerOfTwo32");
CSA_ASSERT(this, UintPtrLessThanOrEqual(value, IntPtrConstant(0x80000000u)));
......@@ -1520,7 +1483,7 @@ Node* CodeStubAssembler::BuildAppendJSArray(ElementsKind kind, Node* context,
// Resize the capacity of the fixed array if it doesn't fit.
Label fits(this, &var_elements);
Node* first = arg_index.value();
Node* growth = IntPtrSubFoldConstants(args.GetLength(), first);
Node* growth = IntPtrSub(args.GetLength(), first);
Node* new_length =
IntPtrOrSmiAdd(WordToParameter(growth, mode), var_length.value(), mode);
GotoUnless(IntPtrOrSmiGreaterThanOrEqual(new_length, capacity, mode), &fits);
......@@ -1902,8 +1865,8 @@ Node* CodeStubAssembler::AllocateNameDictionary(Node* at_least_space_for) {
Node* length = EntryToIndex<NameDictionary>(capacity);
Node* store_size =
IntPtrAddFoldConstants(WordShl(length, IntPtrConstant(kPointerSizeLog2)),
IntPtrConstant(NameDictionary::kHeaderSize));
IntPtrAdd(WordShl(length, IntPtrConstant(kPointerSizeLog2)),
IntPtrConstant(NameDictionary::kHeaderSize));
Node* result = Allocate(store_size);
Comment("Initialize NameDictionary");
......@@ -1935,8 +1898,7 @@ Node* CodeStubAssembler::AllocateNameDictionary(Node* at_least_space_for) {
NameDictionary::kElementsStartIndex) -
kHeapObjectTag));
Node* end_address = IntPtrAdd(
result_word,
IntPtrSubFoldConstants(store_size, IntPtrConstant(kHeapObjectTag)));
result_word, IntPtrSub(store_size, IntPtrConstant(kHeapObjectTag)));
StoreFieldsNoWriteBarrier(start_address, end_address, filler);
return result;
}
......@@ -2309,7 +2271,7 @@ void CodeStubAssembler::CopyStringCharacters(Node* from_string, Node* to_string,
Node* to_offset =
ElementOffsetFromIndex(to_index, to_kind, mode, header_size);
Node* byte_count = ElementOffsetFromIndex(character_count, from_kind, mode);
Node* limit_offset = IntPtrAddFoldConstants(from_offset, byte_count);
Node* limit_offset = IntPtrAdd(from_offset, byte_count);
// Prepare the fast loop
MachineType type =
......@@ -5480,7 +5442,7 @@ Node* CodeStubAssembler::ElementOffsetFromIndex(Node* index_node,
: ((element_size_shift > 0)
? WordShl(index_node, IntPtrConstant(element_size_shift))
: WordShr(index_node, IntPtrConstant(-element_size_shift)));
return IntPtrAddFoldConstants(IntPtrConstant(base_size), shifted_index);
return IntPtrAdd(IntPtrConstant(base_size), shifted_index);
}
Node* CodeStubAssembler::LoadTypeFeedbackVectorForStub() {
......@@ -8219,7 +8181,7 @@ CodeStubArguments::CodeStubArguments(CodeStubAssembler* assembler, Node* argc)
Node* offset = assembler->ElementOffsetFromIndex(
argc_, FAST_ELEMENTS, CodeStubAssembler::INTPTR_PARAMETERS,
(StandardFrameConstants::kFixedSlotCountAboveFp - 1) * kPointerSize);
arguments_ = assembler_->IntPtrAddFoldConstants(fp_, offset);
arguments_ = assembler_->IntPtrAdd(fp_, offset);
}
Node* CodeStubArguments::GetReceiver() const {
......@@ -8230,8 +8192,8 @@ Node* CodeStubArguments::GetReceiver() const {
Node* CodeStubArguments::AtIndex(Node* index,
CodeStubAssembler::ParameterMode mode) const {
typedef compiler::Node Node;
Node* negated_index = assembler_->IntPtrSubFoldConstants(
assembler_->IntPtrOrSmiConstant(0, mode), index);
Node* negated_index =
assembler_->IntPtrSub(assembler_->IntPtrOrSmiConstant(0, mode), index);
Node* offset =
assembler_->ElementOffsetFromIndex(negated_index, FAST_ELEMENTS, mode, 0);
return assembler_->Load(MachineType::AnyTagged(), arguments_, offset);
......@@ -8254,10 +8216,10 @@ void CodeStubArguments::ForEach(
if (last == nullptr) {
last = argc_;
}
Node* start = assembler_->IntPtrSubFoldConstants(
Node* start = assembler_->IntPtrSub(
arguments_,
assembler_->ElementOffsetFromIndex(first, FAST_ELEMENTS, mode));
Node* end = assembler_->IntPtrSubFoldConstants(
Node* end = assembler_->IntPtrSub(
arguments_,
assembler_->ElementOffsetFromIndex(last, FAST_ELEMENTS, mode));
assembler_->BuildFastLoop(
......@@ -8271,8 +8233,7 @@ void CodeStubArguments::ForEach(
void CodeStubArguments::PopAndReturn(Node* value) {
assembler_->PopAndReturn(
assembler_->IntPtrAddFoldConstants(argc_, assembler_->IntPtrConstant(1)),
value);
assembler_->IntPtrAdd(argc_, assembler_->IntPtrConstant(1)), value);
}
Node* CodeStubAssembler::IsFastElementsKind(Node* elements_kind) {
......
......@@ -135,8 +135,6 @@ class V8_EXPORT_PRIVATE CodeStubAssembler : public compiler::CodeAssembler {
Node* IntPtrOrSmiConstant(int value, ParameterMode mode);
Node* IntPtrAddFoldConstants(Node* left, Node* right);
Node* IntPtrSubFoldConstants(Node* left, Node* right);
// Round the 32bits payload of the provided word up to the next power of two.
Node* IntPtrRoundUpToPowerOfTwo32(Node* value);
// Select the maximum of the two provided IntPtr values.
......
......@@ -232,6 +232,10 @@ bool CodeAssembler::ToSmiConstant(Node* node, Smi*& out_value) {
}
bool CodeAssembler::ToIntPtrConstant(Node* node, intptr_t& out_value) {
if (node->opcode() == IrOpcode::kBitcastWordToTaggedSigned ||
node->opcode() == IrOpcode::kBitcastWordToTagged) {
node = node->InputAt(0);
}
IntPtrMatcher m(node);
if (m.HasValue()) out_value = m.Value();
return m.HasValue();
......@@ -292,6 +296,43 @@ Node* CodeAssembler::LoadStackPointer() {
CODE_ASSEMBLER_BINARY_OP_LIST(DEFINE_CODE_ASSEMBLER_BINARY_OP)
#undef DEFINE_CODE_ASSEMBLER_BINARY_OP
Node* CodeAssembler::IntPtrAdd(Node* left, Node* right) {
intptr_t left_constant;
bool is_left_constant = ToIntPtrConstant(left, left_constant);
intptr_t right_constant;
bool is_right_constant = ToIntPtrConstant(right, right_constant);
if (is_left_constant) {
if (is_right_constant) {
return IntPtrConstant(left_constant + right_constant);
}
if (left_constant == 0) {
return right;
}
} else if (is_right_constant) {
if (right_constant == 0) {
return left;
}
}
return raw_assembler()->IntPtrAdd(left, right);
}
Node* CodeAssembler::IntPtrSub(Node* left, Node* right) {
intptr_t left_constant;
bool is_left_constant = ToIntPtrConstant(left, left_constant);
intptr_t right_constant;
bool is_right_constant = ToIntPtrConstant(right, right_constant);
if (is_left_constant) {
if (is_right_constant) {
return IntPtrConstant(left_constant - right_constant);
}
} else if (is_right_constant) {
if (right_constant == 0) {
return left;
}
}
return raw_assembler()->IntPtrSub(left, right);
}
Node* CodeAssembler::WordShl(Node* value, int shift) {
return (shift != 0) ? raw_assembler()->WordShl(value, IntPtrConstant(shift))
: value;
......
......@@ -87,9 +87,7 @@ typedef std::function<void()> CodeAssemblerCallback;
V(Float64Pow) \
V(Float64InsertLowWord32) \
V(Float64InsertHighWord32) \
V(IntPtrAdd) \
V(IntPtrAddWithOverflow) \
V(IntPtrSub) \
V(IntPtrSubWithOverflow) \
V(IntPtrMul) \
V(Int32Add) \
......@@ -277,6 +275,9 @@ class V8_EXPORT_PRIVATE CodeAssembler {
CODE_ASSEMBLER_BINARY_OP_LIST(DECLARE_CODE_ASSEMBLER_BINARY_OP)
#undef DECLARE_CODE_ASSEMBLER_BINARY_OP
Node* IntPtrAdd(Node* left, Node* right);
Node* IntPtrSub(Node* left, Node* right);
Node* WordShl(Node* value, int shift);
Node* WordShr(Node* value, int shift);
Node* Word32Shr(Node* value, int shift);
......
......@@ -563,7 +563,7 @@ TARGET_TEST_F(InterpreterAssemblerTest, IntPtrAdd) {
TRACED_FOREACH(interpreter::Bytecode, bytecode, kBytecodes) {
InterpreterAssemblerTestState state(this, bytecode);
InterpreterAssemblerForTest m(&state, bytecode);
Node* a = m.Int32Constant(0);
Node* a = m.Parameter(0);
Node* b = m.Int32Constant(1);
Node* add = m.IntPtrAdd(a, b);
EXPECT_THAT(add, IsIntPtrAdd(a, b));
......@@ -574,7 +574,7 @@ TARGET_TEST_F(InterpreterAssemblerTest, IntPtrSub) {
TRACED_FOREACH(interpreter::Bytecode, bytecode, kBytecodes) {
InterpreterAssemblerTestState state(this, bytecode);
InterpreterAssemblerForTest m(&state, bytecode);
Node* a = m.Int32Constant(0);
Node* a = m.Parameter(0);
Node* b = m.Int32Constant(1);
Node* add = m.IntPtrSub(a, b);
EXPECT_THAT(add, IsIntPtrSub(a, b));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment