]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix miscompilation of loop over boolean range
authorEric Botcazou <ebotcazou@adacore.com>
Wed, 25 Oct 2023 15:52:38 +0000 (17:52 +0200)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 21 Nov 2023 09:57:43 +0000 (10:57 +0100)
The optimized form generated in this case turns out to be problematic.

gcc/ada/

* gcc-interface/trans.cc (Loop_Statement_to_gnu): Always use the
simpler form for a loop with a boolean iteration variable.

gcc/ada/gcc-interface/trans.cc

index c7d91628f80c388c88d88c62bf0fad4d47cd6992..9c418beda9643827df4f8cd106f61da2a1a9344b 100644 (file)
@@ -3021,7 +3021,9 @@ Loop_Statement_to_gnu (Node_Id gnat_node)
        }
 
       /* We use two different strategies to translate the loop, depending on
-        whether optimization is enabled.
+        whether optimization is enabled, except for the very peculiar case
+        of a loop running over a boolean type where we use the simpler form
+        in order to avoid manipulating negative values in a boolean context.
 
         If it is, we generate the canonical loop form expected by the loop
         optimizer and the loop vectorizer, which is the do-while form:
@@ -3067,7 +3069,9 @@ Loop_Statement_to_gnu (Node_Id gnat_node)
 
         which works in all cases.  */
 
-      if (optimize && !optimize_debug)
+      if (optimize
+         && !optimize_debug
+         && TREE_CODE (gnu_base_type) != BOOLEAN_TYPE)
        {
          /* We can use the do-while form directly if GNU_FIRST-1 doesn't
             overflow.  */