summaryrefslogtreecommitdiff
path: root/compiler/optimizing/loop_optimization.cc
diff options
context:
space:
mode:
author Santiago Aboy Solanes <solanes@google.com> 2024-05-24 16:36:52 +0100
committer Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-06-21 15:27:19 +0000
commit08b60ea29684c526f7bfb4ec30f6b0bf2186422d (patch)
tree8e8281eb3c03cf0d7fb1801567a126b9c7e34b12 /compiler/optimizing/loop_optimization.cc
parent50a7c38d0ec0c8e2817e782ef7f52e6f670ccb33 (diff)
Eliminate never taken loops
If a loop is never taken we can safely eliminate it. Known limitation: Due to how the loop trip calculation works, if the calculation would overflow we would get "unknown" number of trips. This could happen even for loops with 0 real iterations like `(int i = Integer.MAX_VALUE; i < Integer.MIN_VALUE; i++)`. Bug: 336236538 Fixes: 336236538 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Test: m test-art-host-gtest-art_compiler_tests64 Change-Id: I26cae89a593e3375f9f1990a47f3c8973a76364c
Diffstat (limited to 'compiler/optimizing/loop_optimization.cc')
-rw-r--r--compiler/optimizing/loop_optimization.cc9
1 files changed, 8 insertions, 1 deletions
diff --git a/compiler/optimizing/loop_optimization.cc b/compiler/optimizing/loop_optimization.cc
index 8d698d6c82..215986910b 100644
--- a/compiler/optimizing/loop_optimization.cc
+++ b/compiler/optimizing/loop_optimization.cc
@@ -1094,9 +1094,16 @@ bool HLoopOptimization::TryFullUnrolling(LoopAnalysisInfo* analysis_info, bool g
bool HLoopOptimization::TryLoopScalarOpts(LoopNode* node) {
HLoopInformation* loop_info = node->loop_info;
int64_t trip_count = LoopAnalysis::GetLoopTripCount(loop_info, &induction_range_);
+ if (trip_count == 0) {
+ // Mark the loop as dead.
+ HIf* loop_hif = loop_info->GetHeader()->GetLastInstruction()->AsIf();
+ int32_t constant = loop_info->Contains(*loop_hif->IfTrueSuccessor()) ? 0 : 1;
+ loop_hif->ReplaceInput(graph_->GetIntConstant(constant), 0u);
+ return true;
+ }
+
LoopAnalysisInfo analysis_info(loop_info);
LoopAnalysis::CalculateLoopBasicProperties(loop_info, &analysis_info, trip_count);
-
if (analysis_info.HasInstructionsPreventingScalarOpts() ||
arch_loop_helper_->IsLoopNonBeneficialForScalarOpts(&analysis_info)) {
return false;