summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/optimizing/instruction_simplifier.cc15
-rw-r--r--test/2252-rem-optimization-dividend-divisor/expected-stderr.txt0
-rw-r--r--test/2252-rem-optimization-dividend-divisor/expected-stdout.txt0
-rw-r--r--test/2252-rem-optimization-dividend-divisor/info.txt2
-rw-r--r--test/2252-rem-optimization-dividend-divisor/src/Main.java38
5 files changed, 49 insertions, 6 deletions
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index a2e9f69933..86b9dbab76 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -1860,13 +1860,16 @@ void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
// Search HDiv having the specified dividend and divisor which is in the specified basic block.
// Return nullptr if nothing has been found.
-static HInstruction* FindDivWithInputsInBasicBlock(HInstruction* dividend,
- HInstruction* divisor,
- HBasicBlock* basic_block) {
+static HDiv* FindDivWithInputsInBasicBlock(HInstruction* dividend,
+ HInstruction* divisor,
+ HBasicBlock* basic_block) {
for (const HUseListNode<HInstruction*>& use : dividend->GetUses()) {
HInstruction* user = use.GetUser();
- if (user->GetBlock() == basic_block && user->IsDiv() && user->InputAt(1) == divisor) {
- return user;
+ if (user->GetBlock() == basic_block &&
+ user->IsDiv() &&
+ user->InputAt(0) == dividend &&
+ user->InputAt(1) == divisor) {
+ return user->AsDiv();
}
}
return nullptr;
@@ -1900,7 +1903,7 @@ void InstructionSimplifierVisitor::TryToReuseDiv(HRem* rem) {
}
}
- HInstruction* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block);
+ HDiv* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block);
if (quotient == nullptr) {
return;
}
diff --git a/test/2252-rem-optimization-dividend-divisor/expected-stderr.txt b/test/2252-rem-optimization-dividend-divisor/expected-stderr.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/2252-rem-optimization-dividend-divisor/expected-stderr.txt
diff --git a/test/2252-rem-optimization-dividend-divisor/expected-stdout.txt b/test/2252-rem-optimization-dividend-divisor/expected-stdout.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/2252-rem-optimization-dividend-divisor/expected-stdout.txt
diff --git a/test/2252-rem-optimization-dividend-divisor/info.txt b/test/2252-rem-optimization-dividend-divisor/info.txt
new file mode 100644
index 0000000000..57e8b2e491
--- /dev/null
+++ b/test/2252-rem-optimization-dividend-divisor/info.txt
@@ -0,0 +1,2 @@
+Test checking that FindDivWithInputsInBasicBlock works correctly
+if the dividend equals the divisor.
diff --git a/test/2252-rem-optimization-dividend-divisor/src/Main.java b/test/2252-rem-optimization-dividend-divisor/src/Main.java
new file mode 100644
index 0000000000..1e1a674e2f
--- /dev/null
+++ b/test/2252-rem-optimization-dividend-divisor/src/Main.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Main {
+ public static void main(String[] args) {
+ $noinline$assertEquals(0, $noinline$testRemCaller());
+ }
+
+ public static void $noinline$assertEquals(int expected, int result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ private static int $noinline$testRemCaller() {
+ return $inline$remMethod(50);
+ }
+
+ private static int $inline$remMethod(int param) {
+ // We were replacing this Rem with the div below when both the dividend and the
+ // divisor were the same. We shouldn't do that since we didn't find a Div(50, 50).
+ int result = param % 50;
+ return result / 50;
+ }
+}