summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Vladimir Marko <vmarko@google.com> 2025-01-17 11:52:54 +0000
committer VladimĂ­r Marko <vmarko@google.com> 2025-01-17 05:57:44 -0800
commit564ffff5b472f3a2dc38c2ab51a78f2d0e3f7ea8 (patch)
tree9efdcdeb41b9778a00b1fcd2256d09d1dea32646
parentf5cca5b0592fdb9ecc836dc9158ba5d63e0c1c05 (diff)
Optimizing: Fix `InsertInputAt()`.
Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Change-Id: I500faee42b02dbc72474e30fa2a3c0388ae86674
-rw-r--r--compiler/optimizing/nodes.cc4
-rw-r--r--compiler/optimizing/nodes_test.cc30
2 files changed, 33 insertions, 1 deletions
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index fc7d0a52ab..ece11435db 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -1541,12 +1541,14 @@ void HVariableInputSizeInstruction::AddInput(HInstruction* input) {
void HVariableInputSizeInstruction::InsertInputAt(size_t index, HInstruction* input) {
inputs_.insert(inputs_.begin() + index, HUserRecord<HInstruction*>(input));
- input->AddUseAt(this, index);
// Update indexes in use nodes of inputs that have been pushed further back by the insert().
for (size_t i = index + 1u, e = inputs_.size(); i < e; ++i) {
DCHECK_EQ(inputs_[i].GetUseNode()->GetIndex(), i - 1u);
inputs_[i].GetUseNode()->SetIndex(i);
}
+ // Add the use after updating the indexes. If the `input` is already used by `this`,
+ // the fixup after use insertion can use those indexes.
+ input->AddUseAt(this, index);
}
void HVariableInputSizeInstruction::RemoveInputAt(size_t index) {
diff --git a/compiler/optimizing/nodes_test.cc b/compiler/optimizing/nodes_test.cc
index 0302298b9c..5f71d053c6 100644
--- a/compiler/optimizing/nodes_test.cc
+++ b/compiler/optimizing/nodes_test.cc
@@ -197,6 +197,36 @@ TEST_F(NodeTest, AddInstruction) {
ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
}
+TEST_F(NodeTest, InsertDuplicateInstructionAt) {
+ HBasicBlock* ret = InitEntryMainExitGraphWithReturnVoid();
+ HInstruction* const0 = graph_->GetIntConstant(0);
+ HInstruction* const1 = graph_->GetIntConstant(1);
+ HInstruction* const2 = graph_->GetIntConstant(0);
+ HInstruction* const3 = graph_->GetIntConstant(1);
+
+ // We should be able to insert a duplicate input to `HPhi`s if we want to
+ // make a graph transformation that adds another predecessor to a block.
+
+ // This used to accidentally end up with correct use information but unexpectedly
+ // using the old `HUseListNode<>` for the new use and the new one for the old use.
+ HPhi* phi1 = MakePhi(ret, {const0, const1});
+ struct AccessProtected : HVariableInputSizeInstruction {
+ using HVariableInputSizeInstruction::InputRecordAt;
+ };
+ const HUseListNode<HInstruction*>* old_use_node_before =
+ std::addressof(*(phi1->*&AccessProtected::InputRecordAt)(1u).GetUseNode());
+ phi1->InsertInputAt(1u, const1); // Moves the old use from position 1 to position 2.
+ const HUseListNode<HInstruction*>* old_use_node_after =
+ std::addressof(*(phi1->*&AccessProtected::InputRecordAt)(2u).GetUseNode());
+ EXPECT_EQ(old_use_node_before, old_use_node_after);
+ EXPECT_EQ(1u, (phi1->*&AccessProtected::InputRecordAt)(1u).GetUseNode()->GetIndex());
+ EXPECT_EQ(2u, (phi1->*&AccessProtected::InputRecordAt)(2u).GetUseNode()->GetIndex());
+
+ // This used to hit a `DCHECK()`.
+ HPhi* phi2 = MakePhi(ret, {const2, const3, const3});
+ phi2->InsertInputAt(1u, const3);
+}
+
TEST_F(NodeTest, ParentEnvironment) {
HGraph* graph = CreateGraph();
HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);