From 08b60ea29684c526f7bfb4ec30f6b0bf2186422d Mon Sep 17 00:00:00 2001 From: Santiago Aboy Solanes Date: Fri, 24 May 2024 16:36:52 +0100 Subject: 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 --- compiler/optimizing/induction_var_range.cc | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'compiler/optimizing/induction_var_range.cc') diff --git a/compiler/optimizing/induction_var_range.cc b/compiler/optimizing/induction_var_range.cc index 8568062933..72719f89bf 100644 --- a/compiler/optimizing/induction_var_range.cc +++ b/compiler/optimizing/induction_var_range.cc @@ -384,6 +384,11 @@ bool InductionVarRange::HasKnownTripCount(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const { bool is_constant = false; CheckForFiniteAndConstantProps(loop, &is_constant, trip_count); + // Set negative trip counts as 0, since it means that no trips would happen. Note that if the + // `is_constant` value is false, `trip_count` would be disregareded. + if (*trip_count < 0) { + *trip_count = 0; + } return is_constant; } -- cgit v1.2.3-59-g8ed1b